////////////////////////
// Validation Routines
////////////////////////
var d = document;
var num_only_rx = /\D/g;

function validate_freeReport( theForm )
{
	//alert("validateFrpt");
	//return false;
	
	var isValid = true;
	var outStr = "";
	
	if( theForm.fname.value == "" )
	{
		isValid = false;
		outStr += "Please enter your First Name.\n";
	}
	if( theForm.lname.value == "" )
	{
		isValid = false;
		outStr += "Please enter your Last Name.\n";
	}
		
	if(!validate_email(theForm.email.value))
	{
		isValid = false;
		outStr += "Please enter valid Email Address.\n";
	}
	
	if(!validate_phone(theForm.phone.value) )
	{
		isValid = false;
		outStr += "Please enter valid Home Phone.\n";
	}
	var checked = false;
	if (theForm.frpt1 != null && theForm.frpt1.checked)
		checked = true;
	if(theForm.frpt2 != null && theForm.frpt2.checked)
		checked = true;
	if(theForm.frpt3 != null && theForm.frpt3.checked)
		checked = true;
	if(theForm.frpt4 != null && theForm.frpt4.checked)
		checked = true;
	if(theForm.frpt5 != null && theForm.frpt5.checked)
		checked = true;
	if(theForm.frpt6 != null && theForm.frpt6.checked)
		checked = true;
	if(theForm.frpt7 != null && theForm.frpt7.checked)
		checked = true;
	if(theForm.frpt8 != null && theForm.frpt8.checked)
		checked = true;
	if(theForm.frpt9 != null && theForm.frpt9.checked)
		checked = true;
	if(theForm.frpt10 != null && theForm.frpt10.checked)
		checked = true;
	
	if(!checked)
	{
		isValid = false;
		outStr += "You have not selected any reports.\n";
	}
	if( !isValid ) alert( outStr );
	
	return isValid;
	
}

function validate_membersSignup()
{
  var zip = get_value('reg_zipcode');
  var email = get_value('reg_email');
  var home_phone = get_value('reg_home_phone');

  var fname = get_value('reg_first_name');
  if(!fname || fname != Trim(fname))
  {
    alert('You must enter your first name');
    document.register.reg_first_name.focus();
    return false;
  }
  
  var lname = get_value('reg_last_name');
  if(!lname || lname != Trim(lname))
  {
    alert('You must enter your last name');
    document.register.reg_last_name.focus();
    return false;
  }
  
  if(!validate_phone(home_phone))
  {
    alert('Please enter valid home phone number, including 3-digit area code');
    document.register.reg_home_phone.focus();
    return false;
  }
  
  if(document.register.reg_work_phone.value != "" && !validate_phone(document.register.reg_work_phone.value))
  {
    alert('Please enter valid work phone number, including 3-digit area code');
    document.register.reg_work_phone.focus();
    return false;
  }
  
  if(document.register.reg_cell_phone.value != "" && !validate_phone(document.register.reg_cell_phone.value))
  {
    alert('Please enter valid cell phone number, including 3-digit area code');
    document.register.reg_cell_phone.focus();
    return false;
  }
  
  if(!validate_zipcode(zip))
  {
    alert('You must enter your 5-digit zip code');
    document.register.reg_zipcode.focus();
    return false;
  }
  
  
  if(!validate_email(email))
  {
    alert('Please enter a valid email address');
    document.register.reg_email.focus();
    return false;
  }
  
  var pwd = get_value('reg_password');
  
  if(!pwd || pwd != Trim(pwd))
  {
    alert('You must enter password');
    document.register.reg_password.focus();
    return false;
  }
   
  if(get_value('reg_password') != get_value('reg_vpassword'))
  {
    alert('Your passwords did not match');
    document.register.reg_vpassword.focus();
    return false;
  }

  return true;
}

function validate_membersRegistration()
{
  var zip = get_value('reg_zipcode');
  var email = get_value('reg_email');
  var home_phone = get_value('reg_home_phone');

  if(!get_value('reg_first_name'))
  {
    alert('You must enter your first name');
    return false;
  }
  
  if(!get_value('reg_last_name'))
  {
    alert('You must enter your last name');
    return false;
  }
  
  if(!validate_phone(home_phone))
  {
    alert('Please enter your home phone number, including 3-digit area code');
    return false;
  }
  
  if(!validate_zipcode(zip))
  {
    alert('You must enter your 5-digit zip code');
    return false;
  }


  
  if(!validate_email(email))
  {
    alert('Please enter a valid email address');
    return false;
  }

  return true;

}

function validate_pwd()
{
  if(get_value('mpo_pwd') != get_value('mpo_verpwd'))
  {
    alert('Your passwords did not match');
    return false;
  }
}
  
function getQueryVariable(variable) 
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) 
  {
    var pair = vars[i].split("=");
    if (pair[0] == variable) 
    {
      return pair[1];
    }
  } 
  alert('Query Variable ' + variable + ' not found');
}


function validate_ChangePwd()
{
	if(!get_value('pwd') || !get_value('verPwd'))
	{
		alert('Enter "password" and "verify password"');
		return false;
	}
	
	if(get_value('pwd') != get_value('verPwd'))
	{
		alert('Your passwords do not match');
		return false;
	}
	else 
	{		
		var urlID = document.getElementById('urlID');	
		urlID.value = getQueryVariable('id');		
	 	return true;
	 }
}
function validate_nlSignup(theForm)
{	
  if (theForm.nlSubscriber_email.value == "")
  {
    alert("Please enter a value for the \"email\" field.");
    theForm.nlSubscriber_email.focus();
    return (false);
  }
  if (theForm.nlSubscriber_email.value.length < 5)
  {
    alert("Please enter at least 5 characters in the \"email\" field.");
    theForm.nlSubscriber_email.focus();
    return (false);
  }
  
  var app_email = theForm.nlSubscriber_email.value;
	if(!validate_email(nlSubscriber_email))
	{
		alert('Please provide valid email address');
		theForm.nlSubscriber_email.focus();
		return false;
	}  
  return (true);
}
function validate_QuestionForm(theForm)
{	
  if (theForm.qform_name.value == "")
  {
    alert("Please enter a value for the \"name\" field.");
    theForm.qform_name.focus();
    return (false);
  }
  if (theForm.qform_email.value == "")
  {
    alert("Please enter a value for the \"email\" field.");
    theForm.qform_email.focus();
    return (false);
  }
  if (theForm.qform_email.value.length < 5)
  {
    alert("Please enter at least 5 characters in the \"email\" field.");
    theForm.qform_email.focus();
    return (false);
  }
  
  var app_email = theForm.qform_email.value;
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		theForm.qform_email.focus();
		return false;
	}
	
	if(theForm.qform_phone1.value != "" && !validate_phone(theForm.qform_phone1.value))
	{
		alert('Please provide valid "Phone"');
		theForm.qform_phone1.focus();
		return false;
	}
	
	if(theForm.qform_phone2.value != "" && !validate_phone(theForm.qform_phone2.value))
	{
		alert('Please provide valid "Evening Telephone"');
		theForm.qform_phone2.focus();
		return false;
	}
  
  return (true);
}

function validate_rateFinder(theForm)
{	
  if (theForm.rate_fName.value == "")
  {
    alert("Please enter a value for the First Name field.");
    theForm.rate_fName.focus();
    return (false);
  }
   if (theForm.rate_lName.value == "")
  {
    alert("Please enter a value for the Last Name field.");
    theForm.rate_lName.focus();
    return (false);
  }
  if (theForm.rate_email.value == "")
  {
    alert("Please enter a value for the \"email\" field.");
    theForm.rate_email.focus();
    return (false);
  }
  if (theForm.rate_email.value.length < 5)
  {
    alert("Please enter at least 5 characters in the \"email\" field.");
    theForm.rate_email.focus();
    return (false);
  }
  
  var app_email = theForm.rate_email.value;
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		theForm.rate_email.focus();
		return false;
	}
	
	if(theForm.rate_phone1.value != "" && !validate_phone(theForm.rate_phone1.value))
	{
		alert('Please provide valid "Home Telephone"');
		theForm.rate_phone1.focus();
		return false;
	}
	 
  return (true);
}
function validate_QuickApp(theForm)
{
  if (theForm.qApp_fName.value == "")
  {
    alert("Please enter a value for the \"first name\" field.");
    theForm.qApp_fName.focus();
    return (false);
  }
  if (theForm.qApp_lName.value == "")
  {
    alert("Please enter a value for the \"last name\" field.");
    theForm.qApp_lName.focus();
    return (false);
  }
  if (theForm.qApp_lName.value == theForm.qApp_fName.value)
  {
    alert("Please enter a real values for the \"first name\" and \"last name\" fields.");
    theForm.qApp_fName.focus();
    return (false);
  }
  if (theForm.qApp_email.value == "")
  {
    alert("Please enter a value for the \"email\" field.");
    theForm.qApp_email.focus();
    return (false);
  }

  
  var app_email = theForm.qApp_email.value;
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		theForm.qApp_email.focus();
		return false;
	}
	
	if(theForm.qApp_phone.value == "" || !validate_phone(theForm.qApp_phone.value))
	{
		alert('Please provide valid "Phone"');
		theForm.qApp_phone.focus();
		return false;
	}
	
	if(theForm.qApp_lAmt.value == "" || !IsNumeric(theForm.qApp_lAmt.value))
	{
		alert('Please provide valid "Loan Amount"');
		theForm.qApp_lAmt.focus();
		return false;
	}
  	if(theForm.qApp_expense.value == "" || !IsNumeric(theForm.qApp_expense.value))
	{
		alert('Please provide valid "Total Monthly Expenses"');
		theForm.qApp_expense.focus();
		return false;
	}
		if(theForm.qApp_income.value == "" || !IsNumeric(theForm.qApp_income.value))
	{
		alert('Please provide valid "Total Monthly Income"');
		theForm.qApp_income.focus();
		return false;
	}
  return (true);
}
function validate_ZipForm(theForm)
{
  if (theForm.zform_fName.value == "" || theForm.zform_fName.value!=Trim(theForm.zform_fName.value) )
  {
    alert("Please enter a value for the \"first name\" field.");
    theForm.zform_fName.focus();
    return (false);
  }
 
  if (theForm.zform_lName.value == "" || theForm.zform_lName.value!=Trim(theForm.zform_lName.value))
  {
    alert("Please enter a value for the \"last name\" field.");
    theForm.zform_lName.focus();
    return (false);
  }
  
  if(!validate_phone(theForm.zform_phone.value))
	{
		alert('Please enter your phone number');
		theForm.zform_phone.focus();
		return false;
	}
	
  if (theForm.zform_email.value == "")
  {
    alert("Please enter a value for the \"email address\" field.");
    theForm.zform_email.focus();
    return (false);
  }
  
   var app_email = theForm.zform_email.value;
   
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		theForm.zform_email.focus();
		return false;
	}
	
  if (theForm.zform_p_addr.value == "")
  {
    alert("Please enter a value for the \"property address\" field.");
    theForm.zform_p_addr.focus();
    return (false);
  }
  
  if (theForm.zform_p_city.value == "")
  {
    alert("Please enter a value for the \"property city\" field.");
    theForm.zform_p_city.focus();
    return (false);
  }
  
  if (theForm.zform_p_state.value == "")
  {
    alert("Please enter a value for the \"property state\" field.");
    theForm.zform_p_state.focus();
    return (false);
  }

	if(!validate_zipcode(theForm.zform_zip.value))
	{
		alert('Please provide valid zip code');
		theForm.zform_zip.focus();
		return false;
	}
	
	if(!IsNumeric(theForm.zform_p_footage.value))
	{
		alert('Please provide numeric value for "Approx. Square Footage"');
		theForm.zform_p_footage.focus();
		return false;
	}
	
	if(theForm.zform_p_sDate.value == "Choose One")
	{
		alert('Please provide input for "Selling Date"');
		theForm.zform_p_sDate.focus();
		return false;
	}
  return (true);
}

function validate_ZipForm_Captcha(theForm)
{
  if (theForm.zform_fName.value == "" || theForm.zform_fName.value!=Trim(theForm.zform_fName.value) )
  {
    alert("Please enter a value for the \"first name\" field.");
    theForm.zform_fName.focus();
    return (false);
  }
 
  if (theForm.zform_lName.value == "" || theForm.zform_lName.value!=Trim(theForm.zform_lName.value))
  {
    alert("Please enter a value for the \"last name\" field.");
    theForm.zform_lName.focus();
    return (false);
  }
  
  if (theForm.zform_email.value == "")
  {
    alert("Please enter a value for the \"email address\" field.");
    theForm.zform_email.focus();
    return (false);
  }
  
   var app_email = theForm.zform_email.value;
   
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		theForm.zform_email.focus();
		return false;
	}
	
  if (theForm.zform_p_addr.value == "")
  {
    alert("Please enter a value for the \"property address\" field.");
    theForm.zform_p_addr.focus();
    return (false);
  }
  
  if (theForm.zform_p_city.value == "")
  {
    alert("Please enter a value for the \"property city\" field.");
    theForm.zform_p_city.focus();
    return (false);
  }
  
  if (theForm.zform_p_state.value == "")
  {
    alert("Please enter a value for the \"property state\" field.");
    theForm.zform_p_state.focus();
    return (false);
  }

	if(!validate_zipcode(theForm.zform_zip.value))
	{
		alert('Please provide valid zip code');
		theForm.zform_zip.focus();
		return false;
	}
	
	if(!IsNumeric(theForm.zform_p_footage.value))
	{
		alert('Please provide numeric value for "Approx. Square Footage"');
		theForm.zform_p_footage.focus();
		return false;
	}
	
	if(theForm.zform_p_sDate.value == "Choose One")
	{
		alert('Please provide input for "Selling Date"');
		theForm.zform_p_sDate.focus();
		return false;
	}
  return (true);
}



function validate_HomeSearch()
{	

		
	if(!document.bform1.bform_name.value)
	{
		alert('Please enter your Name');
		document.bform1.bform_name.focus();
		return false;
	}
	
	if(!document.bform1.bform_email.value)
	{
		alert('Please enter your email address');
		document.bform1.bform_email.focus();
		return false;
	}
	
	var app_email = document.bform1.bform_email.value;
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		document.bform1.bform_email.focus();
		return false;
	}
		
	if(!validate_phone(document.bform1.bform_phone.value))
	{
		alert('Please enter valid phone number');
		document.bform1.bform_phone.focus();
		return false;
	}
	return true;
}
	
	
function validate_HomeSearchExtended()
{	
		
	if(!document.bform1.bform_name.value)
	{
		alert('Please enter your Name');
		document.bform1.bform_name.focus();
		return false;
	}
	
	if(!document.bform1.bform_email.value)
	{
		alert('Please enter your email address');
		document.bform1.bform_email.focus();
		return false;
	}
	
	var app_email = document.bform1.bform_email.value;
	if(!validate_email(app_email))
	{
		alert('Please provide valid email address');
		document.bform1.bform_email.focus();
		return false;
	}
	
	var app_phone = document.bform1.bform_phone.value;
	
	if(!validate_phone(app_phone))
	{
		alert('Please enter your phone number');
		document.bform1.bform_phone.focus();
		return false;
	}
	
	if(document.bform1.bform_own.options[0].selected)
	{
		alert('Please answer for "Do you currently own a home?"');
		document.bform1.bform_own.focus();
		return false;
	}
	
	if(document.bform1.bform_own.value == "1" && document.bform1.bform_selling.options[0].selected)
	{
		alert('Please answer for "Are you selling your current home?"');
		document.bform1.bform_selling.focus();
		return false;
	}
	
	if( document.bform1.bform_move.options[0].selected)
	{
		alert('Please answer for "When do you plan to buy or move?"');
		document.bform1.bform_move.focus();
		return false;
	}
		
	if(toNumber(document.bform1.bform_priceHigh.value) < toNumber(document.bform1.bform_priceLow.value))
	{
		alert('In Price range, "Low price" must be less than "High price"');
		document.bform1.bform_priceLow.focus();
		return false;		
	}
	
	return true;
}

function validate_Login()
{
	
	if(!document.frmLogin.mpo_username.value)
	{
		alert('Please enter user name');
		document.frmLogin.mpo_username.focus();
		return false;
	}
	
	if(!document.frmLogin.mpo_password.value)
	{
		alert('Please enter password');
		document.frmLogin.mpo_password.focus();
		return false;
	}
	return true;
	
}

function validate_Appt(theForm){
	var app_name = theForm.appointment.name.value;
	var app_phone = theForm.appointment.phone.value;
	var app_email = theForm.appointment.email.value;
	if(!app_name || app_name == 'My Name'){
		alert('Please enter your name');
		return false;
		}
	if(!validate_phone(app_phone) || app_phone == '619-555-1234'){
		alert('Please enter your phone number');
		return false;
		}
	if(!validate_email(app_email) || app_email == 'name@example.org'){
		alert('Please enter your email address');
		return false
		}
	return true;
}

//////////////////////////////
//////HELPER FUNCTIONS//////
////////////////////////////
function get_value(id){
  return d.getElementById(id).value;
}
function validate_email(email) {
  var e_rx = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
  var regex = new RegExp(e_rx);
  return regex.test(email);
}

function validate_zipcode(tZip){
  tZip = tZip.replace(num_only_rx, '');
  return (tZip.length == 5);  
}

function validate_phone(pnumber){
  pnumber = pnumber.replace(num_only_rx, '');
  return (pnumber.length == 10);  
}

function toNumber(stringValue)
{
	var numVal = stringValue * 1;
	return numVal;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var Char;
   var DecimalOccured = false;
   var IsNumber = true;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
            
      if(Char=='.')
      {
		if(DecimalOccured)
		{
			IsNumber = false;
		}
		else
		{
			DecimalOccured = true;
		}
      }
      
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
   }
   return IsNumber;
   
}

function ReplaceSpecial(sText)
{
   
   alert(sText);
   var SpecialChars = "'\"";
   var Char;
   var resultString = "";

 
   for (i = 0; i < sText.length; i++) 
   { 
      Char = sText.charAt(i);            
            
      if (SpecialChars.indexOf(Char) == -1) 
      {
         resultString = resultString + Char;
      }
      else
      {
		resultString = resultString + "\\" + Char;
      }
   }
   return resultString;
   alert(resultString);
}

function Trim(TRIM_VALUE)
{
	if(TRIM_VALUE.length < 1)
	{
		return"";
	}
	
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	
	if(TRIM_VALUE=="")
	{
		return "";
	}
	else
	{
		return TRIM_VALUE;
	}
} //End Function

function RTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	
	if(v_length < 0)
	{
		return"";
	}

	var iTemp = v_length -1;

	while(iTemp > -1)
	{
		if(VALUE.charAt(iTemp) == w_space){}
		else
		{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;

	} //End While
	
	return strTemp;

} //End Function

function LTrim(VALUE)
{
	var w_space = String.fromCharCode(32);
	if(v_length < 1)
	{
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length)
	{
		if(VALUE.charAt(iTemp) == w_space){}
		else
		{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	
	return strTemp;
} //End Function


