$(document).ready(function(){

	$.post('/website.php/home/cookiesEnabled', function(j) {

		if(j==0)
		{
			$('#cookie_alert').show();
		}
		
	});
});

// toggle container and hide this
function toggleTable(container, hidethis)
{
	
	if(hidethis != undefined)
	{
		$(hidethis).hide();
	}

	if ($(container).is(':hidden')) {	
		$(container).show();		
    } else {
        $(container).hide();
    }

	return false;
	
}


// 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').html('<span class="loader_small">Loading. Please wait...</span>');	
							
		$.post('/website.php/cart/add', $('#addtocartform'+pid).serialize(), function(j) {
				
			if(j)
			{
				$('.loader_small').remove();
				$('#cart_panel').html(j);
				$('#ajax_cart_response').html('<span class="tick_response">Item added to cart</span>');	
			}
			else
			{
				$('#ajax_cart_response').html('<span class="error_notice" style="font-size: 0.9em">Select lower quantity</span>');
			}
				
		});
		
	} 	
}

function subscribeNewsletter()
{

	$('#newsletter_response').html('<span class="ajax_loading_ddd">Subscribing. Please wait....</span>');
	
	$.post('/website.php/home/newsletterSubscribe', $('#newsletter_subscribe_form').serialize(), function(j) {
		$('#newsletter_response').html(j);
	}); 
	
		
}

function submitProdEnquiry()
{	
	
	$('#prod_enquiry_response').html('<span class="ajax_loading_ddd">Sending enquiry. Please wait....</span>');
	
	$.post('/website.php/product/submitProdEnquiry', $('#prod_enquiry_form').serialize(), function(j) {
		$('#prod_enquiry_response').html(j);
	}); 		
		
}

function submitCustLogin()
{
	var randnum = Math.floor(Math.random()*10000);
		
	$('#customer_login_response').html('<span class="ajax_loading_ddd">Please wait....</span>');
	
	$.getJSON('/website.php/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 == 1)
		{			
			for (var key in j)
			{
				if(j[key] == 'empty')
				{
					$('#'+key).addClass('error_input');
				}
			}
		}
	});		
}

//Log customer out. Remove cookie 'cuid'
function custLogOut()
{
	$('#customer_login_response').html('<span class="ajax_loading_ddd">Please wait....</span>');

	$.post('/website.php/customer/logout', function(j) {
		$('#wholesaler_panel').html(j);
	});
}

function sendRecoveryEmail(string)
{
	$('#customer_login_response').html('<span class="ajax_loading_ddd">Please wait....</span>');

	$.post('/website.php/customer/sendRecoveryEmail', { hash_id: string }, function(j) {
		$('#customer_login_response').html(j);
	});
}

// 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 )
	{

		
		$('#'+next_aid+'_'+pid).html('<option value="">Updating...</option>');
		
   		$('#'+next_aid+'_'+pid).load('/website.php/product/buildList?selection='+$('#'+aid+'_'+pid).val()+'&next_aid='+next_aid, $('#addtocartform'+pid).serialize()); 
		
		document.getElementById(next_aid+'_'+pid).disabled = false;
		
	}
	else
	{
		$.post('/website.php/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;
}

//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 getAusPostage()
{
	 $('#postage_response').html('Getting postage');
	 
	 $.getJSON('/website.php/cart/getAuspostQuote', function (data){
	
		 $('#postage_response').html(data.charge);
		 
	 });


}


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;');
}


