/*_------------------------------------------------_*/
function initForms() {
  var FS = {
	           submitForm: function(event) {
				  form = Event.element(event);
				  var allF = $A(form.getElements()).compact();
				  var rF = [];
				  var valid = true;
				  var eMailField = [], webAddress = [], passField = [], intOnlyF = [], atleast = [], odn = [], chk1= [];
				  var showMsg = '';
				  // find all the required fields and find out email and web address input fields
				  for (i =0; i < allF.size(); i++) {
					  allF[i].setStyle({borderColor:oldBorder});
					  if ( allF[i].hasClassName('required') ) { rF.push(allF[i]); }
					  if ( allF[i].hasClassName('intOnly') ) { intOnlyF.push(allF[i]); }
					  if ( allF[i].hasClassName('dn') ) { odn.push(allF[i]); }
					  if ( allF[i].hasClassName('mustCheck') ) { chk1.push(allF[i]); }
					  if ( allF[i].name.toLowerCase().include('mail') || allF[i].id.toLowerCase().include('mail') ) {
					    eMailField.push(allF[i]);
					  }
					  if ( allF[i].name.toLowerCase().include('web') || allF[i].id.toLowerCase().include('web') ) {
						webAddress.push(allF[i]);
					  }
					  //if ( (allF[i].type.toLowerCase() == 'password') ) {
						 // passField.push(allF[i]);
					  //}
					  if ( allF[i].className.include('atleast') ) { atleast.push(allF[i]); }
				  }
				  
				  
				  // check if the required fields are empty
				  if (rF.size() > 0 ) {
					 for ( i = 0; i < rF.size(); i++ ) {
						if ( rF[i].value.blank() ) {
						   rF[i].setStyle({borderColor:'#ff0000'});
						   Event.observe(rF[i], 'keyup', (function(event){elm = Event.element(event);$(elm).setStyle({borderColor: $(elm).value.blank()?'#ff0000':oldBorder});}));
						   valid = false;
						   showMsg = msg[0];
						}
					 } // end for
				  } // end if
				  
				  


                  // check if email or web address given and invalid
				  if ( valid ) { 
					  for (i =0; i < eMailField.size(); i++) {
						  if ( !eMailField[i].value.blank() ) {
						     if (!emailchar.test(eMailField[i].value)) {
							    eMailField[i].setStyle({borderColor:'#ff0000'});
								valid = false;
						     }
						  } // end if
					  } // end for
					  for (i =0; i < webAddress.size(); i++) {
						  if ( !webAddress[i].value.blank() ) {
						     if (!webchar.test(webAddress[i].value)) {
							    webAddress[i].setStyle({borderColor:'#ff0000'});
								valid = false;
						     } 
						  } // end if
					  }
					  showMsg = msg[1];
				  } // end if email check and web check
				  
				  // check if there is password value and retype password and if exist then check identicality
				  if ( valid ) {
					 if ( passField.size() > 1 )  {
						 if ( passField[0].value !== passField[1].value ) {
						    passField[0].setStyle({borderColor:'#ff0000'});
							passField[1].setStyle({borderColor:'#ff0000'});
							valid = false; 
							showMsg = msg[2];
						 }
					 }
				  } // end if password check
				  
				  // check if any integer/numeric only field has some other value
				  if ( valid ) {
					 for (i =0; i < intOnlyF.size(); i++) {
						 if ( !intOnlyF[i].value.blank() ) {
						    if (!intchar.test(intOnlyF[i].value)) {
							   intOnlyF[i].setStyle({borderColor:'#ff0000'});
							   valid = false; showMsg = msg[3];
							}
						 }
					 }
				  } // end check numeric only
				  
				  // check if any fields has less then required character
				  if ( valid ) {
					 for (i =0; i < atleast.size(); i++) {
						 if ( !atleast[i].value.blank() ) {
							len = atleast[i].className.gsub(/(.*?)atleast(\d+)/, function(mat){return mat[2];});
							if ( atleast[i].value.strip().length < parseInt(len) ) {
							   atleast[i].setStyle({borderColor:'#ff0000'});
							   valid = false; showMsg = msg[4];
							}
						 }
					 }
				  } // heck if any fields has less then required character
				  
				  // check if any fields value is not only digit and number
				  if ( valid ) {
					 for (i =0; i < odn.size(); i++) {
						 if ( !odn[i].value.blank() ) {
						    if (!intDigitchar.test(odn[i].value)) {
							   odn[i].setStyle({borderColor:'#ff0000'});
							   valid = false; showMsg = msg[5];
							}
						 }
					 }
				  } // heck if any fields has less then required character
				  
				  // check if the user check at least one checkbox 
				  if ( valid ) {
					 var f = 0;
					 for (i =0; i < chk1.size(); i++) {
						 if ( chk1[i].checked ) {
						    f = 1;
						 }
						 if (f) {
						   valid = true; break; 
						 }
						valid = false;
						showMsg = msg[6];
					 }
				  } // heck if any fields has less then required character
				  
				  
				  
				  if (!valid) { Event.stop(event); if ($('msgDiv')){$('msgDiv').update(showMsg);$('msgDiv').scrollTo(); } alert(showMsg); }
			   },
			   resetForm: function(event) {
				  Event.element(event).reset();
			   }
		   };
  FS.Submit = FS.submitForm.bindAsEventListener(FS);
  // find all the form in a document
  var forms = $A($$('form')).compact();
  for (i = 0; i < forms.size(); i++ ) {
	  Event.observe(forms[i], 'submit', FS.Submit); 
  }
}
/*_------------------------------------------------_*/
// global variable for email address pattern check
var emailchar = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i; // global variable for email address pattern check
var intchar = /^([\d-]+)$/i; // global variable for email address pattern check
var intDigitchar = /^([0-9A-Za-z]+)$/i; // global variable for email address pattern check

var webchar = new RegExp("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)"); // global variable for web address pattern check
var oldBorder = '#D7D7D7'; // empty field original color must be global variable
var msg = [
		    'Required Value Missing!!!\n Required Fields are RED bordered Now!!!',
			'Email Address and/or Web address seems Invalid \n Please check the RED bordered input fields and try again.',
			'Password Mismatched!!! \n Check again!!',
			'RED Color Field Needs Numeric Value but Given Value is not a Number!! \n Check and try again',
			'You have Given less than required character length!! \n Check and try again',
			'Only digit and numeric character are allowed in the red bordered field',
			'Check Atleast one checkbox of the related destination'
		   ];
document.observe('dom:loaded', initForms);