﻿// JScript File

var arrId;
var arrNm;
//var __cc_val;
var __cc_ind;
var __strDefaultCCVal="Please Select / Type country name";
//var __cc_id;

function makeCntryComboDiv(i_strDivId,i_strDisp,i_strPos,i_zIndex,i_bgColor)
{
    var objDiv=document.createElement('div');
    objDiv.setAttribute('id', i_strDivId);
    objDiv.style.display = i_strDisp;
    objDiv.style.position = i_strPos;
    objDiv.style.zIndex = i_zIndex;
    objDiv.style.backgroundColor=i_bgColor;
    
    var oBody = document.getElementsByTagName('body')[0];
    oBody.appendChild(objDiv);
    
    return objDiv;
    
}
function createAFilledDiv()
{
    var objCcLeft=document.getElementById("cc_image");
    var xPos=findPosX(objCcLeft)+6;
    var yPos=parseInt(findPosY(objCcLeft))+parseInt(objCcLeft.offsetHeight);
    
    var objFullCombo=makeCntryComboDiv("div_full_cbo_cntry","none","absolute",1052,"White");  
    var wd=235;
    var top = yPos;
    var left = xPos;
    
    objFullCombo.style.top = (top < 0 ? 0 : top) + 'px';
    objFullCombo.style.left = (left < 0 ? 0 : left) + 'px';
    objFullCombo.style.width = wd + 'px';
    objFullCombo.style.border="1px solid #B9BABC";
    objFullCombo.className="DIV_SCROLLS";
    
    objFullCombo.innerHTML=getCountriesTableHtml("","C");
    //objFullCombo.onmouseout=removeComboDiv; 
}

function changeCntryCombo(ind)
{
    
    
    var objCcLeft=document.getElementById("cc_image");
    var xPos=findPosX(objCcLeft)+6;
    var yPos=parseInt(findPosY(objCcLeft))+parseInt(objCcLeft.offsetHeight);
    
    __cc_ind=ind;
    document.onclick=removeComboDiv;
    
    if(ind=="C")
    {
        removeElement("div_cbo_cntry");
        
        if(document.getElementById("txt_cc").value=="")
        {
            document.getElementById("txt_cc").value=__strDefaultCCVal;   
            document.getElementById("td_cc_flag").innerHTML="";
        }
        
        if(document.getElementById("div_full_cbo_cntry").style.display=="block")
        {
            slideUpCountryDiv();
        }
        else
        {
           $("#div_full_cbo_cntry").slideDown("slow"); 
        }
        return;
    }
    else if(ind=="T")
    {
       removeElement("div_cbo_cntry"); 
       document.getElementById("div_full_cbo_cntry").style.display='none';
    }  
        
    /*if(document.getElementById("div_cbo_cntry"))
    {      
        //removeElement("div_cbo_cntry");
        if(ind=="C")
        {
            if(document.getElementById("txt_cc").value=="")
            {
                document.getElementById("txt_cc").value=__strDefaultCCVal;   
                document.getElementById("td_cc_flag").innerHTML="";
            }
            //$("#div_cbo_cntry").slideDown("slow");
            return;
        }
        else if(ind=="T")
        {
           removeElement("div_cbo_cntry"); 
        }  
    }*/
    
    var objCombo=makeCntryComboDiv("div_cbo_cntry","block","absolute",1052,"White");

    var wd=235;
    var top = yPos;
    var left = xPos;
    
    objCombo.style.top = (top < 0 ? 0 : top) + 'px';
    objCombo.style.left = (left < 0 ? 0 : left) + 'px';
    objCombo.style.width = wd + 'px';
    objCombo.style.border="1px solid #B9BABC";
    objCombo.className="DIV_SCROLLS";
       
    if(document.getElementById("txt_cc").value==__strDefaultCCVal)
         document.getElementById("txt_cc").value="";   
    
    var val=document.getElementById("txt_cc").value;
           
    __cc_val=val;
    __cc_ind=ind;
    /*if(ind=="C")
    {
        $("#div_full_cbo_cntry").slideDown("slow");
        document.getElementById("div_cbo_cntry").onmouseout=removeComboDiv; 
        //objCombo.innerHTML="<table width='100%'><tr align='center'><td align='center'><img src='images/website/process.gif' alt='Loading' /></td></tr></table>";
        //setTimeout(callFillCombo,2000);
    }
    else if(ind=="T")
    {
       __cc_id="div_cbo_cntry";
       callFillCombo();  
    }*/
    fillCombo(val,ind);
    //document.getElementById("div_cbo_cntry").onmouseout=removeComboDiv; 
 
}

/*function callFillCombo()
{
   fillCombo(__cc_val,__cc_ind); 
   document.getElementById("div_cbo_cntry").onmouseout=removeComboDiv; 
}*/
function fillCombo(value,ind)
{	
	var objCombo=document.getElementById("div_cbo_cntry"); 
    objCombo.style.display='none'
    
    var strHtml=getCountriesTableHtml(value,ind);
    if(strHtml=="") return;
    objCombo.innerHTML=strHtml;
    $("#div_cbo_cntry").slideDown("slow");

}


function getCountriesTableHtml(value,ind)
{
    arrId=strId.split("^"); // strId is declared globally homepage.aspx
	arrNm=strNm.split("^"); // same as above
	
	var count=0;
	var sH="";
	var outerH="";
	var img="";
		
	for(var i=0;i<arrNm.length;i++)
	{
		var temp=arrNm[i];
		var img;
		if(arrId[i].toLowerCase()=="all")
		    img="images/website/spacer.gif";
		else
		    img="images/Flags/"+arrId[i]+"_flag.gif";
		    
		if(value=="" || ind=="C")
		{
			sH += "<tr style='height:20px'>";
			sH += "<td id='td_cc_option' style='width:235px;border-bottom:1px solid #EFEFEF;vertical-align:middle;cursor:pointer' class='COUNTRY_COMBO_TXT' align='left' onmouseover='this.style.backgroundColor=\"#D8E3F2\"' onmouseout='this.style.backgroundColor=\"White\"' onclick='updateCountry(\""+arrId[i]+"\",\""+arrNm[i]+"\")'>";
			sH += "&nbsp;<img id='img_cc_option' src='"+img+"' style='width:16px;height:11px' alt='"+arrNm[i]+"' title='"+arrNm[i]+"'/>&nbsp;&nbsp;"+arrNm[i];
			sH += "</td>";
			sH += "</tr>";
			
			count++;
		}
		else if(temp.startsWith(value))
		{	
		
			sH += "<tr style='height:20px'>";
			sH += "<td id='td_cc_option' style='width:235px;border-bottom:1px solid #EFEFEF;vertical-align:middle;cursor:pointer' class='COUNTRY_COMBO_TXT' align='left' onmouseover='this.style.backgroundColor=\"#D8E3F2\"' onmouseout='this.style.backgroundColor=\"White\"' onclick='updateCountry(\""+arrId[i]+"\",\""+arrNm[i]+"\")'>";
			sH += "&nbsp;<img id='img_cc_option' src='"+img+"' style='width:16px;height:11px' alt='"+arrNm[i]+"' title='"+arrNm[i]+"'/>&nbsp;&nbsp;"+arrNm[i];
			sH += "</td>";
			sH += "</tr>";
			
			count++;
		}
	}
	
	if(count==0) return "";
	
	if(count>9)
	{
		outerH="<div style='width:235px;height:245px;overflow:auto'><table style='width:100%'>";
	}
	else
	{
		outerH="<div style='width:235px;'><table style='width:100%'>";
	}
	
	outerH += sH+"</table></div>";
	
	return outerH;
}

function removeComboDiv(e)
{
   /* var toElement = null;
    
    if(!e) var e=window.event;
    
    if (e.relatedTarget)
        toElement = e.relatedTarget;
    else if (e.toElement)
        toElement = e.toElement;
    
    while (toElement && toElement.tagName != "DIV")
        toElement = toElement.parentNode;
    
    if(toElement)
    {    
        if (toElement.id!="")
        {
            //removeElement("div_cbo_cntry"); 
            setTimeout(slideUpCountryDiv,1000);
        }
    }*/
    
    /**************************************************************/
    
    if(!e) e=window.event;
    
    var target;
    
    if(e.srcElement) {target=e.srcElement;} // IE
    else if(e.target) {target=e.target;} // All other standard browsers
    
    //alert(target.id);
    if(target.id != "img_cc_option" && target.id != "td_cc_option" && target.id != "img_cc_drop_down_btn")
    {
        slideUpCountryDiv();
        document.onclick=null;
    }
}

function slideUpCountryDiv()
{
   if(__cc_ind=="T") 
       $("#div_cbo_cntry").slideUp("slow"); 
   else if(__cc_ind=="C")
       $("#div_full_cbo_cntry").slideUp("slow");  
}

function updateCountry(strCntryId,strCntryNm)
{
    //document.getElementById("hidCntryId").value=strCntryId;
    
    //************* SET COOKIE *******************//
    
    //First delete the cookie
    var dt=new Date();
    dt.setTime(dt.getTime()-1);
    
    document.cookie="cntry_id="+strCntryId+"; expires="+dt.toGMTString();    
    
    //Now set the cookie
    document.cookie="cntry_id="+strCntryId;

    //*******************************************//
    
    document.getElementById("txt_cc").value=strCntryNm;
    
    var objTDflag=document.getElementById("td_cc_flag");
    
    if(strCntryId.toLowerCase()=="all")
        objTDflag.innerHTML="<img src='images/website/spacer.gif' width='16' height='12' />";
    else
        objTDflag.innerHTML="<img src='images/Flags/"+strCntryId+"_flag.gif' width='16' height='12' />";
    
    var strPageName=""; 
    //removeElement("div_cbo_cntry"); 
    slideUpCountryDiv();
    
    
    //MAP
    fnUpdateCntryProgress("div_map");
    GetAjaxData("HomePage.aspx?QccAjax=Y&QccAjaxSub=MAP&QgblCntryId="+strCntryId+"&now="+Math.floor(Math.random()*1000),update_map);
    
    //NEWS
    fnUpdateCntryProgress("slide_table_parent_news");
    GetAjaxData("WebPages/News.aspx?QSrc=HOME&QNewsPb=Y&QccAjax=Y&QgblCntryId="+strCntryId+"&now="+Math.floor(Math.random()*1000),update_news);
    
    //CONSULATES
    //fnUpdateCntryProgress("slide_table_parent_consulates");
    //GetAjaxData("WebPages/Consulate.aspx?QccAjax=Y&QgblCntryId="+strCntryId+"&now="+Math.floor(Math.random()*1000),update_consulates);
    
    //FORMS
    fnUpdateCntryProgress("slide_table_parent_forms");
    GetAjaxData("WebPages/Forms.aspx?QSrc=HOME&QFormsPb=Y&QccAjax=Y&QgblCntryId="+strCntryId+"&now="+Math.floor(Math.random()*1000),update_forms);
    
    //LINKS
    //fnUpdateCntryProgress("links_content");
    //GetAjaxData("HomePage.aspx?QccAjax=Y&QccAjaxSub=LINKS&QgblCntryId="+strCntryId+"&now="+Math.floor(Math.random()*1000),update_links);
}

function update_map(strData)
{
    //alert(strData);
    removeElement("load_div_map");
    var objDiv=document.getElementById("div_map");
    var id="#"+objDiv.id;
    objDiv.innerHTML=strData;
    objDiv.style.display='none';
    $(id).fadeIn("normal");
    
}

function update_news(strData)
{
    removeElement("load_slide_table_parent_news");
    var objDiv=document.getElementById("slide_table_parent_news");
    var id="#"+objDiv.id;
    objDiv.innerHTML=strData;
    objDiv.style.display='none';
    $(id).fadeIn("normal");
}

/*function update_consulates(strData)
{
    removeElement("load_slide_table_parent_consulates");
    var objDiv=document.getElementById("slide_table_parent_consulates");
    var id="#"+objDiv.id;
    objDiv.innerHTML=strData;
    objDiv.style.display='none';
    $(id).fadeIn("normal"); 
}*/

function update_forms(strData)
{
    removeElement("load_slide_table_parent_forms");
    var objDiv=document.getElementById("slide_table_parent_forms");
    var id="#"+objDiv.id;
    objDiv.innerHTML=strData;
    objDiv.style.display='none';
    $(id).fadeIn("normal");
}

/*function update_links(strData)
{
    removeElement("load_links_content");
    var objDiv=document.getElementById("links_content");
    var id="#"+objDiv.id;
    objDiv.innerHTML=strData;
    objDiv.style.display='none';
    $(id).fadeIn("normal");
}*/
      
function fnUpdateCntryProgress(strDivId)
{
    var obj=document.getElementById(strDivId);  
    var y=findPosY(obj);
    var x=findPosX(obj);
    //alert("X="+x+"\nY="+y);

    var ht=parseInt(obj.offsetHeight);
    var wd=parseInt(obj.offsetWidth);
    //alert(ht+"\n"+wd);

    var strProgressId="load_"+strDivId;
    var elem;
    var img = "<img src='Images/website/loading.gif' height='32px' width='32px' border='0'>";
    var obLoad = document.createElement('div');
    obLoad.setAttribute('id', strProgressId);
    obLoad.style.display = 'block';
    obLoad.style.position = 'absolute';

    var ht_img=32; //The height of the div which will contain the image.
    var wd_img=32; //The width of the div which will contain the image.

    var top = y + (ht/2)-(ht_img/2);
    var left = x + (wd/2) - (wd_img/2);


    obLoad.style.top = (top < 0 ? 0 : top) + 'px';
    obLoad.style.left = (left < 0 ? 0 : left) + 'px';
    obLoad.style.width = wd_img + 'px';
    obLoad.style.height = ht_img + 'px';
    obLoad.style.zIndex = 999;
    obLoad.innerHTML=img;

    var oParent = document.getElementsByTagName('body')[0];
    oParent.appendChild(obLoad);
}

