
/*
 purpose: helper scripts for form interaction
 date: Feb. 2005
*/

// this function might not be used anymore, delete later
function clearForms(myParent)
{
	if (myParent) {
		// clear all form fields superficially
		clearAllForms(myParent);
	}
}

// clear all input forms given the parent frame object
function clearAllForms(myParent)
{
  // loop through all frames
	if (myParent) {
		for (var i = 0; i < myParent.frames.length; i++) {
			var currentForm = myParent.frames[i].document.forms[0];
			clearInput(currentForm);
		}
	}
}

// clear all input data of the current form, superficially
// Mozilla might give an error saying "formX has no properties",
// which might be due to element indexing that's not fully
// supported? but the result is ok
function clearInput(myForm)
{
  // loop through all text fields of the form in the frame
	if (myForm) {
		for (var j = 0; j < myForm.length; j++) {
			// if element is a textbox
			if ( ( myForm.elements[j].nodeName.toLowerCase() == "input" ) &&
					 ( myForm.elements[j].type.toLowerCase() == "text" ) ) {
				myForm.elements[j].value = ""; // clear the field
			}
			else if( myForm.elements[j].nodeName.toLowerCase() == "textarea" ) {
				myForm.elements[j].value = ""; // clear the textarea
			}
		}
	}
}

function setQueryToURL(myParent, varName, query)
{
	if (myParent) {
		var newLocation = setQueriesInURL(myParent.location.href, varName, query);
		myParent.location.replace(newLocation);
	}
}

function getQueriesFromURL(url)
{
	var parts = url.split("?"); // separate url with trailing query strings
	if (parts.length == 2) {
		return parts[1].split("&"); // separate queries
	}
	else {
		return null;
	}
}

function setQueriesInURL(url, varName, query)
{
	if (query && (query.split("=")).length > 1) { // the new query has value
		var parts = url.split("?"); // separate url with trailing query strings
		var resultURL = parts[0];
		
		var queries = getQueriesFromURL(url);
		resultURL += "?"; // start the query string accumulation
		if (queries && queries.length > 0) { // has queries
			for (var i = 0; i < queries.length; i++) {
				parts = queries[i].split("="); // separate var name and value
				if (parts[0] != varName) { // rebuild url only for other variables
					resultURL += queries[i];
					resultURL += "&";
				}
			}
		}
		// add the new query to the end, if its value is not empty
		parts = query.split("=");
		if (parts && parts.length > 1 && parts[1]) {
			resultURL += query;
		}
		else { // make sure resulting url is as well-formed as possible
			resultURL = resultURL.replace(/\?&/g, "\?");
			resultURL = resultURL.replace(/&$/, "");
			resultURL = resultURL.replace(/\?$/, "");
		}
		return resultURL;
	}
	else {
		return url;
	}
}

// open a resizable helper window, without menu buttons
function helperWindow(target, name, dimension)
{
  window.open(target,name,dimension+',menubar=yes,resizable=yes,scrollbars=yes');
}

/** Jan, 2006 ////////////////
	reference: http://www.webreference.com/programming/javascript/jf/column11/
*/
 function input(formName, obj, val)
 {
	 opener.document.forms[formName].elements[obj].value = val;
	 self.close();
 }
function input(formName, obj, val)
{
	 opener.document.forms[formName].elements[obj].value = val;
	 self.close();
}
function select(formName, obj, idx)
{
	opener.document.forms[formName].elements[obj].selectedIndex = idx;
	self.close();
}
function checkRadio(formName, obj, choice)
{
	opener.document.forms[formName].elements[obj][choice].checked = true;
	self.close();
}
function check(formName, obj, choice)
{
	opener.document.forms[formName].elements[obj].checked = choice;
	self.close();
}
///////

//// FORM VALIDATION ////

function checkUserID(str)
{
	// alphanumeric string between length 4 and 20 inclusive
	if ( str.match(/^[0-9a-z]{4,20}$/i) ) {
		return true;
	}
	else {
		return false;
	}
}

function checkUserPassword(str)
{
	// alphanumeric string between length 4 and 20 inclusive
	if ( str.match(/^[0-9a-z]{4,20}$/i) ) {
		return true;
	}
	else {
		return false;
	}
}

function checkEmail(str)
{
	if ( str.match(/^[a-z0-9][a-z0-9_.-]*@[a-z0-9.-]+\.[a-z]{2,4}$/i) ) {
		return true;
	}
	else {
		return false;
	}
}
