
/*
   Copyright 2002 The GoHome Networks, Inc. All rights reserved. No
   reproduction, distribution, or transmission of the copyrighted
   materials at this site is permitted without the written permission
   of The GoHome Networks, unless otherwise specified. MLSWorks,
   BrokerWorks, ActiveListing, and AdTracker are Trademarks of
   The GoHome Networks, Inc.

   ActiveAgent is a registered Service Mark of The GoHome Networks, Inc.
*/

/*
	ClearNonZip(theform)
	This will find the approriate fields in the form and clear their value if a
	zip code has been given. If there is no zip code, it restores the appropriate
	fields in the form to their default value.

	Arguments:	theform = the Javascript form object

	Variables:	clearFields = a list of fields to be cleared when a zip is given,
				              or to be restored when a zip is not given
*/

function ClearNonZip(theform) {
	var clearFields = new Array('citycode','countycode','statecode','area');

	var NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape6')!=-1)?true:false;

	if (NS6) {
		if (theform.zip.value) {
			alert('Due to a bug in Netscape 6, you can only search for a zip code in the areas you have chosen. If the zip code you have entered ('+theform.zip.value+') is not in the area you have chosen, your search will not return any results.\n\nPlease try another browers if you need to do a zip code search.');
		}
		return;
	}

	if (theform.zip.value) {
		for(j=0; j<clearFields.length; j++) {
			SetNamedElementValue(theform, clearFields[j], "", 0);
		}
	} else {
		for(j=0; j<clearFields.length; j++) {
			RestoreNamedElementDefaultValue(theform, clearFields[j], 0);
		}
	}
}

/*
	SetNamedElementValue (theform, thefield, thevalue, maxchanges)
	This will find ALL (or "maxchanges") elements in an object named "thefield"
	and set them to "the value".

	Arguments:	theform = the Javascript form object
				thefield = the form object element name
				thevalue = the value to be assigned to form object element
				maxchanges = if greater than 0, then only this many elements will be set
*/

function SetNamedElementValue(theform, thefield, thevalue, maxchanges) {
	var changes = 0;
	var debug = 0;

	var isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
	var IEmac = ((document.all)&&(isMac)) ? true : false;

	for(i=0; i<theform.length; i++) {
		if (theform.elements[i].name == thefield) {
			if (!IEmac && theform.elements[i].type == 'hidden' && theform.elements[i].defaultValue == undefined) {
				if (debug) alert('Fixing undefined default value for hidden field...\n' + thefield + ' = ' + theform.elements[i].value);
				theform.elements[i].defaultValue = theform.elements[i].value;
			}
			if (debug) alert('Setting...\n' + thefield + ' = ' + thevalue);
			theform.elements[i].value = thevalue;
			changes++;
			if (maxchanges && (changes >= maxchanges))
				break;
		}
	}
}

/*
	RestoreNamedElementDefaultValue (theform, thefield, maxchanges)
	This will find ALL elements in an object named "thefield" and set them to their "defaultValue".
	If "maxchanges" is greater than 0, then it will change "maxchanges" elements instead of "ALL".

	Arguments:	theform = the Javascript form object
				thefield = the form object element name
				maxchanges = if greater than 0, then only this many elements will be restored
*/

function RestoreNamedElementDefaultValue(theform, thefield, maxchanges) {
	var changes = 0;
	var debug = 0;

	var isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
	var IEmac = ((document.all)&&(isMac)) ? true : false;

	for(i=0; i<theform.length; i++) {
		if (theform.elements[i].name == thefield) {
			if ( theform.elements[i].value == theform.elements[i].defaultValue ) {
				if (debug) alert('Skipping...\n'+ thefield +' has not changed from default.');
				continue;
			}
			if (IEmac || theform.elements[i].type != 'hidden' || theform.elements[i].defaultValue != undefined) {
				if (debug) alert('Resetting...\n' + thefield + ' = ' + theform.elements[i].defaultValue);
				theform.elements[i].value = theform.elements[i].defaultValue;
				changes++;
				if (maxchanges && (changes >= maxchanges))
					break;
			} else {
				if (debug) alert('Not resetting...\n'+ thefield +' is a hidden field with an undefined default value.');
			}
		}
	}
}

function EMPTY(val)
{
	if (val == null || val == '')
		return true;

	emptyRegExp = /^\s*$/;
	if (emptyRegExp.test(val))
		return true;

	return false;
}

function ValidateEmail(emailAddress)
{
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|COM|NET|ORG|EDU|INT|MIL|GOV|ARPA|BIZ|AERO|NAME|COOP|INFO|PRO|MUSEUM)$/i;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var hostPat=new RegExp("^([a-zA-Z0-9][a-zA-Z0-9-]{0,60}){0,1}[a-zA-Z0-9]$");
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailAddress.match(emailPat);
	if (matchArray==null) {
		alert("The Email address seems incorrect (check @ and .'s)");
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			alert("The username of the Email address contains invalid characters.");
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			alert("The domain name of the Email address contains invalid characters.");
			return false;
		}
	}
	if (user.match(userPat)==null) {
		alert("The username of the Email address doesn't seem to be valid.");
		return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP of the Email address is invalid!");
				return false;
			}
		}
		return true;
	}
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].match(hostPat) == null) {
			alert("The domain name of the Email address does not seem to be valid.");
			return false;
		}
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("The Email address must end in a well-known domain or two letter country.");
		return false;
	}
	if (len<2) {
		alert("The Email address is missing a hostname!");
		return false;
	}
	return true;
}

function ValidateForm(theform,validateFields)
{
    var validationNames = [];

    for (var i=0; i<theform.length; i++)
    {
        var input = theform.elements[i];
        if (typeof validateFields[input.name] == 'object' && validateFields[input.name].required)
        {
            validationNames.push(input.name);
            if (validateFields[input.name].validateEmail)
            {
                if (ValidateEmail(input.value))
                    validateFields[input.name].success = 1;
            }
            else if (validateFields[input.name].isCheckbox)
            {
                if (input.checked)
                    validateFields[input.name].success = 1;
            }
            else
            {
                if (!EMPTY(input.value))
                    validateFields[input.name].success = 1;
            }
        }
    }
    var allok = true;
    var i;
    while (validationNames.length > 0)
    {
        i = validationNames.pop();
        if (!validateFields[i].success)
        {
            if (validateFields[i].requiredMessage)
                alert(validateFields[i].requiredMessage);
            allok = false;
        }
    }
    return allok;
}

function VerifyMLS(theForm)
{
	if (theForm.mls) {
		if (theForm.mls.value == '') {
			alert( "Please enter a valid MLS #");
			return false;
		} else {
			return true;
		}
	}
	return true;
}

function VerifyZip(theForm)
{
	if (theForm.zip) {
		if (theForm.zip.value == '') {
			alert( "Please enter a valid Zip Code");
			return false;
		} else {
			return true;
		}
	}
	return true;
}

function VerifyAdCode(theForm)
{
	if (theForm.mls) {
		if (theForm.mls.value == '') {
			alert( "Please enter a valid Ad Code");
			return false;
		} else {
			return true;
		}
	}
	return true;
}

function clearInputText(thefield)
{
	if (thefield.defaultValue==thefield.value)
		thefield.value = ""
}

function toggleMenu(currMenu) {
	if (document.getElementById) {
		thisMenu = document.getElementById(currMenu).style
		if (thisMenu.display == "block") {
			thisMenu.display = "none"
		}
		else {
			thisMenu.display = "block"
		}
		return false
	}
	else {
		return true
	}
}


function hideMenu(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "none";
}



