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();

///---------------------------------------------------------------------------------
/// CompareDate: compare two input dates
/// Usage: 
///		- date1, date2: two input date
///		- type: EQ 1 if the input date in format mm/dd/yyyy
/// Return:
///		- 1 : if date1>date2
///		- 2 : if date1<date2
///		- 3 : if date1=date2
///---------------------------------------------------------------------------------
function CompareDate(date1,date2,type){
	type = (typeof type == 'undefined') ? 0 : type;
	var dateInput=(type==1)?1:0;
	var dateType=new Array();
	dateType[0]=/^\d{1,2}(\-|\/|\.|\s)\d{1,2}(\-|\/|\.|\s)\d+$/;	//date format dd mm yyyy
	dateType[1]=/^\d{1,2}(\-|\/|\.|\s)\d{1,2}(\-|\/|\.|\s)\d+$/;	//date format mm dd yyyy

	if (date1.search(dateType[dateInput])==-1||date2.search(dateType[dateInput])==-1) return 0;
	
	var seperator1=(date1.indexOf("-")!=-1)?"-":(date1.indexOf("/")!=-1)?"/":(date1.indexOf(".")!=-1)?".":(date1.indexOf(" ")!=-1)?" ":"";
	var seperator2=(date2.indexOf("-")!=-1)?"-":(date2.indexOf("/")!=-1)?"/":(date2.indexOf(".")!=-1)?".":(date2.indexOf(" ")!=-1)?" ":"";
	
	if (seperator1==""||seperator2=="") return 0;
	
	var dateArr1=date1.split(seperator1);
	var dateArr2=date2.split(seperator2);
	
	if (dateArr1.length!=3||dateArr2.length!=3) return 0;
	
	if (dateInput==1){
		var year1 = dateArr1[2];
		var year2 = dateArr2[2];
		var month1 = dateArr1[0];
		var month2 = dateArr2[0];
		var day1 = dateArr1[1];
		var day2 = dateArr2[1];
	}else{
		var year1 = dateArr1[2];
		var year2 = dateArr2[2];
		var month1 = dateArr1[1];
		var month2 = dateArr2[1];
		var day1 = dateArr1[0];
		var day2 = dateArr2[0];
	}

	if (CompareNumber(year1, year2) != 3) return CompareNumber(year1, year2);
	if (CompareNumber(month1, month2) != 3) return CompareNumber(month1, month2);
	if (CompareNumber(day1, day2) != 3) return CompareNumber(day1, day2);
	return 3;
}

///---------------------------------------------------------------------------------
/// compareNumber: compare two input numbers
/// Usage: 
///---------------------------------------------------------------------------------
function CompareNumber(num1, num2){
	if (parseFloat(num1)>parseFloat(num2)){
		return 1;
	}else if (parseFloat(num1)<parseFloat(num2)){
		return 2;
	}else{
		return 3;
	}
}

///---------------------------------------------------------------------------------
/// 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)
{
	days = (typeof days == 'undefined') ? 7 : days;
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = cname+"="+cvalue+expires+"; path=/";
}

///---------------------------------------------------------------------------------
/// 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

///---------------------------------------------------------------------------------
/// 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);
	}
} // ShowMessage

///----------------------------------------------------------------------------------
/// 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	: ShowFullSize
///----------------------------------------------------------------------------------
var popPhotoWin = null;
function ShowFullSize(objId, type, siteId )
{
	var x = (screen.width - 560)/2;
	var url = "?cmd=accm_view_photo&obj_type="+type+"&obj_id="+objId+"&site_id="+siteId;
	if(popPhotoWin == null || popPhotoWin.closed) {
		popPhotoWin = window.open(url, "", "status = false, menubar = false, width = 200, height=200, scrollbars = false, resizable = true, center = true, left="+x)
 	} else { popPhotoWin.location.href=url;}
}
///----------------------------------------------------------------------------------
/// Function	: ShowFullSize
///----------------------------------------------------------------------------------
var popWin = null;
function Popup( uri, width, height )
{
	width  = (width) ? width : 680;
	height = (height) ? height : 420;
	var x = (screen.width - width)/2;
	if(popWin == null || popWin.closed) {
		popWin = window.open(uri, "", "width="+width+",height="+height+",scrollbars,resizable,center,left="+x)
 	} else { popWin.location.href=uri;}
}

///----------------------------------------------------------------------------------
/// Function	: InitDestination
///----------------------------------------------------------------------------------
function InitDestination()
{
	var frm = document.frmDestination;
	if (frm==null) return;
	
	curCountry = (typeof curCountry == 'undefined') ? '' : curCountry;
	var curSelCountry = 0;
	for (var i=0; i<arrWhlCountry.length; i++){
		if ( arrWhlCountry[i]==curCountry){
			curSelCountry=i+1;
		}
		AddComboOption(frm.country,arrWhlCountry[i],i+1);
	}
	if (curSelCountry>0){
		frm.country.selectedIndex=curSelCountry;
		OnSelCountry(frm.country);
	}
}
///----------------------------------------------------------------------------------
/// Function	: InitDestination
///----------------------------------------------------------------------------------
function InitDestinationWhl()
{
	var frm = document.frmGlobal;
	for (var i=0; i<arrWhlCountry.length; i++){
		AddComboOption(frm.selCountry,arrWhlCountry[i],arrWhlCountryUrl[i]);
	}
}
///----------------------------------------------------------------------------------
/// Function	: GotoCountry (for WHL-main)
///----------------------------------------------------------------------------------
function GotoCountry()
{
	var frm = document.frmGlobal;
	if (frm.selCountry.selectedIndex<=0) return;
	frm.action = frm.selCountry.options[frm.selCountry.selectedIndex].value;
	frm.submit();
}

///----------------------------------------------------------------------------------
/// Function	: InitDestination
///----------------------------------------------------------------------------------
function OnSelCountry(elm)
{
	var frm = document.frmDestination;
	var len = frm.destination.options.length;
	for(var i=0;i<len-1;i++){
		frm.destination.remove(1);	
	}
	///-- if no country selected or there is no city, disable it
	if ((elm.selectedIndex==-1) || 
		(elm.selectedIndex==0) ||
		(!arrWhlCity[elm.selectedIndex-1]) || 
		(arrWhlCity[elm.selectedIndex-1].length==1 && arrWhlCity[elm.selectedIndex-1][0]=='-')){
		frm.destination.disabled = true;
		return;
	} else frm.destination.disabled = false;
	for (var i=0; i<arrWhlCity[elm.selectedIndex-1].length; i++){
		AddComboOption(frm.destination,arrWhlCity[elm.selectedIndex-1][i],i);
	}
}

///----------------------------------------------------------------------------------
/// Function	: OpenDestination
///----------------------------------------------------------------------------------
function OpenDestination()
{
	var frm = document.frmDestination;
	var cnt = frm.country;
	var des = frm.destination;
	var url = '';
	if (cnt.selectedIndex==-1 || cnt.selectedIndex==0) return;
	if (des.disabled || des.selectedIndex==0 || des.selectedIndex==-1){
		url = arrWhlCountryUrl[cnt.selectedIndex-1];
	} else {
		url = arrWhlCityUrl[cnt.selectedIndex-1][des.selectedIndex-1];
	}
	frm.action = url;
	frm.submit();
}
//validate contact us form
function IsBlank(value){
	return (value.length == 0);
}
function IsEmail(s){
	if (s.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,6}$/) != -1)
		return true ;
	return false ;
}
function CheckForm(frm, arr_ctrl) {
	for(var i=0; i<arr_ctrl.length; i++) {
		expr = eval( arr_ctrl[i][0] );
		if( expr ) {
			alert( arr_ctrl[i][1] ) ;
			if( arr_ctrl[i][2] )
				eval( "frm."  + arr_ctrl[i][2] ).focus() ;
			return false ; //== Error
		}
	}
	return true ; //== OK
}
