//////////////////////////////////////////////
//  Forms
//////////////////////////////////////////////

function submitbutton( pressbutton ) {
	if (pressbutton == 'delete') {
		if ( confirm ('Are you sure?') ) {
			submitform( pressbutton );
		}
	} else {
		submitform( pressbutton );
	}
}

function submitform( pressbutton ) {
	var frm = document.forms[0];
	frm.task.value = pressbutton;
	try {
		frm.onsubmit();
	}
	catch(e) { }
	frm.submit();
}

function tableOrdering( order, dir , form) {



	var frm = document.forms.requestForm;

	frm.order.value 	= order;
	frm.orderDir.value	= dir;
	//submitform( task );
	document.requestForm.submit();
}



//////////////////////////////////////////////
//  Calendar
//////////////////////////////////////////////

// JS Calendar
var calendar = null; // remember the calendar object so that we reuse
// it and avoid creating another

// This function gets called when an end-user clicks on some date
function selected(cal, date) {
	cal.sel.value = date; // just update the value of the input field
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks the "Close" (X) button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
	cal.hide();			// hide the calendar

	// don't check mousedown on document anymore (used to be able to hide the
	// calendar when someone clicks outside it, see the showCalendar function).
	Calendar.removeEvent(document, "mousedown", checkCalendar);
}

// This gets called when the user presses a mouse button anywhere in the
// document, if the calendar is shown.  If the click was outside the open
// calendar this function closes it.
function checkCalendar(ev) {
	var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
	for (; el != null; el = el.parentNode)
	// FIXME: allow end-user to click some link without closing the
	// calendar.  Good to see real-time stylesheet change :)
	if (el == calendar.element || el.tagName == "A") break;
	if (el == null) {
		// calls closeHandler which should hide the calendar.
		calendar.callCloseHandler(); Calendar.stopEvent(ev);
	}
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id) {
	var el = document.getElementById(id);
	if (calendar != null) {
		// we already have one created, so just update it.
		calendar.hide();		// hide the existing calendar
		calendar.parseDate(el.value); // set it to a new date
	} else {
		// first-time call, create the calendar
		var cal = new Calendar(true, null, selected, closeHandler);
		calendar = cal;		// remember the calendar in the global
		cal.setRange(1900, 2070);	// min/max year allowed
		calendar.create();		// create a popup calendar
		calendar.parseDate(el.value); // set it to a new date
	}
	calendar.sel = el;		// inform it about the input field in use
	calendar.showAtElement(el);	// show the calendar next to the input field

	// catch mousedown on the document
	Calendar.addEvent(document, "mousedown", checkCalendar);
	return false;
}



//////////////////////////////////////////////
//  Check boxes
//////////////////////////////////////////////

/**
* Toggles the check state of a group of boxes
*
* Checkboxes must have an id attribute in the form cb0, cb1...
* @param The number of box to 'check'
* @param An alternative field name
*/
function checkAll( n, fldName ) {
	var frm = document.requestForm;
	 
	if (!n) {
		
		n = frm.elements.length;
		
	}
	if (!fldName) {
		fldName = 'cb';
	}
	var c = frm.toggle.checked;
	
	var n2 = 0;
	for (i=0; i < n; i++) {
		cb = eval( 'frm.' + fldName + '' + i );
		if (cb) {
			cb.checked = c;
			isChecked(i, c);
			n2++;
		}
	}
	if (c) {
		frm.boxchecked.value = n2;
	} else {
		frm.boxchecked.value = 0;
	}
}

function isChecked(i, isitchecked){
	var frm = document.requestForm;
	cb = eval( 'frm.cb' + i );
	if (cb) {
		row = cb.parentNode.parentNode;
		if (isitchecked == true){
			row.className = 'rowc';
		}
		else {
			row.className = 'row'+i%2;
		}
	}
	
	if (isitchecked == true){
		frm.boxchecked.value++;
	}
	else {
		frm.boxchecked.value--;
	}
}



//////////////////////////////////////////////
//  Utilities
//////////////////////////////////////////////

// LTrim(string) : Returns a copy of a string without leading spaces.
function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

// Trim(string) : Returns a copy of a string without leading or trailing spaces
function trim(str) {
   return rtrim(ltrim(str));
}

/**
* Pops up a new window in the middle of the screen
*/
function popupWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function submit1() {

	var frm = document.requestForm;
	frm.task.value 	= 'join';
	document.requestForm.submit();
}

function settask(inf) {
	var frm = document.requestForm;
	if(inf == 1) 
		frm.task.value 	= 'return1';
	if(inf == 2) 

		frm.task.value 	= 'return2';
		
	if(inf == 3) 

		frm.task.value 	= 'return2';	
		
	return true;
}
