//***************THE FOLLOWING FUNCTIONS ARE USED THROUGHOUT************
function openWin(url,windowName,options){
	var WindowHandle=window.open(url,windowName,options);
	WindowHandle.focus();
}

var ns6=document.getElementById&&!document.all
var ie=document.all

function showSpan(spanID,thetext){
	if (ie) eval("document.all."+spanID).innerHTML=thetext
	else if (ns6) document.getElementById(spanID).innerHTML=thetext
}
function hideSpan(spanID){
	if (ie) eval("document.all."+spanID).innerHTML=' '
	else if (ns6) document.getElementById(spanID).innerHTML=' '
}

function getElementBy(elemTag){
	var elem = document.getElementById (elemTag);
	if (elem)
		return elem;
	var elems = document.getElementsByName (elemTag);
	if (elems.length > 0)
		return elems[0];
	return null;
}

function toggleOpenCloseElem(elemName){
	var elem = getElementBy (elemName);
	if (!elem)
		return;
	if (elem.style.display == "")
		elem.style.display = "none";
	else
		elem.style.display = "";
}
//***************THE ABOVE FUNCTION IS USED THROUGHOUT************



//***************THE FOLLOWING FUNCTION IS USED FOR FORM VALIDATION************
function confirmPrompt(msg,url){
	if(confirm(msg)) 
		document.location = url;
}

function validateLength(objTB,maxChar){
	if (objTB.value.length > maxChar){return false;}
	return true;
}

function validateEmail(objTB){
	var invalidChars = "*|,\":<> []{}`\';()&$#%";
	if (objTB.value.indexOf('@') < 0 || objTB.value.indexOf('.') < 0 || objTB.value.length < 5){return false;}
	for (var i = 0; i < objTB.value.length; i++){
	   if (invalidChars.indexOf(objTB.value.charAt(i)) != -1){return false;}
	}
	return true;
} 

function validateEmailNonReq(objTB){
	var invalidChars = "*|,\":<> []{}`\';()&$#%";
	if (objTB.value.length > 0){
		if (objTB.value.indexOf('@') < 0 || objTB.value.indexOf('.') < 0 || objTB.value.length < 5){return false;}
		for (var i = 0; i < objTB.value.length; i++){
		   if (invalidChars.indexOf(objTB.value.charAt(i)) != -1){return false;}
		}
		return true;
	}
	return true;
} 

function validateTextBox(objTB){
	if (objTB.value==''){return false;}
	return true;
}

function validateSelectList(objTB){
	if (objTB.selectedIndex==''){return false;}
	return true;
}

function validateNumberTextBoxNonReq(objTB){
	if (isNaN(objTB.value)){return false;}
	return true;
}

function validateNumberTextBox(objTB){
	if (objTB.value=='' || isNaN(objTB.value)){return false;}
	return true;
}

function restrictNumberKeys(e){
		/*
		Description: This function restricts an input box to only accept numerics, dashes,
					 parentheses and spaces.
		  
		Usage:
			<input onKeyPress="return restrictNumberKeys(event);">
			
		Known Browser Support: MSIE 6.0, FireFox 1.0.4, Safari 2.0
		Written By: Chris Pietschmann, MCSD, MCAD
		*/
		var keyCode;
		
		if(window.event) //MSIE
			keyCode = e.keyCode;
		else //FireFox
			keyCode = e.which;

		if(keyCode >= 48 && keyCode <= 57 //Numeric Digits 0-9
			|| (keyCode == 8) //Backspace
			|| (keyCode == 0) //keys like Enter and Delete will return zero
			|| (keyCode == 99 && e.ctrlKey) // Ctrl-C
			|| (keyCode == 118 && e.ctrlKey) // Ctrl-V
			)
			return true;
		else
			return false;
	}
	
	function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
var dtCh="/";
var minYear=1900;
var maxYear=2100;

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

//***************THE ABOVE FUNCTION IS USED FOR FORM VALIDATION************



//***************THE FOLLOWING FUNCTIONS ARE USED BY THE PROPERTY COMPARE FEATURE************
var thecookie = document.cookie;

function getCookie(name) { // use: getCookie("name");
	var index = thecookie.indexOf(name + "=");
	if (index == -1) return null;
	index = thecookie.indexOf("=", index) + 1; // first character
	var endstr = thecookie.indexOf(";", index);
	if (endstr == -1) endstr = thecookie.length; // last character
	return unescape(thecookie.substring(index, endstr));
}

function setCookie(name, value) { // use: setCookie("name", value);
    document.cookie=name + "=" + escape(value) + ";";
}

function commitCompareList(){		
	setCookie("VAR_CompareList",GLOBAL_CompareList);
	setCookie("CompareCount",GLOBAL_CompareCount);
	//
}		

function sendToCompare(){


	if (GLOBAL_CompareCount < 2){
		alert('Please check more than 1 property!');
	}else{
		if (GLOBAL_CompareCount > 10){
			alert('Please check no more than 10 properties!');
		}else{



			window.location = "/property/propertycompare.asp?VAR_CompareList=" + GLOBAL_CompareList + "&CompareCount=" + GLOBAL_CompareCount;
		}
	}
}
		
function compareClear(){
	GLOBAL_CompareList = "";
	GLOBAL_CompareCount = 0;		
	setCookie("VAR_CompareList","");
	setCookie("CompareCount","");
}


function resetCompare(){
	GLOBAL_CompareList = "";
	GLOBAL_CompareCount = 0;		
	setCookie("VAR_CompareList","");
	setCookie("CompareCount","");
	for(i=0; i<document.frmCompare.elements.length; i++)
		{
			if(document.frmCompare.elements[i].type=="checkbox")
		{
			document.frmCompare.elements[i].checked=false;
		}
	}	

}


function addCompare(obj){
	//the maximum number of listings a user can check to compare
	var Max_Number_Properties = 10;
	
	if (obj.checked == true) 
	{
		if (GLOBAL_CompareCount == Max_Number_Properties) {			
			obj.checked = false;
			alert('You have already chosen ' + Max_Number_Properties + ' properties!');
		}
		else {
			GLOBAL_CompareList = GLOBAL_CompareList + obj.value;
			GLOBAL_CompareCount = parseInt(GLOBAL_CompareCount) + 1;
		}
	}	
	else 
	{
		GLOBAL_CompareCount = parseInt(GLOBAL_CompareCount) - 1
		GLOBAL_CompareList = GLOBAL_CompareList.replace(obj.value,"");
	}
}
//***************THE ABOVE FUNCTIONS ARE USED BY THE COMPARE TOOL************


/*

Note that this tries several methods of creating the XmlHttpRequest object,

depending on the browser in use. Also note that as of this writing, the

Opera browser does not support the XmlHttpRequest.

*/

function XMLHTTPRequest_createRequester()
{
      var myRequest;
    try{
        myRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            myRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(oc){
            myRequest = null;
        }
    }
 
    if(!myRequest && typeof XMLHttpRequest != "undefined"){
        myRequest = new XMLHttpRequest();
    }
    return myRequest;
}

//***************THE ABOVE FUNCTION IS USED WITH AJAX************