//START GENERAL FUNCTIONS
function doesValueExistInList(value, list, separator) {		
	var arrayList = list.split(separator);
	value = trim(value);

	for(var i = 0; i < arrayList.length; i++){
		tempValue = trim(arrayList[i])
		if (tempValue.length > 0) {
			if (value == tempValue) {
				return true;
			} 
		}
    }  
    return false 		
}

function refreshPage() {
	window.location.href = window.location.href;
}

function objectExists(objectName) {
	objectName = (typeof objectName == "string") ? document.getElementById(objectName) : objectName;
	if (objectName) {
		return true;
	} else {
		return false;
	}
}


//center the window
function window_center(p_sWindowHeight,p_sWindowWidth) {
	var l_iwindow_height = p_sWindowHeight;
	var l_iwindow_width = p_sWindowWidth;
	
	// find out how big the screen is
	var l_iheight = window.screen.availHeight;
	var l_iwidth = window.screen.availWidth;

	// get the left position
	var l_ileft_point = parseInt(l_iwidth / 2) - parseInt(l_iwindow_width / 2);

	// get the top position
	var l_itop_point = parseInt(l_iheight/2) - parseInt(l_iwindow_height / 2);

	// now, move the window to center
	window.moveTo(l_ileft_point, l_itop_point);
}

function findPosX(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj) {
	var curtop = 0;
	if (obj.offsetParent)	{
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getWidthOrHeight(widthOrHeight) {
	var x,y;
	if (self.innerHeight) { // all except Explorer
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) { // other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	if (widthOrHeight == 'width') { return x; }
	else { return y; }
}

function getScroll(topOrLeft) {
	var x,y;
	if (self.pageYOffset) { // all except Explorer
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) { // all other Explorers
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	if (topOrLeft == 'top') { return y; }
	else { return x; }
}

function getPageHeight() {
	var x,y;
	var test1 = document.body.scrollHeight;
	var test2 = document.body.offsetHeight
	if (test1 > test2) { // all but Explorer Mac
		x = document.body.scrollWidth;
		y = document.body.scrollHeight;
	}
	else { // Explorer Mac;  //would also work in Explorer 6 Strict, Mozilla and Safari
		x = document.body.offsetWidth;
		y = document.body.offsetHeight;
	}
}


function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}	

function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function resetCSS(cssPath) {
	//cssPath = parent.document.styleSheets[0].href;	
	parent.document.styleSheets[0].href = cssPath;
}

function quickSort(tableName, selectedColumn, sortAsc, left, right) {
	//quickSort('tableVisitsRequests', orderBy, sortAsc, 2, document.getElementById('tableVisitsRequests').rows.length - 1);
	var pivotIndex;
	var table = document.getElementById(tableName);

	//The sort is finished when p_iLeft is greater than or equal to p_iRight
	if (left < right) {

		//Choose a random pivot, to minimize the likelihood that the worst case scenario occurs
		pivotIndex = parseInt((right - left) * Math.random()) + left;

		//partition the table with the given pivot
		pivotIndex = partition(table, selectedColumn, sortAsc, left, right, pivotIndex);

		//sort left of the pivot
		quickSort(tableName, selectedColumn, sortAsc, left, pivotIndex - 1);

		//sort right of the pivot
		quickSort(tableName, selectedColumn, sortAsc, pivotIndex + 1, right);	
	}
}

//Partition returns the index of the final location of the pivot element.
function partition(table, selectedColumn, sortAsc, left, right, pivotIndex) {

	var pivotValue, rowIndex, storeIndex, curCellValue;

	//store the pivot value	
	pivotValue = table.rows(pivotIndex).getAttribute(selectedColumn);
	//swap the pivot value with the right most element to get the pivot out of the way
	table.rows[pivotIndex].swapNode(table.rows[right]);

	storeIndex = left;

	//Loop from left to right - 1, since the pivot value is stored in the right position
	for(var rowIndex = left; rowIndex < right; rowIndex++){
		curCellValue = table.rows(rowIndex).getAttribute(selectedColumn);
		if (((curCellValue < pivotValue) && sortAsc) || ((curCellValue > pivotValue) && sortAsc == false)) {
			table.rows[storeIndex].swapNode(table.rows[rowIndex]);
			storeIndex += 1;
		}
	}

	//swap the pivot with the element in the l_iStoreIndex position.
	//This puts the pivot in the correct place.
	table.rows[storeIndex].swapNode(table.rows[right]);

	//return the correct location of the pivot row
	return storeIndex;
}

//select a single option value for an html select box.
	function selectOptionInSelectBox(selectID, value) {
		var selectBox = document.getElementById(selectID);
		
		for(var i = 0; i < selectBox.length; i++){
			tempValue = selectBox.options[i].value;
			if (tempValue.length > 0) {
				if (tempValue == value) {
					document.getElementById(selectID).options[i].selected = true;
					return;
				} 
			}
		}  	
	}
	
	function sort2d(arrayName, element, num, cs) {
		if (num) {
			for (var i=0; i<(arrayName.length-1); i++) {
				for (var j=i+1; j<arrayName.length; j++) {
					if (parseInt(arrayName[j][element],10) < parseInt(arrayName[i][element],10)) {
						var dummy = arrayName[i];
						arrayName[i] = arrayName[j];
						arrayName[j] = dummy;
					}
				}
			}
		} else {
			for (var i=0; i<(arrayName.length-1); i++) {
				for (var j=i+1; j<arrayName.length; j++) {
					if (cs) {
						if (arrayName[j][element].toLowerCase() < arrayName[i][element].toLowerCase()) {
							var dummy = arrayName[i];
							arrayName[i] = arrayName[j];
							arrayName[j] = dummy;
						}
					} else {
						if (arrayName[j][element] < arrayName[i][element]) {
							var dummy = arrayName[i];
							arrayName[i] = arrayName[j];
							arrayName[j] = dummy;
						}
					}
				}
			}
		}
	}
	
	/* sort the list!
	by = 0 - order by text (default)
	by = 1 - order by value
	by = 2 - order by color
	by = 3 - order by backgroundcolor
	by = 4 - order by classname
	by = 5 - order by id
	num = if true sorts numbers e.g. 2 before 10
	cs = casesensitive e.g. a before Z*/
	function sortSelect(obj, by, num, cs) { 
		obj = (typeof obj == "string") ? document.getElementById(obj) : obj;
		by = (parseInt("0" + by) > 5) ? 0 : parseInt("0" + by);
		if (obj.tagName.toLowerCase() != "select" && obj.length < 2)
			return false;
		var elements = new Array();
		for (var i=0; i<obj.length; i++) {
			elements[elements.length] = new Array((document.body.innerHTML ? obj[i].innerHTML : obj[i].text), obj[i].value, (obj[i].currentStyle ? obj[i].currentStyle.color : obj[i].style.color), (obj[i].currentStyle ? obj[i].currentStyle.backgroundColor : obj[i].style.backgroundColor), obj[i].className, obj[i].id, obj[i].selected);
		}
		sort2d(elements, by, num, cs);
		for (i = 0 ; i < obj.length; i++) {
			if (document.body.innerHTML) obj[i].innerHTML = elements[i][0];
			else obj[i].text = elements[i][0];
			obj[i].value = elements[i][1];
			obj[i].style.color = elements[i][2];
			obj[i].style.backgroundColor = elements[i][3];
			obj[i].className = elements[i][4];
			obj[i].id = elements[i][5];
			obj[i].selected = elements[i][6];
		}
	}
	
	function getSelectedTextFromHTMLSelect(objectID) {
		var list = '';
		objectID = (typeof objectID == "string") ? document.getElementById(objectID) : objectID;
	
		for (var i = 0; i < objectID.options.length; i++) {
			if (objectID.options[i].selected) {
				list += objectID.options[i].text + ',';
			}
		}
		
		return removeEndChar(list,',');
	}
	
	function getSelectedValuesFromHTMLSelect(objectID) {
		var list = '';
		objectID = (typeof objectID == "string") ? document.getElementById(objectID) : objectID;
	
		for (var i = 0; i < objectID.options.length; i++) {
			if (objectID.options[i].selected) {
				list += objectID.options[i].value + ',';
			}
		}
		
		return removeEndChar(list,',');
	}
	
	function getAllValuesFromHTMLSelect(objectID) {
		var list = '';
		objectID = (typeof objectID == "string") ? document.getElementById(objectID) : objectID;
	
		for (var i = 0; i < objectID.options.length; i++) {
			list += objectID.options[i].value + ',';
		}
		
		return removeEndChar(list,',');
	}
	function setHTMLMultiSelect(objectID, valueList) {
		var value = '';
		valueList = valueList.split(',');
		for (var i = 0; i < valueList.length; i++) {
			value = trim(valueList[i]);
			if (value.length > 0) {	setHTMLSelect(objectID, value);	}
		}	
	}
	
	function setHTMLSelect(objectID, value) {

		objectID = (typeof objectID == "string") ? document.getElementById(objectID) : objectID;
	
		for (var i = 0; i < objectID.options.length; i++) {
			if (objectID.options[i].value == value) { objectID.options[i].selected = true; }
			if (objectID.options[i].text == value) { objectID.options[i].selected = true; }
		}
	}
	
	function clearHTMLMultiSelect(objectID) {
		objectID = (typeof objectID == "string") ? document.getElementById(objectID) : objectID;
		for (var i = 0; i < objectID.options.length; i++) {
			objectID.options[i].selected = false; 
		}
	}
	
	function getTrueFalseFromRadioObject(objectID) {
		if (document.getElementById(objectID).checked) { return '1'; }
		else { return '0'; }
	}
	
	function setTextBox(id, value) {
		document.getElementById(id).value = trim(value);
	}
	
	function setRadioTrue(id) {
		document.getElementById(id).checked = true;
	}
	
	function getElementsByName_iefix(tag, name) {
 
		 var elem = document.getElementsByTagName(tag);
		 var arr = new Array();
		 for(i = 0,iarr = 0; i < elem.length; i++) {
		      att = elem[i].getAttribute("name");
		      if(att == name) {
		           arr[iarr] = elem[i];
		           iarr++;
		      }
		 }
		 return arr;
	}
	
	function getValuesFromCheckboxesByName(name) {
		var rows = document.getElementsByName(name); var returnList = '';
		if (rows.length == 0) { rows = getElementsByName_iefix('input',name); }
		for (var i = 0; i < rows.length; i++) {
			if (rows[i].checked) { returnList = returnList + rows[i].value + ','; }
		}	
		return removeEndChar(returnList,',');
	}
	
	function transferOptions(selectIDSource, selectIDDest, moveAllOptions) {
		var sourceSelect = document.getElementById(selectIDSource);
		var destSelect = document.getElementById(selectIDDest);
		if (moveAllOptions) { 
			for (i = sourceSelect.length - 1; i>=0; i--) { sourceSelect.options[i].selected = true; }
		}
		
		for (i = sourceSelect.length - 1; i>=0; i--) {				
	    if (sourceSelect.options[i].selected) {		    	
				addOptionToSelect(selectIDDest, sourceSelect.options[i].text, sourceSelect.options[i].value, 0);		    	
	      sourceSelect.remove(i);
	    }
	  }		
	  
	  sortSelect(destSelect);
	}
	
	function hasOptions(obj){
		if(obj != null && obj.options != null) { return true;}
		return false;
	}
	
	function sortSelect(obj){
		var o = new Array();
		if(!hasOptions(obj)){ return; }
		
		for(var i=0;i<obj.options.length;i++){
			o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected);
		}
		
		if(o.length==0) { return; }
		o = o.sort(function(a,b) 	{
										if((a.text+"") < (b.text+"")) { return -1; }
										if((a.text+"") > (b.text+"")) { return 1; }
										return 0;
									}
							);
		
		for(var i=0;i<o.length;i++){
			obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	}
	
	function addOptionToSelect(selectID, text, value, index) {
		var select = document.getElementById(selectID);
		var selOption = document.createElement('option');    
		if (index.length == 0) { index = 0; }
    
	    selOption.text = text;
	    selOption.value = value;
	    try { select.add(selOption, select.options[index]); } // standards compliant; doesn't work in IE 
	    catch(ex) { select.add(selOption, index); } // IE only 			  
	}
	
	function removeSelectedOptions(selectID) {
		var select = document.getElementById(selectID);
		for (var i = select.length - 1; i>=0; i--) {
			if (select.options[i].selected) { select.remove(i); }
		}
	}
	
	function convertBool(value) {
		if (isNumeric(value)) {
			if (value == 1) { return true; }
			else { return false; }
		} else {			
			if (value == true) { return 1; }
			else { return 0; }
		}
	}		
	

//END GENERAL FUNCTIONS

//************************************************************************************************************************************

//START NUMERIC FUNCTIONS
function round(p_nNum,p_iRoundTo){
	var digits = Math.pow(10, p_iRoundTo);
	return Math.round(p_nNum * digits) / digits;
}

function isNumeric(value) {
	var valid = "0123456789.-";
	var ok = true;
	var temp;
	value =	String(value);
	if (value.length == 0) return false;

	for (var i = 0; i < value.length; i++) {
		temp = "" + value.substring(i, i + 1);
		if (valid.indexOf(temp) == "-1") { ok = false; }
	}
	
	if (ok == false) { return false; }
	else { return true; }
}


function validateFieldNumeric(field) {
	var valid = "0123456789";
	var ok;
	
	ok = isNumeric (field.value);
	
	if (ok == false) {
		alert("Please supply a valid quantity.");
		field.focus();
		field.value = ""
		field.select();
	}
}

function ccurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) { num = "0"; }
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) { cents = "0" + cents; }
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
		num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
	}
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}
//END NUMERIC FUNCTIONS

//************************************************************************************************************************************

//START STRING FUNCTIONS

function replace(value, find, replaceWith) {
	return String(value).replace(find,replaceWith);
}

function trim(p_sStr){
	return String(p_sStr).replace(/^\s*|\s*$/g,'');
} 

function removeEndChar(value, charToRemove) {
	var tempValue = trim(value);
	var charToRemoveLength = charToRemove.length;
	
	if ((tempValue.length > 0) && (right(tempValue, charToRemoveLength) == charToRemove)) {
		tempValue = left(tempValue, tempValue.length - charToRemoveLength);
		tempValue = removeEndChar(tempValue, charToRemove); //this will strip off all chars at the end.
	}
	
	return tempValue;
}

function instr(p_iPosition,p_sStr1,p_sStr2){
	p_iPosition = p_iPosition -1;
	p_sStr1 = String(p_sStr1).substring(p_iPosition, p_sStr1.length);
	return parseInt(p_sStr1.indexOf(p_sStr2) + 1);
}

function left(p_sStr, p_iLen){
	//p_iLen = p_iLen + 1;
	if (p_iLen <= 0)
	    return '';
	else if (p_iLen > String(p_sStr).length)
	    return p_sStr;
	else
	    return String(p_sStr).substring(0,p_iLen);
}

function right(p_sStr, p_iLen){
    if (p_iLen <= 0)
       return '';
    else if (p_iLen > String(p_sStr).length)
       return p_sStr;
    else {
       var l_iLen = String(p_sStr).length;
       return String(p_sStr).substring(l_iLen, l_iLen - p_iLen);
    }
}

function len(p_sStr){
	return parseInt(String(p_sStr).length);
}

function isString(value) {
	var valIsString = true;
	
	if (value.length == 0) {
		valIsString = false;
	}
	
	if (isNumeric(value)) {
		valIsString = false;
	}
	
	return valIsString;
}

function getLetterAndNumberCount(value) {
	var validCharacter = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
	var ok = true; var temp = ''; var count = 0;
	
	if (value.length == 0) return false;
	for (var i=0; i < value.length; i++) {
		temp = value.substring(i, i + 1);
		if (validCharacter.indexOf(temp) >= 0) { count = count + 1; }
	}
	
	return count;
}
//END STRING FUNCTIONS

//************************************************************************************************************************************

//START DATE/TIME FUNCTIONS

function cDate(p_sVal){
	if (isDate(p_sVal)) {
		return new Date(p_sVal);
	} else {
		return p_sVal;
	}
}

function getDate(pDate) {
	var newDate = new Date();
	if (isDate(pDate)) {
		newDate = cDate(pDate);
	} else {
		newDate = new Date();
	}
	return (newDate.getMonth() + 1) + '/' + newDate.getDate() + '/' + newDate.getFullYear();
}

function getTime() {
	var AMorPM = '';
	var d = new Date();
	var curr_hour = d.getHours();
	var curr_min = d.getMinutes();

	if (curr_hour < 12)	{
		AMorPM = "AM";
	} else {
		AMorPM = "PM";
	}

	if (curr_hour == 0) { curr_hour = 12; }

	if (curr_hour > 12) { curr_hour = curr_hour - 12; }

	if (curr_min < 10) { curr_min = '0' + curr_min;	}

	return curr_hour + ':' + curr_min + ' ' + AMorPM;
}

function isDate(p_sDate){
	try {
		var date = new Date(p_sDate);
		return !isNaN(date) && !date != null;
	} catch(ex) {
		return false;
	}
}

function isTime(p_sTime){
	var l_Pattern = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	var l_aMatches = p_sTime.match(l_Pattern);
	
	if (l_aMatches == null) {
		return false;
	}
	
	hour = l_aMatches[1];
	minute = l_aMatches[2];
	second = l_aMatches[4];
	ampm = l_aMatches[6];

	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }

	if (hour < 0  || hour > 23) {
		return false;
	}

	if  (hour > 12 && ampm != null) {
		return false;
	}

	if (minute<0 || minute > 59) {
		return false;
	}
	if (second != null && (second < 0 || second > 59)) {
		return false;
	}
	return true;
}

function hourDiff(p_sDate1,p_sDate2){
	var l_dDate1,l_dDate2;
	l_dDate1 = new Date(p_sDate1);
	l_dDate2 = new Date(p_sDate2);

	var numDays = parseInt(l_dDate2.getDate() - l_dDate1.getDate());
	var numHours = parseInt(l_dDate2.getHours() - l_dDate1.getHours());

	return numHours + (numDays * 24);
}

function dateDiff(p_sInterval,p_sDate1,p_sDate2){
	var l_dDate1,l_dDate2,total, diff;
	
	l_dDate1 = new Date(p_sDate1);
	l_dDate2 = new Date(p_sDate2);
	
	diff = (l_dDate2 - l_dDate1);

	switch(p_sInterval.toUpperCase()){
		case 'YYYY':{	//Year 
			return parseInt(l_dDate2.getFullYear() - l_dDate1.getFullYear());
			break;
		}
		case 'Q':{		//Quarter 
			//not implemented
			break;
		}
		case 'M':{		//Month 
			return parseInt((l_dDate2.getMonth()+1) - (l_dDate1.getMonth()+1));
			break;
		}
		case 'Y':{		//Day Of Year 
			//not implemented
			break;
		}
		case 'D':{		//Day 
			return parseInt(l_dDate2.getDate() - l_dDate1.getDate());
			break;
		}
		case 'W':{		//WeekDay 
			//not implemented
			break;
		}
		case 'WW':{		//Week Of Year 
			//not implemented
			break;
		}
		case 'H':{		//Hour 		
			diff = (diff/3600000)
			break;
		}
		case 'N':{		//Minute 
			return parseInt(l_dDate2.getMinutes() - l_dDate1.getMinutes());
			break;
		}
		case 'S':{		//Second
			return parseInt(l_dDate2.getSeconds() - l_dDate1.getSeconds()); 
			break;
		}
	}// end of Switch statement
	
	return diff;
}

function dateAdd (interval, num, pDate){

	if (isDate(pDate)) { 	
  		pDate = cDate(pDate);
		switch (interval.toUpperCase()){
		case "MS":
			pDate.setMilliseconds(pDate.getMilliseconds() + num);
			break;
		case "S":
			pDate.setSeconds(pDate.getSeconds() + num);
			break;
		case "N":s
			pDate.setMinutes(pDate.getMinutes() + num);
			break;
		case "H":
			pDate.setHours(pDate.getHours() + num);
			break;
		case "D":
			pDate.setDate(pDate.getDate() + num);
			break;
		case "MO":
			pDate.setMonth(pDate.getMonth() + num);
			break;
		case "Y":
			pDate.setFullYear(pDate.getFullYear() + num);
			break;
		}
  	}
  return pDate;
}

//END DATE/TIME FUNCTIONS

//************************************************************************************************************************************

//START AJAX FUNCTIONS
//a = query string
//b = return function
//c = XML or responsetext
//d = POST OR GET
function sendAJAX(a,b,c,d) {
	var e = createAJAXObject();
	if(!d) d=null;

	e.open(d?"POST":"GET",a,true);
	if(b) {
		e.onreadystatechange=function() {
				if(e.readyState==4) {
					b(c&&e.responseXML?e.responseXML:e.responseText)
				}
		}
	}
	e.send(d)
}

function createAJAXObject() {
	var a = null;
	if(window.ActiveXObject){
	    	a = new ActiveXObject("Msxml2.XMLHTTP");
	    	if(!a) {
		    	a = new ActiveXObject("Microsoft.XMLHTTP");
	    	}
	} else if(window.XMLHttpRequest) {
	    	a = new XMLHttpRequest;
	}
	
	return a;
}

function contextHelp(object, helptext){
	object.style.cursor = 'help';
	object.setAttribute('title', helptext);
}

//END AJAX FUNCTIONS

function validateField(fieldID, required, requiredmessage, dataType, dataTypeMessage, expectedLength, lengthMessage) {
	var field = document.getElementById(fieldID);
	var wrongDataType = false; var passed = true;
	if (expectedLength == undefined) { expectedLength = ''; lengthMessage = ''; }
	
	if (field.value.length == 0 && required) { alert(requiredmessage); field.focus(); passed = false; }
	else if (field.value.length > 0) {
		
		if (dataType == 'numeric') {
			if (!isNumeric(field.value)) { wrongDataType = true; } 
		} else if(dataType == 'date') {
			if (!isDate(field.value)) { wrongDataType = true; } 
		} else if(dataType == 'email') {
			if (!isEmailAddress(field.value)) { wrongDataType = true; } 
		} 
		
		if (wrongDataType) { alert(dataTypeMessage); field.focus(); passed = false; }

		if (passed && expectedLength.length > 0) {			
			if (field.value.length != expectedLength) { alert(lengthMessage); passed = false; } 
		}			
	}

	return passed;	
}

function isEmailAddress(emailAddress) {

	var at="@";
	var dot=".";
	var lat = emailAddress.indexOf(at);
	var lstr = emailAddress.length;
	var ldot = emailAddress.indexOf(dot);
		
	if (emailAddress.indexOf(at) == -1){ return false; }
	if (emailAddress.indexOf(at) == -1 || emailAddress.indexOf(at)==0 || emailAddress.indexOf(at)==lstr){ return false; }
	if (emailAddress.indexOf(dot) == -1 || emailAddress.indexOf(dot)==0 || emailAddress.indexOf(dot)==lstr){ return false; }
	if (emailAddress.indexOf(at,(lat+1)) != -1){ return false; }
	if (emailAddress.substring(lat-1,lat) == dot || emailAddress.substring(lat+1,lat+2)==dot){ return false; }
	if (emailAddress.indexOf(dot,(lat+2)) == -1){ return false; }			
	if (emailAddress.indexOf(" ") != -1){ return false; }
	return true;
}

function deleteCookie(name) {
	var cookie_date = new Date ();  // current date & time
	cookie_date.setTime(cookie_date.getTime() - 1);
	document.cookie = name += "=; expires=" + cookie_date.toGMTString();
}

function setCookie(cookieName, cookieValue, nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString();
}

// this function gets the cookie, if it exists
function getCookie(name) {	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length))) {
		return '';
	}
	if ( start == -1 ) return '';
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
