// JavaScript Document
function submit_form()
{
	    //alert("inside submit_form");
  if (!validateFormOnSubmit(document.work_order_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");
		
  reason += validateEmpty(theForm.Tenant_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';
    }


				    //alert("validate applicant_present_address");
  reason += validateEmpty(theForm.apartment);
  reason += validatePhone(theForm.Day_phone);
  reason += validatePhone(theForm.Evening_phone);
  reason += validateEmail(theForm.email_address);
  reason += validateEmpty(theForm.Description);
  reason += validateRadioButtons(theForm.Permission_to_enter);



			
  if (reason != "") 
		{
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  return true;
}


function validateRadioButtons(fld) {

	  var error = "";
   var interval = fld.length;
   
			for (i=0; i<interval; i++) {
     if(fld[i].checked) {
       var radio_but = fld[i].value;
     }
   }
   if (!radio_but) {
    //fld.style.background = 'Yellow';
    //fname = fld.name
    error = "Please let us know if we can enter your apartment.\n";
   }
			//else {
   // fld.style.background = 'White';
   //}
		 return error;
}





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) {
  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;
}