/*
	This javascript file is included in the menu control, so is available to 
	all pages.  Put javascript functions in here that are used by more than one page
*/

function Trim(inputstringTrim) {
	fixedTrim = "";
	lastCh = " ";
	for (x = 0; x < inputstringTrim.length; x++) {
		ch = inputstringTrim.charAt(x);
		if ((ch != " ") || (lastCh != " ")) { fixedTrim += ch; }
		lastCh = ch;
	}
	if (fixedTrim.charAt(fixedTrim.length - 1) == " ") {
		fixedTrim = fixedTrim.substring(0, fixedTrim.length - 1);
	}
	return fixedTrim;
}

function openEAWindow(strUrl)
{
	iheight = 650;
	iwidth = 990;
	itop = (((screen.availHeight) - iheight - 80) / 2);
	ileft = (((screen.availWidth) - iwidth) / 2);

	var windowOptions = "scrollbars=no,toolbar=no,menubar=no,resizable=yes,status=no,width=" + iwidth + ",height=" + iheight + ",top=" + itop + ",left=" + ileft;
		
	window.open('EAWaitScreen.aspx?url=' + escape(strUrl), '_blank', windowOptions);
}

function openEAWindowScrollable(strUrl)
{
	iheight = 650;
	iwidth = 990;
	itop = (((screen.availHeight) - iheight - 80) / 2);
	ileft = (((screen.availWidth) - iwidth) / 2);

	var windowOptions = "scrollbars=yes,toolbar=no,menubar=yes,resizable=yes,status=no,width=" + iwidth + ",height=" + iheight + ",top=" + itop + ",left=" + ileft;
		
	window.open('EAWaitScreen.aspx?url=' + escape(strUrl), '_blank', windowOptions);
}

function openRPWindow(strUrl)
{
	iheight = 650;
	iwidth = 990;
	itop = (((screen.availHeight) - iheight - 80) / 2);
	ileft = (((screen.availWidth) - iwidth) / 2);

	var windowOptions = "scrollbars=yes,toolbar=no,menubar=yes,resizable=yes,status=no,width=" + iwidth + ",height=" + iheight + ",top=" + itop + ",left=" + ileft;
		
	window.open('EAWaitScreen.aspx?url=' + escape(strUrl), '_blank', windowOptions);
}

function detectAdobeVersion()
{
	/*
		This will check if the Adobe reader is installed on the client machine.
		Its return value is the version number e.g. 7.0 or 0.0 if not installed.
		Looks like Adobe release a new activeX object from time to time, check this each release.		
		4.0 = ActiveXObject('PDF.PdfCtrl.1')
		7.0 = ActiveXObject('AcroPDF.PDF.1') 
	*/
	var acrobat=new Object();
	acrobat.installed=false;
	acrobat.version='0.0';

		if (navigator.plugins && navigator.plugins.length)
		{
			for ( var x = 0, l = navigator.plugins.length; x < l; ++x )
			{
				if (navigator.plugins[x].description.indexOf('Adobe Acrobat') != -1)
				{
					acrobat.version=parseFloat(navigator.plugins[x].description.split('Version ')[1]);
					if (acrobat.version.toString().length == 1) acrobat.version+='.0';
					acrobat.installed=true;
					break;
				}
			}
		}
		else if (window.ActiveXObject)
		{
			for (x=2; x<10; x++)
			{
				try
				{
					oAcro=eval("new ActiveXObject('PDF.PdfCtrl."+x+"');");
					if (oAcro)
					{
						acrobat.installed=true;
						acrobat.version=x+'.0';
					}
				}
				catch(e) {}
			}

			try
			{	
				oAcro4=new ActiveXObject('PDF.PdfCtrl.1');
				if (oAcro4)
				{
					acrobat.installed=true;
					acrobat.version='4.0';
				}
			}
			catch(e) {}

			try
			{
				oAcro7=new ActiveXObject('AcroPDF.PDF.1');
				if (oAcro7)
				{
					acrobat.installed=true;
					acrobat.version='7.0';
				}
			}
			catch(e) {}
		}
			
		return acrobat.version;
}

function SplitterResized(sender, eventArgs)
{
    setCookie("SplitterDistance", sender.get_width(), 1);
}

function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+ expiredays);
    document.cookie=c_name + "=" + escape(value) + ";path=/" +
                                    ((expiredays==null) ? "" 
                                    : ";expires="+exdate.toGMTString());
}

function ReadCookie(cookieName) {
    var theCookie = "" + document.cookie;
    var ind = theCookie.indexOf(cookieName);
    if (ind == -1 || cookieName == "") return "";
    var ind1 = theCookie.indexOf(';', ind);
    if (ind1 == -1) ind1 = theCookie.length;
    return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
}

function AddEventHandler(mObject, sEventName, mEvent)
{
    //Add client event handlers
    if (mObject.attachEvent)
    {
        mObject.attachEvent("on" + sEventName, mEvent); 
    }
    else if (mObject.addEventListener)
    { 
        mObject.addEventListener(sEventName, mEvent, false); 
    }
}

function getQueryVariable(variable)
{  
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++)
    {    
        var pair = vars[i].split("=");   
        if (pair[0] == variable) 
        {      
            return pair[1];    
        }  
    }   
    return "-1";
}

function convertDateFormat(date) {

    if (date != null)
    {
        var formattedDate = date.getFullYear() + "-"; // date is now yyyy-
        var month = date.getMonth() + 1;
        formattedDate = formattedDate + month + "-";  // date is now yyyy-mm-
        formattedDate = formattedDate + date.getDate() + " "; // date is now yyyy-mm-dd

        formattedDate = formattedDate + date.getHours() + ":";  // date is now yyyy-mm-dd hh:
        formattedDate = formattedDate + date.getMinutes() + ":00"; // date is now yyyy-mm-dd hh:mm:00
        
        return formattedDate;
    }
    else
    {
        return "";
    }
}

function ActiveTabChanged(sender) {
    try {
        if (document.getElementById("M_hdnActiveTab") != null && document.getElementById("M_hdnActiveTab") != 'undefined') {
            var activeTab = document.getElementById("M_hdnActiveTab");
            activeTab.value = sender.get_activeTabIndex();
        }
    }
    catch (err)
    { }
}

function InputNumericValuesOnly() {
    if (!(event.keyCode == 44 || event.keyCode == 45 || event.keyCode == 46 || event.keyCode == 48 || event.keyCode == 49 || event.keyCode == 50 || event.keyCode == 51 || event.keyCode == 52 || event.keyCode == 53 || event.keyCode == 54 || event.keyCode == 55 || event.keyCode == 56 || event.keyCode == 57)) {
        event.returnValue = false;
    }
}

/*
    This function is used to pass arguments to a string 
    E.g. String.format("{0}{1}", arg1, arg2);
*/
String.format = function() {
     var s = arguments[0];
        for (var i = 0;i<arguments.length-1;i++){       
            var reg = new RegExp("\\{"+i+"\\}","gm");             
            s = s.replace(reg,arguments[i+1]);
        }
        return s;
    }