/* This function is used to submit a form with the given action url */
function submitAction(form, action) {
	form.action = action;
	form.submit();
}

/* This function is used to open a pop-up window */
function openWindow(url, winTitle, winParams) {
	winName = window.open(url, winTitle, winParams);
	winName.focus();
}

/* This function is used to prompt confirmation message */
function showConfirmation(msg) {
	var ans = confirm(msg);
	if (ans) {
		return true;
	} else {
		return false;
	}
}

/* This function will return the given object's position after adding a top and left offset */
function findPos(obj, offsetTop, offsetLeft) {
	var curleft = 0;
	var curtop = 0;
	if (!offsetTop) { offsetTop = 0;	}
	if (!offsetLeft) { offsetLeft = 0;	}
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj === obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [ curleft + offsetLeft, curtop + offsetTop ];
}

/* This function is used to check for undefined element */
function isUndefined(value) {
    var undef;
    return value == undef;
}

/* This function is used to check all the checkboxes in the list */
function checkAll(theForm) {
	var e;
	for (var i=0;i<theForm.elements.length;i++) {
	    e = theForm.elements[i];
		eName = e.name;
		if (eName != 'allbox' && (e.type.indexOf("checkbox") === 0)) {
			if (!e.disabled) {
				e.checked = theForm.allbox.checked;
			}
		}
	}
}

/* This function is used to clear a form of all it's values */
function clearForm(frmObj) {
	var element, j;
	for (var i = 0; i < frmObj.length; i++) {
		element = frmObj.elements[i];
		if (isUndefined(element.type)) {
			continue;
		}
		if (element.type.indexOf("text") === 0 || element.type.indexOf("password") === 0) {
			element.value="";
		} else if (element.type.indexOf("radio") === 0) {
			element.checked=false;
		} else if (element.type.indexOf("checkbox") === 0) {
			element.checked = false;
		} else if (element.type.indexOf("select") === 0) {
			for(j = 0; j < element.length ; j++) {
				element.options[j].selected=false;
			}
			element.options[0].selected=true;
		}
	}
}

// This function is for stripping leading and trailing spaces
function trim(str) {
	var i;
	if (str !== null) {
		for (i=0; i<str.length; i++) {
			if (str.charAt(i)!=" ") {
				str=str.substring(i,str.length);
				break;
			}
		}
		for (i=str.length-1; i>=0; i--) {
			if (str.charAt(i)!=" ") {
				str=str.substring(0,i+1);
				break;
			}
		}
		if (str.charAt(0)==" ") {
			return "";
		} else {
			return str;
		}
	}
}

// This function is used to perform client-side simple form validation.
function validateRequired(form) {
	var bValid = true;
	var focusField = null;
	var i = 0;
	var fields = [];
	var oRequired = new Required();
	for (x in oRequired) {
		if ((form[oRequired[x][0]].type === 'text' || form[oRequired[x][0]].type === 'textarea' || form[oRequired[x][0]].type === 'select-one' ||
			 form[oRequired[x][0]].type === 'radio' || form[oRequired[x][0]].type === 'password') && form[oRequired[x][0]].value === '') {
			if (i === 0) {
				focusField = form[oRequired[x][0]];
			}
			fields[i++] = oRequired[x][1];
			bValid = false;
		}
	}
	if (fields.length > 0) {
		focusField.focus();
		alert(fields.join('\n'));
	}
	return bValid;
}

// This function is a generic function to create form elements
function createFormElement(element, type, name, id, value, parent) {
	var e = document.createElement(element);
	e.setAttribute("name", name);
	e.setAttribute("type", type);
	e.setAttribute("id", id);
	e.setAttribute("value", value);
	parent.appendChild(e);
}

// This function is used to highlight the table's row on mouse over.
function highlightTableRows(tableId) {
	var previousClass = null;
	var table = document.getElementById(tableId);
	var tbody = table.getElementsByTagName("tbody")[0];
	var rows;
	if (tbody === null) {
		rows = table.getElementsByTagName("tr");
	} else {
		rows = tbody.getElementsByTagName("tr");
	}
	// add event handlers so rows light up and are clickable
	for (i=0; i < rows.length; i++) {
		rows[i].onmouseover = function() { previousClass=this.className;this.className+=' over'; };
		rows[i].onmouseout = function() { this.className=previousClass; };
	}
}

// This function is used to highlight form's element on mouse over.
function highlightFormElements() {
	// add input box highlighting
	addFocusHandlers(document.getElementsByTagName("input"));
	addFocusHandlers(document.getElementsByTagName("textarea"));
}

// This function is used to add a focus handler on the given element.
function addFocusHandlers(elements) {
	for (i=0; i < elements.length; i++) {
		if (elements[i].type != "button" && elements[i].type != "submit" && elements[i].type != "reset" && elements[i].type != "checkbox" && elements[i].type != "radio") {
			if (!elements[i].getAttribute('readonly') && !elements[i].getAttribute('disabled')) {
				elements[i].onfocus=function() {this.style.backgroundColor='#ffeebf';this.select();};
				elements[i].onmouseover=function() {this.style.backgroundColor='#ffeebf';};
				elements[i].onblur=function() {this.style.backgroundColor='';};
				elements[i].onmouseout=function() {this.style.backgroundColor='';};
			}
		}
	}
}

// This function is used to handle the error/success message display.
function handleMessageDisplay() {
	if ($('successMessages')) {
		//new Effect.Highlight('successMessages');
		window.setTimeout("Effect.DropOut('successMessages')", 10000);
	}
	if ($('errorMessages')) {
		// new Effect.Highlight('errorMessages');
		window.setTimeout("Effect.DropOut('errorMessages')", 10000);
	}
}

// Window default onload routine.
window.onload = function() {
	highlightFormElements();
	//handleMessageDisplay();
};

// Show the document's title on the status bar
window.defaultStatus=document.title;

//do not allow back button
//window.history.forward(1);

