if (window.g_initScript == null)
{
    var g_initScript = '';
}

function OnDOMLoaded(scriptToRun){
    g_initScript += scriptToRun;
}

function OnLoadHandler() 
{
    // quit if this function has already been called
//    if (arguments.callee.done) return;

    // flag this function so we don't do the same thing twice
//    arguments.callee.done = true;

    // kill the timer
    if (_timer) clearInterval(_timer);

    // do stuff
    try
    {
        eval(g_initScript);        
    }
    catch (e)
    {}
    // clear the script
    g_initScript = '';
    
    
    try
    {
		document.getElementById('dSuperimpose').style.display = 'none';
	}
	catch(e)
	{}
};
if (window._onloadHandled == null)
{
	/* for Mozilla/Opera9 */
	if (document.addEventListener) 
	{
		document.addEventListener("DOMContentLoaded", OnLoadHandler, false);
	}
	else if (/WebKit/i.test(navigator.userAgent)) /* for Safari */
	{ // sniff
		var _timer = setInterval(function() 
		{
			if (/loaded|complete/.test(document.readyState)) 
			{
				OnLoadHandler(); // call the onload handler
			}
		}, 10);
	}
	else
	{
	/* for other browsers, including Internet Explorer */
		window.onload = OnLoadHandler;
	}
	
	window._onloadHandled = true;
	// move from Script.js
	OnDOMLoaded('ReadCookies();');

}
function NGPostBack(target,arg)
{
////    alert(target);
   var finalargument='';
 if (arguments.length > 0 && target != null)
      finalargument  = target;
           
 if (arguments.length > 1 && arg != null) 
    finalargument = finalargument + '|' + arg;
////    
////    alert(finalargument);
////    alert(target);
  //__doPostBack('ctl01$cpTarget$hdf_Target', finalargument);


} 
//UpdatePanel::used to PostBack the page & update panel named panel.
function UpdatePanel(panel)
{   
    __doPostBack(_Target, panel);
} 


//This function is called whenever user click on close button of Tool
function CloseTool(toolBarName, toolName)
{       
  __doPostBack(toolBarName, toolName);
}


//Added by prashant
//PRS:Function to call on the close button of the border control without postback...
function CloseToolWithoutPostBack(toolname,resetDefaultTool)
{  
    var ele = NgGetElementById(toolname);
    if (ele)
    {
        ele.style.display="none";
    }
    
    var hiddenEle = NgGetElementById(toolname + '_SaveBorderStyle');
    if(hiddenEle)
    {
        hiddenEle.value = 'none';
    }
    var boxEle = GetBoxItem(toolname);
    if(boxEle)
    { 
      boxEle.visible = "none"; 
    }   
    
    if (resetDefaultTool)
    {
        //change mode to zoomIn. Hardcoded, need to see how to read from server
        var defTool = ClickTool('ZoomIn');
    }
    
    
}

//PRS:Function to call by the server code to display the border control...
function DisplayBox(toolname)
{
    var ele = NgGetElementById(toolname);
	if (ele)
	{
        ele.style.display = 'block';
    }
    
    var hiddenEle = NgGetElementById(toolname + '_SaveBorderStyle');
    if(hiddenEle)
    {
        hiddenEle.value = 'block';
    }
    var boxEle = GetBoxItem(toolname);
    if(boxEle)
    {
        boxEle.visible = "block";
    }
}


//This checks if the client browser has a popupblocker software installed or not.
function IsPopupBlocker() 
{
var oWin = window.open("","testpopupblocker","width=10,height=10,top=5000,left=5000");
if (oWin==null || typeof(oWin)=="undefined") {
    alert("Please allow popups on this site or you may not be able to access some of the functionalities. ");
} else 
{
	oWin.close();
}
return;
}


function ValidateBuffer(src, args)
{   
    if (args != null)
    {   
        args.IsValid = ! isNaN(args.Value);        
    }    
}

//---------------------
//returns the <protocol>//hostname/pathname
function NGServerUrl()
{
    var protocol = window.location.protocol;
    var theHost = window.location.hostname;
    var port = window.location.port;
    
    if (port)
    {
        port=":"+ port;
    }
    
    var path = window.location.pathname;
        path = path.substring(0, path.indexOf("/", 1));
    
    var newUrl = protocol + "//" + theHost +  port + path;
    return newUrl;	
}



//NGNavigate - navigate's to the specified url.
//Url        : this value can be either relativeUrl or absolute url
//resolveUrl : can be either true or false. if the value is true, the perfix's the url with server url 
function NGNavigate(url, resolveUrl)
{
    var prefixServerUrl = new Boolean(resolveUrl);
    var newUrl = url;
    if(prefixServerUrl)
    {
        newUrl = NGServerUrl() + url;
    }
    //window.navigate(newUrl);    
    window.location.href = newUrl;
}



//-------------------------
function SetDirection(elementID)
{
     
   try{
     var elm=NgGetElementById(elementID);
     var eLeft=parseInt(xreplace(elm.style.left,"px",""));
     var mapWidth= parseInt(xreplace(NgGetElementById("MapControlDiv").style.width,"px",""));
      // alert(elementID)
      // alert(elm.getAttribute("SetSmartNavValues"))
     if(elm.getAttribute("SetSmartNavValues")==null && eLeft < 50)
      {
    
       if (eLeft==1)
         mapWidth=mapWidth/4;
       
       if (htmlDir=="LTR")
          {
            if( elm.style.left != "")
                 elm.style.left=eLeft+ mapWidth + "px";
          }
        else
          {   
            if( eLeft!=1 && eLeft < 50)
                eLeft=20;
            
           
            // This is when the box loads after execution of this line
            var cWidth= elm.clientWidth;
           
            if (cWidth==0)
                cWidth=220;
            
                 elm.style.left=document.body.clientWidth - mapWidth - cWidth - eLeft + "px";                   
        }
      }
     }catch(e){}
}

//-------------------------
function SetDirectionFlip(elementID)
{
     
   try{
     var elm=NgGetElementById(elementID);
     var eLeft=parseInt(xreplace(elm.style.left,"px",""));
     
      // alert(elementID)
      // alert(elm.getAttribute("SetSmartNavValues"))
     if(elm.getAttribute("SetSmartNavValues")==null)
      {
       
       if (htmlDir=="RTL")
          {
            if( elm.style.left != "")
                elm.style.left=document.body.clientWidth - eLeft - elm.clientWidth + "px";  
          }
             
                                  
        }
      
     }catch(e){}
}


    //-------------------------	
    function NgOpenPopup(url, iWidth, iHeight, PopupTitle)
    {
        NgOpenPopup(url, iWidth, iHeight, PopupTitle, false);
    }
     
    function NgOpenPopup(url, iWidth, iHeight, PopupTitle, scrolling)
    {                
        var commoniFrame = "commoniFrame";
        var commoniFrameWraper = _CommonPopupLayer;
        var popupTitleId = "commoniFrameTitle";
        
        //Find the iFrame by ID
        var _iFrame;
        try
        {
            _iFrame = NgGetElementById(commoniFrame);
        } 
        catch(e) { }
            
        //Find the iFrame LayerWraper by ID
        if (commoniFrameWraper != null)
        {
            var _iFrameLayer;
            try
            {
                _iFrameLayer = NgGetElementById(commoniFrameWraper);
            } 
            catch(e) { }
               
            if (_iFrameLayer != null)
            {
                ShowLayer(commoniFrameWraper, null, null);
                _iFrameLayer.style.visibility = 'visible';
            }
        }   
             
        if (_iFrame != null)
        {
            ShowLayer(commoniFrame, null, null);
                 
            _iFrame.style.visibility = 'visible';
            _iFrame.scrolling = (scrolling ? "yes": "no");
            frames[commoniFrame].location.href = url;
                 
            if (iHeight!=null)
                _iFrame.height = iHeight;
            if (iWidth!=null)
                _iFrame.width = iWidth;
        }
        
        //Set the popup title
        var _PopupTitleElement = NgGetElementById(popupTitleId);

        if (_PopupTitleElement != null)
            _PopupTitleElement.innerHTML = PopupTitle;
    }
    
    //-------------------------	
    function NgClosePopup()
    { 
        var commoniFrame = "commoniFrame";
        var commoniFrameWraper = _CommonPopupLayer;           
        var _iFrame;
        try
        {
            _iFrame = window.top.NgGetElementById(commoniFrame);
        } 
        catch(e) { }
        
        if (commoniFrameWraper != null)
        {
            var _iFrameLayer;
            try
            {
                _iFrameLayer=window.top.NgGetElementById(commoniFrameWraper);
            } 
            catch(e) { }
                   
            if (_iFrameLayer != null)
            {
                HideLayer(commoniFrameWraper, null, null);
                _iFrameLayer.style.visibility = 'hidden';
            }
        }
         
        if (_iFrame!=null)
        {
            _iFrame.style.display = 'none';
            HideLayer(commoniFrame, null, null);
            window.top.frames[commoniFrame].location.href = 'about:blank';
        }         
    }

    //-------------------------	
     function NgOpenWindow(url,windowTitle, attributes)
        { 
        
        //height= Specifies the height of the window. 
        //left= Specifies the x coordinates of the window in pixels. IE only. See "screenX" as well. 
        //location= Specifies if the location bar of the window should be included. 
        //menubar= Specifies if the menu bar of the window should be included. 
        //resizable= Specifies if window should be resizable. 
        //screenX= Specifies the x coordinates of the window in pixels. NS only. See "left" as well. 
        //screenY= Specifies the x coordinates of the window in pixels. NS only. See "top" as well. 
        //scrollbars =Specifies if window should contain scrollbars. 
        //status= Specifies if the status bar of the window should be included. 
        //toolbar= Specifies if the toolbar of the window (ie: reload button) should be included. 
        //top= Specifies the y coordinates of the window in pixels. IE only. See "screenY" as well. 
        //width= Specifies the width of the window. 
        
        // Example:
        // windowTitle='Orion Window';
        // attributes= 'width=600,height=500,status,resizable';
        // window.open('http://www.Oriongis.com',windowTitle,attributes);

        if(attributes ==null)
            attributes="";
            
        if(url !=null)    
            window.open(url,windowTitle, attributes);

        }
        

    //-------------------------	
     function NgCloseWindow()
        { 
            window.close();
        }
        
     
     //-------------------------	
     function NgRefreshParentWindow()
     {        
        if (window.top)
        {        
            window.top.RefreshPage();
        }
     }
        
    //-------------------------	
    function NgRefreshWindow()
    {
        try {
            window.RefreshPage();
        } catch (e) { 
            if (window.top)
            {        
                window.top.RefreshPage();
            }
        }
    }
        
        
    //------------------------- 
    function xreplace(s, r, w){
       return s.split(r).join(w);
    }


 function getUniqueValue()
 {
    var date = new Date();
    var value0 = date.toGMTString();
    var value1 = replaceAll(value0, " ", "_");
    var value2 = replaceAll(value1, ":", "_");
    return replaceAll(value2, ",", "_");
 }
 
 function replaceAll(strValue, sourceChar, destChar)
 {
    if(strValue != null && sourceChar != null && destChar != null)
    {
        while(strValue.indexOf(sourceChar) >= 0)
        {
            strValue = strValue.replace(sourceChar, destChar);
        }
    } 
    
    return strValue;
 }
 
  function NgAdjustPopupSize()
  {
    var commoniFrame="commoniFrame";
    
    var _iFrame;
    try{
              _iFrame=window.top.NgGetElementById(commoniFrame);
    } catch(e){}
   
   
        try{
            window.top.calcHeight(commoniFrame);
            window.top.calcWidth(commoniFrame);    
        }catch(e){}
   }




// disable default context menu
    function _hideDefaultContextmenu(e)
    {       
        var firingEle = (m_ie5) ? event.srcElement : e.target;
         
        if(firingEle.href != null && firingEle.href.substring(0,11)=='javascript:')
        { 
           if (!m_ie5)
              e.stopPropagation();
          
          return false;
        }          
    }

   
    var m_ie5 = (window.navigator.appName.toLowerCase().indexOf("microsoft")>=0);

    if (m_ie5)
    {
        document.attachEvent('oncontextmenu',_hideDefaultContextmenu);	            
    }
    else
    {
        
        document.addEventListener('contextmenu',_hideDefaultContextmenu,false);
       
    }
   
function IncludeJavaScript(jsFile)
{
  document.write('<script type="text/javascript" src="'
    + jsFile + '"></script>'); 
}


function CreateElementRadio(radioGrpName, elementId, elementValue, selecteded)
{
    var rdo = null;
    try
    {   //IE
        rdo = document.createElement("<input name=" + radioGrpName + "/>");
    }
    catch(e)
    {
        rdo = document.createElement("input");
    }
    
    rdo.setAttribute("type", "radio");
    rdo.setAttribute("name", radioGrpName);
    rdo.id = elementId;
    rdo.defaultChecked = selecteded;
    rdo.value = elementValue;
    
    return rdo;
}

function CreateElementTextBox(elementId, elementValue)
{
    var txtBox = null;
    
    try
    {   //IE
        txtBox = document.createElement("<input name=" + elementId + "/>");
    }
    catch(e)
    {
        txtBox = document.createElement("input");
    }
    txtBox.setAttribute("type", "text");
    txtBox.name = elementId;            
    txtBox.id = elementId;
    txtBox.value = elementValue;
    
    return txtBox;
}

function CreateElementDropDown(elementId)
{
    var select = document.createElement("select");                
    select.name = elementId;
    select.id = elementId;
    
    return select;

}

function RemoveMapCtrl(mapCtrlId, preserveOnPointSetting)
{
    if (preserveOnPointSetting == undefined || !preserveOnPointSetting)
    {    
        RemoveOnPointSettings(mapCtrlId);           
    }
               
    if (mapCtrlId == null)
    {        
        g_OLMapCtrls[0].CloseToolsPanel(); 
        g_OLMapCtrls[0].destroy();                   
        g_OLMapCtrls = null;    
    }
    else
    {
        for (var i=0; i < g_OLMapCtrls.length; i++)
        {
            if ((g_OLMapCtrls[i].id != null) && (g_OLMapCtrls[i].id == mapCtrlId))
            {                
                g_OLMapCtrls[i].CloseToolsPanel();
                g_OLMapCtrls[i].destroy();       
                g_OLMapCtrls.splice(i,1);
                break;
            }
        }         
    }  
                  
}


// OnPoint Setting JSON
if (window.g_onPointSettings == null)
{
    var g_onPointSettings = new Array();
}

function AddOnPointSettings(jsonSetting)
{      
  
    eval(jsonSetting);
    g_onPointSettings.push(onPointSettings);
}

function GetOnPointSettings(mapCtrlId)
{
    var onPointSetting = null;
    
    if (mapCtrlId == null)
    {
        onPointSetting = g_onPointSettings[0];
    }
    else
    {
        for (var i=0; i < g_onPointSettings.length; i++)
        {
            if (g_onPointSettings[i].id == mapCtrlId)
            {
                onPointSetting = g_onPointSettings[i];
                break;
            }
        }    
    }
    return onPointSetting;    
}

function RemoveOnPointSettings(mapCtrlId)
{
    if (mapCtrlId == null)
    {
        g_onPointSettings = null;    
    }
    else
    {
        for (var i=0; i < g_onPointSettings.length; i++)
        {
            if (g_onPointSettings[i].id == mapCtrlId)
            {                
                g_onPointSettings.splice(i,1);
                break;
            }
        }                
    }
}

// -- -- -- -- -- -- -- -- -- -- -- --
function changeiframeHeight(iFrameId) {

    try {
        //find the height of the internal page
        var the_height = parent.document.getElementById(iFrameId).contentWindow.document.body.scrollHeight + 'px';
        //change the height of the iframe
        // alert(the_height);
        parent.document.getElementById(iFrameId).height = the_height;
        parent.document.getElementById(iFrameId).style.height = the_height;
    } catch (e) { }
}

//-------------------------
function changeiframeWidth(iFrameId) {
    //find the Width of the internal page
    try {
        var the_width = parent.document.getElementById(iFrameId).contentWindow.document.body.scrollWidth + 'px';
        //change the Width of the iframe
        parent.document.getElementById(iFrameId).width = the_width;
        parent.document.getElementById(iFrameId).style.width = the_width;
    } catch (e) { }

}
// ------------------------------------------------------------------------------------------------------------------------
// * Desc      : Prevent the default action for an event.    
// ------------------------------------------------------------------------------------------------------------------------
function StopEvent(e) 
{
    if (!e) var e = window.event;
    // --------------------------------------------------------------------------------------------------------------------
    // * e.cancelBubble is supported by IE - this will kill the bubbling process.          
    // --------------------------------------------------------------------------------------------------------------------
    e.cancelBubble = true;
    e.returnValue = false;
    // --------------------------------------------------------------------------------------------------------------------
    // * e.stopPropagation works only in Firefox and other.          
    // --------------------------------------------------------------------------------------------------------------------
    if (e.stopPropagation) 
    {
        e.stopPropagation();
        e.preventDefault();
    }
    return false;
}

function getElementTopPostion(element) {
    var elementTop = 0;
    
    if (element != null) {
        var elementLeft = element.offsetLeft;
        elementTop = element.offsetTop;
        var TmpElem = element.offsetParent;
        while (TmpElem != null) {
            elementLeft = elementLeft + TmpElem.offsetLeft;
            elementTop = elementTop + TmpElem.offsetTop;
            TmpElem = TmpElem.offsetParent;
        }
    }
    return elementTop;
}

// String.format START

// Use as
// (Static)var str = String.format("This is a {0} string using the {1} method.","formatted","static");
// (inline)var str = "This is a {0} string using the {1} method.".format("formatted","inline");

function _StringFormatInline()
{
	var txt = this;
	for(var i=0;i<arguments.length;i++)
	{
		var exp = new RegExp('\\{' + (i) + '\\}','gm');
		txt = txt.replace(exp,arguments[i]);
	}
	return txt;
}

function _StringFormatStatic()
{
	for(var i=1;i<arguments.length;i++)
	{
		var exp = new RegExp('\\{' + (i-1) + '\\}','gm');
		arguments[0] = arguments[0].replace(exp,arguments[i]);
	}
	return arguments[0];
}

if(!String.prototype.format)
{
	String.prototype.format = _StringFormatInline;
}

if(!String.format)
{
	String.format = _StringFormatStatic;
}
// String.format END
