//  -------------------------------------------------------------------------------
//  Common.js
//	Script contains commmon functions
//  Copyright 2006 BASE77
//  -------------------------------------------------------------------------------

// ***********************************************
//		Browser Detection goes here

var agt=navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();
var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);

// Note: On IE, start of appVersion return 3 or 4
// which supposedly is the version of Netscape it is compatible with.
// So we look for the real version further on in the string

var iePos  = appVer.indexOf('msie');
if (iePos !=-1) {
   is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)))
   is_major = parseInt(is_minor);
}

// Netscape6 is mozilla/5 + Netscape6/6.0!!!
// Mozilla/5.0 (Windows; U; Win98; en-US; m18) Gecko/20001108 Netscape6/6.0
var nav6Pos = agt.indexOf('netscape6');
if (nav6Pos !=-1) {
   is_minor = parseFloat(agt.substring(nav6Pos+10))
   is_major = parseInt(is_minor)
}

// Netscape 7 is mozilla/5 + Netscape/7.0
// Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0
if ((agt.indexOf('mozilla') != -1) && (agt.indexOf('netscape/') != -1))
{
	is_minor = parseFloat(agt.substring(agt.indexOf('netscape/')+9))
	is_major = parseInt(is_minor)
}

var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
            && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
            && (agt.indexOf('webtv')==-1));
var is_nav2 = (is_nav && (is_major == 2));
var is_nav3 = (is_nav && (is_major == 3));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav4up = (is_nav && (is_major >= 4));
var is_navonly = (is_nav && ((agt.indexOf(";nav") != -1) ||
                 (agt.indexOf("; nav") != -1)) );

var is_nav6   = (is_nav && is_major==6);    // new 010118 mhp
var is_nav6up = (is_nav && is_minor >= 6) // new 010118 mhp

var is_nav5   = (is_nav && is_major == 5 && !is_nav6); // checked for ns6
var is_nav5up = (is_nav && is_minor >= 5);

var is_ie   = (iePos!=-1);
var is_ie3  = (is_ie && (is_major < 4));

var is_ie4   = (is_ie && is_major == 4);
var is_ie4up = (is_ie && is_minor >= 4);
var is_ie5   = (is_ie && is_major == 5);
var is_ie5up = (is_ie && is_minor >= 5);
var is_ie6   = (is_ie && is_major == 6);
var is_ie6up = (is_ie && is_minor >= 6);

// KNOWN BUG: On AOL4, returns false if IE3 is embedded browser
// or if this is the first browser window opened.  Thus the
// variables is_aol, is_aol3, and is_aol4 aren't 100% reliable.

var is_aol   = (agt.indexOf("aol") != -1);
var is_aol3  = (is_aol && is_ie3);
var is_aol4  = (is_aol && is_ie4);

var is_opera = (agt.indexOf("opera") != -1);
var is_webtv = (agt.indexOf("webtv") != -1);
var is_safari = (agt.indexOf("safari") != -1);
var is_mac = (appVer.indexOf("ppc") != -1);
var is_firefox = (agt.indexOf("firefox") != -1);

var		KEY_BACK = 8;
var		KEY_F5 =116;
var		KEY_CTRL= 17;
var		KEY_ALT = 18;
var		KEY_F11 = 122;
var		KEY_F3 = 114;
var		KEY_SHIFT = 16;
var		KEY_ALTBACK = 37;
var		KEY_CTRLA = 97;
var		KEY_CTRLC = 99;
var		KEY_CTRLP = 112;
var		KEY_CTRL_INS = 45;
var		KEY_CTRLU = 117;
var		KEY_CTRLX = 120;
var		KEY_CTRLS = 115;
var		KEY_SHIFT_DEL = 46;
var     	KEY_SHFT=16;

/*if(!is_firefox)
{
		document.onkeydown = new Function("return IsKeyDisable();")
}*/
// *****************************************************
function IsKeyDisable(e)
{
	var bKeyDisable = false;
//	alert(navigator.userAgent.toLowerCase());
	e = (e) ? e : window.event;
	var keyPressed =  e.keyCode || e.charCode;

	//alert(e.charCode + " GG ");
//	alert(keyPressed);

	ob = e.srcElement;
	ob = (ob)? ob : e.target;

	if(!is_firefox)
	{
	    var isCtrlOrShft = keyPressed==KEY_CTRL||keyPressed==KEY_SHFT;
	    if(isCtrlOrShft) {
		alert('Ctrl key is not allowed on this page');
            	bKeyDisable = true;
	    }
	}

	else if(keyPressed)
	{
		if(keyPressed == KEY_BACK  && ob.type != "text") // Back
			bKeyDisable = true;
		else if(keyPressed == KEY_F5 && ob.type != "text") // F5
			bKeyDisable = true;
		else if(keyPressed == KEY_SHIFT  && ob.type != "text") // Shift
			bKeyDisable = true;
		else if(keyPressed == KEY_CTRL && ob.type != "text") // Ctrl
		{
			alert('Ctrl key is not allowed on this page.');
			bKeyDisable = true;
		}
		else if(keyPressed == KEY_ALT && ob.type != "text") // Alt
			bKeyDisable = true;
		else if(keyPressed == KEY_F11 && ob.type != "text") // F11
			bKeyDisable = true;
		else if(keyPressed == KEY_F3 && ob.type != "text") // f3
			bKeyDisable = true;
		else if(keyPressed == KEY_ALTBACK && ob.type != "text") // f3
			bKeyDisable = true;
		else if(keyPressed == KEY_CTRLA && ob.type != "text") // ctrl+A
			bKeyDisable = true;
		else if(keyPressed == KEY_CTRLC && ob.type != "text") // ctrl+C
			bKeyDisable = true;
		else if(keyPressed == KEY_CTRLP && ob.type != "text") // CTRL+P
			bKeyDisable = true;
		else if(keyPressed == KEY_CTRLU && ob.type != "text") // CTRL+U
			bKeyDisable = true;
		else if(keyPressed == KEY_CTRLX && ob.type != "text") // CTRL+X
			bKeyDisable = true;
		else if(keyPressed == KEY_CTRLS && ob.type != "text") // CTRL+S
                        bKeyDisable = true;
		else if(keyPressed == KEY_CTRL_INS && ob.type != "text") // CTRL+INS
			bKeyDisable = true;
		else if(keyPressed == KEY_SHIFT_DEL && ob.type != "text") // SHFT+DEL
			bKeyDisable = true;

	}

	if(bKeyDisable)
	{

		if(e.cancelBubble) e.cancelBubble = true;
		if(e.returnValue ) e.returnValue  = false;
		if (e.stopPropagation) e.stopPropagation();
		if (e.preventDefault) e.preventDefault();
	//	if(e.keyCode) e.keyCode = 0;
	}

	return !bKeyDisable;
}

/*function clearData(){
    window.clipboardData.setData('text','')
}*/



document.oncontextmenu = new Function("return false;")
//document.onselectstart = new Function("return false;")
document.ondragstart = new Function("return false;")
//document.onclick = new Function("if(event.shiftKey) return false;")

function FFKeyDisabled(e)
{
//	if(is_firefox)
		return IsKeyDisable(e);

//		return true;
}

// Returns element handle
function GetElementHandle(sElmID)
{
	var obj;

	if(document.all)
	{
		obj = document.all(sElmID);
	}
	else if(document.getElementById)
	{
		obj = document.getElementById(sElmID);
	}
	else
	{
		obj = NULL;
	}

	return obj;
}

// preloads images
function PLI(s, callbackFunc)
{
	var imgStr = new String(s);

	if(imgStr.length > 0)
	{
		if(imgStr.indexOf(',') > 0)
		{
			b = imgStr.substring(0, imgStr.indexOf(','));
			c = imgStr.substring(imgStr.indexOf(',')+1, imgStr.length);
		}
		else
		{
			b = imgStr;
			c = "";
		}

		d = b.split('#');

		eval(d[0] + " =  new Image;");
		eval(d[0] + ".onload = function(){PLI2('" + c + "','" + callbackFunc + "');}");
		eval(d[0] + ".src = '" + d[1] + "';");
	}
	else
	{
		eval(callbackFunc);
	}
}

// preloads images, same as PLI , used to avoid recursion call bubg of IE
function PLI2(s, callbackFunc)
{
	var imgStr = new String(s);

	if(imgStr.length > 0)
	{
		if(imgStr.indexOf(',') > 0)
		{
			b = imgStr.substring(0, imgStr.indexOf(','));
			c = imgStr.substring(imgStr.indexOf(',')+1, imgStr.length);
		}
		else
		{
			b = imgStr;
			c = "";
		}

		d = b.split('#');

		eval(d[0] + " =  new Image;");
		eval(d[0] + ".onload = function(){PLI('" + c + "','" + callbackFunc + "');}");
		eval(d[0] + ".src = '" + d[1] + "';");
	}
	else
	{
		eval(callbackFunc);
	}
}

function secs_to_string(tmpSecs)
{
	str = tmpSecs;
	hh = parseInt(tmpSecs/3600) ;


	if (hh<10){
		hh = "0" + hh;
	}

	minToCalc = tmpSecs % 3600;
	mm=  parseInt(minToCalc/60);


	if (mm<10){
		mm = "0" + mm;
	}

	ss = minToCalc % 60;

	if (ss<10){
		ss = "0" + ss;
	}

	return(hh.toString() + ":" + mm.toString() + ":" + ss );
}


function HandleError(nErrNum)
{
	switch(nErrNum)
	{
		case 1:
			alert("Your browser is not compatible for this application." + "\nError Code: " + nErrNum );
			break;
		case 2:
			alert("Internal Error." + "\nError Code: " + nErrNum );
			break;
		case 3:
		case 4:
			alert("Application could not create window." + "\nError Code: " + nErrNum );
			break;
		case 5:
			alert("Internal Error." + "\nError Code: " + nErrNum );
			break;
		case 6:
			alert("Testing Screen Tutorial could not be downloaded." + "\nError Code: " + nErrNum );
			break;
		case 7:
			alert("Application could not submit the test data to the server." + "\nError Code: " + nErrNum );
			break;
		case 8:
		case 9:
		case 10:
		case 11:
		case 12:
			alert("Internal Error." + "\nError Code: " + nErrNum );
			break;
		case 13:
			alert("Application cannot initialize object!" + "\nError Code: " + nErrNum );
			break;
		case 14:
		case 15:
		case 16:
			alert("Test Data could not be loaded." + "\nError Code: " + nErrNum );
			break;
	}
}


function Trim(s)
   {
	// Remove leading spaces and carriage returns
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
	{ s = s.substring(1,s.length); }

	// Remove trailing spaces and carriage returns
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
	{ s = s.substring(0,s.length-1); }

   	return s;
}



