var GE_form_validation = function() {
	//regular expression criteria for validating fields
	this.reWhitespace=/^[\s]+$/;
	this.reAlphabetic=/^[A-Za-zÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïñòóôõöøùúûüýÿ\'\-\. ]+$/;
	this.reDigit=/^[0-9]$/;
	this.reNumeric=/^[0-9]+$/;
	this.reAlphanumeric=/^[A-Za-zÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïñòóôõöøùúûüýÿ\'\-\. 0-9]+$/;
	this.rePunctuation=/^[\-\_\.\,\?\¿\!\¡\/\#\%]+$/;
	this.reText=/^[A-Za-zÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïñòóôõöøùúûüýÿ\'\-\. 0-9\-\_\.\,\?\¿\!\¡\/\#\%]+$/;
	this.reUSZipCode=/^\d{5}(-\d{4})?$/;
	this.reCanadianPostalCode=/^[A-Za-z]\d[A-Za-z]( |-)\d[A-Za-z]\d$/;
	this.rePhoneDelimiter=/(\+|\(|\)|\-|\.|\/| )/g;
	this.reUSPhoneNumber=/^\d{10}$/;
	this.reInternationalPhoneNumber=/^[0-9\+\(\)\-\.\/ ]{7,20}$/;
	this.reEmailAddress=/^([a-zA-Z0-9_\-])([a-zA-Z0-9_\-\.]*)@(\[((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}|((([a-zA-Z0-9\-]+)\.)+))([a-zA-Z]{2,}|(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\])$/;

	//declare error logging array object
	this.err = new Array();
}

GE_form_validation.prototype.init = function(el) {
	//create form object
	this.el = $(el);

	//assign onsubmit to form
	Event.observe(this.el, 'submit', function(e){this.clearErr();this.validate(e);}.bindAsEventListener(this));
}

GE_form_validation.prototype.clearErr = function() {
	this.err = new Array();
	var els = this.el.getElementsBySelector('.error_msg');
	var i = els.length;
	while (i--) {
		if (els[i].nodeName=='UL') {
			els[i].remove();
		} else {
			els[i].removeClassName('error_msg');
		}
	}
}

GE_form_validation.prototype.validate = function(e) {
	//validate form fields
	this.checkFields();

	//report any errors
	if (this.err.length>0) this.reportErr(e);
}

GE_form_validation.prototype.checkFields = function() {

}

GE_form_validation.prototype.IsWhitespace = function(s) {
	return (this.IsEmpty(s) || this.reWhitespace.test(s));
}

GE_form_validation.prototype.IsNumeric = function(s) {
	return this.reNumeric.test(s);
}

GE_form_validation.prototype.IsPhone = function(s) {
	return this.reInternationalPhoneNumber.test(s);
}

GE_form_validation.prototype.IsEmpty = function(s) {
	return ((s==null) || (s.length==0));
}

GE_form_validation.prototype.CheckEmailAddress = function(f) {
	var v = f.getValue();
   	if (this.IsEmpty(v)) return false;
	else {
		if (!this.IsEmailAddress(v)) return false;
		else return true;
	}
}

GE_form_validation.prototype.IsEmailAddress = function(s) {
	return this.reEmailAddress.test(s);
}

GE_form_validation.prototype.logErr = function(f,l,s) {
	var err = {
		field: f,
		label: l,
		msg: s
	}
	this.err.push(err);
}

GE_form_validation.prototype.reportErr = function(e) {
	//stop form from submitting
	Event.stop(e);

	//create unordered list to contain errors
	var errUL = document.createElement('ul');
	errUL.className = 'error_msg';

	//loop through errors to set label style and append error messages to list
	for (var i=0; i<this.err.length; i++) {
		if (this.err[i].label.length) {
			for (var j=0; j<this.err[i].label.length; j++) {
				this.err[i].label[j].addClassName('error_msg');
			}
		} else {
			this.err[i].label.addClassName('error_msg');
		}
		var errLI = document.createElement('li');
		errLI.innerHTML = this.err[i].msg;
		errUL.appendChild(errLI);
	}

	//append error list to form
	this.el.insertBefore(errUL, this.el.firstChild);
}

//-------Feedback form Validation-------

function GE_contact_form_validation() {
	GE_form_validation.call(this);
}

GE_contact_form_validation.prototype = new GE_form_validation();

GE_contact_form_validation.prototype.init = function(el) {
	//initialize form element and error array
	GE_form_validation.prototype.init.call(this, el);

	//declare error messages
	this.sInvalidRelationship = "Please tell us your Relationship to GE.";
	this.sInvalidEmail = "Please enter a valid Email (such as john.doe@mail.com).";
	this.sInvalidComment = "Please enter your Comments or Questions.";
	this.sInvalidSubject = "Please select a Subject.";
	this.sInvalidAreaOfInterest = "Please select your Area of Interest.";

	//create required field objects
	this.relationship = this.el.getInputs('radio','contact_type');
	this.email = this.el.getInputs('text','email')[0];
	this.comment = this.el.down('#contact_comments');
	this.subject = this.el.down('#contact_subject');
	// this.interest = this.el.down('#contact_area_of_interest');
}

GE_contact_form_validation.prototype.checkFields = function() {
	//validate relationship
	var bRelationship = false;
	for (var i=0;i<this.relationship.length;i++) {
		if (this.relationship[i].checked) { bRelationship = true; }
	}
	if (!bRelationship) {
		this.logErr(this.relationship, $('contact_subject_label'), this.sInvalidRelationship);
	}

	//validate subject
	if (this.subject.selectedIndex==0) {
		this.logErr(this.subject, $('contact_subject_label'), this.sInvalidSubject);
	}

	//validate area of interest
	// if (this.subject.selectedIndex>0 && this.interest.selectedIndex==0) {
	// 	this.logErr(this.interest, $('aoi_label'), this.sInvalidAreaOfInterest);
	// }

	//validate email address
	if (this.IsWhitespace(this.email.getValue()) || !this.CheckEmailAddress(this.email)) {
		this.logErr(this.email, $('email_label'), this.sInvalidEmail);
	}

	//validate comments
	if (this.IsWhitespace(this.comment.getValue()) || this.comment.getValue()=='Comments...') {
		this.logErr(this.comment, $('contact_comments_label'), this.sInvalidComment);
	}
}

//-------Submit Ideas form Validation-------

function GE_form_comp_validation() {
	GE_form_validation.call(this);
}

GE_form_comp_validation.prototype = new GE_form_validation();

GE_form_comp_validation.prototype.init = function(el) {
	//initialize form element and error array
	GE_form_validation.prototype.init.call(this, el);

	//declare error messages
	this.sInvalidTitleIdea = "Please enter a Title of Idea.";
	this.sInvalidFirstName = "Please enter your First Name.";
	this.sInvalidLastName = "Please enter your Last Name.";
	this.sInvalidStreet = "Please enter a Street Address.";
	this.sInvalidState = "Please select a State or enter Not Applicable if this does not pertain to you.";
	this.sInvalidCity = "Please enter a City.";
	this.sInvalidZip = "Please enter a Zip.";
	this.sInvalidCountry = "Please select a Country.";
	this.sInvalidEmail = "Please enter a valid Email (such as john.doe@mail.com).";
	this.sInvalidPhone = "Please enter a valid Phone Number.";
	this.sInvalidGEBusiness = "Please select a GE Component.";
	this.sInvalidDescription = "Please enter a Description.";
	this.sInvalidLongDescription = "Please enter 4000 characters or less for the Description.";

	//create required field objects
	this.ideatitle = this.el.getInputs('text','idea_title')[0];
	this.firstname = this.el.getInputs('text','firstname')[0];
	this.lastname = this.el.getInputs('text','lastname')[0];
	this.street = this.el.getInputs('text','address')[0];
	this.city = this.el.getInputs('text','city')[0];
	this.state = this.el.getInputs('text','state')[0];
	this.zip = this.el.getInputs('text','zip')[0];
	this.country = this.el.down('#sai_country');
	this.phone = this.el.getInputs('text','phone')[0];
	this.email = this.el.getInputs('text','email')[0];
	this.gebusiness = this.el.down('#sai_gebusiness');
	this.description = this.el.down('#sai_description');
	//this.interest = this.el.down('#contact_area_of_interest');
}

GE_form_comp_validation.prototype.checkFields = function() {

	//validate Title of Idea
	if (this.IsWhitespace(this.ideatitle.getValue())) {
		this.logErr(this.ideatitle, $('idea_title_label'), this.sInvalidTitleIdea);
	}

	//validate First Name
	if (this.IsWhitespace(this.firstname.getValue())) {
		this.logErr(this.firstname, $('first_name_label'), this.sInvalidFirstName);
	}

	//validate Last Name
	if (this.IsWhitespace(this.lastname.getValue())) {
		this.logErr(this.lastname, $('last_name_label'), this.sInvalidLastName);
	}

	//validate Street Address
	if (this.IsWhitespace(this.street.getValue())) {
		this.logErr(this.street, $('address_label'), this.sInvalidStreet);
	}

	//validate City
	if (this.IsWhitespace(this.city.getValue())) {
		this.logErr(this.city, $('city_label'), this.sInvalidCity);
	}

	//validate State
	if (this.IsWhitespace(this.state.getValue())) {
		this.logErr(this.state, $('state_label'), this.sInvalidState);
	}

	//validate Zip
	if (this.IsWhitespace(this.zip.getValue())) {
		this.logErr(this.zip, $('zip_label'), this.sInvalidZip);
	}

	//validate Country
	if (this.country.selectedIndex==0) {
		this.logErr(this.country, $('country_label'), this.sInvalidCountry);
	}

	//validate email address
	if (this.IsWhitespace(this.email.getValue()) || !this.CheckEmailAddress(this.email)) {
		this.logErr(this.email, $('email_label'), this.sInvalidEmail);
	}

	//validate phone
	if (!this.IsPhone(this.phone.getValue())) {
//		if (!this.IsWhitespace(this.phone.getValue()))
//			{
			this.logErr(this.phone, $('phone_label'), this.sInvalidPhone);
//			}
	}

	//validate GE Business
	if (this.gebusiness.selectedIndex==0) {
		this.logErr(this.gebusiness, $('gebusiness_label'), this.sInvalidGEBusiness);
	}

	//validate Description
	if (this.IsWhitespace(this.description.getValue())) {
		this.logErr(this.description, $('description_label'), this.sInvalidDescription);
	}

	var string = this.description.getValue();
	if (string.length> 4000)
		{
		this.logErr(this.description, $('description_label'), this.sInvalidLongDescription);
		}


}

//-------Citizenship Order form Validation-------

function GE_citizenship_order_validation() {
	GE_form_validation.call(this);
}

GE_citizenship_order_validation.prototype = new GE_form_validation();

GE_citizenship_order_validation.prototype.init = function(el) {
	//initialize form element and error array
	GE_form_validation.prototype.init.call(this, el);

	//declare error messages
	this.sInvalidFirstName = "Please enter your First Name.";
	this.sInvalidLastName = "Please enter your Last Name.";
	this.sInvalidAddress = "Please enter your Address.";
	this.sInvalidCity = "Please enter a City.";
	this.sInvalidState = "Please enter a State.";
	this.sInvalidZip = "Please enter a Zip Code.";
	this.sInvalidCountry = "Please select a Country.";
	this.sInvalidPhone = "Please enter a valid Phone Number.";
	this.sInvalidEmail = "Please enter a valid Email (such as john.doe@mail.com).";
	this.sInvalidQuantity = "Please enter a Quantity.";
	this.sInvalidShipping = "Please enter a Shipping Code.";

	//create required field objects
	this.firstname = this.el.getInputs('text','txtcomment1121555')[0];
	this.lastname = this.el.getInputs('text','txtcomment1121556')[0];
	this.address = this.el.getInputs('text','txtcomment1121558')[0];
	this.city = this.el.getInputs('text','txtcomment1121571')[0];
	this.state = this.el.getInputs('text','txtcomment1121572')[0];
	this.zip = this.el.getInputs('text','txtcomment1121574')[0];
	this.country = this.el.down('#contact_country');
	this.phone = this.el.getInputs('text','txtcomment1121606')[0];
	this.email = this.el.getInputs('text','txtcomment1121607')[0];
	this.quantity = this.el.getInputs('text','txtcomment1121609')[0];
	this.shipping = this.el.getInputs('text','txtcomment1121610')[0];
	this.countryname = this.el.getInputs('hidden','countryName')[0];
}

GE_citizenship_order_validation.prototype.checkFields = function() {

	//validate First Name
	if (this.IsWhitespace(this.firstname.getValue())) {
		this.logErr(this.firstname, $('first_name_label'), this.sInvalidFirstName);
	}

	//validate Last Name
	if (this.IsWhitespace(this.lastname.getValue())) {
		this.logErr(this.lastname, $('last_name_label'), this.sInvalidLastName);
	}

	//validate Street Address
	if (this.IsWhitespace(this.address.getValue())) {
		this.logErr(this.address, $('address_label'), this.sInvalidAddress);
	}

	//validate City
	if (this.IsWhitespace(this.city.getValue())) {
		this.logErr(this.city, $('city_label'), this.sInvalidCity);
	}

	//validate State
	if (this.IsWhitespace(this.state.getValue())) {
		this.logErr(this.state, $('state_label'), this.sInvalidState);
	}

	//validate Zip
	if (this.IsWhitespace(this.zip.getValue())) {
		this.logErr(this.zip, $('zip_label'), this.sInvalidZip);
	}

	//validate Country
	if (this.country.selectedIndex==0) {
		this.logErr(this.country, $('country_label'), this.sInvalidCountry);
	}

	this.countryname.value = this.country.options[this.country.selectedIndex].text;

	//validate email address
	if (this.IsWhitespace(this.email.getValue()) || !this.CheckEmailAddress(this.email)) {
		this.logErr(this.email, $('email_label'), this.sInvalidEmail);
	}

	//validate phone
	if (!this.IsPhone(this.phone.getValue())) {
//		if (!this.IsWhitespace(this.phone.getValue()))
//			{
			this.logErr(this.phone, $('phone_label'), this.sInvalidPhone);
//			}
	}

	//validate Quantity
	if (this.IsWhitespace(this.quantity.getValue())) {
		this.logErr(this.quantity, $('quantity_label'), this.sInvalidQuantity);
	}

	//validate Shipping if quantity is more than 3

	if (this.quantity.getValue()>3)
		{
			if (this.IsWhitespace(this.shipping.getValue())) {
				this.logErr(this.shipping, $('shipping_label'), this.sInvalidShipping);
			}
		}

}


//-------Annual Report Sign up form Validation-------

function GE_annual_form_validation() {
	GE_form_validation.call(this);
}

GE_annual_form_validation.prototype = new GE_form_validation();

GE_annual_form_validation.prototype.init = function(el) {
	//initialize form element and error array
	GE_form_validation.prototype.init.call(this, el);

	//declare error messages
	this.sInvalidForm = "Please enter a Social Security Number or Account Information."
	this.sInvalidSSN = "Please enter a valid Social Security Number.";
	this.sInvalidAccount = "Please enter an Account Number.";
	this.sInvalidName = "Please enter the Name(s) on Account.";

	//create required field objects
	this.accountNumber = this.el.getInputs('text','account_number')[0];
	this.accountName = this.el.getInputs('text','names')[0];
	this.ssn1 = this.el.getInputs('text','ssn_1')[0];
	this.ssn2 = this.el.getInputs('text','ssn_2')[0];
	this.ssn3 = this.el.getInputs('text','ssn_3')[0];
	this.ss_number = this.el.getInputs('hidden','ss_number')[0];
}

GE_annual_form_validation.prototype.checkFields = function() {

	var bInvalidForm = false;

	//check for SSN
	//if (this.IsWhitespace(this.ssn1.getValue()) || this.IsWhitespace(this.ssn2.getValue()) || this.IsWhitespace(this.ssn3.getValue())) {
   if (this.IsWhitespace(this.ss_number.getValue())) {
		bInvalidForm = true;
	} else {
		//validate SSN
		//if (!this.IsNumeric(this.ssn1.getValue()) || !this.IsNumeric(this.ssn2.getValue()) || !this.IsNumeric(this.ssn3.getValue()) || this.ssn1.getValue().length<3 || this.ssn2.getValue().length<2 || this.ssn3.getValue().length<4) {
	   if (!this.IsNumeric(this.ss_number.getValue()) || this.ss_number.getValue().length<10) {
			//log error for invalid/missing SSN
			this.logErr([this.ss_number], $('form_ssn_label'), this.sInvalidSSN);
		}
		else {
			this.ssn1.value = this.ss_number.getValue().substring(0,2);
			this.ssn2.value = this.ss_number.getValue().substring(3,4);
			this.ssn3.value = this.ss_number.getValue().substring(5,8);
		}
		bInvalidForm = false;
	}

	if (bInvalidForm) {
		//check for account info
		if (this.IsWhitespace(this.accountNumber.getValue()) && this.IsWhitespace(this.accountName.getValue())) {
			bInvalidForm = true;
		} else {
			//validate account number
			if (!this.IsNumeric(this.accountNumber.getValue())) {
				//log error for invalid account number
				this.logErr(this.accountNumber, $('form_account_label'), this.sInvalidAccount);
			}
			//validate account name
			if (this.IsWhitespace(this.accountName.getValue())) {
				//log error for invalid account name
				this.logErr(this.accountName, $('form_name_label'), this.sInvalidName);
			}
			bInvalidForm = false;
		}
	}

	//report invalid form
	if (bInvalidForm) {
		//log error for missing account name and number
		this.logErr([this.accountNumber,this.accountName,this.ssn1,this.ssn2,this.ssn3], [$('form_account_label'),$('form_name_label'),$('form_ssn_label')], this.sInvalidForm);
	}
}

//-------Investors Alert form Validation-------

function GE_investor_alert_signup_validation() {
	GE_form_validation.call(this);
}

GE_investor_alert_signup_validation.prototype = new GE_form_validation();

GE_investor_alert_signup_validation.prototype.init = function(el) {
	//initialize form element and error array
	GE_form_validation.prototype.init.call(this, el);

	//declare error messages
	this.sInvalidForm = "Please enter a Full Name or Email."
	this.sInvalidName = "Please enter a valid Full Name.";
	this.sInvalidEmail = "Please enter a valid Email address.";

	//create required field objects
	this.signup_name = this.el.getInputs('text','txtcomment704606')[0];
	this.signup_email = this.el.getInputs('text','txtcomment704607')[0];

}

GE_investor_alert_signup_validation.prototype.checkFields = function() {

	//validate email address
	if (this.IsWhitespace(this.signup_email.getValue()) || !this.CheckEmailAddress(this.signup_email)) {
		this.logErr(this.signup_email, $('signup_email_label'), this.sInvalidEmail);
	}


}

//-------Investors Retiree form Validation-------

function GE_retiree_form_validation() {
	GE_form_validation.call(this);
}

GE_retiree_form_validation.prototype = new GE_form_validation();

GE_retiree_form_validation.prototype.init = function(el) {
	//initialize form element and error array
	GE_form_validation.prototype.init.call(this, el);

	//declare error messages
	this.sInvalidForm = "Please enter a First Name, Last Name or Email."
	this.sInvalidfName = "Please enter a valid First Name.";
	this.sInvalidlName = "Please enter a valid Last Name.";
	this.sInvalidSSO = "Please enter a valid GE Single Sign On (SSO). (Note: this should be a 9 digit number)";
	this.sInvalidEmail = "Please enter a valid Email address.";

	//create required field objects
	this.contact_first_name = this.el.getInputs('text','txtcomment2536015')[0];
	this.contact_last_name = this.el.getInputs('text','txtcomment2536016')[0];
	this.contact_sso = this.el.getInputs('text','txtcomment2537987')[0];
	this.contact_email = this.el.getInputs('text','txtcomment2536017')[0];

}

GE_retiree_form_validation.prototype.checkFields = function() {

	//validate email address
	if (this.IsWhitespace(this.contact_email.getValue()) || !this.CheckEmailAddress(this.contact_email)) {
		this.logErr(this.contact_email, $('email_label'), this.sInvalidEmail);
	}
		
	//validate firts name
	if (this.IsWhitespace(this.contact_first_name.getValue())) {
		this.logErr(this.contact_first_name, $('first_name_label'), this.sInvalidfName);
	}
	
    //validate last name
	if (this.IsWhitespace(this.contact_last_name.getValue())) {
		this.logErr(this.contact_last_name, $('last_name_label'), this.sInvalidlName);
	}
	
    //validate sso
	if (this.IsWhitespace(this.contact_sso.getValue()) || !this.IsNumeric(this.contact_sso.getValue()) || this.contact_sso.getValue().length<9 || this.contact_sso.getValue().length>9) {
		this.logErr(this.contact_sso, $('sso_label'), this.sInvalidSSO);
	}
}


//initialize the form validator
GE_Main.mapCSSToFn('bhv_contact_form',
   function(el) {
      new GE_contact_form_validation().init(el);
   }
);

GE_Main.mapCSSToFn('bhv_annual_form',
   function(el) {
      new GE_annual_form_validation().init(el);
   }
);


GE_Main.mapCSSToFn('bhv_investor_alert_signup',
   function(el) {
      new GE_investor_alert_signup_validation().init(el);
   }
);

GE_Main.mapCSSToFn('bhv_form_comp',
   function(el) {
      new GE_form_comp_validation().init(el);
   }
);

GE_Main.mapCSSToFn('bhv_citizenship_order_form',
   function(el) {
      new GE_citizenship_order_validation().init(el);
   }
);

GE_Main.mapCSSToFn('bhv_retiree_form',
   function(el) {
      new GE_retiree_form_validation().init(el);
   }
);
