function conlog(t){
	if(window.console && window.console.log){
		window.console.log(t);
	}
}

function InfoMsg(txt,title){
	if(title=='' || title=='undefined' || title==undefined){
		title = "Belgique Alert";
	}
	$('#dialog').html('<br>'+txt+'<br>');
	$('#dialog').dialog({
		modal: true,
		show: "fade",
		hide: "fade",
		title: title,
		width: '650px',
		buttons: {
			Ok: function() {
				$( this ).dialog( "close" );
			}
		}
	})
}

function LInfoMsg(txt,title){
	if(title=='' || title=='undefined' || title==undefined){
		title = "Belgique Alert";
	}
	$('#dialog').html('<br>'+txt+'<br>');
	$('#dialog').dialog({
		modal: true,
		show: "fade",
		hide: "fade",
		title: title,
		width: '980px',
		heigth: '620px',
		buttons: {
			Ok: function() {
				$( this ).dialog( "close" );
			}
		}
	})
}

function EqualHeights(){
	$('.equal').equalHeights();
}

function WaitEqualHeights(){
	var t=setTimeout("EqualHeights",1500);
}

function ShowCat(id){
	$('#mainmenu').css('height','');
	if($('.'+id).is(':visible')){
		$('.'+id).hide(function(){
			$('#mainmenu').css('height','');	
			$('.equal').equalHeights();
		});
	} else {
		$('.'+id).slideDown(function(){
			$('.equal').equalHeights();
		});
	}
	WaitEqualHeights()
}

function ShowShops(){
	if($('.shop').is(':visible')){
		$('.shop').hide(function(){
			$('.equal').equalHeights();
		})
	} else {
		$('.shop').slideDown(function(){
			$('.equal').equalHeights();
		})
	}
}


function SelectSize(id,size,price){
	$('.pmsize').removeClass('selected');
	$('#pmods_size_'+id).addClass('selected');
	$('#module_size').val(size);
	$('#productprice').val(price);
}

function SelectAlc(val){
	$('.pmalc').removeClass('selected');
	$('#pmods_alc_'+val).addClass('selected');
	$('#module_alc').val(val);
}

function SelectNoMess(){
	$('.pmmess').removeClass('selected');
	$('#pmods_mess_No').addClass('selected');
	$('#module_mess').val('No');
	$('#msginput').hide();
	$('.equal').equalHeights();
}

function ShowMessBox(){
	$('.pmmess').removeClass('selected');
	$('#pmods_mess_Yes').addClass('selected');
	$('#module_mess').val('Yes');
	$('#msginput').show();
	$('.equal').equalHeights();
}

function AddProductToCart(){
	var err = '';
	if($('#module_size')){
		if($('#module_size').val()==''){
			err += '- select the size of the product<br>'
		}
	}
	
	if($('#module_mess')){
		if($('#module_mess').val()==''){
			err += '- select if you need the personal message on the cake<br>'
		} else if($('#module_mess').val()=='Yes'){
			if($('#module_mess_txt').val()==''){
				err += '- enter the personal message you would like on the cake<br>'
			}
		}
	}
	
	if($('#module_alc')){
		if($('#module_alc').val()==''){
			err += '- select if you would like any alcohol in the cake<br>'
		}
	}
	
	if(err!=''){
		InfoMsg('Before adding the product to the basket, please complete the following:<br><br>'+err);
	} else {
		$('#ProdsBackup').val($('#container').html());
		$('#addtocartform').submit();
	}
}

function PrintProduct(){
	window.print();
	return false;
}

function SendToFriend(pid){
	var email = prompt('Please enter email to send the product information to');
	if(email){
		var regem = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if (email == '' ||  regem.test(email) == false){
			InfoMsg("Please enter a valid email address to send the product information to!");
		} else {
			var qurl = "action=sendtofriend&email="+email+"&pid="+pid;
			$.ajax({
				type: "POST",
				url: "/redir.php",
				data: qurl,
				success: function(msg){
					if (msg == "OK"){
							InfoMsg("Email with link to this page has ben sent to the address specified.<br /><br />Thank you for recommending our website.");
					} else {
							InfoMsg("There was an error while sending product information email. Please try again!");
					}
				}
			});
		}
	} else {
		InfoMsg("Please enter an email address to send the product information to!");
	}
}

function UpdateQuan(op,inp){
	var val = $('#quality_'+inp).val();
	if(op == 'add'){
		val++;
	} else if(op == 'rem'){
		val--;
	}
	if(val<0){
		val = 0;
	}
	if(val == '0'){
		if(confirm("This action will delete this product and any additional products from the cart.\nAre your sure you want to continue?")){
			$('#quality_'+inp).val(val);
			$('#fualform_'+inp).submit();
		}
	} else {	
		$('#quality_'+inp).val(val);
		$('#fualform_'+inp).submit();
	}
}

function UpdateAddQuan(op,inp){
	var val = $('#addprodquality_'+inp).val();
	if(op == 'add'){
		val++;
	} else if(op == 'rem'){
		val--;
	}
	if(val<0){
		val = 0;
	}
	if(val == '0'){
		if(confirm("This action will delete this additional product from the cart.\nAre your sure you want to continue?")){
			$('#addprodquality_'+inp).val(val);
			$('#addprodupform_'+inp).submit();
		}
	} else {	
		$('#addprodquality_'+inp).val(val);
		$('#addprodupform_'+inp).submit();
	}
}

function ClearCart(){
	if(confirm("Are you sure you want to clear your cart?")){
		$('#clearcartform').submit();
	}
}

function CheckOut(cnt){	
	if(cnt>0){
		if(confirm("Have you added all of your products and want to proceed to checkout? ")){
			window.location.href = "/checkout/";
		}
	} else {
		InfoMsg("You do not have any products in your cart yet");
	}
}

function UpdAddProdSize(apid,tpe){
	var val = $('#adprod_'+apid).val();
	if(tpe == 'add'){
		val++;
	} else if(tpe == 'sub'){
		val--;
	}
	if(val<0){
		val = 0;
	}
	if(val == '0'){
		$('#adprod_'+apid).val(val);
	} else {	
		$('#adprod_'+apid).val(val);
	}

	var price = $('#adprodprice_'+apid).val();
	var total = val * price;
	$('#adprodstot_'+apid).html(total.toFixed(2));
}

/* ################################################ LOGIN/REGISTER PAGES */

function Register (){
	var mail = $('#registermail').val();
	var regem = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(mail == ''){
		InfoMsg("Please enter your email address to register");
	} else if(regem.test(mail) == false) {
		InfoMsg("Please enter valid email address!");
	} else {	
		var qurl = "act=regcheck&address="+mail;
		$.ajax({
		    type: "POST",
		    url: "/redir.php",
		    data: qurl,
		    success: function(msg){
				if (msg == "OK"){
					$('#email').val(mail);
					$('#regemail').hide();
					$('#login').hide();
					$('#regdetails').show();
				} else if (msg == "NOTOK"){
					InfoMsg("This email has already been registered with us.<br><br>If you wish to recover your password, please use the password reminder service on the right.");
				} else if (msg == "QERROR"){
					InfoMsg("There was a problem updating the database. Please let us know at info@belgique.co.uk");
				} else if (msg == "MQERROR"){
					InfoMsg("There was a problem with sending an email to you. Please let us know at info@belgique.co.uk");
				} else {
					InfoMsg("There was a problem communicating with the server!");
				}
			} 
		});
	}
}

function ShowRegLogPanel(){	
	$('#regdetails').hide();
	$('#regemail').show();
	$('#login').show();
}

function Login(){
	var mail = $('#loginmail').val();
	var pass = $('#loginpass').val();
	var errors = '';
	var regem = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(mail==''){
		errors += ' - No email address entered<br>';
	} else if(regem.test(mail) == false){
		errors += ' - Email address specified is not valid<br>';
	}
	if(pass==''){
		errors += ' - No password entered<br>';
	}
	if (errors!=''){ 
		InfoMsg("Following errors occured while trying to log in:<br><br>"+errors);
	} else {
		$('#loginform').submit();
	}
}

function RegisterNewAccount(){
	var regem = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var errors = '';
	var title = $('#title :selected').text();
	var mail = $('#email').val();
	
	if (title == 'please select'){ 
		$('#title').addClass("error");
		errors += ' - Please select a title<br>'; 
	} else { 
		$('#title').removeClass("error");
	}
		
	if ( $('#fname').val().length < 1 ){ 
		$('#fname').addClass("error");
		errors += ' - Please enter your first name<br>';
	} else { 
		$('#fname').removeClass("error");
	}
	
	if ( $('#lname').val().length < 1 ){ 
		$('#lname').addClass("error"); 
		errors += ' - Please enter your last name<br>';
	} else { 
		$('#lname').removeClass("error"); 
	}
		
	if ( mail == '') { 
		$('#email').addClass("error");
		errors += ' - Please enter your email<br>';
	} else if ( regem.test(mail) == false) { 
		$('#email').addClass("error");
		errors += ' - Please enter valid email<br>';
	} else { 
		$('#email').removeClass("error");
	}
		
	if ( $('#pass1').val().length > 5 && $('#pass2').val().length > 5 && $('#pass1').val() == $('#pass2').val()){ 
		$('#pass1').removeClass("error");
		$('#pass2').removeClass("error");
	} else { 
		if($('#pass1').val().length < 6){
			errors += " - Password must be at least 6 characters long.<br>";
		}
		if($('#pass1').val() != $('#pass2').val()){
			errors += " - Password and password confirmation fields must match.<br>";
		}
		$('#pass1').addClass("error");
		$('#pass2').addClass("error");
	}
	
	if (errors != ''){
		InfoMsg("Please fill in highlighted fields<br><br>"+errors);
	} else {
		$('#registerform').submit();
	}
}

function ResetPassword(){
	$('#dialog2').html('<br>'+$('#changepassword').html()+'<br>');
	$('#dialog2').dialog({
		modal: true,
		show: "fade",
		hide: "fade",
		title: "Password reminder",
		width: '345px',
		buttons: {
			Close: function() {
				$( this ).dialog( "close" );
			}
		}
	})
}

function ResetSend(){
	var mail = $('.ui-dialog #resetemail').val()
	var regem = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(mail==''){
		InfoMsg("Please enter your email address!");
	} else if(regem.test(mail) == false){
		InfoMsg("Please enter valid email address!");
	} else {
		var qurl = "act=passwordreset&address="+mail;
		$.ajax({
		    type: "POST",
		    url: "/redir.php",
		    data: qurl,
		    success: function(msg){
				if (msg == "OK"){
					$('#dialog2').dialog("close");
					InfoMsg("Your password has been reset. Please check your email.");
				} else if (msg == "NOEMAIL"){
					InfoMsg("There is no user account created for this email address!");
				} else {
					InfoMsg("There was a problem communicating with the server!");
				}
			} 
		});
	}
}

function SelectCollectionStore(sid,sname){
	$('.collectionshop').removeClass('active');
	$('#collectionshop_'+sid).addClass('active');
	$('#collectionstoreid').val(sid);
	$('#collectionstorename').val(sname);
	populateCaledar();
}

function SelectCollectionTimeSlot(tsid,sname){
	$('.collectionts').removeClass('active');
	$('#collectionts_'+tsid).addClass('active');
	$('#collectionstoretimeslot').val(sname)
}

function CollectionDateSelected(){
}

function SetDCToColl(){
	ResetDeliveryCharge();
	$('.dcoptdiv').hide();
	$('#collection').show();
	$('#dctype').val('col');
}

function SetDCToDel(){
	ResetDeliveryCharge();
	$('.dcoptdiv').hide();
	$('#delivery').show();
	$('#dctype').val('del');
	$('#DeliveryDate').val('');
}
	
function SetDCToCollAtTheEvent(){
	ResetDeliveryCharge();
	$('.dcoptdiv').hide();
	$('#collectionattheevent').show();
	$('#dctype').val('catv');
}
	
function ConfirmPay(){
	var errors = '';
	if($('#dctype').val() == 'del' && $('#deliverycharge').val() == '' ){
		if($('#postcode').val() == ''){
			errors += ' - Please enter a postcode for delivery<br>';
		}
		if(!isValidPostcode($('#postcode').val())){
			errors += ' - Please enter a valid postcode<br>';
		}
		if($('#DeliveryDate').val() == ''){
			errors += ' - Please select collection date<br>';
		}
	} else {
		if($('#title').val() == ''){
			errors += ' - Please select your title<br>';
		}
		if($('#fname').val() == ''){
			errors += ' - Please enter your first name<br>';
		}
		if($('#lname').val() == ''){
			errors += ' - Please enter your last name<br>';
		}
		if($('#phone').val() == ''){
			errors += ' - Please enter your phone<br>';
		}
		if($('#email').val() == ''){
			errors += ' - Please enter your email<br>';
		}
		if($('#dctype').val() == ''){
			errors += ' - Please select collection/delivery options<br>';
		} else {
			if($('#dctype').val() == 'col'){
				if($('#collectionstoreid').val() == ''){
					errors += ' - Please select a collection store<br>';
				}
				if($('#collectionstoredate').val() == ''){
					errors += ' - Please select collection date<br>';
				}
				if($('#collectionstoretimeslot').val() == ''){
					errors += ' - Please select collection time slot<br>';
				}
				
				if($('#coladdr1').length == 1){
					if($('#coladdr1').val() == ''){
						errors += ' - Please enter first line of your billing address<br>';
					}
					if($('#coltown').val() == ''){
						errors += ' - Please enter billing address town<br>';
					}
					if($('#colpostcode').val() == ''){
						errors += ' - Please enter billing address postcode<br>';
					}
					if($('#colcountry').val() == ''){
						errors += ' - Please enter billing address country<br>';
					}
				}
			} else if($('#dctype').val() == 'del'){
				if($('#addr1').val() == ''){
					errors += ' - Please enter first line of your address<br>';
				}
				if($('#town').val() == ''){
					errors += ' - Please enter a delivery town<br>';
				}
				if($('#postcode').val() == ''){
					errors += ' - Please enter a postcode for delivery<br>';
				if(!isValidPostcode($('#postcode').val())){
					errors += ' - Please enter a valid postcode<br>';
				}
				} else if($('#deliverypostcodeok').val() == ''){
					errors += " - We do not currently offer delivery in the postcode. Please use different address or collect your products from one of our stores<br>";
				}
				if($('#DeliveryDate').val() == ''){
					errors += ' - Please select collection date<br>';
				}
			}
		}
	}
	if(errors == ''){	
		$('#orderform').submit();
	} else {
		InfoMsg("Please fill in all required fields<br><br>"+errors);
	}
}

function is_array(input){
    return typeof(input)=='object'&&(input instanceof Array);
}

function is_numeric(value) {
  if (value != null && !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}

function isValidPostcode(p) {
	var postcodeRegEx = /[A-Z]{1,2}[A-Z0-9]{1,2} ?[0-9][A-Z]{2}/i;
	return postcodeRegEx.test(p);
}

function IsPostcodeAvailale(day){
	var carrier = $('#carrier').val();
	if(isValidPostcode($('#postcode').val())){
		if(carrier == 'cdel'){
			$('#deliverypostcodeok').val('ok');
			CalculateDeliveryFee(day);
		} else {
			var qurl = "action=postcodecheck&tp=beldel&pc="+$('#postcode').val()
			$.ajax({
				type: "POST",
				url: "/check.php",
				data: qurl,
				success: function(msg){
					if (msg == "OK"){
						$('#deliverypostcodeok').val('ok');
						CalculateDeliveryFee(day);
					} else {
						$('#DeliveryDate').val('');
						$('#deliverypostcodeok').val('');
						InfoMsg('Unfortunately we do not currently offer delivery of this product for entered postcode.<br><br>Please use different address or collect your products from one of our stores.<br/><br/>Please check <a href="/p/5-collection_and_delivery/" target="_blank">Collection &amp; Delivery</a> page for detailed information about our delivery service.');
					}
				}
			});
		}
	} else {
		InfoMsg("Please enter a valid postcode!");
	}
}

function CalculateDeliveryFee(day){
	var date = $('#DeliveryDate').val();
	var qurl = "check=deliverycharge&date="+date+"&day="+day+"&pc="+escape($('#postcode').val());
	$.ajax({
		type: "GET",
		url: "/check.php",
		data: qurl,
		success: function(msg){
			if (msg != "FAIL"){
				if(is_numeric(msg)){
					$('#deliverycharge').val(msg);
					$('#delcharge').html("&pound;"+msg);
					RefreshShortCart();
					if(msg=='0.00'){
						InfoMsg("There will be no charge for delivery at selected date!");
					} else {
						var deltofifty = '';
						if( $('#delto50').val() > 0 ){
							deltofifty = 'You need to spend additional &pound;'+$('#delto50').val()+' to qualify for free weekday delivery.<br><br><strong>';
						}
						InfoMsg('Delivery charge of &pound;'+msg+' has been added to your order.<br><br>'+deltofifty+'Free weekday delivery or discounted Saturday is available for orders above &pound;50.</strong><br/><br/>Please check <a href="/p/5-collection_and_delivery/" target="_blank">Collection &amp; Delivery</a> page for detailed information about our delivery service.');
					}
				} else if(msg=='NMPCSAT'){
					$('#DeliveryDate').val('');
					$('#deliverypostcodeok').val('');
					InfoMsg("We're sorry but Saturday delivery is not available for non-mainland UK postcode!");
				}
			}
		}
	});
}

function ResetDeliveryCharge(){
	$('#deliverycharge').val('');
	$('#delcharge').html("&pound;0");
	$.ajax({
	type: "GET",
	url: "/check.php?check=resetdeliverycharge",
	success: function(msg){
			RefreshShortCart();
		}
	});
}

function PayProtex(){
	$('#protx_form').submit();
}

function UpdateAccDetails(){
	var regem = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var reqfields = 4;
	var title = $('#title :selected').text();
	var mail = $('#email').val();
	
	if (title == 'please select'){ 
		$('#title').css('color','#ff0000'); 
	} else { 
		$('#title').css('color','#cccccc'); reqfields = reqfields - 1; 
	}
	if ( $('#fname').val().length < 1 ){ 
		$('#fname').css("border", "1px solid #ff0000"); 
	} else { 
		$('#fname').css("border", "1px solid #424242");  reqfields = reqfields - 1; 
	}
	if ( $('#lname').val().length < 1 ){ 
		$('#lname').css("border", "1px solid #ff0000"); 
	} else { 
		$('#lname').css("border", "1px solid #424242");  reqfields = reqfields - 1; 
	}
	if ( regem.test(mail) == false) { 
		$('#email').css("border", "1px solid #ff0000"); 
	} else { 
		$('#email').css("border", "1px solid #424242");  reqfields = reqfields - 1; 
	}
	if ( reqfields > 0 ){
		str = 'Please fill all fields marked red. '
		$('#upeghlp2').css('color','#ff0000');
		$('#upeghlp2').html(str);
	} else {
		$('#updateform').submit();
	}
}

	
function UpdatePass(){
	var cpass = false;
	var npass = false;
	var str = '';
	if ( $('#cpass').val().length > 5 ) { 
		$('#cpass').css("border", "1px solid #424242"); cpass=true; 
	} else { 
		$('#cpass').css("border", "1px solid #ff0000"); 
	}	
	if ( $('#pass1').val().length > 5 && $('#pass2').val().length > 5 && $('#pass1').val() == $('#pass2').val()) { 
		$('#pass1').css("border", "1px solid #424242"); $('#pass2').css("border", "1px solid #424242"); npass=true; 
	} else { 
		$('#pass1').css("border", "1px solid #ff0000"); 
		$('#pass2').css("border", "1px solid #ff0000"); }
	if ( !cpass || !npass){
		if (!cpass) str = str +'Please fill all fields marked red. ';
		if (!npass) str = str +'Password and confirm password fields must be equal and contain at least 6 characters.';
		$('#upeghlp2').css('color','#ff0000');
		$('#upeghlp2').html(str);
	} else {
		$('#changepass').submit();
	}
}

function UpdateAddrDetails(){
	var reqfields = 4;
	var country = $('#country :selected').text();
	
	if ( $('#addr1').val().length < 4 ){ 
		$('#addr1').css("border", "1px solid #ff0000"); 
	} else { 
		$('#addr1').css("border", "1px solid #424242");  reqfields = reqfields - 1; 
		}
	if ( $('#town').val().length < 1 ){ 
		$('#town').css("border", "1px solid #ff0000"); 
	} else { 
		$('#town').css("border", "1px solid #424242");  reqfields = reqfields - 1; 
	}
	
	if ( $('#postcode').val().length < 1 ){ 
		$('#postcode').css("border", "1px solid #ff0000"); 
	} else { 
		$('#postcode').css("border", "1px solid #424242");  reqfields = reqfields - 1; 
	}
	
	if (country == 'please select'){ 
		$('#country').css('color','#ff0000'); 
	} else { 
		$('#country').css('color','#cccccc'); reqfields = reqfields - 1; 
	}

	if ( reqfields > 0 ){
		str = 'Please fill all fields marked red.'
		$('#upeghlp2').css('color','#ff0000');
		$('#upeghlp2').html(str);
	} else {
		$('#billingaddrform').submit();
	}
}

function CheckPromoCode(){
	var code = $('#promocode').val();
	if(code == ''){
		InfoMsg("Please enter the promo code first!");
	} else {
		var qurl = "act=checkoffer&code="+code;
		$.ajax({
			type: "POST",
			url: "/checkoffer.php",
			data: qurl,
			success: function(msg){
				RefreshShortCart();
				InfoMsg(msg);
			}
		});		
	}
}

function RefreshShortCart(){
	$.ajax({
		type: "GET",
		url: "/basket_short.php",
		success: function(msg){
			$('#productright').html(unescape(msg));
		}
	});		
}

function NLSubmit(){
	var regem = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if ( $('#email').val() == '' ||  regem.test($('#email').val()) == false){
		InfoMsg("Please enter a valid email address to subscribe to our newsletter","Belgique Newsletter");
	} else {
		$('#nlsignup').submit();
	}
}

function NewsletterClear(){
	$('#email').val('');
}

function ContactClear(){
	$('#cfname, #cfemail, #cftel, #cfmess').val('');
}

function ContactSubmit(){
	var regem = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var errors = '';
	
	if ( $('#cfname').val() == ''){
		errors += ' - Please enter your name<br />';
	}
		
	if ( $('#cfemail').val() == '' ||  regem.test($('#cfemail').val()) == false){
		errors += ' - Please enter valid email address<br />';
	}

	if ( $('#cfmess').val() == ''){
		errors += ' - Please enter your message<br />';
	}
		
	if (errors == ''){
	
		var query = 'action=contactrequest&cfname='+$('#cfname').val()+'&cfemail='+$('#cfemail').val()+'&cftel='+$('#cftel').val()+'&cfmess='+$('#cfmess').val();
		$.ajax({ type: "POST", url: "/redir.php", data: query, success: function(msg){
			if (msg == 'OK'){ 
				ContactClear();
				InfoMsg("Your message has been sent!<br />Thank you for contacting Belgique.","Belgique");
			} else {
				InfoMsg("There was an error delivering your message!<br />Please contact us directly on info@belgique.co.uk","Belgique");
			}
		}})
	} else {
		InfoMsg("Please fill all required fields. <br /><br />"+errors,"Belgique Newsletter");
	}
}

function ShowDelInfo(type){
	var bid;
	if(type == 'col'){
		bid = '9'
	} else if(type == 'bdel'){
		bid = '10'
	} else if(type == 'cdel'){
		bid = '11'
	} 
	$.ajax({
		type: "GET",
		url: "/check.php?action=gettextblock&bid="+bid,
		success: function(msg){
			LInfoMsg(unescape(msg),'Belgique Info');
		}
	});
}

function SearchNearestPostcode(){
	if($('#searchpostcodef').val() == 'enter postcode'){
		$('#searchpostcodef').val('');
	}
}

function SearchNearest(){
	if($('#searchpostcodef').val() == 'enter postcode' || $('#searchpostcodef').val() == ''){
		InfoMsg('Please enter your postcode','Belgique Alert');
	} else if (!isValidPostcode($('#searchpostcodef').val())){
		InfoMsg('Please enter valid postcode','Belgique Alert');
	} else {
		$('#pcsearchform').submit();
	}
} 

function RefreshTopCart(){
	$.ajax({
		type: "GET",
		url: "/redir.php?action=refreshtopcart",
		success: function(msg){
			if(msg != 'FAIL'){
				$('#lboxb').html(unescape(msg));
				iMsg('Your Cart has been updated');
			}
		}
	});		
}

function AddItemToCart(pid){
	var quan = prompt('Please confirm the quantity of the product (numbers only)','1');
	if(is_numeric(quan) && quan > 0 && quan < 1000){
		var qurl = "action=addprodtocart&quan="+quan+"&pid="+pid;
		$.ajax({
			type: "POST",
			url: "/redir.php",
			data: qurl,
			success: function(msg){
				if (msg == "OK"){
						RefreshTopCart();
				} else {
						InfoMsg("There was an error while adding product to your cart. Please try again or try to refresh the page!");
				}
			}
		});
	} else if(quan){
		InfoMsg("Please enter valid number quantity (1-999)!");
	}
}

var msgTimer;
var msgTimerOn 	= false;
var messagesQ = Array();

function eMsg(msg){
	messagesQ.push("0::=::"+msg);
	Msg();
}

function wMsg(msg){
	messagesQ.push("1::=::"+msg);
	Msg();
}

function iMsg(msg){
	messagesQ.push("2::=::"+msg);
	Msg();
}

function Msg(){
	if(msgTimerOn == false){
		if(is_array(messagesQ) && messagesQ.length>0){
			var msgArr = messagesQ[0].split("::=::");
			if(msgArr.length == 2){
				msgTimerOn = true;
				stat = msgArr[0];
				msg = msgArr[1];
				if (stat == 0){
					$('#infodiv').removeClass("WarnMsg InfoMsg").addClass('ErrorMsg');
				} else if (stat == 1){
					$('#infodiv').removeClass("ErrorMsg InfoMsg").addClass('WarnMsg');
				} else if (stat == 2){
					$('#infodiv').removeClass("WarnMsg ErrorMsg").addClass('InfoMsg');
				}
				$('#infodiv').html(msg+'<div class="msgx" onclick="javascript:MsgHide();">close [X]</div>');
				$('#infodiv').fadeIn(500,function(){
					msgTimer = setTimeout("MsgClx()",3000);	
				})
			}
		}
	}
}

function MsgClx(){
	if(msgTimerOn){
		$('#infodiv').fadeOut(500,function(){
			if(is_array(messagesQ) && messagesQ.length>0){
				messagesQ.splice(0,1).toString();			
				msgTimerOn = false;
				Msg();
			}
		});
	}
}

function MsgHide(){
	$('#infodiv').fadeOut(500);
}
