/*
 *
 * Plugin forCheck
 * 
 * http://www.filipekiss.com.br/forCheck
 *
 *
 * Release 1.4
 * July, 10 - 2008
 * 
 * Changelog for v1.4
 *
 * New option for the class: -len([0-9]{1,}) Requires a minimum length for the field
 *
 * Release 1.3
 *	June, 26 - 2008
 *
 * Changelog for v1.3
 *
 * Compatible with the 'noConflict' option from jQ
 *
 * Release 1.2
 * June, 03 - 2008
 *
 * Changelog for v1.2
 *
 * Added support to 'select' type fields
 *
 * Release 1.0
 * May, 06 - 2008
 *
 * Changelog for v1.0
 * First Release
 *
 * Usage: 
 * The requirements to fill the field are defined through pseudo-classes.
 * You Specify a class for the plugin and you may also specify any other class for styling your input field
 *
 * Pseudo-Classes Allowed
 * forCheck[*] - for any allowed data, but a required field (usernames, etc)
 * forCheck[int] - number field. you may leave this in blank. otherwise you should fill with a number
 * forCheck[str] - string field. no numbers or symbols allowed. (including spaces)
 * forCheck[email] - validate an email. accepts @ and also [at]
 * forCheck[password] - here's the real deal. i've started this plugin just because I needed to validate a password AND a confirmation field. So you give your password this class
 * and give you confirmation field the confirmation (forCheck[confirmation]) class. Done! It will check them! If they match, you're good to go. Otherwise, 'The passwords do not match'
 * forCheck[select] - ATTENTION! THIS DOES NOT ALLOW THE REQUIRED (*) OPTION! Just add the class 'select' and if the value is equal 0, select will be invalid.
 * forCheck[-len(<number>)] - Determines a minimun length for the field. This can be mixed with any other option (except for select) for example forCheck[int-len(2)] denotes a required field
 * that will require a number and at least two digits before validate. The -len attribute must be AFTER the type attribute. So forCheck[-len(2)int] would not work
 *
 * Future Classes (Or, ToDo list, as you wish)
 * date - date Validation. Probably mm/dd/yyyy
 * Support for FCKEditor
 * 
 * 
 *
 * Notes:
 *
 * To make a required field and also a 'value type' verification just add an '*' after the type in the class name;
 * For example:
 * forCheck[email] denotes a simple, but not required, e-mail validation
 * forCheck[email*] denotes a required field. First it will check if it's filled. Then, it will check for a valid e-mail
 *
 * If you use an inexistent class, for example, forCheck[someClassHere], the plugin will simple ignore it. However
 * if you use an inexistent class followed by a '*' (forCheck[someClassHere*]), the plugin WILL ASK for content,
 * and WILL NOT allow the submition if the field is not filled.
 * This IS NOT a BUG.
 */
 
function validateField(field)
{
	
	/* Do not edit beyond this point */
	var valid = true;
	var jField = jQuery(field)
	var value = jField.val()
	if(jField.attr("class").indexOf("-len(") != -1)
	{
		var re = new RegExp(/\-len\([0-9]{1,}\)/)
		// //console.log(re)
		// //console.log(jField.attr("class"))
		s = ""
		var m = re.exec(jField.attr("class"))
		for (i = 0; i < m.length; i++) {
			s = s + m[i] + "\n";
		}
		s = s.replace("-len(","")
		s = s.replace(")","")
		if(value.length < s)
		{
			valid = false;
		}
	}
	if(jField.attr("class").indexOf("forCheck[") != -1 && jField.attr("class").indexOf("*]") != -1 && value.length < 1)
	{
			valid = false;
	}
	else if(jField.attr("class").indexOf("forCheck[int") != -1)
	{
		if(!/^[0-9]*$/.test(value))
		{
			valid = false;
		}
	}
	else if(jField.attr("class").indexOf("forCheck[str") != -1)
	{
		if(!/^[a-zA-ZöÖäÄåÅáÁàÀãÃâÂéÉèÈêÊíÍìÌîÎóÓòÒõÕôÔúÚùÙûÛ]*$/.test(value))
		{
			valid = false;
		}
	}
	else if(jField.attr("class").indexOf("forCheck[select") != -1)
	{
		if(value = 0)
		{
			valid = false;
		}
	}
	else if(jField.attr("class").indexOf("forCheck[email") != -1)
	{
		if(!/^[a-zA-Z0-9]{1}([\._a-zA-Z0-9-]+)(\.[_a-zA-Z0-9-]+)*(@|\[at\])[a-z0-9-]+(\.[a-z0-9-]+){1,3}$/.test(value))
		{
			valid = false;
		}
	}
	else if(jField.attr("class").indexOf("forCheck[fck") != -1)
	{
		var objName = jField.val()
		var  fckObj = getFCKObj(objName)
		var objValue = fckObj.GetXHTML(true)
		if(objValue.length < 1)
		{
			errorFCK(fckObj)
		}
		else
		{
			removeErrorFCK(fckObj)
		}
	}
	else if(jField.attr("class").indexOf("forCheck[password") != -1)
	{
		if(value != jQuery("input[class~=forCheck\[confirm]").val())
		{
			valid = false;
		}
	}
	return valid;
}
//Function that show the user an error happened
function error(jField)
{
	var errorId = "_error"
	errorId = jField.attr("id")+errorId
	jField.addClass("error_input")
	if(errorId != "_error")
	{
		if($("#"+errorId).size())
		{
			$("#"+errorId).slideDown("slow")
		}
	}
	else
	{
		return;
	}
}

//Function that remove the errors highlight after the validation (if it is valid, of course)
function removeError(jField)
{
	var errorId = "_error"
	errorId = jField.attr("id")+errorId
	jField.removeClass("error_input")
	if(errorId != "_error")
	{
		if($("#"+errorId).size())
		{
			$("#"+errorId).slideUp("slow")
		}
	}
	else
	{
		return;
	}
}

function getFCKObj(objName)
{ 
	return FCKeditorAPI.GetInstance(objName)
}
function errorFCK(objName)
{
	objName.EditorDocument.body.style.border= "2px solid red"
}
function removeErrorFCK(objName)
{ 
	objName.EditorDocument.body.style.border= "0px"
}
//jQuery extension
jQuery.fn.forCheck = function(options)
{
	var defaults = {
		validSubmit: false,
		isAjax: false
	}
	var opts = jQuery.extend(defaults, options);
	jQuery(this).submit
	(
		function() 
		{
			var validationError = false;
			jQuery("input").each( function() {
				if (jQuery(this).attr("class")) {
					if (!validateField(this))
					{
						error(jQuery(this))
						validationError = true;
					}
					else
					{
						removeError(jQuery(this))
					}
				}
			});
			if(defaults.validSubmit)
			{
				if(!validationError)
					defaults.validSubmit()
			}
			if(defaults.isAjax)
			{
				return false;
			}
			return !validationError;
		} 
	)
	jQuery(jQuery(this).children("input")).each(
		function()
		{
			jQuery(this).blur(
				function()
				{
					if (jQuery(this).attr("class")) {
						if (!validateField(this))
						{
							error(jQuery(this))
							validationError = true;
						}
						else
						{
							removeError(jQuery(this))
						}
					} 
				}
			)
		}
	)
}

// jQuery(document).ready(
// function()
// {
	// $("form").forCheck();
// }
// )