var PATTERN_ANY = '.';
var PATTERN_CC = '^[0-9]{4}-?[0-9]{4}-?[0-9]{4}-?[0-9]{4}$';
var PATTERN_DATE = '^[0-9]{1,2}[\\-/]{1}[0-9]{1,2}[\\-/]{1}[0-9]{4}$';
var PATTERN_EMAIL = '^[A-Za-z0-9_.-]+@([A-Za-z0-9_.-]+[.])+[A-Za-z]+$';
var PATTERN_MONEY = '^[0-9]+.[0-9]{2}$';
var PATTERN_NUMBER = '^[0-9]+$';
var PATTERN_PHONE = '^((\([0-9]{3}\) ?)|([0-9]{3}-))?[0-9]{3}-[0-9]{4}$';
var PATTERN_SSN = '^[0-9]{3}-[0-9]{2}-[0-9]{4}$';
var PATTERN_ZIP = '^[0-9]{5}(-[0-9]{4})?$';

function validateRegisterAccount()
{
	vform = new ValidationForm(document.aspnetForm);
	vform.AddElement('ctl00_CPHMainBody_uxFirstNameTxt', 'First Name', 1, PATTERN_ANY);
	vform.AddElement('ctl00_CPHMainBody_uxLastNameTxt', 'Last Name', 1, PATTERN_ANY);
	vform.AddElement('ctl00_CPHMainBody_uxEmailTxt', 'Email', 1, PATTERN_EMAIL);
	vform.AddElement('ctl00_CPHMainBody_uxUserNameTxt', 'User Name', 1, PATTERN_ANY);
	vform.AddElement('ctl00_CPHMainBody_uxPasswordTxt', 'Password', 1, PATTERN_ANY);
	vform.AddElement('ctl00_CPHMainBody_uxConfirmPasswordTxt', 'Confirm Password', 1, PATTERN_ANY);	
	vform.AddElement('ctl00_CPHMainBody_uxEntityTxt', 'Entity', 1, PATTERN_ANY);	
    
    if (document.getElementById("ctl00_CPHMainBody_uxPasswordTxt").value != document.getElementById("ctl00_CPHMainBody_uxConfirmPasswordTxt").value)
    {
        alert("Please ensure your password matches the confirm password");
        return false;
    }
    
	if (vform.Validate())
	{
		return true;
	}
	else
	{
		return false;
	}
}

function validateApplicationMaint()
{
	vform = new ValidationForm(document.aspnetForm);
	vform.AddElement('ctl00_CPHMainBody_uxApplicationNameTxt', 'Application Name', 1, PATTERN_ANY);
	vform.AddElement('ctl00_CPHMainBody_uxApplicationRolesDdl', 'Application Roles', 1, PATTERN_ANY, 'ListBox');
    
	if (vform.Validate())
	{
		return true;
	}
	else
	{
		return false;
	}
}

function validateContactUs()
{
	vform = new ValidationForm(document.aspnetForm);
	vform.AddElement('ctl00_CPHMainBody_uxEmailAddressTxt', 'Email Address', 1, PATTERN_EMAIL);
	vform.AddElement('ctl00_CPHMainBody_uxConfirmEmailAddressTxt', 'Confirm Email Address', 1, PATTERN_EMAIL);
	vform.AddElement('ctl00_CPHMainBody_uxCommentsTxt', 'Comments', 1, PATTERN_ANY);	
	
	var email = eval(document.getElementById("ctl00_CPHMainBody_uxEmailAddressTxt"));
	var email2 = eval(document.getElementById("ctl00_CPHMainBody_uxConfirmEmailAddressTxt"));
	
    if (email.value != email2.value)
    {
        alert("Message NOT sent, the Email Addresses do not match");
        return false;
    }
    else
    {
        if (vform.Validate())
	    {
		    return true;
	    }
	    else
	    {
		    return false;
	    }
    }
}

function trim(str)
{
     s = str.replace(/^(\s)*/, '');
     s = s.replace(/(\s)*$/, '');
     return s;
}

function ValidationForm(oForm) {
	this.Elements = new Array();
	this.Form = oForm;
	this.AddElement = AddElement;
	this.Validate = Validate;
}

function Element(sName, sCaption, bRequired, sPattern, sType) {
	this.Name = sName;
	this.Caption = sCaption;
	this.Value = '';
	this.Required = bRequired;
	this.Pattern = sPattern;
	this.FieldType = sType;
}

function AddElement(sName, sCaption, bRequired, sPattern, sType) {
	index = this.Elements.length;
	this.Elements[index] = new Element(sName, sCaption, bRequired, sPattern, sType);
}

function Validate() {
	sMissing = '';
	sInvalid = '';
	
	for (i=0;i<this.Elements.length;i++)
	{
		oElement = this.Elements[i];
		oElement.Value = eval('document.' + this.Form.name + '.' + oElement.Name).value;
		
		// Validate required fields are filled in.		
		if (oElement.Required == 1)
		{		
			if (oElement.Value == '')
			{
				sMissing += oElement.Caption + '\n';
			}
			if (oElement.FieldType == 'Name')
			{				
				if (oElement.Value == 'First Name')
				{
					sMissing += oElement.Caption + '\n';
				}
				if (oElement.Value == 'Middle Name')
				{
					sMissing += oElement.Caption + '\n';
				}				
				if (oElement.Value == 'Last Name')
				{
					sMissing += oElement.Caption + '\n';
				}				
			}
			if (oElement.FieldType == 'Dropdown')
			{
				if (oElement.selectedindex == 0)
				{
					sMissing += oElement.Caption + '\n';
				}
			}
			if (oElement.FieldType == 'ListBox')
			{
			    if (oElement.selectedindex < 1)
			    {
			        sMissing += oElement.Caption + '\n';
			    }			
			}
			if (oElement.FieldType == 'Radio')
			{
				sMissing += oElement.Caption + '\n';
			}			
		}

		// Validate required patterns match.
		if (oElement.Value != '' && oElement.Pattern != '')
		{
			oRegExp = new RegExp(oElement.Pattern, 'gi');
			
			if (!oRegExp.test(oElement.Value))
			{
				sInvalid += oElement.Caption + '\n';
			}
		}
	}
	
	if (sMissing != '')
	{
		sMissing = 'The following fields are missing:\n\n' + sMissing + '\n';
	}
	
	if (sInvalid != '')
	{
		sInvalid = 'The following fields are invalid:\n\n' + sInvalid;
	}
	
	if (sMissing != '' || sInvalid != '')
	{
		alert(sMissing + sInvalid);
		return 0;
	}
	else
	{
		return 1;
	}
}