// JavaScript Document
function changeSize(){
	var sizes = document.forms.quote.sizeId;
	for (var i = 0; i < sizes.options.length; i++ ) {
		if (sizes.options[i].text == "Other") {
			sizes.selectedIndex = i;
		}
	}
}

function changeSizeSelect(selectField) {
	var optionText = selectField.options[selectField.selectedIndex ].text;
	if (optionText != "Other") {
		document.forms.quote.height.value = '';
		document.forms.quote.width.value = '';
	}
}

function isNumericWhole (fieldValue) {
	var fieldValue;
	var c;
	var returnValue = true; 
	var validChars = "1234567890"; 
	
	if (fieldValue == null || fieldValue.length == 0){
		returnValue = false;
	}
		
	for (var i = 0; i < fieldValue.length && returnValue == true; i++) {
    	c = fieldValue.charAt(i);
      	if (validChars.indexOf(c) == -1){
        	returnValue = false;
      	}
    }
   
   	return returnValue;
}

function isTelephoneNumber (fieldValue) {
	var fieldValue;
	var c;
	var returnValue = true; 
	var validChars = "1234567890 ";
	
	if (fieldValue == null || fieldValue.length == 0){
		returnValue = false;
	}
		
	for (var i = 0; i < fieldValue.length && returnValue == true; i++) {
    	c = fieldValue.charAt(i);
      	if (validChars.indexOf(c) == -1){
        	returnValue = false;
      	}
    }
   
   	return returnValue;
}

function isGreaterThanZero(fieldValue, fieldName) {
	var isNumeric = isNumericWhole(fieldValue);
	var isGreater = false;
	
	if (isNumeric) {
		if (parseInt(fieldValue) > 0) {
			isGreater = true;
		}
	}
	
	if (!isNumeric || !isGreater) {
		alert ("Please enter a number greater than zero");
	}
	
	return isGreater;
}


function isNullString(strVal) {
	var rtn = false; 
	
	if (strVal == null || strVal.length == 0) { 
		rtn = true;
	}
	else {
		var newVal = trim(strVal);
		if (newVal.length == 0) {
			rtn = true;
		}
	}
	
	return rtn;
}

function ltrim(str) { 
	if (str == null) {
		str ='';
	}
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function rtrim(str) {
	if (str == null) {
		str ='';
	}
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--);
	return str.substring(0,j+1);
}
function trim(str) {
	if (str == null) {
		str ='';
	}
	return ltrim(rtrim(str));
}
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
		
	 if (str.indexOf(" ")!=-1){
	    return false
	 }

	return true					
}

function getFormValues (formObj) {
	var str = '';
	
	//Get all field values from form
	for (var i = 0; i < formObj.length; i++) {
		str += formObj.elements[i].name + "=" + escape(formObj.elements[i].value) + "&";
	}
	
	//alert (str);
	return str;
}

function uploadArtwork(theForm) {
	if (theForm.uploadType.selectedIndex > 0) {
		theForm.submit();
	}
	else {
		alert("Please select the file type");
	}
}

function setNetTotal() {
	var netTotal = (parseInt(document.forms["quoteLine"].Quantity.value) * parseFloat(document.forms["quoteLine"].ProductPrice.value));
	document.forms["quoteLine"].NetTotal.value = netTotal;
	//var netTotalText = document.getElementById("netTotal");
	//netTotalText.innerHtml = netTotal;
	
}

function setSampleType(sampleType) {
	try {
		document.forms["requestSample"].sampleType.value = sampleType;
	}
	catch (e) {  }
	
	if(document.forms["requestSample"].onsubmit()) {
    	document.forms["requestSample"].submit();
 	}
}

function submitRequestCallback(){
	if(document.forms["requestCallback"].onsubmit()) {
    	document.forms["requestCallback"].submit();
 	}
}

function valRequestCallback(theForm) {
	var errors = '';
	var customer = isNullString(theForm.customerName.value);
	var contact = isNullString(theForm.contactName.value);
	var email = trim(theForm.emailAddress.value);
	var telephone = trim(theForm.telephoneNumber.value);
	var enquiry = trim(theForm.enquiry.value);
	var responseType = theForm.responseType.value;
	
	/* Check responseType is a numeric id */
	if (! isNumericWhole(responseType)) {
		errors = "Oops an error has occurred on this page";
	}
	
	/* check customer & contact details */
	if (customer && contact) {
		errors = "You must enter either a company name or your name<br>";
	}
	
	if (email.length > 0) {
		if (! echeck(email)){
			errors += "Please enter a valid email address";
		}
	}
	
	/* check the telephone number */
	if (telephone.length > 0) {
		if (! isTelephoneNumber(telephone)) {
			errors += "Please enter a valid telephone number<br>";
		}
	}
	
	if (responseType == "1") { /*is email*/
		if(isNullString(email)) {
			errors += "You have requested a response by email, please enter your email address<br>";
		}
	}
	else if (responseType == "2") {
		if (isNullString(telephone)) {
			errors += "You have requested a response by phone, please enter your phone number<br>";
		}
	}
		
	/* check for html tag starts - disallow '<' char */
	if (enquiry.indexOf('<') > -1) {
		errors += "Please remove the < character from your enquiry";
	}
	
	if (enquiry.length < 10) {
		errors += "Please enter your question/comments in the enquiry box";
	}
	
	/* Display the errors on the form */
	try {
		var errorSpace = document.getElementById("formErrors");
		if (errorSpace != null) {
			errorSpace.innerHTML = errors; 
			errorSpace.style.visibility = 'visible';
		}
		else {
			alert(errors.replace("<br>", "\n"));
		}
	}
	catch (e) {
		alert(errors.replace("<br>", "\n"));
	}
	
	if (errors.length == 0) {
		errorSpace.style.visibility = 'hidden';
		return true;
	}
	else { 
		alert("There are errors on this form, please correct them and try again");
		return false;
	}
}

function valRequestSample(theForm) {
	var errors = '';
	var customer = isNullString(theForm.customerName.value);
	var contact = isNullString(theForm.contactName.value);
	var address1 = isNullString(theForm.addressLine1.value);
	var address2 = isNullString(theForm.addressLine2.value);
	var city = isNullString(theForm.city.value);
	var county = isNullString(theForm.county.value);
	var postcode = trim(theForm.postcode.value);
	var email = trim(theForm.emailAddress.value);
	var telephone = trim(theForm.telephoneNumber.value);
	var comments = trim(theForm.customerComments.value);
	var productCategoryId = trim(theForm.productCategoryId.value);
	
	/* check the product is a numeric id */
	if (! isNumericWhole(productCategoryId)) {
		errors = "Oops - an error has occured on this page<br>";
	}
	
	/* check customer & contact details */
	if (customer && contact) {
		errors = "You must enter either a company name or your name<br>";
	}
	
	/* check simple address details */
	if (address1) {
		errors += "You must enter the first line of your address<br>";
	}
	
	if (city) {
		errors += "You must enter the city<br>";
	}
	
	/* check the email address */
	if (isNullString(email)) {
		errors += "You must enter an email address<br>";
	}
	else {
		if (! echeck(email)) {
			errors += "Please enter a valid email address<br>";
		}
	}
	
	/* check the telephone number */
	if (telephone.length > 0 && (! isTelephoneNumber(telephone))) {
		errors += "Please enter a valid telephone number<br>";
	}
		
	/* convert postcode to upper case */
	theForm.postcode.value = postcode.toUpperCase();
	
	/* check for html tag starts - disallow '<' char */
	if (comments.indexOf('<') > -1) {
		errors += "Please remove the < character from your comments";
	}
	
	/* Display the errors on the form */
	try {
		var errorSpace = document.getElementById("formErrors");
		if (errorSpace != null) {
			errorSpace.innerHTML = errors; 
			errorSpace.style.visibility = 'visible';
		}
		else {
			alert(errors.replace("<br>", "\n"));
		}
	}
	catch (e) {
		alert(errors.replace("<br>", "\n"));
	}
	
	if (errors.length == 0) {
		errorSpace.style.visibility = 'hidden';
		return true;
	}
	else { 
		alert("There are errors on this form, please correct them and try again");
		return false;
	}
}

function submitRequestQuote(){
	if(document.forms["quote"].onsubmit()) {
    	document.forms["quote"].submit();
 	}
}

function valRequestQuote(theForm) {
	var errors = '';
	var customer = isNullString(theForm.customerName.value);
	var contact = isNullString(theForm.contactName.value);
	var address1 = isNullString(theForm.addressLine1.value);
	var address2 = isNullString(theForm.addressLine2.value);
	var city = isNullString(theForm.city.value);
	var county = isNullString(theForm.county.value);
	var postcode = trim(theForm.postcode.value);
	var email = trim(theForm.emailAddress.value);
	var telephone = trim(theForm.telephoneNumber.value);
	var comments = trim(theForm.customerComments.value);
	var productId = theForm.productId.selectedIndex + '';
	var isKeyFobs = (theForm.productId.options[theForm.productId.selectedIndex].text == "Key Fobs");
	var quantity = trim(theForm.quantity.value);
	var badgeFixingId = theForm.badgeFixingId.selectedIndex + '';
	var shapeId = theForm.shapeId.selectedIndex + '';
	var sizeIdSelect = theForm.sizeId;
	var height = trim(theForm.height.value);
	var width = trim(theForm.width.value);
	var materialFinishingId = theForm.materialFinishingId.selectedIndex + '';
	var noOfTextLines = theForm.noOfTextLines.selectedIndex + '';
	
	/* check the product is a numeric id */
	if (! isNumericWhole(productId)) {
		errors = "Oops - an error has occured on this page<br>";
	}
	
	/* check customer & contact details */
	if (customer && contact) {
		errors = "You must enter either a company name or your name<br>";
	}
	
	/* check simple address details */
	if (address1) {
		errors += "You must enter the first line of your address<br>";
	}
	
	if (city) {
		errors += "You must enter the city<br>";
	}
	
	/* check the email address */
	if (isNullString(email)) {
		errors += "You must enter an email address<br>";
	}
	else {
		if (! echeck(email)) {
			errors += "Please enter a valid email address<br>";
		}
	}
	
	/* check the telephone number */
	if (telephone.length > 0 && (! isTelephoneNumber(telephone))) {
		errors += "Please enter a valid telephone number<br>";
	}
		
	/* convert postcode to upper case */
	theForm.postcode.value = postcode.toUpperCase();
	
	/* check for html tag starts - disallow '<' char */
	if (comments.indexOf('<') > -1) {
		errors += "Please remove the < character from your comments<br>";
	}
	
	if (quantity.length == 0 || (! isNumericWhole(quantity)) || quantity == "0") {
		errors += "Please enter the number of items you require<br>";
	}
	
	if (! isKeyFobs) {
		if (sizeIdSelect.options[sizeIdSelect.selectedIndex].text == "Other") {
			if (! isNumericWhole(height) || parseInt(height) < 10 || parseInt(height) > 45) {
				errors += "You have selected a custom size, please enter a height between 10mmm and 45mm<br>";
			}
			
			if (! isNumericWhole(width) || parseInt(width) < 10 || parseInt(width) > 95) {
				errors += "You have selected a custom size, please enter a width between 10mmm and 95mm<br>";
			}
			
		}
	}
	
	if (
			(! isNumericWhole(badgeFixingId)) || 
			(! isNumericWhole(shapeId)) || 
			(! isNumericWhole(sizeIdSelect.selectedIndex)) || 
			(! isNumericWhole(materialFinishingId)) || 
			(! isNumericWhole(noOfTextLines))
		){
		errors = "Oops, an error has occurred on this page<br>";
	}
	
	/* Display the errors on the form */
	try {
		var errorSpace = document.getElementById("formErrors");
		if (errorSpace != null) {
			errorSpace.innerHTML = errors + "<br>"; 
			errorSpace.style.visibility = 'visible';
		}
		else {
			alert(errors.replace("<br>", "\n"));
		}
	}
	catch (e) {
		alert(errors.replace("<br>", "\n"));
	}
	
	if (errors.length == 0) {
		errorSpace.style.visibility = 'hidden';
		return true;
	}
	else { 
		alert("There are errors on this form, please correct them and try again");
		//theForm.customerName.focus();
		return false;
	}
}	

function valQStatusForm(theForm) {
	var acceptedBy = isNullString(theForm.acceptedBy.value);
	
	if (acceptedBy) {
		alert ("Please enter your name");
		theForm.acceptedBy.focus();
		return false;
	}
	else {
		theForm.submit();
	}
}
