// This library contains useful client-side script functions for checking forms.

// This function will check a piece of an eMail address to see if it's well
// formed.  It must consist of alphanumeric characters, start with alphanumeric, and
// contain only . and - and _ with no consecutive dots.
function checkEmailSubPart(strPart)
{
	var nLen = strPart.length;
	if (nLen == 0)
		return false;

	// If we didn't have to deal with UNICODE, we could just compare character
	// codes against a range, but UNICODE isn't necessarily a contiguous range
	// for the alpha characters.
	var strAlpha  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var strDigits = "0123456789";

	var nLastDot = -1;
	var i;
	for (i=0; i < nLen; i++)
	{
		var ch = strPart.charAt(i).toUpperCase();
		if (i == 0)
		{
			if (strAlpha.indexOf(ch, 0) < 0 && strDigits.indexOf(ch, 0) < 0)
				break;				// Doesn't start with an alpha or number
		}
		else if (ch == ".")
		{
			if (nLastDot == i-1)
				break;				// Consecutive dots are not allowed
			nLastDot = i;
		}
		else if (strAlpha.indexOf(ch, 0) < 0 && strDigits.indexOf(ch, 0) < 0 && ch != "_" && ch != "-")
			break;
	}

	if (i < nLen)
		return false;

	return true;
}

// This function will check an email address to see if it's formed properly.
function checkEmailAddress(strAddress)
{
	// An email address consists of text before an @ and text after.  The only
	// punctuation allowed is a . and an _, and we don't allow consecutive dots.
	// Find the @ symbol and check everything before and after it.
	var nPos = strAddress.indexOf("@", 0);
	if (nPos <= 0)
		return false;			// No @ symbol or nothing in front of it?

	var nLenAfter = strAddress.length - nPos - 1;
	if (nLenAfter <= 0)
		return false;			// Nothing after the @ symbol?

	if (checkEmailSubPart(strAddress.substr(0, nPos)) == false ||
	    checkEmailSubPart(strAddress.substr(nPos+1, nLenAfter)) == false)
	{
		return false;
	}

	return true;
}
// This function will check a string for multiple email addresses, and verify each one
function checkMultiEmailAddress(strAddr)
{
	var nSep = strAddr.indexOf(";",0);
	while (nSep > 0) {
		if (!checkEmailAddress(strAddr.substr(0, nSep)))
			return false;
		strAddr = strAddr.substr(nSep+1);
		nSep = strAddr.indexOf(";",0);
	}
	if (strAddr.length > 0)
		return checkEmailAddress(strAddr);

	return true;
}


// Return true if value is a number
function isNumber(value) {
	if (value=="") return false;

	var d = parseInt(value);
	if (!isNaN(d)) return true; else return false;		

}

function checkFormPhone(oArea, oPrefix, oNum)
{
    var strPhoneAlert = "Phone number format is nnn-nnn-nnnn.";

    var strArea   = "" + oArea.value;
	var strPrefix = "" + oPrefix.value;
	var strNum    = "" + oNum.value;

    strArea   = trim(strArea);
	strPrefix = trim(strPrefix);
	strNum    = trim(strNum);

		if (!isNumber(strArea) || strArea.length < 3) {
			alert(strPhoneAlert);
			oArea.focus();
			return false;
		}
		if (!isNumber(strPrefix) || strPrefix.length < 3) {
			alert(strPhoneAlert);
			oPrefix.focus();
			return false;
		}
		if (!isNumber(strNum) || strNum.length < 4) {
			alert(strPhoneAlert);
			oNum.focus();
			return false;
		}

	return true;
}
// This function will validate a postal code
function checkPCode(strCode)
{
	// If we didn't have to deal with UNICODE, we could just compare character
	// codes against a range, but UNICODE isn't necessarily a contiguous range
	// for the alpha characters.
	var strAlpha  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var strDigits = "0123456789";

	var bValid = true;
	var nLen = strCode.length;
	if (nLen < 6 || nLen > 7)
		bValid = false;
	else
	{
		// The format of a Postal Code is ANA{-}NAN
		var ch1 = strCode.charAt(0).toUpperCase();
		var ch2 = strCode.charAt(1).toUpperCase();
		var ch3 = strCode.charAt(2).toUpperCase();
		if (strAlpha.indexOf(ch1, 0) < 0 || strAlpha.indexOf(ch3, 0) < 0)
			bValid = false;
		else if (strDigits.indexOf(ch2, 0) < 0)
			bValid = false;
			
		if (bValid)
		{
			var nPos = 3;
			ch1 = strCode.charAt(nPos).toUpperCase();
			if ("- ".indexOf(ch1, 0) >= 0)
				nPos = nPos + 1;
				
			ch1 = strCode.charAt(nPos).toUpperCase();
			ch2 = strCode.charAt(nPos+1).toUpperCase();
			ch3 = strCode.charAt(nPos+2).toUpperCase();
			if (strDigits.indexOf(ch1, 0) < 0 || strDigits.indexOf(ch3, 0) < 0)
				bValid = false;
			else if (strAlpha.indexOf(ch2, 0) < 0)
				bValid = false;
		}
	}
	
	return bValid;
}

// This function trims leading and trailing spaces
function trim(strIn)
{
    var strOut = "";
    var nLen=strIn.length;
    var i;
    for (i=0; i < nLen; i++) {
        var ch = strIn.charAt(i);
        if (ch != " " && strIn.charCodeAt(i) != 160)
            break;
    }
    if (i < nLen) {
        strOut = strIn.substr(i,nLen);
        nLen -= i;
        for (i=nLen-1; i > 0; i--) {
            var ch = strOut.charAt(i);
            if (ch != " " && strOut.charCodeAt(i) != 160)
                break;
        }
        if (i > 0)
            strOut = strOut.substr(0,i+1);
    }
    return strOut;
}

// This function strips commas and dollar signs from a digit sequence.
function stripCommas(strIn)
{
	var nLen = strIn.length;
	var strOut = "";
	var i;
	for (i=0; i < nLen; i++)
	{
		var ch = strIn.charAt(i);
		if (ch != "," && ch != " ")
			strOut += ch;
	}

	return parseFloat(strOut);
}

// This function will format a string containing a value by stuffing commas into it
// every three characters to the left of the decimal.
function placeCommas(strIn)
{
	var nPos = strIn.indexOf(".");
	if (nPos < 0)
		nPos = strIn.length;

	var strOut = strIn.substr(nPos);
	for (; nPos > 3; nPos -= 3)
	{
		strOut = "," + strIn.substr(nPos-3, 3) + strOut;
	}
	strOut = strIn.substr(0, nPos) + strOut;

	return strOut;
}

// This function is called when the submit button on the form is clicked.
// It will check the form contents to make sure all required fields have
// been entered.
function check_oef(oForm)
{
	// If a MLS # was entered, make sure it's all digits.
    var strOther = "" + oForm.Other.value;
    var nLen = strOther.length;
    if (nLen > 5) {
        var strLeft = strOther.substr(0,4);
        if (strLeft.toLowerCase() == "mls=") {
            var strMLS = strOther.substr(4, nLen - 4);
	        if (strMLS != "") {
		        var nMLS = stripCommas(strMLS);
		        if (isNaN(nMLS) == false && nMLS > 0) {
    		        oForm.MLS.value = nMLS.toString();
                    oForm.Other.value = "";
                }
	        }	
        }
    }

	return true;
}

// Converts the date to milliseconds since Jan 1, 1970.  Returns 0 if not in valid format.
function checkDate(strDate)
{
	// Crack apart the date. Format is yyyy-mm-dd
	var nDash1 = strDate.indexOf("-",0);
	if (nDash1 > 0) {
		var nDash2 = strDate.indexOf("-",nDash1+1);
		if (nDash2 > 0) {
			var nSpc = strDate.indexOf(" ",nDash2+1);
            if (nSpc < 0) nSpc = strDate.length;
			if (nSpc > 0) {
				// Convert date to milliseconds
				var strTest = strDate.substr(nDash1+1,nDash2-nDash1-1) + "/" + strDate.substr(nDash2+1, nSpc-nDash2) + "/" + strDate.substr(0, nDash1);
				return Date.parse(strTest);
			}
		}
	}

    return 0;
}


function check_ps_cmn(oForm)
{
    if (oForm.FirstName.value == "") {
		alert("First name is required.");
		oForm.FirstName.focus();
		return false;
	}
	if (oForm.LastName.value  == "") {
		alert("Last name is required.");
		oForm.LastName.focus();
		return false;
	}
	/*
	if (oForm.Street.value  == "") {
		alert("Street address is required.");
		oForm.Street.focus();
		return false;
	}
	if (oForm.City.value  == "") {
		alert("City is required.");
		oForm.City.focus();
		return false;
	}
	*/
	if (oForm.client_email.value == null ) {
		oalert("Email address must be of the form yourname@someplace.");
		oForm.client_email.focus();
		return false;
	}
	if (checkEmailAddress(oForm.client_email.value) == false) {
		alert("Email address must be of the form yourname@someplace.");
		oForm.client_email.focus();
		return false;
	}
    var str = ""+oForm.how_did_you_find_us.value;
    if (str == "" || str == "---- Select One ----") {
        alert("Please indicate how you found us.");
        oForm.how_did_you_find_us.focus();
        return false;
    }
	return true;
}
function check_ps(oForm)
{

    if (check_ps_cmn(oForm) == false)
        return false;

	if (checkFormPhone(oForm.dayPhoneArea, oForm.dayPhonePrefix, oForm.dayPhoneNum) == false)
		return false;

    if(oForm.nightPhoneArea.value !=null && oForm.nightPhoneArea.value!="" ){		
	    if (checkFormPhone(oForm.nightPhoneArea, oForm.nightPhonePrefix, oForm.nightPhoneNum) == false)
		    return false;
    }
    	
    return true;
}


function check_eval(oForm)
{
    if (oForm.FirstName.value == "") {
		alert("First name is required.");
		oForm.FirstName.focus();
		return false;
	}

	if (oForm.LastName.value == "") {
		alert("Last name is required.");
		oForm.LastName.focus();
		return false;
	}

	if (oForm.Street.value == "") {
		alert("Street address is required.");
		oForm.Street.focus();
		return false;
	}


	if (checkFormPhone(oForm.dayPhoneArea, oForm.dayPhonePrefix, oForm.dayPhoneNum) == false)
		return false;

    if(oForm.nightPhoneArea.value !=null && oForm.nightPhoneArea.value!="" ){		
	    if (checkFormPhone(oForm.nightPhoneArea, oForm.nightPhonePrefix, oForm.nightPhoneNum) == false)
		    return false;
    }
	
    if (oForm.HouseSize.value == "") {
		alert("Square footage of home is required.");
		oForm.HouseSize.focus();
		return false;
	}

    if (oForm.BedsUp.value == "" && oForm.BedsDown.value == "") {
        alert("Number of bedrooms is required.");
        oForm.BedsUp.focus();
        return false;
    }

    if (oForm.HalfBaths.value == "" && oForm.FullBaths.value == "") {
        alert("Number of bathrooms is required.");
        oForm.FullBaths.focus();
        return false;
    }

    if (oForm.TypeOfHome.value == " ") {
		alert("Type of home is required.");
		oForm.TypeOfHome.focus();
		return false;
	}

    if (oForm.Style.value == " ") {
		alert("Style of home is required.");
		oForm.Style.focus();
		return false;
	}

    if (oForm.BasementType.value == " ") {
		alert("Type of basement is required.");
		oForm.BasementType.focus();
		return false;
	}
  
	if (oForm.client_email.value == null ) {
		oalert("Email address must be of the form yourname@someplace.");
		oForm.client_email.focus();
		return false;
	}
	if (checkEmailAddress(oForm.client_email.value) == false) {
		alert("Email address must be of the form yourname@someplace.");
		oForm.client_email.focus();
		return false;
	}
	
    var str = ""+oForm.how_did_you_find_us.value;
    if (str == "" || str == "---- Select One ----") {
        alert("Please indicate how you found us.");
        oForm.how_did_you_find_us.focus();
        return false;
    }
	return true;
}