var GlossaryResponse="";
var wordList=[];
var definitionList=[];
var midWindow=0;
var tipContainer="";
var IE=true;
var exact="";var style_name_suffix="";var keys="";
if(navigator.appName!='Microsoft Internet Explorer'){IE=false;}

var XmlHttp;
/**Sector Focus code**/
function SectorFocusLoad()
{
    try
    {
	    var objImgAero=document.getElementById("imgAerospace");
	    var objImgAuto=document.getElementById("imgAutomotive");
	    var objImgCons=document.getElementById("imgConsumerAndRetail");
	    var objImgEnergy=document.getElementById("imgEnergy");
	    var objImgHealth=document.getElementById("imgHealthcare");
	    var objImgTech=document.getElementById("imgTechnology");
	    var objImgIndus=document.getElementById("imgIndustrial");
	    var objImgPubl=document.getElementById("imgPublishing");
    	 
    	 //alert("objImgAuto="+objImgAuto);
        //objImgAuto.onclick=GetPopup;
	    if(objImgAero){
		    objImgAero.onclick=function(){GetPopup(this);};
	    }
	    if(objImgAuto){
		    objImgAuto.onclick=function(){GetPopup(this);};
	    }
	    if(objImgCons){
		    objImgCons.onclick=function(){GetPopup(this);};
	    }
	    if(objImgEnergy){
		    objImgEnergy.onclick=function(){GetPopup(this);};
	    }
	    if(objImgHealth){
		    objImgHealth.onclick=function(){GetPopup(this);};
	    }
	    if(objImgTech){
		    objImgTech.onclick=function(){GetPopup(this);};
	    }
	    if(objImgIndus){
		    objImgIndus.onclick=function(){GetPopup(this);};
	    }
	    if(objImgPubl){
		    objImgPubl.onclick=function(){GetPopup(this);};
	    }
        
       // alert("Done");
	    /*
        var objrss = document.getElementById("imgrss");
        if(objrss){
            objrss.onclick=function(){RedirectToRssPage();};
        }*/
	}catch(e){
	    alert(e);
	}
}

function RedirectToRssPage()
{
    alert("RedirectToRssPage");
    //window.location="/_layouts/feed.aspx?xsl=1&web=/en&page=dbd7af1e-11b3-4f3e-a9e9-f2433baff8de&wp=ba2abca3-d617-4d04-8849-dd5d7f57806d";
}

var linkName="";
function GetPopup(obj)
{
    //alert("obj=");
    //debugger;
	var lnkId=new String();
	lnkId=obj.id;
	linkName=lnkId.replace("img","");
	var url=window.location;
	var requestUrl="";
	var requestUrl="/_layouts/custom/CEVALogistics/styles/includes/aspx/ajaxpage.aspx?LinkName="+linkName+"&Page=SectorFocus&url="+url+"&num="+Math.random();
	
	XmlHttp=null;

	CreateXmlHttp();
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleGetPopup;
		XmlHttp.open("GET", requestUrl, true);
		XmlHttp.send(null);
	}
}

function HandleGetPopup()
{
	if(XmlHttp.readyState==4)
	{
		if(XmlHttp.status==200)
		{
			SetPopup(XmlHttp.responseText);
		}
		else
		{
			alert("There was a problem." );
		}
	}
}

function SetPopup(string)
{
    document.getElementById("pnl"+linkName).innerHTML=string;
    document.getElementById("popup"+linkName).style.display="inline";
}

function ClosePopup(obj)
{
    var lnkId=new String();
	lnkId=obj.id;
	var dvId=lnkId.replace("imgdv","");
	document.getElementById("pnl"+dvId).innerHTML="";
}
/**Sector Focus code end**/

/**Code for user location**/
function OnLoadUserLocation()
{ 
    try{
        //var objlocationImage=document.getElementById("locationImage");
        var objlocationImage=document.getElementById("ctl00_CountryImageUserControl_locationImage");
        //alert("objlocationImage="+objlocationImage);
        if(objlocationImage){
            objlocationImage.onclick=function(){GetUserLocation();};
        }
    }catch(e){
        //alert(e);
    }
}

function GetUserLocation()
{
    //alert("Hello");
	var url=window.location;
	var requestUrl="";
	var requestUrl="/_layouts/custom/CEVALogistics/styles/includes/aspx/ajaxpage.aspx?Page=UserLocation&url="+url+"&num="+Math.random();
	
	XmlHttp=null;

	CreateXmlHttp();
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleUserLocation;
		XmlHttp.open("GET", requestUrl, true);
		XmlHttp.send(null);
	}
}

function HandleUserLocation()
{
    if(XmlHttp.readyState==4)
	{
		if(XmlHttp.status==200)
		{
			//alert(XmlHttp.responseText);
			SetUserLocation(XmlHttp.responseText);
		}
		else
		{
			alert("There was a problem in retrieving user location." );
		}
	}
}

function SetUserLocation(string)
{
    var objLocation=document.getElementById("locationLocation");
    var objucLocation=document.getElementById("ucLocation");
    if(objucLocation){
        objucLocation.length=0;
    }
    if(objLocation){
        objLocation.length=0;
    }
    
    var locationISO=new String();
    locationISO=string;
    var arrlocISO=locationISO.split('#');
    if(objLocation){
        objLocation.options[objLocation.length]=new Option("Please select", "Please select");
    }
    if(objucLocation){
        objucLocation.options[objucLocation.length]=new Option("Please select", "Please select");
    }
    for(var cnt=0;cnt<arrlocISO.length;cnt++)
    {
        if(arrlocISO[cnt]!="")
        {
            var locISO=arrlocISO[cnt];
            var location=locISO.split("^")[0];
            var ISOCode=locISO.split("^")[1]; 

            if(objLocation){
                objLocation.options[objLocation.length]=new Option(location, ISOCode);
            }
            if(objucLocation){
                objucLocation.options[objucLocation.length]=new Option(location, ISOCode);
            }
        }
    }
    //alert("objucLocation="+objucLocation.length);
    //document.getElementById("panelUCUserLocation").style.display="inline";
}

/*User location end*/
/**Customer Tools and Resources**/
function CustomerToolsResources()
{

    try{
        var objCustToolsRes=document.getElementById("imgCustToolsResources");
        if(objCustToolsRes){
            objCustToolsRes.onclick=function(){GetCustomerToolsResources();};
        }
    }catch(e){
        //alert(e);
    }
}

function CallToolsResources()
{
    GetCustomerToolsResources();
}

function GetCustomerToolsResources()
{

    var url=window.location;
	var requestUrl="";
	var requestUrl="/_layouts/custom/CEVALogistics/styles/includes/aspx/ajaxpage.aspx?Page=CustomerToolsResources&url="+url+"&num="+Math.random();
	
	XmlHttp=null;

	CreateXmlHttp();
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleCustomerToolsResources;
		XmlHttp.open("GET", requestUrl, true);
		XmlHttp.send(null);
	}
}

function HandleCustomerToolsResources()
{
    if(XmlHttp.readyState==4)
	{
		if(XmlHttp.status==200)
		{
		    try
		    {
		        //alert(XmlHttp.responseText);
                //document.getElementById("pnlCustomerToolsResources").innerHTML=XmlHttp.responseText;
                document.getElementById("panelToolsResources").innerHTML=XmlHttp.responseText;
                document.getElementById("panelToolsResources").style.display="inline";
            }
            catch(e)
            {
                //alert(e);
            } 
		}
		else
		{
			alert("There was a problem in retrieving Tools/Resources." );
		}
	}
}
/**Customer Tools and Resources end**/
/**Other CEVA Sites**/
function OtherCevaSites()
{
    try{
    var objImgcevasites=document.getElementById("imgOthercevasites");
    var objhrfcevasites=document.getElementById("hrfOthercevasites");
    
    if(objImgcevasites)
        objImgcevasites.onclick=function(){GetOtherCevaSites();return false;};
    if(objhrfcevasites)
        objhrfcevasites.onclick=function(){GetOtherCevaSites();return false;};
        }catch(e){}
}

function GetOtherCevaSites()
{
    var url=window.location;
	var requestUrl="";
	var requestUrl="/_layouts/custom/CEVALogistics/styles/includes/aspx/ajaxpage.aspx?Page=OtherCevaSites&url="+url+"&num="+Math.random();
	
	XmlHttp=null;

	CreateXmlHttp();
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleOtherCevaSites;
		XmlHttp.open("GET", requestUrl, true);
		XmlHttp.send(null);
	}
}

function HandleOtherCevaSites()
{
    if(XmlHttp.readyState==4)
	{
		if(XmlHttp.status==200)
		{
		    try
		    {
			    document.getElementById("pnlOthercevasites").innerHTML=XmlHttp.responseText;
			    document.getElementById("popupOtherSites").style.display="inline";
            }
            catch(e){
                //alert(e);
            } 
		}
		else{
			alert("There was a problem in retrieving Tools/Resources." );
		}
	}
}

/**Other CEVA Sites end**/
function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e){
		try{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc){
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined") {
	   //alert("in firefox cond");
		XmlHttp = new XMLHttpRequest();
	}
}

var currItemId=0;

function GetDefinition(itemID)
{
    currItemId="";
    currItemId=itemID.id;
    currItemId=currItemId.replace("spn","");
    //alert("currItemId="+currItemId);
    var url=window.location;
	var requestUrl="";
	var requestUrl="/_layouts/custom/CEVALogistics/styles/includes/aspx/ajaxpage.aspx?Page=Glossary&Term="+currItemId+"&url="+url+"&num="+Math.random();
	
	XmlHttp=null;

	CreateXmlHttp();
	if(XmlHttp)
	{
		XmlHttp.onreadystatechange = HandleGlossaryDefinition;
		XmlHttp.open("GET", requestUrl, true);
		XmlHttp.send(null);
	}
}

function HandleGlossaryDefinition()
{
    if(XmlHttp.readyState==4)
	{
		if(XmlHttp.status==200)
		{
		    try
		    {
			    //alert(XmlHttp.responseText);
			    document.getElementById("pnl"+currItemId).innerHTML=XmlHttp.responseText;
			    document.getElementById("panelGlossary").style.display="inline";
            }
            catch(e){
                //alert(e);
            } 
		}
		else{
			alert("There was a problem in retrieving Glossary definition." );
		}
	}
}

function CloseToolsResources()
{
    document.getElementById('panelToolsResources').innerHTML="";
    document.getElementById('panelToolsResources').style.display="none";
}

//var options="";
function createGlossary()
{
    try{
        var GlossaryRequest=window.ActiveXObject?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();
        GlossaryRequest.onreadystatechange=function(){
	        if(GlossaryRequest.readyState==4){
		        if(GlossaryRequest.status==200){
		            var GlossaryResponse=GlossaryRequest.responseText;
		            var url="http://"+window.location.host+"/en/_layouts/custom/CEVALogistics/styles/includes/aspx/ajaxpage.aspx";
		            //alert(url);
		            //var url="http://wwwdev.logistics.corp/en/_layouts/custom/CEVALogistics/styles/includes/aspx/ajaxpage.aspx";
                    
                    $(document).ready(function(){var options = {exact:"exact", style_name_suffix:false, keys:GlossaryResponse };
		                $(document).SearchHighlight(options);

	                    /**/
	                    $(".hilite").mouseout(function() {
		                    //Remove the appended tooltip template
		                    $(this).children('div#nFloat').remove();
		                    hideDefinition();
	                    });

	                    /**/
	                    $(".hilite").mouseover(
                        function (e){
                            hideDefinition();
                            var selWord=$(this).text();
                            debugger;
                            
                            var xOffset = -10; 
                            var yOffset = -175;
	                        if(!e)e=window.event;
	                        //alert("e.pageY="+e.pageY);
                            $("#nFloat").css("top",(e.pageY - xOffset) + "px");
                            $("#nFloat").css("left",(e.pageX + yOffset) + "px"); 
                           
                            //$("#nFloat").fadeIn("slow");
                            showDefinition(selWord);
                                       
                            /**/
                           // var wrd = this;
                            //hideDefinition();
                            //showDefinition(selWord);
                            //$.get(url, { Page:"glossary",term: selWord,num:Math.random()*555}, function(data){$(wrd).attr("title",data); });
                            }, 
                        function(){ }
                        )
	               });
	                
		        }else{
			        alert('Error in Glossary:- '+GlossaryRequest.statusText);
		        }
	        }
        };
        var forceGET = "n="+ Math.random()*9999;
        GlossaryRequest.open("GET","/_layouts/custom/CEVALogistics/styles/includes/aspx/ajaxpage.aspx?page=term&"+forceGET,true);
        GlossaryRequest.send(null);
    }
    catch(e){}
}

function hideDefinition()
{
    var tipContainer=document.getElementById("nFloat");
    tipContainer.style.display='none';
    while (tipContainer.lastChild){
        tipContainer.removeChild(tipContainer.lastChild);
    }
}

function ScriptLoad()
{
    try{
    
        SectorFocusLoad();
        OnLoadUserLocation();
        CustomerToolsResources();
        OtherCevaSites();
        createGlossary();
    }
    catch(e){
        alert(e);
    }
}

function showDefinition(defineWord)
{
	try
	{
	    //alert("defineWord="+defineWord);
		if(defineWord!="")
		{
			//tipContainer.style.display='';
		    var GlossaryRequest=window.ActiveXObject?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest();
		    GlossaryRequest.onreadystatechange=function(){
			    if(GlossaryRequest.readyState==4){
				    if(GlossaryRequest.status==200){
				       // alert("GlossaryRequest.responseText="+GlossaryRequest.responseText);
				        if(GlossaryRequest.responseText!=""){
				        var objContainer=document.getElementById("nFloat");
				        while (objContainer.lastChild){
			                objContainer.removeChild(objContainer.lastChild);
		                }
				        objContainer.appendChild(document.createTextNode(GlossaryRequest.responseText));
				        objContainer.style.display="";
				         //   tipContainer.style.display='';
					    //    tipContainer.appendChild(document.createTextNode(GlossaryRequest.responseText));
                        }
    					
				    }else{
					    alert('Error in Glossary:- '+GlossaryRequest.statusText);
				    }
			    }
		    };
		    var forceGET = "n="+ Math.random()*9999;
		    GlossaryRequest.open("GET","/_layouts/custom/CEVALogistics/styles/includes/aspx/AJAXPage.aspx?Page=glossary&term="+defineWord+"&"+forceGET,true);
		    GlossaryRequest.send(null);
		}
	}catch(e){}
}

ScriptLoad();
