﻿// JScript File
var gdiv_id;
var gcontrolid;
var gtblid;
var gSelectedCountry='';

window.onload = clicktoHide;

var IE = document.all ? true : false;
if (!IE) document.captureEvents(Event.MOUSEMOVE)
document.onmousemove = getMouseXY;
var tempX = 0;
var tempY = 0;
function getMouseXY(e) {
    
    if (IE) { // grab the x-y pos.s if browser is IE
        tempX = event.clientX + document.body.scrollLeft;
        tempY = event.clientY + document.body.scrollTop;
    }
    else {  // grab the x-y pos.s if browser is NS
        tempX = e.pageX;
        tempY = e.pageY;
    }
    if (tempX < 0) { tempX = 0; }
    if (tempY < 0) { tempY = 0; }

    return true;
}

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 clicktoHide() 
{
    document.rootnode = document.documentElement;

    if (document.attachEvent && document.compatMode != 'CSS1Compat') 
    {
        document.rootnode = document.body;
    }
    document.rootnode.onclick = function(e) {
        e = window.event || e;

        var obj = document.getElementById(gdiv_id);
        var topValue = 0, leftValue = 0, width = 0, height = 0, bottomright = 0, topright = 0;

        if (obj) {
                        leftValue += findPosX(obj);
                        topValue += findPosY(obj) -25;
            
            if (IE) {
                width += obj.style.pixelWidth;

            }
            else {
                width += obj.offsetWidth;
            }
            if (IE) {
                height += (obj.style.pixelHeight + 25);

            }
            else
            { height += (obj.offsetHeight + 25); }

            topright = leftValue + width;
            bottomright = topValue + height;

            obj = obj.offsetParent;

            if (document.getElementById(gdiv_id).style.display == '') {
                if (!
                    ((tempX > leftValue) &&
                    (tempX < topright) &&
                    (tempY > topValue) &&
                    (tempY < bottomright))
                    ) {

                    document.getElementById(gdiv_id).style.display = 'none';

                    if (gSelectedCountry != '') {
                        SelectCountry("none");
                    }
                    
                    HideCounties(gtblid, gcontrolid, gdiv_id);
                    //Apply Height
                    ApplyHeight(gdiv_id, gtblid);

                }
                else {

                    //Apply Height
                    ApplyHeight(gdiv_id, gtblid);
                }
            }
        }
    }    
} 

String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}   

//Will show or hide the div
function show(id, tblid, controlid, divid) {
    
     gdiv_id = divid;
     gcontrolid = controlid;
     gtblid=tblid;

     var tbl = document.getElementById(tblid);
     
        el = document.getElementById(id);
        
        if (el.style.display == 'none')
        {   
            el.style.display = '';            
        }
            
        else {
            el.style.display = 'none';

            if (gSelectedCountry != '') {                
                SelectCountry("none");
            }
            
            HideCounties(tblid, controlid, divid);
        }
        //Apply Height
        var div = id.split('_')[3];
        if(div!='divHelp')
            ApplyHeight(id,tblid);
    }

    function HideCounties(tblid, controlid, divid) {
    
        var div = document.getElementById(divid);
        var tbl = document.getElementById(tblid);

        
        var i;
        var tbllen = document.getElementById(tblid);

        for (i = 0; i < tbllen.rows.length; i++) 
        {
            if (tbllen.rows[i].id != 'searchcounties_header') 
            {
                var countycountry = tbllen.rows[i].id.split('_');
                if (countycountry.length == 4)
                 {                    
                    var c = controlid + '_' + countycountry[3] + '_counties';
                    var el = document.getElementById(c);
                    el.style.display = 'none';
                }
            }
        }
    }

    //Will be called when country or county is selected and will set its name
    function SelectCounty(countyname, countyid, countyn, countyi, controlid, tblid,divid) 
    {
        var county = document.getElementById(countyname);
        
        var hdncountyname = document.getElementById(countyn);
        
        var hdncountyid = document.getElementById(countyi);

        
        if(IE)
            var s = new String(county.innerText);
        else
            var s = new String(county.textContent);
        
        // use it like this
        s = s.trim();

        hdncountyname.value = s;
        
        hdncountyid.value = countyid.split('_')[2];
        var c = controlid + '_tdSelectCounty';
        var tdSelectCounty = document.getElementById(c);
        if(IE)
            tdSelectCounty.innerText = s;
        else
            tdSelectCounty.textContent = s;
        var c1 = controlid + '_ddlMain';
        show(c1, tblid, controlid, divid);        
        return false;
        
        
    }

    function showcounties(id, controlid, divid, tblid) {
      
        var div = document.getElementById(divid);
        var tbl = document.getElementById(tblid);
        
        var country=id.split('_');        
        var countryname=country[3];        
        var i;
        var tbllen = document.getElementById(tblid);
        
        for(i=0;i<tbllen.rows.length;i++)
        {       
            if(tbllen.rows[i].id!='searchcounties_header')
            {
                var countycountry = tbllen.rows[i].id.split('_');
                if(countycountry.length==4)
                {
                    if(countycountry[3]==countryname)
                    {
                        var c=controlid + '_' + countycountry[3] + '_counties';
                        var el = document.getElementById(c);                        
                        el.style.display='';                        
                    }
                    else
                    {
                        var c=controlid + '_' + countycountry[3] + '_counties';
                        var el = document.getElementById(c);
                        el.style.display='none';
                    }
                }
            }
        }
    }

    function ChangeSelectCountyStyle(aid, backcolor, color, trid) 
    {

        var acounty = document.getElementById(aid);
        var trcounty = document.getElementById(trid);
        
        var backgroundcolor=document.getElementById(backcolor).value;
        var forecolor=document.getElementById(color).value;
        
        acounty.style.backgroundColor = forecolor;
        acounty.style.color = backgroundcolor;

        trcounty.style.backgroundColor = forecolor;
        trcounty.style.color = backgroundcolor;
    }
    function ChangeSelectCountyStyleOriginal(id,backcolor,color,trid) {

        var acounty = document.getElementById(id);
        var trcounty = document.getElementById(trid);
        
        var backgroundcolor=document.getElementById(backcolor).value;
        var forecolor=document.getElementById(color).value;
        
        acounty.style.backgroundColor = backgroundcolor;
        acounty.style.color = forecolor;

        trcounty.style.backgroundColor = backgroundcolor;
        trcounty.style.color = forecolor;
    }
    
    //Apply Height
    function ApplyHeight(divid,tblid)
    {   
        var div=document.getElementById(divid);
        var tbl=document.getElementById(tblid);

        if (tbl.clientHeight <= 205) {
            if (IE)
                div.style.height = tbl.clientHeight+10;
            else
                div.style.height = tbl.clientHeight;
        }
        else {
            if (IE)
                div.style.height = "215px"; 
            else
                div.style.height = "205px"; 
        }
    }

    function ChangeSelectCountryStyle(id) {

        if (gSelectedCountry == '') 
        {
            gSelectedCountry = id;
            SelectCountry("underline");
        }
        else 
        {        
            SelectCountry("none");            
            gSelectedCountry = id;
            SelectCountry("underline");
        }
    }
    
    function  SelectCountry(style)
    {
        var href = document.getElementById(gSelectedCountry);
        href.style.textDecoration = style;
    }