/*!
 * [alert]
 * 
 *  
 */

function reinitialiser(){
    jQuery("#rbt_knowDepartureDate").attr("checked", checked);
    jQuery("#destination div:last").hide();
    jQuery("#destination div:first").show();
    jQuery(jQuery("#rbt_knowDepartureDate").parents("p").children()[1]).addClass("checked");
    jQuery(jQuery("#rbt_dontKnowDepartureDate").parents("p").children()[1]).removeClass("checked");
    jQuery("#Recap-duree").hide();
    jQuery("#rbt_onceAWeek").attr("checked", checked);
    jQuery("#frequency p input").each(function(i){
        if(jQuery(this).is(":checked")){
            jQuery(jQuery(this).parents("p").children()[1]).addClass("checked");
        }else {
            jQuery(jQuery(this).parents("p").children()[1]).removeClass("checked");
        }
    });
    jQuery("#ipt_departureDate").attr("value","");
    jQuery("#ipt_alertName").attr("value","");
    jQuery("#slt_departureCity").get(0).selectedIndex = 0;
    jQuery("#ipt_destination").get(0).selectedIndex = 0;
    jQuery("#ipt_aj").get(0).selectedIndex = 4;
    jQuery("#ma_departureCity").html(jQuery("#slt_departureCity option:selected").text());
    jQuery("#ma_destination").html(jQuery("#ipt_destination option:selected").text());
    jQuery("#ma_date").html(" +/- "+jQuery("#ipt_aj  option:selected").attr("value")+"jrs");
    jQuery("#ma_frequency").html("Une fois par semaine") ;
}

jQuery("#slt_departureCity").change(function () {
 jQuery("#ma_departureCity").html(jQuery("#slt_departureCity option:selected").text());
});

jQuery("#ipt_destination").change(function () {
 jQuery("#ma_destination").html(jQuery("#ipt_destination option:selected").text());
});

jQuery("#slt_period").change(function () {
 jQuery("#ma_date").html(jQuery("#slt_period option:selected").text());
});

jQuery("#slt_travelTime").change(function () {
 jQuery("#ma_duree").html(jQuery("#slt_travelTime option:selected").text());
});

// when change the choice of la frequence d'envoi
jQuery("#rbt_onceAWeek").click(function () {
	jQuery("#ma_frequency").html(jQuery("#rbt_onceAWeek").attr("value"));
    jQuery(jQuery("#rbt_onceAWeek").parents("p").children()[1]).addClass("checked");
    jQuery(jQuery("#rbt_every15Days").parents("p").children()[1]).removeClass("checked");
    jQuery(jQuery("#rbt_everyMonth").parents("p").children()[1]).removeClass("checked");
});

jQuery("#rbt_every15Days").click(function () {
	jQuery("#ma_frequency").html(jQuery("#rbt_every15Days").attr("value"));
    jQuery(jQuery("#rbt_every15Days").parents("p").children()[1]).addClass("checked");
    jQuery(jQuery("#rbt_onceAWeek").parents("p").children()[1]).removeClass("checked");
    jQuery(jQuery("#rbt_everyMonth").parents("p").children()[1]).removeClass("checked");
});

jQuery("#rbt_everyMonth").click(function () {
	jQuery("#ma_frequency").html(jQuery("#rbt_everyMonth").attr("value"));
    jQuery(jQuery("#rbt_everyMonth").parents("p").children()[1]).addClass("checked");
    jQuery(jQuery("#rbt_onceAWeek").parents("p").children()[1]).removeClass("checked");
    jQuery(jQuery("#rbt_every15Days").parents("p").children()[1]).removeClass("checked");
});

jQuery("#ipt_email").change(function () {
    jQuery("#ma_email").html(jQuery("#ipt_email").attr("value"));
});

jQuery("#ipt_departureDate").change(function () {
    var aj=jQuery("#ipt_aj  option:selected").attr("value");
    if(aj=="0"){
        jQuery("#ma_date").html(jQuery("#ipt_departureDate").attr("value"));
    }else{
        aj=" +/- "+aj+"jrs";jQuery("#ma_date").html(jQuery("#ipt_departureDate").attr("value")+aj);
    }
});

jQuery("#ipt_aj").change(function () {
    var aj=jQuery("#ipt_aj  option:selected").attr("value");
    if(aj=="0"){
        jQuery("#ma_date").html(jQuery("#ipt_departureDate").attr("value"));
    }else{
    	aj=" +/- "+aj+"jrs";
    	jQuery("#ma_date").html(jQuery("#ipt_departureDate").attr("value")+aj);
    }
});

jQuery("#rbt_knowDepartureDate").click(function () {
    jQuery("#destination div:last").hide();
    jQuery("#destination div:first").show();
    jQuery(jQuery("#rbt_knowDepartureDate").parents("p").children()[1]).addClass("checked");
    jQuery(jQuery("#rbt_dontKnowDepartureDate").parents("p").children()[1]).removeClass("checked");
    jQuery("#ipt_departureDate").attr("needValidate","true");
    jQuery("#ma_date").html(jQuery("#ipt_departureDate").attr("value"));
    jQuery("#Recap-duree").hide();
});

jQuery("#rbt_dontKnowDepartureDate").click(function () {
    jQuery("#destination div:first").hide();
    jQuery("#destination div:last").show();
    jQuery(jQuery("#rbt_dontKnowDepartureDate").parents("p").children()[1]).addClass("checked");
    jQuery(jQuery("#rbt_knowDepartureDate").parents("p").children()[1]).removeClass("checked");
    jQuery("#ipt_departureDate").attr("needValidate","false");
    jQuery("#ma_date").html(jQuery("#slt_period option:selected").text());
    jQuery("#ma_duree").html(jQuery("#slt_travelTime option:selected").text());
    jQuery("#Recap-duree").show();
});

function validate() {
    var valid = true;
    jQuery("input[needValidate='true']").each(function(i){
        if(!requireField(this)){
            valid = false;
        }
     });
    jQuery("input[needValidate='false']").each(function(i){
        jQuery('#'+this.id+'_error').html("");
     });
    return valid;
}

function requireField(o){
   jQuery('#'+o.id+'_error').html("");
   var ipt_email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
   var ipt_firstName = /^(\w){2,20}|[^u4e00-u9fa5]{2,20}$/;
   var ipt_lastName = /^(\w){2,20}|[^u4e00-u9fa5]{2,20}$/;
   var ipt_zipCode = /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
   var ipt_alertName = /^(\w){2,20}|[^u4e00-u9fa5]{2,20}$/;
   var ipt_departureDate = /(0[1-9]|[12][0-9]|3[01])+\/(0[1-9]|1[012])+\/(19|20)\d\d/;
   var ipt_email_error = "-L'adresse mail doit \xEAtre renseign\xE9e et valide";
   var ipt_firstName_error = "-Le pr\xE9nom doit \xEAtre renseign\xE9\n";
   var ipt_lastName_error ="-Le nom doit \xEAtre renseign\xE9\n";
   var ipt_zipCode_error = "-Le code postal doit \xEAtre renseign\xE9 et valide";
   var ipt_alertName_error = "-Le nom pour identifier votre alerte doit \xEAtre renseign\xE9";
   var ipt_departureDate_error = "-La date de d\xE9part doit \xEAtre renseign\xE9 et valide";

   if(o.value.match(eval(o.id))){
       return true;
   } else{
      jQuery('#'+o.id+'_error').html(eval(o.id+'_error')).attr("style","color:red;");
      jQuery('#'+o.id).addClass("errorForm");
   }
   return false;
}

jQuery(function() {

    // Initialize all the contents
    jQuery("#ma_departureCity").html(jQuery("#slt_departureCity option:selected").text());
    jQuery("#ma_destination").html(jQuery("#ipt_destination option:selected").text());
    jQuery("#ma_frequency").html(jQuery("#frequency p input:checked").attr("value"));
    jQuery("#ma_email").html(jQuery("#ipt_email").attr("value"));
    
    if(jQuery("#rbt_knowDepartureDate").is(":checked")){
        jQuery("#destination div:last").hide();
        jQuery("#destination div:first").show();
        jQuery(jQuery("#rbt_knowDepartureDate").parents("p").children()[1]).addClass("checked");
        jQuery(jQuery("#rbt_dontKnowDepartureDate").parents("p").children()[1]).removeClass("checked");
        var aj=jQuery("#ipt_aj  option:selected").attr("value");
        if(aj=="0"){
            jQuery("#ma_date").html(jQuery("#ipt_departureDate").attr("value"));
        }else{
            aj=" +/- "+aj+"jrs";jQuery("#ma_date").html(jQuery("#ipt_departureDate").attr("value")+aj);
        }
        jQuery("#Recap-duree").hide();
    }else{
	    jQuery("#destination div:first").hide();
	    jQuery("#destination div:last").show();
	    jQuery(jQuery("#rbt_dontKnowDepartureDate").parents("p").children()[1]).addClass("checked");
	    jQuery(jQuery("#rbt_knowDepartureDate").parents("p").children()[1]).removeClass("checked");
	    jQuery("#ipt_departureDate").attr("needValidate","false");
	    jQuery("#ma_date").html(jQuery("#slt_period  option:selected").text());
	    jQuery("#ma_duree").html(jQuery("#slt_travelTime option:selected").text());
    }
    
    jQuery("#frequency p input").each(function(i){
        if(jQuery(this).is(":checked")){
        	jQuery(jQuery(this).parents("p").children()[1]).addClass("checked");
        } else {
        	jQuery(jQuery(this).parents("p").children()[1]).removeClass("checked");
        }
    })
    
    // Open message alert windows
	jQuery("#alertForm").submit(function() {
		jQuery(":input").not(":radio").removeClass("errorForm");
	    if(validate()){
			var parameters="";
			parameters +="dolist_form_ListId=2585&no_confirmation=1&dolist_force_update=1";
			parameters +="&dolist_form_lastname="+jQuery("#ipt_lastName").attr("value")+"&dolist_form_firstname="+jQuery("#ipt_firstName").attr("value")+"&dolist_form_email="+jQuery("#ipt_email").attr("value")+"&dolist_form_zipcode="+jQuery("#ipt_zipCode").attr("value");
			parameters += "&dolist_form_customstr1="+jQuery("#slt_departureCity option:selected").attr("value")+"&dolist_form_customstr2="+jQuery("#ipt_destination option:selected").attr("value");
			var URL="dpci="+jQuery("#slt_departureCity option:selected").attr("value")+"&c.de="+jQuery("#ipt_destination option:selected").attr("value");
			if(jQuery("#rbt_knowDepartureDate").is(":checked")){
				var date=jQuery("#ipt_departureDate").attr("value");
				var dmy=date.split("/")[1]+"/"+date.split("/")[2];
				var dd=date.split("/")[0];
				URL+="&dmy="+dmy+"&dd="+dd+"&aj="+jQuery("#ipt_aj  option:selected").attr("value");
			}else{
				URL+="&dmy="+jQuery("#slt_period option:selected").attr("value")+"&minMan="+jQuery("#slt_travelTime").attr("value");
			}
			parameters += "&dolist_form_customstr5="+ jQuery("#ipt_departureDate").attr("value") ;
			parameters += "&dolist_form_customDate1="+ jQuery("#ipt_departureDate").attr("value") ;
			if(jQuery("#ipt_departureDate").attr("value") == ""){
				var dateFormat = jQuery("#slt_period option:selected").attr("value") ;
	        	var dateTable = dateFormat.split('/');
	        	dateFormat = dateTable[1] + "/" + dateTable[0] + "/" + "01" + " 00:00:00";
	        	//alert(dateFormat);
	            parameters += "&dolist_form_customstr6="+ dateFormat ;
			}else{
				parameters += "&dolist_form_customstr6=";
			}
			URL="http://www.voyages-auchan.com/recherche-produits.html?"+URL;
			parameters +="&dolist_form_customstr4="+escape(URL);
			var frequencyValue = "";
			if(jQuery("#frequency p input:checked").attr("value") == "Une fois par semaine"){
			    frequencyValue = 1;
			}else if(jQuery("#frequency p input:checked").attr("value") == "Tous les 15 jours"){
				frequencyValue = 2;
			}else if(jQuery("#frequency p input:checked").attr("value") == "Tous les mois"){
			    frequencyValue = 3;
			}
			parameters +="&dolist_form_customInt1="+frequencyValue;
			parameters +="&dolist_form_customstr3="+jQuery("#ipt_alertName").attr("value");
			parameters += "&redirect=" + "http://www.voyages-auchan.com/CS/static-pages/alert/alert-message-ok.htm";
			parameters += "&redirect_error=" + "http://www.voyages-auchan.com/CS/static-pages/alert/alert-message-error.htm";
			Set_Cookie('departureDate',jQuery("#ipt_departureDate").attr("value"),'720', '/');
			var u = "http://cgi.dolist.fr/member_add.asp?"+parameters+"&KeepThis=true&TB_iframe=true&height=180&width=480&modal=true";
			var t = "Message";
			var g = null;
			tb_show(t, u, g);
		}
		return false;
	});
	
});

