//Developer:		Ryan McDaniel
//Company:			Webize Interactive Media, Inc.
//Customer:         Indiana Republican Party
//Purpose:			This file will hold all Javascript for the VoterRegistration.asp page.
//Version:			1.2
//Modifications:	07/14/2008 - (Ryan McDaniel) Modified date validation to validate 3 separate boxes for month,
//						day, and year. (Version 1.2)
//					07/09/2008 - (Ryan McDaniel) Added validation for Applicant Assistant phone number. (Version 1.1)
//                  07/08/2008 - (Ryan McDaniel) Modified validation to use county drop down box. (Version 1.1)
//                  06/13/2008 – (Ryan McDaniel) Original creation date. (Version 1.0)
//=================================================================================================================

//=================================================================================================================
//This function will set the mailing address fields to match the residence address fields.
function SetMailing(objCB) {
    if (objCB.checked == true) {
        document.frmInput.txtMailingAddress.value = document.frmInput.txtResidenceAddress.value;
        document.frmInput.txtMailingAptNo.value = document.frmInput.txtResidenceAptNo.value;
        document.frmInput.txtMailingCity.value = document.frmInput.txtResidenceCity.value;
        document.frmInput.txtMailingState.value = "IN";
        document.frmInput.txtMailingZip.value = document.frmInput.txtResidenceZip.value;
    }
    else {
        document.frmInput.txtMailingAddress.value = "";
        document.frmInput.txtMailingAptNo.value = "";
        document.frmInput.txtMailingCity.value = "";
        document.frmInput.txtMailingState.value = "";
        document.frmInput.txtMailingZip.value = "";
    }
}
//=================================================================================================================
//This function will validate necessary inputs for the voter registration form.
function ValidateInputs() {
    var InputForm = document.frmInput;
    //the following regular expression pattern will validate a 10 digit number
    var RegExTenDigits = /^\d{10}/;
    //the following regular expression pattern will validate the format of an email address
    var RegExEmail = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	
	if (Trim(InputForm.selCounty.value) == "") {
	    alert("Please select the Indiana county where you live.");
	    InputForm.selCounty.focus();
	    return false;
	}
	else if (ValidateDate(Trim(InputForm.txtBirthMonth.value), document.frmInput.txtBirthMonth, Trim(InputForm.txtBirthDay.value), document.frmInput.txtBirthDay, Trim(InputForm.txtBirthYear.value), document.frmInput.txtBirthYear, "Birthdate") == false) {
	    //if provided, birthdate must be a valid date
		//user message handled in ValidateDate function
	    return false;
	}
	else if ((Trim(InputForm.txtTelephone.value) != "") && (!InputForm.txtTelephone.value.match(RegExTenDigits))) {
	    //if provided, phone number must be a valid 10 digit number
	    alert("Phone number must be 10 digits, starting with area code. No spaces.");
	    InputForm.txtTelephone.select();
	    return false;
	}
	else if ((Trim(InputForm.txtEmail.value) != "") && (!InputForm.txtEmail.value.match(RegExEmail))) {
	    //if provided, email must appear to be valid
	    alert("Email address is invalid. Please check format.");
	    InputForm.txtEmail.select();
	    return false;
	}
	else if (Trim(InputForm.txtFirstName.value) == "") {
	    alert("Please provide a first name.");
	    InputForm.txtFirstName.focus();
	    return false;
	}
	else if (Trim(InputForm.txtLastName.value) == "") {
	    alert("Please provide a last name.");
	    InputForm.txtLastName.focus();
	    return false;
	}
	else if (Trim(InputForm.txtResidenceAddress.value) == "") {
	    alert("Please provide a residence address.");
	    InputForm.txtResidenceAddress.focus();
	    return false;
	}
	else if (Trim(InputForm.txtResidenceCity.value) == "") {
	    alert("Please provide a residence city.");
	    InputForm.txtResidenceCity.focus();
	    return false;
	}
	else if (Trim(InputForm.txtResidenceZip.value) == "") {
	    alert("Please provide a residence zip code.");
	    InputForm.txtResidenceZip.focus();
	    return false;
	}
	else if (Trim(InputForm.txtMailingAddress.value) == "") {
	    alert("Please provide a mailing address.");
	    InputForm.txtMailingAddress.focus();
	    return false;
	}
	else if (Trim(InputForm.txtMailingCity.value) == "") {
	    alert("Please provide a mailing city.");
	    InputForm.txtMailingCity.focus();
	    return false;
	}
	else if (Trim(InputForm.txtMailingState.value) == "") {
	    alert("Please provide a mailing state.");
	    InputForm.txtMailingState.focus();
	    return false;
	}
	else if (Trim(InputForm.txtMailingZip.value) == "") {
	    alert("Please provide a mailing zip code.");
	    InputForm.txtMailingZip.focus();
	    return false;
	}
	else if ((Trim(InputForm.txtAssistantPhone.value) != "") && (!InputForm.txtAssistantPhone.value.match(RegExTenDigits))) {
	    //if provided, phone number must be a valid 10 digit number
	    alert("Assistant's phone number must be 10 digits, starting with area code. No spaces.");
	    InputForm.txtAssistantPhone.select();
	    return false;
	}
	
	//if made it here...all required inputs are valid.
	return true;
}
//=================================================================================================================
//This function will take month, day, and year inputs and validate that combined, they make a valid date.
//MonthControl is the control element that displays month
//ControlText is the type of date being validated (for message box display purposes only)
function ValidateDate(PassedMonth, MonthControl, PassedDay, DayControl, PassedYear, YearControl, ControlText) {
	//the following regular expression pattern will validate a date and time (including leap years)
    var RegExDate = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    
	//the following regular expression pattern will validate a month value with or without leading zero
	var RegExMonth = /^(0?[1-9]|1[012])$/;
	//the following regular expression pattern will validate a day value with or without leading zero
	var RegExDay = /^(0?[1-9]|[12][0-9]|3[01])$/;
	//the following regular expression pattern will validate a year value as four digit year
	var RegExYear = /^(19|20)\d\d$/;
	
	if (PassedMonth != "" || PassedDay != "" || PassedYear != "") {
		//month, day, or year was provided so they all must be accurate
		if (!PassedMonth.match(RegExMonth)) {
			//month must be valid
			alert(ControlText + " month is invalid.");
			MonthControl.select();
			return false;
		}
		
		if (!PassedDay.match(RegExDay)) {
			//day must be valid
			alert(ControlText + " day is invalid.");
			DayControl.select();
			return false;
		}
		
		if (!PassedYear.match(RegExYear)) {
			//year must be valid (4 digit year)
			alert(ControlText + " year must be 4 digits between 1900 and 2099");
			YearControl.select();
			return false;
		}
		
		var TryDate;	//combine month, day, year, and slash separator
		TryDate = PassedMonth + "/" + PassedDay + "/" + PassedYear
		//validate combined date parts against proper month length and leap year
		if (!TryDate.match(RegExDate)) {
			alert(ControlText + " date is invalid.\n\nProper format is mm/dd/yyyy.\n\nCheck days in month or leap year.");
			MonthControl.focus();
			return false;
		}
		
		//if made it here, a valid date was provided
		return true;
	}
	else {
		//no inputs were provided for date
		return true;	
	}
}
//=================================================================================================================
//This function receives a text string and will call necessary functions to remove leading and trailing spaces.
function Trim(TrimString) {
	return TrimTrail(TrimLead(TrimString));
}
//=================================================================================================================
//This function receives a text string and will trim off any leading spaces.
function TrimLead(TrimString) {
	while (TrimString.charAt(0) == " ") {
		TrimString = TrimString.substring(1,TrimString.length);
	}
	return TrimString;
}
//=================================================================================================================	
//This function receives a text string and will trim off any trailing spaces.
function TrimTrail(TrimString) {
	while (TrimString.charAt(TrimString.length - 1) == " ") {
		TrimString = TrimString.substring(0,TrimString.length - 1);
	}
	return TrimString;
}
//=================================================================================================================