//<!--

// convert all characters to lowercase to simplify testing
var agt=navigator.userAgent.toLowerCase();

// *** BROWSER VERSION ***
// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);

// Note: Opera and WebTV spoof Navigator.  We do strict client detection.
// If you want to allow spoofing, take out the tests for opera and webtv.
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
            && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
            && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-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 == 5));
var is_nav6up = (is_nav && (is_major >= 5));
var is_gecko = (agt.indexOf('gecko') != -1);


var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
var is_ie4up  = (is_ie && (is_major >= 4));
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);

// 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_aol5  = (agt.indexOf("aol 5") != -1);
var is_aol6  = (agt.indexOf("aol 6") != -1);

var is_opera = (agt.indexOf("opera") != -1);
var is_opera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
var is_opera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
var is_opera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
var is_opera5 = (agt.indexOf("opera 5") != -1 || agt.indexOf("opera/5") != -1);
var is_opera5up = (is_opera && !is_opera2 && !is_opera3 && !is_opera4);

var range = ""

if (is_ie || is_opera)
{
	range = "all."
}

// Convert object name string or object reference
// into a valid object reference
function getObject(obj) {
	var theObj;
	
	if (typeof obj == "string") {
		theObj = document.getElementById(obj);
		// this is bad - it works only under IE!
		//theObj = eval("document." + range + obj)

		// this works under IE 6.0 and opera 7
		//theObj = eval(obj)
	} else {
		theObj = obj
	}
	
	return theObj
} 

function getObjectByName(obj) {
	var theObjs = document.getElementsByName(obj);
	if (theObjs == null)
		return null;
	return theObjs.item(0);
}

function DelayedWindowFocuser_Timeout() {
	if (this.m_hWnd != null && !this.m_hWnd.closed) {
		this.m_hWnd.focus();
	}
}

function DelayedWindowFocuser(_hWnd) {
	this.m_hWnd = _hWnd;
}

DelayedWindowFocuser.prototype.TimeOut = DelayedWindowFocuser_Timeout;

var LoginWindowCommon = null;
var idCheckLoginWindowCommon = null;

function CheckLoginWindowCommon() {
	if (LoginWindowCommon != null && LoginWindowCommon.closed) {
		LoginWindowCommon = null;
		clearInterval(idCheckLoginWindowCommon);
		__doPostBack('AfterLogin','');
	}
}

function GetPositionY(o) {
	var OffsetTop = 0;
	if (is_nav) {
		OffsetTop = o.y;
	} else {
		while (o != null) {
			OffsetTop += o.offsetTop;
			o = o.offsetParent;
		}
	}
	return OffsetTop;
}

function showProps(o) {
	var result = "";
	count = 0;
	for (var i in o) {
		result += o + "." + i + "=" + o[i] + "\n";
		count++;
		if (count == 25) {
			alert(result);
			result = "";
			count = 0;
		}
	}
	alert(result);
} 

function SaveScrolling() {
//	showProps(document.body);
	var obj = getObjectByName("LastScrollTop");
	if (obj != null) {
		obj.value = document.body.scrollTop;
	}
}

function DoScrollablePostBack(CmdName) {
	SaveScrolling();
	__doPostBack(CmdName, '');
}

function DoScrollablePostBack2(CmdName, CmdArg) {
	SaveScrolling();
	__doPostBack(CmdName, CmdArg);
}

// restore the last known scrollTop
function RestoreScrolling() {
	var obj = getObjectByName("LastScrollTop");
	if (obj != null) {
		document.body.scrollTop = obj.value;
	}
}

var evt;
function fnTrapEnter(MyEvent, btn, OldFnc){
	if (MyEvent == null)
		MyEvent = event;
		
	if (MyEvent.keyCode == 13) {
		MyEvent.returnValue=false;
		MyEvent.cancelBubble = true;
		var xObj = getObject(btn);
		if (xObj != null) {
			if (xObj.click != null && !is_opera) {
				xObj.click();
			} else if (xObj.href.indexOf('javascript') >= 0) {
				eval(xObj.href);
				//();
			} else {
				// some dummy browser
				//xObj.click();
			}
		}
	}
	if (!MyEvent.cancelBubble && OldFnc != null) {
		OldFnc(MyEvent);
	}
	return MyEvent.returnValue;
}
function fnTrapEnterJS(MyEvent, js, OldFnc){
	if (MyEvent == null)
		MyEvent = event;
		
	if (MyEvent.keyCode == 13) {
		MyEvent.returnValue=false;
		MyEvent.cancelBubble = true;
		eval(js);
	}
	if (!MyEvent.cancelBubble && OldFnc != null) {
		OldFnc(MyEvent);
	}
	return MyEvent.returnValue;
}

function SetOnKeyDownEH(obj, NewEH) {
	if (obj == null)
		return null;
		
	var retVal = obj.onkeydown;
	obj.onkeydown = NewEH;
	return retVal;
}

function AutoResize() {
	try {
		var ScrollWidth = parseInt(document.body.scrollWidth);
		var ExtendWidth = 0;
		var ClientWidth = parseInt(document.body.clientWidth);
		ExtendWidth = ScrollWidth - ClientWidth;

		var ScrollHeight = parseInt(document.body.scrollHeight);
		var ExtendHeight = 0;
		var ClientHeight = parseInt(document.body.clientHeight);
		ExtendHeight = ScrollHeight - ClientHeight;

		self.resizeBy(ExtendWidth, ExtendHeight); 
		// self.resizeTo(screen.availWidth,screen.availHeight));
	} catch (e) {
	}
}

function AddMonth(MonthYear) {
	var Year = Number(MonthYear.substring(0, 4));
	var Month = Number(MonthYear.substring(4, 6));
	Month = Month + 1;

	if (Month == 13) {
		Month = 1;
		Year++;
	}
	var sMonth = Month.toString();
	if (Month < 10) 
		sMonth = "0" + sMonth;

	return (Year.toString() + sMonth);
}


function HideImage(IconName) {
	document.getElementById(IconName).width = 0;
}

// Create a cookie with the specified name and value.
// The cookie expires at the end of the 20th century.
function SetCookie(sName, sValue) {
  date = new Date();
  document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString();
}

// Delete the cookie with the specified name.
function DelCookie(sName) {
	var today = new Date();
	var expired = new Date(today.getTime() - 48 * 60 * 60 * 1000); // less 2 days

	document.cookie = sName + "=null; expires=" + expired.toGMTString();
    bikky = document.cookie;
}

function SafeEnable(obj, state) {
	var theObj = null;
	if (typeof obj == "string") {
		theObj = getObject(obj);
	} else {
		theObj = obj;
	}
	if (theObj != null) {
		theObj.disabled = !state;
	}
}

function SetCheckedSafe(obj, value) {
	var theObj = null;
	if (typeof obj == "string") {
		theObj = getObject(obj);
	} else {
		theObj = obj;
	}
	if (theObj != null) {
		theObj.checked = value;
	}
}

function EnableCheckBox(theObj, bEnable) {
	theObj.disabled = !bEnable;
	if (bEnable && theObj.parentElement != null) {
		theObj.parentElement.disabled = false; // stupid SPAN around checkbox
	}
}

function SetWaitCursor(evt) {
	var MyEvent1 = evt;
	if (MyEvent1 == null)
		MyEvent1 = event;
		
	if (MyEvent1 != null && MyEvent1.srcElement != null) {
		if (MyEvent1.srcElement.style.WaitText != null)
			MyEvent1.srcElement.innerText = eval(MyEvent1.srcElement.style.WaitText);
		MyEvent1.srcElement.style.cursor = "wait";
	}
	document.body.style.cursor = "wait";
}

function SetWaitCursorOnClick(obj) {
	if (obj != null) 
		obj.onclick = SetWaitCursor;
}

function GetAbsolutePositionX(obj) {
	var retVal = 0;
	
	while (obj != null) {
		retVal += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return retVal;
}

function CalcOffsetLeftFromAbsoluteX(obj, X) {
	X -= obj.offsetLeft;
	obj = obj.offsetParent;
	while (obj != null) {
		X -= obj.offsetLeft;
		obj = obj.offsetParent;
	}
	return X;
}

function CloseCurrentWindow() {
	var wnd = window;
	while (wnd.parent != null && wnd.parent != wnd) {
		wnd = wnd.parent;
	}
	wnd.close();
}

function CloseCurrentWindowRefreshOpener() {
	try {
		window.opener.__doPostBack('RefreshPage','');
	 } catch (_err) {}
			
	var wnd = window;
	while (wnd.parent != null && wnd.parent != wnd) {
		wnd = wnd.parent;
	}
	wnd.close();
}

function OpenUserMgmt(StartURL, CustomerID, URLExt, bRefreshAfterClose) {
	if (URLExt == null)
		URLExt = "";
	var wnd = window.open(StartURL + "/Academy.Backoffice/UserMgmt.aspx?&CustomerID=" + CustomerID.toString() + URLExt, "Academy_UserMgmt_" + CustomerID.toString(), "width=850,height=600,scrollbars=yes,menubar=no,status=yes,resizable=yes");
	wnd.focus();
	if (bRefreshAfterClose) {
		LoginWindowCommon = wnd;
		idCheckLoginWindowCommon = window.setInterval(CheckLoginWindowCommon, 1000);
	}
}

