// ########## Start Browser Compatibility ######### //
if (document.all && !document.getElementById) {
	document.getElementById = function(id) {
		return document.all[id];
	}
}

if (typeof Array.prototype.push == "undefined") {
	Array.prototype.push = function() {
		var A_p = 0;
		for (A_p = 0; A_p < arguments.length; A_p++) {
			this[this.length] = arguments[A_p];
		}
		return this.length;
	}
}
// ########## END Browser Compatibility ######### //


	function sortSelect(selectToSort) {
		var myOptions = new Array(selectToSort.options.length);
		for (var i=0; i<selectToSort.options.length; i++) {
			myOptions[i] = selectToSort.options[i];
		}
		myOptions.sort(sortOptionsByName);
		selectToSort.options.length = 0;
		for (var i=0; i<myOptions.length; i++) {
			selectToSort.options[selectToSort.length] = myOptions[i];
		}
		selectToSort.options[0].selected=true;
	}
	
	function sortOptionsByName(record1, record2) {
		var value1 = record1.text.toLowerCase();
		var value2 = record2.text.toLowerCase();
		// Special handling for the default case
		if (value1.indexOf('-all-') > 0 || value1 > value2) return(1);
		if (value2.indexOf('-all-') > 0 || value1 < value2) return(-1);
		return(0);
	}
	
	function setElementDisplay(elName, display) {
		var el = document.getElementById(elName);
		if (el && el.style)
			el.style.display=display;
	}


// ########## Popup Functions ################### //

// Master popup function
function popup(URL, name, properties) {
	var myWindow = window.open(URL, name, properties);
	if (myWindow) {
		myWindow.focus();
	} else {
		if (confirm('Your web browser appears to be configured to block popup windows.\nFor more information, please click OK.')) {
			location.href='/Consumer/Popup_Info.aspx';
		}
	}
}

function email_property(URL) {
	popup(URL, "EmailProperty", "height=600,width=475,scrollbars=yes,menubar=no,toolbar=no,resizable=yes");
}

function open_tour(URL) {
	popup(URL, "VirtualTour", "height=360,width=512,scrollbars=yes,menubar=no,toolbar=no,resizable=yes");
}

function open_browserUpgrade(URL) {
	popup(URL, "BrowserUpgrade",  "height=360,width=512,scrollbars=yes,menubar=no,toolbar=no,resizable=yes");
}

function open_financialcalculator(URL) {
	popup(URL, "FinancialCalculator",  "height=400,width=485,scrollbars=no,menubar=yes,toolbar=yes,status=yes,resizable=yes");
}

function open_financialcalculator_scrollbars(URL) {
	popup(URL, "FinancialCalculator", "height=400,width=485,scrollbars=yes,menubar=yes,toolbar=yes,status=yes,resizable=yes");
}

function open_financialcalculators(URL) {
	popup(URL, "FinancialCalculator", "height=600,width=620,scrollbars=no,menubar=yes,toolbar=yes,status=yes,resizable=yes");
}

function open_RatePlug(URL) {
	popup(URL, "RatePlug", "height=335,width=600,scrollbars=yes,menubar=no,toolbar=no,status=yes,resizable=yes");
}

function open_bannerAd(URL) {
	popup(URL, "BannerAd", "height=450,width=650,scrollbars=auto,menubar=yes,toolbar=yes,status=yes,resizable=yes");
}

function open_mortgageApp(URL) {
	popup(URL, "MortgageApplication", "height=450,width=650,scrollbars=yes,menubar=yes,toolbar=yes,status=yes,resizable=yes");
}

function open_mortgageApplication() {
	var URL = "/Consumer/FrameSet.aspx?redirectto=/Consumer/Content/Forms/MortgageApplication.aspx";
	popup(URL, "MortgageApplication", "height=450,width=650,scrollbars=yes,menubar=yes,toolbar=yes,status=yes,resizable=yes");
}

function open_oldMortgageCalc() {
	popup('http://www.homeservices.com/TRIOtools/MortgageCalculator/MortgageCalculator.asp', "oldMortgageCalculator", "height=438,width=500,scrollbars=no,menubar=no,toolbar=no,status=no,resizable=no");
}

function Popup_Window(URL) {
	popup(URL, "WindowPreview", "resizable,toolbar,scrollbars,status,height=550,width=800");
}

function PopupMortgage_Window(URL) {
	popup(URL, "WindowPreview", "toolbar=1,resizable=1,scrollbars=1,height=590,width=695");
	SourceWindow.focus(); 
}

function open_ListingSearchHelp(URL) {
	popup(URL,"ListingSearchHelp","resizable,scrollbars,status,height=350,width=300");
}

function open_IntranetLink(URL) {
	popup(URL,"Intranet","resizable,toolbar,menubar,scrollbars,status,height=550,width=600");
}

function openContactMe(URL) {
	popup(URL, "ContactMe", "height=" + getWindowHeight(730) + ",width=600,scrollbars=yes,menubar=no,toolbar=no,status=yes,resizable=yes");
}

function open_ScheduleViewing(URL) {
	popup(URL, "ScheduleViewing", "height=" + getWindowHeight(730) + ",width=600,scrollbars=yes,menubar=no,toolbar=no,status=yes,resizable=yes");
}

function openTheFixUp(URL) {
	// NOTE: this function is currently only being used by CBSHOME, see Left Nav "Home Improvement" - 9/12/02, BKF
	popup(URL, "TheFixUp", "height=" + getWindowHeight(750) + ",width=630,scrollbars=no,menubar=no,toolbar=no,status=yes,resizable=yes");
}

function Popup_Article(nContentID) {
	popup('/Irp/Content/ArticlePopup.asp?ContentID=' + nContentID);
}

function open_map(URL) {
	popup(URL, "ListingMap", "height=600,width=730,scrollbars=yes,menubar=no,toolbar=no,resizable=yes");
}

function open_printableBrochure(URL) {
	popup(URL, "PrintableBrochure", "height=600,width=630,scrollbars=yes,menubar=no,toolbar=no,status=yes,resizable=yes");
}

function open_preferredParters(URL) {
	popup(URL, "PreferredParters", "height=" + getWindowHeight(800) + ",width=615,scrollbars=yes,menubar=yes,toolbar=yes,status=yes,resizable=yes");
}

function open_showingtime(URL) {
	popup(URL, "ShowingTime", "height=" + getWindowHeight(730) + ",width=620,scrollbars=yes,menubar=no,toolbar=no,status=yes,resizable=yes");
}

function open_neighborhood(URL) {
	popup(URL, "Neighborhood", "height=400,width=640,scrollbars=no,menubar=no,toolbar=no,status=yes,resizable=yes");
}
					
function open_homedocs() {
	popup("https://www.homedocs.com/index.htm", "HomeDocs", "height=440,width=600,scrollbars=yes,menubar=yes,toolbar=yes,status=yes,resizable=yes");
}

function open_referencelibrary(URL) {
	popup(URL, "ReferenceLibrary", "height=" + getWindowHeight(680) + ",width=640,scrollbars=yes,menubar=yes,toolbar=yes,status=yes,resizable=yes");
}

function open_ExternalSearch(URL) {
	popup(URL, "TRIOExternalSearch", "height=440,width=600,scrollbars=yes,menubar=yes,toolbar=yes,status=yes,resizable=yes");
}

function openValueSureRequest(URL) {
	popup(URL, "ValueSure", "height=640,width=777,scrollbars=yes,menubar=yes,toolbar=no,status=yes,resizable=yes");
}

// ########## END Popup Functions ############### //

function emailobfuscator(username,domain,ext) {
	location.href = "mail" + "to:" + username + "@" + domain + "." + ext;
}

function rollover(whichimg,overimg) {
	eval('document.images["' + whichimg + '"].src = ' + overimg + '.src');
}

function getWindowHeight(maxHeight) {
	var bufferHeight = 90;
	if (screen.height >= (maxHeight + bufferHeight)) {
		return maxHeight;
	} else {
		return (screen.height - bufferHeight);
	}
}


function fieldLengthLimit(field, maxlimit) {
    maxlimit = maxlimit - 1;
    if (field.value.length > maxlimit) {    // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
    }
}

function validateEmailAddress(address) {
	if (address != '' && address.search) {
      if (address.search(/^\w+((-\w+)|(\.\w+)|(\'\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) return true;
      else return false;
	}
   // allow empty strings to return true - screen these with either a 'required' test or a 'length' test
   else return true;
}

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_changeProp(objName,x,theProp,theValue) { //v6.0
  var obj = MM_findObj(objName);
  if (obj && (theProp.indexOf("style.")==-1 || obj.style)){
    if (theValue == true || theValue == false)
      eval("obj."+theProp+"="+theValue);
    else eval("obj."+theProp+"='"+theValue+"'");
  }
}

function currentlySearchingMenu(target) {
	if (target != "separator") {
		if (target.indexOf("popup") > 0) {
			var myWindow = window.open(target, "TRIOExternalSearch", "height=440,width=600,scrollbars=yes,menubar=yes,toolbar=yes,status=yes,resizable=yes");
			myWindow.focus();
		} else {
			location.href = target;
		}
	}
}

function showHelp(menuName, containerName, show)
{
	if (document.getElementById(menuName) && document.getElementById(containerName)) {
		if (show == 1) {
			//alert('will display menu: ' + menuName)
			document.getElementById(menuName).style.top = document.getElementById(containerName).style.top; 
			document.getElementById(menuName).style.left = document.getElementById(containerName).style.left; 
			document.getElementById(menuName).style.visibility = 'visible';
		}
		else {
			document.getElementById(menuName).style.visibility="hidden";
		}
	}
}

function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function stripCharsNotInBag (s, bag){
  var i;
  var returnString = "";
  for (i = 0; i < s.length; i++){   
    // caracterul nu este spatiu
    var c = s.charAt(i);
    if (bag.indexOf(c) != -1) 
    {
		// remove everything after decimal point
		if( c == "." )
		{
			break;
		}
		else
		{
			returnString += c;
		}
	}
  }
  return returnString;
}

function checkInt (val)
{
    return (!isNaN(val));
}

function checkIntMaxValue (val)
{
	val = stripCharsNotInBag(val, '1234567890.');
	if (val > 2147483647)
	{
		return 2147483647;
	}
	return val;
}

function checkZip (val)
{
	if(val.length > 0) {
		var strippedVal = stripCharsInBag(val, ' ');
		return (strippedVal.length == 5 && checkInt(strippedVal));
	} else {
		return true;
	}
}

function delayPageRefresh(milliSecondWait)
{
	setTimeout("location.reload()", milliSecondWait);
}

function getRandom(num) 
{
	return Math.floor(Math.random() * num) + 1;
}
function getRandomImage(path,imagename,num,ext,background)
{
	if (background == 1)
	{
		return '<td width="100%" background="' + path + imagename + getRandom(num) + '.'+ ext + '"></' + 'td>';
	}
	else
	{
		return '<img src="' + path + imagename + getRandom(num) + '.' + ext + '" border="0"/>';
	}
}

// ########## Cookie Functions ############### //
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
// ########## END Cookie Functions ############### //
