// Validator Object
var valid = new Object();

// REGEX Elements

// all available characters
valid.allavailable = /\w/;

// matches zip codes
valid.zipCode = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

// matches $17.23 or $14,281,545.45 or ...
valid.Currency = /\$\d{1,3}(,\d{3})*\.\d{2}/;

// matches 17.23 or 14,281,545.45 or ...
valid.CommonNumbers = /\d{1,3}(,\d{3})*[\.\d{2}]/;

// matches 17.23 or 14,281,545.45 or ...
valid.SomeNumbers = /^\d{1,6}$/;

// matches 5:04 or 12:34 but not 75:83
valid.Time = /^([1-9]|1[0-2]):[0-5]\d$/;

//matches email
valid.emailAddress = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;

// matches phone ###-###-####
valid.phoneNumber = /^\(?\d{3}\)?\s|-\d{3}-\d{4}$/;

// International Phone Number
valid.phoneNumberInternational = /^\d(\d|-){7,20}/;

// IP Address
valid.ipAddress = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;

// Date xx/xx/xxxx
valid.Date = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/;
//valid.Date = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/;

// percentage
valid.Percent =  /^\d{2}$/;

// State Abbreviation
valid.State = /^(AK|AL|AR|AZ|CA|CO|CT|DC|DE|FL|GA|HI|IA|ID|IL|IN|KS|KY|LA|MA|MD|ME|MI|MN|MO|MS|MT|NB|NC|ND|NH|NJ|NM|NV|NY|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VA|VT|WA|WI|WV|WY)$/i;

// Social Security Number
valid.SSN = /^\d{3}\-\d{2}\-\d{4}$/;
		
// 255 characters
valid.Count = /\w/;
				
// language 
valid.Lang = /^((?!english)(?!none).)*$/i;

function validateForm (theForm)
{
	var elArr = theForm.elements.length;		
  
	for(var i = 0; i < elArr; i++)
	{
		with(theForm.elements[i])
		{
			var v = theForm.elements[i].id;
			var rq = theForm.elements[i].title;
				
			if(!v) continue;

			var thePat = valid[v];
			var gotIt = thePat.exec(value);

			if (value.length > 0  || rq)
			{
				if (! gotIt)
				{
				  if (value.length > 0)
					{
						err = "'s format is invalid. Please follow the instructions."
					}
					else
					{
						err = "is required.";
					}
        	  
					alert("Error: " + name + " " + err);
        	theForm.elements[i].focus();
        	if(theForm.elements[i].length == null)
						theForm.elements[i].select();
        	return false;
        }
			}

			if(v=="Count" && value.length > 255)
			{
				err="needs to be less than 255 characters. \n[Currently:" + value.length + "]";
				alert("Error: " + name + " " + err);
       	theForm.elements[i].focus();
				return false
			}
		}
	}
	return true;
}

function radioSelect(theForm, radioName)
{
	var xcount;
	var rad = document.forms[theForm].elements[radioName];
	
	xcount = 0;

	if (typeof(rad.length) != "undefined")
	{					
		for (var x=0; x<rad.length; x++)
		{
			if(rad[x].checked)
			{
				xcount=xcount+1;
			}
		} 		
		if(xcount == 0)
		{
			alert("Please Select the individual that made the RSVP");
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		if (rad.checked != true)
		{
			alert("Please Select the individual that made the RSVP");
			return false;
		}
		else
		{
			return true;
		}	
	}
}

//	function SSN(a){
//		if((a.value.length==3)||(a.value.length==6)){a.value=a.value + "-";}
//	}

//	function Phone(a){
//		if((a.value.length==3)||(a.value.length==7)){a.value=a.value + "-";}
//	}