<!-- This can prototype allows you to perform a trim on any var -->
<!-- so if you have                 -->
<!-- var x=" Hello World ";         -->
<!-- x.trim();                      -->
<!--                                -->
<!-- So not x="Hello World";        -->
String.prototype.trim = function()
{
    return( this.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') );
}


<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->
<!-- Original:  jgw (jgwang@csua.berkeley.edu ) -->
<!-- Web Site:  http://www.csua.berkeley.edu/~jgwang/ -->
<!-- Begin
function checkCapsLock( e ) {
	var myKeyCode=0;
	var myShiftKey=false;
	var myMsg='Caps Lock is On.\n\nTo prevent entering your password incorrectly,\nyou should press Caps Lock to turn it off.';

	// Internet Explorer 4+
	if ( document.all ) {
		myKeyCode=e.keyCode;
		myShiftKey=e.shiftKey;

	// Netscape 4
	} else if ( document.layers ) {
		myKeyCode=e.which;
		myShiftKey=( myKeyCode == 16 ) ? true : false;

	// Netscape 6
	} else if ( document.getElementById ) {
		myKeyCode=e.which;
		myShiftKey=( myKeyCode == 16 ) ? true : false;

	}

	// Upper case letters are seen without depressing the Shift key, therefore Caps Lock is on
	if ( ( myKeyCode >= 65 && myKeyCode <= 90 ) && !myShiftKey ) {
		alert( myMsg );

	// Lower case letters are seen while depressing the Shift key, therefore Caps Lock is on
	} else if ( ( myKeyCode >= 97 && myKeyCode <= 122 ) && myShiftKey ) {
		alert( myMsg );

	}
}
//  End -->

<!--

function expand(id){
	if (document.getElementById(id).style.display != "none"){
		document.getElementById(id).style.display = "none"
		return;
	}

	if (document.getElementById(id).style.display = "none"){
		document.getElementById(id).style.display = "";
	}
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		plus_arrows_01_down = newImage("images/plus_arrows_01-down.gif");
		plus_arrows_02_down = newImage("images/plus_arrows_02-down.gif");
		show = newImage("images/hideButton.gif");
		hide = newImage("images/showButton.gif");
		plus_arrows_01_down = newImage("images/minus_arrows_01-down.gif");
		plus_arrows_02_down = newImage("images/minus_arrows_02-down.gif");
		preloadFlag = true;
	}
}


function AddTownsByName(){
	for(var i = 0; i < AddTownsByName.arguments.length; ++i) {
		AddTownByName(AddTownsByName.arguments[i], document.f1.TownList)
	}
}


function AddTownByName(townName, to){

	if (to.disabled == true){
		return;
	}

	if(to.options.length>=100){
		alert("Sorry, you may only select 100 towns to search in.\nIf you wish to search for more, remove one of your selected towns\nor clear your selection to search the entire database.");
		return;
	}

	var bMoveIt = true;
	for ( j=0; j<to.options.length; j++ ) {
		if (to.options[j].text == townName){
			bMoveIt = false;
			break;
		}
	}
	
	if (true == bMoveIt)	{
		to.options[to.options.length] = new Option( townName, townName, false, false);
		document.f1.save.disabled=false;
	}
	
}


// Functions for UI list minipulation ////////////////////////////////////////////////////////////////
function AddTowns(from,to)
{
	if (to.disabled == true)
		return;
	if(to.options.length>=100)
	{
		alert("Sorry, you have selected more than 100 towns to search in.\nIf you wish to search for more, remove one of your selected towns\nor clear your selection to search the entire database.");
		return;
	}
	for (i=0; i<from.options.length; i++)
	{
		if (from.options[i].selected == true)
		{
			var bMoveIt = true;
			if(from.options[i].text == 'Your MLS(s) have no listings in this area.' ||
			   from.options[i].text == 'Contact agent about listings in this area.'){
				bMoveIt = false;
				break;
			}

			for (j=0; j<to.options.length; j++)
			{
				if (to.options[j].text == from.options[i].text)
				{
					bMoveIt = false;
					break;
				}
			}

			if (true == bMoveIt)
			{
				if(to.options.length>=100)
				{
					alert("Sorry, you have selected more than 100 towns to search in.\nIf you wish to search for more, remove one of your selected towns\nor clear your selection to search the entire database.");
					break;
				}
				var o = from.options[i];
				to.options[to.options.length] = new Option( o.text, o.value, false, false);
			}
		}
	}
}

function DelItems(alist)
{
	for (i=alist.options.length; i>=0; i--)
	{

		if (alist.options[i] != null)
		{
			if (alist.options[i].selected == true)
			{
				if (alist.options[i].value != "spacer")
				{
					alist.options[i] = null;
				}
			}
		}
	}
}

<!-- Replace all spaces with non-breaking spaces -->
function nbsp (strin) {
  var strout = "";
  var intag = false;
  var len = strin.length;
  for(var i=0, j=0; i < len; i++) {
    var ch = strin.charAt(i);
    if (ch == "<")
      intag = true;
    else if (ch == ">")
      intag = false;
    else if (ch == " " && !intag) {
      strout += strin.substring(j,i) + "&nbsp";
      j = i + 1;
    }
  }
  return strout + strin.substring(j,len);
}

function mainSelectPage( page )
{
    // Set the curPage to page
    document.f1.curPage.value = page;
    document.f1.submit();
}

function mainPreviousPage( )
{
    // Here we need to know if this is the first
    // page in the set then we need to do a
    // previousPageSet.
    var startPg = document.f1.startPage.value;
    var curPg = document.f1.curPage.value;

    if( curPg == startPg ) {
        // We need the previous set
        previousPageSet();
    } else {
        // Set the curPage to page
        document.f1.curPage.value = parseInt(curPg) - 1;
        document.f1.submit();
    }
}

function mainNextPage( )
{
    // Here we need to know if this is the last
    // page in the set.  If it is then we need to
    // do a nextPageSet.
    var startPg = document.f1.startPage.value;
    var curPg = document.f1.curPage.value;

    var lastPage = parseInt(startPg) + 9;

    if( curPg == lastPage ) {
        // We need the next set of pages
        nextPageSet();
    } else {
        // Set the curPage to page
        document.f1.curPage.value = parseInt(curPg) + 1;
        document.f1.submit();
    }
}



function mainPreviousPageSet()
{
    numMatches = document.f1.numMatches.value;
    pageSize = document.f1.pageSize.value;
    startPage = document.f1.startPage.value;
    curPage = document.f1.curPage.value;

    if( (parseInt(startPage) - 10) > 1 ){
        newStartPage = parseInt(startPage) - 10;
    } else {
        newStartPage = 1;
    }
    document.f1.curPage.value = newStartPage;
    document.f1.startPage.value = newStartPage;
    document.f1.submit();
}

function mainNextPageSet()
{

    numMatches = document.f1.numMatches.value;
    pageSize = document.f1.pageSize.value;
    startPage = document.f1.startPage.value;
    curPage = document.f1.curPage.value;

    // Here we need to check that if we add 10 to
    // our start page we are not left with a start page
    // that is outside the valid range.
    maxPages = Math.ceil(parseFloat(numMatches) / parseFloat(pageSize));

    if( (parseInt(startPage) + 9) < maxPages ){
        newStartPage = parseInt(startPage) + 10;
    } else {
        newStartPage = parseInt(maxPages) - 10;
    }
    document.f1.curPage.value = newStartPage;
    document.f1.startPage.value = newStartPage;
    document.f1.submit();
}

/*
// Validate that all of the characters in the text input are valid for a price
// field and return the value with commas (,), dollar signs ($), and spaces
// removed.  If the value is not valid for a text input then "invalid" is
// returned.
*/
function checkPriceField(thisone){
	szBadChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!`~@#%^&*()_-+={}[]\\|:;'<>/?\"";
	szRemoveChars = "$, ";

	var val = thisone.value

	// Check for characters that can't be in a price
	for (i=0;i < szBadChars.length;i++)	{
		badChar = szBadChars.charAt(i);
		if (val.indexOf(badChar) != -1)		{
			return "invalid";
		}
	}

	// Check for too many digits after the decimal
	dotIndex = val.indexOf(".");
	if (dotIndex != -1 && dotIndex < (val.length - 3)) {
		return "invalid";
	}

	// Take out instances of $ and comma and space
	szNum = "";
	for (i=0;i < val.length;i++)	{
		currentChar = val.charAt(i);
		if (szRemoveChars.indexOf(currentChar) == -1)		{
			szNum += val.charAt(i);
		}
	}

	return szNum;
}

/*
// Validate that the value in the text input is a valid percentage.  It must
// be a number 0 through 100.  If the input is blank, 0 is returned.  If the
// value is invalid, "invalid" is returned.
*/
function checkPercentField(thisone){
	val = 0;

	val = checkDecimalField(thisone);
	if (val != null && val != "invalid" && val.length > 0) {
		if (val < 0 || val > 100) {
			val = "invalid";
		}
	}

	return val;
}

/*
// Validate that the value in the text input is a valid integer.  If the input
// is blank, 0 is returned.  If the value is invalid, "invalid" is returned.
*/
function checkIntField(thisone){
	intVal = 0;

	if (thisone.value.length > 0) {
		intVal = parseInt(thisone.value);
	}

	if (intVal < 0) {
		intVal = "invalid";
	}

	return intVal;
}

/*
// Validate that the value in the text input is a valid decimal.  If the input
// is blank, 0 is returned.  If the value is invalid, "invalid" is returned.
*/
function checkDecimalField(thisone){
	szBadChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!`~@#%^&*()_-+={}[]\\|:;'<>/?\"$, ";

	var val = thisone.value
	for (i=0;i < szBadChars.length;i++)	{
		badChar = szBadChars.charAt(i);
		if (val.indexOf(badChar) != -1)		{
			return "invalid";
		}
	}

	return val;
}


function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num );
}

function unFormatCurrency(val) {
	val = val.replace('$', '').trim();
	val = val.replaceAll(",","");
	return val;
}

function mortgageCalculator() {
	myForm = document.fNavBar;
	myForm.CMD.value = "Mortgage_Calculator";
	myForm.setAttribute('action', '/MyServices');
	myForm.submit();
}

function showMortgageCalc()
{
	myForm = document.f1;
	myForm.CMD.value = "Mortgage_Calculator";
	myForm.setAttribute('action', '/MyServices');
	myForm.submit();
}
 function openPopup(theURL, width, height) {   

	var x = (screen.width - width ) / 2;
	var y = (screen.height - height) / 2;

	window.open( theURL,'', "toolbar=0, location=0, directories=0, status=1, menubar=0, width=" + width + ", height=" + height + ", left=" + x + ", top=" + y + ", resizable=yes, scrollbars=yes" );   
} 

function isValidDate(szCheckDate){
	var CheckDate = new Date(szCheckDate.substring(0,4),szCheckDate.substring(5,7)-1,szCheckDate.substring(8,10))
	var Today = new Date();

	if ((szCheckDate.indexOf("-")!=4) || (szCheckDate.lastIndexOf("-")!=7) || (szCheckDate.length!=10) || (CheckDate=="NaN")) {
		return "Please enter a valid date (YYYY-MM-DD) or \"never\".";
	} else {
		var Year = szCheckDate.substring(0,4);
		var Month = szCheckDate.substring(5,7);
		var Day = szCheckDate.substring(8,10);
		if ((Month<1) || (Month>12))		{
			return "Please enter a valid value for Month (1-12).";
		} else {
			if ((Day<1) || (Day>31)){
				return "Please enter a value value for Date (1-31).";
			} else {
				if (CheckDate>Today){
					return "Please enter a past date (YYYY-MM-DD).";
				} else {
					return true;
				}
			}
		}
	}
}

function encode(str) 
{
	var result = "";
	
	for (i = 0; i < str.length; i++) 
	{
		if (str.charAt(i) == " ") 
		{
			result += "+";
		}
		else 
		{
			result += str.charAt(i);
		}
	}
	
	result = escape(result);
	return result;
}

function decode(str) 
{
	var result = "";
	
	for (i = 0; i< str.length; i++) 
	{
		if (str.charAt(i) == "+") 
		{
			result += " ";
		}
		else 
		{
			result += str.charAt(i);
		}
	}

	result = unescape(result);
	return result;
}

function AddToFavorites(url,title) { 
	// If either are blank, we just use window.location.
	if(null == url || url == "" ){url = window.location.toString();}
	if( null == title || title == ""){
		title = window.location.toString();
	}
	
	// Cross-browser compatible (more or less)
	
	if( window.opera && window.print ) { 
		// we are in opera
		message =  'To bookmark this page in Opera, \n';
		message += 'Type Ctrl-T or click on "bookmarks->Bookmark Page';
		alert(message);
		
	} else if (window.sidebar) { 
		// we are in firefox
		window.sidebar.addPanel(title, url,""); 
		
	} else if( document.all ) { 
		// we are in IE
		window.external.AddFavorite( url, title); 
		
	} else {
		// we don't know
		message =  'Sorry, we are unable to bookmark this page.';
		message += 'If you are using Safari, bookmark us by:';
		message += 'clicking on "Bookmarks -> Add Bookmark". ';
		alert(message);
	}

}

/*
 * Loops through all the values of the f1 form
 * and alerts them. For debugging purposes.
 */
function alertValues(){

	message = "FORM VARIABLES: \n";
	for(i = 0; i < document.f1.length; i++){
		if(document.f1.elements[i].name != 'TownList'){
			message += "\t " + document.f1.elements[i].name + " = " + document.f1.elements[i].value + ". \n";
		}
	}
	alert(message);
}

function goToTop(){
	// try to go to the top of the enclosing page
	// this will fail if we are framed in a site with a different domain 
	// than ours - IE if an agent is framing the IDX search on their own
	// website - but if we are in the CustomWebSite case, we should scroll
	// all of the way to the top of the page
	try{
		try{
			parent.window.scrollTo(0,0);
		} catch(e1) {
			location.href="#";	
		}
	}catch(e2){
		debug(e2, "goToTop()");
	}
}

var CTTWindow = null; //Pop up window
function gotoClickToTalk(targetWin, agtParam, isAgt) {
	var bNew = false;
	var targetPage = "";
	if(null != targetWin && targetWin == "new"){
		bNew = true;
	}
	if(!bNew){
		// If we didn't pass any parameters, just
		// load the page like any other
		document.fNavBar.setAttribute("action", "/MyClickToTalk");
		document.fNavBar.CMD.value = "Call_From_Pulse";
		return fNavBar.submit();
	}else{
		// If we did pass parameters, 
		// we want to pop up the window using the NoLogin Servlet
		targetPage = "/NoLogin?TargetJSP=/MyClickToTalk/PulseClickToTalk.jsp&agt=" + agtParam;
		
		if( isAgt != null && isAgt=='true'){
			targetPage += "&IsAgent=true";
		}

//		<%if(null != szUserType && !szUserType.endsWith("User") && !szUserType.equalsIgnoreCase("Anonymous")){ %>
//			targetPage += "&IsAgent=true";
//		<%}%>


		var width = Math.ceil(screen.width * .7), height = Math.ceil(screen.height * .9);
		var x = Math.ceil((screen.width - width ) / 2), y = Math.ceil((screen.height - height) / 2);
		var openChild = true;
		if (null != CTTWindow){
			if (CTTWindow.closed == false){
				CTTWindow.focus();
				openChild = false;
			}
		}
		if (openChild == true){
			CTTWindow = window.open(targetPage,
						"Help",
						"toolbar=0, location=0, directories=0, status=1, menubar=0, width=" + width + ", height=" + height + ", left=" + x + ", top=" + y + ", resizable=yes, scrollbars=yes");
		}
	}
} // end gotoClickToTalk()


