function MM_swapImgRestore() { //v3.0
    var i,x,a=document.MM_sr;
    for(i=0; a && i<a.length && (x=a[i]) && x.oSrc; i++)
        x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
    var d=document;
    if (d.images) {
        if (!d.MM_p)
            d.MM_p=new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments;
        for(i=0; i<a.length; i++)
            if (a[i].indexOf("#") != 0) {
                d.MM_p[j] = new Image;
                d.MM_p[j++].src = a[i];
            }
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x;
    if (!d)
        d=document;
    if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p+1)].document;
        n = n.substring(0,p);
    }
    if (!(x = d[n]) && d.all)
        x = d.all[n];
    for (i=0; !x && i < d.forms.length; i++)
        x = d.forms[i][n];
    for (i=0; !x && d.layers && i < d.layers.length; i++)
        x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById)
        x = d.getElementById(n);
    return x;
}

function MM_swapImage() { //v3.0
    var i, j=0, x, a=MM_swapImage.arguments;
    document.MM_sr = new Array;
    for (i=0; i < (a.length-2); i += 3)
        if ((x=MM_findObj(a[i])) != null) {
            document.MM_sr[j++] = x;
            if (!x.oSrc)
                x.oSrc = x.src;
            x.src = a[i+2];
        }
}

function openPopup(theURL, windowName, width, height) { //v2.0
  getPopup(theURL, windowName, width, height);
  return false;
}

function getPopup(theURL, windowName, width, height) {
  return window.open(theURL,windowName,'scrollbars=yes,resizable=yes,width='+ width +',height='+ height);
}

function doSubmit(formName) {
    document.forms[formName].submit();
}

// Prevents the double-clicking of form controls.
function onSubmitNoDC(formRef) {
    if (formRef.alreadySubmitted)
        return false;
    formRef.alreadySubmitted = "yupper";
    return true;
}

//script for switching form btn mouse over state
function formBtnsEast(submitButton, state) {
    if (state == "over") {
        document.getElementById(submitButton).className = "formButtonOn";
        document.getElementById(submitButton+"L").src = "/images/form_btn_east_left_on.gif";
        document.getElementById(submitButton+"R").src = "/images/form_btn_east_right_on.gif";
    }
    else if (state == "off") {
        document.getElementById(submitButton).className = "formButtonOff";
        document.getElementById(submitButton+"L").src = "/images/form_btn_east_left.gif";
        document.getElementById(submitButton+"R").src = "/images/form_btn_east_right.gif";
    }
}
function formBtnsWest(submitButton, state) {
    if (state == "over") {
        document.getElementById(submitButton).className = "formButtonOn";
        document.getElementById(submitButton+"L").src = "/images/form_btn_west_left_on.gif";
        document.getElementById(submitButton+"R").src = "/images/form_btn_west_right_on.gif";
    }
    else if (state == "off") {
        document.getElementById(submitButton).className = "formButtonOff";
        document.getElementById(submitButton+"L").src = "/images/form_btn_west_left.gif";
        document.getElementById(submitButton+"R").src = "/images/form_btn_west_right.gif";
    }
}
function formBtnsNone(submitButton, state) {
    if (state == "over") {
        document.getElementById(submitButton).className = "formButtonOn";
        document.getElementById(submitButton+"L").src = "/images/form_btn_east_left_on.gif";
        document.getElementById(submitButton+"R").src = "/images/form_btn_west_right_on.gif";
    }
    else if (state == "off") {
        document.getElementById(submitButton).className = "formButtonOff";
        document.getElementById(submitButton+"L").src = "/images/form_btn_east_left.gif";
        document.getElementById(submitButton+"R").src = "/images/form_btn_west_right.gif";
    }
}

function formLinkEast(submitButton, state) {
    if (state == "over") {
        document.getElementById(submitButton).className = "formLinkOn";
        document.getElementById(submitButton+"L").src = "/images/form_btn_east_left_on.gif";
        document.getElementById(submitButton+"R").src = "/images/form_btn_east_right_on.gif";
    }
    else if (state == "off") {
        document.getElementById(submitButton).className = "formLinkOff";
        document.getElementById(submitButton+"L").src = "/images/form_btn_east_left.gif";
        document.getElementById(submitButton+"R").src = "/images/form_btn_east_right.gif";
    }
}
function formLinkWest(submitButton, state) {
    if (state == "over") {
        document.getElementById(submitButton).className = "formLinkOn";
        document.getElementById(submitButton+"L").src = "/images/form_btn_west_left_on.gif";
        document.getElementById(submitButton+"R").src = "/images/form_btn_west_right_on.gif";
    }
    else if (state == "off") {
        document.getElementById(submitButton).className = "formLinkOff";
        document.getElementById(submitButton+"L").src = "/images/form_btn_west_left.gif";
        document.getElementById(submitButton+"R").src = "/images/form_btn_west_right.gif";
    }
}
function formLinkNone(submitButton, state) {
    if (state == "over") {
        document.getElementById(submitButton).className = "formLinkOn";
        document.getElementById(submitButton+"L").src = "/images/form_btn_east_left_on.gif";
        document.getElementById(submitButton+"R").src = "/images/form_btn_west_right_on.gif";
    }
    else if (state == "off") {
        document.getElementById(submitButton).className = "formLinkOff";
        document.getElementById(submitButton+"L").src = "/images/form_btn_east_left.gif";
        document.getElementById(submitButton+"R").src = "/images/form_btn_west_right.gif";
    }
}

function updateCharCount(tt, maxlen) {
    if (!tt)
        return;

    var theSpan = document.getElementById(tt.id +"Span");
    var len = tt.value.length + getMultCharCount(tt.value);
    theSpan.innerHTML = ""+ len +" of max "+ maxlen;
    if (len > maxlen)
        theSpan.style.color = "red";
    else
        theSpan.style.color = "";
}

function getMultCharCount(str) {
    var char;
    var x = 0;
    var count = 0;
    var desc = "";
	for (var i=0; i<str.length; i++) {
	    char = str.charAt(i);
	    if (char == '<')
	        count += 3;
	    if (char == '\n')
	        count += 1;
	    if (char == '&')
	        count += 4;
        if (char == '\u2026')
            count += 2;
	}
    return count;
}

function replaceSpecials(ttId) {
    if (!ttId)
        return;
    var tt = document.getElementById(ttId);
    if (!tt)
        return;
    
    var str = tt.value;
    // The BFI way of handling unicode.
    str = str.replaceAll("\u2018", "'");
    str = str.replaceAll("\u2019", "'");
    str = str.replaceAll("\u201C", "\"");
    str = str.replaceAll("\u201D", "\"");
    str = str.replaceAll("\u2026", "...");
    tt.value = str;
}

function replaceAll(beforeStr, afterStr) {
    var pos = 0;
    var result = this;
    while ((pos = result.indexOf(beforeStr, pos)) != -1) {
        result = result.substring(0,pos) + afterStr + result.substring(pos + beforeStr.length);
        pos += afterStr.length;
    }
    return result;
}
String.prototype.replaceAll=replaceAll;


function AdImage() {
    this.href;
    this.img;
}

function createAdImage(imgSrc, href) {
    var ad = new AdImage();
    ad.img = imgSrc;
    ad.href = href;
    return ad;
}

function randomizeImages(arr) {
    var ads = new Array();
    var rand, ad, temp;
    while (arr.length > 0) {
        rand = Math.floor(Math.random() * arr.length);
        ad = arr[rand];
        temp = ad.img;
        ad.img = new Image();
        ad.img.src = temp;
        ads[ads.length] = ad;
        while (rand < arr.length - 1)
            arr[rand] = arr[++rand];
        arr.length = arr.length - 1;
    }
    return ads;
}

function isCurrency(fieldValue) {
	var nNum = 0;			// Total numbers for currency value.
	var nDollarSign = 0;	// Total times a dollar sign occurs.
	var nDecimal = 0;		// Total times a decimal point occurs.
	var nCommas = 0;		// Total times a comma occurs.
	var txtLen;				// Length of string passed.
	var xTxt;				// Assigned object passed.
	var sDollarVal;			// Assigned dollar amount with or without commas.
	var bComma;				
	var decPos;				// Assigned value of numbers or positions after decimal point.
	var nNumCount = 0;		// Total number between commas.
	var i;					// For forloop indexing.
	var x;					// Assigned each indivual character in string.
	
	// Set the xTxt variable to the object passed to this function.
	// Assign the length of the string to txtLen.
	xTxt = fieldValue;
	txtLen = xTxt.length;

	for(i = 0; i < txtLen; i++) {
		// Assign charater in substring to x.
		x = xTxt.substr(i, 1);
		
		
		if(x == "$")
			nDollarSign = nDollarSign + 1; // Sum total times dollar sign occurs.
		else if(x == ".")
			nDecimal = nDecimal + 1; // Sum total times decimal point occurs.
		else if(x == ",")
			nCommas = nCommas + 1; // Sum total times comma occurs.
		else if(parseInt(x) >= 0 || parseInt(x) <= 9)
			nNum = nNum + 1; // If the character is a number sum total times a number occurs.
		else {
			// Error occurs if any other character value is in the string
			// othere then the valid characters.
			alert("ERROR! \n\nYou have entered an illegal value!\nPlease enter only: Dollar" +
				  " Signs, Commas, Decimal Points, and numbers between 0...9!");
			return false;
		} // end else
	} // end for

	if(nDollarSign > 1) {
		alert("ERROR! \n\nYou have entered more then one dollar sign!\nPlease only enter one!");
		return false;
	} // end if
		
	if(nDecimal > 1) {
		alert("ERROR! \n\nYou have entered more then one decimal point!\nPlease only enter one!");
		return false;
	} // end if
		
	if(nDollarSign == 1) {
		// Make sure dollar sign in the first character in string
		// if there is a dollar sign present.
		if(xTxt.indexOf("$") != 0) {
			alert("ERROR!  \n\nThe dollar sign you entered is not in the correct position!");
			return false;
		} // end if
	}// end if
	
	if(nDecimal == 1) {
		// Get the number of numbers after the decimal point in
		// the string if there is a decimal point present
		decPos = (txtLen - 1) - xTxt.indexOf(".");
		
		// Floating point cannot be more then two.
		// Valid format after decimal point.
		/**********************************/
		/*   $#.##, $#.#, $.#, $#., $.##  */
		/**********************************/
		if(decPos > 2) 	{
			alert("ERROR! \n\nThe decimal point you entered is not in the correct position!");
			return false;
		} // end if
	} // end if
	
	if(nCommas == 0)
		// If no commas are present value is a valid US
		// currency.
		return true;
	else {
		// Get total number of dollar number(s), removing
		// floating point numbers or cents.
		nNum = nNum - decPos;
		
		// Determine if dollar sign is in string so to be 
		// removed.
		// After determining dollar sign, assign sDollarVal
		// numbers and comma(s)
		if(xTxt.indexOf("$", 0) == 0)
			sDollarVal = xTxt.substr(1, (nNum + nCommas));
		else
			sDollarVal = xTxt.substr(0, (nNum + nCommas));
		
		
		// Determine if a zero is the first number or if a
		// comma is the first or last character in the string.
		if(sDollarVal.lastIndexOf("0", 0) == 0 ) {
			alert("ERROR! \n\nYou cannot start the dollar amount out with a zero!");
			return false;
		}
		else if(sDollarVal.lastIndexOf(",", 0) == 0) {
			alert("ERROR! \n\nYou cannot start the dollar amount out with a comma!");
			return false;
		}
		else if(sDollarVal.indexOf(",", (sDollarVal.length - 1)) == (sDollarVal.length - 1)) {
			alert("ERROR! \n\nYou cannot end the dollar amount with a comma!");
			return false;
		}
		else {
			// Initialize bComma indicating a comma has not been
			// occured yet.
			bComma = false;
			for(i = 0; i < sDollarVal.length; i++) {
				// Assign charater in substring to x.
				x = sDollarVal.substr(i, 1);
				
				if(parseInt(x) >= 0 || parseInt(x) <= 9) {
					// If x is a number add one to the number counter.
					nNumCount = nNumCount + 1;
					
					// Sense comma(s) are present number counter cannot
					// be more then three before the first or next comma.
					if(nNumCount > 3) {
						alert("ERROR! \n\nYou have a mis-placed comma!");
						return false;
					} // end if
				}
				else {
					// If the number counter is less then three and
					// the comma indicator is true the comma is either
					// mis-placed or there are not enough values.
					if(nNumCount != 3 && bComma) {
						alert("ERROR! \n\nYou have a mis-placed comma!");
						return false;
					} // end if
					
					// Reset the number counter back to zero.
					nNumCount = 0;
					
					// Set the comma indicator to true indicating
					// that the first comma has been found and that
					// there now MUST be three numbers after each
					// comma until the loop hits the end.
					bComma = true;
				} // end i
			} // end for
			
			// Determine if after the loop ended that there
			// was a total of three final numbers after the
			// last comma.
			if(nNumCount != 3 && bComma) {
				alert("ERROR! \n\nYou have a mis-placed comma!");
				return false;
			} // end if
		} // end if
	} // end if
	
	// Return true indicating that the value is a valid
	// currency.
	return true;
}

function stringFilter(inputField, filteredValues) {
    inputField.value = getFilteredString(inputField.value, filteredValues);
}

function getFilteredString(s, filteredValues) {
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (filteredValues.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function openHelp(name) {
    var helpWin = getPopup('onlineHelpPop.crl#'+name, 'help', 480, 650);
    if (window.focus)
        helpWin.focus();
    openHelpWait(helpWin, name);
}

function openHelpWait(helpWin, name) {
    // Wait for the window to finish loading.
    var loaded = false;
    try {
        if (helpWin.document.getElementById("windowLoaded"))
            loaded = true;
    }
    catch (e) {}
    if (!loaded) {
        setTimeout(function () { openHelpWait(helpWin, name); }, 100);
        return;
    }
    
    // Find elements with the orange class and reset them.
    var elems = helpWin.document.getElementsByTagName("td");
    for (var i=0; i<elems.length; i++) {
        if (elems[i].className == 'copyTitleOrange')
            elems[i].className = "titleSm";
        if (elems[i].id == name)
            elems[i].className = "copyTitleOrange";
    }
}

function crlOnLoad() {
    // no op. Should be overridden by pages that need to.
}


// Location data
function countryChange(listings) {
    if (!listings)
        listings = false;
    LocationData.getRegions(countryChangeCB, $("countries").value, listings);
}

function countryChangeCB(regions) {
    var regionDD = $("regions");
    clearList(regionDD);
    if (regions)
        DWRUtil.addOptions(regionDD, regions, "id", "name");
        
    if (defaultRegion)
        regionDD.value = defaultRegion;
    
    if ($("subRegionGroups"))
        regionChangeGroups();
    if ($("subRegions"))
        regionChange();
}

function regionChangeGroups() {
    LocationData.getSubRegionGroups(regionChangeGroupsCB, $("regions").value);
}

function regionChangeGroupsCB(groups) {
    var subRegionGroupDD = $("subRegionGroups");
    clearList(subRegionGroupDD);
    if (groups) {
        var option, i, j;
        for (i=0; i<groups.length; i++) {
            option = new Option(groups[i].name, "g"+groups[i].id);
            option.className = "formSelectDark";
            subRegionGroupDD.options[subRegionGroupDD.options.length] = option;
            
            for (j=0; j<groups[i].subRegions.length; j++) {
                option = new Option(groups[i].subRegions[j].name, "s"+ groups[i].subRegions[j].id);
                option.className = "formSelectLight";
                subRegionGroupDD.options[subRegionGroupDD.options.length] = option;
            }
        }
    }
    
    if (defaultSubRegionGroup)
        subRegionGroupDD.value = defaultSubRegionGroup;
    subRegionGroupChange();
}

function regionChange() {
    LocationData.getSubRegions(regionChangeCB, $("regions").value);
}

function regionChangeCB(subRegions) {
    var subRegionDD = $("subRegions");
    clearList(subRegionDD);
    if (subRegions) {
        var option, i;
        for (i=0; i<subRegions.length; i++) {
            option = new Option(subRegions[i].name, subRegions[i].id);
            subRegionDD.options[subRegionDD.options.length] = option;
        }
    }
    
    if (defaultSubRegion)
        subRegionDD.value = defaultSubRegion;
    subRegionChange();
}

function subRegionGroupChange() {
    subRegionChangeImpl($("subRegionGroups").value);
}

function subRegionChange() {
    subRegionChangeImpl($("subRegions").value);
}

function subRegionChangeImpl(id) {
    if ($("cities"))
        LocationData.getCities(setCitiesCB, id);
    if ($("counties"))
        LocationData.getCounties(setCountiesCB, id);
}

function setCitiesCB(cities) {
    var citiesDD = $("cities");
    clearList(citiesDD);
    if (cities) {
        var option, i;
        for (i=0; i<cities.length; i++) {
            option = new Option(cities[i].name, cities[i].id);
            option.className = "formSelectLight";
            citiesDD.options[citiesDD.options.length] = option;
        }
        
        option = new Option("Other city...", -1)
        option.className = "formSelectDark";
        citiesDD.options[citiesDD.options.length] = option;
    }
    
    if (defaultCity)
        citiesDD.value = defaultCity;
    cityChange();
}

function cityChange() {
    var otherCityTX = $("otherCity");
    if (otherCityTX)
        otherCityTX.disabled = $("cities").value != -1;
}

function setCountiesCB(counties) {
    var countiesDD = $("counties");
    clearList(countiesDD);
    if (counties) {
        DWRUtil.addOptions(countiesDD, counties, "id", "name");
        if (defaultCounty)
            countiesDD.value = defaultCounty;
        if (countiesDD.value == 0 && counties.length == 1)
            countiesDD.value = counties[0].id;
    }
}

