/*
 *
 * Plugin forCheck
 * 
 * http://www.filipekiss.com.br/forCheck
 *
 *
 * Release 1.2
 * June, 03 - 2008
 *
 * Changelog for 1.2
 *
 * Added support to 'select' type fields
 *
 * Release 1.0
 * May, 06 - 2008
 *
 * Changelog for 1.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] - Check to see if a select is valid. A value equal 0 will be made invalid. a value different will be true.
 *
 * Future Classes (Or, ToDo list, as you wish)
 * date - date Validation. Probably mm/dd/yyyy
 * [Support for FCKEditor] DONE!
 * 
 * 
 *
 * 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("forCheck[") != -1 && jField.attr("class").indexOf("*]") != -1 && value.length < 1)
	{
			valid = false;
			removeError(jField)
			error(jField, 0);
	}
	else if(jField.attr("class").indexOf("forCheck[int") != -1)
	{
		if(!/^[0-9]*$/.test(value))
		{
			valid = false;
			removeError(jField)
			error(jField, 1);
		}
		else
		{
			removeError(jField)
		}
	}
	else if(jField.attr("class").indexOf("forCheck[str") != -1)
	{
		if(!/^[a-zA-ZöÖäÄåÅáÁàÀãÃâÂéÉèÈêÊíÍìÌîÎóÓòÒõÕôÔúÚùÙûÛ]*$/.test(value))
		{
			valid = false;
			removeError(jField)
			error(jField, 2);
		}
		else
		{
			removeError(jField)
		}
	}
	else if(jField.attr("class").indexOf("forCheck[select") != -1)
	{
		if(value == "0")
		{
			valid = false;
			removeError(jField)
			error(jField, 2);
		}
		else
		{
			removeError(jField)
		}
	}
	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;
			removeError(jField)
			error(jField, 4);
		}
		else
		{
			removeError(jField)
		}
	}
	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)
		}
		/* 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;
			removeError(jField)
			error(jField, 4);
		}
		else
		{
			removeError(jField)
		} */
	}
	else if(jField.attr("class").indexOf("forCheck[password") != -1)
	{
		if(value != jQuery("input[class~=forCheck\[confirm]").val())
		{
			valid = false;
			removeError(jField)
			error(jField, 4);
		}
		else
		{
			jremoveError(jField)
		}
	}
	else
	{
		removeError(jField)
	}
	return valid;
}
//Function that show the user an error happened
function error(jField)
{
	jField.addClass("error_input")
	$(jField.children()).addClass("error_input")
}

//Function that remove the errors highlight after the validation (if it is valid, of course)
function removeError(jField)
{ 
	jField.removeClass("error_input")
	$(jField.children()).removeClass("error_input")
}

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)
{
	if(window.console && console.log)
		console.log($(this))
	jQuery(this).submit
	(
		function() 
		{
			//Check if we have the impromptu plugin enabled
			if(jQuery.ImpromptuDefaults)
			{
				//We have! Lets clear the error message! (or define it)
				message = ""
			}
			var validationError = false;
			//Look for inputs to validate when submitting
			jQuery("input").each( function() {
				if ($(this).attr("class")) {
					if (!validateField(this))
						validationError = true;
				}
			});
			//Look for Selects
			jQuery("select").each( function() {
				if ($(this).attr("class")) {
					if (!validateField(this))
						validationError = true;
				}
			});
			//Look For Textareas
			jQuery("textarea").each( function() {
				if ($(this).attr("class")) {
					if (!validateField(this))
						validationError = true;
				}
			});
			return !validationError;
		} 
	)
}

//Apply it to all forms
function apply(){
	$("form").forCheck();
}
$(document).ready(apply)