///---------------------------------------------------------------------------------
/// Browser Detect: determine the browser type and its version
/// Usage: 
/// Everything you always wanted to know about your JavaScript client
/// but were afraid to ask. Creates "is_" variables indicating:
/// (1) browser vendor:
///     is_nav, is_ie, is_opera, is_hotjava, is_webtv, is_TVNavigator, is_AOLTV
/// (2) browser version number:
///     is_major (integer indicating major version number: 2, 3, 4 ...)
///     is_minor (float   indicating full  version number: 2.02, 3.01, 4.04 ...)
/// (3) browser vendor AND major version number
///     is_nav2, is_nav3, is_nav4, is_nav4up, is_nav6, is_nav6up, is_gecko, is_ie3,
///     is_ie4, is_ie4up, is_ie5, is_ie5up, is_ie5_5, is_ie5_5up, is_ie6, is_ie6up, is_hotjava3, is_hotjava3up,
///     is_opera2, is_opera3, is_opera4, is_opera5, is_opera5up
/// (4) JavaScript version number:
///     is_js (float indicating full JavaScript version number: 1, 1.1, 1.2 ...)
/// (5) OS platform and version:
///     is_win, is_win16, is_win32, is_win31, is_win95, is_winnt, is_win98, is_winme, is_win2k
///     is_os2
///     is_mac, is_mac68k, is_macppc
///     is_unix
///     is_sun, is_sun4, is_sun5, is_suni86
///     is_irix, is_irix5, is_irix6
///     is_hpux, is_hpux9, is_hpux10
///     is_aix, is_aix1, is_aix2, is_aix3, is_aix4
///     is_linux, is_sco, is_unixware, is_mpras, is_reliant
///     is_dec, is_sinix, is_freebsd, is_bsd
///     is_vms
///---------------------------------------------------------------------------------
var Browser = {
	init: function () {
    // 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.
    this.is_major = parseInt(navigator.appVersion);
    this.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.
    this.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));
    this.is_nav2 = (this.is_nav && (this.is_major == 2));
    this.is_nav3 = (this.is_nav && (this.is_major == 3));
    this.is_nav4 = (this.is_nav && (this.is_major == 4));
    this.is_nav4up = (this.is_nav && (this.is_major >= 4));
    this.is_navonly      = (this.is_nav && ((agt.indexOf(";nav") != -1) ||
                          (agt.indexOf("; nav") != -1)) );
    this.is_nav6 = (this.is_nav && (this.is_major == 5));
    this.is_nav6up = (this.is_nav && (this.is_major >= 5));
    this.is_gecko = (agt.indexOf('gecko') != -1);


    this.is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    this.is_ie3    = (this.is_ie && (this.is_major < 4));
    this.is_ie4    = (this.is_ie && (this.is_major == 4) && (agt.indexOf("msie 4")!=-1) );
    this.is_ie4up  = (this.is_ie && (this.is_major >= 4));
    this.is_ie5    = (this.is_ie && (this.is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
    this.is_ie5_5  = (this.is_ie && (this.is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
    this.is_ie5up  = (this.is_ie && !this.is_ie3 && !this.is_ie4);
    this.is_ie5_5up =(this.is_ie && !this.is_ie3 && !this.is_ie4 && !this.is_ie5);
    this.is_ie6    = (this.is_ie && (this.is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
    this.is_ie6up  = (this.is_ie && !this.is_ie3 && !this.is_ie4 && !this.is_ie5 && !this.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.
    this.is_aol   = (agt.indexOf("aol") != -1);
    this.is_aol3  = (this.is_aol && this.is_ie3);
    this.is_aol4  = (this.is_aol && this.is_ie4);
    this.is_aol5  = (agt.indexOf("aol 5") != -1);
    this.is_aol6  = (agt.indexOf("aol 6") != -1);

    this.is_opera = (agt.indexOf("opera") != -1);
    this.is_opera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
    this.is_opera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
    this.is_opera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
    this.is_opera5 = (agt.indexOf("opera 5") != -1 || agt.indexOf("opera/5") != -1);
    this.is_opera5up = (this.is_opera && !this.is_opera2 && !this.is_opera3 && !this.is_opera4);

    this.is_webtv = (agt.indexOf("webtv") != -1); 

    this.is_TVNavigator = ((agt.indexOf("navio") != -1) || (agt.indexOf("navio_aoltv") != -1)); 
    this.is_AOLTV = this.is_TVNavigator;

    this.is_hotjava = (agt.indexOf("hotjava") != -1);
    this.is_hotjava3 = (this.is_hotjava && (is_major == 3));
    this.is_hotjava3up = (this.is_hotjava && (is_major >= 3));

    // *** JAVASCRIPT VERSION CHECK ***
    this.is_js = 0.0;
    if (this.is_nav2 || this.is_ie3) this.is_js = 1.0;
    else if (this.is_nav3) this.is_js = 1.1;
    else if (this.is_opera5up) this.is_js = 1.3;
    else if (this.is_opera) this.is_js = 1.1;
    else if ((this.is_nav4 && (this.is_minor <= 4.05)) || this.is_ie4) this.is_js = 1.2;
    else if ((this.is_nav4 && (this.is_minor > 4.05)) || this.is_ie5) this.is_js = 1.3;
    else if (this.is_hotjava3up) this.is_js = 1.4;
    else if (this.is_nav6 || this.is_gecko) this.is_js = 1.5;
    // NOTE: In the future, update this code when newer versions of JS
    // are released. For now, we try to provide some upward compatibility
    // so that future versions of Nav and IE will show they are at
    // *least* JS 1.x capable. Always check for JS version compatibility
    // with > or >=.
    else if (this.is_nav6up) this.is_js = 1.5;
    // NOTE: ie5up on mac is 1.4
    else if (this.is_ie5up) this.is_js = 1.3


	}
};
Browser.init();

///---------------------------------------------------------------------------------
/// Function: OpenPage
/// Description: Open new page
/// Input: link
///---------------------------------------------------------------------------------
function OpenPage(link)
{
	window.document.location.replace(link);
}

///---------------------------------------------------------------------------------
/// Function: OpenPage
/// Description: Open new page
/// Input: link
///---------------------------------------------------------------------------------
function SetCookie(cname,cvalue,days)
{
	if (!days) days=7;
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = cname+"="+cvalue+expires+"; path=/";
}
function ReadCookie(cname) {
	var nameEQ = cname + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function EraseCookie(cname) {
	SetCookie(cname,"",-1);
}
///---------------------------------------------------------------------------------
/// EventRegister: register a event handler for a specified event
/// Usage: 
///		- element		: document's element object
///		- type			: event name
///		- handler		: function that handle the event
///---------------------------------------------------------------------------------
function EventRegister(element,type,handler)
{
	if( element.attachEvent )	{
		element.attachEvent("on" + type, handler);
	}
	else if( element.addEventListener ) {
		element.addEventListener(type, handler, false);
	} else {
		alert('event register failed !');
	}
}

///---------------------------------------------------------------------------------
/// GetObjectById: return the according object from its ID
/// Usage: 
///		- objectId		: the object's ID
///---------------------------------------------------------------------------------
function GetObjectById(objectId) {
	// cross-browser function to get an object given its id
	if(document.getElementById && document.getElementById(objectId)) {
		// W3C DOM
		return document.getElementById(objectId);
	} else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId);
	} else {
		return false;
	}
}

///----------------------------------------------------------------------------------
/// Function	: getElementsByBaseTagClass
/// Description	: get page element by their tag type and class name	
/// Input		: 
///		- base	: the search scope base (e.g. document..)
///		- tag	: the HTML tag (e.g. tr,td ..)
///		- class	: class name
/// Output		: return all the matched elements
///----------------------------------------------------------------------------------
function GetElementsByBaseTagClass(base, tag, className) { 
    var classPat = new RegExp('\\b'+className+'\\b'); 
    var nodes = base.getElementsByTagName(tag); 
    var matching = []; 
    for (var i = 0; i < nodes.length; i++) { 
			if (classPat.test(nodes[i].className)) { 
					matching.push(nodes[i]); 
			} 
    } 
	return matching; 
}

///---------------------------------------------------------------------------------
/// GetLayerStyle: return the layer's style object
/// Usage: 
///		- objectId		: the layer's ID
///---------------------------------------------------------------------------------
function GetLayerStyle(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // GetLayerStyle
///---------------------------------------------------------------------------------
/// GetScrollOffset: return the current scrolled position 
/// Usage: 
///---------------------------------------------------------------------------------
function GetScrollOffset() {
	//-- get page scroll offset
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
	return [scrOfX,scrOfY];
}
///---------------------------------------------------------------------------------
/// ChangeObjectVisibility: change a specified object's visibility
/// Usage: 
///		- objectId		: the object's ID
///		- visible			: TRUE/FALSE
///---------------------------------------------------------------------------------
function ChangeObjectVisibility(objectId, visible) {
	// get a reference to the cross-browser style object and make sure the object exists
	var styleObject = GetLayerStyle(objectId);
	if(styleObject) {
		if (visible) styleObject.display = "block";
		else styleObject.display = "none";
		return true;
	} else {
		// we couldn't find the object, so we can't change its visibility
		return false;
	}
} // ChangeObjectVisibility

///---------------------------------------------------------------------------------
/// ShowError: show error message
/// Usage: 
///		- errMsg		: Error message
///---------------------------------------------------------------------------------
function ShowError(errMsg) {
	var elmError = GetObjectById("error");
	if (elmError){
		elmError.innerHTML = errMsg;
		ChangeObjectVisibility("error",true);
	}
} // ShowError

///----------------------------------------------------------------------------------
/// Function:		ShowMessage
///	Description:	
///----------------------------------------------------------------------------------
function ShowMessage(strMsg)
{	
	var elmError = GetObjectById("error");
	if (elmError){
		elmError.innerHTML = "<span class='style1'>"+strMsg+"</span>";
		ChangeObjectVisibility("error",true);
	}
	
	scroll(0,0);
} // ShowMessage

///--------------------------------------------------------------
/// HideMessage: hide the error or information message
///--------------------------------------------------------------
function HideMessage()
{
   ChangeObjectVisibility('error',false);
   GetObjectById('error').innerHTML = '';
} // HideMessage

///----------------------------------------------------------------------------------
/// Function:		ShowWait
///----------------------------------------------------------------------------------
function ShowWait()
{
	//-- get page scroll offset
	var scrOffset = GetScrollOffset();
  var scrOfX = scrOffset[0]; var scrOfY = scrOffset[1];
	
	//-- adjust the showing position
	var elmWait = GetObjectById("wait");
	if (elmWait){
		elmWait.style.top  = (scrOfY + 20) + "px";
		elmWait.style.left = (scrOfX + 20) + "px";
		elmWait.style.visibility = "visible";
	}
} // ShowMWait

///--------------------------------------------------------------
/// HideWait: hide the waiting message
///--------------------------------------------------------------
function HideWait()
{
	var elmWait = GetObjectById("wait");
	if (elmWait){
		elmWait.style.visibility = "hidden";
	}
} // HideWait
///----------------------------------------------------------------------------------
/// Function	: GetKeycode
/// Description	: get key code of the keypress	
/// Input		: event, for exam: <input type='text' onkeypress=GetKeynum(event)>
/// Output		: return the keycode, if fail return -1
///----------------------------------------------------------------------------------
function GetKeycode(e)
{	
	if(window.event) // IE
	{ return e.keyCode;
	} else if(e.which) // Netscape/Firefox/Opera
		   { return e.which;
		   } else { return -1; }
}

///----------------------------------------------------------------------------------
/// Function		: InitHiliTr
/// Description	: Init highlight table TR
/// Input		: array TR elements
/// Output	: none
///----------------------------------------------------------------------------------
function InitHiliTr (objArr) {
	for (i=0; i<objArr.length; i++) {
		objArr[i].onmouseover = mouseOverTr;
		objArr[i].onmouseout = mouseOutTr;
	}
	function mouseOverTr() {
		this.className="trhili";
	}
	function mouseOutTr() {
		if(this.lang=="") this.className="hili";
		else this.className=this.lang;
	}
}


///----------------------------------------------------------------------------------
/// Function: SelectIndexOfCombo
///	Description: select the item of a combobox
///	Input: 
///		elm = the combobox that wish to change selectedIndex
///		value = the value that will be selected.
///----------------------------------------------------------------------------------
function SelectIndexOfCombo(elm,value)
{
	for(i=0;i<elm.length;i++)
		if (elm.options[i].value == value)
		{	elm.selectedIndex = i;
			return;
		}
}
///----------------------------------------------------------------------------------
/// Function	: AddComboOption
/// Description	: Add a new combo option into combo box
/// Input		: combo-box elm, text and value of new option
/// Output		: N/A
///----------------------------------------------------------------------------------
function AddComboOption()
{	
	var newitem = document.createElement("OPTION");
	newitem.text  = arguments[1];
	newitem.value = arguments[2];
	if (arguments.length==4 && arguments[3])
		newitem.selected=true;
	e = arguments[0];
	if (Browser.is_ie) e.add(newitem);
	else e.add(newitem,null);
}
///----------------------------------------------------------------------------------
/// Function	: DelComboOption
/// Description	: remove a combo option from combo box
/// Input			: combo-box elm, index of option
/// Output		: N/A
///----------------------------------------------------------------------------------
function DelComboOption()
{	
	var idx = arguments[1];
	e = arguments[0];
	e.remove(idx);
}

///----------------------------------------------------------------------------------
/// Function: ValidateJsonVar
///	Description: check if the input string is a valid JSON variable declare
///	Input: Json variable declare
///----------------------------------------------------------------------------------
function ValidateJsonVar(strVar)
{
	return (strVar.search(/^\{(.*)\}$/) != -1 );
}

///----------------------------------------------------------------------------------
/// Function: ShowFuncError
///	Description: Display error message when there was an error occur 
///	Input:  Module / Function / Action
///----------------------------------------------------------------------------------
function ShowFuncError(strModule, strFunction, strAction)
{
	alert("There was an error occured. Please contact our technical team.\r\nError at module:"+strModule+" - Function:"+strFunction+" - Action:"+strAction)
}

///----------------------------------------------------------------------------------
/// DISPLAY ERROR/INFO MESSAGE FUNCTIONS
///----------------------------------------------------------------------------------
/// Global variables and initialize code
///----------------------------------------------------------------------------------
var xMsgPos=0; var xMsgShowing=false;
EventRegister(window,"scroll",XMsgKeepTop);
///----------------------------------------------------------------------------------
/// Event handler: XMsgKeepTop
///	Description: fired when page scrolling, adjust the showing position of message
///----------------------------------------------------------------------------------
function XMsgKeepTop(){
	if (!xMsgShowing) return;
	//-- get page scroll offset
	var scrOffset = GetScrollOffset();
  var scrOfX = scrOffset[0]; var scrOfY = scrOffset[1];
	var elm = GetLayerStyle("xMsgContent");
	if (elm) elm.top = scrOfY+"px";
	return(true);
}
///----------------------------------------------------------------------------------
/// Function: ShowMsgError
///	Description: show an error message
///----------------------------------------------------------------------------------
function DisplayError(strErrorMsg){
	var scrOffset = GetScrollOffset();

	var elm = GetLayerStyle("xMsgContent");
	if (!elm) return;

	var msg = GetObjectById("xMsgContentTxt");
	if (msg) msg.innerHTML = strErrorMsg;
	GetLayerStyle("xMsgIconErr").display="block";
	GetLayerStyle("xMsgIconInf").display="none";
//	elm.left 		= scrOfX+"px";
	elm.width 	= document.body.clientWidth;
//	elm.visibility = "visible";
	xMsgShowing = true;
	xMsgPos=0;
	if (scrOffset[1]>70) setTimeout("scrollBy(0,-70);",80)
	setTimeout("XMsgMoving(true);",100);
}
///----------------------------------------------------------------------------------
/// Function: ShowInfoMsg
///	Description: show an informational message. The message would closed after 5s
///----------------------------------------------------------------------------------
function DisplayMessage(strInfoMsg){
	var elm = GetLayerStyle("xMsgContent");
	if (!elm) return;
	var msg = GetObjectById("xMsgContentTxt");
	if (msg) msg.innerHTML = strInfoMsg;
	GetLayerStyle("xMsgIconErr").display="none";
	GetLayerStyle("xMsgIconInf").display="block";
//	elm.top 		= (scrOfY-70)+"px";
//	elm.left 		= scrOfX+"px";
	elm.width 	= document.body.clientWidth;
//	elm.visibility = "visible";
	xMsgShowing = true;
	xMsgPos=0;
	setTimeout("XMsgMoving(true);",100);
	setTimeout("CloseMsg();",3000);
}
///----------------------------------------------------------------------------------
/// Function: CloseMsg
///	Description: close an displaying error/info message box
///----------------------------------------------------------------------------------
function CloseMsg(){
	if (!xMsgShowing) return;
	if (!GetLayerStyle("xMsgContent")) return;
	xMsgPos=0;
	xMsgShowing = false;
	XMsgMoving(false);
}
///----------------------------------------------------------------------------------
/// Function: XMsgClosing
///	Description: dont call this function directly
///----------------------------------------------------------------------------------
function XMsgMoving(bIsShow){
	//-- get page scroll offset
	var scrOffset = GetScrollOffset();
  var scrOfX = scrOffset[0]; var scrOfY = scrOffset[1];

	var elm = GetLayerStyle("xMsgContent");
	if (!elm) return;
	xMsgPos = xMsgPos + 8;
	if (xMsgPos>70){
		if (!bIsShow){
			elm.visibility = "hidden";
		}
		return;
	}

	if (!bIsShow)	{
		elm.top = (scrOfY-xMsgPos)+"px";
		setTimeout("XMsgMoving(false);",80);
	} else {
		elm.top = (scrOfY-70+xMsgPos)+"px";
		setTimeout("XMsgMoving(true);",80);
		if (xMsgPos==8) elm.visibility = "visible";
	}
}



