var Validator = new Object();

Validator.Initialize = function(formId, fieldNum, submitId, validImage, invalidImage)
{
	Validator.currentSelector = '';
	Validator.currentForm = formId;
	gebid(Validator.currentForm).reset();
	Validator.fieldNumValidated = 0;
	Validator.fieldNumToValidate = fieldNum;
	Validator.submitId = submitId;
	Validator.validImage = validImage;
	Validator.invalidImage = invalidImage;
}

Validator.Validate = function(selector, inputType, browser)
{
	this.currentSelector = selector;
	this.preload(selector);
	var isEmpty = true;
	
	switch(selector.type)
	{
		case 'undefined': break;
		case 'radio':
		
			switch(inputType)
			{
				case 'gender':
					this.validateGender();
					return;

				default:
					for(var x=0; x < selector.length; x++)
					{
						if(selector[x].checked == true)
						{
							isEmpty = false;
						}
					}
			}
			
			
			break;
		case 'select-multiple':
			for(var x=0; x < selector.length; x++)
			{
				if(selector[x].selected == true)
				{
					isEmpty = false;
				}
			}
			break;
		case 'checkbox':
			if(selector.checked)
			{
				isEmpty = false;
			}
			break;
		
		case 'text':
			if(selector.value)
			{
				isEmpty = false;
			}
			
			switch(inputType)
			{
				case 'email':
					this.validateEmail();
					if (this.currentSelector.name == 'Email1_retype') { // 2nd email field
						this.validateRetypeEmail();
					}
					return;
				case 'gender':
					this.validateGender();
					return;
				case 'ZIP':
					this.ZIP();
					return;	
			}
			break;
			
		case 'password':
			if (inputType) {
				this.validateRetypePassword();
				return;
			} else {
				if(selector.value)
				{
					isEmpty = false;
				}
			}
			break;
		default:
			if(selector.value)
			{
				// safari 3.0 = 1024
				// IE 7.0 = 2147483647
				if(selector.maxLength > 0 && !(selector.maxLength == 1024) && !(selector.maxLength == 2147483647))
				{
					if(selector.value.length == selector.maxLength)
					{
						isEmpty = false;
					}
				}
				else
				{
					isEmpty = false;
				}
			}
			switch(inputType)
			{
				case 'email':
					this.validateEmail();
					return;
			}
	
	}
	
	if(isEmpty) this.invalid();
	else this.valid();
}


Validator.ZIP = function()
{	
	alert('zip');
	if(selector.value)
	{
		// safari 3.0 = 1024
		// IE 7.0 = 2147483647
		if(selector.maxLength > 0 && !(selector.maxLength == 1024) && !(selector.maxLength == 2147483647))
		{
			if(selector.value.length == selector.maxLength)
			{
				isEmpty = false;
				
			}
		}
		else
		{
			isEmpty = false;
			
		}
	} else {
		this.invalid();
		return false;
	}
	
	this.valid();
}

Validator.validateGender = function()
{
	var genderM = document.getElementById('Child1_Sex_M').value; //first sex field
	var genderF = document.getElementById('Child1_Sex_F').value; //second sex field
	if ( (selector[0].checked == false) && (selector[1].checked == false) ) {
		this.invalid();
		return false;
	}
	
	this.valid();
}

Validator.validateRetypePassword = function()
{
	var pw1 = document.getElementById('pw1').value; //first pw field
	var pw2 = document.getElementById('pw2').value; //second pw field
	if (pw1 != pw2) {
		this.invalid();
		return false;
	}
	
	this.valid();
}

Validator.validateRetypeEmail = function()
{
	var email1 = document.getElementById('Email1').value;
	var email2 = document.getElementById('Email1_retype').value;
	if (email1 != email2) {
		this.invalid();
		return false;
	}
	
	this.valid();
}

Validator.validateEmail = function()
{
	

	
	var str = this.currentSelector.value;
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1)
	{
		this.invalid();
		return false;
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		this.invalid();
		return false;
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		this.invalid();
		return false;
	}
	
	if (str.indexOf(at,(lat+1))!=-1)
	{
		this.invalid();
		return false;
	}
	
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		this.invalid();
		return false;
	}
	
	if (str.indexOf(dot,(lat+2))==-1)
	{
		this.invalid();
		return false;
	}
	
	if (str.indexOf(" ")!=-1)
	{
		this.invalid();
		return false;
	}
	
	this.valid();
}

Validator.valid = function()
{
	rc(this.currentSelector.invalidImage);
	InsertAfter(this.currentSelector.parentNode, this.currentSelector.validImage, this.currentSelector);
	if(!this.currentSelector.isValid)
	{
		this.fieldNumValidated++;
	}
	if(Validator.AllFieldsValidated())
	{
		gebid(this.submitId).disabled = false;
	}
	this.currentSelector.isValid = true;
	this.currentSelector.validated = true;
}

Validator.invalid = function()
{
	rc(this.currentSelector.validImage);
	InsertAfter(this.currentSelector.parentNode, this.currentSelector.invalidImage, this.currentSelector);
	if(this.currentSelector.isValid)
	{
		this.fieldNumValidated--;
	}
	
	gebid(this.submitId).disabled = true;
	this.currentSelector.isValid = false;
	this.currentSelector.validated = true;
}

Validator.preload = function(selector)
{
	if(selector)
	{
		Validator.currentSelector = selector;		
		if(!Validator.currentSelector.validImage && !Validator.currentSelector.invalidImage)
		{
			Validator.currentSelector.validImage = ce('img', {src: Validator.validImage});
			Validator.currentSelector.invalidImage = ce('img', {src: Validator.invalidImage});
		}
		
		if(Validator.currentSelector.isValid == undefined)
		{
			Validator.currentSelector.isValid = false;
		}
	}
}

Validator.AllFieldsValidated = function(override)
{
	if(this.fieldNumValidated >= this.fieldNumToValidate || override) return true;
	else return false;
}