﻿// JScript File
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}
}

function addLoadEvent1(func, param1)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = function()
    	{
    	    func(param1);
    	}
	} else {
		window.onload = function(){
		oldonload();
		func(param1);
		}
	}
}

function ShowModalPopup(popUpId)
{
    var overlay = document.getElementById("overlay");
    var objPopUp = document.getElementById(popUpId);
    
    overlay.style.display = "block";
    objPopUp.style.visibility = "visible";
    var pageSize = getPageSize(objPopUp);
    
    overlay.style.width = pageSize[0] + "px";
    overlay.style.height = pageSize[1] + "px";
    
    objPopUp.style.top = pageSize[7] + pageSize[3]/2 - pageSize[5]/2 + "px";
    objPopUp.style.left = pageSize[6] + pageSize[2]/2 - pageSize[4]/2 + "px";
}

function CloseModalPopup(popUpId)
{
    var overlay = document.getElementById("overlay");
    var objPopUp = document.getElementById(popUpId);
    
    objPopUp.style.visibility = "hidden";
    overlay.style.display = "none";
}


/*
Browser Sniffer Script-
*/

function browserSniffer(browserCode, browserVersion) 
{
	var agt1 = navigator.userAgent.toLowerCase();
	var agt2 = navigator.appVersion.toLowerCase();
	if(browserCode==1)
	{	//check for IE
		if(browserVersion == 0)
		{
			if(agt2.indexOf("msie")!=-1)
				return true;
		}
		else
		{
			if(agt2.indexOf("msie " + browserVersion)!=-1)
				return true;
		}
	}
	else if(browserCode==2)
	{	//check for mozilla
		if(agt1.indexOf("firefox")!=-1)
			return true;
	}
	else if(browserCode==3)
	{	//check for safari
		if(agt2.indexOf("safari")!=-1)
			return true;
	}
	return false;
}


function getPageSize(objLayer)
{
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body != null && document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

    var layerHeight = 0, layerWidth = 0;
    if(objLayer != null)
    {
        layerHeight = objLayer.clientHeight;
	    layerWidth = objLayer.clientWidth;
	}
	
	var scrollTop = document.documentElement.scrollTop;
	if(scrollTop==0)
		scrollTop = document.body.scrollTop;
		
	var scrollLeft = document.documentElement.scrollLeft;
	if(scrollLeft==0)
		scrollLeft = document.body.scrollLeft;

	arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight, layerWidth, layerHeight, scrollLeft, scrollTop) 
	return arrayPageSize;
}

function AdjustObjectWidthCustom(obj, width, attribute, reduceCount)
{   
     var sizeReduced = false;
     if(obj != null)
     {
        while(obj.offsetWidth > width)
        {            
            obj.attributes[attribute].value = obj.attributes[attribute].value.substring(0, obj.attributes[attribute].value.length - reduceCount);
            sizeReduced = true;
        }
        if(sizeReduced)
            obj.attributes[attribute].value += "...";
     }
}

function AdjustObjectWidth(obj, width, reduceCount)
{   
     var sizeReduced = false;
     if(obj != null)
     {
        while(obj.offsetWidth > width)
        {            
            obj.innerHTML = obj.innerHTML.substring(0, obj.innerHTML.length - reduceCount);
            sizeReduced = true;
        }
        if(sizeReduced)
            obj.innerHTML += "...";
     }
}

function GetURL()
{
	var url = document.URL;
	var indexOfQues = url.indexOf("?");
	if(indexOfQues!=-1)
	{
		url = url.substring(0, indexOfQues);
	}
	return url;
}

function GetPageName()
{
	var url = GetURL();
	var pageName = "";
	if(url.substring(url.length - 1) == '/' || url.substring(url.length - 1) == '\\')
	{
	    url = url.substring(0, url.length - 1);
	}
	var lastIndexOfSlash = url.lastIndexOf("/");
	if(lastIndexOfSlash == -1)
	    lastIndexOfSlash = url.lastIndexOf("\\");
	if(lastIndexOfSlash != -1)
	{
	    pageName = url.substring(lastIndexOfSlash + 1);
	}
	return pageName.toLowerCase();
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime( today.getTime() );

    /*
    if the expires variable is set, make the correct
    expires time, the current script below will set
    it for x number of days, to make it for hours,
    delete * 24, for minutes, delete * 60 * 24
    */
    if ( expires )
    {
        expires = expires * 1000 * 60;
    }
    var expires_date = new Date( today.getTime() + (expires) );

    document.cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
}


// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


function hideEmptyContainersInLiveMode()
{
    $("div[FwObjectId] > span").each( function() 
    {
        if(this.innerHTML == "&nbsp;")
            this.parentNode.style.display = "none";
    })
 }


function HideDeafultText(objTextBox)
{
    if(objTextBox.getAttribute("defaultSetValue") == null)
    {
        objTextBox.setAttribute("defaultSetValue", objTextBox.value);
    }
    if(objTextBox.value == objTextBox.getAttribute("defaultSetValue"))
        objTextBox.value = "";
}

function ShowDeafultText(objTextBox)
{
    if(objTextBox.value == "")
    {
        if(objTextBox.getAttribute("defaultSetValue") != null)
        {
            objTextBox.value = objTextBox.getAttribute("defaultSetValue");
        }
    }
}

function HasTextChanged(objTextBox)
{
    var returnValue = false;
    
    if(objTextBox.value != "" && objTextBox.getAttribute("defaultSetValue") != null)
    {
        if(objTextBox.value != objTextBox.getAttribute("defaultSetValue"))
            returnValue = true;
    }
    
    return returnValue;
}

function fnTrapKD(btnID)
{
    if (event.keyCode == 13)
    {
        var btn = document.getElementById(btnID);
        event.returnValue=false;
        event.cancel = true;
        if (btn)
            btn.click();
    }
}

function getQueryString(key)
{
	var pageURL = "";
	pageURL = location.search;
	if(pageURL == "")
		return "";
	var indexOfKey = pageURL.indexOf("?" + key + "=");
	if(indexOfKey == -1)
		indexOfKey = pageURL.indexOf("&" + key + "=");
	if(indexOfKey == -1)
		return "";
	pageURL = pageURL.substring(indexOfKey+1);
	var indexOfEqualSign = pageURL.indexOf("=");
	if(indexOfEqualSign==-1)
		return "";
	var indexOfAmperSign = pageURL.indexOf("&");
	var queryString = "";
	if(indexOfAmperSign==-1)
		queryString = pageURL.substring(indexOfEqualSign + 1);
	else
		queryString = pageURL.substring(indexOfEqualSign + 1, indexOfAmperSign);
	return queryString;
}

function AddCondtion(filterCriteria, dataField, condition, like, addingClause)
{
    var likeChar = "%";
    var compareClause = "like"
    if(!like)
    {
        likeChar = "";
        compareClause = "=";
    }
    if(condition != "")
    {
        if(filterCriteria == "")
            filterCriteria = dataField + " " + compareClause + " '" + likeChar + condition + likeChar + "'";
        else
            filterCriteria = "( " + filterCriteria + " ) " + addingClause + " " + dataField + " " + compareClause + " '" + likeChar + condition + likeChar + "'";
    }
    
    return filterCriteria;
}