// FORM VALIDATION
function FormValidation(FormObject) {
	var Validation = true;
	var ValidationMessageID = '';	
	var ValidationRequireClassName = '';	
	var ValidationClassName = '';	
	
	// GET VALIDATION REQUIRED CLASS
	if (FormObject['ValidationRequiredClass']) {
	ValidationRequireClassName = FormObject['ValidationRequiredClass'].value;
	}	
	
	// GET VALIDATION CLASS
	if (FormObject['ValidationClass']) {
	ValidationClassName = FormObject['ValidationClass'].value;
	}	
	
	for (var i = 0; i < FormObject.length; i++) {
		if (FormObject[i].id.match('RQ')) {
				// EMAIL
				if (FormObject[i].id.match('Email')) {
					var ValidateEmail = FormObject[i].value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
					if (!ValidateEmail) {
					FromValidationFalse('',FormObject[i].id,FormObject[i],ValidationRequireClassName,ValidationClassName);
					Validation = false;
					}
				} 
			
				// RADIO BUTTONS				
				else if (FormObject[i].type == 'radio') {	
					var ValidateRadioButton = false;				
					for(var r=0; r < FormObject[FormObject[i].name].length; r++) {
						if(FormObject[FormObject[i].name][r].checked) {
						ValidateRadioButton = true;
						break;
						}
					}	
					if (!ValidateRadioButton) {
					FromValidationFalse('',FormObject[i].id,FormObject[i],ValidationRequireClassName,ValidationClassName);
					Validation = false;
					}	
				}	
				
				// CHECKBOX				
				else if (FormObject[i].type == 'checkbox') {	
					var ValidateRadioButton = false;				
					for(var r=0; r < FormObject[FormObject[i].name].length; r++) {
						if(FormObject[FormObject[i].name][r].checked) {
						ValidateRadioButton = true;
						break;
						}
					}	
					if (!ValidateRadioButton) {
					FromValidationFalse('',FormObject[i].id,FormObject[i],ValidationRequireClassName,ValidationClassName);
					Validation = false;
					}	
				}	
				
				// SELECT 
				else if (FormObject[i].type == 'select-one' || FormObject[i].type == 'select') {
					if (FormObject[i].value == '') {
					FromValidationFalse('',FormObject[i].id,FormObject[i],ValidationRequireClassName,ValidationClassName);
					Validation = false;
					}	
				}
				
				// FILE 
				else if (FormObject[i].type == 'file') {
					if (FormObject[i].value == '') {
					FromValidationFalse('',FormObject[i].id,FormObject[i],ValidationRequireClassName,ValidationClassName);
					Validation = false;
					}	
				}
				
				// TEXT & TEXT AREA
				else if (FormObject[i].type == 'password' || FormObject[i].type == 'text' || FormObject[i].type == 'textarea') {
					
					// NUMBER OF CHARECTERS
					if (FormObject[i].id.match('#')) {
						var ObjectParts = FormObject[i].id.split('#');
						if (FormObject[i].value.length != parseInt(ObjectParts[1])) {
						FromValidationFalse('',ObjectParts[0].replace('_',''),FormObject[i],ValidationRequireClassName,ValidationClassName);
						Validation = false;
						}
					} 
					
					else if (!FormObject[i].id.match('Empty') && FormObject[i].value == '') {
						FromValidationFalse('',FormObject[i].id,FormObject[i],ValidationRequireClassName,ValidationClassName);
						Validation = false;						
					}
					
					// CONFIRM
					else if (document.getElementById(FormObject[i].id+'_Confirm')) {
						if (document.getElementById(FormObject[i].id+'_Confirm').value != FormObject[i].value ) {
						FromValidationFalse('',FormObject[i].id+'_Confirm',document.getElementById(FormObject[i].id+'_Confirm'),ValidationRequireClassName,ValidationClassName);
						Validation = false;
						}
					}			
					
				}
		}	
	
		// VERIFICATION
		if (FormObject[i].name == 'Verification') {
			if (FormObject[i].value == '') {
			FromValidationFalse('',FormObject[i].name,FormObject[i],ValidationRequireClassName,ValidationClassName);
			Validation = false;
			} else if (FormObject[i].value != '5') {
			FromValidationFalse('',FormObject[i].name,FormObject[i],ValidationRequireClassName,ValidationClassName);
			Validation = false;
			}
		}	
	
		// GET VALIDATION WARDING ID
		if (FormObject[i].name == 'ValidationMessageID') {
		ValidationMessageID = FormObject[i].value;
		}	
	}
	
	
	
	// VALIDATION WARNING
	if (ValidationMessageID != '') {
		if (!Validation) {
		document.getElementById(ValidationMessageID).style.display = "block";
		} else {
		document.getElementById(ValidationMessageID).style.display = "none";
		}
	} 
	
	return Validation;
}


// MARK FAILED OBJECT
function FromValidationFalse(Type,ObjectID,FormObject,ValidationRequireClassName,ValidationClassName) {
	if (ValidationRequireClassName != '') { 	
	document.getElementById(ObjectID+'_Title').className = ValidationRequireClassName;
	} else {
	document.getElementById(ObjectID+'_Title').className = 'required';	
	}
	if (ValidationClassName != '') {
	FormObject.onfocus = new Function("document.getElementById('"+ObjectID+"_Title').className = '"+ValidationClassName+"'");	
	} else {
	FormObject.onfocus = new Function("document.getElementById('"+ObjectID+"_Title').className = ''");		
	}
}

// CLEAN INPUT
function FormCleanInput(Type,FormObject) {
	if (Type == 'Numbers') {
	var CleanValue = FormObject.value.replace (/[~|%|^|*|(|)|{|}|:|;|\"|\'<|>|,|\/|`|\||\\|\-|]/gi,''); 
	CleanValue = CleanValue.replace (/[a-zA-Z]/gi,''); 	
	}	
FormObject.value = CleanValue;		
}

// FROM POPULATE
function FormPopulate(URL,Paremeters,SelectObject,ClearMessage) {
	var Length = document.getElementById(SelectObject).length;
	for (r=Length; r >= 0; r--) {
		 document.getElementById(SelectObject).remove(r);
	}
	document.getElementById(SelectObject).options[0] = new Option(ClearMessage,'');	
	
	var FormSelectPost = new HTTPRequest ();
	FormSelectPost.URL = URL;
	FormSelectPost.Parameters = Paremeters;;
	FormSelectPost.PraseXML = true;	
	FormSelectPost.Completed = function(Response) {
				for(i = 0; i < Response.getElementsByTagName("date").length; i++) {
					document.getElementById(SelectObject).options[i+1] = new Option(Response.getElementsByTagName("date")[i].childNodes[0].nodeValue, Response.getElementsByTagName("date")[i].childNodes[0].nodeValue);
				}
			};	
	FormSelectPost.Error = function (Response) {
			};	
	FormSelectPost.POST();	
	
}




