/* Check Fields Functions */
function IsEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}
function IsNumeric(sText) {
   var ValidChars = "0123456789.-";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
      	IsNumber = false;
      }
   }
   return IsNumber;
}


/* Check final Checkout Submittion */
function checkComplete(form) {
	var error = "0";
	var errors = "The following errors occured:\n\n";	
	if (IsEmpty(form.card_type)) {
		error = "1";
		errors += "- Please select your card type\n";
	}
	if (!IsEmpty(form.card_number)) {
		if (!IsNumeric(form.card_number)) { 
			error = "1";
			errors += "- Please enter the card number\n";
		}
	}
	if (IsEmpty(form.card_number)) {
		error = "1";
		errors += "- Please enter the card number\n";
	}
	if (IsEmpty(form.expire_month) || IsEmpty(form.expire_year) ) {
		error = "1";
		errors += "- Please provide the card's expiration date\n";
	}
	/*if (IsEmpty(form.issue_number)) {
		error = "1";
		errors += "- Please enter the card issue number\n";
	}
	if (!IsEmpty(form.issue_number)) {
		if (!IsNumeric(form.issue_number)) { 
			error = "1";
			errors += "- Please enter the card issue number\n";
		}
	}*/
	if (IsEmpty(form.card_holder)) {
		error = "1";
		errors += "- Please enter the card holder's name\n";
	}
	if (IsEmpty(form.card_cvv)) {
		error = "1";
		errors += "- Please enter the card verification number\n";
	}
	if (!IsEmpty(form.card_cvv)) {
		if (!IsNumeric(form.card_cvv)) { 
			error = "1";
			errors += "- Please enter the card verification number\n";
		}
	}
	if (IsEmpty(form.payment_address1)) {
		error = "1";
		errors += "- Please enter card holders address\n";
	}
	if (IsEmpty(form.payment_city)) {
		error = "1";
		errors += "- Please enter card holders city/town\n";
	}
	if (IsEmpty(form.payment_county)) {
		error = "1";
		errors += "- Please enter card holders county\n";
	}
	if (IsEmpty(form.payment_postcode)) {
		error = "1";
		errors += "- Please enter card holders postcode\n";
	}
	if (IsEmpty(form.customer_email)) {
		error = "1";
		errors += "- Please enter your email address\n";
	}
	if (IsEmpty(form.verify_email)) {
		error = "1";
		errors += "- Please verify your email address\n";
	}
	if (!IsEmpty(form.customer_email) && !IsEmpty(form.verify_email)) {
		validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
    	if (form.customer_email.value.search(validRegExp) == -1) {
			error = "1";
			errors += "- Your email address is not valid.\n";
		} else {
			if(form.customer_email.value != form.verify_email.value) {
				error = "1";
				errors += "- Email's do not match\n";
			}
		}
	}
	if (!form.disclaimer.checked) {
		error = "1";
		errors += "- You must agree to disclaimer and t&c\n";
	}
	if (error == "1") {
		alert(errors);
		return false;
	}
	return true;
}

/* Check final Samples Order before Submittion */
function checkSamples(form) {
	var error = "0";
	var errors = "The following errors occured:\n\n";	
	if (IsEmpty(form.customer_name)) {
		error = "1";
		errors += "- Please enter your full name\n";
	}
	if (IsEmpty(form.delivery_address1)) {
		error = "1";
		errors += "- Please enter your delivery address (1)\n";
	}
	/*if (IsEmpty(form.delivery_address2)) {
		error = "1";
		errors += "- Please enter your delivery address (2)\n";
	}*/
	if (IsEmpty(form.delivery_city)) {
		error = "1";
		errors += "- Please enter your city/town\n";
	}
	if (IsEmpty(form.delivery_county)) {
		error = "1";
		errors += "- Please enter your county\n";
	}
	if (IsEmpty(form.delivery_postcode)) {
		error = "1";
		errors += "- Please enter your postcode\n";
	}
	if (IsEmpty(form.customer_email)) {
		error = "1";
		errors += "- Please enter your email address\n";
	}
	if (!IsEmpty(form.customer_email)) {
		validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
    	if (form.customer_email.value.search(validRegExp) == -1) {
			error = "1";
			errors += "- Your email address is not valid.\n";
		}
	}
	if (error == "1") {
		alert(errors);
		return false;
	}
	return true;
}

/* Check Dimensions Before Allowing Submittion */
function checkAdd() {
	var width = document.getaprice.widthmm.value;
	var height = document.getaprice.dropmm.value;
	var price = document.getElementById('price').value;
	if(width == "" || width == "0" || height == "" || height == "0" || price == "" || price == "0.00") {
		alert('Please enter your width and drop, then click \'Calculate Price\'.');
		document.getElementById("add-basket").disabled=true;
		document.getElementById("add-basket").className='add-to-basket-off';
		return false;
	}
	return true;
}

/* Show Total Price (Blind Price & Extras) */
function showTotal() {
	if(document.getElementById("price") && document.getElementById("price_extras")) {
		var totalAmount = parseFloat(document.getElementById("price").value) + parseFloat(document.getElementById("price_extras").value);
		document.getElementById('priceTotal').innerHTML = totalAmount.toFixed(2);
	}
	if(document.getElementById("price").value && !document.getElementById("price_extras")) {
		var totalAmount = parseFloat(document.getElementById("price").value) + parseFloat(document.getElementById("price_extras").value);
		document.getElementById('priceTotal').innerHTML = totalAmount.toFixed(2);
	}
}

/* Optional Extras - Price Addup - Normal */
var costArray = [1];
var costTotal = 0;
var costLength = "";
function addOption(option, value) {
	costArray[option] = value;
	optionTotal();
}
function optionTotal() {
	costTotal = 0;
	costLength = costArray.length-1;
	for(i=1; i<=costLength; i++) {
		var val = parseFloat(costArray[i]);
		if (!isNaN(val)) costTotal +=val; 
	}
	document.getElementById("price_extras").value=costTotal.toFixed(2);
	showTotal();
}

/* Optional Extras - Price Addup - Percentage */
/*function percent(number, whole, inverse, rounder){
	whole=parseFloat(whole);
	if(!whole){whole=100;};
	number=parseFloat(number);
	if(!number){number=0;};
	if(!whole || !number){return 0;};
	rounder=parseFloat(rounder);
	rounder=( rounder && (!(rounder%10) || rounder==1) )? rounder:100;
	return (!inverse)? Math.round( ((number*100)/whole) *rounder)/rounder:
	Math.round( ((whole*number)/100) *rounder)/rounder;
}*/
function percent(percent, total) {
	percent = parseFloat(percent);
	per = (total / 100 * percent);
	return per;
}
function addOptionPer(option, value) {
	parent_price = document.getElementById("price").value;
	if(parent_price == "0.00" || parent_price == "" || parent_price == "0") {
		alert('Please enter your desired width/drop before choosing options.');	
		return false;
	} else {
		child_price = parseFloat(percent(value, parent_price));
		costArray[option] = child_price;
		optionTotal();
	}
}

function resetOptionTotal() {
	if(document.getElementById('custom_1_1')) {document.getElementById('custom_1_1').checked = 'true';}
	if(document.getElementById('custom_2_1')) {document.getElementById('custom_2_1').checked = 'true';}
	if(document.getElementById('custom_3_1')) {document.getElementById('custom_3_1').checked = 'true';}
	if(document.getElementById('custom_4_1')) {document.getElementById('custom_4_1').checked = 'true';}
	if(document.getElementById('custom_5_1')) {document.getElementById('custom_5_1').checked = 'true';}
	if(document.getElementById('custom_6_1')) {document.getElementById('custom_6_1').checked = 'true';}
	if(document.getElementById('custom_7_1')) {document.getElementById('custom_7_1').checked = 'true';}
	document.getElementById("price_extras").value = '0.00';
}

/* Get Price Function - AJAX */
var xmlHttp
function getPrice(table) {
	var width = document.getaprice.widthmm.value;
	var height = document.getaprice.dropmm.value;
	if(width=="" || height=="") {
		alert('Please enter your desired width and drop.');
		document.getElementById("add-basket").disabled=true;
		document.getElementById("add-basket").className='add-to-basket-off';
		return false;
	}
	xmlHttp=GetXmlHttpObject()
	var url="../../../modules/getprice.php"
	url=url+"?t="+table
	url=url+"&w="+width
	url=url+"&h="+height
	xmlHttp.onreadystatechange=stateChanged
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function stateChanged() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		if(xmlHttp.responseText.length > 6) {
			alert('Dimensions out of range, please try again.');
			document.getElementById("add-basket").disabled=true;
			document.getElementById("add-basket").className='add-to-basket-off';
			return false;
		} else {
			document.getElementById("price").value=xmlHttp.responseText;
			document.getElementById("add-basket").disabled=false;
			document.getElementById("add-basket").className='add-to-basket';
			resetOptionTotal();
			showTotal();
		}
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
 		// Firefox, Opera 8.0+, Safari
 		xmlHttp=new XMLHttpRequest();
 	} catch (e) {
 		//Internet Explorer
 		try {
  			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  		} catch (e) {
  			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  		}
 	}
	return xmlHttp;
}
