﻿// JScript File
function Redirect(strPage)
{
	window.open(strPage, '_self');
}

function LogError(strError, strLoggedError)
{
	//TODO: Attempt to call a webservice with error info and get back a result
	return 0;
}

function AlertError(strUserError, strLoggedError, iSeverity)
{

}

function CheckAJAXResult(oResult)
{
	var bValid			= false;
	
	if (oResult == null)
		AlertError(	"The operation could not be completed.",
					"AJAX call returned a null result",
					1);
					
	else if (oResult.error != null)
	{	AlertError(	"The opertion could not be completed.",
					oResult.error,
					1);
	    trace(oResult.error)
	}
	else bValid			= true;
	
	return bValid;
}

function IgnoreAJAXResult(oResult)
{
	if (null != oResult && null != oResult.error)
		LogError("The operation could not be completed.", oResult.error);
}		

function RedirectAJAXResult(oResult, strPage)
{
	if (CheckAJAXResult(oResult))
		Redirect(strPage);
}
	
function IsNullOrEmpty(strTest)
{
	return (strTest == null || strTest.length == 0);
}
	
function Confirm(strMessage, strOK, strCancel)
{
	if (!IsNullOrEmpty(strOK))
		strMessage += "\n\n\t- OK: " + strOK;
		
	if (!IsNullOrEmpty(strCancel))
		strMessage += "\n\t- Cancel: " + strCancel;
		
	return confirm(strMessage);
}


function Hide(oVar)
{
	if (null != oVar)
	    $(oVar).setStyle("display", "none");
}

function ShowBlock(oVar)
{
	if (null != oVar)
		$(oVar).setStyle("display", "block");
}


function ShowHover(oVar)
{
	if (null != oVar)
	{
		$(oVar).setStyle("display", "block");
		$(oVar).setStyle("position", "absolute");
	}
}

function ShowInline(oVar)
{
	if (null != oVar)
		oVar.style.display	= "inline";
}

function IsEnter(e)
{
	if (e.keyCode == 13)
		return true;
	else
		return false;
}

function AddOnload(oVar)
{
    if (document.addEventListener) {
	    document.addEventListener("DOMContentLoaded", oVar, false);
    }
    else
      window.attachEvent('onload', function() {oVar(window.event)});
}


function AddOnUnload(oVar)
{
    if (document.addEventListener) {
	    document.addEventListener("DOMContentUnloaded", oVar, false);
    }
    else
      window.attachEvent('onbeforeunload', function() {oVar(window.event)});
}


 function addEvent2(obj, type, fn) 
 {
    if (obj.attachEvent) 
    {
        obj['e'+type+'fn'] = fn;
        obj[type+'fn'] = function()
        {   obj['e'+type+'fn']( window.event );  }
        obj.attachEvent('on' + type, obj[type+'fn'] );
    } 
    else
       obj.addEventListener( type, fn, false );
 }

 function replaceEvent(obj, type, fn)
 {
    $(obj).addEvent(type, function(e) { fn(); var e2 = new Event(e); e2.preventDefault(); e2.stopPropagation(); return false;});
 }
 


function cancelEvent(e) {
    var e = e || window.event;

    e.cancelBubble = true; // for IE
    if (typeof e.stopPropagation == 'function')
        e.stopPropagation();

    e.returnValue = false; // for IE
    if (typeof e.preventDefault == 'function')
        e.preventDefault();
}

function trace( msg ){
  if( typeof( jsTrace ) != 'undefined' ){
    jsTrace.send( msg );
  }
}

	function FixEvent (event) {
		if (!event) event = window.event

		if (event.target) {
			if (event.target.nodeType == 3) event.target = event.target.parentNode
		} else if (event.srcElement) {
			event.target = event.srcElement
		}

		return event
	}


function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}

function DisableBG()
{
    grayOut(true, {'opacity':'50', 'bgcolor':'#8fa1b7', 'zindex':'1' });	
}

function EnableBG()
{
    grayOut(false);	
}