﻿var ajaxcount = 1;
var nPages = 0;
var selectedelem;
var bussearchtype = 'bnc';
var Total = 0;
var HideAC = false;
var is411 = '';
/* Constants */
var GhostBus = 'Business Name or Category';
var GhostLoc = 'City, State or Zip';
var GhostLookup = 'Enter your cell number';




function ShowMenuSettings(android) {        
    var siteContainer = document.getElementById("innercont");
    //siteContainer.style.opacity = "0.3";    
    if (android == 'android') {
        siteContainer.style.visibility = "hidden";
    }
    
    var width = siteContainer.offsetWidth;
    var height = siteContainer.offsetHeight;
    
    var elem = document.getElementById("CellMenuButton");
    var top = elem.offsetParent.offsetTop;
    		
	var modal = document.getElementById("ContainerModalMenu");
	modal.style.top = top - 2 + "px";
	modal.style.width = width + "px";
	
	var cover = document.getElementById("ContainerCover");
	cover.style.top = top - 2 + "px";
	cover.style.height = height + "px";
}

function HideMenuSettings(android) {
    if (android == 'android') {
        var siteContainer = document.getElementById("innercont");
        //siteContainer.style.opacity = "1";
        siteContainer.style.visibility = "visible";
    }
}
function DisableLink() {
        var elem = document.getElementById('msftve_1000_200000_10000')
        if (elem != null)
            elem.removeAttribute("href");

}


//Not used anymore
function SetSearchMode(type) {
    var btnN = document.getElementById('btnName');
    var btnC = document.getElementById('btnCategory');
    var hiddenST = document.getElementById('hiddenSearchMode');
        
    if (type == 'name') {
        hiddenST.value = 'name';
        btnN.src = '/Images/' + is411 +'btn_name_pressed.png';
        btnC.src = '/Images/' + is411 + 'btn_category.png';
        bussearchtype = 'bnc'    
    }
    if (type == 'category') {
        hiddenST.value = 'category';
        btnN.src = '/Images/' + is411 + 'btn_name.png';
        btnC.src = '/Images/' + is411 + 'btn_category_pressed.png';
        bussearchtype = 'bnc'    
    }
}
function Select(value) {
    document.getElementById(selectedelem.id).value = value;
    CloseAutocomplete();
    if (selectedelem.id != 'InputLocation') {
        var elem = document.getElementById('InputLocation');
        if (elem) {
            elem.focus();
        }
    }
}

function CloseAutocomplete() 
{
    var dropdwn = document.getElementById("ContainerAC");    
	var innercont = document.getElementById("innercont");    
    if (dropdwn.lastChild != null && dropdwn.style.display == "block") {
        dropdwn.innerHTML = ""; 
        dropdwn.style.display = "none";
		innercont.style.height="auto";
    }
    
}
function hideClearBox(elementname) {
    var elemcb = document.getElementById(elementname)
    if (elemcb != null)
        elemcb.style.display = "none";

}
function showClearBox(elementname) {
    var elemcb = document.getElementById(elementname)
    if (elemcb != null)
        elemcb.style.display = "inline";

}
function showClearBoxes() {
    var elem = document.getElementById('InputNameCat' );
    var elemcb = document.getElementById('InputNameCat' + 'cb');
    if (elem != null && elemcb != null) {
        if (elem.value == '' || elem.value == GhostBus)
            elemcb.style.display = "none";
        else
            elemcb.style.display = "inline";
    }
    var elem = document.getElementById('InputLocation');
    var elemcb = document.getElementById('InputLocation' + 'cb');
    if (elem != null && elemcb != null) {
        if (elem.value == '' || elem.value == GhostLoc)
            elemcb.style.display = "none";
        else
            elemcb.style.display = "inline";
    }

}
function hideEmptyAC() {

    var dropdwn = document.getElementById("ContainerAC");
    if (!dropdwn.innerHTML) {
        dropdwn.style.display = "none";
    }


}
function showAC(elem) {
    if (!elem) {
        elem = selectedelem;
    }

    
    var dropdwn = document.getElementById("ContainerAC"); 
    var aTop = Netbiscuits.core.helpers.getOffsetTop(elem);
    aTop += elem.offsetHeight;
    var aLeft = Netbiscuits.core.helpers.getOffsetLeft(elem);
    dropdwn.style.left = aLeft + "px";
    dropdwn.style.top = aTop - 1 + "px";
    dropdwn.style.width = (elem.offsetWidth - 2) + "px";
    dropdwn.style.display = "block";
    //	innerCont.style.height = "600px";
    window.scrollToTop(elem.id, 15);

}


function Autocomplete(elem, stype) {
  
    if (HideAC)
    { }
    else {
            selectedelem = elem;
            tempelem = elem;
            var dropdwn = document.getElementById("ContainerAC");
            if (elem.value.length > 1) {
                Netbiscuits.core.ajax.getRemoteBiscuit('ContainerAC', 'GetAutoC.ashx?list=' + stype + '&startswith=' + elem.value, 'AC', '1');
                showAC(elem);
                
                
            }
            else {
                dropdwn.innerHTML = "";
                dropdwn.style.display = "none";
            }
    }
}
/*Autocomplete disable OS AC */
/* create namespace */

var nb = {};

nb.setAttribute = function (id, name, value) {

      var elem = Netbiscuits.core.helpers.getElement(id);

      if (elem != null)

            elem.setAttribute(name, value);

};





/*GEO IP functions */
function showGeoip() 
{
    if (navigator.geolocation) {
        var temp = document.getElementById('wpgreenlocatelink');
        if (temp != null)
        { temp.style.display = "block"; }

        var temp = document.getElementById('wpgreylocatelink');
        if (temp != null)
        { temp.style.display = "block"; }
    }
    else 
    {
       
    }
}

function getGeoip() 
{
   // elem.textContent= 'Getting location...';
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(success, error);
    }
   
}
function changeGetPositionLink(appendvalue) {

    var InpName = document.getElementById('InputNameCat');
    if (InpName == null) //PeopleSearch page
        InpName = document.getElementById('InputName');
    if (InpName == null) //ReverseAddress page
        InpName = document.getElementById('InputLocation');
    var NameCat = document.getElementById('hiddenSearchMode');
    var Inpvalue = "";
    if (InpName != null) {
        Inpvalue = "&" +InpName.name + "=" + InpName.value;
    }
    if (NameCat != null) {
        Inpvalue += "&" + NameCat.name + "=" + NameCat.value;
    }
    for (i = 0; i < document.links.length; i++) 
    {   var temp;
        if (document.links[i].href.search('locateme=true') != -1 && document.links[i].href.search('#') == -1 ) {
            temp = document.links[i].href + appendvalue + Inpvalue;
            window.location = temp.replace("#", "");
            return true;
        }
    }
}
function success(position) {
    var lat = position.coords.latitude;
    var lon = position.coords.longitude;
    var changepos = '&lat=' + lat + '&lon=' + lon;
   
    if (!changeGetPositionLink(changepos)) {
        Netbiscuits.core.ajax.getRemoteContent('ContainerAC', 'AsyncCall.ashx?job=loc' + changepos);
    }
   
    return false;
    
    //Testing only
 /*    var s = document.getElementById('rowpagename');
    s.innerHTML = "found you!";
    s.className = 'success';
    */

}

function phoneReport(listingid) {


    Netbiscuits.core.ajax.getRemoteContent('ContainerAC', 'AsyncCall.ashx?job=phone&listingid=' + listingid);
    return false;
}
var DoMapReport = true;
function mapReport(listingid) {

    if (DoMapReport) {
        Netbiscuits.core.ajax.getRemoteContent('ContainerAC', 'AsyncCall.ashx?job=map&listingid=' + listingid);
        DoMapReport = false;
    }
    return false;
}

var DoDescReport = true;
function descReport(listingid) {

    if (DoDescReport) {

        Netbiscuits.core.ajax.getRemoteContent('ContainerAC', 'AsyncCall.ashx?job=desc&listingid=' + listingid);
        DoDescReport = false;
    }
    return false;
}
    //Testing only
    /*    var s = document.getElementById('rowpagename');
    s.innerHTML = "found you!";
    s.className = 'success';
    */



var Messageshown = 0;
function error(msg) {
    ShowLocationError();
    Messageshown = 1;
    
}

function ShowLocationError() {

    if (Messageshown == 0) {
        //alert('Sorry, we were unable to find your location.');
        Netbiscuits.core.ajax.getRemoteContent('', 'AsyncCall.ashx?job=loc' + '&result=fail');
    }
}

function ShowGhostText() {
    var elem = document.getElementById('InputNameCat');
    if (elem != null && elem.value == '') {
        elem.value = GhostBus;
        elem.style.color = '#BAB1A5';
    }
    ShowGhostTextLocation();


}
function ShowGhostTextLocation() {
    var elem = document.getElementById('InputLocation');
    if (elem != null && elem.value == '') {
        elem.value = GhostLoc;
        elem.style.color = '#BAB1A5';
    }


}

function ShowGhostLookup() {
    var elem = document.getElementById('billno');
    if (elem != null && elem.value == '') {
        elem.value = GhostLookup;
        elem.style.color = '#BAB1A5';
    }


}

function HideGhostText() {
    var elem = document.getElementById('InputNameCat');
    if (elem.value == GhostBus) {
        elem.value = '';
        elem.style.color = '#433F3A';
    }
}
function HideGhostTextLocation() {
    var elem = document.getElementById('InputLocation');
    if (elem.value == GhostLoc) {
        elem.value = '';
        elem.style.color = '#433F3A';
    }
}


function HideGhostLookup() {
    var elem = document.getElementById('billno');
    if (elem.value == GhostLookup) {
        elem.value = '';
        elem.style.color = '#433F3A';
    }
}



function HideMore() {

    var elem = document.getElementById('morelink');
    Netbiscuits.core.css.hide('loadingimage');
    if (ajaxcount == nPages) {
        elem.style.display = 'none';
    }
    else {
        var Remain = Total - (20 * ajaxcount);
        if (Remain > 20)
            Remain = 20;
        if (Remain < 20) {
            elem.innerHTML = elem.innerHTML.replace(/Show \d+/i, 'Show ' + Remain);
        }
        Netbiscuits.core.css.show('moretext');
    }
    
    return false;
  }

  function DisableZoom() {

      WebSettings.setBuiltInZoomControls(false);
  }

  function ClearCityState() {

      var CityStateField = document.getElementById('InputLocation')
      if (CityStateField != null)
          CityStateField.value = '';
 
          //BusinessSearch Name
      var Name = document.getElementById('InputNameCat')
      if (Name != null) 
          Name.value = '';
      
      //PeopleSearch Name    
      Name = document.getElementById('InputName')
      if (Name != null) 
          Name.value = '';
          
          
      return false;


  }
  function Clearbox(controlname) {
      var Box = document.getElementById(controlname);
      if (Box != null) {
          Box.value = '';
          Box.focus();
      }
        

}

scrollToTop = function(id, padding) {
    var elem = Netbiscuits.core.helpers.getElement(id);
    if (elem) {
        var top = Netbiscuits.core.helpers.getOffsetTop(elem);
        if (padding) {
            top -= padding;
        }
        if (window.scrollTo) {
            window.scrollTo(0, top);
        }
    }
};



