var formChecker = new Class(
{
	Implements : Options,
	
	options :
	{
		bodyHeight : '',
		bodyWidth : '',
        frm : "thisForm",
		ctr : 0,
		error : 0,
		val_date : "",
		elRequiredClone : "",
		elMaxClone : "",
		elAtLeastRequiredClone : "",
		fieldFxError : {"background-color" : "#ff0000"},
		fields :
		{
			choice_check : "",
			required : "",
			e_validate : "",
			alpha : "",
			alphanum : "",
			digit : "",
			nodigit : "",
			number : "",
			email : "",
			phone : "",
			mobile : "",
			fax : "",
			url : "",
			confirm : "",
			length_str : "",
			range_str : "",
			atleast_required : "",
			maximum : "",
			trim_spaces : ""
		},
		y_height : "",
		x_width : "",
		regexp :
		{
			required : /[^.*]/,
			alpha : /^[a-z ._-]+$/i,
			alphanum : /^[a-z0-9 ._-]+$/i,
			digit : /^[-+]?[0-9]+$/,
			nodigit : /^[^0-9]+$/,
			number : /^[-+]?\d*\.?\d+$/,
			email : /^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i,
			phone : /^[\d\s ().-]+$/,
			mobile : /^[\d\s ().-]+$/,
			fax : /^[\d\s ().-]+$/,
			url : /^((http|https|ftp)\:\/\/|(www\.))+[a-z0-9\-\.]+\.[a-z]{2,3}(:[a-z0-9]*)?\/?([a-z0-9\-\._\?\,\'\/\\\+&amp;%\$#\=~])*$/i
		}
	},

	register : function(el)
	{
		el.validation = [];
		el.getProperty("rel").split(' ').each(function(classX)
		{
			if(classX.match(/^validate(\[.+\])$/))
			{
				
				var atleastRequiredCtr = 1;
				var validators = eval(classX.match(/^validate(\[.+\])$/)[1]);
				
				for(var i = 0; i < validators.length; i++)
				{
					var cbCheckeds = false;
					var maxCheckeds = 0;
					
					
					
					if (validators[i].contains("e_validate"))
					{
						if($("e_validate").value==1){
							this.options.fields.e_validate = "<tr><td>Email address already exists." + "</td></tr>";
						}else{
							this.options.fields.e_validate = "";
						}

					}
					
					if (validators[i] == "required")
					{
						
						if ((el.type == "radio") || (el.type == "checkbox"))
						{
							var nlButtonGroup = this.form[el.getProperty("name")];
							el.group = nlButtonGroup;
							if (nlButtonGroup.length != undefined) {
								for(var ax = 0; ax < nlButtonGroup.length; ax++)
								{
									if(nlButtonGroup[ax].checked)
									{
										cbCheckeds = true;
									}
								}
							} else {
								if (nlButtonGroup.checked) {
									cbCheckeds = true;
								}
							}
							if(cbCheckeds == false)
							{
								if (this.options.elRequiredClone != el.title)
								{
									if (this.options.fields.required == "")
									{
										this.options.fields.required = "<tr><td>" + el.title + " is required." + "</td></tr>";
									}
									else
									{
										this.options.fields.required = this.options.fields.required + "<tr><td>" + el.title + " is required." + "</td></tr>";
									}
									this.options.elRequiredClone = el.title;
									this.options.error = this.options.error + 1;
								}
							}
						}
						if (el.value.length == 0)
						{
							if (this.options.fields.required == "")
							{
								this.options.fields.required = "<tr><td>" + el.title + " is required." + "</td></tr>";
							}
							else
							{
								this.options.fields.required = this.options.fields.required + "<tr><td>" + el.title + " is required." + "</td></tr>";
							}
							this.options.error = this.options.error + 1;
						}
					}
					

					if (validators[i].contains("choice_check"))
					{
						/*
					   for (var i=0; i < document.thisForm.answerkey.length; i++)
					   {
						   if (document.thisForm.answerkey[i].checked){
						       answerkey_value = (document.thisForm.answerkey[i].value);
						   }
					   }
					   */
						var template = $('choice_check').value;
						template = template.split(','); 
						var error_count = 0;
						var no_error_count = 0;
						for(x=0;x<template.length;x++){
							txt_value = $(template[x]).value.trim();
							if(txt_value==""){
								error_count +=1;
								//if(template[x]==answerkey_value){
								//   this.options.fields.choice_check = "<tr><td>Add at least 3 choices." + "</td></tr>";
								//}
							}else{
								no_error_count +=1;
							}
						}
						
						if(no_error_count<3){
							this.options.fields.choice_check = "<tr><td>Add at least 3 choices." + "</td></tr>";
							this.options.error = this.options.error + 1;
						}else{
							this.options.fields.choice_check = "";
						}
						
					}					
				
					
					if (validators[i].contains("atleast_required"))
					{
						var atleast_required_text = validators[i].split(":");
						if (atleast_required_text.length >= 2)
						{
							for (arr_required = 1; arr_required < atleast_required_text.length; arr_required++) {
								if ($(atleast_required_text[arr_required]).value == "") {
									atleastRequiredCtr = atleastRequiredCtr + 1;
								}
							}
								if (atleastRequiredCtr >= atleast_required_text.length) {
									if (this.options.elAtLeastRequiredClone != el.title)
									{
										if (this.options.fields.atleast_required == "")
										{
											this.options.fields.atleast_required = "<tr><td>At least one (1) " + el.title + " is required." + "</td></tr>";
										}
										else
										{
											this.options.fields.atleast_required = this.options.fields.atleast_required + "<tr><td>At least one (1) " + el.title + " is required." + "</td></tr>";
										}
										this.options.elAtLeastRequiredClone = el.title;
									}
									this.options.error = this.options.error + 1;
								}
						}

						
						if (atleast_required_text.length == 1)
						{
							if (($(atleast_required_text[1]).value == "") && (el.value == ""))
							{
								if (this.options.fields.atleast_required == "")
								{
									this.options.fields.atleast_required = "<tr><td>At least one (1) " + el.title + " is required." + "</td></tr>";
								}
								this.options.error = this.options.error + 1;
							}
						}
					}
					
					
					
					
					if (validators[i].contains("maximum"))
					{
						var maximum_text = validators[i].split(":");
						if (el.type == "checkbox")
						{
							var nlButtonGroup = this.form[el.getProperty("name")];
							el.group = nlButtonGroup;

							for(var ax = 0; ax < nlButtonGroup.length; ax++)
							{
								if(nlButtonGroup[ax].checked)
								{
									maxCheckeds = maxCheckeds + 1;
								}
							}

							if (maxCheckeds > 0)
							{
								if(maxCheckeds > maximum_text[1])
								{
									if (this.options.elMaxClone != el.title)
									{
										if (this.options.fields.maximum == "")
										{
											this.options.fields.maximum = "<tr><td>Please select a maximum of (" + maximum_text[1] + ") " + el.title + " only." + "</td></tr>";
										}
										else
										{
											this.options.fields.maximum = this.options.fields.maximum + "<tr><td>Please select a maximum of (" + maximum_text[1] + ") " + el.title + " only." + "</td></tr>";
										}
										this.options.elMaxClone = el.title;
										this.options.error = this.options.error + 1;
									}
								}
							}
						}
					}
					

								
					
					if (validators[i] == "email")
					{
                        if (el.value != "")
						{
							el.value = el.value.trim();
                            if (!this.options.regexp.email.test(el.value))
							{
								if (this.options.fields.email == "")
								{
									this.options.fields.email = "<tr><td>" + el.title + " is invalid." + "</td></tr>";
                                }
								else
								{
									this.options.fields.email = this.options.fields.email + "<tr><td>" + el.title + " is invalid." + "</td></tr>";
								}
								this.options.error = this.options.error + 1;
							}
						}
					}
					if (validators[i].contains("confirm"))
					{
						var confirm_text = validators[i].split(":");
						if (($(confirm_text[1]).value != "") && (el.value != ""))
						{
							if ($(confirm_text[1]).value != el.value)
							{
								if (this.options.fields.confirm == "")
								{
									this.options.fields.confirm = "<tr><td>" + $(confirm_text[1]).title + " and " + el.title + " do not match." + "</td></tr>";
								}
								else
								{
									this.options.fields.confirm = this.options.fields.confirm + "<tr><td>" + $(confirm_text[1]).title + " and " + el.title + " do not match." + "</td></tr>";
								}
								this.options.error = this.options.error + 1;
							}
						}
					}
					if (validators[i].contains("length"))
					{
						var length_range = eval(validators[i].match(/^length(\[.+\])$/)[1])
						if (el.value != "")
						{
							if (el.value.length < length_range[0])
							{
								if (this.options.fields.length_str == "")
								{
									this.options.fields.length_str = "<tr><td>" + el.title + " should be more than " + length_range[0] +  " characters." + "</td></tr>";
								}
								else
								{
									this.options.fields.length_str = this.options.fields.length_str + "<tr><td>" + el.title + " should be more than " + length_range[0] + " characters." + "</td></tr>";
								}
							}
							if (el.value.length > length_range[1])
							{
								if (this.options.fields.length_str == "")
								{
									this.options.fields.length_str = "<tr><td>" + el.title + " should not be more than " + length_range[1] + " characters." + "</td></tr>";
								}
								else
								{
									this.options.fields.length_str = this.options.fields.length_str + "<tr><td>" + el.title + " should not be more than " + length_range[1] + " characters." + "</td></tr>";
								}
								this.options.error = this.options.error + 1;
							}
						}
					}
					if (validators[i].contains("range"))
					{
						var range_text = validators[i].split(":");
						var includeBlank = "";
						var includeValidate = true;

						if (range_text.length == 4)
						{
							var includeBlank = range_text[3];
						}

						if (range_text[1] == "gt")
						{
							if (this.options.regexp.digit.test(el.value) && this.options.regexp.digit.test($(range_text[2]).value))
							{
								var val_from = parseInt(el.value);
								var val_to = parseInt($(range_text[2]).value);
							}
							else
							{
								var val_from = el.value;
								var val_to = $(range_text[2]).value;
							}

							if (includeBlank != "")
							{
								if ($(range_text[2]).value == "")
								{
									includeValidate = false;
								}
							}

							if (includeValidate == true)
							{
								if (val_from > val_to)
								{
									if (this.options.fields.range_str == "")
									{
										this.options.fields.range_str = "<tr><td>" + el.title + " should not be greater than " + $(range_text[2]).title + ".</td></tr>";
									}
									else
									{
										this.options.fields.range_str = this.options.fields.range_str + "<tr><td>" + el.title + " should not be greater than " + $(range_text[2]).title + ".</td></tr>";
									}
									this.options.error = this.options.error + 1;

								}
							}
						}
						else if (range_text[1] == "lt")
						{
							if (el.value < $(range_text[2]).value)
							{
								if (this.options.fields.range_str == "")
								{
									this.options.fields.range_str = "<tr><td>" + el.title + " should not be less than " + $(range_text[2]).title + ".</td></tr>";
								}
								else
								{
									this.options.fields.range_str = this.options.fields.range_str + "<tr><td>" + el.title + " should not be less than " + $(range_text[2]).title + ".</td></tr>";
								}
								this.options.error = this.options.error + 1;

							}
						}
						else if (range_text[1] == "eq")
						{
							if (el.value = $(range_text[2]).value)
							{
								if (this.options.fields.range_str == "")
								{
									this.options.fields.range_str = "<tr><td>" + el.title + " should not be equal to " + $(range_text[2]).title + ".</td></tr>";
								}
								else
								{
									this.options.fields.range_str = this.options.fields.range_str + "<tr><td>" + el.title + " should not be equal to " + $(range_text[2]).title + ".</td></tr>";
								}
								this.options.error = this.options.error + 1;

							}
						}
						else if (range_text[1] == "gteq")
						{
							if (el.value >= $(range_text[2]).value)
							{
								if (this.options.fields.range_str == "")
								{
									this.options.fields.range_str = "<tr><td>" + el.title + " should not be greater than or equal to " + $(range_text[2]).title + ".</td></tr>";
								}
								else
								{
									this.options.fields.range_str = this.options.fields.range_str + "<tr><td>" + el.title + " should not be greater than or equal to " + $(range_text[2]).title + ".</td></tr>";
								}
								this.options.error = this.options.error + 1;

							}
						}
						else if (range_text[1] == "lteq")
						{
							if (el.value <= $(range_text[2]).value)
							{
								if (this.options.fields.range_str == "")
								{
									this.options.fields.range_str = "<tr><td>" + el.title + " should not be less than or equal to " + $(range_text[2]).title + ".</td></tr>";
								}
								else
								{
									this.options.fields.range_str = this.options.fields.range_str + "<tr><td>" + el.title + " should not be less than or equal to " + $(range_text[2]).title + ".</td></tr>";
								}
								this.options.error = this.options.error + 1;

							}
						}
					}
					if (validators[i] == "phone")
					{
						if (this.form.txt_phone_num.value.length == 0 && this.form.txt_mobile_num.value.length==0) {
							this.options.fields.phone = this.options.fields.phone + " At least one (1) contact number is required.";
							this.options.error = this.options.error + 1;
						}
					}
					if (validators[i] == "alpha")
					{
						if (el.value != "")
						{
							if (!this.options.regexp.alpha.test(el.value))
							{
								if (this.options.fields.alpha == "")
								{
									this.options.fields.alpha = "<tr><td>" + el.title + " accepts characters only." + "</td></tr>";
								}
								else
								{
									this.options.fields.alpha = this.options.fields.alpha + "<tr><td>" + el.title + " accepts characters only." + "</td></tr>";
								}
								this.options.error = this.options.error + 1;
							}
						}
					}
					if (validators[i] == "alphanum")
					{
						if (el.value != "")
						{
							if (!this.options.regexp.alphanum.test(el.value))
							{
								if (this.options.fields.alphanum == "")
								{
									this.options.fields.alphanum = "<tr><td>" + el.title + " accepts alphanumeric characters only." + "</td></tr>";
								}
								else
								{
									this.options.fields.alphanum = this.options.fields.alphanum + "<tr><td>" + el.title + " accepts alphanumeric characters only." + "</td></tr>";
								}
								this.options.error = this.options.error + 1;
							}
						}
					}
					if (validators[i] == "digit")
					{
						if (el.value != "")
						{
							if (!this.options.regexp.digit.test(el.value))
							{
								if (this.options.fields.digit == "")
								{
									this.options.fields.digit =  "<tr><td>" + el.title + " accepts digits only." + "</td></tr>";
								}
								else
								{
									this.options.fields.digit = this.options.fields.digit + "<tr><td>" + el.title + " accepts digits only." + "</td></tr>";
								}
								this.options.error = this.options.error + 1;
							}
						}
					}
					if (validators[i] == "date")
					{
						if(!el.value.contains("/"))
						{
							if (el.getProperty("class") == "date datepicker_calendar")
							{
								if (this.options.val_date == "")
								{
									this.options.val_date = el.value;
								}
								else
								{
									this.options.val_date = this.options.val_date + "," + el.value;
								}
							}
						}
					}
					if (validators[i] == "url")
					{
						if (el.value != "")
						{
							if (!this.options.regexp.url.test(el.value))
							{
								if (this.options.fields.url == "")
								{
									this.options.fields.url = "<tr><td>" + el.title + " is an invalid url." + "</td></tr>";
								}
								else
								{
									this.options.fields.url = this.options.fields.url + "<tr><td>" + el.title + " is an invalid url." + "</td></tr>";
								}
								this.options.error = this.options.error + 1;
							}
						}
					}

					if (validators[i] == "trim_spaces")
					{
						if (el.value != "") {
							if (el.value.contains(" ")) {
								if (this.options.fields.trim_spaces == "")
								{
									this.options.fields.trim_spaces = "<tr><td>" + el.title + " cannot contain spaces." + "</td></tr>";
								}
								else
								{
									this.options.fields.trim_spaces = this.options.fields.trim_spaces + "<tr><td>" + el.title + " cannot contain spaces." + "</td></tr>";
								}
								this.options.error = this.options.error + 1;
							}
						}
					}
				}
			}
		}, this);

		
		
		if (this.options.error > 0)
		{
			return false;
		}
		else
		{
		
		
			return true;
		}
	},

	notifier : function()
	{
		var setHtml = "<table cellpadding='0' cellspacing='0' border='0' class='errorHeading'>";
		
		
		if (this.options.fields.e_validate != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.e_validate;
			}
			else
			{
				setHtml = setHtml + this.options.fields.e_validate;
			}
		}



		
		if (this.options.fields.required != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.required;
			}
			else
			{
				setHtml = setHtml + this.options.fields.required;
			}
		}
		
		if (this.options.fields.choice_check != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.choice_check;
			}
			else
			{
				setHtml = setHtml + this.options.fields.choice_check;
			}
		}
		

		if (this.options.fields.email != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.email;
			}
			else
			{
				setHtml = setHtml + this.options.fields.email;
			}
		}
		if (this.options.fields.confirm != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.confirm;
			}
			else
			{
				setHtml = setHtml + this.options.fields.confirm;
			}
		}
		if (this.options.fields.length_str != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.length_str;
			}
			else
			{
				setHtml = setHtml + this.options.fields.length_str;
			}
		}
		if (this.options.fields.range_str != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.range_str;
			}
			else
			{
				setHtml = setHtml + this.options.fields.range_str;
			}
		}
		if (this.options.fields.maximum != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.maximum;
			}
			else
			{
				setHtml = setHtml + this.options.fields.maximum;
			}
		}
		if (this.options.fields.phone != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.phone;
			}
			else
			{
				setHtml = setHtml + this.options.fields.phone;
			}
		}
		if (this.options.fields.alpha != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.alpha;
			}
			else
			{
				setHtml = setHtml + this.options.fields.alpha;
			}
		}
		if (this.options.fields.alphanum != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.alphanum;
			}
			else
			{
				setHtml = setHtml + this.options.fields.alphanum;
			}
		}
		if (this.options.fields.digit != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.digit;
			}
			else
			{
				setHtml = setHtml + this.options.fields.digit;
			}
		}
		if (this.options.fields.url != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.url;
			}
			else
			{
				setHtml = setHtml + this.options.fields.url;
			}
		}
		
		if (this.options.fields.atleast_required != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.atleast_required;
			}
			else
			{
				setHtml = setHtml + this.options.fields.atleast_required;
			}
		}

		if (this.options.fields.trim_spaces != "")
		{
			if (setHtml == "")
			{
				setHtml = this.options.fields.trim_spaces;
			}
			else
			{
				setHtml = setHtml + this.options.fields.trim_spaces;
			}
		}

		
		
		setHtml = setHtml + "</table>";
		$("popup_content_error").set("html",setHtml);
		this.options.ctr = 0;
		this.options.val_date = "";
		this.options.fields.required = "";
		this.options.fields.email = "";
		this.options.fields.confirm = "";
		
		this.options.fields.length_str = "";
		this.options.fields.phone = "";
		this.options.fields.alpha = "";
		this.options.fields.alphanum = "";
		this.options.fields.digit = "";
		this.options.fields.url = "";
		this.options.fields.range_str = "";
		this.options.fields.atleast_required = "";
		this.options.fields.maximum = "";
		this.options.elRequiredClone = "";
		this.options.elMaxClone = "";
		this.options.elAtLeastRequiredClone = "";
		this.options.fields.trim_spaces = "";
	},

	
			
	openPopup : function()
	{
		var scroller = new Fx.Scroll(document.body);
		var size = getSize();
		var pos_left = ((size.x / 2)) - (this.options.x_width / 2);
		var pos_top = ((size.y / 2)) - (this.options.y_height / 2);

		this.notifier();
		scroller.toTop();
		$("popup_dropshadow_error").setStyles(
		{
			left : 8,
			top : 8,
			height : this.options.y_height,
			width : this.options.x_width
		});
        if ($("popup_shadow_error") != null) {
			var li_bodyHeight = '';
			var li_bodyWidth = '';
			if (this.options.bodyHeight == '') {
				li_bodyHeight = document.body.offsetHeight
			} else {
				li_bodyHeight = this.options.bodyHeight;
			}

			if (this.options.bodyWidth == '') {
				li_bodyWidth = document.body.offsetWidth
			} else {
				li_bodyWidth = this.options.bodyWidth;
			}
            $("popup_shadow_error").setStyles(
            {
                display : "block",
                left : 0,
                top : 0,
                height : li_bodyHeight,
                width : li_bodyWidth
            });
        }
		$("popup_error").setStyles(
		{
			display : "block",
			left : pos_left,
			top : pos_top
		});
		$("popup_container_error").setStyles(
		{
			height : this.options.y_height,
			width : this.options.x_width
		});
		$("popup_content_error").setStyle("font-weight","bold");
	},

	closePopup : function()
	{
        if ($("btn_close") != null) {
            $("btn_close").addEvents({
                "click" : function() {
                    $("popup_error").setStyle("display","none");
                    $("popup_shadow_error").setStyle("display","none");
                    $("popup_content_error").set("html","");
                }
            })
        }
	},

	onSubmit: function(event)
	{
		var continueSubmit = true;
		if (this.form = $(this.options.frm))
		{
			this.form.getElements("*[rel*=validate]").each(function(el)
			{
				if (this.register(el) == true)
				{
					continueSubmit = true;
				}
				else
				{
					continueSubmit = false;
				}
			}, this);
		}
		if (continueSubmit)
		{
			if (this.submitPost != undefined)
			{
				this.submitPost();
			}
		}
		else
		{
			
			this.openPopup();
		}
		this.options.error = 0;
		return continueSubmit;
	},

	initialize : function(options)
	{
		this.setOptions(options);
		if (this.form = $(this.options.frm))
		{
			this.form.addEvents(
			{
				"submit": this.onSubmit.bind(this)
			});
		}
		this.closePopup();
	}
});

