/**************** Global Functions ****************/
function createImage(imagePath){
	var img = new Image();
	img.src = imageDir + imagePath;
	return img;
}

function rollon(imageid, imagename){
	var img = new Element(imagename);
	img.obj.src = eval(imageid + "On.src");
	img = null;
}

function rolloff(imageid, imagename){
	var img = new Element(imagename);
	img.obj.src = eval(imageid + "Off.src");
	img = null;
}

function showDiv(divName){
	var div = new Element(divName);
	div.changeDisplay("block");
	div = null;
}

function hideDiv(divName){
	var div = new Element(divName);
	div.changeDisplay("none");
	div = null;
}

function selectButton(buttonName){
	//for use with radio buttons or to set checkboxes
	var button = new Element(buttonName);
	button.obj.checked = true;
	button = null;
}

function deSelectButton(buttonName){
	//for use with radio buttons or to set checkboxes
	var button = new Element(buttonName);
	button.obj.checked = false;
	button = null;
}

function toggleButton(buttonName){
	//for use with checkboxes
	var button = new Element(buttonName);
	button.obj.checked = !button.obj.checked;
	button = null;
}

function disableField(fieldName){
	var field = new Element(fieldName);
	field.obj.disabled = true;
	field = null;
}

function enableField(fieldName){
	var field = new Element(fieldName);
	field.obj.disabled = false;
	field = null;
}

function submitForm(formName){
	var form = new Element(formName);
	form.obj.submit();
	form = null;
}

function submitParentForm(strFormName){
	var parentForm = new Element(strFormName, true);
	
	parentForm.obj.submit();
	parentForm = null;
	
	window.close();
}

function properCase(strText){
	var firstChar = strText.substr(0,1);
	return strText.replace(new RegExp(firstChar), firstChar.toUpperCase());
}

function LTrim(strText){
	var startAt, strLength;

	startAt = strLength;
	strLength = strText.length;
	
	for(var i=0; i < strLength; i++){
	  if(strText.charAt(i) != " "){
		startAt = i;
		break;
	  }
	}

	strText = strText.substr(startAt);
	return strText;
}

function RTrim(strText){
	var endAt, strLength;
	
	endAt = 0;
	strLength = (strText.length-1);
	
	for(var i=strLength; i >= 0; i--){
		if(strText.charAt(i) != " "){
			endAt = i+1;
			break;
		}
	}
	
	strText = strText.substring(0,endAt);
	return strText;
}

function Trim(strText){
	return LTrim(RTrim(strText));
}

function createDate(strDateValue, format){
	var datePartArray = strDateValue.split("/");
	var result;
	var strDate = "";
	var monthArray = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	format = format.toLowerCase();

	switch(format){
		case 'd/m/y':
			strDate = monthArray[parseInt(datePartArray[1].replace(/^0/,''))-1] + " " + parseInt(datePartArray[0]) + ", " + datePartArray[2];
			break;
		case 'm/d/y':
			strDate = monthArray[parseInt(datePartArray[0].replace(/^0/,''))-1] + " " + parseInt(datePartArray[1]) + ", " + datePartArray[2];
			break;
		case 'y/m/d':
			strDate = monthArray[parseInt(datePartArray[1].replace(/^0/,''))-1] + " " + parseInt(datePartArray[2]) + ", " + datePartArray[0];
			break;
	}

	result = new Date(Date.parse(strDate));
	return result;
}

function clearSelectBox(selectBox){
	var box = new Element(selectBox);
	box.obj.options.length = 0;
	box = null;
}

function setFocus(element){
	var e = new Element(element);
	e.obj.focus();
	e = null;
}

function setFieldValue(fieldName, fieldValue, parent){
	parent = (parent == null) ? false : parent;
	var field = new Element(fieldName, parent);
	field.obj.value = fieldValue;
	field = null;
}

function getFieldValue(fieldName, parent){
	parent = (parent == null) ? false : parent;
	return field = new Element(fieldName, parent).obj.value;
}

function checkedStatus(switchName){
	var switchElement = new Element(switchName).obj.checked;
	return switchElement;
}

function limitText(limitField, limitNum) {
	var field = new Element(limitField);
	if(field.obj.value.length > limitNum){
		field.obj.value = field.obj.value.substring(0, limitNum);
	}
}

function setFormAction(formName, formAction, parent){
	parent = (parent == null) ? false : parent;
	var form = new Element(formName, parent);
	form.obj.action = formAction;
	form = null;
}

//A template function that can be customized to needs
//height & width are setup as variables so it only needs
//to be put in once.
function windowTemplate(url, windowName, width, height, return_obj){
	var windowObject;
	var winWidth = (width == null) ? 200 : width;  //for the sake of example
	var winHeight = (height == null) ? 200 : height;  //for the sake of example
	var xPos = (screen.width / 2) - (winWidth / 2);
	var yPos = (screen.height / 2) - (winHeight / 2);
	
	return_obj = (return_obj == null) ? false : return_obj;
	
	//customize this line to needs
	windowObject = window.open(url , windowName, 'height=' + winHeight + ', width=' + winWidth + ', scrollbars=yes, toolbar=no, status=no, menubar=no, location=no, left=' + xPos + ', top=' + yPos);
	windowObject.focus();
	
	if(return_obj){
		//return a reference to this window object so it can be manipulated outside this function
		return windowObject;
	}
}



