var httpRequest,requestType,lastSelection,curWizVw,pageID
var isLoading=false


var xmlVersions=new Array("Msxml2.XMLHTTP.7.0","Msxml2.XMLHTTP.6.0","Msxml2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP")
var sWaitHTML='<HTML><HEAD><TITLE>DevInfo 6.0</TITLE></HEAD><BODY><TABLE width=100% height=100%><TR><TD align=center>'
sWaitHTML +='<OBJECT codeBase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" height="30" width="250" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" VIEWASTEXT> <PARAM NAME="_cx" VALUE="6615"> <PARAM NAME="_cy" VALUE="794"> <PARAM NAME="FlashVars" VALUE=""> <PARAM NAME="Movie" VALUE="Images/window_loading.swf"> <PARAM NAME="Src" VALUE="Images/window_loading.swf"> <PARAM NAME="WMode" VALUE="Window"> <PARAM NAME="Play" VALUE="-1"> <PARAM NAME="Loop" VALUE="-1"> <PARAM NAME="Quality" VALUE="High"> <PARAM NAME="SAlign" VALUE=""> <PARAM NAME="Menu" VALUE="-1"> <PARAM NAME="Base" VALUE=""> <PARAM NAME="AllowScriptAccess" VALUE="always"> <PARAM NAME="Scale" VALUE="ShowAll"> <PARAM NAME="DeviceFont" VALUE="0"> <PARAM NAME="EmbedMovie" VALUE="0"> <PARAM NAME="BGColor" VALUE=""> <PARAM NAME="SWRemote" VALUE=""> <PARAM NAME="MovieData" VALUE=""> <PARAM NAME="SeamlessTabbing" VALUE="1"> <PARAM NAME="Profile" VALUE="0"> <PARAM NAME="ProfileAddress" VALUE=""> <PARAM NAME="ProfilePort" VALUE="0"> <embed src="images/window-loading.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="250" height="30"> </embed></OBJECT>'
sWaitHTML += '</TD></TR></TABLE></BODY></HTML>'

function closeWindow()
{
	window.close()
}

var counter=0
function openCenteredWindow(url, name, params,height, width)
{   
    // -- If height is give then dialog is opened with the given height else the 80% of the screen    
    if(width==null || width==undefined)
    {
        width= Math.floor( ( (screen.width * 80) / 100 ) );
    }
	if(height==null || height==undefined) 
	{
	    height = Math.floor( ( (screen.height * 80) / 100 ) );
	}
	var left = Math.floor( (screen.width - width) / 2);
	var top = Math.floor( (screen.height - height) / 2);
	var winParams = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width
	var win
	if(params) winParams += "," + params
	try
	{
		counter+=1
		// Bug Fix 07 June 2006: When window try to open any new popup it checks if old name is present and open the window in the same name
		win=window.open(url, "DI6Web" + counter + name, winParams)
		win.document.write(sWaitHTML)
		win.location.href=url
	}
	catch(ex)
	{}
	if(parseInt(navigator.appVersion)>=4)
	{
		try
		{
			win.window.focus()
		}
		catch(ex)
		{}
	}
	return win
}

function getXMLHTTPRequest()
{
	if(window.XMLHttpRequest)
	{
		return new window.XMLHttpRequest()
	}
	else
	{
		for(i=0;i<xmlVersions.length;i++)
		{
			try
			{
				return new ActiveXObject(xmlVersions[i]);
			}
			catch(ex)
			{
			}
		}
	}
}
function CallBack(callfor,action,param,sType)
{   
	httpRequest=getXMLHTTPRequest()
	httpRequest.onreadystatechange=loadValue;
	if (window.ActiveXObject)
	{
	    httpRequest.open("POST","icallback.aspx",true)
	}
	else if(window.XMLHttpRequest)
	{
	    httpRequest.open("GET","icallback.aspx",true)
	}
	if(httpRequest)
	{
		requestType=sType
		httpRequest.setRequestHeader("callback",callfor)
		httpRequest.setRequestHeader("action",action)
		httpRequest.setRequestHeader("param",param)
		isLoading=true
		httpRequest.send(null)
	}
}

function loadValue()
{
	if(httpRequest.readyState==4)
	{
		if(httpRequest.responseText=="LOGOUT")
		{ 
			window.location.href="Default.aspx"
			return
		}
		if (requestType=="DELETE")
		{
		    XMLDoc1 = new ActiveXObject("Microsoft.XMLDOM")
			XMLDoc1.loadXML(httpRequest.responseText)
		    document.getElementById("ft_Text").innerText=XMLDoc1.documentElement.attributes.getNamedItem("RECORDS").value			
		    if (XMLDoc1.documentElement.attributes.getNamedItem("DisableTbr").value=="True")
		    {
		        disableTbr=true
		     }
		    else
		    {
		        disableTbr=false
		     }
			isLoading=false
			ITAChanged=false
			return
		}
		if(window.ActiveXObject)
		{
			XMLDoc = new ActiveXObject("Microsoft.XMLDOM")
			if(requestType=="D") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) makeTable();}
			else if(requestType=="Graph") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) makeGraph();}
			else if(requestType=="GRAPH1") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) makeGraphStep1();}
			else if(requestType=="TABLE1") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) makeTableStep1();}
			else if(requestType=="FootNote") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) ShowFootNote();}
            else if(requestType=="ChartType") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) ApplyChartType();}
            else if(requestType=="TableFootNote") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) ShowTableFootNote();}
            else if(requestType=="TableSuppressDuplicates") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) ShowTableSuppressDuplicates();}
            else if(requestType=="TableComments") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) ShowTableComments();}
            else if(requestType=="TableDenominator") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) ShowTableDenominator();}
            else if(requestType=="TablePivoting") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) ShowTablePivoting();}
            else if(requestType=="CheckDataCount") MoveToIndicator(httpRequest.responseText);
            else if(requestType=="CheckMostRecentStatus") SetMostRecentStatus(httpRequest.responseText);
            // -- Generate Table without any parameters --
            else if(requestType=="GenerateTable") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) makeTableStep1();}
            // -- Update Table when sort item image is clicked (for ascending/descending sorting) --
            else if(requestType=="SortItems") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) UpdateTableSorting();}
            // -- Update Table when sort items are checked/unchecked --
            else if(requestType=="UpdateSortItemList") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) UpdateTableSorting();}
            // -- Build Sort Items Table --
            else if(requestType=="BuildSortItemsTable") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) GenerateSortItems(httpRequest.responseText);}
            //Set graph Image on Preview conrtol based on chart types
            else if(requestType=="GraphChartType") SetChartType(httpRequest.responseText);
            else if(requestType=="GalReload") GalleryReload(httpRequest.responseText);            
            // request for getting IndicatorNId from IUSNID
            else if(requestType=="GetIndicatorNIDFromIUSNID") SetIndicatorNId(httpRequest.responseText);
            else if(requestType == "SaveTableFormat") TableFormatSaveStatus(httpRequest.responseText);
			//Implemented for the Map Presentation
			else if(requestType=="DeleteInset") setInsets();
			else if(requestType=="MapZoom") SetImagePath(httpRequest.responseText);
			else if(requestType=="SLIDESHOW") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) ShowAnimation();}
            else if(requestType=="NewSearch") MakeNewSearch();
            else if(requestType=="CheckPreview") CheckPreviewGeneration(httpRequest.responseText);
			else if(requestType=="Toggle")
			{
			    if(httpRequest.responseText)
		        {
		            document.getElementById("dgTabletd").innerHTML=httpRequest.responseText;
	            }
            }
            else if(requestType=="DBChange") ChangeSelectedDB(httpRequest.responseText);

			//This case is for Showing the Information window after clicking on the Map for Information
			else if(requestType=="I") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) SetIdentityWindow() }
			// -- need to check this function calling..  on which requestType this function (onTreePopulate) should execute -- 
			//else XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) onTreePopulate();}
			XMLDoc.loadXML(httpRequest.responseText)
		}
		else if(document.implementation && document.implementation.createDocument)
		{   // this part is for Mozilla and other DOM standards browsers
			XMLDoc= document.implementation.createDocument("","",null)
			if(requestType=='A') XMLDoc.onreadystatechange= onTablePopulate()
			else if(requestType=='S') XMLDoc.onreadystatechange= onSelectPopulate()
			else if(requestType=="D") 
			{
			     if (httpRequest.status == 200)
	            {
	                var xmlDOMFF;
                    xmlDOMFF = new DOMParser();
                    var vari = httpRequest.responseText;
                    XMLDoc = xmlDOMFF.parseFromString(vari,"text/xml");
                }
			    XMLDoc.onreadystatechange=makeTable()
			}
			else if(requestType=="SLIDESHOW") XMLDoc.onreadystatechange=function(){if(XMLDoc.readyState==4) ShowAnimation();}
			else if(requestType=="CheckDataCount") MoveToIndicator(httpRequest.responseText);
			else if(requestType=="GraphChartType") SetChartType(httpRequest.responseText);
            else if(requestType=="GalReload") GalleryReload(httpRequest.responseText);            
			else if(requestType=="CheckMostRecentStatus") SetMostRecentStatus(httpRequest.responseText);
			else if(requestType=="DeleteInset") setInsets();
			else if(requestType=="MapZoom") SetImagePath(httpRequest.responseText);
			else if(requestType=="DBChange") ChangeSelectedDB(httpRequest.responseText);
			else if(requestType=="CheckPreview")
			{			    
			     CheckPreviewGeneration(httpRequest.responseText);
			}
			else if(requestType=="TableSuppressDuplicates") 
			{
			    if (httpRequest.status == 200)
	            {
	                var xmlDOMFF;
                    xmlDOMFF = new DOMParser();
                    var vari = httpRequest.responseText;
                    XMLDoc = xmlDOMFF.parseFromString(vari,"text/xml");
                    if (XMLDoc.documentElement.childNodes.length>0)
                    {
                        var root_node = XMLDoc.getElementsByTagName('root');
                        document.getElementById("divTableView").innerHTML = root_node[0].getElementsByTagName("row")[0].firstChild.nodeValue;
                        //ShowTableSuppressDuplicates();
                    }
                }
			    //XMLDoc.onreadystatechange=function(){   if(XMLDoc.readyState==4) ShowTableSuppressDuplicates();   }
			}
			else if ( requestType=="GetIndicatorNIDFromIUSNID" )
			{
			    SetIndicatorNId(httpRequest.responseText);
			}
			else if(requestType=="Toggle")
			{
			    if(httpRequest.responseText)
		        {
		            document.getElementById("dgTabletd").innerHTML=httpRequest.responseText;
	            }
            }
			//else XMLDoc.onreadystatechange= onTreePopulate
			try
			{
				XMLDoc.loadXML(httpRequest.responseText)
			}
			catch (ex)
			{}
		}
	}
}

function ShowInfoPage(Param)
{
	var Return=openCenteredWindow('Information.aspx?' + Param,'Information','help:no;center:yes;resizable:yes;scroll:yes;',485,485)
	if(Return=='LOGOUT') parent.location.href='Default.aspx'
}
function AllowNumeric(obj)
{
	var compareString = '0123456789.,-'
	var mainString=String.fromCharCode(window.event.keyCode)
	if(compareString.indexOf(mainString)!=-1) return
	else window.event.returnValue=false
}

function icon_mouseOver(val){if(val.className!="toolbar_sel") val.className="highlight_icon"}
function icon_mouseOut(val){if(val.className!="toolbar_sel") val.className=""}
function addTD(sText,sClass,sGlbClr,sID,gNoWrap,sWidth,sCursor,sAlign)
{
	var TD=document.createElement("TD");
	if(sAlign) TD.align=sAlign
	if(gNoWrap) TD.noWrap=true
	if(sWidth) TD.style.width=sWidth//"25"
	if(sCursor) TD.style.cursor=sCursor
	if(sID) TD.id=sID
	if(sClass) TD.className=sClass
	if(sGlbClr)
	{
		TD.style.color=sGlbClr
		TD.chOff=true
	}
	TD.innerHTML=sText
	return TD
}

function disableTopPanel()
{
        var wDoc = parent.frames[0].document;
        wDoc.getElementById("imgIndicator").onclick="";
        wDoc.getElementById("imgIndicator").style.cursor="";
        wDoc.getElementById("imgIndicator").onmouseover="";
        
        wDoc.getElementById("imgArea").onclick="";
        wDoc.getElementById("imgArea").style.cursor="";
        wDoc.getElementById("imgArea").onmouseover="";
        
        wDoc.getElementById("imgTimePeriod").onclick="";
        wDoc.getElementById("imgTimePeriod").style.cursor="";
        wDoc.getElementById("imgTimePeriod").onmouseover="";
        
        wDoc.getElementById("imgSource").onclick="";
        wDoc.getElementById("imgSource").style.cursor="";
        wDoc.getElementById("imgSource").onmouseover="";
        
        wDoc.getElementById("imgDataView").onclick="";
        wDoc.getElementById("imgDataView").style.cursor="";
        wDoc.getElementById("imgDataView").onmouseover="";
        
        wDoc.getElementById("imgGallery").onclick="";
        wDoc.getElementById("imgGallery").style.cursor="";
        wDoc.getElementById("imgGallery").onmouseover="";
        
        wDoc.getElementById("imgReport").onclick="";
        wDoc.getElementById("imgReport").style.cursor="";
        wDoc.getElementById("imgReport").onmouseover="";
}

function enableTopPanel()
{
        var wDoc = parent.frames[0].document;
        var IndImg = wDoc.getElementById("imgIndicator");
        if(window.ActiveXObject)
        {
            IndImg.attachEvent("onclick",getIndicatorClick);
        }
        else if(document.implementation && document.implementation.createDocument)
        {
            IndImg.onclick = function() { getIndicatorClick(); }
        }
        IndImg.src = "images/adaptation/TopPanel/indicator_normal.gif";
        IndImg.onmouseover = function() {RollOverImages(IndImg.id,'images/adaptation/TopPanel/indicator_hover.gif')};
        IndImg.onmouseout = function() {RollOverImages(IndImg.id,'images/adaptation/TopPanel/indicator_normal.gif')};
        IndImg.style.cursor = "pointer";
        
        var AreaImg = wDoc.getElementById("imgArea");
        if(window.ActiveXObject)
        {
            AreaImg.attachEvent("onclick",getAreaClick);
        }
        else if(document.implementation && document.implementation.createDocument)
        {
            AreaImg.onclick = function() { getAreaClick(); }
        }
        AreaImg.src = "images/adaptation/TopPanel/area_normal.gif";
        AreaImg.onmouseover = function() {RollOverImages(AreaImg.id,'images/adaptation/TopPanel/area_hover.gif')};
        AreaImg.onmouseout = function() {RollOverImages(AreaImg.id,'images/adaptation/TopPanel/area_normal.gif')};
        AreaImg.style.cursor = "pointer";

        var TimImg = wDoc.getElementById("imgTimePeriod");
        if(window.ActiveXObject)
        {
            TimImg.attachEvent("onclick",getTimePeriod);
        }
        else if(document.implementation && document.implementation.createDocument)
        {
            TimImg.onclick = function() {getTimePeriod();}
        }
        TimImg.src = "images/adaptation/TopPanel/timeperiod_normal.gif";
        TimImg.onmouseover = function() {RollOverImages(TimImg.id,'images/adaptation/TopPanel/timeperiod_hover.gif')};
        TimImg.onmouseout = function() {RollOverImages(TimImg.id,'images/adaptation/TopPanel/timeperiod_normal.gif')};
        TimImg.style.cursor = "pointer";
        
        var SrcImg = wDoc.getElementById("imgSource");
        if(window.ActiveXObject)
        {
            SrcImg.attachEvent("onclick",getSourceClick);
        }
        else if(document.implementation && document.implementation.createDocument)
        {
            SrcImg.onclick = function() { getSourceClick();}
        }
        SrcImg.src = "images/adaptation/TopPanel/source_normal.gif";
        SrcImg.onmouseover = function() {RollOverImages(SrcImg.id,'images/adaptation/TopPanel/source_hover.gif')};
        SrcImg.onmouseout = function() {RollOverImages(SrcImg.id,'images/adaptation/TopPanel/source_normal.gif')};
        SrcImg.style.cursor = "pointer";
        
        var DataViewImg = wDoc.getElementById("imgDataView");
        if(window.ActiveXObject)
        {
            DataViewImg.attachEvent("onclick",getData);
        }
        else if(document.implementation && document.implementation.createDocument)
        {
            DataViewImg.onclick = function() {getData();}
        }
        DataViewImg.src = "images/adaptation/TopPanel/dataview_normal.gif";
        DataViewImg.onmouseover = function() {RollOverImages(DataViewImg.id,'images/adaptation/TopPanel/dataview_hover.gif')};
        DataViewImg.onmouseout = function() {RollOverImages(DataViewImg.id,'images/adaptation/TopPanel/dataview_normal.gif')};
        DataViewImg.style.cursor="pointer";
        
        var GalImg = wDoc.getElementById("imgGallery");
        if(window.ActiveXObject)
        {
            GalImg.attachEvent("onclick",OpenGallery);
        }
        else if(document.implementation && document.implementation.createDocument)
        {
            GalImg.onclick = function() {OpenGallery();}
        }
        GalImg.onmouseover = function() {RollOverImages(GalImg.id,'images/adaptation/TopPanel/gallery_hover.gif')};
        GalImg.onmouseout = function() {RollOverImages(GalImg.id,'images/adaptation/TopPanel/gallery_normal.gif')};
        GalImg.style.cursor="pointer";
}

// function to toggle the Navigation Control Vs Close Button for non Wizard Mode
function toggleNavigationBar(navBar,trCloseButton,CallFrom)
{
    if (document.getElementById(navBar).style.display == "none" )
    {
        document.getElementById(navBar).style.display = "";
        document.getElementById(trCloseButton).style.display = "none";
        if(CallFrom == 'G')
        {
            document.getElementById("WizardMode").value="False";
            CallBack(5,3,"False","");   
        }
        if(CallFrom == 'T')
        {
            document.getElementById("WizardMode").value="False";
            window.document.title=WMTitle;
            CallBack(17,3,"False","");
        }
    }
    else if ( document.getElementById(trCloseButton).style.display == "none" )
    {
        document.getElementById(navBar).style.display = "none";
        document.getElementById(trCloseButton).style.display = "";
        if(CallFrom == 'G')
        {
            document.getElementById("WizardMode").value="True"; 
            CallBack(5,3,"True",""); 
        }
        if(CallFrom == 'T')
        {
            document.getElementById("WizardMode").value="True"; 
            window.document.title=NWMTitle;
            CallBack(17,3,"True",""); 
        }
    }
}

function getFileName()
{
	var ans=prompt("Please enter the filename")
	if(ans)
	{
		document.getElementById("selFileName").value=ans
		return true
	}
	return false
}

function HighlightText(label)  
{
    if(document.getElementById(label).style.color == "black")
    {
        document.getElementById(label).style.color = "#808080";
    }
    else
    {
        document.getElementById(label).style.color = "black";
    }
}

function unloadGalleryPresentationEditing()
{                     
     // for closing the opener window when wizard is closed.
     if (EditPresentationName)
     {
        if (EditPresentationName.length > 1)
        {
            window.opener.close();
        }
     }
     if (window.ActiveXObject)
     {
        if((window.event.clientX<0) || (window.event.clientY<0))
         {
           
           CallBack(19,0,"0","")    
         }   
         if (event.altKey==true && event.keyCode==0 ) 
         {
            CallBack(19,0,"0","")
         }   
     }
     else
     {
         CallBack(19,0,"0","")
     }
     
}