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;
}

function generateOutput(aConcise) {
  if(aConcise)
    parent.generateConciseOutput();
  else
    parent.generateOutput();
	alert(aConcise);
}


/***** Load & Unload calls *****/
$(document).ready(function(){

	var bgheight = getBgHeight('sub_title');


	$('#hdrh1').animate({
			top: "10px",
			"font-size": "1.8em",
			width: "100%"
		},2200, "linear", function() {
					if($('body').width() > 1060)
						$('#head_special').fadeIn( 1500 );
		  		}
	);
	$('#emerghead').animate({
			"margin-right": "0px"
		},2200
	);
	
	
	
	var isclicked = 0;
	// fade in navbar quickly
	$('#p7PMnav').fadeIn( 800 );

	// fade in the title on load
	$('#sltxt').fadeIn( 1500 );

	//fade page on exit  	/*
	$('a').click(function() {
		var url = $(this).attr("href");
		if(url.indexOf('htm') > 0){
			isclicked = 1;
			$('#homemain').animate({
				opacity: 0.2
				}, 300, "linear", function() {
					window.location = url;
		  		}
			);
			return false;
		}
	});  //*/

	$(window).unload(function () {
		if(isclicked == 1)
			return;
		$('#homemain').animate({
			opacity: 1
		}, 200	);
	});	
	
	/* animate the bg image on a scroll */
	var bpos = 0;
	$(window).scroll(function () {
		if($('#sub_title').css("display") == "none")
			return;			//alert($('#sub_title').css("display"));
		var scto = $(this).scrollTop();
		//$('#crumb').text( scto + " " + bpos + " " + bgheight);
		if (scto < 20 && bpos > 0) {
			bpos = 0;
			$('#sub_title').animate({
				backgroundPosition: "0px 0px",
				opacity: 1
			}, 1200	);
		} else if (scto > 20 && bpos == 0) {
			bpos = scto;
			$('#sub_title').animate({
				backgroundPosition: "0px -"+(bgheight-224)+"px",
				opacity: 0.8
			}, 2200	);
		}
	});
//*/

});


function getBgHeight(theid) {
        var height = 0;
        var path = $('#'+theid).css('background-image').replace('url', '').replace('(', '').replace(')', '').replace('"', '').replace('"', '');
        var tempImg = '<img id="tempImg" src="' + path + '"/>';
        $('body').append(tempImg); // add to DOM before </body>
        $('#tempImg').hide(); //hide image
        height = $('#tempImg').height(); //get height
        $('#tempImg').remove(); //remove from DOM
        return height;
    };
	
	

