// JavaScript Document
function submit_form()
{
	    //alert("inside submit_form");
  if (!validateFormOnSubmit(document.rental_form) ) 
		{
				    //alert("submit_form false");
    return false;
  } 
						  //alert("submit_form  true");
		return true;
		
}

function validateFormOnSubmit(theForm) {
		    //alert("inside validateFormOnSubmit");
  //document.getElementById('errormsg').style.visibility="hidden";
  //document.getElementById('errormsg').innerHTML = "";

  var reason = "";
				    //alert("validate name of applicant_name");
		
// check name of complex drop down
    if ( theForm.name_of_complex.selectedIndex == 0 )
    {
    theForm.name_of_complex.style.background = 'Yellow';
    fname = theForm.name_of_complex.name
    error = fname + " has not been entered.\n";
				reason += error;
    }
			
				if ( theForm.name_of_complex.selectedIndex != 0 )
    {
     theForm.name_of_complex.style.background = 'White';
    }
				

  reason += validateEmpty(theForm.applicant_name);
				    //alert("validate applicant_present_address");
  reason += validateEmpty(theForm.applicant_present_address);
  reason += validateEmpty(theForm.applicant_city);
  reason += validateEmpty(theForm.applicant_state);
  reason += validateEmpty(theForm.applicant_zip);
  reason += validatePhone(theForm.applicant_phone);
  reason += validateEmpty(theForm.family_1_name);
  reason += validateEmpty(theForm.family_1_DOB);
  reason += validateSSN(theForm.family_1_SSN);
  reason += validateEmpty(theForm.applicant_present_employer);
  reason += validatePhone(theForm.applicant_present_employer_telephone);
  reason += validateEmpty(theForm.applicant_present_employer_position);
  reason += validateEmpty(theForm.applicant_present_employer_salary);
  reason += validateEmpty(theForm.applicant_present_employer_years_there);
  reason += validateEmpty(theForm.name_address_of_present_landlord);
  reason += validatePhone(theForm.present_landlord_telephone);
  reason += validateEmpty(theForm.rent_amount);
  reason += validateEmpty(theForm.years_there);
  reason += validateEmpty(theForm.reason_for_leaving);
 
	// check crime drop down
  if ( theForm.crime.selectedIndex == 0 )
    {
    theForm.crime.style.background = 'Yellow';
    theForm.convicted_of_a_crime_explanation.style.background = 'White';
    fname = theForm.crime.name
    error = fname + " has not been entered.\n";
				reason += error;
    }
			
  // validate "conviced of a crime field only if "crime" field is YES
  if ( theForm.crime.selectedIndex == 1 )
    {
     theForm.crime.style.background = 'White';
     reason += validateEmpty(theForm.convicted_of_a_crime_explanation);
    }


  if ( theForm.crime.selectedIndex == 2 )
		  {
     theForm.crime.style.background = 'White';
     theForm.convicted_of_a_crime_explanation.style.background = 'White';
		  }
				
		
		
  reason += validateEmpty(theForm.nearest_relative_or_friend);
		
	
// check referral  drop down
    if ( theForm.referral.selectedIndex == 0 )
    {
    theForm.referral.style.background = 'Yellow';
    fname = theForm.referral.name
    error = fname + " has not been entered.\n";
				reason += error;
    }
			
				if ( theForm.referral.selectedIndex != 0 )
    {
     theForm.referral.style.background = 'White';
    }
		
		
  reason += validateEmpty(theForm.acknowledgement_initials);
  reason += validateEmpty(theForm.signature);
  reason += validateEmpty(theForm.signature_date);
	
/*
	alert("validate email");
  reason += validateEmail(theForm.email);
				    alert("validate website");
  reason += validateEmpty(theForm.website);
				    alert("validate phone");
  reason += validatePhone(theForm.phone_number);
*/
			
  if (reason != "") 
		{
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  return true;
}



function validateEmpty(fld) {
			    //alert("inside validateEmpty");
  var error = "";
		

  if (fld.value.length == 0 || fld.value == "") {
						    //alert("inside validateEmpty if field length is zero");
    fld.style.background = 'Yellow';
    fname = fld.name
    error = fname + " has not been entered.\n";
    //document.getElementById('errormsg').style.visibility="visible";
								
    //var currentmsg = document.getElementById('errormsg').innerHTML;

    /*    
    if (currentmsg == "") {		
      document.getElementById('errormsg').innerHTML = 'Please fill out the required fields.';
      document.getElementById('errormsg').style.visibility="visible";
      document.getElementById('errormsg').style.top="221px";
    }
    else if (currentmsg == "Please enter a valid email address." || currentmsg == "Remove illegal characters in Email." ) {
      var oldHTML = document.getElementById('errormsg').innerHTML;
      var newHTML = oldHTML + "<br />Please fill out the required fields.";
      document.getElementById('errormsg').innerHTML = newHTML;
      document.getElementById('errormsg').style.top="214px";
    }
								*/
										
  } 
  else {
    fld.style.background = 'White';
  }
  return error;   
}


function trim(s) {
  return s.replace(/^\s+|\s+$/, '');
} 


function validateEmail(fld) {
							    alert("inside validateEmail");
  var error="";
  var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
  var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
  var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
  if (fld.value == "") {
    fld.style.background = 'Yellow';
    fname = fld.name;
    error = fname + " has not been entered.\n";
  } 
  else if (!emailFilter.test(tfld)) {              //test email for illegal characters
    fld.style.background = 'Yellow';
    error = "Please enter a valid email address.\n";
        
    //var currentmsg = document.getElementById('errormsg').innerHTML;
    /*    
    if (currentmsg == "") {		
      document.getElementById('errormsg').innerHTML = 'Please enter a valid email address.';
      document.getElementById('errormsg').style.visibility="visible";
      document.getElementById('errormsg').style.top="221px";
    }
    else {
      var oldHTML = document.getElementById('errormsg').innerHTML;
      var newHTML = oldHTML + "<br />Please enter a valid email address.";
      document.getElementById('errormsg').innerHTML = newHTML;
      document.getElementById('errormsg').style.top="214px";
    }*/
  } 
		else if (fld.value.match(illegalChars)) {
    //fld.style.background = 'Yellow';
    error = "The email address contains illegal characters.\n";
								
    //var currentmsg = document.getElementById('errormsg').innerHTML;
      /*  
    if (currentmsg == "") {		
      document.getElementById('errormsg').innerHTML = 'Remove illegal characters in Email.';
      document.getElementById('errormsg').style.visibility="visible";
      document.getElementById('errormsg').style.top="221px";
    }
    else {
      var oldHTML = document.getElementById('errormsg').innerHTML;
      var newHTML = oldHTML + "<br />Remove illegal characters in Email.";
      document.getElementById('errormsg').innerHTML = newHTML;
      document.getElementById('errormsg').style.top="214px";
    }
								*/
  } 
  else {
    fld.style.background = 'White';
  }
  return error;
}


function validatePhone(fld) {
								    //alert("inside validatePhone");
  var error = "";
  var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

  if (fld.value == "") {
    fname = fld.name;
    error = fname + " has not been entered.\n";
    fld.style.background = 'Yellow';
  } 
  else if (isNaN(stripped)) {  
    error = "The phone number contains illegal characters.\n";
    fld.style.background = 'Yellow';
								
   // var currentmsg = document.getElementById('errormsg').innerHTML;
   /*
    if (currentmsg == "") {		
      document.getElementById('errormsg').innerHTML = 'Remove illegal characters in Phone.';
      document.getElementById('errormsg').style.visibility="visible";
      document.getElementById('errormsg').style.top="221px";
    }
    else if (currentmsg == "Please enter a valid email address." || currentmsg == "Remove illegal characters in Email." || currentmsg == "Please fill out the required fields." ) {
      var oldHTML = document.getElementById('errormsg').innerHTML;
      var newHTML = oldHTML + "<br />Remove illegal characters in Phone.";
      document.getElementById('errormsg').innerHTML = newHTML;
      document.getElementById('errormsg').style.top="214px";
    }			*/
    } else if (!(stripped.length == 10)) {
      error = "The phone number is the wrong length. Make sure you included an area code.\n";
      fld.style.background = 'Yellow';
  } else {
    fld.style.background = 'White';
  }
  return error;
}

function validateSSN(fld) {
								    //alert("inside validatePhone");
  var error = "";
  var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

  if (fld.value == "") {
    fname = fld.name;
    error = fname + " has not been entered.\n";
    fld.style.background = 'Yellow';
  } 
  else if (isNaN(stripped)) {  
    error = "The Social Security Number contains illegal characters.\n";
    fld.style.background = 'Yellow';
								
   // var currentmsg = document.getElementById('errormsg').innerHTML;
   /*
    if (currentmsg == "") {		
      document.getElementById('errormsg').innerHTML = 'Remove illegal characters in Phone.';
      document.getElementById('errormsg').style.visibility="visible";
      document.getElementById('errormsg').style.top="221px";
    }
    else if (currentmsg == "Please enter a valid email address." || currentmsg == "Remove illegal characters in Email." || currentmsg == "Please fill out the required fields." ) {
      var oldHTML = document.getElementById('errormsg').innerHTML;
      var newHTML = oldHTML + "<br />Remove illegal characters in Phone.";
      document.getElementById('errormsg').innerHTML = newHTML;
      document.getElementById('errormsg').style.top="214px";
    }			*/
    } else if (!(stripped.length == 9)) {
      error = "The Social Security Number is the wrong length.\n";
      fld.style.background = 'Yellow';
  } else {
    fld.style.background = 'White';
  }
  return error;
}