function getEle(id) { var ele = document.getElementById(id); if (ele == 'undefined' || ele == null) return false; else return ele; }
function getEleName(name) { var ele = document.getElementsByName(name); if (ele == 'undefined' || ele == null) return false; else return ele; }
function getVal(id) { if (ele = getEle(id)) return getEle(id).value; return false; }
function getEleVal(id) { if (ele = getEle(id)) return getEle(id).value; return false; }
function getSelVal(id) { if (ele = getEle(id))	return ele[ele.selectedIndex].value; return false; }
function hideEle(id) {var ele = getEle(id); if (ele) ele.style.display = 'none';}
function showEle(id) {var ele = getEle(id); if (ele) ele.style.display = '';}
function togEle(id) { ele = getEle(id); if (ele.style.display == '') ele.style.display = 'none'; else ele.style.display = ''; }
function getRadVal(name) { if (ele = getEleName(name)) { for (i = 0; i < ele.length; i++) { if (ele[i].checked == true) { return ele[i].value; alert(ele[i].value); } } } return ''; }
function getFrmEle(form_id, ele_name) {
	var form = getEle(form_id);
	if (form == false) return false;
	var inputs = Array('input','select','textarea');
	for (var i=0; i < inputs.length; i++) {
		eles = form.getElementsByTagName(inputs[i]);
		for (var p=0; p < eles.length; p++) { if (eles[p].name == ele_name) return eles[p]; }
	}
}
function hideEles(eles) { for (i=0; i < eles.length; i++) hideEle(eles[i]); }


function OpenWindow(WinURL) {
	var w;
	this.name="parentwindow";
	w = window.open(WinURL, "popup", "width=500,height=600,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0");
	w.focus();
}

function VerifyDelete(cat) {
	return window.confirm("Are you sure you want to delete '" + cat + "'?");
}

function OpenWindowWH(WinURL,w,h) {
	var w;
	this.name="parentwindow";
	w = window.open(WinURL, "add", "width="+w+",height="+h+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0");
	w.focus();
}

function OpenWindowWHScroll(WinURL,w,h) {
	var w;
	this.name="parentwindow";
	w = window.open(WinURL, "add", "width="+w+",height="+h+",toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0");
	w.focus();
}
function fileUpload(page) {
  var winl = (screen.width-480)/2;
  var wint = (screen.height-150)/2;

  var w;
  //this.name="parentwindow";
  w = window.open(page, "upload", "top="+wint+",left="+winl+",width=480,height=150,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0");
  w.focus();
}
function hideshowid(id) {
  if (document.getElementById(id).style.display == '') {
	document.getElementById(id).style.display = 'none';
  } else {
	document.getElementById(id).style.display = '';
  }
}

function changeListStatus(status,list_id) {
	var url = '/xml/work_list_status.xml.asp';

	var myRequest = new ajaxObject(url);
	myRequest.callback = function(responseText, responseStatus, responseXML) {
		if (responseStatus==200) {
			document.getElementById('worklist_'+list_id).innerHTML = responseText
		}
	}
	myRequest.update('status='+status+'&list_id='+list_id);
}

function toggleDivSimple(id) {
	ele = getEle(id);
	ele.style.display = (ele.style.display == 'none') ? '' : 'none';
}

function toggleDiv(anchorId, contentId, anchorShowContents, anchorHideContents) {
	if (document.getElementById(contentId).style.display == '') {
		document.getElementById(contentId).style.display = 'none';
		document.getElementById(anchorId).innerHTML = anchorShowContents;
	} else {
		document.getElementById(contentId).style.display = '';
		document.getElementById(anchorId).innerHTML = anchorHideContents;
	}
}

function updateTimeEstimate(taskid) {
	var hourElement = document.getElementById('timehours_'+taskid);
	var minElement = document.getElementById('timeminutes_'+taskid);
	
	var newTime = parseInt(hourElement[hourElement.selectedIndex].value) + parseInt(minElement[minElement.selectedIndex].value);
	
	//
	var url = '/xml/update_time_estimate.asp';

	var myRequest = new ajaxObject(url);
	myRequest.callback = function(responseText, responseStatus, responseXML) {
		if (responseStatus==200) {
			//alert(responseText);
		}
	}
	myRequest.update('time='+newTime+'&taskid='+taskid);
}

function time_updn(direction,hoursId,minId) {
	var hourElement = getEle(hoursId);
	var minElement = getEle(minId);

	var sel_min_index = minElement.selectedIndex;
	var sel_hr_index = hourElement.selectedIndex;
		
	//	Calculate the next Minute
	if (direction == 'up') {
		sel_min_new_index = (sel_min_index + 1) % minElement.length;
	} else {
		sel_min_new_index = (sel_min_index + (minElement.length-1)) % minElement.length;
	}
	minElement.selectedIndex = sel_min_new_index;
	
	//Calculate if the hour needs to change
	if (direction == 'up') {
		if (sel_min_new_index == 0) {
			sel_hr_new_index = (sel_hr_index + 1) % hourElement.length;
			hourElement.selectedIndex = sel_hr_new_index;
		}		
	} else {
		if (sel_min_new_index == (minElement.length-1)) {
			sel_hr_new_index = (sel_hr_index + (hourElement.length-1)) % hourElement.length;
			hourElement.selectedIndex = sel_hr_new_index;
		}
	}	
}

function em_up() {
	//	Get the current selectes Minutes and Hours
	sel_min_index = document.frmTime.minutes.selectedIndex;
	sel_hr_index = document.frmTime.hours.selectedIndex;
		
	//	Calculate the next Minute
	sel_min_new_index = (sel_min_index + 1) % 4;
	document.frmTime.minutes.selectedIndex = sel_min_new_index;
	
	//Calculate if the hour needs to change
	if (sel_min_new_index == 0) {
		sel_hr_new_index = (sel_hr_index + 1) % 9;
		document.frmTime.hours.selectedIndex = sel_hr_new_index;
	}
}

//FOR WHEN THE END TIME DOWN ARROW IS PUSHED	
function em_dn() {
	//	Get the current selectes Minutes and Hours
	sel_min_index = document.frmTime.minutes.selectedIndex;
	sel_hr_index = document.frmTime.hours.selectedIndex;
		
	//	Calculate the next Minute
	sel_min_new_index = (sel_min_index + 3) % 4;
	document.frmTime.minutes.selectedIndex = sel_min_new_index;
		
	//Calculate if the hour needs to change
	if (sel_min_new_index == 3) {
		sel_hr_new_index = (sel_hr_index + 8) % 9;
		document.frmTime.hours.selectedIndex = sel_hr_new_index;
	}
}

function em_up_v2(taskid) {
	var hourElement = document.getElementById('timehours_'+taskid);
	var minElement = document.getElementById('timeminutes_'+taskid);

	var sel_min_index = minElement.selectedIndex;
	var sel_hr_index = hourElement.selectedIndex;
		
	//	Calculate the next Minute
	sel_min_new_index = (sel_min_index + 1) % 4;
	minElement.selectedIndex = sel_min_new_index;
	
	//Calculate if the hour needs to change
	if (sel_min_new_index == 0) {
		sel_hr_new_index = (sel_hr_index + 1) % 9;
		hourElement.selectedIndex = sel_hr_new_index;
	}
}

function em_dn_v2(taskid) {
	var hourElement = document.getElementById('timehours_'+taskid);
	var minElement = document.getElementById('timeminutes_'+taskid);
	
	sel_min_index = minElement.selectedIndex;
	sel_hr_index = hourElement.selectedIndex;
		
	//	Calculate the next Minute
	sel_min_new_index = (sel_min_index + 3) % 4;
	minElement.selectedIndex = sel_min_new_index;
		
	//Calculate if the hour needs to change
	if (sel_min_new_index == 3) {
		sel_hr_new_index = (sel_hr_index + 8) % 9;
		hourElement.selectedIndex = sel_hr_new_index;
	}
	updateTimeEstimate(taskid);
}

function show_notes(id) {
	if (document.getElementById(id).style.display == 'none')
		document.getElementById(id).style.display = '';
	else
		document.getElementById(id).style.display = 'none';
}

function close_fb() {
	$.fn.fancybox.close()
}

function fb_refresh() {
	window.location = window.location;
}

function submitTimeLog(which) {
	if (which.jobid.selectedIndex == 0 || which.jobActivityID.selectedIndex == 0 || (which.hours.selectedIndex == 0 && which.minutes.selectedIndex == 0) || which.note.value == '') {
		alert("Please complete the form before submitting!");
		return false;
	} else {
		return true;
	}
}

function goToJobPage(job_short_name) {
	var url = '/xml/jobs/get_job_id.asp';

	var ajax = new ajaxObject(url);
	ajax.callback = function(responseText, responseStatus, responseXML) {
		if (responseStatus==200) {
			if (isNaN(responseText)) {
				alert('The job could not be found.');
			} else {
				window.location = '/job_detail.asp?jobid='+responseText;
			}
		}
	}
	ajax.update('job_short_name='+encodeURIComponent(job_short_name), 'POST');
}

function showLoading() {	
	//create it if it doesnt exist
	if($("#table_loading").length < 1){		
		//make the div
		$('body').append('<div id="table_loading"><div style="background-color:#EEE"><img src="/images/loadingb.gif" /></div></div>');
	
		//get the position
		var top = $("body").position().top;
		var left = $("body").position().left;
		var width = $("body").width();
		var height = $("body").height();
		var offset_top = (height / 2) - ($("#table_loading img").height() / 2);
		var offset_left = (width / 2) - ($("#table_loading img").width() /2);
		
		//set the css
		$("#table_loading").css({ 'position':'absolute', 'width':width, 'height':height, 'background-color':'#EEEEEE', 'opacity':'0', 'top':top, 'left':left });
		$("#table_loading img").css({ 'position':'relative', 'top':offset_top, 'left':offset_left, 'opacity':'1', 'border':'solid #000000 1px', 'padding':'5px 15px 5px 15px' });
		
		//fix for ie
		$("#table_loading").animate({ 'opacity':'.7' },500);
	} else {
		//show the table
		$("#table_loading").show();
	}
}

//hide the loader
function hideLoading(){
	//hide the loading div
	$("#table_loading").hide();
}

Date.prototype.format=function(format){var returnStr='';var replace=Date.replaceChars;for(var i=0;i<format.length;i++){var curChar=format.charAt(i);if(replace[curChar]){returnStr+=replace[curChar].call(this);}else{returnStr+=curChar;}}return returnStr;};Date.replaceChars={shortMonths:['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],longMonths:['January','February','March','April','May','June','July','August','September','October','November','December'],shortDays:['Sun','Mon','Tue','Wed','Thu','Fri','Sat'],longDays:['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],d:function(){return(this.getDate()<10?'0':'')+this.getDate();},D:function(){return Date.replaceChars.shortDays[this.getDay()];},j:function(){return this.getDate();},l:function(){return Date.replaceChars.longDays[this.getDay()];},N:function(){return this.getDay()+1;},S:function(){return(this.getDate()%10==1&&this.getDate()!=11?'st':(this.getDate()%10==2&&this.getDate()!=12?'nd':(this.getDate()%10==3&&this.getDate()!=13?'rd':'th')));},w:function(){return this.getDay();},z:function(){return"Not Yet Supported";},W:function(){return"Not Yet Supported";},F:function(){return Date.replaceChars.longMonths[this.getMonth()];},m:function(){return(this.getMonth()<9?'0':'')+(this.getMonth()+1);},M:function(){return Date.replaceChars.shortMonths[this.getMonth()];},n:function(){return this.getMonth()+1;},t:function(){return"Not Yet Supported";},L:function(){return(((this.getFullYear()%4==0)&&(this.getFullYear()%100!=0))||(this.getFullYear()%400==0))?'1':'0';},o:function(){return"Not Supported";},Y:function(){return this.getFullYear();},y:function(){return(''+this.getFullYear()).substr(2);},a:function(){return this.getHours()<12?'am':'pm';},A:function(){return this.getHours()<12?'AM':'PM';},B:function(){return"Not Yet Supported";},g:function(){return this.getHours()%12||12;},G:function(){return this.getHours();},h:function(){return((this.getHours()%12||12)<10?'0':'')+(this.getHours()%12||12);},H:function(){return(this.getHours()<10?'0':'')+this.getHours();},i:function(){return(this.getMinutes()<10?'0':'')+this.getMinutes();},s:function(){return(this.getSeconds()<10?'0':'')+this.getSeconds();},e:function(){return"Not Yet Supported";},I:function(){return"Not Supported";},O:function(){return(-this.getTimezoneOffset()<0?'-':'+')+(Math.abs(this.getTimezoneOffset()/60)<10?'0':'')+(Math.abs(this.getTimezoneOffset()/60))+'00';},P:function(){return(-this.getTimezoneOffset()<0?'-':'+')+(Math.abs(this.getTimezoneOffset()/60)<10?'0':'')+(Math.abs(this.getTimezoneOffset()/60))+':'+(Math.abs(this.getTimezoneOffset()%60)<10?'0':'')+(Math.abs(this.getTimezoneOffset()%60));},T:function(){var m=this.getMonth();this.setMonth(0);var result=this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/,'$1');this.setMonth(m);return result;},Z:function(){return-this.getTimezoneOffset()*60;},c:function(){return this.format("Y-m-d")+"T"+this.format("H:i:sP");},r:function(){return this.toString();},U:function(){return this.getTime()/1000;}};
