function getObj(id){
	return document.getElementById(id);
}

//menu fuct	
var cur_opened_menu = false;
var close_menu_timeout = false;
function openMenu(parent, to_open, pr_top){
	var o = document.getElementById(to_open);
	var po = document.getElementById(parent);
	if(po.menuOpenedChild){
		po.menuOpenedChild.style.display = "none";
	}
	po.menuOpenedChild = o;
	o.menuParentEl = po;
	o.menuActiveElement = true;
	o.style.display = "block";
	if(pr_top){
		if(cur_opened_menu != parent && cur_opened_menu){
			closeAll(document.getElementById(cur_opened_menu));
			document.getElementById(cur_opened_menu).menuActiveElement = true;
			closeMenuAct(false);
		}
		po.topel = true;
		cur_opened_menu = parent;
		document.getElementById(cur_opened_menu).menuActiveElement = true;
	}
}
function closeAll(o){
	if(o.menuOpenedChild){
		closeAll(o.menuOpenedChild);
	}
	o.menuActiveElement = false;
}

function closeMenu(close){
	document.getElementById(close).menuActiveElement = false;
	document.getElementById(close).menuParentEl.menuActiveElement = false;
	setTimeout("closeMenuAct(false)", 2500);
}

function closeMenuAct(pr_el){
	if(!pr_el){
		pr_el = document.getElementById(cur_opened_menu);
	}
        if (null != pr_el && undefined != pr_el) {
	if(pr_el.menuOpenedChild){
		var close = closeMenuAct(pr_el.menuOpenedChild);
		if(close){
			pr_el.menuOpenedChild = false;
		}else{
			pr_el.menuActiveElement = true;	
		}
	}
	if(!pr_el.menuActiveElement && !pr_el.topel){
		pr_el.style.display = "none";
		return true;
	}else{
		return false;
	}	
        }
}

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);
}
//----------------------------------------------------------------------------
// by Yarosh A. <testtset@gmail.com>
// add and show loader indicator from 'loader_image' <div> to 'div_id' element
// return added element ID
function show_loader_indicator(div_id, pr_loader){
    var dv = document.getElementById("global_loader");
    dv.style.display = 'block';
	get_Position();
    return "global_loader";
        }
isDOM=document.getElementById //DOM1 browser (MSIE 5+, Netscape 6, Opera 5+)
isOpera=isOpera5=window.opera && isDOM //Opera 5+
isOpera6=isOpera && window.print //Opera 6+
isOpera7=isOpera && document.readyState //Opera 7+
isMSIE=document.all && document.all.item && !isOpera //Microsoft Internet Explorer 4+
isMSIE5=isDOM && isMSIE //MSIE 5+
isNetscape4=document.layers //Netscape 4.*
isMozilla=isDOM && navigator.appName=="Netscape" //Mozilla ??? Netscape 6.*

var mousex = 0
var mousey = 0
function get_Position(){
	if(isNetscape4) document.captureEvents(Event.MOUSEMOVE)
	if(isMSIE || isOpera7){
	  document.onmousemove=function(){
		mousex=event.clientX+document.body.scrollLeft;
		mousey=event.clientY+document.body.scrollTop;
		showLoader();
		return true
        }
	}else if(isOpera){
	  document.onmousemove=function(){
		mousex=event.clientX;
		mousey=event.clientY;
		showLoader();
		return true
    }
	}else if(isNetscape4 || isMozilla){
	  document.onmousemove=function(e){
		mousex = e.pageX;
		mousey = e.pageY;
		showLoader();
		return true
	     }
	     }
    }
function showLoader(){
	obj_d=document.getElementById('global_loader');
	var x = mousey+25;
	obj_d.style.top= x + "px";
	x = mousex-10;
	obj_d.style.left=x + "px";
}

function ajaxData(url,data_to_send,function_on_complite){
    var req = new JsHttpRequest();
    // Code automatically called on load finishing.
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            eval(function_on_complite);
        }
    }
    // Prepare request object (automatically choose GET or POST).
    var href = document.location.href;
	if(href.indexOf("https:")!=-1){
		url = url.replace("http://www.fizber.com", "");
		url = url.replace("https://www.fizber.com", "");
		url = "https://www.fizber.com" + url;
	}
    req.open("POST", url, true);
    // Send data to backend.
    req.send( data_to_send );
}


function ajaxDataScript(url,data_to_send,function_on_complite){
    var req = new JsHttpRequest();
    // Code automatically called on load finishing.
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
            eval(function_on_complite);
        }
    }
    // Prepare request object (automatically choose GET or POST).
    var href = document.location.href;
	if(href.indexOf("https:")!=-1){
		url = url.replace("http://www.fizber.com", "");
		url = url.replace("https://www.fizber.com", "");
		url = "https://www.fizber.com" + url;
	}
	req.loader = 'SCRIPT';
    req.open("GET", url, true);
    // Send data to backend.
    req.send( data_to_send );
}


function showHideObj(obj_id){
    var div = document.getElementById(obj_id);
    if(!div){
    	return;
    }
    if(div.style.display == "none"){
        div.style.display = "block";
    }else{
        div.style.display = "none";
    }
}

var qrtimeout = false;
var div_opened = false;
function quickResultCount(pr_submit){
	try{
		clearTimeout(qrtimeout);
		if(div_opened){
			document.getElementById('quick_res_count').style.display = 'block';
		}
	}
	catch(e){}
	if(pr_submit){
		eval("quickResultCount__2(" + pr_submit + ")");
	}else{
		qrtimeout = setTimeout("quickResultCount__2(" + pr_submit + ")", 700);
		if(document.getElementById('quick_res_count').style.display == 'block'){
			div_opened = true;
		}else{
			div_opened = false;
		}
	}
	document.getElementById('quick_res_count').style.display = 'none';
	return false;
}
function quickResultCount__2(pr_submit){
	var csz = document.getElementById('csz');
	if(csz.value == "City, State OR ZIP"){
		csz.value = "";
	}
	if(csz.value <2 ){
		if(pr_submit){
		var o = document.getElementById('quick_res_count');
		o.style.display = "block";
		o.innerHTML = "<span style='color:red'>Please enter city, state or zip</span>";
		}
		return;
	}
    sendQuickResultRequest(csz.value, pr_submit);
}
function sendQuickResultRequest(csz, pr_submit){
	var search_params = {csz: csz};
	var fld_arr = ['prices_range', 'beds_bath', 'min_price', 'max_price', 'property_type',
					'sqft_value_min', 'sqft_value_max', 'price_value_max', 'price_value_min', 
					'with_photos', 'fsbo_agent','sell_sold']
	var c = fld_arr.length;
	var f1;
	for(var i=0;i<c;i++){
		f1 = document.getElementById(fld_arr[i]);
		if(f1){
			if (f1.type == "checkbox") {
				if (f1.checked) {
					search_params[fld_arr[i]] = f1.value;
				}
			}else {
				search_params[fld_arr[i]] = f1.value;
			}
		}
	}
	beds_value = new Array();
	baths_value = new Array();
	prop_type_ch = new Array();
	for(var i=1;i<5;i++){
		f1 = getObj("bt" + i);
		if (f1) {
			if(f1.checked){
				baths_value[baths_value.length] = f1.value;
			}
		}
		f1 = getObj("bd" + i);
		if (f1) {
			if(f1.checked){
				beds_value[beds_value.length] = f1.value;
			}
		}
	}
	for(var i=0;i<15;i++){
		f1 = getObj("ptp" + i);
		if (f1) {
			if(f1.checked){
				prop_type_ch[prop_type_ch.length] = f1.value;
			}
		}
	}
	search_params['beds_value'] = beds_value.join(",");
	search_params['baths_value'] = baths_value.join(",");
	search_params['prop_type_ch'] = prop_type_ch.join(",");
    ajaxDataScript("/get_quick_houses_search_res.html",search_params,
    "setQuickSResCount(req.responseText, req.responseJS, " + pr_submit + ")");
}
function setQuickSResCount(count, hash,pr_submit){
	try{
		add_str="";
		if(showSearchParams){
			add_str	= " in " + document.getElementById('csz').value;
		}
	}
	catch(e){}
	


	var o = document.getElementById('quick_res_count');
	o.style.display = "block";
	var pr_found = false;
	if(count!="error" && count!="0"){
		if(count == "cities_vars"){
			if(pr_submit || true){
				if(document.body.offsetWidth < 1100){
					var str = "<div style='z-index:100;text-align:left;padding:3px;border:1px #B6CFE2 solid;background:#FFFFFF;width:135px;margin-left:10px;'>"
				}
				else {
					var str = "<div style='z-index:100;position:relative;left:-165px;top:-70px;text-align:left;padding:3px;border:1px #B6CFE2 solid;background:#FFFFFF;width:135px;margin-left:10px;'>"
				}
				str += "Narrow your search:";
				for(var i=0;i<hash.length;i++){
					if (hash[i].id) {
						str += "<br><a href='/"+hash[i].id+"' onclick='document.getElementById(" + '"quick_res_count"' + ").style.display=" + '"none"' + ";return true;' style='padding:2px 3px;text-decoration:none;'>Listing #" + hash[i].id + "</a>";
					str += "<br><a href='#' onclick='document.getElementById(" + '"csz"' + ").value=this.innerHTML;document.getElementById(" + '"quick_res_count"' + ").style.display=" + '"none"' + ";quickResultCount(true);return false;' style='padding:2px 3px;text-decoration:none;'>" + hash[i].city.toLowerCase() + ", " + hash[i].state.toUpperCase() + "</a>";
				}			
					else {
						str += "<br><a href='#' onclick='document.getElementById(" + '"csz"' + ").value=this.innerHTML;document.getElementById(" + '"quick_res_count"' + ").style.display=" + '"none"' + ";quickResultCount(true);return false;' style='padding:2px 3px;text-decoration:none;'>" + hash[i].city.toLowerCase() + ", " + hash[i].state.toUpperCase() +"</a>";
					}
				}			
				o.innerHTML = str + "</div>";
			}else{
				o.style.display = "none";
			}
		}else{
			pr_found = true;
			o.innerHTML = count + " houses found" + add_str;
		}
	}else{
		o.innerHTML = "<span style='color:red'>No houses found" + add_str + ". Try to change a search criteria.</span>";
	}	
	
	if(pr_submit && pr_found ){
		if(hash.url_str){
			document.getElementById('form_search_result').action = hash.url_str;
		}
		document.getElementById('form_search_result').submit();
	}
}
//-----------------------------------------------------------------------------
function advanced_search_house_id(house_id){
         ajaxData('/ajax_ping_house_id.html',{house_id:house_id},'result_ping_house_id(\''+house_id+'\',req.responseJS,req.responseText)');
}

function result_ping_house_id(house_id,data, mes){
	 if (mes=="yes"){
	 	 
	     window.location.href = "/" + house_id;	 
		 }
    else{
		el = document.getElementById('message_advanced_search_house_id');
		el.innerHTML = "House ID "+house_id+" not found"  
		}   
}

function index_advanced_search_house_id(e){
    
    i = document.getElementById('house_id'); 
    if (i){
       if (i.value != ''){
		           
		        advanced_search_house_id(i.value);
                //window.location.href = "/" + i.value;
                if (undefined == e)
                   { 
                    e = window.event
                   }
                if (e.stopPropagation)
                   {
                    e.stopPropagation();
                   }
               else
                   {
                    e.cancelBubble = true;
                    }

            return false;
       }
	   	el = document.getElementById('message_advanced_search_house_id');
		el.innerHTML = "Please enter a valid House ID code";
    }
	return false;
}


function advanced_search_params(){
         address = document.getElementById("address").value;
    	 sq_footage_min = document.getElementById("sq_footage_min").value;
		 sq_footage_max = document.getElementById("sq_footage_max").value;
         zip = document.getElementById("zip_adv_search").value;
         badrooms = document.getElementById("badrooms").value;
		 city = document.getElementById("city").value;
		 baths = document.getElementById("baths").value;
		 state = document.getElementById("state").value;
		 fsbo_agent = document.getElementById("fsbo_agent_adv_search").value;
		 property_type = document.getElementById("property_type").value;
		 min_price = document.getElementById("min_price").value;
		 max_price = document.getElementById("max_price").value;
		 with_photos = document.getElementById("with_photos_adv_search").value;
		 feature = document.getElementById("feature").value; 
         hash = {"address":address,
				 "sell_sold": document.getElementById('idsell_sold').options[document.getElementById('idsell_sold').selectedIndex].value,
				"sq_footage_min":sq_footage_min,
				"sq_footage_max":sq_footage_max,
				"zip":zip,
				"csz":zip,
				"badrooms":badrooms,
				"city":city,
				"baths":baths,
				"state":state, 
				"fsbo_agent":fsbo_agent, 
				"property_type":property_type, 
				"min_price":min_price, 
				"max_price":max_price, 
				"with_photos":with_photos, 
				"feature":feature}
         ajaxData('ajax_get_quick_houses_advanced_search_res.html',hash,'result_ping_advanced_params(req.responseJS,req.responseText)');
}

function result_ping_advanced_params(data, mes){
	 //alert(mes); 
	 if (mes!="error" && mes!="0"){
		 if (mes=="1")
		     window.location.href = "/" + data["house_id"];
		 else {
		    fr = document.getElementById('form_adv_sch_result_params');
		    fr.zip.value = document.getElementById('zip_adv_search').value; 
		    fr.submit();
		 }
		   	 
		 }
    else{
		document.getElementById('message_advanced_search_parrams').innerHTML = "We're sorry, we found no results that closely match your search.<br/> Try using less strict search criteria.";
		//alert('error');
		//el = document.getElementById('message_advanced_search_params');
		//el.innerHTML = "Not found House ID  - " + house_id 
		}   
}

function index_advanced_search_params(e){
    //i = document.getElementById('house_id'); 
	document.getElementById('message_advanced_search_parrams').innerHTML = "";
    mes = check_bind_advanced_params();
	if (mes=='ok'){
		        advanced_search_params();
                //window.location.href = "/" + i.value;
                if (undefined == e)
                   { 
                    e = window.event
                   }
                if (e.stopPropagation)
                   {
                    e.stopPropagation();
                   }
               else
                   {
                    e.cancelBubble = true;
                    }

            return false;
        } else {
	  document.getElementById('message_advanced_search_parrams').innerHTML = mes;	
	}
	return false;
}

function check_bind_advanced_params(){
	     zip = document.getElementById('zip_adv_search').value;
		 city = document.getElementById('city').value;
		 state = document.getElementById('state').value;
         if ((zip=='') && ((city=='') || (state=='')))	
	        return "Pleas enter ZIP or City and State"
		else
		    return "ok"
}


//------------------------------------------------------
var global_zen_help_timeout;
var pr_loaded_zen_help = false;

function runzenhelp(){
	if (window.startChat){
		startChat('0');	
		clearTimeout(global_zen_help_timeout);
		pr_loaded_zen_help = true;
	}else{
		global_zen_help_timeout = setTimeout("runzenhelp()",1000);
	}
}

function showZenHelp(){
	if(pr_loaded_zen_help){
		startChat('0');	
		return;
	}
	var script = document.createElement('SCRIPT');
	script.src = "http://zen-help.com/visitor/index.php?_m=livesupport&_a=htmlcode&departmentid=9&pr_not_show_html=1";
	document.getElementById('zenhelparea').appendChild(script);
	global_zen_help_timeout = setTimeout("runzenhelp()",1000);
}


//----------------------------------------------------------------------------
// by Marat Novitsky <marat.novitsky@gmail.com>
// dhtml features to get suggestion data
function applySuggestionAdv(data_to_send,data_to_fill){
    //var loader_id = show_loader_indicator(div_id, "");
    var target = "/ajax-suggestion-zip.html";
    var data = {zip: data_to_send};	
//	ajaxData(target,data,"document.getElementById('" + loader_id + "').style.display='none';applyFill(req.responseJS,req.responseText,{state:'"+data_to_fill['state']+"',city:'"+data_to_fill['city']+"'})");
    ajaxData(target,data,"applyFill(req.responseJS,req.responseText,{state:'"+data_to_fill['state']+"',city:'"+data_to_fill['city']+"'})");
}

function applyFill (data,text,dataToFill){
    //alert(data[0].city)
    if(text == 'error') return false;
    if(dataToFill['state']!="" && (data[0]['state'] || data[0]['zone_id'])){
        if(document.getElementById(dataToFill['state'])!='undefined'){
            var filled = 0;
            var obj = document.getElementById(dataToFill['state']);
            obj.style.display = 'block';
            for (i=0;i<obj.options.length;i++){
                if(obj.options[i].value == data[0]['state']){			
                    obj.options[i].selected = 'true';
                    filled = 1;
                }
            }
            if (filled!=1){
                for (i=0;i<obj.options.length;i++){
                    if(obj.options[i].value == data[0]['zone_id']){			
                        obj.options[i].selected = 'true';
                        filled = 1;
                    }
                }
            }
        }
    }
    
    if(dataToFill['city']!="" && data[0]['city']){
        if(document.getElementById(dataToFill['city'])){
            var obj = document.getElementById(dataToFill['city']);
            obj.style.display = 'block';
            obj.value = data[0]['city'];
        }
    }
}

function sendContactFormAdvertise(form_id, div_id, template_id){
    var loader_id = show_loader_indicator(div_id, "");
    ajaxData("/contact_ajax_send_form_advertise.html", {data:document.getElementById(form_id),pr_result:0,mode:'send'}, "document.getElementById('" + loader_id + "').style.display='none';setBlock('"+div_id+"','"+template_id+"',req.responseJS,req.responseText)");
}

function CheckZipMlsSell (zipCode,e) {
	if(!zipCode) return false;
	removeHandler(e);
	ajaxData("/check_zip_mls.html", {zip:zipCode}, "document.getElementById('check_zip_mls').style.display='none';setCheckZipMlsSell(req.responseJS,req.responseText)");
}

function setCheckZipMlsSell (data, text) {
	if(data['is_mls'] == '1'){
		document.getElementById('check_zip_mls').style.display = 'block';
	}
	return false;
}
function removeHandler(e) {
	if (undefined == e)
	   { 
		e = window.event
	   }
	if (e.stopPropagation)
	   {
		e.stopPropagation();
	   }
   else
	   {
		e.cancelBubble = true;
		}

	return false;
}

  function sendInaccurateData(){
     var mes = document.getElementById("report_inaccurate_data_message").value;
	 var email = document.getElementById("report_inaccurate_email").value; 
	 var name = document.getElementById("report_inaccurate_name").value;
	 var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	 if (!filter.test(email)) {
		  	document.getElementById("incorrect_mes_error").style.display = "block";
			return false;
	 }
	 
	 ajaxDataScript("/services/report_inaccurate_data.html",{mes:mes, url:document.location.href, email:email,name:name},"");

	 document.getElementById("report_inaccurate_data").style.display = "none";
	 document.getElementById("report_inaccurate_data_send").style.display = "block";

	 document.getElementById("report_inaccurate_data_message").value = "";
     document.getElementById("incorrect_mes_error").style.display = "none";
     return false;
  }

function addHandler(object, event, handler, useCapture) {
if (object.addEventListener) {
object.addEventListener(event, handler, useCapture ? useCapture : false);
} else if (object.attachEvent) {
object.attachEvent('on' + event, handler);
} else alert("Add handler is not supported");
}
function hotSave(evt) {

evt = evt || window.event;
 if((evt.ctrlKey) && ((evt.keyCode == 0xA)||(evt.keyCode == 0xD)))
 {
 		showDivIncorrect();
        return false;
  }
}
  
 function showDivIncorrect(){
 	 	var d_w = screen.width;
  		var df = document.getElementById('report_inaccurate_data');
		df.style.top = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + 220 + 'px';
		df.style.left = Math.round( (d_w / 2) - 639) + 'px';  	  	

		showHideObj('report_inaccurate_data');
 }
 
 function mapsLoaded(){
	if (load_google_script) return;
	load_google_script = true;
	//google.load("search", "1");
	
}
function mapsSearchLoaded(){
	    load_google_search_script = true;
}

function loadMaps() {
  google.load("maps", "2.x",{"callback" : mapsLoaded});
}

function loadMapSearh(){
   if (!load_google_script) return;
   google.load("search", "1",{"callback" : mapsSearchLoaded});
}




function adsense_init () {

	if (document.all) {  //ie

		var el = document.getElementsByTagName("iframe");
	
		for(var i = 0; i < el.length; i++) {
			if(el[i].src.indexOf('googlesyndication.com') > -1) {
				adUnit = i+1;
				browser = "IE";
				adunitsize = el[i].width + "x" + el[i].height;
				el[i].onfocus = trackAdsense;
			}
		}
	
	} else {   // firefox
	
		window.addEventListener('beforeunload', doPageExit, false);
		window.addEventListener('mousemove', getMouse, true);
	
	}
		
}
function getMouse(e) {
	px=e.pageX;
	py=e.clientY;
}

function findY(obj) {
	var y = 0;
	while (obj) {
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return(y);
}

function findX(obj) {
	var x = 0;
	while (obj) {
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return(x);
}

function doPageExit(e) {
	ad = document.getElementsByTagName("iframe");
	for (i=0; i<ad.length; i++) {
		var adLeft = findX(ad[i]);
		var adTop = findY(ad[i]);
		var inFrameX = (px > (adLeft - 10) && px < (parseInt(adLeft) + parseInt(ad[i].width) + 15));
		var inFrameY = (py > (adTop - 10) && py < (parseInt(adTop) + parseInt(ad[i].height) + 10));
		
		if (inFrameY && inFrameX) {
			adUnit = i+1;
			browser = "Firefox";
			adunitsize = ad[i].width + "x" + ad[i].height;
			trackAdsense();
		
		}
	}

}