﻿   var Index = 0;
   var selectValue = false;
   var t = null;
   var form_element = null;
   var result_container = null;
   var px="cc_"; 
   var cityMes='type a city name here...';
    if (!String.prototype.addQuery)
    {
        String.prototype.addQuery = function(params)
        {
            var concat = this.indexOf('?') == -1 ? this + '?' : this;
            for (var property in params)
            {
                if (typeof (params[property]) != 'object' && params[property] !== undefined && params[property] !== null && params[property].toString().length > 0)
                    concat += (concat.indexOf('?') == concat.length - 1 ? '' : '&') + property + '=' + params[property];
            }
            return concat;
        };
    }
    function ccOnFocus(cityCountryControl)
    {
        if ( cityCountryControl.value == cityMes )
        {
            cityCountryControl.value = "";
        }
    }
    function inputOnBlur(cityCountryControl)
    {   
        if ( cityCountryControl.value == "" )
        {
            cityCountryControl.value = cityMes;
        }
       if (selectValue == false)
          searchHide();
    }
    function searchHide(){
        if ( result_container != null )
            result_container.innerHTML = "";
    }
    
    function searchStart(e, element, containterId){
       document.getElementById(hCity).value = "";
        document.getElementById(hCountry).value = "";
        result_container = document.getElementById(containterId);

        if(e.keyCode == 27) {//ESC
            searchHide();
            return false;
        }
        else if ((e.keyCode == 8) || (e.keyCode == 46)) {//"backspace" & "del"           
            t = window.setTimeout("searchDoSearch()",200);
        }    
    
        if(e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 9 
            && e.keyCode != 40 && e.keyCode != 38 && e.keyCode != 13) {//"tab","down arr","up arr","enter"
            if (t) { window.clearTimeout(t);}
            form_element = document.getElementById(element);           
            if ( form_element.value.length > 1 )
            {
                Index=0;
                t = window.setTimeout("searchDoSearch()",200);
            }
        } 
         key = e.keyCode;
         if(key == 13)
         { 
            d=parseInt(Index);
            div = document.getElementById(px+d); 
            if(div!=null) 
            {
                selectThis(div);
                return false;
            }
          }   
          return true;
    }
    function searchDoSearch(){
        var params = '';
        form_element=document.getElementById(cityCountry);
        params = params.addQuery({method:'CitySearch',q:form_element.value});
        
        citySearchClientCall(endSearch,params);
    }
    function endSearch(resp)    {        
        result_container.innerHTML = resp;
    }
    var tmpCityId = "";
  

    
    function citySearchClientCall(callBackFunction, params, aditionalMethod){
        var object = typeof (params) == 'object' && params !== null;
        var string = typeof (params) == 'string';
        var url = object ? citySearchUrl.addQuery(params) : string ? citySearchUrl + params : citySearchUrl;

        var rnd = { r: Math.random() }

        url = url.addQuery(rnd);
       

        var oXMLHttpRequest = new XMLHttpRequest();
        oXMLHttpRequest.open("GET", url, true);
        oXMLHttpRequest.onreadystatechange = function(){
            if (this.readyState == 4){
                if (callBackFunction)
                   callBackFunction(this.responseText);
                    
                if ( aditionalMethod )
                    aditionalMethod();
            }
            return false;
        };
        oXMLHttpRequest.send(null);
        
    }
    function hoverIn(el){
        el.className = "citySearchHighlight";
    }
    function hoverOut(el){
         el.className = "citySearchRow";
    }
    function selectThis(el){
        form_element.value = el.innerHTML;
        document.getElementById(hCity).value    = el.attributes['cityId'].value;
        document.getElementById(hCountry).value = el.attributes['countryId'].value;
        hoverOut(el);
        searchHide();   
    }
 
    function clearText(){
        document.getElementById(cityCountry).value = "";
    }
    
    
function selectItem(keycode) {
    if (resultCount > 0) {
        if (keycode == 40 && keyIndex < (resultCount - 1))
            keyIndex++;
        else if (keycode == 38 && keyIndex > 0)
            keyIndex--;

        var tags = result_container.getElementsByTagName("div");
        if (tags != null && tags.length > 2) {
            for (i = 2; i < tags.length; i++) {
                if ((i - 2) == keyIndex) {
                    hoverIn(tags[i]);
                    scrollIntoView(tags[i]);
                    tags[i].focus();
                }
                else
                    hoverOut(tags[i]);
            }

        }
    }
}

 
function scrollIntoView(target) {
    var container = target.parentNode, scrollTop = container.scrollTop, scrollBottom = scrollTop + container.offsetHeight, targetTop = target.offsetTop, targetBottom = targetTop + target.offsetHeight;
    if (targetBottom > scrollBottom) {
        container.scrollTop += 50 + targetBottom - scrollBottom;
    }
    else if (targetTop < scrollTop) {
        container.scrollTop += targetTop - scrollTop - 50;
    }
}

function OnArrowKeyPress(e, element)
{
 var key; //if(window.event)key = window.event.keyCode; else key = event.which;  
 key = e.keyCode;
 if(key >= 37 && key <= 40){
  divCount = CountElementsById(px); 
  
  if(key ==40){//down")
  
   div      = document.getElementById(px + (parseInt(Index, 10) + 1));
   prevDiv  = document.getElementById(px+ parseInt(Index, 10));
   
   if (Index!=divCount)
   {
       if(prevDiv!=null) hoverOut(prevDiv); 
       
       if (Index > 10)
       {
            if (div!=null)
                div.parentNode.parentNode.parentNode.scrollTop += div.clientHeight;
       }

        Index++;
       
       if(div!=null) hoverIn(div); 
   }      
  }
  else
   if(key ==38){    
   //up
   if (Index!=1&&Index!=0)
   {
     div = document.getElementById(px +parseInt((Index-1), 10));
     prevDiv=document.getElementById(px+Index);
   
    if(prevDiv!=null) 
        hoverOut(prevDiv); 
    
    if ((Index-1) > 10)
    { 
        if (div!=null)
            div.parentNode.parentNode.parentNode.scrollTop -= div.clientHeight;
    }

    Index--;
    
    if(div!=null) 
        hoverIn(div); 
    }
  }
 } 
 if(key == 13)
 {
    d=parseInt(Index);
    div = document.getElementById(px+d); 
    if(div!=null) 
    {
        
        selectThis(div);
        return false;
    }
  }  
  return true;
}

function CountElementsById(code){count = 0; for(idx=1; idx<500; idx++){div = document.getElementById(code +idx); if(div != null)count++; else return count;}}


function GetCountriesForSite(){
   var ddlCountries = document.getElementById("ddlCountries");
    ddlCountries.options.length = 0;
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    } 
    var baseURL = location.href.substring(0, location.href.indexOf("Admin/Forms/"));
    if(baseURL == '')
        baseURL = location.href.substring(0, location.href.indexOf("Forms/"));
    var url = baseURL + "Forms/SelectDestination.aspx";
    url=url+"?ctr=ctrforsite";
    url=url+"&sid="+Math.random();
    xmlHttp.onreadystatechange=stateChangedCountry;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function stateChangedCountry (){
    if (xmlHttp.readyState==4){
        FillCountriesForSite(xmlHttp.responseText);
    }
}

function stateChangedCity (){
    if (xmlHttp.readyState==4){
        FillCitiesSO(xmlHttp.responseText);
    }
}

function FillCountriesForSite (response){
    var strOptions = response;
    var inHtml = '<select class="inputSelect" style="width: 180px;" id="ddlCountries" onchange="GetCitiesByCountrySO(this.value);">';
    inHtml += response;    
    inHtml +='</select>';
    document.getElementById("divCountries").innerHTML = inHtml;
}

function FillCitiesSO (response){
    var strOptions = response;
    strOptions = strOptions.replace('<Options>','');
    strOptions = strOptions.replace('</Options>','');
    strOptions = strOptions.replace('<?xml version="1.0" encoding="utf-8"?>','');
    var inHtml = '<select class="inputSelect" style="width: 180px;" id="ddlCities">';
    inHtml += strOptions;
    inHtml +='</select>';
    document.getElementById("divCities").innerHTML = inHtml;
}

function GetCitiesByCountrySO(countryIso){
var ddlCities = document.getElementById("ddlCities");
    ddlCities.options.length = 0;
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
    {
        alert ("Your browser does not support AJAX!");
        return;
    } 
    var baseURL = location.href.substring(0, location.href.indexOf("Admin/Forms/"));
    if(baseURL == '')
        baseURL = location.href.substring(0, location.href.indexOf("Forms/"));
    var url = baseURL + "Forms/SelectDestination.aspx";
    url=url+"?ciso="+countryIso;    
    xmlHttp.onreadystatechange=stateChangedCity;
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}

function w2cs22(wnd) {
    var pageW = null;
    var pageH = null;

    var wndH = wnd.offsetHeight;
    var wndW = wnd.offsetWidth;

    wnd.style.position = "absolute";

    if (window.innerHeight != null) {
        pageH = window.innerHeight;
        pageW = window.innerWidth;
    }
    else if (document.documentElement.clientHeight != null) {
        pageH = document.documentElement.clientHeight;
        pageW = document.documentElement.clientWidth;
    }
    else if (document.body.clientHeight != null) {
        pageH = document.body.clientHeight;
        pageW = document.body.clientWidth;
    }

    if (pageH == 0 && pageW == 0) {
        pageH = document.body.clientHeight;
        pageW = document.body.clientWidth;
    }
    if (wndH > pageH) {
        wnd.style.top = document.body.scrollTop + "px";
    }
    else {
        wnd.style.top = ((pageH - wndH) / 2) +document.body.scrollTop + document.documentElement.scrollTop + "px";
    }
    wnd.style.left = (pageW - wndW) / 2 + "px";
}



function CloseMeOk(url){ 
     var divElem=document.getElementById('myBackGroundMessageTable');
     var divElem2=document.getElementById('myBackGroundDiv');
     if ( divElem.parentNode != null && divElem2.parentNode != null)
     {
        divElem.parentNode.removeChild(divElem);
        divElem2.parentNode.removeChild(divElem2);
        if (typeof(url)!='undefined')
        {
            window.location=url;
        }
     }
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	try	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)	{
		// Internet Explorer
		try		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}


