/*
 * 
 * Utopic Farm 2010
 * @author Tolga Arican
 * @website www.utopicfarm.com
 * @version 1.0.4
 * 
 */


// FORM VALIDATOR JQUERY PLUGIN - START

(function($) {
	
	$.fn.formValidator = function(options) {
		var merged_options = $.extend({}, $.formValidator.defaults, options);
		$(merged_options.scope + ' .input').each(function() {
			$(this).after("<span class='error'> </span>")
		});

		$(this).click(function() { 
			var result = $.formValidator(options);
			if (result && jQuery.isFunction(options.onSuccess)) {
				options.onSuccess();
				return false;
			} else if (!result && jQuery.isFunction(options.onError)) {
				options.onError();
				return false;
			} else {
				return result; 
			}
		});
	};
	$.formValidator = function (options) {
		var mail_filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		var numeric_filter = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)|(^-?\d*$)/;

		var merged_options = $.extend({}, $.formValidator.defaults, options);
		//$(merged_options.scope + ' .error').html("").hide();
		var globalResult=true;
		$(merged_options.scope + ' .input').each(function() {
			message="";
			result=true;
			var obj = $(this);
			var valAttr = obj.val();
			if($(this).hasClass("req-numeric")){
				tmpresult = numeric_filter.test(valAttr);
				intAttr=parseInt(valAttr);

				//alert(intAttr);
				if(tmpresult&&obj.attr('minval'))
					tmpresult = intAttr>=parseInt(obj.attr('minval'));
				if(tmpresult&&obj.attr('maxval'))
					tmpresult = intAttr<=parseInt(obj.attr('maxval'));
				if (!tmpresult) message+="Veuillez entrer un nombre entre "+obj.attr('minval')+" et "+obj.attr('maxval')+". ";
				result = result && tmpresult;
			}
			// MINIMUM REQUIRED FIELD VALIDATION
			if (obj.hasClass('req-min')) {
				tmpresult = (valAttr.length >= obj.attr('minlength'));
				if (!tmpresult) message+="Taille minimal du texte : "+obj.attr('minlength')+". ";
				result = result && tmpresult;
			}
			// E-MAIL VALIDATION
			if (obj.hasClass('req-email')) {
				tmpresult = mail_filter.test(valAttr);
				if (!tmpresult) message+="Mauvais format d'adresse email. ";
				//errorTxt = (valAttr == '') ? opts.errorMsg.reqMailEmpty : opts.errorMsg.reqMailNotValid;
				result = result && tmpresult;
			}
			// E-MAIL VALIDATION
			if (obj.hasClass('req-val')) {
				tmpresult = valAttr!="";
				if (!tmpresult) message+="Choisir une valeur. ";
				//errorTxt = (valAttr == '') ? opts.errorMsg.reqMailEmpty : opts.errorMsg.reqMailNotValid;
				result = result && tmpresult;
			}
			nextElement=$(this).next();
			var offset = $(this).offset();
			offsetLeft=offset.left+270;
			offsetTop=offset.top-2;
			offsetTopScroll=offset.top-10;
			if(message.length > 0){
				nextElement.html("<p>"+message+"</p> ").css({left: offsetLeft+"px", top: offsetTop+"px"}).slideDown();
				$('html,body').animate({ scrollTop: offsetTopScroll }, 1000);
				//alert(false);
				globalResult=false;
				return false;
			} else {
				$(this).next().html("").hide();
			}
			globalResult=globalResult&&result;
		});
		return globalResult;
	};

})(jQuery);

function getAbsoluteLeft(o) {
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	return oLeft
}

function getAbsoluteTop(o) {
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop
}

