 /**
 * Javascript Form Validation Class
 *
 * This validation class works along with jQuery and CI's validation class
 * to provide a 0-time javascript implementation of the php validation. Use
 * is very easy. Just need to follow theese two steps:
 *
 * 1. Add the onsubmit method to the form:
 *
 * 		onsubmit="return validateForm()"
 *
 * 2. Add the following code at the end of page:
 *
 * 		< ?php echo $this->validation->getJsValidation(); ?>
 *
 * @package		Mobile Momma
 * @subpackage	Library
 * @category	Javascript
 * @author		Md Emran Hasan <emran@rightbrainsolution.com>
 * @link		http://www.rightbrainsolution.com
 */

function FormValidator()
{

    // Rules array for holding multiple rules
	var rules = new Array();
	
	// The first element that had error
	var firstError = null;

	// Adds a rule to the collection
	this.addRule = function (field, message)
	{
		var ruleArray = {
			'field' : field,
			'msg'	: message
		};

		rules.push(ruleArray);
	}

	// Validates a form against all the rules
	this.validate = function()
	{
		var isValid = true;
		rules.reverse();

		for (var i in rules)
		{
			if (!this.checkValue(rules[i]))
			{
				isValid = false;
			}
		}
		
		if (!isValid)
		{
			if($("#" + this.firstError).attr('type') == "text" || $("#" + this.firstError).attr('type') == "password" || $("#" + this.firstError).attr('type') == "checkbox" || $("#" + this.firstError).attr('type') == "select" || $("#" + this.firstError).attr('type') == "textarea")
			{
				$("#" + this.firstError).focus();
			}
		}

		return isValid;
	}

	// Checks the value of a form element and determines its validity
	this.checkValue = function (rule)
	{
		// This is the jackpot
		var theValue = "";

		// Check text and password fields
		if($("#" + rule.field).attr('type') == "text" || $("#" + rule.field).attr('type') == "password" || $("#" + rule.field).attr('type') == "checkbox" || $("#" + rule.field).attr('type') == "textarea" )
		{
			theValue = $("#" + rule.field).val();
		}

		// Check select boxes
		else if($("#" + rule.field).attr('type') == "select")
		{
			theValue = $("#" + rule.field).val();

			if (theValue == "none" || theValue == "-- Select --")
			{
				theValue = "";
			}
		}

		// Check TinyMCE editor
		else
		{
			try {
				if ($("#" + rule.field).attr('mce_editable') == 'true')
				{
					theValue = tinyMCE.getContent(rule.field);
				}
				else
				{
					theValue = $("#" + rule.field).val();
				}
			} catch(err) {
				// muri khao
			}
		}

		// If the value is blank, we need to report it
		if(theValue == "" || theValue == null)
		{
			if ($("#" + rule.field).next("p.errorBox").html() != rule.msg)
			{
				$("#" + rule.field + "-error").remove();
				$("#" + rule.field).after("<p id='" + rule.field + "-error' class='errorBox' align='center'>" + rule.msg + "</p>");
			}
			
			// We'll focus on 'em
			this.firstError = rule.field;

			// Its *ILLEGAL*
			return false;
		}

		// Well, it seems to be a valid field
		else
		{
			// So, remove the error msg if its there
			$("#" + rule.field + "-error").remove();
			
			// Its *LEGAL*
			return true;
		}
	}
}