var illegalChars = /[\W_]/; // allow only letters and numbers
var nospaces = /^\s+|\s+$/g; //remove all spaces
var onlynumbers = /[\(\)\.\-\ a-z]/g; //only numbers

path = "http://www.peterblairltd.com/";

nav = new Image();
nav.src = path + "images/nav_on_1.gif";
nav2 = new Image();
nav2.src = path + "images/nav_on_2.gif";
nav3 = new Image();
nav3.src = path + "images/nav_on_3.gif";
nav4 = new Image();
nav4.src = path + "images/nav_on_4.gif";
nav5 = new Image();
nav5.src = path + "images/nav_on_5.gif";

/*
one = new Image();
one.src = path + "images/aboutbg.jpg";
two = new Image();
two.src = path + "images/cartbg.jpg";
three = new Image();
three.src = path + "images/custombg.jpg";
four = new Image();
four.src = path + "images/prodbg.jpg";
five = new Image();
five.src = path + "images/slbg.jpg";
*/


function imagepop(theimage) {
	var random_num1 = (Math.round((Math.random()*9)+1));
	var random_num2 = (Math.round((Math.random()*9)+1));
	window.open("http://www.peterblairltd.com/includes/imagepop.php?image="+theimage,'PBLAIRApopupImage'+random_num1+random_num2,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150')
}
function updatecart() {
	document.getElementById("cartaction").value = "update";
	theform = document.getElementById("cartform");
	theform.submit();
	return true;
}
function deleteselected() {
	document.getElementById("cartaction").value = "delete";
	theform = document.getElementById("cartform");
	theform.submit();
	return true;
}
function formatcurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}
function validateemail (email) {
	var error = "";
	var emailFilter=/^.+@.+\..{2,3}$/;
	if (!(emailFilter.test(email))) { 
	   alert("'"+email+"' is an Invalid Email Address");
	   return false;
	}
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (email.match(illegalChars) && error == "") {
	   alert("'"+email+"' Contains Illegal Characters");
	   return false;
	}
	else {
		return true;
	}
}
function handlereturn (message) {
	if (message != "") {
		alert("The Following Errors Occured:\n"+message);
		return false;
	}
	else return true;
}
function validatecheckout() {
	theform = document.checkoutform;
	theemail = document.getElementById("email").value;
	thephone = document.getElementById("phone1").value + document.getElementById("phone2").value + document.getElementById("phone3").value;
	thephone = thephone.replace(onlynumbers,"")
	var message = "";
	if (!validateemail(theemail)) return false;
	if (isNaN(parseInt(thephone)) || thephone.length < 10) {
		message += "\nPlease Enter a Valid Phone Number.";
	}
	thesname = document.getElementById("sdatetoship").value;//.replace(/^\s+|\s+$/g, "");
	thescname = document.getElementById("sname").value;//.replace(/^\s+|\s+$/g, "");
	thesline1 = document.getElementById("sline1").value;//.replace(/^\s+|\s+$/g, "");
	thescity = document.getElementById("scity").value;//.replace(/^\s+|\s+$/g, "");
	thesstate = document.getElementById("sstate").value;//.replace(/^\s+|\s+$/g, "");
	thespostal = document.getElementById("spostal").value;//.replace(/^\s+|\s+$/g, "");
	if (thesname.replace(nospaces, "") == "") message += "\nYou Must Supply a Date To Ship.";
	if (thescname.replace(nospaces, "") == "") message += "\nThe Contact Name Can Not Be Empty.";
	if (thesline1.replace(nospaces, "") == "") message += "\nThe Shipping Street Address Line 1 Can Not Be Empty.";
	if (thescity.replace(nospaces, "") == "") message += "\nThe Shipping City Can Not Be Empty.";
	if (thesstate.replace(nospaces, "") == "") message += "\nThe Shipping State Can Not Be Empty.";
	if (thespostal.replace(nospaces, "") == "") message += "\nThe Shipping Postal / Zip Code Can Not Be Empty.";
	thebname = document.getElementById("bname").value;//.replace(/^\s+|\s+$/g, "");
	thebline1 = document.getElementById("bline1").value;//.replace(/^\s+|\s+$/g, "");
	thebcity = document.getElementById("bcity").value;//.replace(/^\s+|\s+$/g, "");
	thebstate = document.getElementById("bstate").value;//.replace(/^\s+|\s+$/g, "");
	thebpostal = document.getElementById("bpostal").value;//.replace(/^\s+|\s+$/g, "");
	if (thebname.replace(nospaces, "") == "") message += "\nThe Billing Name Can Not Be Empty.";
	if (thebline1.replace(nospaces, "") == "") message += "\nThe Billing Street Address Line 1 Can Not Be Empty.";
	if (thebcity.replace(nospaces, "") == "") message += "\nThe Billing City Can Not Be Empty.";
	if (thebstate.replace(nospaces, "") == "") message += "\nThe Billing State Can Not Be Empty.";
	if (thebpostal.replace(nospaces, "") == "") message += "\nThe Billing Postal / Zip Code Can Not Be Empty.";
	
	return handlereturn(message);
}