// <SCRIPT>
//

baseURL	=	"";

function openWindow(pageURL){
	
	var newWin = window.open(pageURL, 'newWin', 'toolbars=no,scrollbars=yes,location=no,status=yes,menubars=no,resizable=yes,width=400,height=350');
	return false;

}


function openWindowWH(pageURL, width, height){
	
	var newWin = window.open(pageURL, 'newWin', 'toolbars=no,scrollbars=yes,location=no,status=yes,menubars=no,resizable=yes,width='+ width +',height='+ height);
	return false;

}

function popupImage(imgUrl){
	width=400;
	height=450;
	window.open(baseURL+ '/asp/popup_image.asp?img=' + imgUrl, 'pop', 'toolbars=no,scrollbars=yes,location=no,status=yes,menubars=no,resizable=yes,width='+ width +',height='+ height +',top=100,left=100');
	return false;
}

function alertWindow(str){
	
	var width	=	300;
	var height	=	250;

	LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-height)/2 : 0;

	if(!str || str == "undefined")
		return;
	var newWin = window.open(baseURL+ "/main/thankyou_popup.asp?message="+ escape(str).replace(/%20/g, "+"), 'newWin', 'toolbars=no,scrollbars=yes,location=no,status=yes,menubars=no,resizable=yes,width='+ width +',height='+ height +',left=' + LeftPosition + ',top=' + TopPosition);
}	

function getbrowserwidth(){
	if (navigator.userAgent.indexOf("MSIE") > 0)
		return(document.body.clientWidth);
	else
		return window.outerWidth;
}

function getbrowserheight(){
	if (navigator.userAgent.indexOf("MSIE") > 0)
		return(document.body.clientHeight);
	else
		return window.outerHeight;
}


function toggleContactForm(strTagId, intUserId, intPropertyId, strAddedQS){
	var t, l;
	strURL	=	document.location.href;
	strURL	=	changeURL(strURL, "div="+ strTagId +"&property_id="+ intPropertyId + ((strAddedQS) ? "&" + strAddedQS : ""));

	if(intUserId > 0){
		if(document.getElementById && document.getElementById(strTagId) != null){
			t	=	document.getElementById(strTagId);
			s	=	document.getElementById('contactAgentTemplate').innerHTML;
			if(strLastTagId)
				l	=	document.getElementById(strLastTagId);
		}else if(document.all){
			t	=	document.all[strTagId];
			s	=	document.all['contactAgentTemplate'].innerHTML;
			if(strLastTagId)
				l	=	document.all[strLastTagId];
		}
		
		if(t){
			if(t.innerHTML == ""){
				if(l)
					l.innerHTML	=	"";
				t.innerHTML	=	s.replace("<!--","").replace("-->","");
				document.contactagentform.div.value	=	strTagId;
				document.contactagentform.property_id.value	=	intPropertyId;
				document.contactagentform.go_url.value	=	strURL;
				
			}else
				t.innerHTML	=	"";
		}
	}else{
		document.location.href = "../asp/validate_redirect.asp?url=" + escape(strURL) +"&failurl="+ escape(changeURL(strURL, "div=")) +"&contactagent=1&property_id="+ intPropertyId + ((strAddedQS) ? "&" + strAddedQS : "");
		return false;
	}
	strLastTagId	=	strTagId;
	
	// RETURN TRUE IF WE NEED TO TARGET THE BROWSER WINDOW TO THE RIGHT SPOT
	// RETURN FALSE OTHERWISE
	
	scrollToProperty(intPropertyId);
	return false;

}

function scrollToProperty(intPropertyId){

	if(!intPropertyId)
		return;

	var AnchorCoords	=	getAnchorPosition("property-"+ intPropertyId);
	if(AnchorCoords)
		window.scrollTo(0, AnchorCoords.y);
}

function scrollToPropertyOnload(){

	if(!intPagePropertyId)
		return;

	var AnchorCoords	=	getAnchorPosition("property-"+ intPagePropertyId);
	if(AnchorCoords)
		window.scrollTo(0, AnchorCoords.y);
}

function toggleVisibility(strTagId, boolFlag) {
	if(document.getElementById && document.getElementById(strTagId) != null)
		s	=	document.getElementById(strTagId).style;
	else if(document.layers && document.layers[strTagId] != null)
		s	=	document.layers[strTagId];
	else if(document.all)
		s	=	document.all[strTagId].style;

	if(boolFlag === false){
		node	=	s.display	=	"none";
	}else if(boolFlag === true){
		node	=	s.display	=	"";
	}else{
		if(s.display == "")
			node	=	s.display	=	"none";
		else
			node	=	s.display	=	"";
	}
}

function anyChecked(formItems, boolFlag){
	if(boolFlag){
		for(var counter=0; counter < formItems.length; counter++){
			formItems[counter].checked = false;
		}
	}
}
function oneChecked(i, boolFlag){
	if(boolFlag){
		i.checked	=	false;
	}
}

// LINK GENERATORS
// str = "";returnstr = "";for(counter=1; counter <= str.length; counter++){returnstr += (str.charCodeAt(counter-1) + (counter + counter/10)) + ",";}returnstr;

function cafemocha(astr, bstr){

	var a	=	astr.split(",");
	var b	=	bstr.split(",");
	
	document.write("<a href=\"");
	sodapop(a);
	document.write("\">");
	sodapop(b);
	document.write("</a>");
}

function sodapop(a){
	var b,c,d;
	
	for(var counter=0; counter < a.length; counter++){
		b	=	a[counter];
		c	=	counter+1;
		
		d	=	Math.round(b - (c + c/10));
		
		document.write(String.fromCharCode(d));
	}
}

// RETURN NULL IF THE STRING'S VALUE IS UNDEFINED, EMPTY, OR NULL
//
function testNullString(str, strAltReturn){
	var str	=	String(str);
	var strAltReturn;
	
	if(strAltReturn == undefined || strAltReturn == null)
		strReturn	=	null;
	else
		strReturn	=	strAltReturn;
	
	if(str == "undefined" || str == "null" || str == "")
		return strReturn;
	else
		return str;	
}

// UPDATES MATH.ROUND TO INCLUDE DECIMAL PLACES
//
function math_round(expression, decimalplaces, isEurope){
	var multiplier;

	if(decimalplaces != null && decimalplaces != 0)
		multiplier = Math.pow(10, decimalplaces);
	else
		multiplier	=	1;

	strExpression	=	String(expression);
	
	if(isEurope){

		if(strExpression.indexOf(".") != -1)
			strExpression	=	strExpression.replace(/\./g, "");
		
		expression	=	strExpression.replace(/,/, ".").replace(/,/g, "");
			
	}else{
		if(strExpression.indexOf(",") != -1)
			expression	=	strExpression.replace(/,/g, "");
	}
	
	if(isNaN(parseFloat(expression)))
		expression	=	0;
	else
		expression	=	parseFloat(expression) * 1;
	
	return Math.round(expression * multiplier)/multiplier;
}

// FORMAT THE NUMBER TO SHOW 2 DIGITS PAST THE DECIMAL PLACE
//
function priceFormat(n, isSplitThousands){
	
	var n,nstr;
	
	n		=	math_round(n,2)+.001;
	nstr	=	String(n);
	nstr	=	nstr.substring(0, nstr.indexOf(".") + 3);
	
	if(!isSplitThousands)
		return nstr;
	else
		return (splitThousands(nstr, ",") + jsRight(nstr, 3));
		
}

	// FORMAT INPUT NUMBER USING SEPARATOR TO MARK THOUSANDS
	//
	function splitThousands(input, strSeparator, numDigitsAfterDecimal, incLeadingDig, parenForNegatives){
		var retVal = numberFormat(input, strSeparator, numDigitsAfterDecimal, incLeadingDig, parenForNegatives);
		return testNullString(retVal, "");
	}
	function numberFormat(origInput, strSeparator, numDigitsAfterDecimal, incLeadingDig, parenForNegatives) {

		if(origInput){
			
			//european assumption -- that is, if the thousands separator is a period, the decimal sep will be a comma
			var decSeparator ="."
			if(strSeparator == "."){
				decSeparator	=	",";
				
				var input		=	testNullString(origInput, "0");
				var intPart		=	numDigitsAfterDecimal ? origInput.replace(/,\d*$/, "").replace(/\D/g, "") : Math.abs(math_round(origInput, 0, true));
				var decimalPart	=	numDigitsAfterDecimal ? origInput.replace(/^.*,/, "").replace(/\D/g, "") : "";//make it a number like 0.232
				var output		=	"";
			
			}else{
				strSeparator	=	",";
				
				var input		=	testNullString(origInput, "0");
				var intPart		=	numDigitsAfterDecimal ? origInput.replace(/\.\d*$/, "").replace(/\D/g, "") : Math.abs(math_round(origInput));
				var decimalPart	=	numDigitsAfterDecimal ? origInput.replace(/^.*\./, "").replace(/\D/g, "") : "";//make it a number like 0.232
				var output		=	"";
			}
			
			if(!math_round(intPart)){
				if(!incLeadingDig)
					intPart = "";
				else
					intPart = "0";
			}else{
				
				intPart = String(intPart);

				for(var counter = intPart.length; counter > 0; counter--){
					output = ((counter != 1 && counter != intPart.length + 1 && (intPart.length - counter + 1) % 3 == 0) ? strSeparator : "" ) + intPart.charAt(counter - 1) + output;
				}
			}
			
			if (numDigitsAfterDecimal){
				if(math_round(decimalPart))
					decimalPart = math_round(decimalPart * Math.pow(10, numDigitsAfterDecimal - String(decimalPart).length));
				else
					decimalPart = String(Math.pow(10, numDigitsAfterDecimal)).substring(1);
				
				output += decSeparator + decimalPart;
			}
			
			//handle how negative numbers are displayed
			if (testNullString(origInput,"").search(/^-/) != -1 && output){
				if(parenForNegatives)
					output = "("+(output.replace(/-/,""))+")";	
				else
					output = "-"+output;//add negative sign back if necessary	
			}
				
			return output;
		}
		return;
	}

function changeURL(strURL, strQueryStringChanges){

		var strItem, strItemValue, reItemValue, strHash;
		var strURL					=	strURL;
		var isQueryString			=	(strURL.indexOf("?") != -1);
		if(strURL.indexOf("#") != -1){
			strHash	=	strURL.substring(strURL.indexOf("#"));
			strURL	=	strURL.substring(0,strURL.indexOf("#"));
		}
		if(strQueryStringChanges.indexOf("#") != -1){
			strHash						=	strQueryStringChanges.substring(strQueryStringChanges.indexOf("#"));
			strQueryStringChanges	=	strQueryStringChanges.substring(0,strQueryStringChanges.indexOf("#"));
		}
		var aQueryStringChanges	=	strQueryStringChanges.split("&");
		
		// CHECK FOR EXISTING QUERYSTRING
		if(isQueryString){
			var strScriptName		=	strURL.substring(0, strURL.indexOf("?"));
			var strQueryString	=	strURL.substring(strURL.indexOf("?"));
		}else{
			var strScriptName		=	strURL;
			var strQueryString	=	"";
		}
		
		for(var counter=0; counter < aQueryStringChanges.length; counter++){
			strItemValue	=	String(aQueryStringChanges[counter]);
			strItem			=	strItemValue.substring(0, strItemValue.indexOf("="));
			reItemValue		=	new RegExp("([\?&])"+ strItem +"=[^&]*", "i");
			
			// CHECK FOR BLANK VALUES
			if(strItemValue.indexOf("=") + 1 == strItemValue.length)
				strItemValue	=	"";

			if(strQueryString.search(reItemValue) != -1){
		
				if(strItemValue.indexOf("order=") != -1 && strQueryString.indexOf(strItemValue.replace(/\*/g,"")) != -1){
					if(strItemValue.indexOf("*ASC*") != -1)
						strItemValue	=	strItemValue.replace(/\*ASC\*/gi, "DESC");
					else if(strItemValue.indexOf("*DESC*") != -1)
						strItemValue	=	strItemValue.replace(/\*DESC\*/gi, "ASC");
				}
				strQueryString		=	strQueryString.replace(reItemValue, "$1"+ strItemValue);
			}else if(strItemValue != "" && !isQueryString){
				strQueryString		+=	String("?" + strItemValue);
				isQueryString		=	true;
			}else if(strItemValue != "" && isQueryString)
				strQueryString		+=	String("&" + strItemValue);
		}
		
		// REPLACE TRAILING FIGURES, AND DOUBLE FIGURES
		strQueryString	=	strQueryString.replace(/\*ASC\*/g, "ASC");
		strQueryString	=	strQueryString.replace(/\*DESC\*/g, "DESC");
		strQueryString	=	strQueryString.replace(/^\?&+/, "?");
		strQueryString	=	strQueryString.replace(/&{2,}/g, "&");
		strQueryString	=	strQueryString.replace(/(?:&|\?)$/, "");
		
		strURL	=	strScriptName.concat(strQueryString);
		
		if(strHash){
			strURL	=	changeURL(changeURL(strURL, "1="), "1=1").concat(strHash);
		}
		
		return strURL;
	}

//
// </SCRIPT>
