var firsttime;

function reOpenPopup(url) {
	var thisWin = window.open(url,'','width=450,height=300,scrollbars=no,left=250,top=150,screenX=250,screenY=150');
	if (thisWin.focus) {thisWin.focus()}
	}
	
function checkCalcform(FormRef){
	if (FormRef.calcWidth.value=="") {alert("Please a width"); FormRef.calcWidth.focus(); return false}
}	
	

function checkPaymentForm(FormRef) {
    if (firsttime=="Y") {alert("Please wait your payment is being processed."); return false}
    else {
		if (FormRef.CustomerFirstName.value=="") {alert("Please enter your First Name"); FormRef.CustomerFirstName.focus(); return false}
		if (FormRef.CustomerLastName.value=="") {alert("Please enter your Last Name"); FormRef.CustomerLastName.focus(); return false}
		if (FormRef.CustomerCity.value=="") {alert("Please enter your City"); FormRef.CustomerCity.focus(); return false}
		if (FormRef.CustomerCounty.value=="") {alert("Please enter your County"); FormRef.CustomerCounty.focus(); return false}
		if (FormRef.CustomerPostcode.value=="") {alert("Please enter your Postcode"); FormRef.CustomerPostcode.focus(); return false}
		if (FormRef.CustomerCountry.value=="") {alert("Please enter your Country"); FormRef.CustomerCountry.focus(); return false}
		if (FormRef.TelephoneNumber.value=="") {alert("Please enter your Telephone Number"); FormRef.TelephoneNumber.focus(); return false}
		if (emailCheck(FormRef.EmailAddress.value)==false) {return false}
		//if (checkCCnum(FormRef.CCnumber.value)==false) {return false}
		if ((FormRef.CCexpiryMonth.value=="None") || (FormRef.CCexpiryYear.value=="None")) {alert("Please enter your Credit Card Expiry Date"); FormRef.CCexpiryMonth.focus(); return false}
		if (FormRef.CCsecurity.value=="") {alert("Please enter your Credit Card Security Code"); FormRef.CCsecurity.focus(); return false}
		firsttime="Y";
		}
	}

function emailCheck (emailStr) {
	emailStr = emailStr.toLowerCase(); // Validate email address
	var checkTLD=1; // Verify that the address ends in a two-letter country or well-known TLD.  1 means check it, 0 means don't.
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|co|uk)$/; // Known TLDs that an e-mail address must end with.
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {alert("The Email address you entered seems incorrect (check @ and .'s)"); return false}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {if (user.charCodeAt(i)>127) {alert("The username you entered contains invalid characters. Please try again.");return false}}
	for (i=0; i<domain.length; i++) {if (domain.charCodeAt(i)>127) {alert("The domain name you entered contains invalid characters. Please try again.");return false}}
	if (user.match(userPat)==null) {alert("The username you entered doesn't seem to be valid.");return false}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {if (IPArray[i]>255) {alert("Destination IP address is invalid!");return false}}
		return true;
		}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {if (domArr[i].search(atomPat)==-1) {alert("The domain name does not seem to be valid.");return false}}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {alert("The email address you enter must end in a well-known domain or two letter " + "country.");return false}
	if (len<2) {alert("This address is missing a hostname!");return false}
	return true;
	}

function checkCCnum(CCnumber) {
	var ccRE, cardMath, doubled;
	ccRE = /\W/gi;
	CCnumber = CCnumber.replace(ccRE, "");
	if (isNaN(CCnumber)) {alert("Credit card number is not numeric."); return false}
	if ((CCnumber.length!=16) && (CCnumber.length!=18) && (CCnumber.length!=19)) {alert("Incorrect number of digits in credit card number."); return false}
	cardMath=0;
	for (i=CCnumber.length; i>0; i--) {
		if (i % 2 == 1) {
			doubled = "" + (parseInt(CCnumber.substring(i - 1, i)) * 2);
			if (doubled.length==2) {doubled = parseInt(doubled.substring(0,1)) + parseInt(doubled.substring(1,2))}
			cardMath += parseInt(doubled);
			}
		else {cardMath += parseInt(CCnumber.substring(i - 1, i))}
		}
	if (cardMath % 10 != 0) {alert("Credit card number is invalid."); return false}
	}

function checkCCexpire(CCexpire) {
	var ccRE = /\d\d\/\d\d/;
	if (CCexpire=="") {alert("Please enter your credit card expiry date."); return false}
	if (ccRE.test(CCexpire)==false) {alert("Expiry date is not in correct format."); return false}
	}

function doCalculation(formRef,Price,DiscountPoint,DiscountPercent) {
	if (formRef.calcWidth.value=="" || isNaN(formRef.calcWidth.value) || formRef.calcWidth.value==" " || formRef.calcWidth.value<0) {formRef.calcWidth.value=0}
	if (formRef.calcDepth.value=="" || isNaN(formRef.calcDepth.value) || formRef.calcDepth.value==" " || formRef.calcDepth.value<0) {formRef.calcDepth.value=0}
	if (formRef.calcQuantity.value=="" || isNaN(formRef.calcQuantity.value) || formRef.calcQuantity.value==" " || formRef.calcQuantity.value<0) {formRef.calcQuantity.value=0}
	formRef.calcQuantity.value=Math.round(formRef.calcQuantity.value);
	var totalCost=formRef.calcQuantity.value * (formRef.calcWidth.value/1000) * (formRef.calcDepth.value/1000)  * Price;
	totalCost=Math.round(totalCost * 100) / 100;
	if (totalCost>DiscountPoint) {
		totalCost=totalCost * ((100 - DiscountPercent) / 100);
		totalCost=Math.round(totalCost * 100) / 100;
		}
	formRef.calcTotal.value=totalCost;
	}
	
function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
	
	
	
