﻿	<!--
	
		/*
			Function:	validate
			Purpose:	validate fields
			In:			form object
			Out:		if all values are fine, return true. Otherwise, false			
		*/
		function validate(whatform)
		{		
		
			with (whatform)
			{
			
				/* 
					Validate required values first 
				*/
				if ( Required(fullname,"Please provide your first and last name.") == false )
				{
					fullname.focus();
					return false;
				}
				
				// --------- HOME PHONE --------
				if ( Required(homephone,"Please provide your home phone number.") == false )
				{
					homephone.focus();
					return false;
				}

				if (!(/^\(?\d{3}\)?\s|-\d{3}-\d{4}$/.test(homephone.value)))
				{
					alert ("Please enter a home phone number in xxx-xxx-xxxx format.");
					homephone.focus();
					return false;
				}
				
				// --------- EMAIL --------
				if ( Required(emailid,"Please provide your email id.") == false )
				{
					emailid.focus();
					return false;
				}				
				
				if ( EmailCheck(emailid,"Please provide a valid email address.") == false )
				{
					emailid.focus();
					return false;
				}	
				
				// --------- WORK PHONE --------
				if (!(workphone.value==null || workphone.value==""))
				{
					if (!(/^\(?\d{3}\)?\s|-\d{3}-\d{4}$/.test(workphone.value)))
					{
						alert ("Please enter a work phone number in xxx-xxx-xxxx format.");
						workphone.focus();
						return false;
					}
				}
				
				// --------- FAX --------
				if (!(fax.value==null || fax.value==""))
				{
					if (!(/^\(?\d{3}\)?\s|-\d{3}-\d{4}$/.test(workphone.value)))
					{
						alert ("Please enter a fax number in xxx-xxx-xxxx format.");
						fax.focus();
						return false;
					}
				}

				// --------- STATE --------
				if (!(state.value==null || state.value==""))
				{
					if (!(/^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i.test(state.value)))
					{
						alert ("Please enter a valid state abbreviation (e.g. IA for Iowa)");
						state.focus();
						return false;
					}
				}
				
				// --------- ZIP --------
				if(!(zip.value==null || zip.value==""))
				{
					if (!(/\d{5}(-\d{4})?/.test(zip.value)))
					{
						alert ("Please enter a valid zip code");
						zip.focus();
						return false;
					}
				}
				
				
				
				// --------- NUMBERS --------
				if (!(ValidateMinMaxValues(yearbuilt,1800,2200,"Please enter a valid year the house was built.")))
				{
					yearbuilt.focus();
					return false;
				}				
	
	
				if (!(ValidateMinMaxValues(beds,0,100,"Please enter valid number of bathrooms.")))
				{
					beds.focus();
					return false;
				}
						
	
				if (!(ValidateMinMaxValues(baths,0,100,"Please enter valid number of bathrooms.")))
				{
					baths.focus();
					return false;
				}				
				
	
				if (!(ValidateMinMaxValues(squarefootage,0,100000000,"Please enter a valid number of square footage.")))
				{
					squarefootage.focus();
					return false;
				}
				
					
				if (!(ValidateMinMaxValues(workphoneextension,0,100000000,"Please enter a valid work phone extension (e.g. 2413).")))
				{
					workphoneextension.focus();
					return false;
				}
								

				if (!(ValidateMinMaxValues(askingrate,0,100000000,"Please keep asking rate between 0 and 100 million.")))
				{
					askingrate.focus();
					return false;
				}
				

				if (!(ValidateMinMaxValues(appraisal,0,100000000,"Please keep appraisal between 0 and 100 million.")))
				{
					appraisal.focus();
					return false;
				}
				
				
				if (!(ValidateMinMaxValues(loanbalance1,0,100000000,"Please keep loan balance between 0 and 100 million.")))
				{
					loanbalance1.focus();
					return false;
				}
				

				if (!(ValidateMinMaxValues(monthlypayments1,0,100000000,"Please keep monthly payments between 0 and 100 million.")))
				{
					monthlypayments1.focus();
					return false;
				}


				if (!(ValidateMinMaxValues(loanbalance2,0,100000000,"Please keep loan balance between 0 and 100 million.")))
				{
					loanbalance2.focus();
					return false;
				}
				

				if (!(ValidateMinMaxValues(monthlypayments2,0,100000000,"Please keep monthly payments between 0 and 100 million.")))
				{
					monthlypayments2.focus();
					return false;
				}
				

				if (!(ValidateMinMaxValues(leastoffer,0,100000000,"Please keep least offer between 0 and 100 million.")))
				{
					leastoffer.focus();
					return false;
				}
				
				
				if ( Required(security_image,"Please enter the text from the image") == false )
				{
					security_image.focus();
					return false;
				}
				

				
			}	
			return true;
		}
		
		

		/*
			Function:	ValidateMinMaxValues
			Purpose:	validates incoming fields value if it is not empty
						and checks if it is within the given min and max range
						If not, throws an alert and returns false, otherwise returns true
			In:			fieldname, minimum value the field can take, max value, alert
			Out:		true if field is good, false otherwise
		*/		
		function ValidateMinMaxValues(fieldname,minvalue,maxvalue,message)		
		{
			with (fieldname)
			{
				if (!(value==null || value==""))
				{
					// validate without the comma
					var valueTemp = value.replace(/,/g,'');					
					if (!(valueTemp >= minvalue && valueTemp <= maxvalue ))
					{						
						alert (message);
						return false;
					}
					else
					{
						return true;
					}
				}
				else
				{
					return true;
				}
			}
		}
		
		

		/*
			Function:	EmailCheck
			Purpose:	validates incoming email address
			In:			fieldname and message
			Out:		true if email address is valid, false otherwise
		*/
		function EmailCheck(fieldname,message)
		{
			with (fieldname)
			{
				if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(value))) 
				{
					alert(message);
					return false;
				}
				else
				{
					return true;
				}
			}
		}
		
		
		
		/*
			Function:	Required
			Purpose:	Checks fieldname to see if it is empty. If empty, 
						throws error message and returns false
			In:			field name and alert message text
			Out:		true if value is contained in the field, false otherwise			
		*/
		function Required(fieldname,message)
		{
			with (fieldname)
			{
				if (value==null || value=="")
				{
					alert(message);
					return false;
				}
				else
				{
					return true;
				}
			}
		}		
	-->


