var disableContextMenu = false;
if(disableContextMenu)
{
	document.oncontextmenu = function(){return false;};
	document.onselectstart = function(){return false;};
}

function printPage()
{
	window.print();
}

function ToggleElementVisibility(el, visible)
{
    try
	{	
	    if(el.style) 
            el.style.visibility = visible == true ? "visible" : "hidden";
        else
            el.visibility = visible == true ? 'show' : 'hide';                
	}
	catch(e)
	{
		ErrorHandler("ToggleElementVisibility()", e);						
	}        
}

function ChangeDisplayStyle(theElement, setTo) 
{
    try
    { 
        if(!theElement) 
        {
            /* The page has not loaded, or the browser claims to
            support document.getElementById or document.all but
            cannot actually use either */
            return;
        }
        
        //Reference the style ...
        if(theElement.style) 
        { 
            theElement = theElement.style; 
        }
        if( typeof( theElement.display ) == 'undefined' ) 
        {
            //The browser does not allow us to change the display style
            //Alert something sensible (not what I have here ...)
            window.alert( 'Your browser does not support this' );
            return;
        }
        
        //Change the display style
        theElement.display = setTo;
    }
	catch(e)
	{
		ErrorHandler("ChangeDisplayStyle()", e);						
	}         
}

function SetElementBackgroundColor(el, color)
{
    try
   {        
        if( el.style ) 
        { 
            el = el.style; 
        }
        if( el.background ) 
        {
            //supported by most browsers
            //like Gecko browsers and the IE series            
            el.background = color;
        }
        else if( el.backgroundColor ) 
        {
            //supported by most browsers
            el.backgroundColor = color;
        } 
        else if( el.bgColor ) 
        {
          //used by layers browsers            
          el.bgColor = color;
        } 
        else 
        {
          //FAILURE, there is no way to change the background colour
        }
   }
   catch(e)
   {
        ErrorHandler("SetElementBackgroundColor()", e);						
   } 
}

function getOuterHTMLFromID(objID)
{
    try
    { 
         var temp=document.getElementById(objID).cloneNode(true);
         document.getElementById('tempDiv').appendChild(temp);
         var outer=document.getElementById('tempDiv').innerHTML;
         document.getElementById('tempDiv').innerHTML="";
         return outer;
    }
    catch(e)
    {
        ErrorHandler("getOuterHTML()", e);						
    } 
}

function NewWin(path, windowName, height, width) 
{
	try
	{
	    height = isNaN(height) ? 400 : height;
	    width = isNaN(width) ? 400 : width;
	    var L = Math.ceil((window.screen.width  - width) - 150);		
		var T = Math.ceil(window.screen.height - (window.screen.height-50));			
		var newWindow = window.open(path,windowName,"fullscreen=0,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width="+width+",height="+height);
		if (newWindow != null)
			newWindow.moveTo(Math.ceil(L),Math.ceil(T));		
	}
	catch(e)
	{
		ErrorHandler("NewWin()", e);						
	}
}	
	
// replaces &, <, >, and " with encoded equivs
function HTMLEncode(s)
{
    try
    { 
        return s.replace(/\&/g, "&amp;").replace(/\</g, "&lt;").replace(/\>/g, "&gt;").replace(/\"/g, "&quot;");
    }
    catch(e)
    {
        return s;
    }     
}

function RemoveHtml(instr)
{
    try
    {
        var regEx = /<[^>]*>/g;
	    return instr.replace(regEx, "");
    }
	catch(e)
	{
		ErrorHandler("RemoveHtml()", e);
	}  
}

function URLEncode(sStr) 
{
    try
    {
        return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');                        
    }
    catch(e)
    {
        ErrorHandler("URLEncode()", e);		    
    } 
}

function URLDecode(sStr) 
{
    try
    {
        return unescape(sStr).replace(/\+/g, ' ').replace(/\"/g,'\"').replace(/\'/g, '\'').replace(/\//g,'\\');                        
    }
    catch(e)
    {
        ErrorHandler("URLDecode()", e);		    
    } 
}

function HTMLDecode(instr)
{
	try
	{
		var reAmp = /&amp;/gi;
		var reGt = /&gt;/gi;
		var reLt = /&lt;/gi;
		var reQut = /&quot;/gi;
		
		var outstr = instr.replace(reAmp, "&");
		outstr = outstr.replace(reGt, ">");
		outstr = outstr.replace(reLt, "<");
		outstr = outstr.replace(reQut, "\"");
	
		return outstr;		
	}
	catch(e)
	{
		ErrorHandler("HTMLDecode()", e);
	}
} 

function DisableAllDDLs()
{
    try
    {
        var ddls = document.getElementsByTagName("select");
        for(var i = 0; i < ddls.length; i++)
            ddls[i].style.visibility = "hidden";        
    }
    catch(e)
    {
        ErrorHandler("DisableAllDDLs()", e);
    }
}

function EnableAllDDLs()
{
    try
    {
        var ddls = document.getElementsByTagName("select");
        for(var i = 0; i < ddls.length; i++)
            ddls[i].style.visibility = "visible";        
    }
    catch(e)
    {
        ErrorHandler("DisableAllDDLs()", e);
    }
}

function GetChildElementById(parentEl, tagName, id)
{
    try
    { 
        var tagElements = parentEl.getElementsByTagName(tagName);
        for(var i = 0; i < tagElements.length; i++)
        {
            var tagEl = tagElements[i];
            if(tagEl != null && tagEl.id == id)
                return tagEl;  
        }            
        return null;
    }
    catch(e)
    {
        ErrorHandler("GetChildElementById()", e);
    }  
}

function StrEndsWith(str, suffix) 
{
    return (str.substr(str.length - suffix.length) == suffix);
}
function StrStartsWith(str, prefix) 
{
    return (str.substr(0, prefix.length) == prefix);
}
function StrLTrim(str) {
    return str.replace(/^\s*/, "");
}
function StrRTrim(str) {
    return str.replace(/\s*$/, "");
}
function StrTrim(str) 
{
    str = StrRTrim(str);
    return StrLTrim(str);     
}

function ErrorHandler(methodName, e)
{
    try
	{
	    var message = "<Error pageName=\"" + document.title + "\" ";
		message += "method=\"" + methodName + "\" ";
		message += "description=\"" + HTMLEncode(e.description) + "\" ";	
		message += "number=\"" + (e.number + 0xFFFF) + "\"/>";
		logErrorToServer(message, "error");
	}
	catch(e)
	{
	    alert("There has been an error trying to log the original error message: " + e.message);
		//ErrorHandler("ErrorHandler()", e);
	}	
}

function logErrorToServer(str, type)
{
	try
	{
	    WSErrors.Log(str, Stub, OnTimeOut);	
	}
	catch(e)
	{
	    alert("There has been an error logging message to the server: " + e.message + " and the original error was: " + str);
		//ErrorHandler("logErrorToServer()", e);
	}
}

function Stub()
{
    // do nothing here
}

function OnTimeOut() 
{
    alert("Timed Out");    
    //NavigateToErrorPage();
}

function NavigateToErrorPage()
{
    alert("In NavigateToErrorPage()");    
    //document.location.href="../err/error.aspx";
}


function StrEndsWith(str, suffix) 
{
    return (str.substr(str.length - suffix.length) == suffix);
}

function StrStartsWith(str, prefix) 
{
    return (str.substr(0, prefix.length) == prefix);
}

function StrLTrim(str) 
{
    return str.replace(/^\s*/, "");
}

function StrRTrim(str) 
{
    return str.replace(/\s*$/, "");
}

function StrTrim(str) 
{
    str = StrRTrim(str);
    return StrLTrim(str);     
}

// seachString, oldSearchStr, newReplaceStr
function strReplace(searchStr, oldSearchStr, newReplaceStr)
{
	try
	{
		searchStr = searchStr.split(oldSearchStr).join(newReplaceStr);
		return searchStr;
	}
	catch(e)
	{
		ErrorHandler("strReplace()", e);
	}
}