function debug( msg ) {
	if( !$('debug') && $('header') )
		new Insertion.Before($('header'), "<div id='debug'></div>");
	if( $('debug') )
		$('debug').innerHTML += "<br/>" + msg;
}

function confirmdelete(url,msg){
	window.status = 'Delete';
	var del= confirm (msg);

	if(del){
		window.location.href = url;
	}
}	

function PopWin(url, wname, w, h, pl, pt) {
	var winX = pl;
	var winY = pt;
	// Nav 4 gives true screen pos %, while default assumes % on 640X480
	if (parseInt(navigator.appVersion) >= 4) {
		winX = (screen.availWidth - w)*pl*.01;
		winY = (screen.availHeight - h)*pt*.01;
	}

	pWin = window.open(url, wname,'scrollbars=yes,status=no,dependent,resizable=yes,width=' + w + ',height=' + h + ',left=' + winX + ',top=' + winY);
	if (pWin.focus) {
		pWin.focus();
	}
}

/************** Custom Functions **************/
// trim facility for sting

// Validate search - require user to enter in at least 1 field
function searchvalidate(searchtype) {
	var type = searchtype; //1=index, 2=box_search, 3= searchpage
	var phrase = "Please select at least one attribute to search by.";
	if(type == "1" || type == "") {
		if(
			document.searchfrm.sqft.value != "" ||
			document.searchfrm.bed.value != "" ||
			document.searchfrm.bath.value != "" ||
			document.searchfrm.location.value != ""
		)
		{
			return true;
		}
		else {
			alert(phrase);
			return false;
		}
	}
	else if(type == "2") {
		if(
			document.searchfrm.bed.value != "" ||
			document.searchfrm.bath.value != "" ||
			document.searchfrm.location.value != ""		
		)
		{
			return true;
		}
		else {
			alert(phrase);
			return false;		
		}
	}
	if(type == "3") {
		if(
			document.searchfrm.sqft.value != "" ||
			document.searchfrm.minprice.value != "" ||
			document.searchfrm.maxprice.value != "" ||
			document.searchfrm.bed.value != "" ||
			document.searchfrm.bath.value != "" ||
			document.searchfrm.location.value != ""
		)
		{
			return true;
		}
		else {
			alert(phrase);
			return false;
		}
	}	
}


String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function format(num) {
	if(num.length >= 2)	return num;
	return (num < 10) ? '0' + num : num; 
}

function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "; path=/") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(ckName) 
{ 
	var search = ckName + "=";
	var ckValue = "";
	if (document.cookie.length > 0) 
	{
		offset = document.cookie.indexOf(search);
		if (offset != -1) 
		{ 
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1) 
				end = document.cookie.length;
			ckValue = unescape(document.cookie.substring(offset, end));
		}
	}
	return ckValue;
}

function clearCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "; path=/") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


function reportError(req) {
	alert("Sorry. There was an error.");
}

// Given  : key is a string of the 26 letters in arbitrary order,
//          message is the string to be encoded using the key
// Returns: the coded version of message using the substitution key 
function Encode(message, key) {
	var alphabet, coded, i, ch, index;
	
	alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-";
	if( !key )
		key = "DXBqMdtfaxkFW7u5cpuq2JPyGyKcmqTT5XvfxqkSQCwxsdxD2MJjqye76NUb9rB";

	coded = "";                                      
	for (i = 0; i < message.length; i++) {        // for as many letters as there are
		ch = message.charAt(i);                   //   access the letter in the message
		index = alphabet.indexOf(ch);             //   find its position in alphabet
		if (index == -1) {                        //   if it's not a letter,
			coded = coded + ch;                   //     then leave it as is & add
		}                                         //   otherwise,
		else {                                    //     find the corresponding
			coded = coded + key.charAt(index);    //     letter in the key & add
		}
	}

	return coded;
}

// Bookmark the page ( only works in IE & firefox )
function bookmark(url, title){
	if( !title )
		title = document.title;
	if( !url )
		url = document.location.href;
		
	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar)
		window.sidebar.addPanel(title, url, "")
}

// Returns array with page width, height and window width, height
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}
	
// Hightlight listtable rows on mouseover & click.. Hightlight form elements on mouseover & focus
function hightlight_effects( ) {
	$A(document.getElementsByTagName("table")).findAll( function(tobj) { return ( tobj && tobj.className == 'listtable' ); } ).each( function(tableobj) {
		$A(tableobj.getElementsByTagName("tr")).each( function(elm) {
			Event.observe(elm, 'mouseover', function(ev) {elm.addClassName('hover');})
			Event.observe(elm, 'mouseout', function(ev) {elm.removeClassName('hover');})
			Event.observe(elm, 'click', function(ev) { elm.hasClassName('focus') ? elm.removeClassName('focus') : elm.addClassName('focus');})
		} );
	});
	
	$A(document.forms).findAll( function(fobj) { return ( fobj && fobj.hasClassName('form') ); } ).each( function(frmobj) {
		var hcls = "";
		var fcls = "";
		Form.getElements(frmobj).collect( function(elm) { 
			if( elm.type == 'textarea' ) {
				hcls = "textarea-hover";
				fcls = "textarea-focus";
			}
			else if( elm.type == 'submit' || elm.type == 'reset' || elm.type == 'button' ) {
				hcls = "";
				fcls = "";
			}
			else {
				hcls = "input-hover";
				fcls = "input-focus";
			}
			
			if( hcls && fcls ) {
				Event.observe(elm, 'mouseover', function(ev) {elm.addClassName(hcls);})
				Event.observe(elm, 'mouseout', function(ev) {elm.removeClassName(hcls);})
				Event.observe(elm, 'focus', function(ev) {elm.addClassName(fcls);})
				Event.observe(elm, 'blur', function(ev) {elm.removeClassName(fcls);})
			}
		} );
	});
}


/***** Load & Unload calls *****/
Event.observe(window,'load',function(){ 
	if( $('p7PMnav') )
		P7_initPM(0,10,1,-20,10);
	
	if( $('gmap') )
		Gload();
},false);

Event.observe(window,'unload',function(){ 
	if( $('gmap') )
		GUnload();
},false);

function changeImg(imgsrc, alt) {
	document.getElementById("planimg").innerHTML = '<img src="' + imgsrc + '" alt="' + alt + '" />';
}

function showfeature(cfid) {
	var cf = $('cf');

	$A(cf.getElementsByTagName("ul")).findAll( function(obj) { return ( obj && obj.className == 'commfeat' ); } ).each( function(divobj) {
		Element.hide( divobj );
		$(divobj.id.replace(/cf/g, 'l')).removeClassName('down');
	} );
	if( $('cf'+cfid) ) {
		Element.show( $('cf'+cfid) );
		$('l'+cfid).addClassName('down');
	}
}
