
jQuery(document).ready(function() {	
 valid.init();
 
});

valid = {
	
	init : function()  {
		
		
		jQuery("#validate_form").submit(function () { 
			var formToValid = this;
			
			jQuery(".emptyInputError").remove();
			jQuery(".validEmailError").remove();
			jQuery(".validTelError").remove();
			jQuery(".validDigitError").remove();
			jQuery(formToValid).find(".validEmpty").css("background-color","");
			jQuery(formToValid).find(".validEmail").css("background-color","");
			jQuery(formToValid).find(".validTel").css("background-color","");
			jQuery(formToValid).find(".validDigit").css("background-color","");
			
			divErrorContainer = document.createElement('div')
			jQuery(divErrorContainer).attr('id','formError');
			jQuery(formToValid).prepend(divErrorContainer)
			
			var lng = 'e';
			var vA=valid.emptyInput(lng, formToValid);
			
			var vB=valid.emptyEmail(lng, formToValid);
			var vC=valid.emptyTel(lng, formToValid);
			var vD=valid.emptyDigit(lng, formToValid);
			
			var vAmount = true;
			var filter  = /^[0-9\.]+$/;
			if( jQuery("#amount_other").css('display') != 'none' && ( !filter.test(jQuery("#amount_other_value").val()) || jQuery("#amount_other_value").val() < 0.01 ) ){				
				vAmount = false;
			}
			
			var vGiftType = true;
			if( jQuery("input[name$='gift_type']:checked").val() == undefined  ){
				vGiftType = false;
			}
			
			if ( vAmount ){
				jQuery("#amount_other_label").css("color","");
				jQuery("#amount_other_value").css('background-color', '');
				jQuery("#amount_other_asterisk").css("color","");
			}
			else{
				if ( !jQuery(".emptyInputError").length ){
					var divError = document.createElement('div')
					jQuery(divError).addClass('emptyInputError');
					jQuery(divError).addClass('error');
					jQuery(divError).html("Invalid amount");
					jQuery(divError).css({ display:"block",color:"red", clear:"both"});
					jQuery('#formError').append(divError);
				}				
				jQuery("#amount_other_label").css("color","red");
				jQuery("#amount_other_value").css('background-color', '#FFEEEE');
				jQuery("#amount_other_asterisk").css("color","red");
			}
			if ( vGiftType ){
				jQuery("#gift_type_label").css("color","");
				jQuery("input[name$='gift_type']").css('background-color', '');
				jQuery("#gift_type_asterisk").css("color","");
			}
			else{
				if ( !jQuery(".emptyInputError").length ){
					var divError = document.createElement('div')
					jQuery(divError).addClass('emptyInputError');
					jQuery(divError).addClass('error');
					jQuery(divError).html("Invalid Gift Type");
					jQuery(divError).css({ display:"block",color:"red", clear:"both"});
					jQuery('#formError').append(divError);
				}				
				jQuery("#gift_type_label").css("color","red");
				jQuery("input[name$='gift_type']").css('background-color', '#FFEEEE');
				jQuery("#gift_type_asterisk").css("color","red");
			}			

			if( vA && vB && vC && vD && vAmount && vGiftType ){
				successResult = true;
				jQuery('input[name$="amount_other"]').val(jQuery("#amount_other_value").val());
				return true;
			}
			
			return valid.finall();

		});

	},
	
	emptyInput : function(lng, formToValid)  {

		var isError=false;

		emptyCountMax = jQuery(formToValid).find(".validEmpty").size();

		jQuery(formToValid).find(".validEmpty").parent().parent().find(".labelName").css("color",""); // put back the normal color
		jQuery(formToValid).find(".validEmpty").parent().parent().find(".form-required").css("color","");
		jQuery(".emptyInputError").css({ display:"none"}); // Each time you start the script it disable error display, so when you push the button a seoncd time , you have only the new error of your form
		jQuery(".validForm").css({ display:"none"});

		for (i=0; i < emptyCountMax; i++) {
			if(jQuery(formToValid).find(".validEmpty").eq(i).attr('value') == "" && jQuery(formToValid).find(".validEmpty").eq(i).css('display') != 'none' ){
				jQuery(formToValid).find(".validEmpty").eq(i).parent().find(".emptyInputError").css({ display:"block"});
				jQuery(".validForm").css({ display:"block"});
				var errMess=jQuery(formToValid).find(".validEmpty").eq(i).parent().find(".emptyInputError");
			
				if(errMess.length == 0){
					var divError = document.createElement('div')
					
					//namee = jQuery('.validEmpty').eq(i).attr('name');
					jQuery('.validEmpty').eq(i).css('background-color', '#FFEEEE');
					var iLabel=jQuery(formToValid).find(".validEmpty").eq(i).parent().parent().find('label').find(".labelName").html();
					iLabel=iLabel.replace(':','');

					if ( !jQuery(formToValid).find(".emptyInputError").length ){
						jQuery(divError).addClass('emptyInputError');
						jQuery(divError).addClass('error');
						jQuery(divError).html("Please complete all of the fields indicated below.");
						jQuery('#formError').append(divError);
					}
					//jQuery(divError).html("The field <i>"+iLabel+"</i> is mandatory");
					jQuery(formToValid).find(".validEmpty").eq(i).parent().parent().find('label').find(".labelName").css("color","red");
					jQuery(formToValid).find(".validEmpty").eq(i).parent().parent().find(".form-required").css("color","red");
					// jQuery('.validEmpty').eq(i).parent().append(divError) Append dans le parent du input
					//jQuery('.validEmpty').eq(i).parent().css("color","red");
					jQuery(divError).css({ display:"block",color:"red", clear:"both"});
				}
				isError=true;//valid.finall();
			
			}
		}
		
		return !isError;
		
	},
	
	emptyEmail : function(lng, formToValid)  {
		var isError=false;
		emptyCountMax =jQuery(formToValid).find(".validEmail").size();				jQuery(".validEmailError").css({ display:"none"});
		jQuery(".validForm").css({ display:"none"});
		jQuery(formToValid).find(".validEmail").parent().parent().find(".labelName").css("color",""); // put back the normal color
		jQuery(formToValid).find(".validEmail").parent().parent().find(".form-required").css("color","");

		
		var filter  = /^[a-zA-Z0-9_\.\-]+\@([a-zA-Z0-9\-]+\.)+[a-zA-Z0-9]{2,4}$/;
		for (i=0; i < emptyCountMax; i++) {
	

			var thisMail=jQuery(formToValid).find(".validEmail").eq(i).attr('value');
			//alert(thisMail);
			if(!filter.test(thisMail) && jQuery(formToValid).find(".validEmail").eq(i).css('display') != 'none' ){
				
				jQuery(formToValid).find(".validEmail").eq(i).parent().find(".validEmailError").css({ display:"block"})
				jQuery(".validForm").css({ display:"block"})
				var errMess=jQuery(formToValid).find(".validEmail").eq(i).parent().find(".validEmailError");
				
				if(errMess.length == 0){

					divError = document.createElement('div');
					var iLabel=jQuery(formToValid).find(".validEmail").eq(i).parent().parent().find('label').find(".labelName").html();
					iLabel=iLabel.replace(':','');					
					
					jQuery('.validEmail').eq(i).css('background-color', '#FFEEEE');
					jQuery(divError).addClass('validEmailError');
					jQuery(divError).addClass('error');
					if ( thisMail !== "" ) jQuery(divError).text("Invalid " + iLabel);
					jQuery(formToValid).find(".validEmail").eq(i).parent().parent().find('label').find(".labelName").css("color","red");
					jQuery(formToValid).find(".validEmail").eq(i).parent().parent().find(".form-required").css("color","red");
					jQuery('#formError').append(divError);//jQuery('.validEmail').eq(i).parent().append(divError);
					//jQuery('.validEmail').eq(i).parent().css("color","red");
					jQuery(divError).css({ display:"block",color:"red"});										if (jQuery(divError).text() == '') {						jQuery(divError).css({ display:"none"});					}
				}
				 isError=true;// valid.finall()
			}
		}
		return !isError;
	},
	
	emptyTel : function(lng, formToValid)  {
		var isError=false;
		emptyCountMax = jQuery(formToValid).find(".validTel").size()
		jQuery(".validTelError").css({ display:"none"})
		jQuery(".validForm").css({ display:"none"})
		
		jQuery(formToValid).find(".validTel").parent().parent().find(".labelName").css("color",""); // put back the normal color
		jQuery(formToValid).find(".validTel").parent().parent().find(".form-required").css("color","");
		
		for (i=0; i < emptyCountMax; i++) {
	
			var filter  = /^[0-9\-\(\)]+$/;
			
			var thisTel = jQuery(formToValid).find(".validTel").eq(i).attr('value');

			if(!filter.test(thisTel) && jQuery(formToValid).find(".validTel").eq(i).css('display') != 'none' ){
				jQuery(formToValid).find(".validTel").eq(i).parent().find(".validTelError").css({ display:"block"})
				var errMess=jQuery(formToValid).find(".validTel").eq(i).parent().find(".validTelError");
				jQuery(".validForm").css({ display:"block"})
				if(errMess.length == 0){
				
					divError = document.createElement('div')
					jQuery('.validTel').eq(i).css('background-color', '#FFEEEE');
					var iLabel=jQuery(formToValid).find(".validTel").eq(i).parent().parent().find('label').find(".labelName").html();
					iLabel=iLabel.replace(':','');					

					jQuery(divError).addClass('validTelError');
					jQuery(divError).addClass('error');
					if ( thisTel !== '' ) jQuery(divError).text("Invalid " + iLabel);
					jQuery('#formError').append(divError);///jQuery('.validTel').eq(i).parent().append(divError);
					jQuery(formToValid).find(".validTel").eq(i).parent().parent().find('label').find(".labelName").css("color","red");
					jQuery(formToValid).find(".validTel").eq(i).parent().parent().find(".form-required").css("color","red");
					jQuery(divError).css({ display:"block",color:"red"});										if (jQuery(divError).text() == '') {						jQuery(divError).css({ display:"none"});					}
				}	
				isError=true;//valid.finall();
			}	
		}
		return !isError;
	},
	
	emptyDigit : function(lng, formToValid)  {
		var isError=false;
		emptyCountMax =jQuery(formToValid).find(".validDigit").size();
		jQuery(".validDigitError").css({ display:"none"});
		jQuery(".validForm").css({ display:"none"});
		jQuery(formToValid).find(".validDigit").parent().parent().find(".labelName").css("color",""); // put back the normal color
		jQuery(formToValid).find(".validDigit").parent().parent().find(".form-required").css("color","");

		
		var filter  = /^[0-9]+$/;
		for (i=0; i < emptyCountMax; i++) {
	

			var thisDigit=jQuery(formToValid).find(".validDigit").eq(i).attr('value');
			//alert(thisMail);
			if(!filter.test(thisDigit) && jQuery(formToValid).find(".validDigit").eq(i).css('display') != 'none' ){
				
				jQuery(formToValid).find(".validDigit").eq(i).parent().find(".validDigitError").css({ display:"block"})
				jQuery(".validForm").css({ display:"block"})
				var errMess=jQuery(formToValid).find(".validDigit").eq(i).parent().find(".validDigitError");
				
				if(errMess.length == 0){
						
					divError = document.createElement('div');
					var iLabel=jQuery(formToValid).find(".validDigit").eq(i).parent().parent().find('label').find(".labelName").html();
					iLabel=iLabel.replace(':','');					
					
					jQuery('.validDigit').eq(i).css('background-color', '#FFEEEE');
					jQuery(divError).addClass('validDigitError');
					jQuery(divError).addClass('error');
					if ( thisDigit !== '' ) jQuery(divError).text("Invalid field " + iLabel);
					jQuery(formToValid).find(".validDigit").eq(i).parent().parent().find('label').find(".labelName").css("color","red");
					jQuery(formToValid).find(".validDigit").eq(i).parent().parent().find(".form-required").css("color","red");
					jQuery('#formError').append(divError);//jQuery('.validDigit').eq(i).parent().append(divError);
					jQuery(divError).css({ display:"block",color:"red"});					if (jQuery(divError).text() == '') {						jQuery(divError).css({ display:"none"});					}
				}
				 isError=true;// valid.finall()
			}
		}
		return !isError;
	},
	
	
	finall : function()  {
		if (jQuery(".error").eq(0).offset() != undefined ) {
			topOffset = jQuery(".error").eq(0).offset().top;
			jQuery('html,body').animate({scrollTop:topOffset},'slow');
		}
		return false;
	}
}

function doCheckAmount( el ){
	var n = el.name;
	var l = jQuery('#'+n+' :selected').attr('label');
	
	if ( l == 'other'){
		jQuery('#amount_other').show();
	}
	else{
		jQuery('#amount_other').hide();
		jQuery('#cardnumber').addClass('validEmpty');
		jQuery('#cardexpiremonth').addClass('validEmpty');
		jQuery('#cardexpireyear').addClass('validEmpty');
		jQuery('#cardcvv').addClass('validEmpty');
		jQuery('#payment_details').show();
	} 
}

function doRecurring( n, oldValue ){
	var l = jQuery('input:radio[name="'+n+'"]:checked').attr('title');
	
	if ( l == 'recurring'){
		jQuery('#frequency_row').show();
		jQuery('#frequency').addClass('validEmpty');
	}
	else{
		jQuery('#frequency_row').hide();
		jQuery('#frequency').removeClass('validEmpty');
	}
	jQuery('#frequency').val(oldValue);
	
}

function changeCountry( el ){
	var v = el.value;
	if ( v == 'US' ) {
		jQuery('#region').hide();
		jQuery('#state').show();
	}
	else{ 
		jQuery('#state').hide();
		jQuery('#region').show();
	}	
}
