String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

String.prototype.startsWith = function(str)
{return (this.match("^"+str)==str)}

String.prototype.count=function(s1) 
{ 
	return this.split(s1).length - 1;
}


var errorIntro = "There is a problem with the submitted information.  Please correct and submit again.<BR/>";

var updateMade = false;

function confirmExit()
{
	if(changesMade() == true)
  		return "Any unsaved changes will be lost.";
}
function saveMade()
{
	updateMade = false;
}
function changesMade()
{
	return updateMade;
}
function warnIfUnsavedChanges()
{
	var inputFields = document.getElementsByTagName('input');
	var selectFields = document.getElementsByTagName('select');
	var textareaFields = document.getElementsByTagName('textarea');

	var i=0;
	var field = null;

	for(i=0; i<inputFields.length; i++)
	{
		field = inputFields[i];
		
		if (window.addEventListener)
			field.addEventListener('change',function () { updateMade = true; },false);
		else
			field.attachEvent("onchange", function() { updateMade = true; });
	}
	
	for(i=0; i<textareaFields.length; i++)
	{
		field = textareaFields[i];
		
		if (window.addEventListener)
			field.addEventListener('change',function () { updateMade = true; },false);
		else
			field.attachEvent("onchange", function() { updateMade = true; });
	}	

	for(i=0; i<selectFields.length; i++)
	{
		field = selectFields[i];
		
		if (window.addEventListener)
			field.addEventListener('change',function () { updateMade = true; },false);
		else
			field.attachEvent("onchange", function() { updateMade = true; });
	}	
	
	window.onbeforeunload = confirmExit;
}

function ValidationField(fieldIdArg, validationArg, errorMsgArg, optionalArg, altLabelArg)
{
	this.fieldId = fieldIdArg;
	this.validation = validationArg;
	this.errorMsg = errorMsgArg;
	this.hasError = false;
	this.altLabel = altLabelArg;
	this.optional = false;
	
	if(optionalArg != null)
		this.optional = optionalArg;
}

function manualResetLabels(labelNames)
{
	var i = 0;
	for(i=0; i<labelNames.length; i++)
	{
		if(document.getElementById(labelNames[i]))
		{
			document.getElementById(labelNames[i]).className = "label";
		}
	}
}
function resetLabels(validationFields)
{
	var i = 0;
	for(i=0; i<validationFields.length; i++)
	{
		if((validationFields[i].altLabel != null) && (validationFields[i].altLabel.length > 0) && (document.getElementById(validationFields[i].altLabel)))
			document.getElementById(validationFields[i].altLabel).className = "label";
		else if(document.getElementById(validationFields[i].fieldId + "Label"))
			document.getElementById(validationFields[i].fieldId + "Label").className = "label";
	}
}

function showClientError(message, validationFields, altErrorDisplayDiv, hideIntroMessage)
{
	var targetDiv = "clientError";
	
	if(isDefined(altErrorDisplayDiv))
		targetDiv = altErrorDisplayDiv;
	
	if(document.getElementById(targetDiv))
	{
		document.getElementById(targetDiv).innerHTML = errorIntro + "<UL class=\"errorBullets\">" + message + "</UL>";		
		
		if(hideIntroMessage == true)
			document.getElementById(targetDiv).style.display = 'none';
		else
			document.getElementById(targetDiv).style.display = 'block';
	}
	
	var i = 0;
	for(i=0; i<validationFields.length; i++)
	{
		if(validationFields[i].hasError)
		{
			if((validationFields[i].altLabel != null) && (validationFields[i].altLabel.length > 0) && (document.getElementById(validationFields[i].altLabel)))
				document.getElementById(validationFields[i].altLabel).className = "labelError";
			else if(document.getElementById(validationFields[i].fieldId + "Label"))
				document.getElementById(validationFields[i].fieldId + "Label").className = "labelError";		
		}
	}
}

function valid(validationFields, altErrorDisplayDiv, hideIntroMessage)
{
	resetLabels(validationFields);
	
	var i = 0;
	var message = '';
	
	for(i=0; i<validationFields.length; i++)
	{		
		if(validationFields[i].validation(validationFields[i]) == false)
		{
			validationFields[i].hasError = true;			
			message += "<LI>" + validationFields[i].errorMsg + "</LI>";
		}
	}
	
	if(message.length > 0)
	{
		showClientError(message, validationFields, altErrorDisplayDiv, hideIntroMessage);
		return false;
	}
	
	return true;
}



function validationSelectedIndexNotZero(validationField)
{	
	//if optional then it doesn't matter what they select
	if(validationField.optional == true)
		return true;
	
	var field = document.getElementById(validationField.fieldId);		
	
	if((field == null) || (field == "undefined"))
		return false;
	
	if(field.selectedIndex > 0)
		return true;
	else
		return false;
}

function validationAtLeastOneInMultipleSelectSelected(validationField)
{
	//if optional then it doesn't matter what they select
	if(validationField.optional == true)
		return true;

	var selObj = document.getElementById(validationField.fieldId);
	
	var i = 0;
	for (i = 0; i < selObj.options.length; i++) 
	{
		if (selObj.options[i].selected) 
		{
			return true;
		}
	}
	
	return false;
}

function validationInputPopulated(validationField)
{
	//if optional then it doesn't matter what they entered
	if(validationField.optional == true)
		return true;
	
	var field = document.getElementById(validationField.fieldId).value;
		
	if(field.trim().length > 0)
		return true;
	else
		return false;
}
function validationValidEmailList(validationField)
{
	var addressList = document.getElementById(validationField.fieldId).value;
	
	if(addressList.indexOf(",") < 0)
		return isValidEmail(addressList.trim());
	
	var addresses = addressList.split(",");
	var i = 0;
	
	for(i = 0; i < addresses.length; i++)
	{
		if(isValidEmail(addresses[i].trim()) == false)
			return false;
	}	
	
	return true;
}
function validationValidEmail(validationField)
{
	var address = document.getElementById(validationField.fieldId).value;
	
	if(address.length < 1)
	{
		//if nothing entered and it's optional, return true, otherwise false
		if(validationField.optional == true)
			return true;
		else
			return false;
	}
	
	//if something was entered validate the format

	return isValidEmail(address);
}
function isValidEmail(address)
{
	var reg = /^([\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+\.)*[\w\!\#$\%\&\'\*\+\-\/\=\?\^\`{\|\}\~]+@((((([a-z0-9]{1}[a-z0-9\-]{0,62}[a-z0-9]{1})|[a-z])\.)+[a-z]{2,6})|(\d{1,3}\.){3}\d{1,3}(\:\d{1,5})?)$/i;
	   
	if(reg.test(address) == false)
		return false;
	else
		return true;
}
function validationValidName(validationField)
{
	var name = document.getElementById(validationField.fieldId).value;
	
	if(name.length < 1)
	{
		//if nothing entered and it's optional, return true, otherwise false
		if(validationField.optional == true)
			return true;
		else
			return false;
	}	
	
	//if something was entered validate the format

	//need to be first and last at minimum
	var parts = name.split(" ");
	if(parts.length < 2)
		return false;
	
	return true;
}

function validationRoleSected(validationField)
{
	//if optional doesn't matter if they clicked anything
	if(validationField.optional == true)
		return true;
	
	var i = 0;
	
	for(i=0; i<document.userForm.elements.length; i++)
	{
		if(document.userForm.elements[i].name.startsWith("function-"))
		{
			if(document.userForm.elements[i].checked == true)
				return true;	
		}
	}
	
	return false;
}

function validationValidZipCode(validationField)
{
	var value = document.getElementById(validationField.fieldId).value;
	
	if(value.length < 1)
	{
		//if nothing entered and it's optional, return true, otherwise false
		if(validationField.optional == true)
			return true;
		else
			return false;
	}		
		
	//if something was entered validate the format
	//assume valid is 55555 or 55555-4444
	
	if((value.length != 5) && (value.length != 10))
		return false;
	
	if(value.length == 5)
	{
		if(isNumeric(value))
			return true;
		else
			return false;
	}
	
	if(value.indexOf("-") < 0)
		return false;
	
	var parts = value.split("-");
	
	if((parts[0].length != 5) || (parts[1].length != 4))
		return false;
	
	if(isNumeric(parts[0]) && isNumeric(parts[1]))
		return true;
	
	return false;
}

function validationValidUKZipCode(validationField) 
{
	var value = document.getElementById(validationField.fieldId).value;
	
	if(value.length < 1)
	{
		//if nothing entered and it's optional, return true, otherwise false
		if(validationField.optional == true)
			return true;
		else
			return false;
	}	
	
	if(value == "GIR 0AA")
		return true;
	
	var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1} ?[0-9][A-Z]{2}/i;
	return postcodeRegEx.test(value);
}


function validationIsNumeric(validationField)
{	
	var value = document.getElementById(validationField.fieldId).value;
	return isNumeric(value);
}

function validationIsInteger(validationField)
{
	var value = document.getElementById(validationField.fieldId).value;
	return isInteger(value);
}

function validationIsIntegerGreaterThanZero(validationField)
{
	var value = document.getElementById(validationField.fieldId).value;
	return isPositiveInteger(value);
}

function validationValidPassword(validationField)
{
	var field = document.getElementById(validationField.fieldId).value.trim();
	
	//if optional then it doesn't matter what they entered
	if((validationField.optional == true) && (field.length < 1))
		return true;
	
	return isValidPassword(field);
}

function isValidPassword(password)
{
	if(password.length < 6)
		return false;
	
	var psLowerCase = password.toLowerCase();
	
	//if all alpha characters it's invalid - must have at least one non-alpha character
	if(onlyContains("abcdefghijklmnopqrstuvwxyz", psLowerCase))
		return false;
	else
		return true;
}

function validationValidUSPhoneNumber(validationField)
{
	var value = document.getElementById(validationField.fieldId).value;
	
	if(value.length < 1)
	{
		//if nothing entered and it's optional, return true, otherwise false
		if(validationField.optional == true)
			return true;
		else
			return false;
	}	
	
	var numbersOnly = getNumericPortion(value);

	if(numbersOnly.length != 10)
		return false;
	else
		return true;
}
function isNumeric(value)
{
	return onlyContains("0123456789.,", value);
}

function isInteger(value)
{
	return onlyContains("0123456789", value);
}

function isPositiveInteger(value)
{
	var isInteger = onlyContains("0123456789", value);
	
	if(isInteger == false)
		return false;
	 
	var valueNum = parseInt(value);
	
	if(valueNum > 0)
		return true;
	else
		return false;
}

function onlyContains(validChars, value)
{
	var isGood = true;
	var Char;

	for (i = 0; i < value.length && isGood == true; i++) 
	{ 
		Char = value.charAt(i); 
		if(validChars.indexOf(Char) == -1) 
		{
			isGood = false;
		}
	}
	
	return isGood;
}

function getNumericPortion(value)
{
	var Char;
	var validChars = "0123456789";
	var result = "";

	for (i = 0; i < value.length; i++) 
	{ 
		Char = value.charAt(i); 
		if(validChars.indexOf(Char) >= 0) 
		{
			result += Char;
		}
	}
	
	return result;
}

function isDefined(value)
{
	if((value != null) && (value.length > 0) && (value != "undefined"))
		return true;
	else
		return false;
}
