function toggleInput() 
{
	if($('input#copy_address').is(':checked'))
	{
       $('.toggle_input').attr('disabled', true);
       $('.toggle_input').css('background', '#d5d5d5');
	}
	else
	{
		$('.toggle_input').attr('disabled', false);
		$('.toggle_input').css('background', '#fff');
		$('.toggle_input').removeClass('disabled_input');
	}
} 






// Show/hide cart dropdown
function showHideCart(selector) {
		
	if ($('#cart_dropdown').is(':hidden'))
	{
		$('#cart_dropdown').slideDown("fast");
		document.getElementById(selector).innerHTML = 'Hide cart';
	}
	else 
	{
		$('#cart_dropdown').slideUp("fast");
		document.getElementById(selector).innerHTML = '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)
	{
		var cartcontent = $('#cart').html();
		
		
		$('#ajax_cart_response').append('<div class="loader_small"></div>');
		$('#cart').html('<div class="loader_small">Updating cart</div>');
		
		
		$.post('/website.php/cart/add', $('#addtocartform'+pid).serialize(), function(j) 
		{		
			if(j)
			{
				$('.loader_small').remove();
				$('#cart').html(j);
				$('#ajax_cart_response'+pid).html('<span style="font-weight:bold;font-size: 0.9em">This has been added to your cart</span>');
			}
			else
			{
				$('#cart').html(cartcontent);
				$('#ajax_cart_response'+pid).html('<span class="error_notice" style="font-size: 0.9em">Select lower quantity</span>');
			}
		});
	}
} 

function subscribeNewsletter()
{
	var email = $('#emailNewsletter').val();

	if(checkEmail(email))
	{

		var randnum = Math.floor(Math.random()*10000);
		
		$('#newsletter_response').html('');
		$('#newsletter_response').append('<div class="loader_small">Please wait</div>');
		$('#newsletter_response').load('/home/newsletterSubscribe?email='+email+'&rand='+randnum); 
		
		$().ajaxStop(function(){		
			$('.loader_small').remove();
		});
	}		
	
}


// Load product attributes available based on selection // NOT CURRENTLY BEING USED
function findStyle(pid, aid, aid_list, aname_list)
{

	var next_aid;

	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 urltopost = "/website.php/product/buildList?pid="+pid+"&selection="+$('#'+aid+'_'+pid).val()+"&next_aid="+next_aid; 
		
		$('#'+next_aid+'_'+pid).html('<option value="">Updating...</option>');		
   		$('#'+next_aid+'_'+pid).load(urltopost, $('#addtocartform'+pid).serialize()); 
		
		document.getElementById(next_aid+'_'+pid).disabled = false;
		
	}
	else
	{
		
		var urltopost = "/website.php/product/buildList?pid="+pid+"&selection="+$('#'+aid+'_'+pid).val();
		
		$.post(urltopost, $('#addtocartform'+pid).serialize(), function(j){			
		//	$().ajaxStop(function() {				
				//$('#style_input'+pid).attr("value", j);	
			$('#style_input'+pid).remove();
			$('#addtocartform'+pid).append(j);
		//	});
			
		});

	}
	
	
	return false;
}

//toggle in/out pics used on homepage
function toggle(category, selector) {
		
	$('.' + category + '_pic').hide();				
	$('#' + category + '_' + selector).show();

}

//toggle in/out pics & links used on category listings
function toggleCategory(container1, container2, selector) {
		
	$('.' + container1).hide();			
	$('.' + container2).hide();				
	$('#' + container1 + '_' + selector).show();	
	$('#' + container2 + '_' + selector).show();

}

// toggle pics/thumbs, hides thumb shown (used in 'website/product/productShowSuccess.php')
function togglehidethumb(category, pic_id) {
	
	$('img.' + category + '_pic').hide();
	$('img#' + category + '_' + pic_id).show();
	$('img.' + category + '_thumb').fadeIn();
	$('img#' + pic_id + '_thumb').hide();
	
	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 checkTermsAccepted()
{
	if ($('#termsandconditions:checked').val()) {
		  return true;
	}
	else
	{
		alert("You must read and accept the Terms & Conditions of Purchase to complete payment");
		return false;
	}
}


function loginWholesaler()
{
	var randnum = Math.floor(Math.random()*10000);
	
	$('#wholesaler_login_response').html(''); 	
	
	$.getJSON('/website.php/home/wholesalerLogin?rand='+randnum, $('#wholesaler_login_form').serialize(), function(j) {
	
		if(j.error_type)
		{
			$('#wholesaler_login_response').html(j.error_msg); 			
		}
		else
		{	
			$('#wholesaler_login_response').html('<a href="http://musq.com.au/wholesaler/intro">&gt;&gt; Go to wholesale stock</a>');
			window.location = 'http://musq.com.au/wholesaler/intro';
		}
		
	});
}

function recoverWholesaler()
{
	var randnum = Math.floor(Math.random()*10000);

	$.getJSON('/website.php/home/wholesalerRecover?rand='+randnum, $('#wholesaler_recover_form').serialize(), function(j) {
						
		$('#wholesaler_recover_response').html(j.error_msg);	
		
	});
	
}

function showWholesalerPanel()
{
	$('#wholesaler_recover').hide(function () { $('#wholesaler_login').hide(function () { $('#wholesaler_panel').show(); }); });
}
function showWholesalerLogin()
{
	$('#wholesaler_panel').hide(function () { $('#wholesaler_login').show(function(){ $('#username').focus(); }); });
}
function showWholesalerRecovery()
{
	$('#wholesaler_login').hide(function () { $('#wholesaler_recover').show(function(){ $('#wholesaler_email').focus(); }); });
}