var disableContextMenu = false;
if(disableContextMenu)
{
	document.oncontextmenu = function(){return false;};
	document.onselectstart = function(){return false;};
}

function printPage()
{
	window.print();
}

function SetDDLValue(el, val)
{
    try
    {             
        var selectedValue = val;            
        var valSet = false;
        for (var i = 0; i < el.options.length; i++) 
        {
            if(el.options[i].value.toLowerCase() == selectedValue.toLowerCase())
            { 
                el.selectedIndex = i;
                valSet = true;
            }
        }            
        if(!valSet)
            el.selectedIndex = 0;
    }
    catch(e)
    {
        ErrorHandler("SetDDLValue()", e);		    
    } 
}  

function CheckMaxLen(txt,maxLen) 
{
    try
    {
        if(txt.value.length > (maxLen-1)) 
        {
            var cont = txt.value;
            txt.value = cont.substring(0,(maxLen -1));
            return false;
        }
    }
    catch(e) 
    {
        ErrorHandler("CheckMaxLen()", e);		    
    }
}

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);						
	}
}	

function NormalizeQualifiedPath(qPath)
{
    try
    {
        qPath = strReplace(qPath, "\\", "/");
        qPath = strReplace(qPath, "\\\\", "/");
        return qPath;
    }
    catch (e)
    {
        ErrorHandler("NormalizeQualifiedPath()", e);
    }
}
	
function setBackgroundImage()
{
    try
    { 
        if(document.getElementById("tdImage1") != null)
        {            
            var tdEl = document.getElementById("tdImage1");
            var inputEls = tdEl.getElementsByTagName("input");            
            if(inputEls != null && inputEls.length > 0)
            {  
                var typeAttr = inputEls[0].getAttribute("type");                
                if(typeAttr != null && typeAttr == "file")
                    tdEl.className = "photoBackground";                
            } 
        }                 
        
        if(document.getElementById("tdImage2") != null)
        {
            var tdEl2 = document.getElementById("tdImage2");
            var inputEls2 = tdEl2.getElementsByTagName("input");            
            if(inputEls2!= null && inputEls2.length > 0)
            {
                var typeAttr2 = inputEls2[0].getAttribute("type");
                if(typeAttr2 != null && typeAttr2 == "file") 
                    tdEl2.className = "photoBackground";            
            } 
        }                            
    }
    catch(e)
    {
        ErrorHandler("setBackgroundImage()", 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 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 ASCIIToHtml(str)
{
    return str.replace(/\r\n/g,"<BR>");   
}

function HtmlToASCII(str)
{
    return str.replace(/<BR>/g,"\n");    
}

function ErrorHandler(methodName, e)
{
    try
	{
	    var message = "<Error pageName=\"" + document.title + "\" ";
		message += "method=\"" + methodName + "\" ";
		message += "description=\"" + HTMLEncode(e.message) + "\" ";	
		message += "number=\"" + (e.number + 0xFFFF) + "\"/>";
		logErrorToServer(message, "error");		
	}
	catch(e)
	{
		ErrorHandler("ErrorHandler()", 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 logErrorToServer(str, type)
{
	try
	{
	    WSErrors.Log(str, NavigateToErrorPage, OnTimeOut, OnError);	
	}
	catch(e)
	{
		ErrorHandler("logErrorToServer()", e);
	}
}

function NavigateToErrorPage()
{
    //document.location.href="../err/error.aspx";
}

function OnTimeOut() 
{
    //alert("Timed Out");    
    NavigateToErrorPage();
}

function OnError() 
{
    NavigateToErrorPage();
}

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);
	}
}
