String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/, "");
}

function createCookie(name, value, days) {
	var date;
	var expires;
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		expires = "; expires=" + date.toUTCString();
	} else{
		expires = "";
	}
	document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	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(name) {
	createCookie(name, "", -1);
}

function change_language(input_id) {
	if (input_id == null || input_id == '') {
		createCookie("lang", 'en', 2);
	} else {
		var language = document.getElementById(input_id).value;
		createCookie("lang", language, 2);
	}
	calculate_locale();
	window.location.reload(false);
}

function find_brand() {
	var host = window.location.host;
	host = host.toUpperCase();
	var brand = readCookie("brand");
	if (brand == null || brand == '') {
		if (host.indexOf('VLS') >= 0) {
			brand = 'VLS';
		} else if (host.indexOf('DCL') >= 0) {
			brand = 'DCL';
		} else if (host.indexOf('BRENNAN') >= 0) {
			brand = 'BRENNAN';
		} else if (host.indexOf('CONTERM') >= 0) {
			brand = 'CONTERM';
		} else if (host.indexOf('BOX') >= 0) {
			brand = 'BOX';
		} else if (host.indexOf('CONFREIGHT') >= 0) {
			brand = 'CONFREIGHT';
		} else if (host.indexOf('OCEANEXPRESS') >= 0) {
			brand = 'OCEANEXPRESS';
		} else if (host.indexOf('OEM') >= 0) {
			var comp = readCookie("comp");
			brand = comp.toUpperCase();
		} else if(host.indexOf('NVOCC') >= 0){
			brand = 'VLS';
		} else if(host.indexOf('VANGUARD') >= 0){
			brand = 'VLS';
		}
		createCookie('brand', brand);
	}
}

function calculate_locale() {
	var reload = false;
	var lang = readCookie("lang");
	if (lang == null || lang == '') {
		lang = 'en';
		createCookie("lang", lang, 2);
		reload = true;
	}
	var brand = readCookie("brand");
	if (brand == null || brand == '') {
		find_brand();
		brand = readCookie("brand");
		reload = true;
	}
	var locale = lang;
	if (locale != '' && brand != '') {
		locale += '__' + brand;
	}
	createCookie('locale', locale);
	if (reload) {
		window.location.reload(false);
	}
}

function load_language(span_id) {
	var lang = readCookie("lang");
	if(lang == null || lang == ''){
		createCookie('lang', 'en', 2);
		lang='en';
	}
	AjaxRequest
			.get( {
				'url' : '/Portal/admin/lang_list.jsp',
				'onSuccess' : function(req) {
					var span = document.getElementById(span_id);
					var html = '<select class="lang_sel" name="language" id="language" onchange="change_language(\'language\')">';
					var data = req.responseText.replace(/\r/g, '');
					data = data.replace(/\n/g, ' ');
					data = eval(data);
					var sel = "";

					for ( var i = 0; i < data.length; i++) {
						if (data[i].code == lang) {
							sel = "selected";
						} else {
							sel = "";
						}
						html += '<option value="' + data[i].code + '" ' + sel
								+ '>' + data[i].uni_name + '</option>';
					}
					html += '</select>';
					span.innerHTML = html;
				}
			});
}

function show_header_banner() {
	var head_span = document.getElementById("header_text");
	var page_span = document.getElementById("header_banner");
	if (head_span != null) {
		if (page_span != null) {
			head_span.innerHTML = page_span.innerHTML;
		} else {
			var title = document.title;
			title = title.substring(title.indexOf('|') + 1);
			head_span.innerHTML = title;
		}
	}
}

function clearLoginText(obj, text) {
	if (obj != null) {
		if (obj.value == text) {
			obj.value = '';
		}
	}
}

function submitLoginForm() {
	showLoginProcess();
	AjaxRequest
			.submit(
					document.getElementById("login_form"),
					{
						'onSuccess' : function(req) {
							//hideLoginDiv();
							var data = req.responseText;
							data = data.trim();
							if (data == 'TRUE') {
								if(isIE6()){
									window.location.reload(false);
								}else{
									window.location.href = window.location.href;
								}
							} else if (data == 'NOACCES') {	
								hideLoginDiv();
								showOverlayMsg('You do not have rights to view this page');
							}else if (data == 'FALSE') {
								showLoginError('Invalid Username or Password Please try again.');
							} else if (data == 'DISABLED') {
								showLoginError('Your account has been disabled, please contact customer service.');
							} else if (data == 'CHNGPWD') {
								document.chng_pass_form.userName.value = document.login_form.username.value;
								document.chng_pass_form.submit();
							}
						}
					});
	return false;
}

function showLoginError(msg) {
	var msg_div = document.getElementById("login_err_msg");
	msg_div.innerHTML = msg;
	animatedcollapse.show('login_msg');
	//document.getElementById("login_msg").style.display = 'block';
}

function showLoginProcess() {
	var msg_div = document.getElementById("login_err_msg");
	var process_img = "<img width='12' height='12' src='/skin1/images/processing.gif'/ border='0'>&nbsp; Processing...";
	msg_div.innerHTML = process_img;
	animatedcollapse.show('login_msg');
	//document.getElementById("login_msg").style.display = 'block';
}

function hideLoginDiv() {
	animatedcollapse.hide('login_msg');
	//document.getElementById("login_msg").style.display = 'none';
}

function goHome() {
	window.location.href = "/";
}

function showOverlayMsg(msg){	
	resizeOverlay();
	document.getElementById("OverlayMsg").innerHTML=msg;
	if(isIE6()){
		document.getElementById("OverlayDiv").style.position="absolute";
		document.getElementById("OverlayMsgDiv").style.position="absolute";	
		hideAllCombo();
	}else{
		document.getElementById("OverlayDiv").style.position="fixed";
		document.getElementById("OverlayMsgDiv").style.position="fixed";	
	}
	document.getElementById("OverlayDiv").style.display="block";
	document.getElementById("OverlayMsgDiv").style.display="block";	
}

function hideOverlayMsg(){
	document.getElementById("OverlayDiv").style.display="none";
	document.getElementById("OverlayMsgDiv").style.display="none";
	if(isIE6){
		showAllCombo();
	}
}

function resizeOverlay(){
	document.getElementById("OverlayDiv").style.width=document.documentElement.clientWidth+'px';
	document.getElementById("OverlayDiv").style.height=document.documentElement.clientHeight+'px';
	document.getElementById("OverlayMsgDiv").style.left=((document.documentElement.clientWidth-300)/2)+'px';
	document.getElementById("OverlayMsgDiv").style.top=((document.documentElement.clientHeight-50)/2)+'px';
}

function isIE6(){
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
		var ieversion=new Number(RegExp.$1) 
		if(ieversion<=6){
			return true;
		}
	}
	return false;
}

function hideAllCombo(){
	if (isIE6()){
		var elms = document.getElementsByTagName("SELECT");
		for(var i=0;i<elms.length;i++){
			elms[i].style.visibility="hidden";
		}
	}
}

function showAllCombo(){
	if (isIE6()){
		var elms = document.getElementsByTagName("SELECT");
		for(var i=0;i<elms.length;i++){
			elms[i].style.visibility="visible";
		}
	}
}