// returns true if the string is empty
function isEmpty (str)
{
	return (str == null) || (str.length == 0);
}
// returns true if the string is a valid email
function isEmail (str)
{
	if (isEmpty(str)) return false;
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
	return re.test(str);
}

function areEmails (str)
{
	if (isEmpty(str)) return false;
	var email = str.split(',');
	for (var i = 0; i < email.length; i++)
		if (!isEmail(email[i]))
			return false;
	return true;
}
// returns true if the string only contains characters A-Z or a-z
function isAlpha (str)
{
	var re = /[^a-zA-Z]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string only contains characters 0-9
function isNumeric (str)
{
	var re = /[\D]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphaNumeric (str)
{
	var re = /[^a-zA-Z0-9]/g
	if (re.test(str)) return false;
	return true;
}
// returns true if the string's length equals "len"
function isLength (str, len)
{
	return str.length == len;
}
// returns true if the string's length is between "min" and "max"
function isLengthBetween (str, min, max)
{
	return (str.length >= min) && (str.length <= max);
}
// returns true if the string is a US phone number formatted as...
// (000)000-0000, (000) 000-0000, 000-000-0000, 000.000.0000, 000 000 0000, 0000000000
function isPhoneNumber (str)
{
	var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
	return re.test(str);
}
// returns true if the string is a valid date formatted as...
// mm dd yyyy, mm/dd/yyyy, mm.dd.yyyy, mm-dd-yyyy
function isDate (str)
{
	var re = /^(\d{1,2})[\s\.\/-](\d{1,2})[\s\.\/-](\d{4})$/
	if (!re.test(str)) return false;
	var result = str.match(re);
	var m = parseInt(result[1]);
	var d = parseInt(result[2]);
	var y = parseInt(result[3]);
	if (m < 1 || m > 12 || y < 1900 || y > 2100) return false;
	if (m == 2)
	{
		var days = ((y % 4) == 0) ? 29 : 28;
	}
	else if (m == 4 || m == 6 || m == 9 || m == 11)
	{
		var days = 30;
	}
	else
	{
		var days = 31;
	}
	return (d >= 1 && d <= days);
}

function focus (formName, controlName)
{
	eval(formName + "." + controlName + ".focus()");
}
function getValue (formName, controlName)
{
	return eval(formName + "." + controlName + ".value");
}
function isRequired (formName, controlName, title)
{
	if (getValue(formName, controlName).replace(/^\s*|\s*$/, "") == '')
		return "Value/Selection required against field : '" + title + "'";
	return null;
}
function isEmailAddress (formName, controlName, title)
{
	if (!isEmail(getValue(formName, controlName)))
		return "Invalid value provided in field : '" + title + "'. Example: abdullah@somedomain.com";
	return null;
}
function areEmailAddresses (formName, controlName, title)
{
	if (!areEmails(getValue(formName, controlName)))
		return "Invalid value provided in field : '" + title + "'. Example: abdullah@somedomain.com,mustafa@anotherdomain.net (Note comma with out spaces)";
	return null;
}
function isWholeNumber (formName, controlName, title, min, max)
{
	var v = getValue(formName, controlName);
	var er = null;
	if (v != null && v != "")
	{
		if (isNumeric(v))
		{
			var wn = parseInt(v);
			if (min && wn < min)
				er = "Number should be greater than or equals to " + min;
			else if (max && wn > max)
				er = "Number should be less than or equals to " + max;
		}
		else
			er = "Only numbers are allowed";
	}
	if (er != null)
		er = "Invalid value provided in field : '" + title + "'. " + er;
	return er;
}
function isRange (formName, controlName, title)
{
	var er;
	var minControlValue = getValue(formName, controlName + postMinRange);
	var maxControlValue = getValue(formName, controlName + postMaxRange);
	if (!isNaN(minControlValue) && !isNaN(maxControlValue) && minControlValue > maxControlValue)
		er = "'max' value should be greater than or equals to 'min' value.";
	return er;
}
function isGroupRange (formName, controlNames, titles)
{
	var er;
	for (var i = 0; i < controlNames.length; i++)
	{
		var minControlValue = getValue(formName, controlNames[i] + postMinRange);
		var maxControlValue = getValue(formName, controlNames[i] + postMaxRange);
		if (!isNaN(minControlValue) && !isNaN(maxControlValue))
		{
			if (maxControlValue > minControlValue)
				break;
			else if (minControlValue > maxControlValue)
			{
				er = "'max' value should be greater than or equals to 'min' value.";
				break;
			}
		}
	}
	return er;
}
function areNotEqual (formName, controlName1, title1, controlName2, title2)
{
	if (getValue(formName, controlName1) == getValue(formName, controlName2))
		return "'" + title1 + "' and '" + title2 + "' are same";
	return null;
}
// -------------------------------------------------------------------
// hasOptions(obj)
//  Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions (obj)
{
	if (obj != null && obj.options != null)
	{
		return true;
	}
	return false;
}

// -------------------------------------------------------------------
// selectAllOptions(select_object)
//  This function takes a select box and selects all options (in a
//  multiple select object). This is used when passing values between
//  two select boxes. Select all options in the right box before
//  submitting the form so the values will be sent to the server.
// -------------------------------------------------------------------
function selectAllOptions (obj)
{
	if (!hasOptions(obj))
	{
		return;
	}
	for (var i = 0; i < obj.options.length; i++)
	{
		obj.options[i].selected = true;
	}
}

// -------------------------------------------------------------------
// moveSelectedOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  This function moves options between select boxes. Works best with
//  multi-select boxes to create the common Windows control effect.
//  Passes all selected values from the first object to the second
//  object and re-sorts each box.
//  If a third argument of 'false' is passed, then the lists are not
//  sorted after the move.
//  If a fourth string argument is passed, this will function as a
//  Regular Expression to match against the TEXT or the options. If
//  the text of an option matches the pattern, it will NOT be moved.
//  It will be treated as an unmoveable option.
//  You can also send this into the <SELECT> object as follows:
//    onDblClick="moveSelectedOptions(this,this.form.target)
//  This way, when the user double-clicks on a value in one box, it
//  will be transferred to the other (in browsers that support the
//  onDblClick() event handler).
// -------------------------------------------------------------------
function moveSelectedOptions (from, to)
{
	// Move them over
	if (!hasOptions(from))
	{
		return;
	}
	for (var i = 0; i < from.options.length; i++)
	{
		var o = from.options[i];
		if (o.selected)
		{
			if (!hasOptions(to))
			{
				var index = 0;
			}
			else
			{
				var index = to.options.length;
			}
			to.options[index] = new Option(o.text, o.value, false, false);
		}
	}
	// Delete them from original
	for (var i = (from.options.length - 1); i >= 0; i--)
	{
		var o = from.options[i];
		if (o.selected)
		{
			from.options[i] = null;
		}
	}
	from.selectedIndex = -1;
	to.selectedIndex = -1;
}

// -------------------------------------------------------------------
// moveAllOptions(select_object,select_object[,autosort(true/false)[,regex]])
//  Move all options from one select box to another.
// -------------------------------------------------------------------
function moveAllOptions (from, to)
{
	selectAllOptions(from);
	if (arguments.length == 2)
	{
		moveSelectedOptions(from, to);
	}
	else if (arguments.length == 3)
	{
		moveSelectedOptions(from, to, arguments[2]);
	}
	else if (arguments.length == 4)
		{
			moveSelectedOptions(from, to, arguments[2], arguments[3]);
		}
}

// -------------------------------------------------------------------
// swapOptions(select_object,option1,option2)
//  Swap positions of two options in a select list
// -------------------------------------------------------------------
function swapOptions (obj, i, j)
{
	var o = obj.options;
	var i_selected = o[i].selected;
	var j_selected = o[j].selected;
	var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	var temp2 = new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
	o[i] = temp2;
	o[j] = temp;
	o[i].selected = j_selected;
	o[j].selected = i_selected;
}

// -------------------------------------------------------------------
// moveOptionUp(select_object)
//  Move selected option in a select list up one
// -------------------------------------------------------------------
function moveOptionUp (obj)
{
	if (!hasOptions(obj))
	{
		return;
	}
	for (i = 0; i < obj.options.length; i++)
	{
		if (obj.options[i].selected)
		{
			if (i != 0 && !obj.options[i - 1].selected)
			{
				swapOptions(obj, i, i - 1);
				obj.options[i - 1].selected = true;
			}
		}
	}
}

// -------------------------------------------------------------------
// moveOptionDown(select_object)
//  Move selected option in a select list down one
// -------------------------------------------------------------------
function moveOptionDown (obj)
{
	if (!hasOptions(obj))
	{
		return;
	}
	for (i = obj.options.length - 1; i >= 0; i--)
	{
		if (obj.options[i].selected)
		{
			if (i != (obj.options.length - 1) && ! obj.options[i + 1].selected)
			{
				swapOptions(obj, i, i + 1);
				obj.options[i + 1].selected = true;
			}
		}
	}
}

// function will clear input elements on each form
function clearForm (_form)
{
	// declare element type
	var type = null;
	// loop through each element on form
	for (y = 0; y < _form.elements.length; y++)
	{
		// define element type
		type = _form.elements[y].type
		// alert before erasing form element
		//alert('form='+x+' element='+y+' type='+type);
		// switch on element type
		switch (type)
				{
			case "text":
			case "textarea":
			case "password":
			//case "hidden":
				_form.elements[y].value = "";
				break;
			case "radio":
			case "checkbox":
				_form.elements[y].checked = "";
				break;
			case "select-one":
				_form.elements[y].options[0].selected = true;
				break;
			case "select-multiple":
				for (z = 0; z < _form.elements[y].options.length; z++)
				{
					_form.elements[y].options[z].selected = false;
				}
				break;
		}
	}
}
function imposeMaxLengthOnKeyPress(Object, MaxLen)
{
	return (Object.value.length <= MaxLen);
}
function imposeMaxLengthOnBlur(Object, MaxLen)
{
	if(Object.value.length > MaxLen)
	{
		Object.focus();
		alert("Exceeds max length of " + MaxLen);
	}
}
