﻿// Project Name       : Shredit - Find a Branch
// Developed by       : Neologix Software Solutions
// Created Date       : 03 August 2010
// Last Modified      : 03 February 2011
// Page Description   : Manages auto suggest functionality

//
// Global variables
//  
var suggestPanel = null;
var searchBox = null;
var selectedItem = -1;
var baseURL = null;
var redirectionURL = "locations.aspx";
var resultContainerIndex = 2; // index starts from 0
var panelLookupStatus = 0;
var isPanelClicked = 0;
var lnkCloseBtn = null;
//var ContryName=null;
//
// Sets the global variables on load event
//
//$(document).ready(loadSettings);

initializeLoadSettings();

function initializeLoadSettings() {
    if (window.addEventListener) window.addEventListener('load', loadSettings, false);
    else if (document.addEventListener) document.addEventListener('load', loadSettings, false);
    else if (window.attachEvent) window.attachEvent('onload', loadSettings);
    else {
        if (typeof window.onload == 'function') { var oldload = window.onload; window.onload = function () { oldload(); loadSettings(); } } else window.onload = loadSettings; //Older browsers only
    }
}

function loadSettings() {
    setBaseURL();
    //Sets the controls for suggest
    //Param #1: Search textbox id   
    //Param #2: Suggest panel id
    setControls('searchfor', 'pnlSuggest');

    if (suggestPanel != null) {
        suggestPanel.onmouseout = function () {
            //toggleSuggestPanel(); 
            isPanelClicked = 0;
        }
    }

    if (lnkCloseBtn != null) {
        lnkCloseBtn.onclick = function () {
            suggestPanel.className = 'suggestPanel';
        }
    }

}

function SuggestPanel_Click(e) {
    isPanelClicked = 1;
    if (navigator.appName == 'Netscape' && e.which == 3) {
        isPanelClicked = 1;
    }
    else {
        if (navigator.appName == 'Microsoft Internet Explorer' && event.button == 2)
            isPanelClicked = 1;
    }
}

/* ========= Map functionality =========== */
//
// Unload google map
//
window.onunload = function () {
    //RenderGoogleMap(0);
}

//
// google map 
//
function RenderGoogleMap(status) {
    try {
        if (status == 1)
            initialize(); // initialize google map
        else
            GUnload(); // unload google map
    }
    catch (e) {

    }
}

//
// Gets the querystring
//
function getMapCenterPoint() {
    var locvalue = "";
    var hu = window.location.search.substring(1);
    var gy = hu.split("&");
    for (var i = 0; i < gy.length; i++) {
        var ft = gy[i].split("=");
        if (ft[0] == "q")
            locvalue = ft[1];
    }

    var ifrmMap = document.getElementById('ifrMap');
    if (ifrmMap != null) {
        ifrmMap.src = ifrmMap.src + "?q=" + locvalue;
    }

    //    return locvalue;
}


/* ========= Map functionality ends =========== */



/* ========= Auto suggest functionality =========== */
//
// Sets the suggest panel toggle
//
document.onclick = function () {
    selectedItem = -1;
    if (isPanelClicked != 1) {
        isPanelClicked = 0;
        toggleSuggestPanel();
    }

}

//
// Toggle suggest panel w.r.t the events 
//
function toggleSuggestPanel() {
    if (suggestPanel != null) {
        suggestPanel.className = 'suggestPanel';
        panelLookupStatus = 0;
    }
}

//
// Sets the searchbox and suggestpanel objects
//
function setControls(srchBox, srchPanel) {
    suggestPanel = document.getElementById(srchPanel);
    searchBox = document.getElementById(srchBox);
    lnkCloseBtn = document.getElementById('lnkClose');
}

//
// Sets the base url of the current site
//
function setBaseURL() {
    baseURL = window.location.href.substring(0, location.href.indexOf('/', 9));
}

//
// 
//
function redirectToMapPage(loc) {
    //loc=loc+','+setCountry('name');
    //window.location.href = baseURL + '/' + redirectionURL + '?q=' + loc;
    parent.window.location.href = baseURL + '/' + redirectionURL + '?q=' + loc + "&form1=true";
}

//
// Stores the selected item in suggest panel to search box Ann
// 
function storeText(textdata) {
    if (document.getElementById('txtSideSearchBox')) {
        if (document.getElementById('txtSideSearchBox').value != "No Results Found!")
            document.getElementById('txtSideSearchBox').value = textdata.replace("<b>", "").replace("</b>", "").replace("<B>", "").replace("</B>", "");
    }

    isPanelClicked = 0;
    //toggleSuggestPanel();  
}

function storeAndHide(textdata) {
    storeText(textdata);
    toggleSuggestPanel();
}

function StoreAndRedirectToMap(textdata) {
    storeText(textdata);
    redirectToMapPage(textdata.replace("<b>", "").replace("</b>", "").replace("<B>", "").replace("</B>", ""));
}

//
// Getting the keystrokes 
//
function getKeystroke(evt) {
    switch (evt.keyCode) {
        case 38:  // up arrow
            highlightItem('-');
            return '-';
            break;

        case 40: // down arrow
            highlightItem('+');
            return '+';
            break;

        case 27: // escape key
            toggleSuggestPanel();
            return '0';
            break;

        case 13: // return key
            toggleSuggestPanel();
            return '0';
            break;

        default:
            return '';
            break;
    }
}

//
// Highlight the selected item in suggest panel
//  
function highlightItem(dir) {
    //    if(suggestPanel.children.length > 0)
    //    {        
    var div = null;

    if (selectedItem > -1) {
        div = document.getElementById('Item' + selectedItem);
        if (div != null) {
            // Dynamically setting style to the suggestbox
            if (div.className == 'searchitemInnerContainerSmall Hover')
                div.className = 'searchitemInnerContainerSmall';
            else
                div.className = 'searchitemInnerContainer';
        }
    }


    if (dir == '+') // down arrow press
    {
        if (selectedItem == -1) {
            selectedItem = 0;
            div = document.getElementById('Item' + selectedItem);
            if (div != null) {
                div.className += ' Hover';
                storeText(div.innerHTML);
            }
        }
        else {
            ++selectedItem;
            div = document.getElementById('Item' + selectedItem);
            if (div != null) {
                div.className += ' Hover';
                storeText(div.innerHTML);
            }
            else {
                --selectedItem;
                div = document.getElementById('Item' + selectedItem);
                if (div != null) {
                    div.className += ' Hover';
                    storeText(div.innerHTML);
                }
            }
        }
    }
    else  // up arrow press
    {
        if (selectedItem != -1) {
            --selectedItem;
            if (selectedItem < 0) selectedItem = 0;
            div = document.getElementById('Item' + selectedItem);
            if (div != null) {
                div.className += ' Hover';
                storeText(div.innerHTML);
            }
        }
    }
    //    }
}

function showProcesStatus(showstatus) {
    if (searchBox != null) {
        if (showstatus != 4)
            searchBox.className = 'searchBox loading';
        else
            searchBox.className = 'searchBox';
    }
}

function setCountry(flag) {
    var userCountryName = geoip_country_name();
    var userCountryCode = geoip_country_code();

    if (userCountryName.indexOf("United States") > -1) {
        userCountryName = "USA";
    }
    if (flag == 'name')
        return userCountryName;
    else
        return userCountryCode;
}


//
// Get details of the state/province/city
//
function getSuggest(searchKey, evt, flag) {

//Check if CURRENT_CULTURE is defined 
	if(typeof(CURRENT_CULTURE) == "undefined")
	{ CURRENT_CULTURE = $('#lng', window.parent.document).attr('value');
	}
	  //Execute the appropriate code based on the current culture 
	  switch(CURRENT_CULTURE.toUpperCase()) 
	  { 
		case "EN-US": //enable Location-Lookup 
		  initializeSuggest(searchKey,evt,flag); 
		  break; 
		case "EN-GB": //disable Location-Lookup 
		  break; 
		default: //disable Location-Lookup 
	  } 
	
	
	
}

//
//
//
function initializeSuggest(searchKey,evt,flag)
{
    if (suggestPanel == null) loadSettings();

    if (searchKey == 'Choose country') searchKey = '';

    if (getKeystroke(evt) == '') {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange = function () {

            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                var CssId = 'suggestPanel';
                if (flag == 'city') CssId = 'suggestPanelSmall panelInner xyz';

                var result = xmlhttp.responseText;
                suggestPanel.innerHTML = result;
                if (result.length != 0)
                    suggestPanel.className = CssId + ' suggestVisible';
                else
                    suggestPanel.className = CssId;
                selectedItem = -1;
                panelLookupStatus = 1;

            }
        }

        var serverurl = '';
        if (flag == 'city')
            serverurl = "/CMSTemplates/Shredit/AjxSuggest.aspx?action=city&q=" + searchKey + "&countrycode=" + setCountry('code') + "&country=" + setCountry('name');
        else
            serverurl = baseURL + "/CMSTemplates/Shredit/AjxSuggest.aspx?q=" + searchKey + "&countrycode=" + setCountry('code') + "&country=" + setCountry('name');
        xmlhttp.open("GET", serverurl, true);
        xmlhttp.send();
    }
}


/* ========= Auto suggest functionality ends =========== */



