$.fn.copyTo = function(to) {
    var to = $(to);
    for ( var i = 1; i < arguments.length; i++ )
        to.set( arguments[i], this.get(0)[ arguments[i] ] );
    return this;
};
var chk_mail=0;
var chk_site=0;
//
String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g,"");
}

//
new function() {
       	// $.fn.validate = validate() {};
     	// $.fn.validate = validate() {};
    $.fn.validate = {
        init: function(o) {
          if(o.name == 'email') { this.email(o) };
          if(o.name == 'dob') { this.dob(o) };
          if(o.name == 'site') { this.testsite(o) };
        },
        testsite: function(o) {
           var sit  = /^((http:\/\/){0,1})+((([a-zA-Z0-9\-])+\.){0,1})+(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/;              
           if (o.value.match(sit)) {
             doSuccess(o);
             chk_site=1;
            } else {
             doError(o,'adresse url non valide');
             chk_site=0;
            };
        },

        email: function(o) {
        
          var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
           if (o.value.match(email)) {
              doSuccess(o);
              chk_mail=1;
            } else {
              doError(o,'email non valide');
              chk_mail=0;
            };
        },
        dob: function(o) {
          var dob  = /(0[1-9]|1[012])+\/(0[1-9]|[12][0-9]|3[01])+\/(19|20)\d\d/;
            if (o.value.match(dob)) {
              doSuccess(o);
            } else {
              doError(o,'date non valide');
            };
        }
     };
     function doSuccess(o) {    
               //$('#' + o.id + '_img').html('<img src="images/accept.gif" border="0" style="float:left;" />');
              //$('#' + o.id + '_li').removeClass("error");
              //$('#' + o.id + '_msg').html("");
              //$('#' + o.id + '_li').addClass("success");
               $('#' + o.name + '_err').html("");
     }

     function doError(o,m) {      
              //$('#' + o.id + '_img').html('<img src="images/exclamation.gif" border="0" style="float:left;" />');
              //$('#' + o.id + '_li').addClass("error");
              //$('#' + o.id + '_msg').html(m);
              //$('#' + o.id + '_li').removeClass("success");
               $('#' + o.name + '_err').html(m);
     }
     //private helper, validates each type after check
     
};
//
function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char; 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;   
};

function calculpack() {
	var calculpack=0;	
	 $(".produit-total-input").each( function () {
        	calculpack+=parseInt($(this).val())|| 0;
         });
    $("#pack-total").val(calculpack);        
};
//
function calculbanner(nb) {
	var calcul=0;
	var tarifs=[0,15,25,34,42,49,55,60];
	calcul=tarifs[nb];
    $("#banner-total").val(calcul);        
};
function calcultotal() {
    var commandetotal = 0;
    var bannertotal = $("#banner-total").val() || 0;
   // var packtotal = $("#pack-total").val() || 0;        
    var commandetotal = parseInt(bannertotal);// + parseInt(packtotal);   
    $("#order-total").val(commandetotal);        
};

$(document).ready(function()
{
// clean d arrivˇe
	 	var result = 0;
         $(".chk-banner").each( function () {
        this.checked=false;
          });
        //
        calculbanner(result);  
        calcultotal();    
        
// empruntˇ a valcontact
	$(".verif").each(function() {
         $(this).val("");
  });
//
  $(".verif").keyup(function() {
  			this.value=this.value.trim();
            $(this).validate.init(this);
  });
  
  $("#envoi").submit(function() {
     if(chk_mail==1 && chk_site==1 && $("#description").val()!="" && $("#order-total").val()!="0"){
      return true
   } else  {
     if($("#order-total").val()=="0") {
     alert("Vous n'avez pas s\351lectionn\351 de banni\350res : veuillez cocher au moins un format de banni\350re !");
     }else{
      alert("Veuillez remplir correctement tous les champs (seuls les champs image de r\351f\351rence  et code de reduction sont optionnels )...");
      }
      return false;
   }

  });
// propre a order
    $('.case').click(function(){
		 var result = 0;
         $(".chk-banner").each( function () {
         if($(this).is(':checked')) {
            result = result + 1;
            }
         });
        //
        calculbanner(result);  
        calcultotal();    
     });
    /*
      $('.selec-produit-input').change(function(){	
      $this=$(this);
      var multiplier = $this.parent().parent().find("td.tarif-produit span").text();
      if($(this).is(':checked')) {
            $this.parent().parent().find("td.produit-total input").val(multiplier);
      }else{	
      		$this.parent().parent().find("td.produit-total input").val(0);
		} 
		
		calculpack();
	 	calcultotal();
      });
    */
	 $("#order-total").val(0);
	 //$("#pack-total").val(0);
	 $("#banner-total").val(0);

});