/* Used by Apartment detail Visit tab 
  apartment-details.jsp and apartment-details-tab-visit.asp
 */
function check_email(Email)
{
	if (Email == "") {
		return -1;
	}
	if (Email.length < 4) {
		return -2;
	}
	if (Email.indexOf("@") == -1) {
		return -3;
	}

	var checkOK = "_*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789-@.";
	var checkStr = Email;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++) {
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++) if (ch == checkOK.charAt(j)) break;
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	if (!allValid) {
		return -4;
	}
    return 1;
}
function VisitForm_Validator(theForm)
{
	if (theForm.Day1.value == "") {
		alert("Please enter a value for the \"First Choice Day\" field.");
		theForm.Day1.focus();
		return false;
	}
	if (theForm.Time1.value == "") {
		alert("Please enter a value for the \"First Choice Time\" field.");
		theForm.Time1.focus();
		return false;
	}

   	if (theForm.amORpm1[0].checked)
	{
		amORpm1 = theForm.amORpm1[0].value;
    }
    else if (theForm.amORpm1[1].checked)
   	{
	    amORpm1 = theForm.amORpm1[1].value;
	}
	else
	{
		amORpm1 = ""
	}
	if (theForm.amORpm2[0].checked)
	{
		amORpm2 = theForm.amORpm2[0].value;
    }
    else if (theForm.amORpm2[1].checked)
   	{
	    amORpm2 = theForm.amORpm2[1].value;
	}
	else
	{
		amORpm2 = ""
	}
	if (theForm.amORpm3[0].checked)
	{
		amORpm3 = theForm.amORpm3[0].value;
    }
    else if (theForm.amORpm3[1].checked)
   	{
	    amORpm3 = theForm.amORpm3[1].value;
	}
	else
	{
		amORpm3 = ""
	}

	if (theForm.amORpm1.value == "" ||  amORpm1 == "") {
		alert("Please select either am or pm for your First Choice.");
		//theForm.amORpm1.focus();
		return false;
	}
	if (theForm.Time2.value != "" && amORpm2 == "" )
	{
		alert("Please select either am or pm for your Second Choice.");
		//theForm.amORpm1.focus();
		return false;
	}
	if (theForm.Time3.value != "" && amORpm3 == "" )
	{
		alert("Please select either am or pm for your Third Choice.");
		//theForm.amORpm1.focus();
		return false;
	}
	tempName = theForm.Firstname.value;
	tempName = tempName.replace(/\s/g,"");
	theForm.Firstname.value = tempName;
	if (theForm.Firstname.value == "") {
		alert("Please enter a value for the \"First Name\" field.");
		theForm.Firstname.focus();
		return false;
	}
	tempName = theForm.Lastname.value;
	tempName = tempName.replace(/\s/g,"");
	theForm.Lastname.value = tempName;
	if (theForm.Lastname.value == "") {
		alert("Please enter a value for the \"Last Name\" field.");
		theForm.Lastname.focus();
		return false;
	}
	tempPhone = theForm.Phone.value;
	tempPhone = tempPhone.replace(/\s/g,"");
	theForm.Phone.value = tempPhone;
	if (theForm.Phone.value == "") {
		alert("Please enter a value for the \"Phone Number\" field.");
		theForm.Phone.focus();
		return false;
	}
	
	if(theForm.Email.value =="")
	{
		alert("Please enter a value for the \"Email Address\" field.");
		theForm.Email.focus();
		return false;
	}
	else
	{
		tempEmail = theForm.Email.value;
		tempEmail = tempEmail.replace(/\s/g,"");
		//alert("["+tempEmail+"]");
		theForm.Email.value = tempEmail;
		
		if (check_email(theForm.Email.value) == -2) {
			alert("Please enter at least 4 characters in the \"Email\" field.");
			theForm.Email.focus();
			return false;
		}
		else if (check_email(theForm.Email.value) == -3) {
			alert("The \"Email\" field needs to contain an @ symbol.");
			theForm.Email.focus();
			return false;
		}
		else if (check_email(theForm.Email.value) == -4) {
			alert("Please enter only letter, digit and \"@\" characters in the \"Email\" field.");
			theForm.Email.focus();
			return false;
		}
	}

	if(theForm.Email2.value == "") {
		alert("Please enter a value in the \"Re-enter Email Address.\" field.");
		theForm.Email2.focus();
		return false;
	}
	if(theForm.Email.value != theForm.Email2.value) {
		alert("The email addresses entered do not match.");
		theForm.Email.focus();
		return false;
	}
	if(theForm.Number_of_people.value == "") {
		alert("Please enter a value in the \"Number of People\" field.");
		theForm.Number_of_people.focus();
		return false;
	}
	if ((theForm.MoveInDate.value == "") || (theForm.MoveInDate.value != "" && !isDate(theForm.MoveInDate.value))) {
		alert("Please enter a valid Move-In Date, in mm/dd/yyyy format.");
		theForm.MoveInDate.focus();
		return false;
	}
	if (theForm.Desired_lease_term.selectedIndex == 0) {
		alert("Please choose an option from the \"Desired Lease Term\" list.");
		theForm.Desired_lease_term.focus();
		return (false);
	}
	if (theForm.aptType.selectedIndex == 0) {
		alert("Please choose an option from the \"Apartment Type\" list.");
		theForm.aptType.focus();
		return (false);
	}
	if (theForm.How_did_you_hear_about_us.selectedIndex == 0) {
		alert("Please choose an option from the \"How did you hear about us\" list.");
		theForm.How_did_you_hear_about_us.focus();
		return (false);
	}

	theForm.submit();
}

function isDate(dateString)
{
    if(isNaN(Date.parse(dateString))) return false;
    dateString = dateString.replace(/(\/|,| )0([1-9])/g,'$1$2');

    //line below is preferred line in place of the above line
    //dateString = dateString.replace(/(\/|,| |^)0([1-9]($)?)/g,'$1$2');

    var date = new Date(dateString).getDate();
    var reg = RegExp("(/" + date + "/)|([, ]" + date +
              "[, ])|^(" + date + "[, ])|([, ]" + date + ")$");
    return dateString.match(reg) != null;
} 