//
var popUpHome=null;
var PopUp=null;
var PopUpReport=null;
var popUpClient=null;
var popUpLocation=null;
var popUpRegistration=null;
var popUpHelp=null;
var popUpNotice=null;
var popUpCalendar=null;
var popUpCancellation=null;
//
function doGetDown() {
	if(arguments.length>0) window.document.forms[0].level.value=arguments[0];
	doCmd('getdown');
}
function doFinish() {
	  doCmd('finish');
}
function doBack() {
	  doCmd('doback');
}
function doNext() {
	  doCmd('donext');
}
function doReset() {
	  doCmd('reset');
}
function doSave() {
	  doCmd('save');
}
function doSignOut(process) {
   if(confirm(singOutConfirmation)) doProcess(process);
}

function doProcess() {
	if(arguments.length>0) window.document.forms[0].process.value=arguments[0];
	if(arguments.length>1) window.document.forms[0].entry.value=arguments[1];
   doCmd('process');
}

function goDirect() {
	var evt = arguments[1];
	if (evt && "cancelBubble" in evt) {
		evt.cancelBubble = true;
	} else if (evt && "stopPropagation" in evt) {
		evt.stopPropagation();
	}
	if(arguments.length>0) window.document.forms[0].entry.value=arguments[0];
	  doCmd('direct');
}
function goTab() {
	if(arguments.length>0) window.document.forms[0].entry.value=arguments[0];
	  doCmd('tab');
}
function resetClient() {
	document.forms[0].method.value='resetClient';
	doCmd('save');
}

var formSubmitted = false;
var reqCounter = 0;
var singleSubmitCheck = true;		// set true to disable multiple submits

function doCmd ( cmd )	{
	if (formSubmitted==false&&reqCounter==0) {
		formSubmitted = true;
		try {
			parent.formSubmitted = true;
		} catch(everything){}
		try {
			if(cmd!='save'&&document.forms[0].frame.value=='true') {
				 document.forms[0].target = '_parent';
			}
		} catch(everything){}
		window.document.forms[0].cmd.value = cmd;
		setTimeout("window.document.forms[0].submit();",10);
	}
}

function doDelete() {
	if (confirm('Are you sure you want to delete this item?')) {
		document.forms[0].method.value = 'delete';
		doSave();
	}
}

function doRemove() {
	if (confirm('Are you sure you want to delete this item?')) {
		document.forms[0].method.value = 'delete';
		doFinish();
	}
}

var isNav = (navigator.appName.indexOf("Netscape") != -1);
var isIE = (navigator.appName.indexOf("Microsoft") != -1);
var ver = parseInt(navigator.appVersion);

function hoverLink(linkobj, mode)	{
//alert("hoverLink name="+linkobj.nodeName);
	if (isIE || ver > 4) {
		if (mode==1){
//alert("name="+linkobj.nodeName);
			if(linkobj.nodeName=='FONT') linkobj.style.textDecoration='underline';
			if(arguments.length>2) self.status=arguments[2];
			else if(linkobj.nodeName=='FONT') {
				var cont=linkobj.childNodes;
				var str=linkobj.firstChild.nodeValue;
				for(var i=1; i<cont.length; i++) str+=cont[i].nodeValue;
				self.status=str;
			} 
			if (isIE){
				linkobj.style.cursor='hand';
			} else {
				linkobj.style.cursor='pointer';
			}
		} else {
			if(linkobj.nodeName=='FONT') linkobj.style.textDecoration='none';
			self.status='';
		}
	}
	return true;
}
function hoverCell(linkobj)	{
//alert("hoverCell name="+linkobj.nodeName);
	if (isIE || ver > 4) {
		if (isIE)linkobj.style.cursor='hand';
		else linkobj.style.cursor='pointer';
		if(arguments.length>1) self.status=arguments[1];
	}
	return true;
}
var cachedImage1=new Image();
cachedImage1.src="../images/lens.gif";
var cachedImage2=new Image();
cachedImage2.src="../images/plus.gif";
var cachedImage3=new Image();
cachedImage3.src="../images/minus.gif";
var cachedImage4=new Image();
cachedImage4.src="../images/sc_popupcal.gif";
var cachedImage5=new Image();
cachedImage5.src="../images/Week.gif";
var cachedImage6=new Image();
cachedImage6.src="../images/checkedin.gif";
var cachedImage7=new Image();
cachedImage7.src="../images/checkedout.gif";
var cachedImage8=new Image();
cachedImage8.src="../images/confirmed.gif";
var cachedImage9=new Image();
cachedImage9.src="../images/uncheckedin.gif";
var cachedImage10=new Image();
cachedImage10.src="../images/uncheckedout.gif";
var cachedImage11=new Image();
cachedImage11.src="../images/unconfirmed.gif";
var cachedImage12=new Image();
cachedImage11.src="../images/corner_lt.gif";
var cachedImage13=new Image();
cachedImage11.src="../images/corner_rt.gif";

function checkBrowser(){
	if(isNav==true&&ver<5) window.location='browserSupportError.html';
	else window.document.forms[0].process.value='login';
}
function openTimeTrade() {
	if(popUpHome!=null&&popUpHome.closed!=true) popUpHome.close();
	popUpHome= window.open('http://www.timetrade.com', 'timetrade');
	popUpHome.focus();
}
var winFocus=true;
var checkUnload=true;
//window.onunload=unloadHandler;
//window.onblur=function(e){winFocus=false;}
//window.onfocus=function(e){winFocus=true;}
function unloadHandler(e){
	if(checkUnload && (!window.formSubmitted) && (!winFocus)) {
		if(popUpHome!=null&&!popUpHome.closed) popUpHome.close();
		if(PopUp!=null&&!PopUp.closed) PopUp.close();
		if(PopUpReport!=null&&!PopUpReport.closed) PopUpReport.close();
		if(popUpClient!=null&&!popUpClient.closed) popUpClient.close();
		if(popUpLocation!=null&&!popUpLocation.closed) popUpLocation.close();
		if(popUpRegistration!=null&&!popUpRegistration.closed) popUpRegistration.close();
		if(popUpHelp!=null&&!popUpHelp.closed) popUpHelp.close();
		if(popUpNotice!=null&&!popUpNotice.closed) popUpNotice.close();
		if(popUpCalendar!=null&&!popUpCalendar.closed) popUpCalendar.close();
		if(popUpCancellation!=null&&!popUpCancellation.closed) popUpCancellation.close();
		window.location='login.do'+getJsession()+'?method=signout';
	}
}
function trimTextareas() {
	var textAreas=document.getElementsByTagName('textarea');
	if(textAreas&&textAreas.length>0) {
		for(var i=0; i<textAreas.length; i++) {
			var arr=textAreas[i];
			arr.value=String(arr.value).trim();
		}
	}
}
/**
 * FF and IE have problem showing border on checkboxes and dropdown respectively.
 * Function handles those scenarios.
 */
function checkErrorItems(){
	var errorField = document.getElementById("errorTag");
	if (!errorField)return;
	var errorData = errorField.innerHTML;
	if (errorData ==null || errorData == "")return;
	var browser=navigator.appName;
	var version = (browser=="Microsoft Internet Explorer")?   parseInt(navigator.userAgent.split("MSIE")[1]): 1;
	//Apparently FF can not attach border to checkboxes and radio buttons with style.border.
	//But it can attach style.outline to checkboxed and buttons.
	//Using outline class for these items.
	if (browser=="Netscape"){
		var arr = document.getElementsByTagName("input");
		var len =  arr.length;
		for(var i=0; i < len; i++){
			var errorClass = arr[i].getAttribute("class");
			var item = arr[i];
			if ((item.type =="checkbox" || item.type =="radio")
				&& errorClass == "q_error"){
				item.setAttribute("class","q_error_ff");	
			}
		}
	}
	//Apparently we can't attach border to select in IE. This is workaround to implement border around select for IE.
	//Attaching a span around select and giving border to it.
	//This workaround may not be elegant. Good ideas always appreciated.
	else if(browser=="Microsoft Internet Explorer" 
			&& (version == 6 || version == 7 || version == 8) ){
		var arr = document.getElementsByTagName("select");
		var len =  arr.length;
		for(var i=0; i < len; i++){
			var item = arr[i];
			var errorClass = arr[i].getAttribute("className");
			if (errorClass == "q_error"){
				var newSpan = document.createElement("span");
				newSpan.setAttribute("className", "f12n q_error");
				newSpan.id = "errorSpan";
				var parentDiv = item.parentElement;
				parentDiv.insertBefore(newSpan, item);
				newSpan.appendChild(item);
			}
			
		}
	
	}
}

//
String.prototype.LTrim=function() {
	return this.replace(/\s*((\S+\s*)*)/, "$1");
}
String.prototype.RTrim=function(){
	return this.replace(/((\s*\S+)*)\s*/, "$1");
}
String.prototype.trim=function() {
	return this.LTrim().RTrim();
}
String.prototype.isNumeric=function() {
	return this.match(/^(-)?(\d*)(\.?)(\d*)$/);
}
Array.prototype.contains=function(val) {
	if(this.length>0) {
		for(var i=0; i<this.length; i++) {
			if(this[i]==val) return true;
		}
	}
	return false;
}
//
if(window.attachEvent) window.attachEvent('onload', trimTextareas);
else window.addEventListener('load', trimTextareas, false);