function getElementValueById(id)
{
    var element = getDiv(id);
    if(element.value)
        return element.value

    return false;
}

function showError(message) {
//	$("#messages")
//		.append($("<div></div>").addClass("ui-state-error ui-corner-all")
//			.append($("<div></div>").addClass("ui-icon ui-icon-alert"))
//			.append($("<div></div>").addClass("error").text(message))
//	).fadeIn();
	
	if ($("#messages").children("div.ui-state-error").length == 0) {
		$("#messages").append($("<div></div>").addClass("ui-state-error ui-corner-all")).fadeIn();
	};
	
	$("#messages").find("div.ui-state-error #customerror").remove();
	$("#messages").children("div.ui-state-error").append($("<div></div>").attr("id", "customerror")
			.append($("<div></div>").addClass("ui-icon ui-icon-alert"))
			.append($("<div></div>").addClass("error").text(message).show("highlight"))
	);
	
};
function hideError() {	
	$("#messages").find("div.ui-state-error #customerror").remove();
	if ($("#messages div.ui-state-error div").length == 0) {
		$("#messages").find("div.ui-state-error").remove();
	};
	/*$("#messages").find("div.ui-state-error #customerror").remove();
	if ($("#messages div.ui-state-error ui-corner-all div#customerror div.error").text.length == 0) {
		$("#messages").find("div.ui-state-error").remove();
		alert ("==0");
	};*/
	

//	$("#messages")
//		.append($("<div></div>").addClass("ui-state-error ui-corner-all")
//			.append($("<div></div>").addClass("ui-icon ui-icon-alert"))
//			.append($("<div></div>").addClass("error").text(message))
//	).fadeOut();
};

function getDiv(divID)
{
    if( document.layers ) //Netscape layers
        return document.layers[divID];

    if( document.getElementById ) //DOM; IE5, NS6, Mozilla, Opera
        return document.getElementById(divID);

    if( document.all ) //Proprietary DOM; IE4
        return document.all[divID];

    if( document[divID] ) //Netscape alternative
        return document[divID];

    return false;
}

function openWindow(href,name,centered,attributes,width,height)
{
    var w = (width) ? width : 400;
    var h = (height) ? height : 300;

    var windowWidth = (document.all) ? document.body.clientWidth : window.outerWidth;
    var windowHeight = (document.all) ? document.body.clientHeight : window.outerHeight;

    x = (windowWidth-w) / 2;
    y = (windowHeight-h) / 2;

    var properties = (attributes)
        ? "toolbar=yes,directories=yes,status=yes,menubar=yes,copyhistory=yes,location=yes"
        : "toolbar=no,directories=no,status=no,menubar=no,copyhistory=no,location=no";

    properties = "width="+w+",height="+h+",top="+y+",left="+x+",scrollbars=yes,resizable=yes," + properties;

    window.open(href,name,properties);
};

function insertLocaleParameter(value) {
	var url = window.location.href;
	if (url.indexOf("?") == -1) {
		url=url+"?locale="+value;
	} else if (url.indexOf("?locale=") != -1) {
		var localeStartIndex = url.indexOf("?locale=");
		var localeEndIndex = url.indexOf("&", localeStartIndex);
		if (localeEndIndex == -1) {
			localeEndIndex = url.length;
		}
		var locale = url.substring(localeStartIndex, localeEndIndex);
		url = url.replace(locale, "?locale="+value);
	} else if (url.indexOf("&locale=") != -1) {
		var localeStartIndex = url.indexOf("&locale=");
		var localeEndIndex = url.indexOf("&", localeStartIndex+1);
		if (localeEndIndex == -1) {
			localeEndIndex = url.length;
		}
		var locale = url.substring(localeStartIndex, localeEndIndex);
		url = url.replace(locale, "&locale="+value);
	} else {
		url = url+"&locale="+value;
	};
	return url;
};

function resizeContainer() {
    // Expand the width to fill the content of the datalink result list.
	// Substract 43 is to cater for the left margin used in menu bar
	if ($(document).width() > $("body").width()) {
		$("#container").width($(document).width());
	};
}

$(function(){
	$(".vertMenu").buildMenu({
		menuWidth:300,
		openOnRight:true,
		menuSelector: ".menuContainer",
		iconPath:"ico/",
		hasImages:false,
		fadeInTime:200,
		fadeOutTime:200,
		adjustLeft:5,
		adjustTop:0,
		opacity:.90,
		openOnClick:false,
		minZindex:5000,
		shadow:true,
		hoverIntent:300,
		submenuHoverIntent:500,
		closeOnMouseOut:true
	});
	
	if (typeof(greetings)!="undefined") {
		var currDate = new Date();
		var currHour = currDate.getHours();
		if (currHour >= 0 && currHour <= 11) {
			$(".loginDetails .greetings").text(greetings.morning);
		} else if (currHour >= 12 && currHour <= 17) {
			$(".loginDetails .greetings").text(greetings.afternoon);
		} else if (currHour >= 18 && currHour <= 23) {
			$(".loginDetails .greetings").text(greetings.evening);
		};
	}
	
	if (typeof(lastloginstatus)!="undefined") {
		if ($(".loginDetails .lastloginstatus").text() == "Y") {
			$(".loginDetails .lastloginstatus").text(lastloginstatus.success);
		}
		if ($(".loginDetails .lastloginstatus").text() == "N") {
			$(".loginDetails .lastloginstatus").text(lastloginstatus.failed);
		}
	}
	
	if ($("#messages div").length == 0) {
		$("#messages").hide();
	} else {
		$("#messages").show("highlight");
	}
	
	$("#locale_switch_en").click(function(){
		window.location.href=insertLocaleParameter("en");
	});
	$("#locale_switch_zhtw").click(function(){
		window.location.href=insertLocaleParameter("zh_TW");
	});
	$("#locale_switch_zhcn").click(function(){
		window.location.href=insertLocaleParameter("zh_CN");
	});
	
	$(".logoutCas").click(function(){
		var homeUrl = $(".eztrackhome").attr("href");
		window.location.href=$(this).attr("href");
		return false;
	});
	$("form").attr("accept-charset","UTF-8");
	$(".deleteButtonLink").click(function(){
		return confirm(label.confirmDelete);
	});
	
	var timezoneOffset = -(new Date()).getTimezoneOffset();
//	var offsetHour = Math.floor(Math.abs(timezoneOffset) / 60);
//	var offsetMin = timezoneOffset % 60;
//	timezoneOffset = (timezoneOffset<0?"-":"+")+(offsetHour<10?"0":"")+offsetHour+":"+(offsetMin<10?"0":"")+offsetMin;
	$.cookie("timezone",timezoneOffset);
	
	resizeContainer();
});


