//General Functions List
function createImage(imagePath){
	var img = new Image();
	img.src = imagePath;
	return img;
}

function rollOn(imageid, imagename){
	if(getFieldValue('ignoreImage').toLowerCase() != imageid.toLowerCase()){
		var img = new Element(imagename);
		img.obj.src = eval(imageid + "On.src");
		img = null;
	}
}

function rollOff(imageid, imagename){
	if(getFieldValue('ignoreImage').toLowerCase() != imageid.toLowerCase()){
		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 limitText(limitField, limitNum) {
	var field = new Element(limitField);
	if(field.obj.value.length > limitNum){
		field.obj.value = field.obj.value.substring(0, limitNum);
	}
}

function isNew(fieldName, newWindow){
	var formField = new Element(fieldName);
	
	if(formField.obj.value == "new"){
		eval(newWindow);
	}
}

function erase(theBox){
	var e = new Element(theBox);
	e.obj.value = "";
	e = null;
}


//JSDebugger Specific Functions
function showJSDebug(){
	showDiv("JSDebugOff");
	showDiv("JSDebug");
	hideDiv("JSDebugOn");
}

function hideJSDebug(){
	showDiv("JSDebugOn");
	hideDiv("JSDebugOff");
	hideDiv("JSDebug");
}

function debugWindow(){
	document.write("<table style='border: 1px solid black; font: 10pt Arial,sans-serif; background: #FFFFFF;' cellpadding='3' cellspacing='0'>");
	document.write("<tr><td style='background: #336699; color: #FFFFFF; font-size: 12pt; font-weight: bold;'>JavaScript Active Debug Window</td></tr>");
	document.write("<tr><td><textarea name='code' id='code' rows='5' cols='50' style=\"font: normal 8pt 'Courier New',courier,monospace;\"></textarea></td></tr>");
	document.write("<tr><td>");
	document.write("<input type='button' onClick=\"runCode();\" value='Run Code!' style='font: bold 8pt arial,sans-serif;'>");
	document.write("<input type='button' onClick=\"clearCode();\" value='Clear' style='font: bold 8pt arial,sans-serif; margin-left: 10px;'>");
	document.write("<input type='button' onClick=\"resizeCodeWindow(5,50);\" value='5x50' style='font: bold 8pt arial,sans-serif; margin-left: 10px;'>");
	document.write("<input type='button' onClick=\"resizeCodeWindow(20,100);\" value='20x100' style='font: bold 8pt arial,sans-serif; margin-left: 10px;'>");
	document.write("</td></tr>");
	document.write("</table>");
}

function runCode(){
	var e = new Element("code");
	if(e.obj.value.indexOf("runCode()") == -1){
		try{
			eval(e.obj.value);
		}
		catch(error){
			alert(error);
		}
	}
	else{
	  alert("Calling the function \"runCode()\" will result in an infinate loop.");
	  e.obj.focus();
	}
	e = null;
}

function clearCode(){
	var e = new Element("code");
	e.obj.value = "";
	e = null;
}

function resizeCodeWindow(rows,cols){
	var e = new Element("code");
	e.obj.rows = rows;
	e.obj.cols = cols;
	e = 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;
	}
}

function setFormAction(formName, formAction, parent){
	parent = (parent == null) ? false : parent;
	var form = new Element(formName, parent);
	form.obj.action = formAction;
	form = null;
}


function finishOperation(currentWindow, message){
	alert(message);
	currentWindow.opener.focus();
	currentWindow.close();
}


function validEmail(email){
	var atSymbol = email.indexOf('@');
	var dot = email.indexOf('.', atSymbol);

	return (atSymbol > 0 && dot > 0) ? true : false;
}


