//Common Script
function replaceDeep(s, s1, s2)
{
	var a = '';
	while(a != s)
	{
		a = s;
		s = s.replace(s1, s2);
	}
	return s;
}

// this function is for confirming the Reset action
function ConfirmReset()
{
	if (confirm('Resetting this filing will erase all the existing data (if any), continue?'))
	{
	    // it makes no sense to ask the user to save before Resetting. Therefore, we need to bypass the is_dirty functionality by clearing its flag.
	    clear_dirty('');
	    return true;
	}
	else
	{
	    // Cancel the bubble up, so that we don't hit the is_dirty functionality. Otherwise, is_dirty will effectively hide our response.
	    StopEvent(event);
	    return false;
	}
}

// this function cancels the event bubble up. 
function StopEvent(pE)
{
    if (!pE)
    {
        if (window.event)
            pE = window.event;
        else
            return;
    }
   if (pE.cancelBubble != null)
      pE.cancelBubble = true;
   if (pE.stopPropagation)
      pE.stopPropagation();
   if (pE.preventDefault)
      pE.preventDefault();
   if (window.event)
      pE.returnValue = false;
   if (pE.cancel != null)
      pE.cancel = true;
}  

// this function calls a server's function. the url parameter should be set to the value of the ClientCallsURL variable, which points to the ClientCalls.ashx handler
function callServerFunction(url, fname, params)
{
    params = params.replace("&", "AmPeRsAnD")
    params = params.replace("?", "QuEsTiOnMaRk")
    params = params.replace(" ", "%20")

    var pageUrl = url + "?fname=" + fname + "&params=" + params;
    var xReq = getXmlhttpObject();
    if(xReq)
    {
        xReq.open("POST", pageUrl, false);
        xReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xReq.send(null);
        return xReq.responseText;
    }
    else
    {
        //alert("This browser does not support an XmlHttpObject.");
        return "browser error";
    }
}

// this function gets the XmlhttpObject. DO NOT REMOVE the commented section below
function getXmlhttpObject()
{
    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
    // JScript gives us Conditional compilation, we can cope with old IE versions
    // and security blocked creation of the objects.
    try
    {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        try
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(E)
        {
            xmlhttp = false;
        }
    }
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined')
    {
        try
        {
            xmlhttp = new XMLHttpRequest();
        }
        catch(e)
        {
            xmlhttp=false;
        }
    }
    if (!xmlhttp && window.createRequest)
    {
        try
        {
            xmlhttp = window.createRequest();
        }
        catch (e)
        {
            xmlhttp=false;
        }
    }
    return xmlhttp;
}

function InitializeRequest(sender, args) 
{
    $get('MasterDiv').style.cursor = 'wait';
    $get(args._postBackElement.id).disabled = true;
}

function EndRequest(sender, args) 
{
    if (args.get_error() != undefined)
    {
        //alert(args.get_response().get_responseData());
        args.set_errorHandled(true); 
    }

    var mstr = $get('MasterDiv');
    if (mstr)
    {
        mstr.style.cursor = 'auto';
    }
    
    if (sender._postBackSettings.sourceElement.id != '' && $get(sender._postBackSettings.sourceElement.id))
    {
        $get(sender._postBackSettings.sourceElement.id).disabled = false;                
    }
    else if (sender._postBackSettings.sourceElement.id)
    {
        sender._postBackSettings.sourceElement.id.disabled = false;
    }
}

function trim(s) 
{
	return s.replace(/^\s+|\s+$/g,"");
}

function ltrim(s) 
{
	return s.replace(/^\s+/,"");
}

function rtrim(s) 
{
	return s.replace(/\s+$/,"");
}

// ToggleVisibility script
function toggle(obj)
{
    if(obj && obj.style)
    {
        obj.style.display = (obj.style.display == 'none') ? 'block' : 'none';
    }
}    

function Opt(obj, RootURL)
{
    var selec = obj.options[obj.selectedIndex];
    var attr = selec.attributes.getNamedItem("target");
    var url = selec.value.replace('~', RootURL);
    
	if(attr && attr.value == 'external')
	{
	    obj.selectedIndex = 0;
		var win = window.open(url,'','resizable=yes,topmost,scrollbars=yes');
	}
	else
	{
	    window.location = url;
	}
	
	return true;	

//	var vUrl = obj.children(obj.selectedIndex).value;
//	var attr = vUrl.indexOf(' target=external')
//	if(attr > -1)
//	{
//	    obj.selectedIndex = 0;
//	    vUrl = vUrl.replace(' target=external', '');
//		var win = window.open(vUrl,'','resizable=yes,topmost,scrollbars=yes');
//		return true;	
//	}
//	else
//	{
//		return false;
//	}

}

function adjustIFrameSize (iframeWindow)
{
    if (iframeWindow.document.height) 
    {
        var iframeElement = document.getElementById('ctl00_H0_'+iframeWindow.name);
        iframeElement.style.height = iframeWindow.document.height + 15 + 'px';
        //iframeElement.style.width = iframeWindow.document.width + 5 + 'px';
    }
    else if (document.all) 
    {
        var iframeElement = document.all[iframeWindow.name];
        if(iframeElement)
        {
            if (iframeWindow.document.compatMode && iframeWindow.document.compatMode != 'BackCompat') 
            {
                  iframeElement.style.height = iframeWindow.document.documentElement.scrollHeight + 5 + 'px';
                  //iframeElement.style.width = iframeWindow.document.documentElement.scrollWidth + 5 + 'px';
            }
            else 
            {
                try
                {
                    iframeElement.style.height = iframeWindow.document.body.scrollHeight + 5 + 'px';
                    //iframeElement.style.width = iframeWindow.document.body.scrollWidth + 5 + 'px';
                }
                catch(ex){}
            }
        }
    }
}

//var giframeZone;
//function isNotificationsZoneActive(iframeZone)
//{
//    giframeZone = iframeZone;
//    setTimeout("isZoneActive()", 500);
//}
//function isZoneActive()
//{
//    var iframeElement = document.getElementById(giframeZone);
//    
//    if(iframeElement)
//    {
//        var doc = iframeElement.contentWindow.document;
//        var a = doc.getElementsByTagName("a");

//        if(a && a.length > 0)
//        {
//            //alert('zone contains '+(a.length)+' link(s).');
//            a[0].removeAttribute('href');
//            a[0].style.cursor = "default";
//            
//        }
//        else
//        {
//            //alert('zone is empty');
//            //iframeElement.style.height = 0;
//        }
//    }
//}

function UnEscapeLanguage(ddlLangClientId)
{
    var LangCtl = document.getElementById(ddlLangClientId);

    if(LangCtl)
    {
        for(var i = 0; i < LangCtl.options.length; i++)
        {
            var itm = LangCtl.options[i];
            itm.innerHTML = itm.text;
        }
    }
}
