// JavaScript Document
 var errors=new Array();

function checktonnageform(){
	var errors=new Array();
	if (document.calculator.length.value==''){
	errors.push("length is blank");
	}
	else if (isNaN(document.calculator.length.value)) { 
	errors.push("length must be a number");
	}
	if (document.calculator.width.value==''){
	errors.push("width is blank");
	}
	else if (isNaN(document.calculator.width.value)) { 
	errors.push("width must be a number");
	}	
	if (document.calculator.depth.value==''){
	errors.push("depth is blank");
	}
	else if (isNaN(document.calculator.depth.value)) { 
	errors.push("depth must be a number");
	}
	if (errors.length<1){
	return true;
	}
	else {
	document.getElementById('errors').innerHTML="<h4>Please correct the following errors:</h4><ul>";
	for (x in errors){document.getElementById('errors').innerHTML+="<li>"+errors[x]+"</li>";}
	document.getElementById('errors').innerHTML+="</ul>";
	document.getElementById('errors').style.color='red';
	return false;
	}
}
function checkblockform(){
	var errors=new Array();
	if (document.calculator.length.value==''){
	errors.push("length is blank");
	}
	else if (isNaN(document.calculator.length.value)) { 
	errors.push("length must be a number");
	}
	if (document.calculator.height.value==''){
	errors.push("height is blank");
	}
	else if (isNaN(document.calculator.height.value)) { 
	errors.push("height must be a number");
	}	
	if (document.calculator.thickness.value=='select'){
	errors.push("wall thickness is not specified.");
	}
	if (errors.length<1){
	return true;
	}
	else {
	document.getElementById('errors').innerHTML="<h4>Please correct the following errors:</h4><ul>";
	for (x in errors){document.getElementById('errors').innerHTML+="<li>"+errors[x]+"</li>";}
	document.getElementById('errors').innerHTML+="</ul>";
	document.getElementById('errors').style.color='red';
	return false;
	}
}
function postcodecheck(){ //check postcode format is valid
 test = document.quotation.postcode.value; size = test.length
 test = test.toUpperCase(); //Change to uppercase
 while (test.slice(0,1) == " ") //Strip leading spaces
  {test = test.substr(1,size-1);size = test.length
  }
 while(test.slice(size-1,size)== " ") //Strip trailing spaces
  {test = test.substr(0,size-1);size = test.length
  }
 document.quotation.postcode.value = test; //write back to form field
 if (size < 6 || size > 8){ //Code length rule
  //alert(test + " is not a valid postcode - wrong length");
  errors.push(test + " is not a valid postcode - wrong length");
   document.quotation.postcode.focus();
  return false;
  }
 if (!(isNaN(test.charAt(0)))){ //leftmost character must be alpha character rule
   errors.push(test + " is not a valid postcode - cannot start with a number");
   document.quotation.postcode.focus();
   return false;
  }
 if (isNaN(test.charAt(size-3))){ //first character of inward code must be numeric rule
   errors.push(test + " is not a valid postcode - alpha character in wrong position");
   document.quotation.postcode.focus();
   return false;
  }
 if (!(isNaN(test.charAt(size-2)))){ //second character of inward code must be alpha rule
   errors.push(test + " is not a valid postcode - number in wrong position");
   document.quotation.postcode.focus();
   return false;
  }
 if (!(isNaN(test.charAt(size-1)))){ //third character of inward code must be alpha rule
   errors.push(test + " is not a valid postcode - number in wrong position");
   document.quotation.postcode.focus();
   return false;
  }
 if (!(test.charAt(size-4) == " ")){//space in position length-3 rule
   errors.push(test + " is not a valid postcode - no space or space in wrong position");
   document.quotation.postcode.focus();
   return false;
   }
 count1 = test.indexOf(" ");count2 = test.lastIndexOf(" ");
 if (count1 != count2){//only one space rule
   errors.push(test + " is not a valid postcode - only one space allowed");
   document.quotation.postcode.focus();
   return false;
  }
//alert("Postcode Format OK");
return true;
}

function emailcheck() {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.quotation.email.value;
   if(reg.test(address) == false) {
      //alert('Invalid Email Address');
  	  errors.push(address + " is not a valid email address");
      return false;
   }
}

function checkquotationform(){
	errors=new Array();
	if (document.quotation.street){
    	var telephone=document.quotation.telephone.value.replace(/[\(\)\.\-\ ]/g, '');     
		if (document.quotation.street.value==''){
		errors.push("Street is blank");
		}
		if (document.quotation.town.value==''){
		errors.push("Town is blank");
		}
		if (document.quotation.county.value==''){
		errors.push("County is blank");
		}
		if (document.quotation.postcode.value!=''){
		postcodecheck();
		}
		if (document.quotation.postcode.value==''){
		errors.push("Postcode is blank");
		}
		if (document.quotation.firstname.value==''){
		errors.push("First Name is blank");
		}
		//else if (isNaN(document.calculator.length.value)) { 
		//errors.push("length must be a number");
		//}
		if (document.quotation.lastname.value==''){
		errors.push("Last Name is blank");
		}
		//else if (isNaN(document.calculator.height.value)) { 
	//	errors.push("height must be a number");
	//	}
		if (document.quotation.email.value!=''){
		emailcheck();
		}
		if (document.quotation.email.value==''){
		errors.push("Email is blank");
		}
		if (telephone==''){
		errors.push("Telephone is blank");
		}
		else if (isNaN(telephone)) { 
		errors.push("telephone must be a number");
		}
		if (errors.length<1){
		return true;
		}
		else {
		document.getElementById('errors').innerHTML="<h4>Please correct the following errors:</h4><ul>";
		for (x in errors){document.getElementById('errors').innerHTML+="<li>"+errors[x]+"</li>";}
		document.getElementById('errors').innerHTML+="</ul>";
		document.getElementById('errors').style.color='red';
		return false;
		}
	}
	else
	{
    	var telephone=document.quotation.telephone.value.replace(/[\(\)\.\-\ ]/g, '');     
		if (document.quotation.firstname.value==''){
		errors.push("First Name is blank");
		}
		//else if (isNaN(document.calculator.length.value)) { 
		//errors.push("length must be a number");
		//}
		if (document.quotation.lastname.value==''){
		errors.push("Last Name is blank");
		}
		//else if (isNaN(document.calculator.height.value)) { 
	//	errors.push("height must be a number");
	//	}
		if (document.quotation.email.value!=''){
		emailcheck();
		}
		if (document.quotation.email.value==''){
		errors.push("Email is blank");
		}
		if (document.quotation.telephone.value==''){
		errors.push("Telephone is blank");
		}
		else if (isNaN(telephone)) { 
		errors.push("telephone must be a number");
		}
		if (errors.length<1){
		return true;
		}
		else {
		document.getElementById('errors').innerHTML="<h4>Please correct the following errors:</h4><ul>";
		for (x in errors){document.getElementById('errors').innerHTML+="<li>"+errors[x]+"</li>";}
		document.getElementById('errors').innerHTML+="</ul>";
		document.getElementById('errors').style.color='red';
		return false;
		}

	}
}
function getIndex()
{
var x=document.getElementById('block');
alert(x.selectedIndex);
}

function buildblocksizemenu(){
	document.getElementById('block').style.display="block";
	var thickness=document.calculator.thickness.value;
	
	switch(thickness)
	{
	case '100':
		document.getElementById('block').innerHTML='<label for="block">Select a block size</label><br /><select name="block" size="2"><option selected="selected" value="440x215x100mm">440x215x100mm (4")</option><option value="440x140x100mm">440x140x100mm (Blockettes)</option></select><br />';
	  break;
	case '225':
		document.getElementById('block').innerHTML='<label for="block">Select a block size</label><br /><select name="block" size="2"><option selected="selected" value="440x215x140mm">440x215x140mm (6")</option><option value="440x215x100mm">440x215x100mm (4")</option></select><br />';
	  break;
	case '300':
		document.getElementById('block').innerHTML='<label for="block">Select a block size</label><br /><select name="block" size="2"><option selected="selected" value="440x290x100mm">440x290x100mm (12")</option><option value="350x290x100mm">350x290x100mm (14")</option></select><br />';
	  break;
	case '350':
		document.getElementById('block').innerHTML='<label for="block">Select a block size</label><br /><select name="block" size="1"><option selected="selected" value="350x290x100mm">350x290x100mm (14")</option></select><br />';
	  break;
	case '450':
		document.getElementById('block').innerHTML='<label for="block">Select a block size</label><br /><select name="block" size="2"><option selected="selected" value="440x215x140mm">440x215x140mm (6")</option><option value="440x215x100mm">440x215x100mm (4")</option></select><br />';
	  break;
	case 'cavity':
		document.getElementById('block').innerHTML='<label for="block">Select a block size</label><br /><select name="block" size="2"><option selected="selected" value="440x215x100mm">440x215x100mm (4")</option><option value="440x140x100mm">440x140x100mm (Blockettes)</option></select><br />';
	  break;
	default:
		document.getElementById('block').innerHTML='<p>Please select a wall thickness</p>';
	}

}
