//-------------------------------------------------------------------
function ConfigPageRequestManager(){
    if (typeof(Sys) == 'undefined')
    {
        return;
    }
    var _prm = Sys.WebForms.PageRequestManager.getInstance();
    _prm.add_initializeRequest(function(sender, args)
    {
        //Get the element which cause the postback
        var postBackElement = args.get_postBackElement();
        //alert(postBackElement.id);
        postBackElement.blur();
        postBackElement.disabled = true;
    });
    
}
AttachEvent(window, "onload", ConfigPageRequestManager);
//-------------------------------------------------------------------
//Virtual desktop function
//-------------------------------------------------------------------
// GLOBAL FUNCTIONS
//-------------------------------------------------------------------
function ChangeLocation(szURL)
{
	if (szURL!="") {
		window.location = szURL;
	}
}
//-------------------------------------------------------------------
function OpenWindow(szURL)
{
	if (szURL!="") {
		window.open(szURL);
	}
}
//-------------------------------------------------------------------
function AttachEvent(obj, eventName, eventHandler) {
    if(obj) {
        if(eventName.substring(0, 2) == 'on') {
            eventName = eventName.substring(2,eventName.length);
        }
        if (obj.addEventListener){
            obj.addEventListener(eventName, eventHandler, false);
        } else if (obj.attachEvent){
                obj.attachEvent('on'+eventName, eventHandler);
            }
    }
}
function DispatchEvent(obj, eventName){
    if(obj){
        if(eventName.substring(0, 2) == 'on') {
            eventName = eventName.substring(2,eventName.length);
        }
    
        if(obj.dispatchEvent){
            obj.dispatchEvent('on'+eventName);
        }
        else if(obj.fireEvent){
            obj.fireEvent('on'+eventName);
        }
    }
}
//-------------------------------------------------------------------
function GGetVirtualDesktopInstance(){
    return clsVirtualDesktop.getInstance();
}
//-------------------------------------------------------------------
AttachEvent(window, "onload", function(){
        if(typeof(oVirtualDesktop) != "undefined"){
            oVirtualDesktop = GGetVirtualDesktopInstance();
        }
    }
);
//-------------------------------------------------------------------
//---------------------------------------------------------------------
function GShowECityConnectorResponseError(responseString){
    //Get Response xml
    var oXML = new clsXmlParser();
    oXML.LoadXML(responseString);
    
    var nErr = -1;
    var nodes = oXML.SelectNodes("/eCityConnectorResponse/ResponseStatus/Status");
    if(nodes.length > 0){
        nErr = parseInt(oXML.GetNodeText(nodes[0]));
    }
	
    var szErrMsg = "";
    nodes = oXML.SelectNodes("/eCityConnectorResponse/ResponseStatus/ErrorMessage");
    if(nodes.length > 0){
        szErrMsg = oXML.GetNodeText(nodes[0]);
    }
	
    //Show response error
    if(nErr != 0){
        oVirtualDesktop.ShowMsgBox("Error", szErrMsg);
        return - 1;
    }
    
    return 0;
}

//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
function GLoadScript(szScript, szScriptID){
    try{
        if(szScriptID == null) szScriptID = "";
        
	    
        var oParent = window;
        if(oParent.parent != null) oParent = oParent.parent;
        
        //Clear Current Load Script
        if( (typeof(oParent.$LoadScript)).toLocaleLowerCase() == "function"){
            oParent.$LoadScript = null;
        }
        oParent.$LoadScript = Function(szScript);
        oParent.$LoadScript();
        
        delete oParent.$LoadScript;
        oParent.$LoadScript = null;
        
    }//End try
    catch(ex){
        //do nothing
    }
}
//-------------------------------------------------------------------
function GLoadScript1(szScript, szScriptID){
    try{
        if(szScriptID == null) szScriptID = "";
        
	    var oDocument = document;
        var oFrames = frames;
        if(parent != null){
            oDocument = parent.document;
            oFrames = parent.frames;
        }

        var iframe = oDocument.getElementById("__DynamicRunscript_Iframe_" + szScriptID )
        if(iframe != null) oDocument.body.removeChild(iframe);

        // create an <iframe>
        iframe = oDocument.createElement("iframe");
        iframe.id = "__DynamicRunscript_Iframe_" + szScriptID;
        iframe.style.display = "none";
        oDocument.body.appendChild(iframe);

        
        var szContent = "<script>"+
            'var oParent = window;\n' +
            'if(oParent.parent != null) oParent = oParent.parent;\n' +
            'function GGetVirtualDesktopInstance(){\n' +
                'if(oVirtualDesktop != null) return oVirtualDesktop;\n' +
                'if(oParent.oVirtualDesktop != null)\n' +
                'return oParent.oVirtualDesktop;\n' +    
                'return null;\n' +
            '};\n' +
            'var oVirtualDesktop = GGetVirtualDesktopInstance();\n' +
            'function GEvalFunction(){\n' +
                szScript +
            '}\n' +
            "GEvalFunction();"+
            "<\/script>";    
        oFrames[oFrames.length - 1].document.write(szContent);
        
    }//End try
    catch(ex){
        //Do nothing
    }
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
function ShowPopup(URL, Width, Height){
    
    var szConfig = ""
        szConfig += 'height=' + Height;
        szConfig += ', width=' + Width
        szConfig += ', status=yes, toolbar=no, menubar=no, location=no';
        
    window.open(
        URL,
        null, 
        szConfig
    ); 
    
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// insertAdjacentHTML(), insertAdjacentText() and insertAdjacentElement()
// for Netscape 6/Mozilla by Thor Larholm me@jscript.dk
// Usage: include this code segment at the beginning of your document
// before any other Javascript contents.

if(typeof HTMLElement!="undefined" && !
HTMLElement.prototype.insertAdjacentElement){
HTMLElement.prototype.insertAdjacentElement = function
(where,parsedNode)
{
switch (where){
case 'beforeBegin':
this.parentNode.insertBefore(parsedNode,this)
break;
case 'afterBegin':
this.insertBefore(parsedNode,this.firstChild);
break;
case 'beforeEnd':
this.appendChild(parsedNode);
break;
case 'afterEnd':
if (this.nextSibling)
this.parentNode.insertBefore(parsedNode,this.nextSibling);
else this.parentNode.appendChild(parsedNode);
break;
}
}

HTMLElement.prototype.insertAdjacentHTML = function
(where,htmlStr)
{
var r = this.ownerDocument.createRange();
r.setStartBefore(this);
var parsedHTML = r.createContextualFragment(htmlStr);
this.insertAdjacentElement(where,parsedHTML)
}


HTMLElement.prototype.insertAdjacentText = function
(where,txtStr)
{
var parsedText = document.createTextNode(txtStr)
this.insertAdjacentElement(where,parsedText)
}
}
//-------------------------------------------------------------------
function getPageSizeWithScroll(){

    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } 
    if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }
  
    arrayPageSizeWithScroll = new Array(myWidth,myHeight);
    //alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );
    return arrayPageSizeWithScroll;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//Created 2008-04-21
// Safari and opera 
// insertAdjacentHTML function prototype
if (document.childNodes&&!document.childNodes[0].insertAdjacentHTML){

    HTMLElement.prototype.insertAdjacentHTML = function (sWhere, sHTML) {
            var df;
            var r = this.ownerDocument.createRange();
            switch (String(sWhere).toLowerCase()) {
                case "beforebegin":
                    r.setStartBefore(this);
                    df = r.createContextualFragment(sHTML);
                    this.parentNode.insertBefore(df, this);
                    break;

                case "afterbegin":
                    r.selectNodeContents(this);
                    r.collapse(true);
                    df = r.createContextualFragment(sHTML);
                    this.insertBefore(df, this.firstChild);
                    break;

                case "beforeend":
                    r.selectNodeContents(this);
                    r.collapse(false);
                    df = r.createContextualFragment(sHTML);
                    this.appendChild(df);
                    break;

                case "afterend":
                    r.setStartAfter(this);
                    df = r.createContextualFragment(sHTML);
                    this.parentNode.insertBefore(df, this.nextSibling);
                    break;
        }
    };
}
//-------------------------------------------------------------------
function autofitIframe(id, maxHeight)
{	
		
	/*if (!window.opera && (!document.mimeType) && document.all && document.getElementById)
	{
		if(parent.document.getElementById(id))
		{
			ScrollHeight=this.document.body.offsetHeight;
			if (ScrollHeight>maxHeight) ScrollHeight = maxHeight;
			parent.document.getElementById(id).style.height = ScrollHeight + "px";
		}
	}
	else */if(document.getElementById) 
	{
		if(parent.document.getElementById(id))
		{
			ScrollHeight=this.document.body.scrollHeight;
			if (ScrollHeight>maxHeight) ScrollHeight = maxHeight;
			parent.document.getElementById(id).style.height = ScrollHeight + "px"
		}
	}
	
} 
//-------------------------------------------------------------------
// Call click event for element
function CallClickEvent(szID){
    var obj = document.getElementById(szID);
    if(obj == null) return;
    if (obj.dispatchEvent)
    {
        var e = document.createEvent("MouseEvents");

        e.initEvent("click", true, true);
        obj.dispatchEvent(e);
        
    }
    else
    {
        obj.click();
    }
}
//-------------------------------------------------------------------
function DisableElementScrollBar(elem){
    //Hide all scroll bar
    elem.setAttribute("scrolling", "no");
    if(typeof(elem.scrolling) != 'undefined'){
        elem.scrolling = "no";
    }
    if(typeof(elem.style.overflow) != 'undefined'){
        elem.style.overflow = "hidden";
    }
}
//-------------------------------------------------------------------
function EnableElementScrollBar(elem){
    //Hide all scroll bar
    elem.setAttribute("scrolling", "yes");
    if(typeof(elem.scrolling) != 'undefined'){
        elem.scrolling = "yes";
    }
    if(typeof(elem.style.overflow) != 'undefined'){
        elem.style.overflow = "auto";
    }
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
function SetAttribute(szID, szAttributeName, szAttributeValue){
    var oElem = document.getElementById(szID);
    if (oElem==null) return;
    oElem.setAttribute(szAttributeName, szAttributeValue);
}
//-------------------------------------------------------------------
function getScrollBarWidth () {
    var inner = document.createElement('p');
    inner.style.width = "100%";
    inner.style.height = "200px";

    var outer = document.createElement('div');
    outer.style.position = "absolute";
    outer.style.top = "0px";
    outer.style.left = "0px";
    outer.style.visibility = "hidden";
    outer.style.width = "200px";
    outer.style.height = "150px";
    outer.style.overflow = "hidden";
    outer.appendChild (inner);

    document.body.appendChild (outer);
    var w1 = inner.offsetWidth;
    outer.style.overflow = 'scroll';
    var w2 = inner.offsetWidth;
    if (w1 == w2) w2 = outer.clientWidth;

    document.body.removeChild (outer);

    return (w1 - w2);
};
//-------------------------------------------------------------------
//-------------------------------------------------------------------