﻿// Common Javascripts used in several pages are put here!

//Global definitions
document.onkeydown = SetKeyPressAsStr;
var g_pressedKeyAsStr = "";
 
 //Simple browesr detection
var g_isIE = (window.ActiveXObject)? true:false;
var g_isMozilla = (document.implementation.createDocument)? true:false;
 
 /*
 * Cross-Browser 'Get'-functions
 */
/* CB - 'getElementById' */
function $(p_elementId) 
{
    try
    {    
        if(document.all) 
        { 
            return document.all[p_elementId]; 
        } 
        else 
        { 
            return document.getElementById(p_elementId); 
        }
    }
    catch(error)
    {
        window.status = error.message;
        return null;
    } 
}
/* CB - 'innerText' */
function GetInnerText(p_elementId) 
{
    var obj = $(p_elementId); 
    var _innerText = obj.innerText;
	if (null == _innerText || typeof(_innerText) == 'undefined') {
	    if (obj.innerHTML)
	        _innerText = obj.innerHTML.replace(/<[^>]+>/g, "");
	    else
	        _innerText = "";
	}
	return _innerText;
}

/* Resize frame */
function ResizeFrame()
{
    try
	{
		// Not a frame
		if (top == self) 
			return;

		var parent  = window.parent;
		var content = window.document.getElementsByTagName("BODY")[0];
		var shuttle = self.frameElement;
		var ie = !!content.currentStyle;

		var resize = function(ev,flag) {
//				shuttle.style.width  = Math.max(content.scrollWidth, content.offsetWidth)+20 + "px";
			var newHeight = (content.scrollHeight + 20);
			if ( newHeight > parseInt(shuttle.style.height,10) )
				shuttle.style.height = (content.scrollHeight + 20) + "px";
			if (ie) flag || window.setTimeout( function(){resize(ev,1);}, 1);
		};
		resize();
	}
	catch(error)
	{
	}
}

function SetKeyPressAsStr(p_sender)
{
    var KeyID = (window.event) ? event.keyCode : p_sender.keyCode;
    switch(KeyID)
    {
        case 16:
            g_pressedKeyAsStr = "Shift";
        break;       
        case 17:
            g_pressedKeyAsStr = "Ctrl";
        break;      
        case 18:
            g_pressedKeyAsStr = "Alt";
        break;      
        default:
            g_pressedKeyAsStr = "";     
    }
}

function GetStyleObject(p_elementId) 
{
	if(document.getElementById && document.getElementById(p_elementId)) 
	{
		return document.getElementById(p_elementId).style;
	} 
	else if (document.all && document.all(p_elementId)) 
	{
		return document.all(p_elementId).style;
	} 
	else 
	{
		return null;
	}
}

function GetURLParamKeyValue( p_urlStr, p_strParamName)
{
	var strReturn = "";
  	var strHref = p_urlStr;
  	
  	if ( strHref.indexOf("?") > -1 )
  	{
    	var strQueryString = strHref.substr(strHref.indexOf("?")).toLowerCase();
    	var aQueryString = strQueryString.split("&");
    	for ( var iParam = 0; iParam < aQueryString.length; iParam++ )
    	{
    		if ( aQueryString[iParam].indexOf(p_strParamName.toLowerCase() + "=") > -1 )
    		{
        		var aParam = aQueryString[iParam].split("=");
       			strReturn = aParam[1];
        		break;
     		}
    	}
  }
  return strReturn;
} 

function DocumentSize(p_width, p_height)
{
    this.Width = p_width;
    this.Height = p_height;
}

function GetDocumentWidth()
{
    return GetDocumentSize().Width;
}

function GetDocumentHeight()
{
    return GetDocumentSize().Height;
}

function GetDocumentSize()
{
    var width = 0;
    var height = 0;
    if (window.innerHeight && window.scrollMaxY) // Mozilla
    { 
	    width = window.innerWidth + window.scrollMaxX;
	    height = window.innerHeight + window.scrollMaxY;
    } 
    else if (document.documentElement) // IE
    { 
        width = document.documentElement.scrollWidth;
	    height = document.documentElement.scrollHeight;
    } 
    else // the rest..
    {
	    width = document.body.offsetWidth;
	    height = document.body.offsetHeight;
    }
//alert("Window width : " + width + ", and height : " + height );

    return new DocumentSize(width, height);
}

function resizeMe()
{
	if (window.parent && window.parent.resizeIFrame) 
	{
		window.parent.resizeIFrame('mainPlaceHolderIFrame', GetDocumentHeight() );
	}
}

/*
*   AJAX Status
*/
function Tool_SetWaitStatus(p_isWaiting, p_msg, p_refreshInfoControl, p_refreshTopMasterControls, p_hideInfoTitle)
{
    if ( p_isWaiting )
    {
	    if( typeof PIC_ShowAjaxStatusMsg == "function" )
	    {
	        Tool_SetWaitCursor();
	        var hideInfoTitle = (typeof(p_hideInfoTitle) != "undefined" ? p_hideInfoTitle : false);
	        PIC_ShowAjaxStatusMsg(p_msg, null, hideInfoTitle);		
	    }
    }
    else
    {
	    if( typeof PIC_SetAjaxStatusMsg == "function" )
            PIC_SetAjaxStatusMsg(p_msg);
        if( typeof PIC_AutoHideAjaxStatusMsg == "function" )
        {
            var refreshInfo = (typeof(p_refreshInfoControl) != "undefined" ? p_refreshInfoControl : false);
            var refreshTopMaster = (typeof(p_refreshTopMasterControls) != "undefined" ? p_refreshTopMasterControls : false);
            Tool_SetCursor("default");          
	        PIC_AutoHideAjaxStatusMsg(refreshInfo, refreshTopMaster);
	    }
    }
}

function Tool_HideWaitStatus(p_refreshInfoControl, p_refreshTopMasterControls)
{
    if( typeof PIC_HideAjaxStatusMsg == "function" )
    {
        var refreshInfo = (typeof(p_refreshInfoControl) != "undefined" ? p_refreshInfoControl : false);
        var refreshTopMaster = (typeof(p_refreshTopMasterControls) != "undefined" ? p_refreshTopMasterControls : false); 
        Tool_SetCursor("default");       
        PIC_HideAjaxStatusMsg(refreshInfo, refreshTopMaster);
    }
}

function Tool_SetStatusMsg(p_msg, p_statusIcon, p_hideInfoTitle, p_showCloseBtn)
{
    Tool_ResetCursor();
    var statusIcon = (typeof(p_statusIcon) != "undefined" ? p_statusIcon : null);
    var hideInfoTitle = (typeof(p_hideInfoTitle) != "undefined" ? p_hideInfoTitle : false);
    var showCloseBtn = (typeof(p_showCloseBtn) != "undefined" ? p_showCloseBtn : false);
    PIC_ShowAjaxStatusMsg(p_msg, statusIcon, hideInfoTitle, showCloseBtn);
}

function Tool_SetCursor(p_cursor)
{
    document.body.style.cursor = p_cursor;
}

function Tool_SetWaitCursor()
{
    Tool_SetCursor("wait");
}

function Tool_ResetCursor()
{
    Tool_SetCursor("default");
}


//Uncomment row below to test if this file is read.
//alert("Have read Global.js...")
