﻿// JScript File

function testredirect()
{
	
		document.formst.action = "default.asp"
		document.formst.submit()
 }
//checks for validity of email
function email(val)
{
	if ((val == " ")||(val.indexOf ('@') == -1)||(val.indexOf ('.') == -1))
		{alert("Email field seems to be invalid")      	
	return false}
  return true
}
//Checks for a field to be numeric
function isnumeric(t)
{
	if(!isNaN(t)) 
	{return false}
return true
}
//performs validations of numeric fields 
function validate1(fieldname,fieldvalue)
{
	if(isnumeric(fieldvalue))
	{alert(fieldname+ " has to be numeric")
	return false}
return true
}

//performs various validations according to parameters entered*//
function validate(fieldname,fieldvalue)
{
	if(isblank(fieldvalue))
	{alert(fieldname+" cannot be left blank")
	return false}
return true
}

//performs various validations according to parameters entered*//
function validateSelected(fieldname,fieldvalue,disallow)
{
	if(isblank(fieldvalue) || fieldvalue == disallow)
	{alert(fieldname+" cannot be left blank")	
	return false}
return true
}

// Check if a drop down is selected from default
function selected(fieldname,fieldvalue)
{
    if (fieldvalue == "")	
	{alert(fieldname+ " has to be selected")
	return false}
return true
}

//checks whether the field is blank or not and returns true false accordingly*//
function isblank(s)
{
	var len= s.length
	var i 
	for(i=0;i<len;++i)
	{if(s.charAt(i)!=" ") return false}
return true
}

// Strip commas from estimated value...
function cleanPrice(val)
{
    re = /,/g;
    val = val.replace(re,"");   // Strip commas
    val = val.replace("$","");  // Strip any additional $    
    return val
}

//performs validations about blank fields on submitting the form*//
function validateform()
{
	
	if(!validate("First Name",document.form1.FirstName.value))
	{document.form1.FirstName.focus()
	return false}
	if(!validate("Last Name",document.form1.LastName.value))
	{document.form1.LastName.focus()
	return false}
	if(!validate("Day Area Code",document.form1.DayAreaCode.value))
	{document.form1.DayAreaCode.focus()
	return false}
	if(!validate("Day Prefix",document.form1.DayPrefix.value))
	{document.form1.DayPrefix.focus()
	return false}
	if(!validate("Day Number",document.form1.DayNumber.value))
	{document.form1.DayNumber.focus()
	return false}
	if(!validate("Street Number",document.form1.StreetNumber.value))
	{document.form1.StreetNumber.focus()	
	return false}
	if(!validate("Street Name",document.form1.StreetName.value))
	{document.form1.StreetName.focus()	
	return false}
	if(!validate("City",document.form1.City.value))
	{document.form1.City.focus()	
	return false}
	if(!validate("Zip",document.form1.Zip.value))
	{document.form1.Zip.focus()
	return false}
	if(!validate("State/County",document.form1.County.value))
	{document.form1.County.focus()
	return false}
	if(!validate1("Zip",document.form1.Zip.value))
	{document.form1.Zip.focus()
	return false}
	if(!validate("Property Type",document.form1.PropertyType.value))
	{document.form1.PropertyType.focus()
	return false}
	if(!validate("How did you hear about us",document.form1.HowDidYouHear.value))
	{document.form1.HowDidYouHear.focus()
	return false}
	if(!validate("E Mail",document.form1.Email.value))
	{document.form1.Email.focus()	
	return false}
	if(!email(document.form1.Email.value)) 
	{document.form1.Email.focus()
	return false}
}

function doFormValidation(product, targetForm, page)
{
    
    // Validate common form fields...
    if(!validate("First Name",targetForm.firstName.value))
    {targetForm.firstName.focus(); 
    return false}
    if(!validate("Last Name",targetForm.lastName.value))
    {targetForm.lastName.focus()
    return false}

    // Validate the site specific form fields...
	switch (product)
	{
	    case 'sellHouse':	    	     
	    
	        switch (page)
	        {
	            case 'page1':   
	                // Clean estimated value (strip commas and $)
	                estimatedValue = targetForm.estimatedValue.value = cleanPrice(targetForm.estimatedValue.value) 
	                
	                if(!validate("Home Phone",targetForm.dayPhone.value))
	                {targetForm.dayPhone.focus()
	                return false}
	                if(!validate("Email",targetForm.email.value))
	                {targetForm.email.focus()	
	                return false}
	                if(!email(targetForm.email.value)) 
	                {targetForm.email.focus()
	                return false}	        	        	        
	                if(!validate("Address",targetForm.street.value))
	                {targetForm.street.focus()	
	                return false}
	                if(!validate("City",targetForm.city.value))
	                {targetForm.city.focus()	
	                return false}	            	                
	                if(!selected("State",targetForm.state.value))
	                {targetForm.state.focus()	            
	                return false}
	                if(!validate("Zip",targetForm.zip.value))
	                {targetForm.zip.focus()
	                return false}	                
	                if(!validateSelected("County",targetForm.county.value,"Choose county"))
	                {targetForm.county.focus()
	                return false}
	                if(!validate("Estimated Value",targetForm.estimatedValue.value))
	                {targetForm.estimatedValue.focus()
	                return false}	                
	                break; /* page1 */
	        
	            case 'page2':
	                // clean the prices
	                firstMtg = targetForm.firstMrtgPayoff.value = cleanPrice(targetForm.firstMrtgPayoff.value)
	                secMtg = targetForm.secMrtgPayoff.value = cleanPrice(targetForm.secMrtgPayoff.value)
	                repairAmt = targetForm.repairAmount.value = cleanPrice(targetForm.repairAmount.value)	                
	                	                
	                // Calculate the potentialEquity 
	                targetForm.potentialEquity.value = eval(estimatedValue - firstMtg - secMtg - repairAmt)
	                
	                if(!validate("Property Type",targetForm.propType.value))
	                {targetForm.PropertyType.focus()
	                return false}
	                if(!validate("How did you hear about us",targetForm.howDidYouHear.value))
	                {targetForm.HowDidYouHear.focus()
	                return false}	                
	                
	                break; /* page2 */
	        }	        
	        break; /* 1800noagent */
	}	
	
	return true;
}