// JavaScript Document

//hierarcial object structure

/*
function WMenu () {

	this.menuName; 
	

}
*/
function Utilities () {
	
	this.sortAjax = sortAjax;
	this.trim = trim;
	this.ltrim = ltrim;
	this.rtrim = rtrim;
	this.getElement = getElement;
	this.regxStringSearch = regxStringSearch;
	this.emptyVal = emptyVal;
	//this.arraySearchKey = arraySearchKey;
	var arraySearchKey = "";
	//this.buildOptionsObj = buildOptionsObj;
	//this.buildOptions = buildOptions;
	
	function sortAjax (array, key) {
		//requires compare function
		arraySearchKey = key;
		//alert(this.arraySearchKey);
		array.sort(compare);
	/*
	for (i = 0; i < array.length; i++) {
		
		var current = array[i][key];
		
		for(j = 1; j < array.length; j++) {
			
			if(compare(array[i], array[j], key)) {
				
				var currentArray =  array[i];
				array[i] = array[j];
				array[j] = currentArray; 
			}
		}
	}
	
	*/
	/*	
	var compare = function compare (a, b) {
	
		return (b.HEALTH_BOARD < a.HEALTH_BOARD) - (a.HEALTH_BOARD < b.HEALTH_BOARD);
	}
  		array.sort(compare);
	*/
		return array;
	}//end function
	
	/*
	function compare (a, b, searchKey) {
	
		return (b[searchKey] < a[searchKey]) - (a[searchKey] < b[searchKey]);
	}
	*/
	
	
	function compare (a, b) {
	
		return (b[arraySearchKey] < a[arraySearchKey]) - (a[arraySearchKey] < b[arraySearchKey]);
	}
	
	function trim(stringToTrim) {
		return stringToTrim.replace(/^\s+|\s+$/g,"");
	}
	
	function ltrim(stringToTrim) {
		
		return stringToTrim.replace(/^\s+/,"");
	}
	
	function rtrim(stringToTrim) {
		
		return stringToTrim.replace(/\s+$/,"");
	}
	
   function getElement(aID){ 
     return (document.getElementById ? document.getElementById(aID) : document.all[aID]);
   } 
   
   function regxStringSearch (text, regx) {
   		//regx example
   		var myString = new String(text);
		var rExp = regx;
		var results = myString.search(rExp);
		
		return results;
   }
   
   function emptyVal (val) {
   
   		if(val == "" || val == " " || val == null) {
   		
   			//alert("val empty: " + val);
   			return 'null';
   		
   		} else {
   		
   			return val;
   		}
   }//end function
  
}//end class

ElementTools.prototype = new Utilities();

function ElementTools () {
	
	this.changeClass = changeClass;
	this.displayElement = displayElement;
	this.hideElement = hideElement;
	this.toggleDisplayElement = toggleDisplayElement;
	this.visibleBlockStyle = visibleBlockStyle;
	this.hideStyle = hideStyle;
	this.displayNone = displayNone;
	this.displayBlock = displayBlock;
	this.changeCursorStyle = changeCursorStyle;
	this.findPos = findPos;
	this.blockPositionByElement = blockPositionByElement;
	
	function changeClass (elementName, className) {
		
		var identity=document.getElementById(elementName);
		identity.className= className;
	}
	
	function displayElement (elementName, className) {
	

		this.changeClass(elementName, className);
	
	}
	
	function hideElement(elementName, className) {
	
		this.changeClass(elementName, className);
	
	}
	
	function displayBlock (elementName) {
	
		var elm = document.getElementById(elementName);
		elm.style.display = "block";
	}
		
	function displayNone (elementName) {
	
		var elm = document.getElementById(elementName);
		elm.style.display = "none";
	}
	
	function changeCursorStyle (elementName, style) {
	
		var elm = document.getElementById(elementName);
		elm.style.cursor = style;
		
	}
	
	function findPos(obj) {
		
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
		return [curleft,curtop];
	}
	
	function visibleBlockStyle (elementName){
		
		var elm = document.getElementById(elementName);
		elm.style.display = "block";
		elm.style.visibility = "visible";
		
		//elm.style.left = op.offsetLeft+"px";
		//elm.style.top = op.offsetTop+"px";
		//alert("OffsetLeft: " + pos[0]);
		//alert("OffsetLeft: " + op.offsetLeft);
		//alert("OffsetTop: " + op.offsetTop);
	
	}
	
	function blockPositionByElement (primaryElementName, positionElementName) {
	
		var elm = document.getElementById(primaryElementName);
		var op = document.getElementById(positionElementName);
		this.visibleBlockStyle(primaryElementName);
		
		var pos = this.findPos(op);
		elm.style.left = pos[0]+"px";
		elm.style.top = pos[1]+"px";
		
		//alert("OffsetLeft: " + pos[0]);
		//alert("OffsetTop: " + pos[1]);
		
	
		
	}
	
	function hideStyle (elementName) {
		
		var elm = document.getElementById(elementName);
		elm.style.display = "none";
		elm.style.visibility = "hidden";
	}
	function toggleDisplayElement (elementName, className) {
	
	
		var toggleBoolean = document.getElementById(elementName+"Toggle");
		
		//alert(toggleBoolean.value);
		 	
		//if the element is current on display
		if(toggleBoolean.value == "false") {
		
			this.changeClass(elementName, className);
			toggleBoolean.value = "true";
			
		} else if(toggleBoolean.value == "true") {
		
			this.changeClass(elementName, "hide");
			toggleBoolean.value = "false";
			
		}
		
		//alert(toggleBoolean.value);
	}

}

function OptionTools () {

	this.selectOption = selectOption;
	this.resetOption = resetOption;
	
	function selectOption (optionElement, selectValue) {
	
		var elm = document.getElementById(optionElement);
		var len = elm.options.length;
		
		for (var i = 0; i < len; i++) {
		
			if(elm.options[i].value ==  selectValue){
			
				elm.options[i].selected = true;
				//alert("Selected : " + selectValue);
			}
			
		}
		
	}//end function
	
	function resetOption (optionElement) {
		
		var elm = document.getElementById(optionElement);
		elm.options[0].selected = true;
	
	} 

}//end calss

CookieMonster.prototype = new Utilities();

function CookieMonster () {
	
	this.setCookie = setCookie;
	this.getCookie = getCookie;
	
	function setCookie(c_name,value,mins)
	{
	var exdate=new Date();
	exdate.setDate(exdate.getMinutes()+mins);
	document.cookie=c_name+ "=" +escape(value)+((mins==null) ? "" : ";expires="+exdate.toGMTString());
	}
	
	function getCookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results )
    return ( unescape ( results[1] ) );
  else
    return null;
}
	
	/*
	function getCookie(c_name)
	{
		//alert("Cookies length: " + document.cookie.length);
	if (document.cookie.length>0)
	  {
	  
	  var c_start = document.cookie.indexOf(c_name + "=");
	  
	  if (c_start!=-1)
	    { 
	   
	    c_start = c_start + c_name.length+1; 
	    
	    var c_end = document.cookie.indexOf(";",c_start); 
	    
	    if (c_end==-1) { 
	    	
	    	c_end=document.cookie.length;
	    	
	    return unescape(document.cookie.substring(c_start,c_end));
	    
	    } 
	  }
	
	return ""
	}
	}
	*/
}//end class

function DocumentUtilities () {
	
	
}
	
DocumentUtilities.prototype = new Utilities();

function ModuleTools () {
	
	this.newWindow = null;
	
	this.openResourceWindow = openResourceWindow;
	this.openResultWindow = openResultWindow;
	this.openResourceWindowAndRedirect = openResourceWindowAndRedirect;
	
	function openResourceWindow (url) {
		
		newwindow=window.open(url,'name','height=768,width=1024,left=100,top=100,resizable=yes,scrollbars=yes,status=yes');
		//window.location="http://www.wemerec.org/client_elearning.php";
		if (window.focus) {newwindow.focus()}
	}
	
	function openResourceWindowAndRedirect (resourceLocation, redirectLocation) {
	
		newwindow=window.open(resourceLocation,'name','height=768,width=1024,left=100,top=100,resizable=yes,scrollbars=yes,status=yes');
		window.location = redirectLocation;
		if (window.focus) {newwindow.focus()}
	}
	
	function openResultWindow (url) {
		
		newwindow=window.open(url,'name','height=768,width=1024,left=100,top=100,resizable=yes,scrollbars=yes,status=yes,menubar=yes');
		
	}
}

ModuleTools.prototype = new DocumentUtilities();

function CertificateTools () {
	
	
	this.openCertWindow = openCertWindow;
	
	function openCertWindow (url) {
		
		newwindow=window.open(url,'name','height=768,width=1024,left=100,top=100,resizable=yes,scrollbars=yes,status=yes');
	}
	
} 

CertificateTools.prototype = new DocumentUtilities();

function VisualUtilities () {

	this.displayElement = displayElement;
	this.hideElement = hideElement;
	this.collapseElement = collapseElement;
	this.customDisplayElement = customDisplayElement;
	
	
	this.util = new Utilities();
	
	function displayElement (elementName) {
	
		var elm = util.getElement(elementName);
		elm.style.visibility = "visible";
			
	}
	
	function hideElement (elementName) {
	
		var elm = util.getElement(elementName);
		elm.style.visibility = "hidden";
	}
	
	function collapseElement (elementName) {
	
		var elm = util.getElement(elementName);
		elm.style.visibility = "collapse";
	} 
	
	function customDisplayElement (elementName, className) {
	
		//document.getElementById(element).style.visibility = "visible";
		document.getElementById(elementName).style.visibility = "visible";
	}
}

VisualUtilities.prototype = new Utilities();

