﻿var siteUrl = window.location.hostname;
siteUrl='http://'+siteUrl;

var xmlhttp;

function GetXmlHttpObject()
{
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    if (window.ActiveXObject)
    {
        // code for IE6, IE5
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
    return null;
}

String.prototype.trim = function () {return this.replace(/^\s*/, "").replace(/\s*$/, "");}
document.getElementById('txtCity').value='Type State';
function setBackText(obj)
{
    obj.style.fontSize='11px';obj.style.fontStyle='normal';obj.style.color='gray';
}
setBackText(document.getElementById('txtCity'));
function setFocus(obj, txt)
{
    if(obj.value.trim().toUpperCase()==txt.toUpperCase())
        obj.value='';
    obj.style.fontSize='14px';obj.style.fontStyle='Normal';obj.style.color='black';
    
}
function hideArea()
{
    if (document.getElementById("divArea").style.display=='inline')
    {
       document.getElementById("divArea").style.display='none';
    }
    else
    {
        
    }
}
function outFocus(obj, txt)
{
    if(obj.value.trim()=='')
    {
        obj.value=txt;
        setBackText(obj);
    }
}
    function ajaxClient()
    {
        var xmlhttp= null;
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
          return xmlhttp;
    }
    var areas=null;var existingCityId=0;
    function divCities()
    {document.getElementById('txtCity').value='Type State';setBackText(document.getElementById('txtCity'));document.getElementById('divcity').style.display='inline';showSeletedCity(this.event);document.getElementById('txtCity').focus();
    }
   
    function addCity(cityId,city)
    {
     var div= document.getElementById("divAllCities");
     div.scrollTop=0;
	document.getElementById('txtActCity').value=city;document.getElementById('divcity').style.display='none';
    if(existingCityId!=cityId){AddAjaxAreas(cityId,0);existingCityId=cityId;} 
     m=0; s=0;
    }
    var m=0; var start=0;var setScroll=0; var s=0;// variable for city drop down
    var n=0; var astart=0;var setAreaScroll=0;var firsttime=0 // variable for area drop down
    
     function closeCity()
    {
        document.getElementById("divAllCities").scrollTop=0;
        document.getElementById('divcity').style.display='none';
         m=0;s=0;  
    }
     function closeArea()
    {
        document.getElementById("divAllArea").scrollTop=0;
        document.getElementById('divArea').style.display='none';
        n=0;firsttime=0;
    }
function getElementsByName_iefix(tag, name) 
 {
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) 
     {
          att = elem[i].getAttribute("name");
          if(att == name) 
          {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}

function keyUpDownCity(e)
{
    var charcode=e.which || e.keyCode;
    var allcity=getElementsByName_iefix('span','allCity');
    if(charcode!=40 && charcode!=13 && charcode!=37 && charcode!=38 && charcode!=39 )
        {
          for(k=0;k<5;k++)
            {
                m=allcity[k].id;  
                break;
            }
            try
            { 
                m=m.substr(1);
                
            }
            catch(e)
            {
            }
            start=m;
            s=0;
          
           
        }
         //alert(m +'=='+setScroll);
         if(m>14)
         {
            var div= document.getElementById("divAllCities");
            //alert(div.scrollTop);
            setScroll=1;
            div.scrollTop=m*8+20;
         }
         else if(m<=14 && setScroll==1 && m>1)
         {
            var div= document.getElementById("divAllCities");
            if(((m*8)-20)>0)
            div.scrollTop=(m*8-20);
            else
            div.scrollTop=0;
         }
         //alert(m +'=='+setScroll);
            if(charcode==40)// for Down arrow
            {
         
                 for(i=0;i<cities.length;i++)
                 {
                   if(document.getElementById('c'+i))
                   document.getElementById('c'+i).style.backgroundColor='#FFFFFF';
                 }
              if(m==start && s==0)
              {
                       
                document.getElementById('c'+m).style.backgroundColor='#D0E3FF';
           
                s=1;  
              }
              else 
              {
                    if(s==1)
                    {
                       m=parseInt(m)+parseInt(2);                                
                        
                          if(document.getElementById('c'+m))
                          {
                             document.getElementById('c'+m).style.backgroundColor='#D0E3FF';                             
                          }
                          else
                          {
                              m=parseInt(m)-parseInt(1);
                              document.getElementById('c'+m).style.backgroundColor='#D0E3FF';                            
                          }
                    }
                    else
                    {
                         m=parseInt(m)+parseInt(2);
                         
                         if(document.getElementById('c'+m))
                          {
                             document.getElementById('c'+m).style.backgroundColor='#D0E3FF';                            
                          }
                          else
                          {
                              m=parseInt(m)-parseInt(2);
                              document.getElementById('c'+m).style.backgroundColor='#D0E3FF';                            
                          }  
                    }
              }

            }
             if(charcode==39)// for Right arrow
            {
           
                 for(i=0;i<cities.length;i++)
                 {
                   if(document.getElementById('c'+i))
                   document.getElementById('c'+i).style.backgroundColor='#FFFFFF';
                 }
               
                   if(m==parseInt(start)+parseInt(1))
                    {
                         document.getElementById('c'+m).style.backgroundColor='#D0E3FF';                       
                    }
                    else
                    {
                         m=parseInt(m)+parseInt(1);
                        if(document.getElementById('c'+m))
                         {
                            document.getElementById('c'+m).style.backgroundColor='#D0E3FF';
                         }
                         else
                         {
                             m=parseInt(m)-parseInt(1);
                             document.getElementById('c'+m).style.backgroundColor='#D0E3FF';
                         }
                         
                    }         
                    
                 
            }    
       
            if(charcode==37)// for Left arrow
            {
            
                for(i=0;i<cities.length;i++)
                 {
                   if(document.getElementById('c'+i))
                   document.getElementById('c'+i).style.backgroundColor='#FFFFFF';
                 }
                 
                 if(m==start)
                 {
                        m=start;
                        document.getElementById('c'+m).style.backgroundColor='#D0E3FF';
                 }
                 else
                 { 
                     m=parseInt(m)-parseInt(1);
                     document.getElementById('c'+m).style.backgroundColor='#D0E3FF';
                 }
            }
            if(charcode==38)// for Up arrow
            {
                 for(i=0;i<cities.length;i++)
                 {
                   if(document.getElementById('c'+i))
                   document.getElementById('c'+i).style.backgroundColor='#FFFFFF';
                  
                 }
                 
                     m=parseInt(m)-parseInt(2);
                     if(m<1)
                        {m=0;}
                     document.getElementById('c'+m).style.backgroundColor='#D0E3FF';
           }
           
          
             if(charcode==13)
             {
                  document.getElementById('c'+m).onclick();
             }
            
}
function keyUpDownArea(e)
{

    var charcode=e.which || e.keyCode;
    var allarea=getElementsByName_iefix('span','allAreas');
        if(charcode!=40 && charcode!=13 && charcode!=37 && charcode!=38 && charcode!=39 )
        {
         for(k=0;k<allarea.length;k++)
            {              
                 n=allarea[k].id;  
                 break;
            }           
              
            try
            {
             n=n.substr(1);
            }
            catch(e)
            {
            }
            firsttime=0;
            astart=n;
           
        }
       
      if(n>14)
         {
            var div= document.getElementById("divAllArea");
            setAreaScroll=1;
            div.scrollTop=n*10+20;
         }
         else if(n<=14 && setAreaScroll==1 && n>1)
         {
            var div= document.getElementById("divAllArea");
            if(((n*8)-20)>0)
            div.scrollTop=(n*8-20);
            else
            div.scrollTop=0;
         }
         
            if(charcode==40)// for Down arrow
            {
         
                 for(i=0;i<areas.length;i++)
                 {
                   if(document.getElementById('a'+i))
                   document.getElementById('a'+i).style.backgroundColor='#FFFFFF';
                 }
              if(n==astart && firsttime==0)
              {
             
                document.getElementById('a'+n).style.backgroundColor='#D0E3FF';
                 firsttime=1
              }
              else if(firsttime==1)
              {
           
                    if(n==parseInt(astart)+parseInt(1))
                    {
                         n=parseInt(n)+parseInt(2);
                        
                          if(document.getElementById('a'+n))
                          {
                             document.getElementById('a'+n).style.backgroundColor='#D0E3FF';
                          }
                          else
                          {
                              n=parseInt(n)-parseInt(1);
                              document.getElementById('a'+n).style.backgroundColor='#D0E3FF';
                          }
                   }
                    else
                    {
                         n=parseInt(n)+parseInt(2);
                        if(document.getElementById('a'+n))
                          {
                             document.getElementById('a'+n).style.backgroundColor='#D0E3FF';
                          }
                          else
                          {
                              n=parseInt(n)-parseInt(2);
                              document.getElementById('a'+n).style.backgroundColor='#D0E3FF';
                          }  
                    }
              }

            }
             if(charcode==39)// for Right arrow
            {
           
                 for(i=0;i<areas.length;i++)
                 {
                   if(document.getElementById('a'+i))
                   document.getElementById('a'+i).style.backgroundColor='#FFFFFF';
                 }
               
                   if(n==parseInt(astart)+parseInt(1))
                    {
                         document.getElementById('a'+n).style.backgroundColor='#D0E3FF';
                    }
                    else
                    {
                         n=parseInt(n)+parseInt(1);
                         if(document.getElementById('a'+n))
                         {
                             document.getElementById('a'+n).style.backgroundColor='#D0E3FF';
                         }
                         else
                         {
                            n=parseInt(n)-parseInt(1);
                            document.getElementById('a'+n).style.backgroundColor='#D0E3FF';

                         }   
                    }         
          }    
   
          if(charcode==37)// for Left arrow
            {
               for(i=0;i<areas.length;i++)
                 {
                   if(document.getElementById('a'+i))
                   document.getElementById('a'+i).style.backgroundColor='#FFFFFF';
                 }
                 if(n==astart)
                 {
                     n=astart
                     document.getElementById('a'+n).style.backgroundColor='#D0E3FF';
                 }
                 else
                 {
                     n=parseInt(n)-parseInt(1);
                     document.getElementById('a'+n).style.backgroundColor='#D0E3FF';
                 }
            }
            if(charcode==38)// for Up arrow
            {
                 for(i=0;i<areas.length;i++)
                 {
                   if(document.getElementById('a'+i))
                   document.getElementById('a'+i).style.backgroundColor='#FFFFFF';
                 }
                 n=parseInt(n)-parseInt(2);
                 if(n<0)
                 {n=0;}
                     
                     document.getElementById('a'+n).style.backgroundColor='#D0E3FF';             
            }
           
          
             if(charcode==13)
             {
                document.getElementById('a'+n).onclick();
             }
   
}
    function showSeletedCity(e)
    {
        var value = document.getElementById('txtCity').value.trim();
        var strHtml="";var note=0;
        var charcodee=0;
        if(e)
            charcodee=e.which || e.keyCode;
        if(charcodee!=40 && charcodee!=13 && charcodee!=37 && charcodee!=38 && charcodee!=39 )
        {
            if(charcodee==27)
            {
                 document.getElementById("divAllCities").scrollTop=0;
                 document.getElementById('divcity').style.display='none';
                 m=0;s=0; 
            }
            else
            {
                for(i=0;i<cities.length;i++)
                {
                    if(value.toUpperCase()!='TYPE STATE' && cities[i][1].length>=value.length && (cities[i][1].substring(0,value.length).toUpperCase()==value.toUpperCase()))
                    {
                        strHtml +="<span name='allCity' id='c"+i+"' onclick=\'addCity("+cities[i][0]+",\""+cities[i][1]+"\")\'>"+cities[i][1]+"</span>";
                        note=1;
                    }
                    else if(value=='' || value.toUpperCase()=='TYPE STATE')
                    {
                        strHtml +="<span name='allCity' id='c"+i+"' onclick=\'addCity("+cities[i][0]+",\""+cities[i][1]+"\")\'>"+cities[i][1]+"</span>";
                        note=1;
                    }
                }
                if(note==0)
                    strHtml="Sorry no matching states found";
                document.getElementById('divAllCities').innerHTML=strHtml;
                if(e)keyUpDownCity(e);
            }
        }
         else
            keyUpDownCity(e);
        
    }
    
    function divArea()
    {
        if(document.getElementById('txtHdnCityId').value!="0")
        {
            if(areas==null || areas.length==0)
                AddAjaxAreas(document.getElementById('txtHdnCityId').value,0);
            document.getElementById('txtTempArea').value='Type Area';setBackText(document.getElementById('txtTempArea'));document.getElementById('divArea').style.display='inline';document.getElementById('divcity').style.display='none';showSeletedArea(this.event);
            document.getElementById('txtTempArea').focus();
        }
        else
            alert("Please select any state");
    }
    
    function addArea(areaId,area)
    {
    
     document.getElementById("divAllArea").scrollTop=0;
     document.getElementById('txtHdnAreaId').value=areaId;document.getElementById('txtArea').value=area;
     document.getElementById('divArea').style.display='none'; 
     n=0;firsttime=0;
     
     }
      
    function AddAjaxAreas(cityId,note)
    {
        var xmlRequest= GetXmlHttpObject();
        //document.getElementById('loading').style.display='inline';
        xmlRequest.onreadystatechange=function(){
          if (xmlRequest.readyState==4 && xmlRequest.status==200)
          {var xmlDoc= xmlRequest.responseXML;
          areas=xmlDoc.getElementsByTagName("area");
            if(note==0)
            {
                document.getElementById('txtHdnAreaId').value='0';document.getElementById('txtArea').value='Any Area';
            }
            else
            {
                if(document.getElementById('txtHdnAreaId').value!='0')
                {
                    if(areas!=null && areas.length>0)
                    {
                        for(j=0;j<areas.length;j++)
                        {
                            if(areas[j].getAttribute("id")==document.getElementById('txtHdnAreaId').value)
                            {
                                document.getElementById('txtArea').value=areas[j].getAttribute("name");
                                break;
                            }
                        }
                    }
                }
            }
            showSeletedArea(this.event);
            document.getElementById('loading').style.display='none';
          }
        }
        xmlRequest.open("GET",siteUrl+"ajax/ajax.ashx?cityId="+cityId+"",true);
        xmlRequest.send(null);
    }
     function showSeletedArea(e)
    {
        
            var value = document.getElementById('txtTempArea').value.trim();
            var strHtml="";var note=0;
             var charcodee=0;
            if(e)
                charcodee=e.which || e.keyCode; 
          if(charcodee!=40 && charcodee!=13 && charcodee!=37 && charcodee!=38 && charcodee!=39 )
            {
                 if(charcodee==27)
                {
                     document.getElementById("divAllArea").scrollTop=0;
                     document.getElementById('divArea').style.display='none';
                     n=0;firsttime=0;
                }
                else
                {
                        if(areas!=null)
                        {
                        
                                for(i=0;i<areas.length;i++)
                                {
                                    if(value.toUpperCase()!='TYPE AREA' &&  areas[i].getAttribute("name").length>=value.length && (areas[i].getAttribute("name").substring(0,value.length).toUpperCase()==value.toUpperCase()))
                                    {
                                        strHtml +="<span name='allAreas' id='a"+i+"' onclick=\'addArea("+areas[i].getAttribute("id")+",\""+areas[i].getAttribute("name")+"\")\'>"+areas[i].getAttribute("name")+"</span>";
                                        note=1;
                                    }
                                    else if(value==''|| value.toUpperCase()=='TYPE AREA')
                                    {
                                        strHtml +="<span name='allAreas' id='a"+i+"' onclick=\'addArea("+areas[i].getAttribute("id")+",\""+areas[i].getAttribute("name")+"\")\'>"+areas[i].getAttribute("name")+"</span>";
                                        note=1;
                                    }
                                }
                           }
                   
                        if(note==0)
                        strHtml="Sorry no matching area found";
                        document.getElementById('divAllArea').innerHTML=strHtml; 
                        if(e)keyUpDownArea(e);
                  }
            }
            else
            keyUpDownArea(e)
     }   
        
        showSeletedCity(this.event);       
        
        if((navigator.appName).toUpperCase()=="OPERA")
        {
            document.getElementById('divcity').style.left='470px';
            document.getElementById('divArea').style.left='680px';
        }
        
        
        <!-- Copyright 2006,2007 Bontrager Connection, LLC
// http://bontragerconnection.com/ and http://willmaster.com/
// Version: July 28, 2007

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
function HideContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "none";
}
