// FORM VALIDATION
function FormValidation(FormObject,RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix,ValidationMessageID,DefaultValues,RequiredID) {
	
	var Validation = true;
	
	var MatchRequiredID = 'RQ';
	if(RequiredID != '') {
		MatchRequiredID = RequiredID;
	} 

	for (var i = 0; i < FormObject.length; i++) {
		if (FormObject[i].id.match(MatchRequiredID)) {
				
				// EMAIL
				if (FormObject[i].id.match('Email')) {
					if(FormObject[i].type == 'textarea') {
						var Emails = FormObject[i].value.split(',');
						for (var e = 0; e < Emails.length; e++) {
							Emails[e] = Emails[e].replace(/^\s+|\s+$/g,"");
							var ValidateEmail = Emails[e].match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
							if (!ValidateEmail) {
								FromValidationFalse('',FormObject[i].id,FormObject[i],RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix);
								Validation = false;
							}	
						}					
					} else {				
						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],RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix);
							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],RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix);
					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],RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix);
					Validation = false;
					}	
				}					

				// SELECT 
				else if (FormObject[i].type == 'select-one' || FormObject[i].type == 'select') {
					if (FormObject[i].value == '') {
					FromValidationFalse('',FormObject[i].id,FormObject[i],RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix);
					Validation = false;
					}
				}				

				// FILE 
				else if (FormObject[i].type == 'file') {
					if (FormObject[i].value == '') {
					FromValidationFalse('',FormObject[i].id,FormObject[i],RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix);
					Validation = false;
					}	
				}			

				// TEXT & TEXT AREA
				else if (FormObject[i].type == 'password' || FormObject[i].type == 'text' || FormObject[i].type == 'textarea' || FormObject[i].type == 'hidden') {					

					// 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],RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix);
						Validation = false;
						}
					} 					

					else if (FormObject[i].value == '' || (FormObject[i].value == FormObject[i].defaultValue && DefaultValues)) {
						FromValidationFalse('',FormObject[i].id,FormObject[i],RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix);
						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'),RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix);
						Validation = false;
						}
					}
				}

		}

		// VERIFICATION
		var Verificaiton = false;
		var VerificaitonObject = '';
		if (FormObject[i].name == 'Verification') {
			if (FormObject[i].value == '') {
				FromValidationFalse('',FormObject[i].name,FormObject[i],RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix);
				Validation = false;
			} else {
				VerificaitonObject = FormObject[i]
				Verificaiton = true;
			}
		}	

	}	

	// VALIDATION WARNING
	if (ValidationMessageID != '' && document.getElementById(ValidationMessageID)) {
		if (!Validation) {
		document.getElementById(ValidationMessageID).style.display = "block";
		} else {
		document.getElementById(ValidationMessageID).style.display = "none";
		}
	} 
	
	// CAPTCHA
	if(Verificaiton && Validation) {		
		var Post = new HTTPRequest ();
		Post.URL = '/designedit_inc/captcha.php';
		Post.PraseXML = false;	
		Post.Parameters = '&verification='+VerificaitonObject.value;
		Post.Completed = function(Response) {
							if(Response == 'true') {
								FormObject.submit();
							} else {
								var Values = {
									'image': 'Sorry',
									'message': 'Please note that certian items on this page were not filled in properly. Items with an * are required and must be completed. Refer to the orange highlighted items to make the correct adjustments.'
								};	
								PopUp('Small','Open',Values);
								FromValidationFalse('',VerificaitonObject.name,VerificaitonObject,RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix);								
							}
					};	
		Post.Error = function (Response) {
					};	
		Post.POST();
		return Validation;	
	} else {
		return Validation;
	}
}

// FORM RESPONSE
function FormValidationButton(FormObject,RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix,ValidationMessageID,DefaultValues,RequiredID) {
	if (FormValidation(FormObject,RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix,ValidationMessageID,DefaultValues,RequiredID)) {
		FormObject.submit();	
	}
}

function RegistraionForm(FormObject,RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix,ValidationMessageID,DefaultValues) {
	
	var Valid = false;
	var Message = '';
	for (var i = 0; i < FormObject.length; i++) {
		if (FormObject[i].id.match('Class_') && FormObject[i].type == 'checkbox') {
			if(FormObject[i].checked) {
				if(parseInt(document.getElementById(FormObject[i].id+'_Q').value) == 0) {
					Message += '<br />Plese enter quantity for class: '+document.getElementById(FormObject[i].id+'_T').value+'<br />';	
				} else if(parseInt(document.getElementById(FormObject[i].id+'_Q').value) > parseInt(document.getElementById(FormObject[i].id+'_A').value)) {
					Message += '<br />Only '+document.getElementById(FormObject[i].id+'_A').value+' spots available for class: '+document.getElementById(FormObject[i].id+'_T').value+'<br />';	
				} else {
					Valid = true;	
				}
			}
		}
	}
	if(Message !='') {
		document.getElementById('RequiredClassMessage').innerHTML = '<strong>'+Message+'</strong>';
	} else if (!Valid) {
		document.getElementById('RequiredClassMessage').innerHTML = '<br /><strong>Please Select Class(s)</strong><br />';	
	} else {
		document.getElementById('RequiredClassMessage').innerHTML = '';	
	}
	if (Valid) {
		Valid = FormValidation(FormObject,RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix,ValidationMessageID,DefaultValues)	
	}
	return Valid;
}

// FORM RESPONSE
function FormResponse(FrameObject,ID) {
	document.getElementById('formcreator'+ID).innerHTML = window.frames[FrameObject].document.getElementById('message').value;
	location.href = location.href+'#f'+ID;
}

// MARK FAILED OBJECT
function FromValidationFalse(Type,ObjectID,FormObject,RequiredColor,Color,ValidationRequiredClass,ValidationClass,ValidationPostfix) {
	
	if (RequiredColor != '' && document.getElementById(ObjectID+ValidationPostfix)) {	
		document.getElementById(ObjectID+ValidationPostfix).style.color = RequiredColor;
	}	
	
	if (Color != '' && document.getElementById(ObjectID+ValidationPostfix)) {
		if (FormObject.defaultValue != '') {
		FormObject.onfocus = new Function("if(this.value==this.defaultValue) this.value='';document.getElementById('"+ObjectID+ValidationPostfix+"').style.color = '"+Color+"'");	
		} else {
		FormObject.onfocus = new Function("document.getElementById('"+ObjectID+ValidationPostfix+"').style.color = '"+Color+"'");
		}
	}
		
	if (ValidationRequiredClass != '' && document.getElementById(ObjectID+ValidationPostfix)) {	
		document.getElementById(ObjectID+ValidationPostfix).className = ValidationRequiredClass;
	}
	
	if (ValidationClass != '' && document.getElementById(ObjectID+ValidationPostfix)) {
		if (FormObject.defaultValue != '') {
		FormObject.onfocus = new Function("if(this.value==this.defaultValue) this.value='';document.getElementById('"+ObjectID+ValidationPostfix+"').className = '"+ValidationClass+"'");	
		} else {
		FormObject.onfocus = new Function("document.getElementById('"+ObjectID+ValidationPostfix+"').className = '"+ValidationClass+"'");
		}
	} else if (ValidationRequiredClass != '' && document.getElementById(ObjectID+ValidationPostfix)) {
		if (FormObject.defaultValue != '') {
		FormObject.onfocus = new Function("if(this.value==this.defaultValue) this.value='';document.getElementById('"+ObjectID+ValidationPostfix+"').className = ''");	
		} else {
		FormObject.onfocus = new Function("document.getElementById('"+ObjectID+ValidationPostfix+"').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;	
}
