﻿// JScript File
//var m_IE = false;
//if(window.navigator.appName.toLowerCase().indexOf('explorer') != -1)
//{
//       m_IE = true;
//}
//document.onmousemove = mouseMove;
document.onmouseup = mouseUp;
//var m_mousePos = null;
var m_dragObject = null;
var m_mouseOffset = null;
var m_bDrag = true;
var m_textFocus = false;

function DoDrag(obj)
{
   OnFocus(obj.id);
}
function getMouseOffset(obj)
{
   return {x:doInt(m_mousePos.x) - doInt(obj.style.left), y:doInt(m_mousePos.y) - doInt(obj.style.top)};
}

function makeClickable(object)
{
    if( m_IE == true)
    {
        if(g_IEvertion < 7)
            return;
    }
   	object.onmousedown = function()
	{
	    OnFocus(m_maindiv.id);
		m_dragObject = this;
		m_mouseOffset = getMouseOffset(m_maindiv);
	}
}

function mouseUp(ev)
{
   	m_dragObject = null;
}

function mouseMove(ev)
{
    
    ev  = ev || window.event;
    m_mousePos = mouseCoords(ev);
	if(m_dragObject && m_bDrag && (m_textFocus == false))
    {
        m_maindiv.style.left = controlCoordsX();
		m_maindiv.style.top = controlCoordsY(); 
		return false;
	}
	
}
function controlCoordsX()
{
    
    if(m_IE == false)
    {
		return (doInt(m_mousePos.x) - m_mouseOffset.x) + 'px';
    }
   return (m_mousePos.x - m_mouseOffset.x);
}

function controlCoordsY()
{
    if(m_IE == false)
    {
		return (doInt(m_mousePos.y) - m_mouseOffset.y) + 'px';
    }
   return (m_mousePos.y - m_mouseOffset.y);
}
function mouseCoords(ev)
{
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}
function doInt(num)
{
    num = num + '';
    var n;
    while (num.length !=0)
    {
       n = num.length;
       if(num.charCodeAt(n - 1) > 57 || num.charCodeAt(n - 1) < 48) 
       {
            num  = num.substr(0,n-1) ;
       }
       else
       {
           return parseInt(num);
       }
   }
   return parseInt(num);
}
//
// data retrieving part
//
function OnTextBlur()
{
    m_textFocus = false;
    OnBlur();
}
function OnFocus(id)
{
    if(id == m_textctrl.id)//"Text1")
    {
        m_textFocus = true;
        if(m_textctrl.value.length != 0)
            m_bDrag = false;
    }
    //else if (id == "Select1" || id == "Select2" || id =="hrefButton1")
    else if (id == m_select1.id || id == m_select2.id || id == m_hrefButton1.id)
    {
        m_textFocus = true;
    }
    m_Focus = 1;
}
function OnBlur()
{
    //alert("blur");
    m_bDrag = true;
    m_Focus--;
    timedOut();
}
function Redirect(link)
{
    //debugger;
    if(link.length > m_statecodelength)
        window.location = link;
}
function EnableButton(selctrl)
{
    //m_button.disabled = false;
    m_textctrl.value = selctrl.options[selctrl.selectedIndex].text;
    m_hrefButton1.style.display = 'block';
}
function OnFocusOut()
{   
    if(m_Focus <=0)
    {
        m_Focus = 0;
        OffControl();
    }
}
function BeginX()
{
    if(m_IE == true)
    {   
        return doInt(m_mousePos.x) - 250;
    }
    return doInt(m_mousePos.x) - 250 + 'px';
}
function BeginY()
{
    if(m_IE == true)
        return doInt(m_mousePos.y) + 15;
    return doInt(m_mousePos.y) + 15 + 'px';
}
function Begin()
{
    //debugger;
    if(m_showcontrol == false)
    {
//        if(g_IEvertion == 6)// hide all critical controls for IE6 browser
//            ShowHideSearchForm(false);
        //m_hrefButton1.style.display = 'block';
        m_maindiv.style.display = 'block';
        makeClickable(m_maindiv);
        m_maindiv.style.top = BeginY();//m_mousePos.y ;
        m_maindiv.style.left = BeginX();//m_mousePos.x;
        //invisible second div for case when previous state was visible
        m_div2.style.display == 'none';
        m_select2.style.display = 'none';   
        // show close location text in link
        m_divlink1.style.display = 'none';
        m_divlink2.style.display = 'block';
        try
        {
            LoadStates();
        }
        catch(e)
        {
            OffControl();
            return;
        }
        //m_button.style.backgroundImage="url('Images/Go.gif')";
        m_textctrl.value = "";
        m_textctrl.focus();
        m_Focus = 1;
        m_showcontrol = true;
    }
    else
    {
        //document.getElementById(g_ScreenBlockerID).style.display = 'none'; this don't job in IE6
        //
        //show all critical for IE6 forms
//        if(g_IEvertion == 6)
//            ShowHideSearchForm(true);
        m_maindiv.style.display = 'none';
        m_divlink1.style.display = 'block';
        m_divlink2.style.display = 'none';
        m_div2.style.display == 'none';
        m_showcontrol = false;
    }
    m_select2.style.display = 'block';// this need for visible this control after display none obove 
    if(states.length == 1)
    {
        m_select1.value = state_codes[states[0]];
        Select1_onclick();
    }
    
}
function OffControl()
{
        //debugger;
        //document.getElementById(g_ScreenBlockerID).style.display = 'none';
//        if(g_IEvertion == 6)
//            ShowHideSearchForm(true);
        m_maindiv.style.display = 'none';
        m_divlink1.style.display = 'block';
        m_divlink2.style.display = 'none';
        m_div2.style.display == 'none';
        m_showcontrol = false;
        m_Focus = 0;
}
function CountyService()
{
    //debugger;
    this.GetStateCode = function(StateCode, array, servarray, ctrl, div)
    {
        var i = 0, j = 0;
        // find according counties from services array
        for(i = 0; i < array.length; i++)
        {
            //var str  = array[i].lastIndexOf(" ");
            var stcode = array[i].substring(array[i].lastIndexOf(" ") + 1, array[i].length);      
            if(stcode == StateCode)
            {
                if(servarray[array[i]] != null)
                {
                    ctrl.options[j] = new Option(array[i],servarray[array[i]]);   
                    j++;
                }
            }
        }
        if( j != 0 )
        {
            //if(div.id == "div2")
            if(div.id == m_div2.id)
                 SetSize(m_maindiv,m_bigheight);
            div.style.display = 'block';
        }
        ctrl.length = j;
    }
    this.GetCounties = function( text, countarr, servarray, select1, div)
    {
        ClearSelect(select1);
        var i = 0, j = 0;
        var begcounty;
        for( i = 0; i < countarr.length; i++)
        {
            begcounty = countarr[i].substring(0, text.length).toUpperCase();
            if(begcounty == text.toUpperCase())
            {
                select1.options[j] = new Option(countarr[i],servarray[countarr[i]]);  
                j++; 
            }
        }
        if(j != 0)
        {
            //alert(div.id);
            div.style.display = 'block';
        }
        else
        {
            //if(div.id = "div1")
            if(div.id == m_div1.id)
                SetSize(m_maindiv, m_verysmallheight);
            div.style.display = 'none';            
        }
        select1.length = j;
    }    
}

function onTextKeyup(textctrl)
{
    //debugger;
    m_hrefButton1.style.display = 'none';
    if(textctrl.value.length == 0)
    {
        m_div1.style.display = 'block';
        SetSize(m_maindiv,m_smallheight);
        LoadStates();
        if(states.length == 1)
        {
            m_select1.value = state_codes[states[0]];
            Select1_onclick();
        }
        return;
    }
    else
    {
        ClearSelect(m_select2);
        m_div2.style.display = "none";        
        SetSize(m_maindiv,m_smallheight);
        m_errorDiv.style.display = "block";
    }
    var ch = textctrl.value.charCodeAt(0);
    if (ch > 57 || ch < 48) // this is letter not digit
    {
        county.GetCounties(textctrl.value, count, name_services, m_select1, m_div1);
    }
    else
    {
        county.GetCounties(textctrl.value, cfipses, fips_services, m_select1, m_div1);
	}
}
function ClearSelect(selectctrl)
{
   //debugger;
   while(selectctrl.options[0] != null)
       selectctrl.options[0] = null;
}
function Select1_onclick()
{
   //debugger;
   var sel = m_select1.value;
   //alert(sel);
   if(sel.length == 0 || sel == null)
       return;
   if(sel.length > m_statecodelength)
   {
        //m_button.disabled = false; 
        m_textctrl.value = m_select1.options[m_select1.selectedIndex].text;
        m_hrefButton1.style.display = 'block';
   }
   else
   {
        //m_button.disabled = true; 
        m_hrefButton1.style.display = 'none';
   }
   county.GetStateCode(sel, count, name_services, m_select2, m_div2);      
}
function LoadStates()
{
   //debugger;
   m_hrefButton1.style.display = 'none'; 
   m_div2.style.display = 'none';
   m_div1.style.display = 'block';
   SetSize(m_maindiv,m_smallheight);
   var ctrl = m_select1;
   ClearSelect(ctrl);
   for(i = 0; i < states.length; i++)
   {
        ctrl.options[i] = new Option(states[i],state_codes[states[i]]);   
   }
   ctrl.length = i;  
}
// date processing class is CountyService
// create data processin object
var county = new CountyService();
// init control objects moved to server site InitContorl method
//// controls
//var m_button = document.getElementById('Button1');
//var m_textctrl = document.getElementById('Text1');
//var m_select1 = document.getElementById('Select1');
//var m_select2 = document.getElementById('Select2');
//// divisions
//var m_div1 = document.getElementById('div1');
//var m_div2 = document.getElementById('div2');
//var m_divlink1 = document.getElementById('link1');
//var m_divlink2 = document.getElementById('link2');
//var m_maindiv = document.getElementById('maindiv');
//var m_hrefButton1 = document.getElementById('hrefButton1');
//members and initialization

//m_maindiv.style.display = 'none';
var m_showcontrol = false;
var m_verysmallheight = '105px';
var m_smallheight = '180px';
var m_bigheight = '340px';
var m_bCountySelected = false;
var m_statecodelength = 3;
var m_Focus = 0;
function SetSize(ctrl,h)
{
    //ctrl.style.height = h;
}

function Button1_onclick()
{
    //debugger;
    if(m_div2.style.display == 'none')
    {
        Redirect(m_select1.value);
    }
    else
    {
        Redirect(m_select2.value);
    }
}

function timedOut()
{
    if(m_dragObject == null)
      var t=setTimeout("OnFocusOut()",300);
}


function clickCounty(cfips)
{
    //debugger;
    var service = fips_services[cfips.toUpperCase()];
    if (service==undefined)
    {
        alert("Service is Undefined");
        return;
    }
    Redirect(service);
}

