// GLOBAL VARIABLES //

// RegExps used in validation
var regexForename = /^\w{2,30}$/;
var regexSurname = /^\w{2,30}$/;
var regexPassword = /^\S{8,20}$/;
var regexEmail = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
var regexPostcode = /^(?:[A-PR-UWYZ][0-9][0-9A-HJKS-UW]?|[A-PR-UWYZ][A-HK-Y][0-9][0-9ABEHMNPRV-Y]?)\s{0,2}[0-9][ABD-HJLNP-UW-Z][ABD-HJLNP-UW-Z]|GIR\s{0,2}0AA$/;  // from Java - not tested
var regexPhone = /^0\d{10}$/;



/**
 * clear default value from text or password input
 */
function clearDefault(ipt) {
	if ((typeof ipt != 'object') || (ipt.type != 'text'  && ipt.type != 'password')) {
		// alert(ipt.type);
		return;
	}
	if (ipt.value == ipt.defaultValue) {
		ipt.value = "";
	};
}


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

//THIS FUNCTION PRE-SELECTS AN OPTION OF SELECT ELEMENT 'e' THAT EQUALS VALUE 'v'
function selectOption(e, v) {
	// NOTE - changed initial value of n to 1 (from 0) to counter problem when e=0 (-eg- January for date selects)
	for (var n=1; n<e.length; n++) {
		// alert(e.options[n].value);
		if (e.options[n].value == v) {
			e.selectedIndex = n;
			 return n;
		}
	}
	return 0;
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ DYNAMIC FORM FUNCTIONS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/*
 *	prefillForm(reference to form to be filled, object containing properties to use filling form)
 *	property names in object must match form element names
 */
function prefillForm(formRef,objRef) {
	for (var n=0; n<formRef.elements.length; n++) {
		var objVal = "";
		var curInput = formRef[n];
		var curInputType = formRef[n].type;
		var curObjPropertyType = typeof objRef[curInput.name];
		if (curObjPropertyType == 'undefined') {
			// if object does not have a property matching current input name don't do anything
			continue;
		}
		if ((curObjPropertyType == 'object') || (curObjPropertyType == 'array')) {
			continue;
		}
		// some object properties may be returned by a function
		// see campaign_manager/editRegisteredUser for an example - used to set roles from CUSTOMER_GROUP field
		if (curObjPropertyType == 'function') {
			objVal = objRef[curInput.name]();
		} else {
			objVal = objRef[curInput.name];
		}
		// alert("mapping "+curInput.name+" : "+objVal);
		switch (curInputType) {
			case 'button' :
			case 'image' :
			case 'file' :
			case 'reset' :
			case 'submit' :
				continue;
			break;
			case 'textarea' :
			case 'password' :
			case 'hidden' :
			case 'text' :
				curInput.value = objVal;
			break;
			case 'select' :
			case 'select-one' :
			case 'select-multiple' :
				selectOption(curInput,objVal);
			break;
			case 'radio' :	
			case 'checkbox' :
				if (curInput.value == objVal) {
					curInput.checked = true;
				} else {
					curInput.checked = false;
				}
			break;
		}  // end switch curInputType
	}
}


/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ SELECT INPUT FUNCTIONS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

/*  addOption()
	Adds an option to the drop-down navigation list
	Compatible with Mozilla and IE6
	*/
function addOption(selectID,value,label,title) {
    newOpt = document.createElement("OPTION");
	newOpt.value = value;
	newOpt.innerHTML = label;
	newOpt.title = title;
    document.getElementById(selectID).appendChild(newOpt);
}

/*
 * removes all options from select list
 */
function clearSelect(selectId) {
	var s = document.getElementById(selectId);
	while (s.length > 0) {
		s.removeChild(s.childNodes[0]);
	}
}


/*
 * maps shared sourceObject property values to targetObject
 * useful for copying values from (transient) objects returned by Ajax requests to (more permanent) objects used by the application
 * does not map functions
 */
function mapObject(sourceObject, targetObject) {
	if ((typeof sourceObject != 'object') || (typeof targetObject != 'object')) {
		return false;
	}
	for (prop in sourceObject) {
		if (typeof sourceObject[prop] == 'function') {
			// do not map functions across (source and destination Objects already share prototype methods)
			continue;
		}
		if (typeof targetObject[prop] == 'undefined') {
			// if destination object does not have property 'prop' then go on to next
			continue;
		}
		if ((sourceObject[prop] == null) || (sourceObject[prop] == "")) {
			// do not over-write destination Object defaults with nulls or empty strings
			continue;
		}
		targetObject[prop] = sourceObject[prop];
	}
}


