// toggle container and hide this
function toggle(container, hidethis)
{
	
	if(hidethis != undefined)
	{
		$(hidethis).hide();
	}

	if ($(container).is(':hidden')) {	
		$(container).show();		
    } else {
        $(container).hide();
    }

	return false;
	
}

// Focus cursor on form element
function formFocus(selector)
{
	if($(selector).val() == '')
	{
		$(selector).focus();
	}
}

// Show/hide cart dropdown
function showHideCart(selector) {
		
	if ($('#cart_dropdown').is(':hidden'))
	{
		$('#cart_dropdown').slideDown("fast");
		$('#'+selector).html('Hide cart');
	}
	else 
	{
		$('#cart_dropdown').slideUp("fast");
		$('#'+selector).html('View cart');
	}
	
}

function addtocart(pid, aid_list, aname_list)
{
	
	var flag = true;
	for (var i=0; i<aid_list.length; i++)
	{
		if($('#'+aid_list[i]+'_'+pid).val() == '')
		{
			alert('Please select a '+ aname_list[i] );
			flag = false;
			break;
		}
		
	}
	
	if (flag)
	{		
		
		
		$('#ajax_cart_response'+pid).html('<span class="loader_small">Adding item...</span>');	
		
		//$('#cart_panel').html('<span class="loader_small">Updating cart</span>');
					
		$.post('/cart/add', $('#addtocartform'+pid).serialize(), function(j) {
				
			if(j)
			{
				$('.loader_small').remove();
				$('#cart_panel').html(j);
				$('#ajax_cart_response'+pid).html('<span class="tick_response">Item added to cart</span>');
			}
			else
			{
				$('#ajax_cart_response'+pid).html('<span class="cross_response">Select lower quantity</span>');
			}
				
		});

		
	} 	
}

function subscribeNewsletter()
{

	$('#newsletter_response').html('<span class="ajax_loading_ddd">Subscribing. Please wait....</span>');
	
	$.post('/home/newsletterSubscribe', $('#newsletter_subscribe_form').serialize(), function(j) {
		$('#newsletter_response').html(j);
	}); 
	
		
}

function submitCustEnquiry()
{		
	
	$('#customer_enquiry_response').html('<span class="ajax_loading">Sending enquiry. Please wait....</span>');
	
	$.getJSON('/customer/submitCustEnquiry', $('#customer_enquiry_form').serialize(), function(j) {
		
		if(j.error_type)
		{
			$('#customer_enquiry_response').html('<span class="error_notice">'+j.error_msg+'</span>');
		}
		else
		{
			$('#customer_enquiry_response').html(j.error_msg);
			$('#name').val('');
			$('#email').val('');
			$('#phone').val('');
			$('#message').val('');			
		}
		
	}); 				
		
}

function submitCustLogin()
{
	var randnum = Math.floor(Math.random()*10000);
		
	$('#customer_login_response').html('<span class="ajax_loading">Please wait....</span>');
	
	$.getJSON('/customer/validateLogin?ran='+randnum, $('#customer_login_form').serialize(), function(j) {
		if(j.error_type)
		{
			$('#customer_login_response').html(j.error_msg);
		}
		else
		{
			$('#wholesaler_panel').html(j.response);
		}
	}); 			
		
}

function submitCustReg()
{
	var randnum = Math.floor(Math.random()*10000);
					
	$('#customer_register_response').html('<span class="ajax_loading">Please wait....</span>');
	
	$('.error_input').removeClass('error_input');
	
	$.getJSON('/customer/register?ran='+randnum, $('#customer_register_form').serialize(), function(j) {		
		$('#customer_register_response').html(j.error_msg);
		
		if(j.error_type)
		{			
			for (var key in j)
			{
				if(j[key] == 'empty')
				{
					$('#'+key).addClass('error_input');
				}
			}
		}
	});		
}

function custLogOut()
{
	$('#customer_login_response').html('<span class="ajax_loading_ddd">Please wait....</span>');

	$.post('/customer/logout', function(j) {
		$('#wholesaler_panel').html(j);
	});
}

// Load product attributes available based on selection // NOT CURRENTLY BEING USED
function findStyle(pid, aid, aid_list, aname_list)
{

	var next_aid;

	// Disable select boxes 
	for (var i=0; i<aid_list.length; i++)
	{
		if(aid_list[i] == aid && (aid_list[i+1] != undefined))
		{
			next_aid = aid_list[i+1];
			
			// if a selection is made, make sure select boxes following the next select box are disabled
			for (var i=i+1; i<aid_list.length; i++)
			{
				if(aid_list[i+1] != undefined)
				{
					document.getElementById(aid_list[i+1]+'_'+pid).disabled = true;		
					$('#'+aid_list[i+1]+'_'+pid).html('<option value="">Select '+aname_list[i+1]+'</option>');				
				}				
			}
			break;
		}
		else
			next_aid = false;
	}

	if ( $('#'+aid+'_'+pid).val() == '') {

		var found = false;
		for( var i=0; i<aid_list.length; i++)
		{
			if (found)
			{
				$('#'+aid_list[i]+'_'+pid).html('<option value="">Select '+aname_list[i]+'</option>');
				document.getElementById(aid_list[i]+'_'+pid).disabled = true;	
			}
			
			if(aid_list[i] == aid)
			{
				found = true;
			}
			
		}
	}
	else if( next_aid != false )
	{

		var randnum = Math.floor(Math.random()*10000);
		
		$('#'+next_aid+'_'+pid).html('<option value="">Updating...</option>');
		
   		$('#'+next_aid+'_'+pid).load('/product/buildList?selection='+$('#'+aid+'_'+pid).val()+'&next_aid='+next_aid+'&ran='+randnum, $('#addtocartform'+pid).serialize()); 
		
		document.getElementById(next_aid+'_'+pid).disabled = false;
		
	}
	else
	{
		$.post('/product/buildList?pid='+pid+'&selection='+$('#'+aid+'_'+pid).val(),$('#addtocartform'+pid).serialize(), function(j){
				//$('#style_input'+pid).attr("value", j);	
			$('#style_input'+pid).remove();
			$('#addtocartform'+pid).append(j);
			
		});

	}
	
	return false;
}



function checkEmail(email) 
{
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		alert('Please provide a valid email address');
		return false;
	}
	else
	{
		return true;
	}
}


function changeVideo(video_name, selector)
{
	$('.video_info_container').hide();
	$('#video_info_container'+selector).fadeIn('fast');
	player = document.getElementById('mpl');
	player.sendEvent('LOAD', { file:'/videos/'+video_name } );
	player.sendEvent('PLAY', 'false');						
}

function showModalContent(contentid)
{
	var thisboxy = null;
	$('#content_boxy').remove();
	contentboxy = new Boxy('<div id="content_boxy"><div style="float: left; height: 350px; padding-top: 150px; width: 700px; text-align:center"><span class="ajax_loading_white">Loading. Please wait..</span></div></div>', { title: true, modal: true });

	contentboxy.setTitle('<span style="padding: 0 10px"></span>');
	$('#content_boxy').load('/website.php/home/modal', { id : contentid });
	
}

function updateShipping(total)
{
	var new_total = total + parseFloat($('#shipping_method option:selected').attr('id'));

	$('#total_price').html('$'+new_total.toFixed(2) + '&nbsp;');
}
