﻿function isValidDate(dateStr) {
// Checks for the following valid date format: MM/DD/YYYY
// Also separates date into month, day, and year variables
var y = document.getElementById(dateStr).value;

var datePat = /^(\d{1,2})(\/)(\d{1,2})\2(\d{4})$/;

var matchArray = y.match(datePat);
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) { // check date range
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) { // check if month has 31 days
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;  // date is valid
}

// returns false if the email, passed in as a parameter, is incorrectly formatted and 
// if the two email boxes do not mathch
function checkEmail(x,z) 
{	
	//email text box
	x="ctl00_mastercontent_"+x;
	var y=document.getElementById(x).value;
	
	//email confirm text box
	var a=document.getElementById(z).value;
	
	// returns false if the email is incorrectly formatted
	if (y.search(/^((\w|-)+\.)*(\w|-)+@((\w|-)+\.)+\w{2,4}$/) == -1) {
		alert("Please enter a valid email address, example: jsmith@yahoo.com");
		document.getElementById(x).value = '';
		return false;
	}

	// returns false if email address does not match confirm email address
	if(a != "")
	{
	    if (a != y){
		    alert("The Email Address Box and the Re-Enter Email Box do not match");
		    document.getElementById(z).value = y;		   
		    return false;
	    }
	}
	return true;
}

function validateEmail()
{
   
   var x= document.getElementById("ctl00_mastercontent_email").value;
   var z= document.getElementById("ctl00_mastercontent_email2").value;   
   var u= document.getElementById("ctl00_mastercontent_COMPL").value;  
   var i= document.getElementById("ctl00_mastercontent_phoneAnonymous").checked;
   var p= document.getElementById("ctl00_mastercontent_area").value + document.getElementById("ctl00_mastercontent_number").value + document.getElementById("ctl00_mastercontent_suffix").value;
   var strField="";
	
	// returns false if the email is incorrectly formatted
	if(x != '' || z != '')
	{
	    if ((x.search(/^((\w|-)+\.)*(\w|-)+@((\w|-)+\.)+\w{2,4}$/) == -1) || (z.search(/^((\w|-)+\.)*(\w|-)+@((\w|-)+\.)+\w{2,4}$/) == -1)) {
	      strField = "- A valid email address, example: jsmith@yahoo.com \n";
	    }
	}
	if(i == true && p.length < 10){	    
	     strField += "- A valid telephone number or un-check call back box. \n";
	}
	if(u == ''){
	     strField += "- The Incident Description \n";	    
	}
	
	if( strField != '')
	{
	      alert( "_________________________________________________________________________________\n" +
                   "HOW AM I DRIVING\n\n" +
		           "Please double-check and complete the field(s) listed below before continuing:\n\n" + strField +
		           "\n \n if you have any questions about these fields or further questions please call (626) 458-5100. \n" +
		           "________________________________________________________________________________ ");
		           return false;
	}
	return true;
	
}

function AutoTab(x,y)
{  
   if(document.getElementById(x).value.length==3){
      document.getElementById("ctl00_mastercontent_"+y).focus();
   }
    
}

//Validate Telephone Number Format, return false if format is not xxx-xxx-xxxx
function phoneCheck(x)
{
   var y=document.getElementById(x).value;
   
   if(y.search(/\d{3}\-\d{3}\-\d{4}/)==-1)
   {
      alert("The phone number you entered is not valid. Please enter a phone number with the format xxx-xxx-xxxx.");
      document.getElementById(x).value = '';
      return false;
   }
   return true;
}

//---------------------------------------------------------------------||
// FUNCTION:    formValidate                                           ||
// PARAMETERS: none                                                    ||
// RETURNS:                                                            ||
// PURPOSE: form validation. User needs to fill out requiered fields   ||
//---------------------------------------------------------------------||   
function formValidate()
{                
   var box="";
   if(document.getElementById("frmType").value.length < 1)
        box+=" - MAP TYPE. \n";    
   if(document.getElementById("unit").value.length < 1)
        box+=" - NUMBER OF LOTS. \n";    
   if(document.getElementById("address").value.length < 1)
        box+=" - ADDRESS. \n";
   if(document.getElementById("zipCode").value.length < 1)
        box+=" - ZIP CODE. \n";    
   if(document.getElementById("thomasNo").value.length < 1)
        box+=" - THOMAS NUMBER. \n";   
   if(document.getElementById("apnNo1").value.length < 1)
        box+=" - ASSESSOR PARCEL NO. \n";
   if(document.getElementById("ownerName").value.length < 1)
        box+=" - OWNER NAME. \n";       
   if(document.getElementById("ownerAddress").value.length < 1)
        box+=" - OWNER ADDRESS. \n";   
   if(document.getElementById("ownerTelephone").value.length < 1)
        box+=" - OWNER PHONE. \n";   
   if(document.getElementById("ownerCity").value.length < 1)
        box+=" - OWNER CITY. \n";
   if(document.getElementById("ownerState").value.length < 1)
        box+=" - OWNER STATE. \n";   
   if(document.getElementById("ownerEmail").value.length < 1)
        box+=" - OWNER EMAIL. \n";   
   if(document.getElementById("engineer").value.length < 1)
        box+=" - ENGINEER NAME. \n";
   if(document.getElementById("engTelephone").value.length < 1)
        box+=" - ENGINEER PHONE. \n";   
   if(document.getElementById("engType").value.length < 1)
        box+=" - ENGINEER TYPE. \n"; 
   if(document.getElementById("engNumber").value.length < 1)  
        box+=" - ENGINEER NUMBER. \n";  
   if(document.getElementById("expirationDate").value.length < 1)  
        box+=" - EXPIRATION DATE. \n";
   if(document.getElementById("engAddress").value.length < 1)  
        box+=" - ENGINEER ADDRESS. \n";
   if(document.getElementById("engCity").value.length < 1)  
        box+=" - ENGINEER CITY. \n";
   if(document.getElementById("engState").value.length < 1)  
        box+=" - ENGINEER STATE. \n";
   if(document.getElementById("engZipCode").value.length < 1)  
        box+=" - ENGINEER ZIP CODE. \n";
   if(document.getElementById("engEmail").value.length < 1)  
        box+=" - ENGINEER MAIL. \n";
        
   if(document.getElementById("chckboxSub").checked == true)
   {    
        if(document.getElementById("subdividerName").value.length < 1)  
            box+=" - SUBDIVIDER NAME. \n";
        if(document.getElementById("subdividerTelephone").value.length < 1)  
            box+=" - SUBDIVIDER PHONE. \n";
        if(document.getElementById("subdividerEmail").value.length < 1)  
            box+=" - SUBDIVIDER EMAIL. \n";
   }
  
   if(box.length > 0)
   {
      alert( "_____________________________________________________________________________________\n" +
             "DMR ALERT MESSAGE \n\n" +
             "Please double-check and complete the field(s) listed below before continuing:\n\n" + box +
	         "\n \n If you have any questions about these fields, feel free to contact the DMR office at 1 626-555-5555. \n" +
	         "_____________________________________________________________________________________ ");		  
	         
	  return false; 
   } 
    else
	{
	   return true;
	}
}

//---------------------------------------------------------------------||
// FUNCTION:    check box validation                                   ||
// PARAMETERS: none                                                    ||
// RETURNS:                                                            ||
// PURPOSE: User need to check the box to be able to submit form       ||
//---------------------------------------------------------------------||  
function checkBoxVal()
{
   var box="- The 'I agree' Check box";
   if(document.getElementById("correctInfo").checked == false)
   {
    alert( "_____________________________________________________________________________________\n" +
             "DMR ALERT MESSAGE \n\n" +
             "Please double-check and complete the field(s) listed below before continuing:\n\n" + box +
	         "\n \n If you have any questions about these fields, feel free to contact the DMR office at 1 626-555-5555. \n" +
	         "_____________________________________________________________________________________ ");		
	         
	         return false;
   }  
   else{   
        return true;
      }
} 
//---------------------------------------------------------------------||
// FUNCTION:    check form validation                                  ||
// PARAMETERS: none                                                    ||
// RETURNS:                                                            ||
// PURPOSE: User need to check the box to be able to submit form       ||
//---------------------------------------------------------------------||  
function searchCheck(x)
{
    var box="- A Map ID.";
    if(x==null)
    {
        alert( "_____________________________________________________________________________________\n" +
               "DMR ALERT MESSAGE \n\n" +
               "Please double-check and complete the field(s) listed below before continuing:\n\n" + box +
	           "\n \n If you have any questions about these fields, feel free to contact the DMR office at 1 626-555-5555. \n" +
	           "_____________________________________________________________________________________ ");	
	   return false;
   }
   else
      return true;
	   	 
    
}