/* ========================================================
   Common
   ======================================================== */

var g_currentlink;
var setMouseXY = false;
var g_mousepos;

var msgWindow = '';
var msgcurlink = '';

function Browser(){
	var agt = navigator.userAgent.toLowerCase();
	var is_major = parseInt(navigator.appVersion);
	var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	this.ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
	this.dom = document.getElementById?1:0;
	this.ie4 = (document.all && !this.dom)?1:0;
	this.ns4 = (document.layers && !this.dom)?1:0;
	this.ns6 = (this.dom && !document.all)?1:0;
	this.ie5 = (this.dom && document.all)?1:0;
	this.ok = this.dom || this.ie4 || this.ns4;
	this.platform = navigator.platform;
}

var browser = new Browser();

function getRef(obj){
	if(typeof obj == "string")
		obj= document.getElementById(obj);
	return obj;
}

function getStyle(obj, style){
   if(!document.getElementById) return;
   
     var obj = getRef(obj);
     var value = obj.style[style];
  	 
  	 if(!value)
         if(document.defaultView)
  	         value = document.defaultView.getComputedStyle(obj, "").getPropertyValue(style);
  	 	    
         else if(obj.currentStyle)
  	         value = obj.currentStyle[style];

     if(!value)
          value = obj.style.style;
     return value;
}

function resizeie() {
	if (browser.ie6) {
/*	if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {*/
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
		var elem = document.getElementById("maintable");
		if (elem != null) { elem.style.height = myHeight - 180; }
	}
}

function findPosition(oLink) {
	if (oLink.offsetParent)
		{
		for (var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent) {
			posX += oLink.offsetLeft;
			posY += oLink.offsetTop;
		}
		return [posX, posY];
	}
	else {
		return [oLink.x, oLink.y];
	}
}

function getParent(el, pTagName)
	{
	if (el == null)
		{
		return null;
		}
	else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase())
		{
		return el;
		}
	else
		{
		return getParent(el.parentNode, pTagName);
		}
	}

function setCookie(name, value, expires, path, domain, secure) {
  var todays_date = new Date();
  var expires_date = new Date(todays_date.getTime() + (1200000)); // 10 secs
  var curCookie = name + "=" + escape(value) +
      ((expires_date) ? "; expires=" + expires_date.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
		return;
	}
}


function showblock() {
	var ids = showblock.arguments;
	for(i=0; i<ids.length; i++) {
		elem = document.getElementById(ids[i]);
		elem.style.display = "block";
	}
}

function hideblock() {
	ids = hideblock.arguments;
	for(i=0; i<ids.length; i++) {
		elem = document.getElementById(ids[i]);
		elem.style.display = "none";
	}
}

function toggleblock() {
	var ids = toggleblock.arguments;
	var disp = false;
	for(i=0; i<ids.length; i++) {
		elem = document.getElementById(ids[i]);
		if(getStyle(elem, "display") == "none" ) {
			showblock(ids[i]);
			disp = true;
		} else {
			hideblock(ids[i]);
			disp = false;
		}
	}
	return disp;
}

function showspan() {
	ids = showspan.arguments;
	for(i=0; i<ids.length; i++) {
		elem = document.getElementById(ids[i]);
		elem.style.display = "inline";
	}
}

function hidespan() {
	ids = hidespan.arguments;
	for(i=0; i<ids.length; i++) {
		elem = document.getElementById(ids[i]);
		elem.style.display = "none";
	}
}

function createHash(array, map, key) {
	if (key == null) {
		key = map[0];
		/*
		for (n in map) {
			if (n == 0) {
				key = map[n];
				break;
			}
		}*/
	}
	var output = new Object();
	for (n in array) {
		tmpObj = new Object();
		for (i in map) {
			tmpObj[map[i]] = array[n][i];
		}
		/*
		for (i in map) {
			tmpObj[i] = array[n][map[i]];
		}*/
		output[tmpObj[key]] = tmpObj;
	}
	return output;
}

// not tested
function html_id(parent, valuename) {
	var ref = valuename.replace(/ /gi, '');
	ref = ref.replace(/\[/gi, '');
	ref = ref.replace(/\]/gi, '');
	var id = ref;
	if (parent) {
		parent = parent.replace(/ /gi, '');
		id = parent + id;
	}
	id = strtolower(id);
	return id;
}

/* ========================================================
   General
   ======================================================== */

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function doTask(myform, action) {
  if (myform.task.length) {
  for (var i = 0; i < myform.task.length; i++) {
    if (myform.task[i].value == action) {
  	myform.task[i].click();
	return;
    }
  }
  } else {
    if (myform.task.value == action) {
      myform.task.click();
    } else {
      myform.task.value = action;
      myform.submit();
    }
  }
}

function doAction(myform, action) {
  myform.action = action;
  myform.submit();
}

function showMsg(text) {
	self.status = text;
}

function hideMsg() {
	self.status = "";
}

function togglenp() {
	var np = document.getElementById("navpanel");
	var npbtn = document.getElementById("navpbutton");
/*	alert(np.style.display);*/
	var display = getStyle(np, "display");
	if (display == null) { display = np.style.display; }
	if (display == "block") {
		np.style.display = "none";
		npbtn.style.backgroundImage = "url(/images/npbutton_show.gif)";
	} else {
		np.style.display = "block";
		npbtn.style.backgroundImage = "url(/images/npbutton_hide.gif)";
	}
	setCookie("navpanel", np.style.display, null, "/");
}

function togglenpmenu(block) {
	var np = document.getElementById("np" + block);
	var img = document.getElementById("btn" + block);
	var display = getStyle(np, "display");
	if (display == null) { display = np.style.display; }
	if (display == "block") {
		np.style.display = "none";
		img.src = "/images/navpanelexp.gif";
	} else {
		np.style.display = "block";
		img.src = "/images/navpanelcol.gif";
	}
	block = block.replace(/\ /gi, '_');
	setCookie(block, np.style.display, null, "/");
}

function toggleOptions(i, option, changebg) {
	if (typeof i == "string") { i = document.getElementById(i); }
	if (i.checked) {
		enableOptions(option, true, changebg);
		return true;
	} else {
		enableOptions(option, false, changebg);
		return false;
	}
}

function enableOptions(i, bool, changebg) {
	if (typeof i == "string") { i = document.getElementById(i); }
	if ((i.tagName.toLowerCase() == "input") || (i.tagName.toLowerCase() == "select")) {
		var inputs = new Array();
		inputs[0] = i;
	} else {
		var inputs = i.getElementsByTagName('input');
		var selects = i.getElementsByTagName('select');
	}
	var disable = null;
	if (!bool) {
		if (changebg) {i.style.backgroundColor = "#eeeeee";}
		disable = "disabled";
	} else {
		if (changebg) {i.style.backgroundColor = "#FFFFFF";}
	}
	for (var n = 0; n < inputs.length; n++) {
		inputs[n].disabled = disable;
	}
	for (var n = 0; n < selects.length; n++) {
		selects[n].disabled = disable;
	}
}

function changebutton(i, name) {
	if (typeof i == "string") { i = document.getElementById(i); }
	if (i) { i.value = name; }
	return false;
}

function disablebutton(i) {
	if (typeof i == "string") { i = document.getElementById(i); }
	if (i) { i.disabled = "disabled"; }
}

function enablebutton(i) {
	if (typeof i == "string") { i = document.getElementById(i); }
	if (i) { i.disabled = null; }
}


function mselsetvalues(select, input) {
	var options = select.options, sOptions = [], opt, k=0;
	while(opt = options[k++])
		sOptions[sOptions.length] = opt.value;
	input.value = sOptions;
	return sOptions;
}

function display(i, display) {
	if (typeof i == "string") { i = document.getElementById(i); }
	i.style.display = display;
}

function setvar(id, value) {
	elem = document.getElementById(id);
	elem.value = value;
}

function unsetradio(gname, formelem) {
	
	if (formelem == null) {
		formelem = document.forms[0];
	}
	for (n=0;n<formelem.elements.length;n++){
		if(formelem.elements[n].name == gname){
			formelem.elements[n].checked = false;
		}
	}
	return false;
}

/* ========================================================
   filter
   ======================================================== */

function checklistonclick(elem, func) {
	for (var n = 0; n < elem.options.length; n++) {
		if (elem.options[n].selected == true) {
			var runfunc = func + "('" + elem.options[n].value + "')";
			eval(runfunc);
		}
	}
}

/* ========================================================
   for nicetitle/context menu
   ======================================================== */

// by Scott Andrew
// add an eventlistener to browsers that can do it somehow.
function addEvent(obj, evType, fn)
	{
	if (obj.addEventListener)
		{
		obj.addEventListener(evType, fn, true);
		return true;
		}
	else if (obj.attachEvent)
		{
		var r = obj.attachEvent('on'+evType, fn);
		return r;
		}
	else
		{
		return false;
		}
	}
	
function getMouseXY(e) {
var IE = document.all?true:false
  if (IE) { // grab the x-y pos.s if browser is IE
  	if (document.body.scrollTop) {
	    tempX = event.clientX + document.body.scrollLeft;
	    tempY = event.clientY + document.body.scrollTop;
    } else {
	    tempX = event.clientX + document.documentElement.scrollLeft;
	    tempY = event.clientY + document.documentElement.scrollTop;
	}
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX;
    tempY = e.pageY;
  } 

  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0;}
  if (tempY < 0){tempY = 0;}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
	g_mousepos = [tempX, tempY];
  return [tempX, tempY];
}

/* ========================================================
   Help hack
   ======================================================== */
function showhelp() {}

/* ========================================================
   Nicetitle
   ======================================================== */

var CURRENT_NICE_TITLE;
var g_tooltips;
var delay;
var interval = 0.5;



if (browser.dom || browser.ie4){
	document.write('<div class="nicetitle" id="tooltips"');
	document.write('><span class="titletext"><b>Loading</b></span></div>');
	document.write('<div class="contextmenu" id="context"');
	document.write('></div>');
}

function Tooltips(){
	g_tooltips = this;
	if (browser.dom || browser.ie4){
		var tmpLayer = browser.dom?document.getElementById('tooltips'):document.all.tooltips;
	}
	this.containerLayer = tmpLayer;
	/*
	var span = document.createElement('span');
	span.className = "titletext";
	tmpLayer.appendChild(span);*/
}

var g_context;

function Contextmenu(){
	g_context = this;
	if (browser.dom || browser.ie4){
		var tmpLayer = browser.dom?document.getElementById('context'):document.all.tooltips;
	}
	this.containerLayer = tmpLayer;
}

// 2003-11-19 sidesh0w
// trailerpark wrapper function
function showDelay(e)
	{
    if (window.event && window.event.srcElement)
		{
        lnk = window.event.srcElement
		}
	else if (e && e.target)
		{
        lnk = e.target
		}
    if (!lnk) return;

	lnk = getParent(lnk, 'a');

/*	g_mousepos = getMouseXY(e);*/

	delay = setTimeout("showNiceTitle(lnk)", interval * 1000);
	}


function showNiceTitle(link) {

/*	var XHTMLNS = 'http://www.w3.org/1999/xhtml';*/

    nicetitle = link.getAttribute('nicetitle');

	var ary = nicetitle.split("\\n");

	var maxlen = 5;
	for (n = 0; n < ary.length; n++) {
		if (ary[n].length > maxlen) {
			maxlen = ary[n].length;
		}
	}
	
	
	if (nicetitle.indexOf("{") != -1) {
		newtitle = "";
		while ((start = nicetitle.indexOf("{")) != -1) {
			end = nicetitle.indexOf("}");
			newtitle += nicetitle.substring(0, start);
			evalstring = nicetitle.substring(start + 1,end);
			newtitle += eval(evalstring);
			nicetitle = nicetitle.substring(end + 1);
		}
		newtitle += nicetitle;
		nicetitle = newtitle;
	}
	
	var titleindex = nicetitle.indexOf("\\n\\n");
	if (titleindex != -1) {
		var tiptitle = nicetitle.substring(0,titleindex);
		var tiptext = nicetitle.substr(titleindex + 4);
	} else {
		var tiptitle = nicetitle;
		var tiptext = "";
	}
	
	tiptext = tiptext.replace(/\\n/gi, '<br/>');

	w = maxlen * 6;


    mpos = findPosition(link);    
    mx = mpos[0];
    my = mpos[1];
    
    mx = g_mousepos[0];
    my = g_mousepos[1];
       
    var space = "<br/>";
    
    
   	if (tiptitle) {
   		if (tiptext) { space = "<br/><br/>"; }
    	g_tooltips.containerLayer.childNodes[0].innerHTML= "<b>" + tiptitle + "</b>" + space + tiptext;
    } else {
    	g_tooltips.containerLayer.childNodes[0].innerHTML= tiptext;
	}

	//alert(getStyle(g_tooltips.containerLayer.childNodes[0], "tagName"));
	//alert(g_tooltips.containerLayer.childNodes[0].className);
	
    g_tooltips.containerLayer.style.left = (mx+ 0) + 'px'

    g_tooltips.containerLayer.style.top = (my+18) + 'px';
    //g_tooltips.containerLayer.style.width = w + 'px'; 

    if (window.innerWidth && ((mx+w) > window.innerWidth)) {
        g_tooltips.containerLayer.style.left = (window.innerWidth - w - 25) + 'px';
	}

    if (document.body.scrollWidth && ((mx+w) > document.body.scrollWidth)) {
        g_tooltips.containerLayer.style.left = (document.body.scrollWidth - w - 25) + 'px';
	}

}
	
function hideNiceTitle(e) {

	if (delay) clearTimeout(delay);
    g_tooltips.containerLayer.style.left = -1000 + 'px';
    g_tooltips.containerLayer.style.top = -1000 + 'px';
}

function initnicetitle() {

	new Tooltips();
	
/*	if (!document.createElementNS) {
		document.createElementNS = function(ns, elt) {
			return document.createElement(elt);
		}
	}*/

	if (!document.links) {
		document.links = document.getElementsByTagName('a');
	}
	for (var ti=0; ti<document.links.length; ti++) {
		var lnk = document.links[ti];
		if (lnk.title) {
			lnk.setAttribute('nicetitle', lnk.title);
			lnk.removeAttribute('title');
			addEvent(lnk, 'mousemove', getMouseXY);
			addEvent(lnk, 'mouseover', showDelay);
			addEvent(lnk, 'mouseout', hideNiceTitle);
/*			addEvent(lnk, 'mousedown', getMouseXY);*/
		}
	}
    g_tooltips.containerLayer.style.left = -1000 + 'px';
    g_tooltips.containerLayer.style.top = -1000 + 'px';
    g_tooltips.containerLayer.style.visibility = "visible";
}

function initcontext() {
	new Contextmenu();
	g_context.containerLayer.style.left = -1000 + 'px';
	g_context.containerLayer.style.top = -1000 + 'px';
	g_context.containerLayer.style.visibility = "visible";
	
}

function showContext(link, items, type, value_id) {

	hideNiceTitle();

	w = 50;

	if (g_currentlink) { g_currentlink.style.backgroundColor = g_currentlink.orgcolour; }
	
	link.orgcolour = getStyle(link, "backgroundColor");
	if (link.orgcolour == null) {
		link.orgcolour = link.style.backgroundColor;
	}
/*	link.style.backgroundColor = "#aaa";*/
	g_currentlink = link;

    mpos = findPosition(link);    
    mx = mpos[0];
    my = mpos[1];
    
    //mx = g_mousepos[0];
    //my = g_mousepos[1];
       
    var space = "<br/>";
    
    var menu = "<table cellspacing='0'>";
    var maxlen = 0;

	// dynamic menu
	if (type != null) {
		if (type == "tslot") {
			var mydiv = getParent(link, "div");
			var ary1 = mydiv.getAttribute("id").split("s");
			var ary2 = ary1[0].split("d");
			var section = ary2[0].substring(1);
			var day = ary1[0].substring(3);
			var slot = ary1[1];
		
			if (link.getAttribute("id")) {
				ary = link.getAttribute("id").split('_');
				assigned_shift_id = ary[0].substring(3);
				link_unassign = "unAssignSlot('" + section + "', '" + day + "', '" + slot + "', '" + assigned_shift_id + "');";
				displaytext = "<b>unassign shift</b>";
				menu += "<tr><td><a onclick=\"" + link_unassign + "hideContext();return false;\" href='#'>" + displaytext + "</a></td></tr>";
				if (displaytext.length > maxlen ) {
						maxlen = displaytext.length;
				}
			} else {
/*				link_unassign = "";
			}*/
		    for (var n in items) {
				//type = dmap["type"];
				shift_name = name_index[items[n]["name_index_num"]]["name"];
				shift_time = time_index[items[n]["time_index_num"]]["time"];
				template_shift_id = items[n]["template_shift_id"];
				tracker = eval('tracker_' + day);
				num_offered = tracker[template_shift_id]["num_offered"];
				_link = "assignSlot('" + section + "', '" + day + "', '" + slot + "', '" + template_shift_id + "');hideContext();return false;";
				
				if (num_offered > 0) {
					displaytext = shift_name + " " + shift_time;
					menu += "<tr><td><a onclick=\"" + _link + "\" href='#'>" + displaytext + "</a></td></tr>";
					if (displaytext.length > maxlen ) {
						maxlen = displaytext.length;
					}
				} else {
				}
			}
			if (menu == "<table cellspacing='0'>") {
				menu += "<tr><td style='padding: 5px;color: #777;'>No more shift</td></tr>";
			}
		}
		} else if (type == "dropmenu") {
			w = getStyle(link, "width");
			if ((_pos = w.indexOf("px")) != -1) {
				 w = w.substring(0, _pos);
			}
			var elem = document.getElementById(value_id);
			var value = elem.value;
		    for (var n in items[0]) {
		    	var i_name = items[0][n];
		    	var i_value = items[1][n];
				_link = "setSelected('" + value_id + "', '" + i_value + "', '" + i_name + "');hideContext();return false;";
		    	if (i_value != value) {
					menu += "<tr><td><a onclick=\"" + _link + "\" href='#'>" + items[0][n] + "</a></td></tr>";
				} else {
					menu += "<tr><td><a class=\"cselected\" onclick=\"" + _link + "\" href='#'>" + items[0][n] + "</a></td></tr>";
				}
			}
		}

	} else {
		
	    for (var n in items) {
	
			if (n == "type") {
			
			}else if (items[n] == "-") {
				menu += "<tr><td><hr/></tr>";
			} else if (items[n] == "#" || items[n] == "") {
				menu += "<tr><td><a href='javascript: void(0)' class='disable'>" + n + "</a></td></tr>";
				if (n.length > maxlen) {
					maxlen = n.length;
				}
			} else {
				if (items[n]["popup"] && items[n]["link"]) {
				menu += "<tr><td><a href='#' onclick='staffdetail(\"" + items[n]["link"] + "\", \"" + items[n]["popup"] + "\");hideContext();'>" + n + "</a></td></tr>";
				} else {
				menu += "<tr><td><a href='" + items[n] + "'>" + n + "</a></td></tr>";
				}
				if (n.length > maxlen) {
					maxlen = n.length;
				}
			}
		}
	}
	
    menu += "</table>";
    
    //w = Math.max(maxlen * 8, w);
    if (y_offset = getStyle(g_currentlink, "height")) {
    	height = y_offset.replace(/px/gi, '');
    	if (height == "auto") {
    		y_offset = 15;	
    	} else {
			y_offset = eval(height);
		}
    } else {
		y_offset = 15;	
    }
    
    g_context.containerLayer.innerHTML= "<span class='titletext'>" + menu + "</span>";
    g_context.containerLayer.style.left = (mx+ 2) + 'px'
    g_context.containerLayer.style.top = (my+ y_offset + 2) + 'px';
//    g_context.containerLayer.style.width = w + 'px';

    w = Math.max(maxlen * 8, w);

    if (window.innerWidth && ((mx+w) > window.innerWidth)) {
        g_context.containerLayer.style.left = (window.innerWidth - w - 25) + 'px';
	}

    if (document.body.scrollWidth && ((mx+w) > document.body.scrollWidth)) {
        g_context.containerLayer.style.left = (document.body.scrollWidth - w - 25) + 'px';
	}
	
}
	
function hideContext(e) {
/*	alert("hide"); */
    if (g_context) {
	    g_context.containerLayer.style.left = -1000 + 'px';
	    g_context.containerLayer.style.top = -1000 + 'px';
    	if (g_currentlink) { g_currentlink.style.backgroundColor = g_currentlink.orgcolour; }
	    g_currentlink = null;
    }
}

function showDynamicContext(link, items) {
		
}

function setSelected(value_id, i_value, i_name) {
	var elem = document.getElementById(value_id);
	elem.value = i_value;
	g_currentlink.innerHTML = i_name;
}

window.document.onclick = function(e) {
    if (window.event && window.event.srcElement)	{
        lnk = window.event.srcElement
	} else if (e && e.target)	{
        lnk = e.target
	}
	
	if (lnk != g_currentlink) {
		if (getParent(lnk,"a") != g_currentlink) {
			hideContext();
		}
	}
}


/* ========================================================
   Cosmetic Form
   ======================================================== */
   
var buttonhcolor = "#AFBDD0";
var buttontexthcolor = "#111";

var fieldcolor = "#F4F7FA";
var fieldbordercolor = "1px solid #B6C7E1";
var fieldhcolor = "#FFFFFF";
var fieldhbordercolor = "1px solid #FFD1B2";

var fieldrowcolor = "transparent";
var fieldrowhcolor = "#FFF3EB";

var fielderrcolor = "#f7f7f7";
var fielderrbordercolor = "1px solid #FF8F8F";
var fielderrhcolor = "#FFFFFF";
var fielderrhbordercolor = "1px solid #FF0000";

var dtitlecolor = "#DEE4EC";
var dtitlehcolor = "#FFD1B2";

function hoverbutton(i) {
	i.orgcolour = getStyle(i, "color");
	if (i.orgcolour == null) {
		i.orgcolour = i.style.color;
	}
	i.style.color = buttontexthcolor;
	
	i.orgbgcolour = getStyle(i, "backgroundColor");
	if (i.orgbgcolour == null) {
		i.orgbgcolour = i.style.backgroundColor;
	}
	i.style.backgroundColor = buttonhcolor;
	
}

function restorebutton(i) {
	i.style.backgroundColor = i.orgbgcolour;
	i.style.color = i.orgcolour;
}

function hltitle(i) {
	
	if (typeof i == "string") {
		i = i.toLowerCase();
		var elem = document.getElementById(i);
	} else {
		if (i.getAttribute("id")) {
			var ary = i.getAttribute("id").split("-");
			ary[0] = ary[0].toLowerCase();
			var elem = document.getElementById(ary[0]);
		}
	}
	if (elem != null) {	elem.style.backgroundColor = dtitlehcolor; }
}

function restitle(i) {
	if (typeof i == "string") {
		i = i.toLowerCase();
		var elem = document.getElementById(i);
	} else {
		if (i.getAttribute("id")) {
			var ary = i.getAttribute("id").split("-");
			ary[0] = ary[0].toLowerCase();
			var elem = document.getElementById(ary[0]);
		}
	}
	if (elem != null) {	elem.style.backgroundColor = dtitlecolor; }
}

function restorefield_row(i) {
	if (i.nodeType == 1 && i.tagName.toLowerCase() == 'tr') {
		var tr = i;
	} else {
		var tr = getParent(i, "tr");
	}
	tr.style.backgroundColor = fieldrowcolor;
}

function focusfield_row(i) {
	if (i.nodeType == 1 && i.tagName.toLowerCase() == 'tr') {
		var tr = i;
	} else {
		var tr = getParent(i, "tr");
	}
	tr.style.backgroundColor = fieldrowhcolor;
}

function focusfield(i) {
	hltitle(i);
	focusfield_row(i);
	i.style.backgroundColor = fieldhcolor;
	i.style.border = fieldhbordercolor;
}

function restorefield(i) {
	restitle(i);
	restorefield_row(i);
	i.style.backgroundColor = fieldcolor;
	i.style.border = fieldbordercolor;
}

function focusfielderr(i) {
	hltitle(i);
	i.style.backgroundColor = fielderrhcolor;
	i.style.border = fielderrhbordercolor;
	var str = i.getAttribute("name").substring(8, (i.getAttribute("name").length - 1));
	elem = document.getElementById(str + "-error");
	elem.style.backgroundImage = "url(/images/arrow.gif)";
	focusfield_row(i);
}

function restorefielderr(i) {
	restitle(i);
	i.style.backgroundColor = fielderrcolor;
	i.style.border = fielderrbordercolor;
	var str = i.getAttribute("name").substring(8, (i.getAttribute("name").length - 1));
	elem = document.getElementById(str + "-error");
	elem.style.backgroundImage = "";
	restorefield_row(i);
}

function hlchecked() {
	document.inputs = document.getElementsByTagName('input');
	for (var i=0; i<document.inputs.length; i++) {
		if (document.inputs[i].type == "checkbox") {
			if (document.inputs[i].checked) {
				var elem = document.getElementById("row" + document.inputs[i].getAttribute("id"));
				hoverrow(elem);
				selectrow(document.inputs[i]);
				restorerow(elem);
			}
		}
	}
}


/* ========================================================
   Cosmetic List
   ======================================================== */

var rowoddcolor = "#F7F7F7";
var rowevencolor = "#FFFFFF";
var rowselectcolor = "#ffeecc";
var rowhlcolor = "#FFD1B2";

function selectrow(i) {
	var elem = document.getElementById("row" + i.getAttribute("id"));
	if (elem != null) {
		if (i.checked) {
			//lnk.setAttribute('nicetitle', lnk.title);
			if (elem.beforecolor == null) {
				if (elem.orgcolour) {
					elem.beforecolor = elem.orgcolour;
				} else {
					elem.beforecolor = "";
				}
			}
			elem.orgcolour = rowselectcolor;
			elem.style.backgroundColor=elem.orgcolour;
		} else {
			elem.orgcolour = elem.beforecolor;
			elem.style.backgroundColor = elem.beforecolor;
			elem.beforecolor = null;
		}
	}
}

function hoverrow(i) {
	i.orgcolour = getStyle(i, "backgroundColor");
	if (i.orgcolour == null) {
		i.orgcolour = i.style.backgroundColor;
	}
	i.style.backgroundColor = rowhlcolor;
}

function restorerow(i) {
	i.style.backgroundColor = i.orgcolour;
}

function checkall(name, self, formelem) {
	if (formelem == null) {
		formelem = document.forms[0];
	}
	for (n=0;n<formelem.elements.length;n++){
		if(formelem.elements[n].name == name){
			formelem.elements[n].checked = self.checked;
			selectrow(formelem.elements[n]);
		}
	}
}

/* ========================================================
   division task
   ======================================================== */
   
function checkdiv(i) {
	var ary = i.getAttribute("id").split("-");
	var elem = document.getElementById(ary[0].concat("-", ary[1]));
	if (elem == null) return;
	if (i.checked) {
		elem.checked = true;
	} else {
		var gname = (i.getAttribute("name"));
		var ischeck = false
		for (n=0;n<document.forms[0].elements.length;n++){
			if(document.forms[0].elements[n].name == gname){
				if (document.forms[0].elements[n].checked) {
					ischeck = true;
					break;
				}
			}
		}
		//if (!ischeck) {
		//	elem.checked = false;
		//}
	}
}

function disabletasks(i) {
	if (typeof i == "string") {
		var i = document.getElementById(i);
	}
	var ary = i.getAttribute("id").split("-");

	var doenable = false;
	if (i.checked) {
		doenable = true;
	}

	for (n = 0; n < document.forms[0].elements.length; n++) {
		if (document.forms[0].elements[n].getAttribute("id")) {
			var elem = document.forms[0].elements[n].getAttribute("id").split("-");
			if (elem == null) continue;
			if (elem[1] == ary[1] && document.forms[0].elements[n] != i) {
				if (doenable) {
					document.forms[0].elements[n].disabled = "";
				} else {
					//if (document.forms[0].elements[n].getAttribute("type") == "checkbox") {
						//document.forms[0].elements[n].checked = false;
					//}
					document.forms[0].elements[n].disabled = "disabled";
				}
			}
		}
	}
}

function hideOptions(i) {
	if (typeof i == "string") {
		check = document.getElementById(i);
/*		var ary = i.split("-"); */
	} else {
		check = i;
		i = i.getAttribute("id");
	}
	
	var elem = document.getElementById(i + "-options");

	if (elem) {
		if (check.checked) {
			elem.style.display = "block";
		} else {
			elem.style.display = "none";
		}
	}

}

function checkother(i) {
	if (typeof i == "string") {
		check = document.getElementById(i);
	} else {
		check = i;
		i = i.getAttribute("id");
	}
	
	var ary = i.split("-");
	var group = ary[0];
	var thisnum = ary[1];
	
	var n = 1;
	while(elem = document.getElementById(group + "-" + n)) {
		if (n != thisnum) {
			if (elem.checked) {
				elem.checked = false;
				hideOptions(elem);
				break;
			}
		}
		n++;
	}
}


function checkop(i, opname) {
	opname = "details[" + opname + "]";
	for (n=0;n<document.forms[0].elements.length;n++){
		if(document.forms[0].elements[n].name == opname){
			if (i.checked) {
				document.forms[0].elements[n].disabled = false;
			} else {
				document.forms[0].elements[n].disabled = true;
				document.forms[0].elements[n].checked = false;
			}
		}
	}
}

/* ========================================================
   Not used ?
   ======================================================== */
   
function resshift(i) {
	var $count = 1;
	while (true) {
		var elem = document.getElementById(i + "-n" + $count);
		if (elem == null) break;
		elem.style.backgroundColor = elem.style.orgcolor;
		$count++;
	}
	hideshifttip(i);
}

function hlshift(i) {
	var $count = 1;
	while (true) {
		var elem = document.getElementById(i + "-n" + $count);
		if (elem == null) break;
		elem.style.backgroundColor = "#FFFFFF";
		$count++;
	}
	showshifttip(i);
}

function restimeline(i, numofblock) {
	var ary = i.getAttribute("id").split("-");
	var $count = parseInt(ary[1].substr(1)) + 1;
	var elem = document.getElementById(ary[0] + "-n" + $count);
	i.style.backgroundColor = "#eee";
	if (elem != null) { elem.style.backgroundColor = "#eee"; }
	hideshifttip(i);
}

function hltimeline(i, numofblock) {
	var ary = i.getAttribute("id").split("-");
	var $count = parseInt(ary[1].substr(1)) + 1;
	var elem = document.getElementById(ary[0] + "-n" + $count);
	i.style.backgroundColor = "#FFFFFF";
	if (elem != null) { elem.style.backgroundColor = "#FFFFFF"; }
	hideshifttip(i);
}

function showshifttip(i) {
	var elem = document.getElementById(i + "-n1");
	showNiceTitle(elem);
}

function hideshifttip(i) {
	hideNiceTitle(i);
}


/* ========================================================
   collapsable box
   ======================================================== */

var tasktitletlbghl = "#FFEECC";

function toggletimeline(i,id) {
	var elem = document.getElementById(id);
	if (elem == null) return;
	var display = getStyle(elem , "display");
	if (display == null) {
		elem.style.display = "none";
		i.style.backgroundImage = "url(/images/crosshair_plus.gif)";
	} else if (display == "none") {
		elem.style.display = "block";
		i.style.backgroundImage = "url(/images/crosshair_minus.gif)";
	} else if (display == "block") {
		elem.style.display = "none";
		i.style.backgroundImage = "url(/images/crosshair_plus.gif)";
	}
}

function hltasktitletl(i) {
	i.orgbgcolor = i.style.backgroundColor;
	i.style.backgroundColor = tasktitletlbghl;
}

function restasktitletl(i) {
	i.style.backgroundColor = i.orgbgcolor;
}

function collapseall(block) {
	if (!document.tables) {
		document.tables = document.getElementsByTagName('table');
	}
	for (var i=0; i<document.tables.length; i++) {
		if (document.tables[i].className == block) {
			document.tables[i].style.display = "none";
		}
	}
	if (!document.spans) {
		document.spans = document.getElementsByTagName('span');
	}
	for (var i=0; i<document.spans.length; i++) {
		if (document.spans[i].className == "tasktitletl") {
			document.spans[i].style.backgroundImage = "url(/images/crosshair_plus.gif)";
		}
	}
}

function expandall(block) {
	if (!document.tables) {
		document.tables = document.getElementsByTagName('table');
	}
	for (var i=0; i<document.tables.length; i++) {
		if (document.tables[i].className == block) {
			document.tables[i].style.display = "block";
		}
	}
	if (!document.spans) {
		document.spans = document.getElementsByTagName('span');
	}
	for (var i=0; i<document.spans.length; i++) {
		if (document.spans[i].className == "tasktitletl") {
			document.spans[i].style.backgroundImage = "url(/images/crosshair_minus.gif)";
		}
	}
}

/* ========================================================
   Leave
   ======================================================== */

var cboxunavailcolor = "#FFEECC";
var cboxpendingcolor = "#C4D7F2";
var cboxavailcolor = "#ffffff";
var cboxdeniedcolor = "#F2BEBE";
var cboxselcolor = "#FF7D00";
var cboxtextcolor = "#777";

function tgAvail(i, boxname) {
	i.orgCursor = i.style.cursor;
/*	i.style.cursor = "progress";*/
	thisi = i;
	thisboxname = boxname;
	setTimeout("tgAvailmain(thisi, thisboxname)", 0);
	return false;
}

function tgAvailmain(i, boxname) {
	var box = document.getElementById(boxname);
	var myform = document.forms[0];
	var orgclass = i.className;
	var orgcolor = "";
	if (box == null) {
		i.style.borderColor = cboxselcolor;
		i.style.color = cboxselcolor;
		var input = document.createElement('INPUT');
        input.setAttribute('type', "hidden");
        input.setAttribute('name', boxname);
        input.setAttribute('value', "checked");
        input.setAttribute('id', boxname);
		myform.appendChild(input);
	} else {
		myform.removeChild(box);
		i.style.borderColor = i.style.backgroundColor;
		i.style.color = cboxtextcolor;
	}
	i.style.cursor = i.orgCursor;
	return false;
}


/* ========================================================
   Popup
   ======================================================== */
   
function staffdetail(url, winName) {
    if (!url) {
	return true;
    }
    if (msgWindow.location && !msgWindow.closed) {
	if (msgcurlink != url) {
	    msgWindow.location.href = url;
	    msgcurlink = url;
	}
    } else {
	   msgWindow=window.open(url, winName, "toolbar=no,width=300,height=370,directories=no,status=no,scrollbars=yes,resizable=no,menubar=no");
	   msgcurlink = url;
	   if (!msgWindow.opener) msgWindow.opener = self;
    }
    if (window.focus) { msgWindow.focus() }
}

function showmap(url, regionlist, winName) {
	var count = 0;
	if (elem = document.getElementById(regionlist)) {
		for (var i=0; i<elem.options.length; i++) {
			if (elem.options[i].selected) {
				if (winName == null) thiswinName = elem.options[i].name;
				else thiswinName = winName;
				window.open((url + elem.options[i].value), thiswinName, "toolbar=no,width=600,height=400,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no");
				count++;
/*				return; */
			}
		}
		if (count == 0) alert("Please select a region");
	}
}

function jobnotes(url, winName) {
    if (!url) {
	return true;
    }
	_msgWindow=window.open(url, winName, "toolbar=no,width=600,height=480,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no");
	if (!_msgWindow.opener) _msgWindow.opener = self;
    if (window.focus) { _msgWindow.focus() }
}

function popupWin(url, winName, width, height) {
    if (!url) {
	return true;
	}
    if (msgWindow.location && !msgWindow.closed) {
	if (msgcurlink != url) {
	    msgWindow.location.href = url;
	    msgcurlink = url;
	}
    } else {
    	if (width && height) {
		msgWindow=window.open(url, winName, "toolbar=no,width=" + width + ",height=" + height + ",directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no");
	} else {
		msgWindow=window.open(url, winName, "toolbar=no,width=350,height=600,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no");
	}
	msgcurlink = url;
	if (!msgWindow.opener) msgWindow.opener = self;
    }
    if (window.focus) { msgWindow.focus() }
}

function helpdetails(url, winName) {
    if (!url) {
	return true;
    }
    if (msgWindow.location && !msgWindow.closed) {
	if (msgcurlink != url) {
	    msgWindow.location.href = url;
	    msgcurlink = url;
	}
    } else {
	   msgWindow=window.open(url, winName, "toolbar=no,width=300,height=400,directories=no,status=no,scrollbars=yes,resizable=no,menubar=no");
	   msgcurlink = url;
	   if (!msgWindow.opener) msgWindow.opener = self;
    }
    if (window.focus) { msgWindow.focus() }
}



/* ========================================================
   Help
   ======================================================== */
function showhelp(helpid) {
	var helpblock = document.getElementById("contexthelp");
/*	var help;*/
	/*helpblock.innerHTML = "No help for this element : " + helpid;*/
	try {
		if (help == null) {};
	} catch (exception) {
		return false;
	}
	if (helpblock == null) {
		return false;
	}
	if (help  == null) {
/*		helpblock.innerHTML = "No help for this element : " + helpid;*/
	} else {
		if (help[helpid]) {
			helpblock.innerHTML = help[helpid];
		} else {
/*			helpblock.innerHTML = "No help for this element : " + helpid;*/
		}
	}
}

function togglehelp() {
	var help = document.getElementById("helpblock");
	var helpcol = document.getElementById("helpcolumn");
	var display = getStyle(help, "display");
	if (display == null) { display = help.style.display; }
	if (display == "block") {
		help.style.display = "none";
		helpcol.style.borderWidth = "0px";
		helpcol.style.width = "0px";
/*		img.src = "/images/navpanelexp.gif";*/
	} else {
		help.style.display = "block";
		helpcol.style.borderWidth = "1px";
		helpcol.style.width = "170px";
/*		img.src = "/images/navpanelcol.gif";*/
	}
/*	block = block.replace(/\ /gi, '_');*/
	setCookie("help", help.style.display, null, "/");
	return false;
}

function helpshow(helptype) {
	var contexthelp = document.getElementById("contexthelp");
	var overview = document.getElementById("overview");
	var contexthelp_btn = document.getElementById("contexthelp_btn");
	var overview_btn = document.getElementById("overview_btn");
	if (helptype == "overview") {
		if (overview_btn.firstChild.nodeValue == "+") {
			contexthelp.style.display = "none";
			overview.style.display = "block";
			contexthelp_btn.firstChild.nodeValue = "+";
			overview_btn.firstChild.nodeValue = "_";
		} else {
			contexthelp.style.display = "block";
			overview.style.display = "none";
			contexthelp_btn.firstChild.nodeValue = "_";
			overview_btn.firstChild.nodeValue = "+";
			helptype = "contexthelp";
		}
	} else {
		if (contexthelp_btn.firstChild.nodeValue == "+") {
			contexthelp.style.display = "block";
			overview.style.display = "none";
			contexthelp_btn.firstChild.nodeValue = "_";
			overview_btn.firstChild.nodeValue = "+";
		} else {
			contexthelp.style.display = "none";
			overview.style.display = "block";
			contexthelp_btn.firstChild.nodeValue = "+";
			overview_btn.firstChild.nodeValue = "_";
			helptype = "overview";
		}
	}
	setCookie("helptype", helptype, null, "/");
	return false;
}

/* ========================================================
   Rotating roster
   ======================================================== */
function unAssignSlot(section, day, slot, shiftid) {

	var array_name = 'slot_' + section + '_' + day;
	var day_array = eval(array_name);
	var tracker_name = 'tracker_' + day;
	var tracker = eval(tracker_name);
	var slot_id = "c" + section + "d" + day + "s" + slot;
	var mydiv = document.getElementById(slot_id);

	
	mydiv.removeChild(document.getElementById('sid' + shiftid + '_' + day));
	
	tracker[shiftid]['num_offered']++ ;
	
	/*
	var new_array = new Array();
	for (n in day_array[shiftid]['assigned']) {
		if (day_array[shiftid]['assigned'][n] != slot) {
			new_array.push(day_array[shiftid]['assigned'][n]);
		}
	}
	day_array[shiftid]['assigned'] = new_array;*/
	
	//alert(day_array[shiftid]['assigned']);
	
	inputname = "fc" + section + "d" + day + "s" + slot;
	var elem = document.getElementById(inputname);
	
	tmpArray = elem.value.split(',');
	var new_array = new Array();
	for (n in tmpArray) {
		//alert(tmpArray[n] + " " + shiftid);
		if (tmpArray[n] != shiftid) {
			new_array.push(tmpArray[n]);
		}
	}
	//alert(new_array);
	elem.value = new_array.join(',');
	cal_total(section, slot);
	//alert(elem.value);
	
/*	hideContext(); */
}

function assignSlot(section, day, slot, shiftid) {

	var array_name = 'slot_' + section + '_' + day;
	var day_array = eval(array_name);
	var tracker_name = 'tracker_' + day;
	var tracker = eval(tracker_name);
	var slot_id = "c" + section + "d" + day + "s" + slot;
	var mydiv = document.getElementById(slot_id);
	var shift_name = name_index[day_array[shiftid]["name_index_num"]]["name"];
	var shift_time = time_index[day_array[shiftid]["time_index_num"]]["time"];
	var displaytext = "<span class='sl'><div>" + sindex[shift_name] + "</div>" + shift_time + "</span>";
	//alert(sindex[day_array[shiftid]['shift_name']]);
	var aid = "sid" + shiftid + "_" + day;
	//var link = "<a class='tshift' id='" + aid + "' nicetitle='{sinfo(" + section + ", " + day + ", " + shiftid + ")}' href='#' onclick='showContext(this, slot_" + section + "_" + day +", \"tslot\");return false;'>" + displaytext +"</a>";
	
	link = document.createElement("a");
	link.setAttribute('nicetitle', "{sinfo(" + section + ", " + day + ", " + shiftid + ")}");
	link.className = 'ssl sbu';
	link.id = aid;
	link.onclick = function() {
		showContext(this, eval("slot_" + section + "_" + day), 'tslot');
		return false;
	}
	link.href = "#";
	link.innerHTML = displaytext;

	addEvent(link, 'mousemove', getMouseXY);
	addEvent(link, 'mouseover', showDelay);
	addEvent(link, 'mouseout', hideNiceTitle);
	
	mydiv.appendChild(link)

	tracker[shiftid]['num_offered']-- ;
	//day_array[shiftid]['assigned'].push(slot);
	
	inputname = "fc" + section + "d" + day + "s" + slot;
	var elem = document.getElementById(inputname);
	
	
	if (elem.value != "") {
		tmpArray = elem.value.split(',');
	} else {
		tmpArray = new Array();
	}
	tmpArray.push(shiftid);
	elem.value = tmpArray.join(',');
	cal_total(section, slot);
	
}

function addRow(section, rowNum) {

	var last_slot_id = "last_slot_" + section;
	var this_slot_id = ++document.getElementById(last_slot_id).value;
	var num_slots_id = "num_slots_" + section;
	var num_slots = ++document.getElementById(num_slots_id).value;;
/*	num_slots.value++; */
	var removelink = "<a href=\"#\" onclick=\"removeRow('" + section + "', '" + this_slot_id +"');return false;\">remove</a>";
	var addlink = "<a href=\"#\" onclick=\"addRow('" + section + "', '" + this_slot_id + "');return false;\">add row</a><br/>";
	var total = "<br/><br/>Total: <span id=\"ct_" + section + "_" + this_slot_id +"\">0</span>";


	if (!rowNum) {
		var trNum = num_slots;
	} else {
		var tr_id = "c" + section + "s" + rowNum;
		var trElem = document.getElementById(tr_id);
		var trNum = trElem.rowIndex;
	}
	

	var this_id = "detailstslots" + section + this_slot_id + "position";
	var this_input = "details[tslots][" + section + "][" + this_slot_id + "][position]";
	var positiondrop = "<a style=\"width: 65px\" class=\"dropmenu\" href=\"#\" onclick=\"showContext(this, i_position, 'dropmenu', '" + this_id + "');return false;\">&nbsp;</a><input class=\"hidden\" id=\"" + this_id + "\" name=\"" + this_input + "\" value=\"\"/>"


	inputname = "rindex_" + section;
	var elem = document.getElementById(inputname);
	if (elem.value != "") {
		tmpArray = elem.value.split(',');
	} else {
		tmpArray = new Array();
	}
	
	firstHalf = tmpArray.slice(0,trNum -1);
	lastHalf = tmpArray.slice(trNum - 1);
	
	newArray = new Array();
	newArray = newArray.concat(firstHalf, [this_slot_id],lastHalf);
	
	elem.value = newArray.join(',');

//	alert(elem.name + ": " + elem.value);
	
	/*
	for (n=0; n<elem.value.length; n++) {
		if (n == trNum) {
			tmpArray.push(shiftid);
		}
	}*/
	
	//elem.value = tmpArray.join(',');
	
	var x = document.getElementById("c" + section).insertRow(trNum);
	var td = document.createElement('td');
	td.className = "wktlsepalt";
	td.innerHTML =  addlink + positiondrop + removelink + total;
	x.appendChild(td);

	var codes = "";
	var td = document.createElement('td');
	td.style.width = "100%";
	codes += '<table style="width: 100%;height: 85px" class="wktimeline" border="0" cellpadding="0" cellspacing="0" width="100%">';
	codes += '<tr>';
	for (i = week_begin_day; i <= week_end_day; i++) {
		codes += '<td>';
		if ((!dayWOShift[section]) || ((dayWOShift[section]) && (!dayWOShift[section][i]))) {
			codes += '<input class="hidden" id="fc' + section + 'd' + i + 's' + this_slot_id + '" name="details[tslots][' + section + '][' + this_slot_id + '][' + i + ']" value="">';
			codes += '<div id="c' + section + 'd' + i + 's' + this_slot_id + '" class="tslot">';
			codes += '<a href="#" nicetitle="" onclick="showContext(this, slot_' + section + '_' + i + ', \'tslot\');return false;">[+]</a>';
			codes += '</div>';
			codes += '<img src="/images/spacer.gif" border="0" height="1">';
		}
		codes += '</td>';
	}
	codes += '</tr>';
	codes += '</table>';
	td.innerHTML = codes;
	x.appendChild(td);
	
	x.id = "c" + section + "s" + this_slot_id;

	//x.innerHTML = codes;
}

function removeRow(section, rowNum) {
	var tr_id = "c" + section + "s" + rowNum;
	var trElem = document.getElementById(tr_id);
	var table_id = "c" + section;

	trNum = trElem.rowIndex;

	inputname = "rindex_" + section;
	var elem = document.getElementById(inputname);
	if (elem.value != "") {
		tmpArray = elem.value.split(',');
	} else {
		tmpArray = new Array();
	}
	
	firstHalf = tmpArray.slice(0,trNum - 1);
	lastHalf = tmpArray.slice(trNum);
	
	newArray = new Array();
	newArray = newArray.concat(firstHalf,lastHalf);
	
	elem.value = newArray.join(',');
	
	var inputs = trElem.getElementsByTagName("input");

	for (n in inputs) {
		if ((inputs[n].name) && (inputs[n].name.indexOf("details[tslots]") != -1) && (inputs[n].name.indexOf("position") == -1)) {
			var d_pos = inputs[n].id.indexOf("d");
			var s_pos = inputs[n].id.indexOf("s");
			var day = inputs[n].id.substring(d_pos + 1, s_pos);
			if (inputs[n].value) {
				var tracker_name = 'tracker_' + day;
				var tracker = eval(tracker_name);
				tmpArray = inputs[n].value.split(',');
				for (n in tmpArray) {
					tracker[tmpArray[n]]['num_offered']++ ;
				}
			}
		}
	}
	
	document.getElementById(table_id).deleteRow(trNum);
	var num_slots_id = "num_slots_" + section;
	--document.getElementById(num_slots_id).value;


		/*
		var ary = div[n].id.split("s");
		var div_id = "c" + section + "d" + day + "s" + slot;
		var input_id = "f" + div_id;
		var d_id = "details[tslots][" + section + "][" + slot + "][" + day + "]";*/
}

function cal_total(section, rowNum) {

	var tr_id = "c" + section + "s" + rowNum;
	var trElem = document.getElementById(tr_id);
	var inputs = trElem.getElementsByTagName("input");
	var total = 0;
				
	for (n in inputs) {
		if ((inputs[n].name) && (inputs[n].name.indexOf("details[tslots]") != -1) && (inputs[n].name.indexOf("position") == -1)) {
			var d_pos = inputs[n].id.indexOf("d");
			var s_pos = inputs[n].id.indexOf("s");
			var day = inputs[n].id.substring(d_pos + 1, s_pos);
			if (inputs[n].value) {
				var dayary_name = 'slot_' + section + '_' + day;
				var dayary = eval(dayary_name);
				tmpArray = inputs[n].value.split(',');
				for (n in tmpArray) {
					total += dayary[tmpArray[n]]['length'] ;
				}
			}
		}
	}
	var counter_id = "ct_" + section + "_" + rowNum;
	var counterElem = document.getElementById(counter_id);
	
	// hack for mozilla bug
	if ((height=getStyle(trElem, "height")) != "auto") {
		trElem.style.height = getStyle(trElem, "height") + 0;
	}
	
	counterElem.innerHTML = total;
}

/* ========================================================
   Region
   ======================================================== */
function selectchild(reglist, list, selfvalue) {
	elem = document.getElementById(reglist);
	for (var n = 0; n < elem.options.length; n++) {
		if (selfvalue == elem.options[n].value) {
			elem.options[n].selected = false;
		}
		if (elem.multiple) {
			for (var i in list) {
				if (list[i] == elem.options[n].value) {
					elem.options[n].selected = true;
					break;
				}
			}
		}
	}
}

function checkregion(elem) {
	var changed = false
	for (var n = 0; n < elem.options.length; n++) {
		if (elem.options[n].selected == true) {
			if (elem.options[n].onclickt) {
				eval(elem.options[n].onclickt)
				changed = true;
			}
		}
	}
	return (!changed);
}

function checkregionlist(elem) {
	var changed = false
	for (var n = 0; n < elem.options.length; n++) {
		if (elem.options[n].selected == true) {
			if (elem.options[n].value == -1) {
				changed = true;
				alert("please select a sub region.");
			}
		}
	}
	return (!changed);
}

