/**************** Global Functions ****************/
function createImage(imagePath){
	var img = new Image();
	img.src = imageDir + imagePath;
	return img;
}

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 getCheckedState(buttonName){
	var button;
	try{
		button = new Element(buttonName).obj.checked;
	}
	catch(e){
		button = null;
	}
	
	return button;
}

function disableField(fieldName){
	var field = new Element(fieldName);
	field.obj.disabled = true;
	field = null;
}

function setReadOnly(fieldName, status){
	var field = new Element(fieldName);
	try{
		field.obj.readOnly = status;
	}
	catch(e){
		//Do Nothing
	}
	field = null;
}

function setBackgroundColor(objectName, color){
	var obj = new Element(objectName);
	obj.changeBackgroundColor(color);
	obj = 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 getFieldType(fieldName, parent){
	parent = (parent == null) ? false : parent;
	var fieldType;
	try{
		fieldType = new Element(fieldName, parent).obj.type;
	}
	catch(e){
		fieldType = null;
	}
	return fieldType;
}

function setFieldValue(fieldName, fieldValue, parent){
	parent = (parent == null) ? false : parent;
	var field = new Element(fieldName, parent);
	try{
		field.obj.value = fieldValue;
	}
	catch(e){
		//Do Nothing
	}
	field = null;
}

function getFieldValue(fieldName, parent){
	parent = (parent == null) ? false : parent;
	return field = new Element(fieldName, parent).obj.value;
}

function getDefaultFieldValue(fieldName, parent){
	parent = (parent == null) ? false : parent;
	var field = new Element(fieldName, parent);
	var result, optionsArray;
	 
	result = null;
	switch(field.obj.type){
		case 'select-one':
			optionsArray = field.obj.options;
			for(var i=0; i < optionsArray.length; i++){
				if(optionsArray[i].defaultSelected){
					return optionsArray[i].value.toString();
				}
			}
			
		case 'radio':
		case 'checkbox':
			result = field.obj.defaultChecked;

		default:
			result = field.obj.defaultValue;
			
	}
	
	field = null;
	return result;
}

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;
}

function protectEmail(domain, user){
	window.location = 'mailto:' + user + '@' + domain;
}


//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;
	}
}

//Derived from: http://www.w3schools.com/jsref/jsref_onkeypress.asp
function FilterForIPAddress(e){
	var keynum;
	var keychar;
	var numcheck;
	
	// IE
	if(window.event){
		keynum = e.keyCode;
	}
	// Netscape/Firefox/Opera
	else if(e.which){
		keynum = e.which;
	}
	
	if(keynum != undefined && keynum != 8){
		keychar = String.fromCharCode(keynum);
		numcheck = /(\d|\.)/;
		return numcheck.test(keychar);
	}
	else{
		return true;
	}
}

function validIPAddress(address){
	// IP Address RegEx pattern attained from: http://www.regular-expressions.info/regexbuddy/ipaccuratecapture.html
	var octetTest = /\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/;
	return octetTest.test(address);
}
	
	
	
	






