function el(id) {
    return document.getElementById(id);
}

// var elems = new Array('city', 'county', 'location', 'zipcode', 'mls_id', 'map');
function toggleMenu(elem) {    
    if(el('search_type').value == elem ) return;
    
    var old_tab = el('search_type').value;
    var new_tab = elem;
    
    deselectTab(old_tab);
    el(old_tab + 'Div').style.display = 'none';    
    if(el(old_tab + 'Lnk')){                            
    	el(old_tab + 'Lnk').className = 'inactiv_tab';
    }
    
    el(new_tab + 'Div').style.display = 'block';                                
    el(new_tab + 'Lnk').className = 'activ_tab';
        
    // assign search type
    if(el('search_type')) el('search_type').value = elem;
    
    //change display style    
    if(new_tab == 'map') { el('display_style').value = 'map'; }
  
    if( el('display_style').value == 'map') {
        if(el('mapDiv') && el('mapDiv').style.display == 'none') el('mapDiv').style.display == 'block'; //show map
        if(el('mapResDiv')) el('mapResDiv').style.display = 'block'; //show map list, if available               
        if(el('workLoaderDiv')) el('workLoaderDiv').style.display = 'none';   // hide work div                      
        //load map for the first time
        if(!_map_loaded_){setTimeout('initMap();',200);}; 
        if(map && map.checkResize) { map.checkResize(); map.setCenter(_o_point,_o_zoom); setTimeout('map.checkResize();', 500); }
        if(window.updateExtent){ updateExtent();}
        if(el('mapResLnk')) showResults('map');                               
    }
    else {        
        // when changing tab from map to smth else, change view
        if(el('workLoaderDiv')) el('workLoaderDiv').style.display = 'block';        
    }

    // change state
    if(new_tab == 'city') { if(def_state){el('statesCity').value = def_state;} el('statesCity').onchange(); }
    if(new_tab == 'county') { if(def_state){el('statesCounty').value = def_state;} el('statesCounty').onchange();}
    if(new_tab == 'location') { if(def_state){el('states').value = def_state;} el('states').onchange();}               
    
    // when MLS_ID -> hide basic - advanced criteria form
    if(new_tab == 'mls_id') {
        el('searchFormDiv').style.display = 'none';
        if(el('search_criteria')){ //remove basic & advanced criteria
            while(el('search_criteria').childNodes.length>0){
               if(el('search_criteria').childNodes[0] && el('search_criteria').childNodes[0].id){
                   deselect(el(el('search_criteria').childNodes[0].id.substring(3)));
               }else{
                   el('search_criteria').removeChild(el('search_criteria').childNodes[0]);
               }
            }
        }
    }
    else { if(el('searchFormDiv').style.display == 'none') { el('searchFormDiv').style.display = 'block'; } }   
    
    // when changing the tab, expand main form div
    if(el('ps_form_content').style.display == 'none'){
        el('ps_form_content').style.display = 'block';
        if(el('ps_form_img')) el('ps_form_img').src = img_path + 'expanded.gif'; 
        if(el('ps_form_msg')) el('ps_form_msg').style.display='none';
        if(el('ps_edit_msg')) el('ps_edit_msg').style.display='none';
    }    
}


function resetCriteria() {
    if(el('scope')){
        el('scope').value = 'ALL';
        if(document.all) el('ps_form').scope.value = 'ALL';
    }
    if(el('mls')) el('mls').value ='';
    if(el('agent_mls_id')) el('agent_mls_id').value ='';
    if(el('office_mls_id')) el('office_mls_id').value ='';
    if(el('attention_msg')) el('attention_msg').style.display='none';
    if(el('press_search_msg')) el('press_search_msg').style.display='block';
}


function deselectTab(elem){
    var des = new Array();
    des['mls_id']= new Array('listing_mls_id_1','listing_mls_id_2','listing_mls_id_3','listing_mls_id_4','listing_mls_id_5','listing_mls_id_6');
    des['zipcode']= new Array('zipcode','radius');
    des['map']= new Array('extent_sw','extent_ne');
    des['location']= new Array('states','county','city');
    des['city']= new Array('statesCity','city');
    des['county']= new Array('statesCounty','county');

    for(var j in des[elem]) { deselect( el(des[elem][j]) ); }  
    
    checkState();
    
    if(el('search_criteria')){ el('search_criteria').style.height='auto'; }   
}

/** SEARCH TYPE TABS FUNCTIONS**/
function numericInput(obj){
    obj.value=obj.value.replace(' ','','gi');
    for(var i=0; i<obj.value.length;i++) if( isNaN(obj.value.charAt(i)) ){
        obj.value=obj.value.replace(obj.value.charAt(i),'','gi');
    }
    if(obj.name == 'zipcode') optionSelected(obj,'Zipcode');
}

function emptyList(obj){
    if(obj.options && obj.options.length>0) for (var i=obj.options.length-1; i>=0; i--) obj.options[i]=null; 
}
    
function selectNone(obj){
    obj.selectedIndex = -1;    
}

function populateCity(){    
    var city = document.getElementById("city");
    var state = document.getElementById("statesCity");        
    var idx = state.options[state.selectedIndex].value;
     
    emptyList(city);
    
    var j = 0;
    for (var i in groupCity[idx]) { 
        if(groupCity[idx][i] == '') city.options[j] = new Option("-- N/A --", groupCity[idx][i]);
        else city.options[j] = new Option(groupCity[idx][i], groupCity[idx][i]); 
        j++;
    }
}


function populateCounty(x){
    var county = document.getElementById("county");
    var state = document.getElementById("statesCounty");    
    if(state){
    	var idx = state.options[state.selectedIndex].value;
    }
    
    emptyList(county);
    
    var j = 0;
    for (var i in groupCounty[idx]) { 
        if(groupCounty[idx][i] == '') county.options[j] = new Option("-- N/A --", groupCounty[idx][i]); 
        else county.options[j] = new Option(groupCounty[idx][i], groupCounty[idx][i]);
        j++;
    }
}

    
function populateLocationCounty(){ 
    var state = document.getElementById("states");      
    var county = document.getElementById("county");
    var city = document.getElementById("city");     
    var idx = state.options[state.selectedIndex].value;
    var textArray = new Array();
    
    el('stateName').innerHTML = 'in <b>' +  state.options[state.selectedIndex].text + '</b> state';
    el('stateCityName').innerHTML = 'in <b>' +  state.options[state.selectedIndex].text + '</b> state';     
    el('countyName').innerHTML = '';
        
    emptyList(city);
    emptyList(county);
        
    var j = 0; var k = 0;
    for (var i in groups[idx]) {            
        if(i == '') county.options[j] = new Option("-- N/A --", i);
        else county.options[j] = new Option(i,i);
        j++; 
        
        for (var l in groups[idx][i]) {                        
            //city.options[k] = new Option(groups[idx][i][l], groups[idx][i][l]);
            textArray[k] = groups[idx][i][l];
            k++;
        }
    }
    textArray = cleanUpArray(textArray);
    textArray.sort(); 
    var y = 0;
    for (var x in textArray) { city.options[y] = new Option(textArray[x], textArray[x]); y++;}
}
    
function populateLocationCity() {
    var state = document.getElementById("states");      
    var county = document.getElementById("county");
    var city = document.getElementById("city");  
    var textArray = new Array();
    
    if( county.options.length > 0) {       
    	if(state){            
        	var idx1 = state.options[state.selectedIndex].value;
        }                
        var idx2 = county.options[county.selectedIndex].value;       
        
        if(idx2!=''){
            el('countyName').innerHTML = 'in <b>' + county.options[county.selectedIndex].text + '</b> county'; 
        }else{
            el('countyName').innerHTML = '';
        }
        if(county.options[county.selectedIndex].value != '') 
            el('stateCityName').innerHTML = '';
        
        emptyList(city);
        
        var j = 0;
        for (var i in groups[idx1][idx2]) { 
            if(groups[idx1][idx2][i] != '') {                                   
                //city.options[j] = new Option(groups[idx1][idx2][i], groups[idx1][idx2][i]);
                textArray[j] =  groups[idx1][idx2][i];
                j++;
            }
        }           
    }
    textArray = cleanUpArray(textArray);
    textArray.sort(); 
    var y = 0;
    for (var x in textArray) { city.options[y] = new Option(textArray[x], textArray[x]); y++;}

}
    
function populateAllCity(){
    var state = document.getElementById("states");             
    var city = document.getElementById("city");          
    var idx = state.options[state.selectedIndex].value;
    
    var textArray = new Array();
    
    el('stateName').innerHTML = 'in <b>' +  state.options[state.selectedIndex].text + '</b> state';
    el('stateCityName').innerHTML = 'in <b>' +  state.options[state.selectedIndex].text + '</b> state'; 
    el('countyName').innerHTML = '';
    
    emptyList(city);     
    var k = 0;       
    for (var i in groups[idx]) {                              
        for (var l in groups[idx][i]) {      
            textArray[k] =  groups[idx][i][l];                                         
            k++;
        }
    }
    textArray = cleanUpArray(textArray);
    textArray.sort(); 
    var y = 0;
    for (var x in textArray) { city.options[y] = new Option(textArray[x], textArray[x]); y++;}
    deselect(el('county'));           
}
    

function cityClick(){
    var multi=0;
    for(var i=0;i<el('city').options.length;i++) { if(el('city').options[i].selected) multi++; } 
    if(el('city').value == selected_city && multi<2) { 
        deselect(el('city')); 
        selected_city = ''; 
    }                        
    else { 
        selected_city = el('city').value; 
    } 
}      


/** SEARCH TYPE TABS**/

function showHide(id, img, msg) {    
    collapse_img = img_path + 'collapsed.gif'; 
    expand_img = img_path + 'expanded.gif'; 
   
    if( el(id).style.display == 'block')   {
        el(id).style.display='none';
        if(el(img)) el(img).src = collapse_img;
        if(el(msg)) el(msg).style.display='block';
        if(id == 'ps_form_content') {  if(el('ps_edit_msg')) el('ps_edit_msg').style.display='block'; }     
    }
    else {
        el(id).style.display = 'block';
        if(el(img)) el(img).src = expand_img;
        if(el(msg)) el(msg).style.display='none';
        if(id == 'ps_form_content') {  if(el('ps_edit_msg')) el('ps_edit_msg').style.display='none'; }   
    }
}




function showToolTip(e,text){
    if(document.all)e = event;
    var index = 0;
    var lnk = (e.srcElement) ? e.srcElement : (e.target ? e.target : null);  
    
    var obj = document.getElementById('bubble_tooltip');
    var obj2 = document.getElementById('bubble_tooltip_content');
       
    obj2.innerHTML = text;        
    obj.style.display = 'block';
    var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
    if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0; 
    //var leftPos = e.clientX - 100;
       
    var leftPos = e.clientX + 5; 
    if(leftPos < 0)leftPos = 0;       
    obj.style.left = leftPos + 'px';
    obj.style.top = e.clientY - obj.offsetHeight -1 + st + 'px';    
}   

function hideToolTip(){
    document.getElementById('bubble_tooltip').style.display = 'none';
    
}



function shownote(id, txt, title){
    //if(note_msg.length < 1 ) { //some default values }
     
    if(note_msg[id] && note_msg[id].title) title =  note_msg[id].title;
    if(note_msg[id] && note_msg[id].text) txt =  note_msg[id].text;   

	if(id !='' && id !=' ' && !note_msg[id] ) {
        title =  note_msg['vip'].title;
        txt =  note_msg['vip'].text;
    }

    if(txt && txt!='undefined') {
        if(window.back2results) { txt = txt.replace(/__REF__/gi,back2results);}
        else { txt = txt.replace(/__REF__/gi,window.location.pathname); }
    }
    var obj = document.getElementById('warning_div');
    obj.style.visibility = "visible";
    obj.style.overflow = "visible";

    var msg = document.getElementById('warning_message');
    msg.innerHTML = txt;
    obj.style.zIndex=190000;   
    if(title && title != ''){
        var titlu = document.getElementById('warning_title') ;
        titlu.innerHTML = title;
    }
    blockObj();
    centerBox(document.getElementById('warning_div'));
}

function closenote(){
    var obj = document.getElementById('warning_div');
    obj.style.visibility = "hidden";
    deblockObj();   
}


function centerBox(obj){    

    var b=(document.documentElement?document.documentElement:document.body);
    var sT = document.body.scrollTop >0 ? document.body.scrollTop : document.documentElement.scrollTop;
    var wH=window.innerHeight?window.innerHeight:(document.documentElement.clientHeight>=document.body.clientHeight?document.documentElement.clientHeight:document.body.clientHeight);
    if(wH > 1000){
		wH = 600;
	}
    var wW=window.innerWidth?window.innerWidth:(document.documentElement.clientWidth>=document.body.clientWidth?document.documentElement.clientWidth:document.body.clientWidth);
    obj.style.top=parseInt(parseInt(wH-obj.offsetHeight)/2+parseInt((document.documentElement.scrollTop>=document.body.scrollTop?document.documentElement.scrollTop:document.body.scrollTop)))+'px';
    //obj.style.top=parseInt(parseInt(wH-obj.offsetHeight)/2+parseInt())+'px'; new code commented relating to bug 19417 
    obj.style.left=parseInt(parseInt(wW-obj.offsetWidth)/2)+'px';
/*
    var b=(document.documentElement?document.documentElement:document.body);
    var wH=window.innerHeight?window.innerHeight:b.clientHeight;
    var wW=window.innerWidth?window.innerWidth:b.clientWidth;
    obj.style.top=parseInt(parseInt(wH-obj.offsetHeight)/2+parseInt(b.scrollTop))+'px';
    obj.style.left=parseInt(parseInt(wW-obj.offsetWidth)/2)+'px';
    alert("hi1");*/
}

function blockObj(){
    var op=50;
    var color='#999';

    if(!document.getElementById('_blocked_')){
        var o=document.createElement("div");
        o.id='_blocked_';
        o.style.overflow = "visible";
        document.body.appendChild(o);
        var p=document.createElement("iframe");
        p.style.height='100%';
        p.style.width='100%';
        p.style.overflow = "visible";
        o.appendChild(p);

    }else{
        var o=document.getElementById('_blocked_');
    }
    o.style.top=0+'px';
    o.style.left=0+'px';
    document.body.style.hight='100%';
    
    //o.style.height=(document.body.offsetHeight>=document.documentElement.offsetHeight?document.body.offsetHeight:document.documentElement.offsetHeight)+30+'px';
    var s=0;
    for(var i in document.body.childNodes) if(document.body.childNodes[i].offsetHeight){
            s+=document.body.childNodes[i].offsetHeight;
    }
    o.style.height=s+30+'px';
    o.style.width=document.body.offsetWidth+19+'px';
    o.style.opacity=op/100;
    o.style.filter='alpha(opacity='+op+')';
    o.style.backgroundColor=color;
    o.style.position='absolute';
    o.style.display='block';
    //o.style.zIndex=290;
    o.style.overflow = "visible"; 
}

function deblockObj(){
    document.getElementById('_blocked_').style.display='none';
}


function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent) { while (obj.offsetParent) { curleft += obj.offsetLeft; obj = obj.offsetParent; } }
    else if (obj.x) { curleft += obj.x; }
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent) { while (obj.offsetParent) { curtop += obj.offsetTop; obj = obj.offsetParent;}}
    else if (obj.y) { curtop += obj.y; }
    return curtop;
}

/**
# number_format(number, decimals, comma, formatSeparator)
# number_format(1234567890, 0, '.', ',') would return 1,234,567,890.
# number_format(1234567890.119, 2, ',', '.') would return 1.234.567.890,12.
 */
function number_format(a, b, c, d) {    
    a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
    e = a + '';
    f = e.split('.');
    if(!f[0]) f[0] = '0';
    if(!f[1]) f[1] = '';
    if(f[1].length < b){
        g = f[1];
        for(i = f[1].length + 1; i <= b; i++) {
            g += '0';
        }
        f[1] = g;
    }
    if(d != '' && f[0].length > 3) {
        h = f[0];
        f[0] = '';
        for(j = 3; j < h.length; j += 3) {
            i = h.slice(h.length - j, h.length - j + 3);
            f[0] = d + i +  f[0] + '';
        }
        j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
        f[0] = j + f[0];
    }
    c = (b <= 0) ? '': c;
    return f[0] + c + f[1];
}


function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}

function cleanUpArray(arr){
    var i,j=1;
    var nar=new Array();
    nar[0]=arr[0];
    for(i=0;i<arr.length;i++){
        if(arr[i]!='' && !in_array(arr[i],nar)){
            nar[j] = arr[i]; j++;
        }
    }
    return nar;
}

function in_array(val,arr){
    var f=false;
    for(var i=0;i<arr.length;i++){
         if(arr[i] && arr[i].toLowerCase && arr[i].toLowerCase()==val.toLowerCase()) f=true;
    }
    return f;
}