// JavaScript Document

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
var isChecked=false;


function logval(fa)
{
	if(fa.username.value=="")
	{
		alert("Enter Username");
		fa.username.focus();
		return false;
	}
	if(fa.password.value=="")
	{
		alert("Enter the Password");
		fa.password.focus();
		return false;
	}
	return true;
}

function validate()
{
	if(document.form1.txtusername.value=="")
	{
		alert("Please enter your User Name");
		document.form1.txtusername.focus();
		return false;
	}
	if(document.form1.txtpassword.value=="")
	{
		alert("Please enter your Password");
		document.form1.txtpassword.focus();
		return false;
	}
	if(document.form1.txtcpassword.value=="")
	{
		alert("Please confirm your Password");
		document.form1.txtcpassword.focus();
		return false;
	}
	if(document.form1.txtpassword.value!=document.form1.txtcpassword.value)
	{
		alert("Your Password and Confirm Password do not match");
		document.form1.txtcpassword.focus();
		return false;
	}
	if(!echeck(document.form1.txtemail.value))
	{
		document.form1.txtemail.focus();
		return false;
	}
	if(document.form1.txtaddress.value=="")
	{
		alert("Please enter your Address");
		document.form1.txtaddress.focus();
		return false;
	}
	if(document.form1.txtcity.value=="")
	{
		alert("Please enter your City ");
		document.form1.txtcity.focus();
		return false;
	}
	if(document.form1.txtstate.value=="")
	{
		alert("Please enter your State ");
		document.form1.txtstate.focus();
		return false;
	}
	if(document.form1.txtzipcode.value=="")
	{
		alert("Please enter your Zip");
		document.form1.txtzipcode.focus();
		return false;
	}
	return true;
}
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid email address");
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid email address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   alert("Invalid email address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   alert("Invalid email address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   alert("Invalid email address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		   alert("Invalid email address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		   alert("Invalid email address")
		    return false
		 } 
		return true;
	}
	
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function ValidateDate(str){
	var dt=str
	if (isDate(dt.value)==false){
		dt.focus()
		return 1;
	}
	return 0;
	}
	
function compareDates(value1,value2)
{
	var date1,date2;
	var month1,month2;
	var year1,year2;
	
	month1=value1.substring(0,value1.indexOf("/"));
	date1=value1.substring(value1.indexOf("/")+1,value1.lastIndexOf("/"));
	year1=value1.substring(value1.lastIndexOf("/")+1,value1.length);
	
	month2=value2.substring(0,value2.indexOf("/"));
	date2=value2.substring(value2.indexOf("/")+1,value2.lastIndexOf("/"));
	year2=value2.substring(value2.lastIndexOf("/")+1,value2.length);
	
	if(year2>=year1)
	return 1;
	else
	return 0;
}

function comVal(f)
{
	 
	if(f.txtapplicationapplicant_name.value=="")
	{
		alert("Enter Applicant's Name");
		f.txtapplicationapplicant_name.focus();
		return false;
	}
	if(f.txtapplicationapplicant_address.value=="")
	{
		alert("Enter Applicant's Address");
		f.txtapplicationapplicant_address.focus();
		return false;
	}
	if(f.txtapplicationapplicant_phone.value=="")
	{
		alert("Enter Applicant's Phone");
		f.txtapplicationapplicant_phone.focus();
		return false;
	}
	if(f.txtapplicationapplicant_fax.value=="")
	{
		alert("Enter Applicant's Zip");
		f.txtapplicationapplicant_fax.focus();
		return false;
	}
	if(f.txtapplicationapplicant_interest.value=="")
	{
		alert("Enter Applicant's Interest in Property");
		f.txtapplicationapplicant_interest.focus();
		return false;
	}
	if(f.txtapplicationowner_name.value=="")
	{
		alert("Enter Owner Name");
		f.txtapplicationowner_name.focus();
		return false;
	}
	if(f.txtapplicationowner_address.value=="")
	{
		alert("Enter Owner Address");
		f.txtapplicationowner_address.focus();
		return false;
	}
	if(f.txtapplicatonowner_phone.value=="")
	{
		alert("Enter Owner Phone");
		f.txtapplicatonowner_phone.focus();
		return false;
	}
	if(f.txtapplicationowber_zip.value=="")
	{
		alert("Enter Owner Zip");
		f.txtapplicationowber_zip.focus();
		return false;
	}
	if(f.txtapplicationagent_name.value=="")
	{
		alert("Enter Agent Name");
		f.txtapplicationagent_name.focus();
		return false;
	}
	if(f.txtapplicationfirm_name.value=="")
	{
		alert("Enter Firm Name");
		f.txtapplicationfirm_name.focus();
		return false;
	}
	if(f.txtapplicationfirm_address.value=="")
	{
		alert("Enter Firm Address");
		f.txtapplicationfirm_address.focus();
		return false;
	}
	if(f.txtapplicationfirm_phone.value=="")
	{
		alert("Enter Firm Phone");
		f.txtapplicationfirm_phone.focus();
		return false;
	}
	if(f.txtapplicationfirm_zip.value=="")
	{
		alert("Enter Firm Zip");
		f.txtapplicationfirm_zip.focus();
		return false;
	}
	if(f.txtapplicationcontact_name.value=="")
	{
		alert("Enter the Contact Name");
		f.txtapplicationcontact_name.focus();
		return false;
	}
	if(f.txtapplicationcontact_phone.value=="")
	{
		alert("Enter the Contact Phone");
		f.txtapplicationcontact_phone.focus();
		return false;
	}
	if(f.txtapplicationcontact_fax.value=="")
	{
		alert("Enter the Contact Fax");
		f.txtapplicationcontact_fax.focus();
		return false;
	}
	if(f.txtapplicationcontact_email.value=="")
	{
		alert("Enter Contact email address");
		f.txtapplicationcontact_email.focus();
		return false;
	}
	else
	 if(echeck(f.txtapplicationcontact_email.value)==false)
	 {
	 f.txtapplicationcontact_email.focus();
	 return false;
	 }
	if(f.txtapplicationlocation.value=="")
	{
		alert("Enter Location/Street address");
		f.txtapplicationlocation.focus();
		return false;
	}
	if(f.txtapplicationassesor_par_no.value=="")
	{
		alert("Enter Assessors Parcel Number(s)");
		f.txtapplicationassesor_par_no.focus();
		return false;
	}
	if(f.txtapplicationtownship.selectedIndex< 1)
	{
		alert("Select a  Township, Range Section");
		f.txtapplicationtownship.focus();
		return false;
	}
	if(f.txtapplicationtotal_acreage.value==""||isNaN(f.txtapplicationtotal_acreage.value))
	{
		alert("Enter a valid number in Total Acreage");
		f.txtapplicationtotal_acreage.focus();
		return false;
	}
	if(f.txtapplicationcurrent_landuse.value=="")
	{
		alert("Select a the Current Land use");
		f.currlandusedropdown.focus();
		return false;
	}
	if(f.txtapplicationcurrent_zone_dist.value=="")
	{
		alert("Select Current Zoning District");
		f.currzonedropdown.focus();
		return false;
	}
	if(f.txtapplicationproject_name.value=="")
	{
		alert("Enter Project/Subdivision Name");
		f.txtapplicationproject_name.focus();
		return false;
	}
	if(f.txtapplicationgeneral_desc.value=="")
	{
		alert("Enter General Description of Proposed Project/Request");
		f.txtapplicationgeneral_desc.focus();
		return false;
	}
	if(f.txtapplicationproposed_zone_dist.value=="")
	{
		alert("Enter Proposed Zoning District(s)");
		f.txtapplicationproposed_zone_dist.focus();
		return false;
	}
	if(f.txtapplicationacres_zones_proposed.value=="")
	{
		alert("Enter Acres of Each Zone Proposed");
		f.txtapplicationacres_zones_proposed.focus();
		return false;
	}
	if(f.txtapplicationamenities_prov.value=="")
	{
		alert("Enter the amenities Provided");
		f.txtapplicationamenities_prov.focus();
		return false;
	}
	if(f.txtapplicationpressurised_irri_sys.value=="")
	{
		alert("Enter Who will own and maintain the pressurised irrigation system in this development");
		f.txtapplicationpressurised_irri_sys.focus();
		return false;
	}
	if(f.txtapplicationdistrict_prpt_lie.value=="")
	{
		alert("Enter Which irrigation district does this property lie within?");
		f.txtapplicationdistrict_prpt_lie.focus();
		return false;
	}
	if(f.txtapplicationprimary_src.value=="")
	{
		alert("Enter the primary Irrigation Source ");
		f.txtapplicationprimary_src.focus();
		return false;
	}
	if(!checknumeric(f.txtapplicationsqft_area_tobeirri.value)) 
		return fail(f.txtapplicationsqft_area_tobeirri,"Please enter a valid number into the Square Footage to be Irrigated");
	
	if(!checknumeric(f.txtapplicationnoofresidentailunits))
		return fail(f.txtapplicationnoofresidentailunits,  "Please enter a valid number into \"Number of Residential Units\"")
	if(!checknumeric(f.txtapplicationnoofbldglots))
		return fail(f.txtapplicationnoofbldglots,  "Please enter a valid number into \"Number of builiding lots\"")
	if(!checknumeric(f.txtapplicationnoofcomorotherlots))
		return fail(f.txtapplicationnoofcomorotherlots,  "Please enter a valid number into \"Number of common and/or other lots\"")
	if(!checknumeric(f.txtapplicationproposed_dwellingunits_1bed))
		return fail(f.txtapplicationproposed_dwellingunits_1bed,  "Please enter a valid number into \"Proposed number of dwelling units(1 Bedroom)\"")
	
	if(!checknumeric(f.txtapplicationproposed_dwellingunits_1bed))
		return fail(f.txtapplicationproposed_dwellingunits_1bed,  "Please enter a valid number into \"Proposed number of dwelling units(1 Bedroom)\"")
	if(!checknumeric(f.txtapplicationproposed_dwellingunits_2bed))
		return fail(f.txtapplicationproposed_dwellingunits_2bed,  "Please enter a valid number into \"Proposed number of dwelling units(2 Bedroom)\"")
	if(!checknumeric(f.txtapplicationminimumsqr_ftg_str))
		return fail(f.txtapplicationminimumsqr_ftg_str,  "Please enter a valid number into \"Minimum Square Footage of structure(s)\"")
	if(!checknumeric(f.txtapplicationproposed_bldg_ht))
		return fail(f.txtapplicationproposed_bldg_ht,  "Please enter a valid number into \"Proposed Building height\"")
	
	
	if(!checknumeric(f.txtapplicationminimum_prpt_size))
		return fail(f.txtapplicationminimum_prpt_size,  "Please enter a valid number into \"Minimum Property size(s.f)\"")
	
	if(!checknumeric(f.txtapplicationaverage_prpt_size))
		return fail(f.txtapplicationaverage_prpt_size,  "Please enter a valid number into \"Average Property Size(s.f)\"")
	
	if(!checknumeric(f.txtapplicationgross_density))
		return fail(f.txtapplicationgross_density,  "Please enter a valid number into \"Gross Density(DU/Total Acres land)\"")
	
	if(!checknumeric(f.txtapplicationnet_density))
		return fail(f.txtapplicationnet_density,  "Please enter a valid number into \"Net Densit(DU/acre excluding roads &amp; alleys)\"")
	
	if(!checknumeric(f.txtapplicationper_open_space_prov))
		return fail(f.txtapplicationper_open_space_prov,  "Please enter a valid number into \"Percentage of open space provided\"")
	
	if(!checknumeric(f.txtapplicationacreage_open_space))
		return fail(f.txtapplicationacreage_open_space,  "Please enter a valid number into \"Acreage of open space\"")
	if(!checknumeric(f.txtapplicationper_useable_space))
		return fail(f.txtapplicationper_useable_space,  "Please enter a valid number into \"Percentage of usable open space\"")
	if(f.txtapplicationopen_space_in_acres.value="")
		return fail(f.txtapplicationopen_space_in_acres,  "Please enter a valid number into \"Type of open space provided in acres\"")
	if(!boxChecked(f.txtapplicationdwell_prop_type))
		return fail(f.txtapplicationdwell_prop_type,  "Please choose the Type(s) of dwellings proposed")
	if(!checknumeric(f.txtapplicationno_of_bldg_lots))
		return fail(f.txtapplicationno_of_bldg_lots,  "Please enter a valid number into \"Number of building lots: \"")
	if(!checknumeric(f.txtapplicationother_lots))
		return fail(f.txtapplicationother_lots,  "Please enter a valid number into \"Other lots\"")
	if(!checknumeric(f.txtapplicationgross_floor_area_prop))
		return fail(f.txtapplicationgross_floor_area_prop,  "Please enter a valid number into \"Gross Floor area proposed\"")
	if(!checknumeric(f.txtapplicationexisting))
		return fail(f.txtapplicationexisting,  "Please enter a valid number into \"Existing\"")
	if(f.txtapplicationhours_of_oper.value=="")
		return fail(f.txtapplicationhours_of_oper,  "Please enter the \"Hours of Operation(days and hours)\"")
	if(!checknumeric(f.txtapplicationbldg_ht))
		return fail(f.txtapplicationbldg_ht,  "Please enter a valid number into \"Building Height\"")
	if(!checknumeric(f.txtapplicationdevoted_landscapping))
		return fail(f.txtapplicationdevoted_landscapping,  "Please enter a valid number into \"Percentage of site/project devoted to Landscaping\"")
	
	
	if(!checknumeric(f.txtapplicationdevoted_building))
		return fail(f.txtapplicationdevoted_building,  "Please enter a valid number into \"Percentage of site/project devoted to Building\"")
	if(!checknumeric(f.txtapplicatondevoted_paving))
		return fail(f.txtapplicatondevoted_paving,  "Please enter a valid number into \"Percentage of site/project devoted to Paving\"")
	
	if(!checknumeric(f.txtapplicationtotal_no_of_emp))
		return fail(f.txtapplicationtotal_no_of_emp,  "Please enter a valid number into \"Total Number of employees\"")
	if(!checknumeric(f.txtapplicationmax_no_of_emp_one_time))
		return fail(f.txtapplicationmax_no_of_emp_one_time,  "Please enter a valid number into \"Maximum Number of employee at any one time\"")
	if(f.txtapplicationno_and_ages_of_stud.value="")
		return fail(f.txtapplicationno_and_ages_of_stud,  "Please enter a valid number into \"Number of ages of students/children(if Applicable)\"")
	if(!checknumeric(f.txtapplicationseating_capacity))
		return fail(f.txtapplicationseating_capacity,  "Please enter a valid number into \"Seating Capacity \"")
	if(!checknumeric(f.txtapplicationtotal_no_of_parking_space))
		return fail(f.txtapplicationtotal_no_of_parking_space,  "Please enter a valid number into \"Total Number of parking spaces provided\"")
	if(!checknumeric(f.txtapplicationno_of_compact_spaces))
		return fail(f.txtapplicationno_of_compact_spaces,  "Please enter a valid number into \"Number of compact spaces provided\"")
	 
		
		
		
	if(!f.chkcertify.checked)
		return fail(f.chkcertify,"Please check that you certify that you are the applicant listed above and that you am authorized to make this application")
		
	return true;
}
function fail(box, msg)
{
	alert(msg)
	if(typeof(box.length)!="undefined")
		box[0].focus()
	else
		box.focus()
	return false;
}
function checknumeric(vl)
{
	if(typeof(vl)=="object")
		vl = vl.value 
	vl = vl.replace(/ /g,"")
	vl = vl.toLowerCase();
	if(vl=="na"||vl=="n/a")
		return true;
	if(isNaN(vl)||vl=="")
		return false
	return true;
}
function boxChecked(str)
{
	cked=false
	if(typeof(str.length)=="undefined")
		return str.checked;
	for(var i=0;i< str.length;i++)
	{
		if(str[i].checked)
		{
			cked = true;
		}
	}	
	
	return cked;
}



  	function addcurrent(bx)
	{
		vl=bx.options[bx.selectedIndex].text 
		addmbox("currlanduse", "currlandusediv",bx)
		bx.options[0].text="Select Another";
		bx.selectedIndex=0
	}function addzone(bx)
	{
		vl=bx.options[bx.selectedIndex].text 
		addmbox("currzone", "currzonediv",bx)
		bx.options[0].text="Select Another";
		bx.selectedIndex=0
	}
	function addmbox(nm,divname,bx)
	{ 
		vl=bx.options[bx.selectedIndex].text
		txtbox = document.getElementById(nm)
		l = txtbox.value.split(",")
		for(i=0;i<l.length;i++) 
		{
			if(l[i]==vl)
				return;
			if(l[i]=="")
				continue
		}
		if(txtbox.value!="")
			txtbox.value=txtbox.value+","
		txtbox.value=txtbox.value+vl
		displaymbox(nm,divname)
	}
	function rmmbox(nm,divname,vl)
	{ 
		txtbox = document.getElementById(nm)
		l = txtbox.value.split(",")
		txtbox.value="";
		for(i=0;i<l.length;i++) 
		{
			if(l[i]==vl)
				continue;
			
			if(l[i]=="")
				continue
			if(txtbox.value!="")
				txtbox.value=txtbox.value+","
			txtbox.value=txtbox.value+l[i]
		}
		displaymbox(nm,divname)
	}
	function displaymbox(nm,divname)
	{
		txtbox=document.getElementById(nm)
		div=document.getElementById(divname)
		l = txtbox.value.split(",")
		h="";
		for(i=0;i<l.length;i++)
		{ 
			v=l[i]
			if(v=="")
				continue
			if(i>0)
				h=""+h+",&nbsp;"
			if(l.length>0)
				h=""+h+""+v+"<sup><a href=\"javascript:rmmbox('"+nm+"','"+divname+"','"+v+"')\">&nbsp;<font size=1>x</font></a></sup>"
			else
				h=v
		}
		div.innerHTML=h;
	}





