// 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;
}

// This function will check a phone number to if it's properly formed.
function checkPhone(strPhone)
{
	// Make sure the phone number is valid.
	var bValid = true;
	var nLen = strPhone.length;
	if (nLen < 7)
		bValid = false;
	else
	{
		var i;
		var nOpen  = -1;			// Position of openening parenthesis
		var ncDash = 0;				// Number of dashes (max 2)
		for (i=0; i < nLen && bValid == true; i++)
		{
			var c = strPhone.charAt(i);
			if (c == "(") {
				if (nOpen >= 0)
					bValid = false;			// Can only have a single (.
				else
					nOpen = i;
			} else if (c == ")") {
				if (nOpen < 0)
					bValid = false;			// Must have an opening (.
			} else if (c == "-") {
				ncDash++;
				if (ncDash > 2)
					bValid = false;			// Can have at most 2 dashes.
			} else if ("1234567890 ".indexOf(c, 0) < 0)
				bValid = false;				// Not a digit or whitespace
		}
	}

    return bValid;
}
function checkFormPhone(oArea, oPrefix, oNum, oFull)
{
    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);

	var strFull = strArea+strPrefix+strNum;
   	if (strFull != "") {
		if (strArea == "") {
			alert(strPhoneAlert);
			oArea.focus();
			return false;
		}
		if (strPrefix == "") {
			alert(strPhoneAlert);
			oPrefix.focus();
			return false;
		}
		if (strNum == "") {
			alert(strPhoneAlert);
			oNum.focus();
			return false;
		}
		if (checkPhone(strFull) == false) {
			alert(strPhoneAlert);
			oArea.focus();
			return false;
		}

		oFull.value = strFull;
	}

	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 will validate a US zip code
function checkZipCode(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 strDigits = "0123456789";

	var bValid = true;
	var nLen = strCode.length;
	if (nLen < 3 || nLen > 5)
		bValid = false;
	else
	{
		// The format of a Zip Code is all digits, usually 5 of them
		for (i=0; i < nLen && bValid; i++) {
		    var ch = strCode.charAt(i).toUpperCase();
		    if (strDigits.indexOf(ch, 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 an MLS System # 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;
}

// Checks the form on sal.asp and some field from ld.asp
function check_sal(oForm)
{
	if (checkFormPhone(oForm.dayPhoneArea, oForm.dayPhonePrefix, oForm.dayPhoneNum, oForm.dayphone) == false)
		return false;
		
	if (checkFormPhone(oForm.nightPhoneArea, oForm.nightPhonePrefix, oForm.nightPhoneNum, oForm.nightphone) == false)
		return false;

    var strDay = "" + oForm.dayphone.value;
    var strNight = "" + oForm.nightphone.value;
    if (strDay == "" && strNight == "") {
		alert("A phone number is required.");
		oForm.dayPhoneArea.focus();
		return false;
    }
    
	var strEmail = oForm.email.value;
	if (strEmail != "" && checkEmailAddress(strEmail) == false)
	{
		alert("eMail address must be of the form yourname@domain.xxx");
		oForm.email.focus();
		return false;
	}
	var strPcode = oForm.pcode.value;
	if (strPcode != "") {
		if (checkPCode(strPcode) == false && checkZipCode(strPcode) == false) {
			alert("Postal codes have the form ANA-NAN.  Zip codes have the form NNNNN.");
		    oForm.pcode.focus();
		    return false;
        } else {
            oForm.pcode.value = strPcode.toUpperCase();
        }
	}
	strPcode = oForm.proppcode.value;
	if (strPcode != "") {
		if (checkPCode(strPcode) == false && checkZipCode(strPcode) == false) {
			alert("Postal codes have the form ANA-NAN.  Zip codes have the form NNNNN.");
		    oForm.proppcode.focus();
		    return false;
        } else {
            oForm.proppcode.value = strPcode.toUpperCase();
        }
	}

	return true;
}

// Checks the form on the ld.asp page
function check_ld(oForm, bIsAdmin)
{
    var bDisabled = false;
    if (oForm.disabled.checked)
        bDisabled = true;
    
	if (!bDisabled && !bIsAdmin && oForm.leadtype.value == "0")
	{
		alert("You must indicate whether the client is buying or selling.");
		oForm.leadtype.focus();
		return false;
	}

    var strFollowAction = "" + oForm.followaction.value;
    if (!bDisabled && !bIsAdmin && strFollowAction == "0") {
        alert("You must select a follow-up action.");
        oForm.followaction.focus();
        return false;
    }

	var strFollowupDt = "" + oForm.followupdt.value;
    if (!bDisabled && !bIsAdmin && strFollowupDt == "" && strFollowAction != "6") {
        alert("A follow-up date is required.");
        oForm.followupdt.focus();
        return false;
    }
	if (strFollowupDt != "")
	{
		var t = checkDate(strFollowupDt);
		if (t == 0) {
			alert("Please enter a follow-up date in the format YYYY-MM-DD or use the calendar.");
			oForm.followupdt.focus();
			return false;
		} else {
			var d = new Date(t);
			strMo = "" + (d.getMonth()+1);
			strDa = "" + d.getDate();
			if (strMo.length == 1)
				strMo = "0" + strMo;
			if (strDa.length == 1)
				strDa = "0" + strDa;
			strFollowupDt = d.getFullYear()+"-"+strMo+"-"+strDa;
			oForm.followupdt.value = strFollowupDt;
		}
	}

    var strResult = "" + oForm.result.value;
    if (!bDisabled && !bIsAdmin && strResult == "0") {
        alert("You must select a result.");
        oForm.result.focus();
        return false;
    }

    if (check_sal(oForm) == false)
        return false;

    if (strFollowAction == "6") {
        if (!confirm("This prospect will become disabled and returned to the Sales Manager. Please ensure the notes section is updated prior to re-submitting prospect. Click CANCEL to go back and edit your notes. Click OK to submit."))
            return false;
    }

    return true;
}

// Checks the form on the sl.asp page
function check_sldt(id)
{
    var str = "" + id.value;
    if (str == "null") str = "";
    if (str != "") {
        var t = checkDate(str);
        if (t==0) {
            alert("Please enter dates in the format YYYY-MM-DD or use the calendar.");
            id.focus();
            return false;
        } else {
			var d = new Date(t);
			strMo = "" + (d.getMonth()+1);
			strDa = "" + d.getDate();
			if (strMo.length == 1)
				strMo = "0" + strMo;
			if (strDa.length == 1)
				strDa = "0" + strDa;
			str = d.getFullYear()+"-"+strMo+"-"+strDa;
			id.value = str;
		}
    }
    return true;
}
function check_sl(oForm)
{
    if (!check_sldt(oForm.fd1) ||
        !check_sldt(oForm.td1) ||
        !check_sldt(oForm.fd2) ||
        !check_sldt(oForm.td2))
    {
        return false;
    }
    return true;
}
function check_ep(oForm)
{
    var strEmailAlert = "eMail address must be of the form yourname@domain.xxx";

	if (checkFormPhone(oForm.hmPhoneArea, oForm.hmPhonePrefix, oForm.hmPhoneNum, oForm.hmPhone) == false)
		return false;

	if (checkFormPhone(oForm.cellNumArea, oForm.cellNumPrefix, oForm.cellNumNum, oForm.cellNum) == false)
		return false;

	if (checkFormPhone(oForm.hmFaxArea, oForm.hmFaxPrefix, oForm.hmFaxNum, oForm.hmFax) == false)
		return false;

	var strHmPhone  = ""+oForm.hmPhone.value;
	var strFaxPhone = ""+oForm.hmFax.value;
	var strCellPhone= ""+oForm.cellNum.value;
	if (strHmPhone=="" && strFaxPhone=="" && strCellPhone=="")
	{
		alert("A phone number is required.");
		oForm.hmPhoneArea.focus();
		return false;
	}

    str = "" + oForm.hmEmail.value;
   	if (str != "" && checkEmailAddress(str) == false)
	{
		alert(strEmailAlert);
		oForm.hmEmail.focus();
		return false;
	}
	var strPagerEmailDisp = "" + oForm.pagerEmailDisp.value;
    str = "" + oForm.pagerEmail.value;
   	if (str != "" || strPagerEmailDisp != "") {
   		if (strPagerEmailDisp != str) str = strPagerEmailDisp;
        if (checkEmailAddress(str) == false) {
		    alert(strEmailAlert);
		    oForm.pagerNum2.focus();
		    return false;
	    } else {
	        if (oForm.webpageenable[1].checked)
                str = "!" + str;
            oForm.pagerEmail.value = str;
        }
    }
    str = "" + oForm.status.value;
    if (str == "1") {
        str = "" + oForm.agent.value;
        if (str == "0") {
            alert("You must indicate who will be handling your calls while you are away.");
            oForm.agent.focus();
            return false;
        }
    }
    
    return true;
}
function updPagerAddr() {
    var strPagerCos = new Array("--- Select One ---", "Bell (PCS)","Fido (PCS)","Rogers (paging)","Rogers (PCS)",
							    "Telus (Mike)", "Telus (Paging)", "Telus (PCS)", "Pagenet", "Citipage");
    var strPagerURL = new Array("","txt.bell.ca","fido.ca","paging.rogers.com","pcs.rogers.com",
                                "msg.telus.com","msg.telus.com","msg.telus.com", "e.pagenet.ca", "msg.citipage.ab.ca");

	var strNum = "" + document.oForm.pagerNum2.value;
	var strCo  = "" + document.oForm.pagerCo2.value;
    if (strNum != "") {
        for (i=1; i < strPagerCos.length; i++) {
            if (strCo == strPagerCos[i]) {
                var str = strNum + "@" + strPagerURL[i];
                document.oForm.pagerEmail.value = str;
                document.oForm.pagerEmailDisp.value = str;
                break;
            }
        } 
    }
}
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;
	}
	var strEmail = oForm.email.value;
	if (strEmail == "") {
		oForm.email.value = "FormDaemon@sellerdirect.com";
	} else if (strEmail != "" && checkEmailAddress(strEmail) == false) {
		alert("eMail address must be of the form yourname@domain.xxx");
		oForm.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)
{
    var bSignUp = false;
/*    if (oForm.signup.checked == true) {
        var strEmail = oForm.client_email.value;
        if (strEmail == "") {
            alert("A valid eMail address must be supplied in order to sign up for our FREE newsletter.");
            oForm.client_email.focus();
            return false;
        }
    }
*/
    if (check_ps_cmn(oForm) == false)
        return false;

	if (checkFormPhone(oForm.dayPhoneArea, oForm.dayPhonePrefix, oForm.dayPhoneNum, oForm.dayphone) == false)
		return false;
		
	if (checkFormPhone(oForm.nightPhoneArea, oForm.nightPhonePrefix, oForm.nightPhoneNum, oForm.nightphone) == false)
		return false;

    var strDay = "" + oForm.dayphone.value;
    var strNight = "" + oForm.nightphone.value;
    if (strDay == "" && strNight == "") {
		alert("A phone number is required.");
		oForm.dayPhoneArea.focus();
		return false;
    }
		
	var strEmail = oForm.client_email.value;
	if (strEmail != "") {
        if (checkEmailAddress(strEmail) == false) {
		    alert("eMail address must be of the form yourname@domain.xxx");
		    oForm.client_email.focus();
		    return false;
        } else {
            oForm.email.value = strEmail;
        }
	}
    return true;
}
function check_ps2(oForm)
{
    if (check_ps_cmn(oForm) == false)
        return false;

    var strHomePh = "" + oForm.HomePh.value;
    var strWorkPh = "" + oForm.WorkPh.value;
    if (strHomePh == "" && strWorkPh == "") {
		alert("A phone number is required.");
		oForm.HomePh.focus();
		return false;
    }

    if (strHomePh != "" && checkPhone(strHomePh) == false) {
		alert("Phone number format is nnn-nnn-nnnn.");
		oForm.HomePh.focus();
		return false;
	}

	if (strWorkPh != "" && checkPhone(strWorkPh) == false) {
		alert("Phone number format is nnn-nnn-nnnn.");
		oForm.WorkPh.focus();
		return false;
    }

	return true;
}
function check_ps3(oForm)
{
    if (check_ps_cmn(oForm) == false)
        return false;

	if (checkFormPhone(oForm.dayPhoneArea, oForm.dayPhonePrefix, oForm.dayPhoneNum, oForm.dayphone) == false)
		return false;
		
	if (checkFormPhone(oForm.nightPhoneArea, oForm.nightPhonePrefix, oForm.nightPhoneNum, oForm.nightphone) == false)
		return false;

    var strDay = "" + oForm.dayphone.value;
    var strNight = "" + oForm.nightphone.value;
    if (strDay == "" && strNight == "") {
		alert("A phone number is required.");
		oForm.dayPhoneArea.focus();
		return false;
    }
		
	return true;
}
function check_pa(oForm)
{
    var bSignUp = false;
/*    if (oForm.signup.checked == true) {
        var strEmail = oForm.email.value;
        if (strEmail == "") {
            alert("A valid eMail address must be supplied in order to sign up for our FREE newsletter.");
            oForm.email.focus();
            return false;
        }
    }
*/    
    if (check_ps3(oForm) == false)
        return false;
    var str = "" + oForm.PostalCode.value;
    if (str != "") {
		if (checkPCode(str) == false && checkZipCode(str) == false) {
			alert("Postal codes have the form ANA-NAN.  Zip codes have the form NNNNN.");
		    oForm.PostalCode.focus();
		    return false;
        } else {
            oForm.PostalCode.value = str.toUpperCase();
        }
	}
    if (oForm.When_Selling.value == "0") {
        alert("Please indicate if you are selling.");
        oForm.When_Selling.focus();
        return false;
    }
    return true;
}
function check_sct(oForm)
{
    var str = oForm.action.value;
    if (str == "0") {
        alert("Please indicate what you want to do with this testimonial.");
        oForm.action.focus();
        return false;
    }
    str = "" + oForm.realname.value;
    if (str == "") {
        alert("Please enter your name.");
        oForm.realname.focus();
        return false;
    }
	if (checkFormPhone(oForm.dayPhoneArea, oForm.dayPhonePrefix, oForm.dayPhoneNum, oForm.dayphone) == false)
		return false;
		
	if (checkFormPhone(oForm.nightPhoneArea, oForm.nightPhonePrefix, oForm.nightPhoneNum, oForm.nightphone) == false)
		return false;

    var strDay = "" + oForm.dayphone.value;
    var strNight = "" + oForm.nightphone.value;
    if (strDay == "" && strNight == "") {
		alert("A phone number is required.");
		oForm.dayPhoneArea.focus();
		return false;
    }
    
	if (oForm.agree.checked!=true) {
		alert("You must check the box to indicate that you agree with the terms and conditions for a testimonial.");
		oForm.agree.focus();
		return false;
	}
    str = oForm.email.value;
	if (str == "") {
		oForm.email.value = "FormDaemon@sellerdirect.com";
	} else if (str != "" && checkEmailAddress(str) == false) {
		alert("e-mail address must be of the form yourname@domain.xxx");
		oForm.email.focus();
		return false;
	}
    return true;
}
function check_req(oForm)
{
    var strEmail = "" + oForm.email.value;
    if (strEmail == "") {
        alert("You must supply an email address to recieve our free reports.");
        oForm.email.focus();
        return false;
    }
    if (check_pa(oForm) == false)
        return false;
    return true;
}
function check_ohd(oForm)
{
	if (oForm.mlsnum.value == "0" && oForm.exclID.value == "0") {
		alert("Please select an MLS<sup>&reg;</sup> System listing or an exclusive listing.");
		oForm.mlsnum.focus();
		return false;
	} else if (oForm.mlsnum.value != "0" && oForm.exclID.value != "0") {
		alert("Please select either an MLS<sup>&reg;</sup> System listing or an exlusive listing, but not both.");
		oForm.mlsnum.focus();
		return false;
	}
	if (oForm.agent.value=="0") {
		alert("Please select an agent to host the open house.");
		oForm.agent.focus();
		return false;
	}
	var str = "" + oForm.date.value;
	if (str == "") {
		alert("Please enter a date for the open house.");
		oForm.date.focus();
		return false;
	} else {
		var strFmtDate = "";
		var nPos = -1;
		do {
			nPos = str.indexOf(";");
			var strLeft = "";
			if (nPos > 0) {
				strLeft = str.substr(0,nPos);
				str = str.substr(nPos+1,str.length);
			} else {
				strLeft = str;
			}
			var t = checkDate(strLeft);
			if (t == 0) {
				alert("Please enter dates in the format YYYY-MM-DD.");
				oForm.date.focus();
				return false;
			} else {
				var d = new Date(t);
				var strMo = "" + (d.getMonth()+1);
				var strDa = "" + d.getDate();
				if (strMo.length == 1)
					strMo = "0" + strMo;
				if (strDa.length == 1)
					strDa = "0" + strDa;
				var strNewDt = d.getFullYear()+"-"+strMo+"-"+strDa;
				if (strFmtDate != "")
					strFmtDate += ";"
				strFmtDate += strNewDt;
			}
		} while (nPos > 0);
		oForm.date.value = strFmtDate;
	}
	
	str = "" + oForm.time.value;
	if (str == "") {
		alert("Please enter a time for the open house.");
		oForm.time.focus();
		return false;
	}
	var strRate = "" + oForm.rate.value;
	if (oForm.recurring.checked) {
		if (strRate == "") {
			alert("Please enter the number of days between each recurring open house.");
			oForm.rate.focus();
			return false;
		}
	}
	if (strRate != "") {
		var n=parseInt(strRate);
		if (isNaN(n)) {
			alert("Please enter a valid number.");
			oForm.rate.focus();
			return false;
		}
	}
	return true;
}
function check_eld(oForm)
{
	if (oForm.agent.value=="0") {
		alert("Please select an agent for the listing.");
		oForm.agent.focus();
		return false;
	}
	var str = "" + oForm.address.value;
	if (str == "") {
		alert("Please enter an address for the listing.");
		oForm.address.focus();
		return false;
	}
	str = "" + oForm.city.value;
	if (str == "") {
		alert("Please enter a city for the listing.");
		oForm.city.focus();
		return false;
	}
	str = "" + oForm.prov.value;
	if (str == "") {
		alert("Please enter a two character province or state code for the listing.");
		oForm.prov.focus();
		return false;
	}
	str = "" + oForm.list_date.value;
	if (str == "") {
		alert("Please enter a listing date for the listing.");
		oForm.list_date.focus();
		return false;
	} else if (checkDate(str) == 0) {
		alert("Please enter a listing date in the format YYYY-MM-DD or use the calendar.");
		oForm.list_date.focus();
		return false;
	}
	str = "" + oForm.expiry_dt.value;
	if (str != "" && checkDate(str) == 0) {
		alert("Please enter an expiry date in the format YYYY-MM-DD or use the calendar.");
		oForm.expiry_dt.focus();
		return false;
	}
	
	str = "" + oForm.list_price.value;
	if (str == "") {
		alert("Please enter a price for the listing.");
		oForm.list_price.focus();
		return false;
	}
	return true;
}
function check_agent(nCount, doc, bCheckAgent, strAction)
{
	if (bCheckAgent) {
		if (doc.getElementById("agent").selectedIndex == 0) {
			alert("Please select the agent you are re-assigning the leads to.");
			doc.getElementById("agent").focus();
			return false;
		}
	}
	doc.getElementById("num_checks").value = "" + nCount;
	for (i=0; i < nCount; i++) {
		if (doc.getElementById("check"+i).checked == true)
			break;
	}
	if (i == nCount) {
		alert("At least one lead must be checked to be "+strAction+".");
		return false;
	}
	return true;
}
function check_corp(oForm)
{
	var strEmailAlert = "email address must be of the form yourname@domain.xxx";

	var str = "" + oForm.officeID.value;
	if (str == "") {
		alert("Please enter an office ID.");
		oForm.officeID.focus();
		return false;
	}
	str = "" + oForm.mlscode.value;
	if (str == "") {
		alert("Please enter an MLS<sup>&reg;</sup> System Firm code.");
		oForm.mlscode.focus();
		return false;
	}
	str = "" + oForm.office_name.value;
	if (str == "") {
		alert("Please enter an office name.");
		oForm.office_name.focus();
		return false;
	}
	str = "" + oForm.short_name.value;
	if (str == "") {
		alert("Please enter a short office name.");
		oForm.short_name.focus();
		return false;
	}
	str = "" + oForm.office_url.value;
	if (str == "") {
		alert("Please enter the web address for the office.");
		oForm.office_url.focus();
		return false;
	}
	str = "" + oForm.board_name.value;
	if (str == "") {
		alert("Please enter a real estate board name.");
		oForm.board_name.focus();
		return false;
	}
	str = oForm.office_email.value;
	if (str != "" && checkMultiEmailAddress(str) == false) {
		alert(strEmailAlert);
		oForm.office_email.focus();
		return false;
	}
	str = oForm.mgr_email.value;
	if (str != "" && checkMultiEmailAddress(str) == false) {
		alert(strEmailAlert);
		oForm.mgr_email.focus();
		return false;
	}
	str = oForm.answer_email.value;
	if (str != "" && checkMultiEmailAddress(str) == false) {
		alert(strEmailAlert);
		oForm.answer_email.focus();
		return false;
	}
	if (checkFormPhone(oForm.busPhoneArea, oForm.busPhonePrefix, oForm.busPhoneNum, oForm.busPhone) == false)
		return false;

	if (checkFormPhone(oForm.tollPhoneArea, oForm.tollPhonePrefix, oForm.tollPhoneNum, oForm.tollPhone) == false)
		return false;

	if (checkFormPhone(oForm.faxNumArea, oForm.faxNumPrefix, oForm.faxNumNum, oForm.faxPhone) == false)
		return false;

	return true;
}
function check_new_cmn(oForm, strEmailMsg) {
    var strEmail = "" + oForm.email.value;
    if (strEmail == "") {
        if (strEmailMsg == "")
            alert("Please enter an eMail address.");
        else
            alert(strEmailMsg);
        oForm.email.focus();
        return false;
    }

	if (check_ps3(oForm) == false)
		return false;

    var str = "" + oForm.PostalCode.value;
    if (str != "") {
		if (checkPCode(str) == false && checkZipCode(str) == false) {
			alert("Postal codes have the form ANA-NAN.  Zip codes have the form NNNNN.");
			oForm.PostalCode.focus();
			return false;
		} else {
			oForm.PostalCode.value = str.toUpperCase();
		}
	}
}
function check_fst(oForm) {
    if (check_new_cmn(oForm, "You must supply an email address to recieve our free reports") == false)
        return false;
        
    return true;
}
function check_ri(oForm) {
    if (check_new_cmn(oForm, "") == false)
        return false;
        
	var str = "" + oForm.Requested_info.value;
	if (str == "" || str == "0") {
		alert("Please indicate what information you would like.");
		oForm.Requested_info.focus();
		return false;
	}
	return true;
}
function check_bdva(oForm) {
    if (check_new_cmn(oForm, "") == false)
        return false;
        
    var str = "" + oForm.min_price.value;
	if (str == "" || str == "0") {
		alert("Please indicate a minimum price.");
		oForm.min_price.focus();
		return false;
	}
	str = "" + oForm.max_price.value;
	if (str == "" || str == "0") {
		alert("Please indicate a maximum price.");
		oForm.max_price.focus();
		return false;
	}
	str = "" + oForm.Location.value;
	if (str == "" || str == " ") {
		alert("Please indicate a preferred location / community.");
		oForm.location.focus();
		return false;
	}
	str = "" + oForm.TypeOfHome.value;
	if (str == "" || str == " ") {
		alert("Please indicate a preferred type.");
		oForm.Preferred_Type.focus();
		return false;
	}
	str = "" + oForm.Style.value;
	if (str == "" || str == " ") {
		alert("Please indicate a preferred style.");
		oForm.Preferred_Style.focus();
		return false;
	}
	str = "" + oForm.BedsUp.value;
	if (str == "" || str == " ") {
		alert("Please indicate the minimum number of bedrooms up.");
		oForm.min_beds_up.focus();
		return false;
	}
	str = "" + oForm.total_bedrooms.value;
	if (str == "" || str == " ") {
		alert("Please indicate the minimum total number of bedrooms.");
		oForm.total_bedrooms.focus();
		return false;
	}
	str = "" + oForm.min_bathrooms.value;
	if (str == "" || str == " ") {
		alert("Please indicate the minimum number of bathrooms.");
		oForm.min_bathrooms.focus();
		return false;
	}
	str = "" + oForm.min_size.value;
	if (str == "" || str == " ") {
		alert("Please indicate the minimum size.");
		oForm.min_size.focus();
		return false;
	}
	str = "" + oForm.Finished_Basement.value;
	if (str == "" || str == " ") {
		alert("Please indicate your basement preference.");
		oForm.Finished_Basement.focus();
		return false;
	}
	str = "" + oForm.Parking.value;
	if (str == "" || str == " ") {
		alert("Please indicate your parking preference.");
		oForm.Parking.focus();
		return false;
	}
	str = "" + oForm.Prequalified.value;
	if (str == "" || str == " ") {
		alert("Please indicate if you are pre-qualified for a mortgage.");
		oForm.Prequalified.focus();
		return false;
	}
	return true;
}
