
jQuery(document).ready(function() {
	updatePrices();

});



function Pop_up(page) { 
	window.open(page,"_new","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizeable=yes,copyhistory=no,width=500,height=500");
}

function quantityChanged() {
	jQuery.cookie('quantity', jQuery("#itemquantity").val(), { expires: 7, path: '/' });
	updatePrices();
}

function updatePrices() {
	/* alert("update prices"); */
	jQuery.getJSON(cart_path + "ajax.php", { p: 'prices' }, function(json) {
		jQuery("#price").val("£" + json.price);
		jQuery("#cost").val("£" + json.itemtotal);
		jQuery("#shipValue").val("£" + json.shipping);
		jQuery("#total").val("£" + json.total);
		jQuery("#totalhidden").val("£" + json.total);
		jQuery("#amount").val("£" + json.total);
		jQuery("#itemquantity").val(json.quantity);
		jQuery("#chkquantity").val(json.quantity);
		jQuery("#insurance").val("£" + json.insurance);
		jQuery("#insurancehidden").val("£" + json.insurance);
	});
};

function updatePricesOnCheck() {
	/* alert("update prices"); */
	jQuery.getJSON(cart_path + "ajax.php", { p: 'prices', is_add_insurance : 'yes' }, function(json) {
		console.log(json);
		jQuery("#price").val("£" + json.price);
		jQuery("#cost").val("£" + json.itemtotal);
		jQuery("#shipValue").val("£" + json.shipping);
		jQuery("#total").val("£" + json.total);
		jQuery("#totalhidden").val("£" + json.total);
		jQuery("#amount").val("£" + json.total);
		jQuery("#itemquantity").val(json.quantity);
		jQuery("#chkquantity").val(json.quantity);
		jQuery("#insurance").val("£" + json.insurance);
		jQuery("#insurancehidden").val("£" + json.insurance);
	});
};


function checkPromo() {
	jQuery.getJSON(cart_path + "ajax.php", { p: "promo", code: jQuery("#promotion").val() }, function(json) {
		if(json.valid == 'false') {
			jQuery("#promotion").css("background-color", "#F28181");
			jQuery("#promsg").html(json.message);
		} else if(json.valid == 'true') {
			jQuery("#promotion").css("background-color", "#76E96B");
			jQuery("#promsg").html(json.message);
		}
	});
	quantityChanged();
}

function checkOther() {
	if(jQuery("#howdidyou").val() == "other") jQuery("#other_wrap").css("display", "block");
	else jQuery("#other_wrap").css("display", "none");
}

function slideText() {
	if(jQuery("#country").val() == "othercountry") {
		jQuery("#slidetext").css("display", "block");
	}
	else
		 jQuery("#slidetext").css("display", "none");
}





function checkoutdata() {
	if(checkValues()) {
		jQuery.get(cart_path + "ajax.php", { p: 'session', fname: jQuery('#fname').val(), lname: jQuery('#lname').val(), add1: jQuery('#add1').val() , add2: jQuery('#add2').val() , postcode: jQuery('#postcode').val() , country: jQuery('#country').val() , phone:jQuery('#phone').val() , email: jQuery('#email').val(), itemquantity: jQuery('#itemquantity').val() , price: jQuery('#price').val(), cost: jQuery('#cost').val(), city: jQuery('#city').val(), select: jQuery('#select').val(), promotion : jQuery('#promotion').val(), ship: jQuery('#shipValue').val(), total: jQuery('#total').val() }, function(json) { });
	} else {
		return false;
	}
}

function checkValues(form_obj) {
	var msg="The following error(s) occurred while submitting the form: \n";
	var is_valid=true;
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = document.forms['checkout'].elements['email'].value;
	var i =0;

	if(form_obj.fname.value=="") {
		msg+="\n Please enter your first name.";
		is_valid=false;
	}
	if(form_obj.lname.value=="") { 
		msg+="\n Please enter your last name.";
		is_valid=false;
	}
	if(form_obj.add1.value=="") { 
		msg+="\n Please enter address line #1.";
		is_valid=false;
	} 
	if(form_obj.city.value=="") { 
		msg+="\n Please enter your city name.";
		is_valid=false;
	} 
	if(form_obj.postcode.value=="") { 
		msg+="\n Please enter your post code.";
		is_valid=false;
	} 
	if((reg.test(address) == false )) {
		if(form_obj.email.value == "") {
			msg+="\n Please enter your e-mail address.";
			is_valid=false;
		}
		else { 
			msg+="\n Please enter a valid e-mail address.";
			is_valid=false;
		}
	}
	if(form_obj.country.value == "othercountry") {
		msg+="\n We are sorry, but we only sell the Portapad inside the UK.";
		is_valid=false;
	}
	if(!is_valid) {
		alert(msg);
		return false;
	}				 
} 
