// Common javascript library

// get out of any frames
if(location.href.toLowerCase().indexOf('search.beverly-hanks.com')==-1){
	if (parent.frames.length > 0) {
		parent.location.href = location.href
	}
}

/***********************************************
* Bookmark site script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

function bookmarkSite(title,url){
if (window.sidebar) // firefox
	window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
	var elem = document.createElement('a');
	elem.setAttribute('href',url);
	elem.setAttribute('title',title);
	elem.setAttribute('rel','sidebar');
	elem.click();
} 
else if(document.all)// ie
	window.external.AddFavorite(url, title);
}

/***********************************************
* Show Hint script- © Dynamic Drive (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit http://www.dynamicdrive.com/ for this script and 100s more.
***********************************************/
		
var horizontal_offset="10px" //horizontal offset of hint box from anchor link

/////No further editting needed

var vertical_offset="0" //horizontal offset of hint box from anchor link. No need to change.
var ie=document.all
var ns6=document.getElementById&&!document.all

function getposOffset(what, offsettype) {
	var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
	var parentEl=what.offsetParent;
	while (parentEl!=null){
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
}

function iecompattest(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function clearbrowseredge(obj, whichedge){
	var edgeoffset=(whichedge=="rightedge")? parseInt(horizontal_offset)*-1 : parseInt(vertical_offset)*-1
	if (whichedge=="rightedge"){
		var windowedge=ie && !window.opera? iecompattest().scrollLeft+iecompattest().clientWidth-30 : window.pageXOffset+window.innerWidth-40
		dropmenuobj.contentmeasure=dropmenuobj.offsetWidth
		if (windowedge-dropmenuobj.x < dropmenuobj.contentmeasure)
			edgeoffset=dropmenuobj.contentmeasure+obj.offsetWidth+parseInt(horizontal_offset)
	} else {
		var windowedge=ie && !window.opera? iecompattest().scrollTop+iecompattest().clientHeight-15 : window.pageYOffset+window.innerHeight-18
		dropmenuobj.contentmeasure=dropmenuobj.offsetHeight
		if (windowedge-dropmenuobj.y < dropmenuobj.contentmeasure)
			edgeoffset=dropmenuobj.contentmeasure-obj.offsetHeight
	}
	return edgeoffset
}

function showHint(menucontents, obj) {
	if ((ie||ns6) && document.getElementById("hintbox")){
		dropmenuobj=document.getElementById("hintbox")
		dropmenuobj.innerHTML=menucontents
		dropmenuobj.style.left=dropmenuobj.style.top=-500
		dropmenuobj.x=getposOffset(obj, "left")
		dropmenuobj.y=getposOffset(obj, "top")
		dropmenuobj.style.left=dropmenuobj.x-clearbrowseredge(obj, "rightedge")+obj.offsetWidth+"px"
		dropmenuobj.style.top=dropmenuobj.y-clearbrowseredge(obj, "bottomedge")+"px"
		dropmenuobj.style.visibility="visible"
		obj.onmouseout=HideTip
	}
}

function HideTip(e){
	dropmenuobj.style.visibility="hidden"
	dropmenuobj.style.left="-500px"
}

function CreateHintBox(){
	var divblock=document.createElement("div")
	divblock.setAttribute("id", "hintbox")
	document.body.appendChild(divblock)
}

if (window.addEventListener) {
	window.addEventListener("load", CreateHintBox, false)
} else if (window.attachEvent) {
	window.attachEvent("onload", CreateHintBox)
} else if (document.getElementById) {
	window.onload=CreateHintBox
}

///////////////////////////////////////////////////////
//
// Agent directory support code

function setMultipleElement(frm, el, values) {
        if(values) {
          var newvalues = values.split(',');
          if (newvalues.length > 1) {
            for (i=0;i<newvalues.length;i++) {
                var e = document.createElement('input');
		e.setAttribute('type', 'hidden');
		e.setAttribute('name', el);
		e.setAttribute('value', newvalues[i]);
                frm.appendChild(e);
            }
          } else {
            //searchform.elements['officecode[]'].value = officecode;
                var e = document.createElement('input');
		e.setAttribute('type', 'hidden');
		e.setAttribute('name', el);
		e.setAttribute('value', values);
                frm.appendChild(e);
          }
	  
        }
}

// cdh 12/12/06: added office code, but at the end so that it can
//  be optional
function getMyListings(formname, agentcode, firmcode, officecode ) {
	// requires a form someplace on the page with the appropriate
	// fields, etc.
	var searchform = document.forms[formname];
	//searchform.agentcode.value = agentcode;

	// the officecode data used to be pretty bad, but it looks fairly
    // solid now, and the fact that MLS is duplicating agent codes between
   // offices means we pretty much have to start using office codes.
//	var vars = new Array('officecode', 'agentcode');
//	for ( var el in vars ) {
	   //var m_el = vars[el];
	   //setMultipleElement(searchform, m_el, eval(vars[el]));
//	}

	
	document.getElementById('infoarray_firmcode').value = firmcode;
	document.getElementById('infoarray_officecode').value = officecode;
	document.getElementById('infoarray_agentcode').value = agentcode;

	searchform.submit();
}

////////////////////////////////////////////////////////
// Utility references

function GetHeightWidth(height, width){
    
    var xposition=0, yposition=0, text;

    xposition = (screen.width - width) / 2;
    yposition = (screen.height - height) / 2;
    
    text = "height=" + height + ",width=" + width + ",screenx=" + xposition + ",screeny=" + yposition + ",left=" + xposition + ",top=" + yposition;
                    
    return(text);
}
                

function stdpop(title, width, height, url, scrollbars) {
	if (scrollbars==null) {
		scrollbars =0;
	}
    var dim = GetHeightWidth(height, width);
    var props = dim + ',location=0,menubar=0,resizable=0,scrollbars='+scrollbars+',status=0,titlebar=0,toolbar=0,hotkeys=0';
    window.open(url, '', props);
}

function mortgagecalc(price) {
    var url = '/dyn/propertysearch/mortcalc.php?price=' + price;
    stdpop('Calculator', 350, 395, url);
}

function emaillisting(mlsid) {
    var url = '/dyn/propertysearch/maillisting.php?id=' + mlsid;
    stdpop('Email', 400, 370, url);
}

function validateEmail(str) {
	var at='@';
	var dot='.';
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1){
		return false;
	} else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		return false;
	} else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		return false;
	} else if (str.indexOf(at,(lat+1))!=-1){
		return false;
	} else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		return false;
	} else if (str.indexOf(dot,(lat+2))==-1){
		return false;
	} else if (str.indexOf(" ")!=-1){
		return false;
	}

	return true;					
}

function createMarker(point,name,html,icon) {
	var marker = new GMarker(point, {icon:icon});
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindow(html);
	});
	return marker;
}

function openMarker(marker, html) {
	map.panTo(marker.getPoint());
	marker.openInfoWindow(html);
}

function confirmAction(url, prompt) {
	if (confirm(prompt)) {
		document.location.href = url;
	}
}

function quickSearch() {
	obj = document.getElementById('quick_search_text');
	if (obj.value=='MLS # or Street Name') {
		obj.value='';
	}
}

function leaveQuickSearch() {
	obj = document.getElementById('quick_search_text');
	if (obj.value.length==0) {
		obj.value='MLS # or Street Name';
	}
}


var directories = ["findagent","findoffice"];
var dirtabs = ["tab_findagent","tab_findoffice"];

function showdirectory(dirname) {
	// first make sure all the directories are hidden
	for(i=0; i < directories.length; i++){
		var obj = document.getElementById(directories[i]);
		var objtab = document.getElementById(dirtabs[i]);
		obj.style.display = "none";
		obj.style.display = "none";
		objtab.setAttribute("class", "inactive");
		objtab.setAttribute("className", "inactive");
	}
          
	// show the directory we're interested in
	var infoitem = "find" + dirname;
	var obj = document.getElementById(infoitem);
	obj.style.display = "block";
		
	var infotab = "tab_find" + dirname;
	var objtab = document.getElementById(infotab);
	objtab.setAttribute("class", "active");
	objtab.setAttribute("className", "active");
}
