/* Check and uncheck checkboxes depending on the value of the checkbox */
function checkAll(fmobj, me) { // default value = (this, checkbox_name)
	//value = document.getElementById(me).value;
	value = '';
	for (var i=0;i<fmobj.elements.length;i++) {
		var e = fmobj.elements[i];
		if ( (e.type=='checkbox') && (!e.disabled) && e.name != me ) {
			e.checked = value;
		} else if (e.name == me && e.type=='checkbox' && !e.disabled ) {
			value = e.checked;
		}
	}
}

/* Change color of table rows on mouse actions */
var DHTML = (document.getElementById || document.all || document.layers);
var sRow_hl = '#eee';			// color of row with more vibrant color
var sRow_ll = '#fff';			// color of row with dull color
var sRow_mover = '#FFFDE9'; //'#879FA9';		// color of row at mouseover
var sRow_active = '#003d4c'; 	// color of row when its the active row
var sFont_mover = 'black';		// font color for the row at mouseover
var sFont_active = 'white';		// font color for the row when its the chosen row. this is called at mouseup event
var sFont_inactive = 'black';	// font color for the row when its not chosen

var sRow_ID = 'highlighted-row';
var sRow_ID_prefix = 'row';
function result_mouseover(id){
	//check that browser has capabilities
	if (!DHTML) return;							
	
	var hl_row = $(sRow_ID);
	if (hl_row.value==id) {
		//row.style.backgroundColor = '#003399';
		return;
	}
		
	var row = $( sRow_ID_prefix + id );
	row.style.backgroundColor = sRow_mover;
	row.style.color = sFont_mover;
}

function result_mouseout(id) {
	//check that browser has capabilities
	if (!DHTML) return;		
						
	var hl_row = $(sRow_ID);
	if (hl_row.value==id) {
		//row.style.backgroundColor = '#003399';
		return;
	}
	
	var row = $( sRow_ID_prefix + id );
	row.style.backgroundColor = __alternate_color(id);	
	row.style.color = sFont_inactive;
}

function result_mouseup(id){
	
	if (!DHTML) return;	
	var hl_row = $(sRow_ID);
	var oldrow_id = hl_row.value;
	
	hl_row.value=id;
	
	var row = $( sRow_ID_prefix + id );
	var row2 = $(sRow_ID_prefix + oldrow_id);
	
	
	// old row
	if (row2 != null) {
		row2.style.color = sFont_inactive;
		row2.style.fontWeight = 'normal';
		row2.style.backgroundColor = __alternate_color(oldrow_id);	
	}
		
	// highlight present row 
	row.style.color = sFont_active;
	row.style.fontWeight = 'bold';
	row.style.backgroundColor = sRow_active;
	return;
		
}

function __alternate_color(num) {
	var count = num % 2;		
	if (count == 1) {
		return sRow_ll;
	} else {
		return sRow_hl;
	}
}

/* For Search Focus */
	/**
	 * Remove the value of the search box if it equals SEARCH
	 *
	 * @param val - value of the caller
	 * @param nodeID - ID of the search input box
	 * @author: Rachel
	 */
	function searchfocus(val,nodeID) {
		if (val.length == 0 || val=='Search') {
			$(nodeID).value = '';
		}
		$(nodeID).style.color = '000000';
	}
	
	/**
	 * Add a watermark Search to search box if it is empty
	 *
	 * @param val - value of the caller
	 * @param nodeID - ID of the search input box
	 * @author: Rachel
	 */
	function searchblur(val,nodeID) {
		if (val.length == 0) {
			$(nodeID).value = 'Search';					
			$(nodeID).style.color = 'cccccc';
		}
	}			

	/**
	 * Returns true if any of radio boxes with the NAMEs of elementName contained in the FORM named formName is checked
	 *
	 * @param formName - ID of the FORM
	 * @param elementName - NAME of the radio boxes
	 * @author: Rachel
	 */
	function isRadioEmpty(formName, elementName) {
		var empty = true;
		for (i=0; i<=document.forms[formName].elements[elementName].length - 1; i++) {
			if (document.forms[formName].elements[elementName][i].checked) empty = false;
		}
		
		if (empty) return true;
		return false;
	}
	
/*
Inserting error message after any input boxes. Reference of use at http://
*/
	/**
	 * Insert error message after the element containerID
	 * Add style to class errFld to add style to the error message
	 * You can delete the previous message created by using @link removeID('err'+errID)
	 *
	 * @param containerID - ID of an element preceeding the error message
	 * @param errID - any unique ID that you can give to the error message to uniquely identify it
	 * @param msg - message
	 * @param containerType - default value is DIV tag
	 * @author: Rachel
	 */
	function insertMsg(containerID, errID, msg, containerType) {
		if (!containerType) containerType = 'div';
		new Insertion.After(containerID, '<'+containerType+' id="err'+errID+'" class="errFld">'+msg+'</'+containerType+'>');
	}

	/**
	 * Delete the element if it exist. Do nothing if it doesnt exist
	 *
	 * @param id - element IDs
	 * @author: Rachel
	 */
	function removeMsg(ids) {
		for (x=0; x<=ids.length - 1; x++) {
			try { Element.remove(ids[x]); } catch (err) {} // do nothing
		}		
	}

/*
EOF: Inserting error message after any input boxes.
*/
	
/* Pop up window */
function popup(mylink, windowname) {
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
   		href=mylink;
	else
   		href=mylink.href;
	
   	window.open(href, windowname, 'width=350,height=525,scrollbars=yes');
	return false;
}

/* Inner FAQ */
var cX = 0; var cY = 0;
/*
function UpdateCursorPosition(e){ cX = e.pageX; cY = e.pageY;}
function UpdateCursorPositionDocAll(e){ cX = e.clientX; cY = e.clientY;}

if(document.all) { document.onmousemove = UpdateCursorPositionDocAll; }
else { document.onmousemove = UpdateCursorPosition; }
*/
document.onmousemove = UpdateCursorPosition;

function UpdateCursorPosition(e) {
  var xcoord, ycoord;
  if( !e ) { e = window.event; }
  if( !e ) { return; }
  if( typeof( e.pageX ) == 'number' ) {
    xcoord = e.pageX;
    ycoord = e.pageY;
  } else if( typeof( e.clientX ) == 'number' ) {
    xcoord = e.clientX;
    ycoord = e.clientY;
    if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
      xcoord += document.body.scrollLeft;
      ycoord += document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
      xcoord += document.documentElement.scrollLeft;
      ycoord += document.documentElement.scrollTop;
    }
  } else { return; }

	cX = xcoord;
	cY = ycoord;

}



function AssignPosition(d) {
	d.style.left = (cX+10) + "px";
	d.style.top = (cY+10) + "px";
}
function HideContent(d) {
	if(d.length < 1) { return; }
	document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
	if(d.length < 1) { return; }
	var dd = document.getElementById(d);
	AssignPosition(dd);
	dd.style.display = "block";
}

function ReverseContentDisplay(d) {
	if(d.length < 1) { return; }
	var dd = document.getElementById(d);
	AssignPosition(dd);
	if(dd.style.display == "none") { dd.style.display = "block"; }
	else { dd.style.display = "none"; }
}
/* EOF Inner FAQ */

/**
 * Whether show or hide a certain text or form when this event is triggered
 *
 * @param id Id of the calling element
 * @param targetId Id of the element to show/hide
 * @param inputType eg checkbox, radiobox, text
 */
function display(id, targetId, inputType) {
	_input = $(targetId);
	switch(inputType) {
		case 'checkbox':
			show = $(id).checked;
			break;
	}
		
	if (show == true) {
		_input.style.display = 'block';
	} else {
		_input.style.display = 'none';
	}
	
}

checkAvail = function(f, url) {
     val = f.form["data[User][username]"].value;
     new Ajax.Updater('msg', url+val, {onLoading:function(request){}, asynchronous:true, evalScripts:true});
}

// Removes leading whitespaces
function LTrim( value ) {	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");	
}

// Removes ending whitespaces
function RTrim( value ) {	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");	
}

// Removes leading and ending whitespaces
function trim( value ) {	
	return LTrim(RTrim(value));	
}
function getElementsByClassName(classname, node) {
	if(!node) node = document.getElementsByTagName("body")[0];
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for(var i=0,j=els.length; i<j; i++)
	if(re.test(els[i].className))a.push(els[i]);
	return a;
}
