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)
{
	this.currentSelector = selector;
	this.preload(selector);
	var isEmpty = true;
	
	switch(selector.type)
	{
		case 'undefined': break;
		case 'radio':
			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;
		default:
			if(selector.value)
			{
				//Browser Implicit maxlength-value
				// safari 3.0 = 1024
				// IE 7.0 = 2147483647
				// Chrome 524288
				if(selector.maxLength > 0 && !(selector.maxLength == 1024) && !(selector.maxLength == 2147483647)&& !(selector.maxLength == 524288))
				{
					if(selector.value.length == selector.maxLength)
					{
						isEmpty = false;
					}
				}
				else
				{
					isEmpty = false;
				}
			}
			switch(inputType)
			{
				case 'email':
					this.validateEmail();
					return;
			}
			switch(inputType)
			{
				case 'phone':
					this.validatePhone();
					return;
			}
			switch(inputType)
			{
				case 'zip':
					this.validateZip();
					return;
			}
			switch(inputType)
			{
				case 'date':
					this.validateDate();
					return;
			}
			switch(inputType)
			{
				case 'ssn':
					this.validateSsn();
					return;
			}


	}
	
	if(isEmpty) this.invalid();
	else this.valid();
}







//Date verify
Validator.validateDate = function() 

 {
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables
var str = this.currentSelector.value;

if (str == "") {
		this.invalid();
		return false;
}




var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = str.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format. Please enter like on of the following:\n MM/DD/YY \n MM/DD/YYYY \n MM-DD-YY \n MM-DD-YYYY")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
this.valid();  // date is valid
}


// zipcode 

Validator.validateZip = function() 
{
var str = this.currentSelector.value;
var zip = str.replace(/\D+/, "");

if (str == "") {
   		error = "You didn't enter a zip code.\n";
		this.invalid();
		return false;
}

if (!(zip.length==5 ))
{
alert("Invalid characters in your zip code.  Please enter only five numbers.");
this.invalid();
return false;
}


this.valid();
}


//SSN validation

Validator.validateSsn = function() 
{
var str = this.currentSelector.value;
var pattern = /^\d{3}-?\d{2}-?\d{4}$/;

if (str == "") {
		this.invalid();
		return false;
}

if (!str.match(pattern)) {
  alert("SSN must be formated like NNN-NN-NNNN or NNNNNNNNN");
		this.invalid();
		return false;	
}




this.valid();	


}




// phone number - strip out delimiters and check for 10 digits

Validator.validatePhone = function() 
{
var str = this.currentSelector.value;
var error = "";
if (str == "") {
   		error = "You didn't enter a phone number.\n";
		this.invalid();
		return false;
}

var stripped = str.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) 
	{
		error = "The phone number contains illegal characters. No letters allowed!\n\n ie 208-789-4567 or (208) 789-4567";
		alert(error);  
		this.invalid();
		return false;
       
  
    }
    if (!(stripped.length == 10)) 
	{
		error = "The phone number is the wrong length. Make sure you included an area code.\n\n ie 208-789-4567 or (208) 789-4567";
		alert(error);   
		this.invalid();
		return false;
    } 
this.valid();
}


//Email validation

Validator.validateEmail = function()
{
	var str = this.currentSelector.value;
	var error = "";
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str == "") 
	{
   		error = "You didn't enter an email address.\n\n ie Please enter your email like johndoe@google.com";
		this.invalid();
		return false;
	}
	
	
	if (str.indexOf(at)==-1)
	{
		error = "Please enter your email like johndoe@google.com";
		alert(error);  
		this.invalid();
		return false;
	}
	
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		error = "Please enter your email like johndoe@google.com";
		alert(error); 
		this.invalid();
		return false;
	}
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		error = "Please enter your email like johndoe@google.com";
		alert(error); 
		this.invalid();
		return false;
	}
	
	if (str.indexOf(at,(lat+1))!=-1)
	{
		error = "Please enter your email like johndoe@google.com";
		alert(error); 
		this.invalid();
		return false;
	}
	
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		error = "Please enter your email like johndoe@google.com";
		alert(error); 
		this.invalid();
		return false;
	}
	
	if (str.indexOf(dot,(lat+2))==-1)
	{
		error = "Please enter your email like johndoe@google.com";
		alert(error); 
		this.invalid();
		return false;
	}
	
	if (str.indexOf(" ")!=-1)
	{
		error = "Please enter your email like johndoe@google.com";
		alert(error); 
		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;
}


//POP UP FORM ENTRY
  function openWin(page){
    var w = window.open(page, "", "menubar=no,history=no,resizable=yes,scrollbars=yes,toolbar=no,width=750,height=600");
  }

  function input(formName, obj, idx){
	var str = obj;
	opener.document.forms[formName].elements[obj].value = idx;
	self.close();
  }
  function select(formName, obj, idx){
	var str = obj;
	opener.document.forms[formName].elements[obj].value = idx;
	self.close();
	
 }
  function checkRadio(formName, obj, choice){
   opener.document.forms[formName].elements[obj][choice].checked = true;
   self.close();
  }
  function check(formName, obj, choice){
   opener.document.forms[formName].elements[obj].checked = choice;
   self.close();
  }

