// JavaScript Document

function check(form){
   
	var allFormInputs = form.elements;

	for (x = 0; x < allFormInputs.length; x++) {
		//alert("Input Name: " + allFormInputs[x].name);
		var isFormInput = Spry.Utils.hasClassName(allFormInputs[x], "formInput");
		//alert("Has class of formInput?: " + isFormInput);
		
		var isRequired = Spry.Utils.hasClassName(allFormInputs[x], "required");
		//alert("Input Name: " + allFormInputs[x].name + " , Has class of formInput?: " + isFormInput + ", Is it required?: " + isRequired);
		
		if(isRequired == true) {
			//alert("If true")
			if (allFormInputs[x].value == "") {
				//alert("If empty");
				
				if (allFormInputs[x].name == "company"){
					alert("Please enter your company.");
					allFormInputs[x].focus();
					return;
				}
				
				else if (allFormInputs[x].name == "realname" || allFormInputs[x].name == "name"){
					alert("Please enter your name.");
					allFormInputs[x].focus();
					return;
				}
				
				else if (allFormInputs[x].name == "address"){
					alert("Please enter your address.");
					allFormInputs[x].focus();
					return;
				}
				
				else if (allFormInputs[x].name == "zip"){
					alert("Please enter your zip code.");
					allFormInputs[x].focus();
					return;
				}
				
				else if (allFormInputs[x].name == "city"){
					alert("Please enter your city.");
					allFormInputs[x].focus();
					return;
				}
				
				else if (allFormInputs[x].name == "state"){
					alert("Please choose a state.");
					allFormInputs[x].focus();
					return;
				}
				
				else if (allFormInputs[x].name == "phone"){
					alert("Please enter your phone number.");
					allFormInputs[x].focus();
					return;
				}
				
				else if(allFormInputs[x].name == "emailAddress"){ //if email 
					alert("Please enter your email address.");
					allFormInputs[x].focus();
					return;
				}
				
				else if(allFormInputs[x].name == "email"){ //if email 
					alert("Please enter your email address.");
					allFormInputs[x].focus();
					return;
				}
				
				else if(allFormInputs[x].name == "verifyemail"){ //if no verify email
					alert("Please verify your email address.");
					allFormInputs[x].focus();
					return;
				}
				
				else if(allFormInputs[x].name == "trans"){
					var qSelect = document.getElementById("trans");
					if(qSelect.options[qSelect.selectedIndex].value == ""){
						allFormInputs[x].focus();
						alert("Choose a transaction type.");
						return;
					}
				}				
			}
			
			if(allFormInputs[x].value == "Please") {
				alert("Please choose a state.");
				allFormInputs[x].focus();
				return;
			}
		}
		
		if(allFormInputs[x].name == "zip" || allFormInputs[x].name == "propertyzip"){//check if zip is all numerals
			var num = /(^\d+$)/;
			var str = allFormInputs[x].value;
			
			if (!num.test(str)){
				alert("Please enter only numerals for the zip code.");
				allFormInputs[x].focus();
				return;
			 }
		}
		
		if(allFormInputs[x].name == "phone"){//Make sure the phone number is input correctly
			var str = allFormInputs[x].value;
			var stripped = str.replace(/[\(\)\.\-\ ]/g, '');
			
			//strip out acceptable non-numeric characters
			if (isNaN(parseInt(stripped))) {
			  alert("The phone number contains illegal characters.");
			  allFormInputs[x].focus();
			  return;
			}
			
			if (!(stripped.length == 10)) {
				alert("The phone number is the wrong length. Make sure you included an area code.\n");
				allFormInputs[x].focus();
				return;
			}
		}
		
		if((allFormInputs[x].name == "emailAddress" && allFormInputs[x].value!="") || (allFormInputs[x].name == "email" && allFormInputs[x].value!="")){//check for valid email address
			var regex2 = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+\.[a-zA-Z]{2,4}$/;
			var str = allFormInputs[x].value;
			
			if(!str.match(regex2)){
				alert("Email address not valid!  Try again.");
				allFormInputs[x].focus();
				return;
			}
		}
		
		if(allFormInputs[x].name == "verifyemail"){//verify second email against first
			var firstEmail;
			
			if (document.getElementById('emailAddress')) {
				firstEmail = Spry.$$("#emailAddress");
			}
			
			else if (document.getElementById('email')) {
				firstEmail = Spry.$$("#email");	
			}
					
			if(allFormInputs[x].value != firstEmail[0].value){
				alert("Please verify correctly your email address.");
				allFormInputs[x].focus();
				return;
			}
		}
		
		if(allFormInputs[x].name == "typeOfCustomer") {
			var selection = document.getElementById("typeOfCustomer");
			//alert("selection.id: " + selection.id);
			//alert("selection.options: " + selection.options);
			//alert("selection.selectedIndex: " + selection.selectedIndex);
			//alert("selection.options[selection.selectedIndex].value: " + selection.options[selection.selectedIndex].value);
			if(selection.options[selection.selectedIndex].value == "Please select one") {
				allFormInputs[x].focus();
				alert("Please indicate who you are.");
				return;
			}
		}
		
	}
	
	form.submit();
}

