jQuery(document).ready(function() {
        var options = {
         beforeSubmit:  validateAdd,  // pre-submit callback
         success:       afterAdd,  // post-submit callback
         dataType : 'json'
    };

     // bind form using 'ajaxForm'
     jQuery('#frmProduct').ajaxForm(options);

});
//var prod_id = {$product_id};
var prod_id = jQuery('#pid').val();
function getSizesForProductAndColor(color_id){
	var prod_id = jQuery('#pid').val();
	var objSelect = document.getElementById("sizes");
	objSelect.options.length = 0;
	if(parseInt(color_id) == 0){
		objSelect.options[0] = new Option("Sélectionnez",0);
		jQuery("#divDisponibilite").slideUp();
		jQuery("#actionButtons").fadeOut();
		jQuery("#product_prix").hide();
		jQuery("#price_start").show();
		return false;
	}
	jQuery.getJSON("request.php",{method:0,prod_id:prod_id,color_id:color_id},
		function(json){
			if(typeof(json.items) != "undefined" && typeof(json)!= "undefined"){
      			jQuery.each(json.items,
	     			function(i,item){
      					objSelect.options[i] = new Option(item.size_value, item.size_id);
      				});
			}
			changeColorImage(jQuery('#colors option:selected').val());
			//getDataForProductWithColorAndStock(jQuery("#sizes").val());
			getContenantForProductAndColorAndSizes(jQuery("#sizes").val());
		});
}
function getContenantForProductAndColorAndSizes(size_id){
	var prod_id = jQuery('#pid').val();
	var color_id = jQuery('#colors').val();
	var objSelect = document.getElementById("contenants");
	objSelect.options.length = 0;
	if(parseInt(color_id) == 0){
		objSelect.options[0] = new Option("Sélectionnez",0);
		jQuery("#divDisponibilite").slideUp();
		jQuery("#actionButtons").fadeOut();
		jQuery("#product_prix").hide();
		jQuery("#price_start").show();
		return false;
	}
	jQuery.getJSON("request.php",{method:3,prod_id:prod_id,color_id:color_id,size_id:size_id},
		function(json){
			if(typeof(json.items) != "undefined" && typeof(json)!= "undefined"){
      			jQuery.each(json.items,
	     			function(i,item){
      					objSelect.options[i] = new Option(item.contenant_value, item.contenant_id);
      				});
			}
			//changeColorImage(jQuery('#colors option:selected').val());
			getDataForProductWithColorAndStock(jQuery("#contenants").val());
		});
}
function getDataForProductWithColorAndStock(contenant_id){
	//alert("contenant"+contenant_id);
	var color_id = jQuery('#colors').val();
	var size_id = jQuery('#sizes').val();
	var finalPrice = 0;
	var prod_id = jQuery('#pid').val();

	jQuery.getJSON("request.php",{method:1,prod_id:prod_id,color_id:color_id,size_id:size_id,contenant_id:contenant_id},
			function(json){
				jQuery("#price-new").html("");
				jQuery("#price-old").html("");
				jQuery("#vat_id").val(json.vat_id);
				jQuery("#vat_value").html(json.vat_value);
				if(json.pricepromo != "undefined" && parseFloat(json.pricepromo) > 0){
				   // jQuery("#info-price-orig").show();
                    // jQuery("#price").html(json.pricepromo+"&nbsp;&euro;"+"<del>"+json.price+"&euro;</del>");
                    jQuery("#price-new").html(json.pricepromo+" &euro;");
                    jQuery("#price-old").html("<del>"+json.price+"&nbsp;&euro;</del>");
                    jQuery("#info-price-old").show();
                    finalPrice = parseFloat(json.pricepromo);
				}else{
				    jQuery("#info-price-old").hide();
                    jQuery("#price-new").html(json.price+" &euro;");
					  jQuery("#price-orig").html(json.price+" &euro;");
					  finalPrice = parseFloat(json.price);
				}
				if(finalPrice <= 0 || parseInt(json.stock) <= 0){
				   jQuery("#actionButtons").fadeOut();
				  	jQuery("#divDisponibilite").slideDown();
				}else{
				 jQuery("#actionButtons").fadeIn();
					jQuery("#divDisponibilite").slideUp();
				}
				if(finalPrice > 0){
			  		jQuery("#product_prix").show();
			  		jQuery("#price_start").hide();
			  	}
			}
	);
}
function validateAdd(){
  	var nQty = jQuery("#quantity").val();
  	var txtErr = "";
  	if(jQuery("#colors").val() == "0"){
  		txtErr += "Vous devez saisir une parfum !" + "<br/>";
  	}
  	if(jQuery("#sizes").val() == "0"){
  		txtErr += "Vous devez saisir une poids !" + "<br/>";
  	}
    if(jQuery("#contenants").val() == "0"){
  		txtErr += "Vous devez saisir un contenant !" + "<br/>";
  	}
  	if(isNumber(nQty) == false || nQty < 1){
  		txtErr += "La quantit&eacute; doit &ecirc;tre un num&eacute;ro !" + "<br />";
  	}

  	if (txtErr != ""){
     	 jError(txtErr, "Erreurs");
     	 return false;
  	}
  	return true;
}
function afterAdd(json){
	if(typeof(json) != "undefined" && json != null){
		if(typeof(json.message) != "undefined" && json.message != null){
		    if(json.messagetitle=='Succ&egrave;s'){
			jAlertNoButton(json.message,json.messagetitle,function(){ loadCartRight() ;  });

          }
          else{
            jAlert(json.message,json.messagetitle,function(){});
          }
		}
		if(typeof(json.total) != "undefined" && json.total != null && parseFloat(json.total) != Number.NaN){
			jQuery("#cartTotal").html(json.total+"&nbsp;&euro;");
		}
		if(typeof(json.items) != "undefined" && json.items != null && parseInt(json.items) != Number.NaN){
		    jQuery("#flashDiv2").hide();
			jQuery("#cartArticles").html(json.items);
		}
	}
	// alert(json);
}
function loadCartRight()
{
	jQuery('#mycartRight').load('modules/cart/tpl/cart-right.tpl.php');
}
function changeImage(src, href){
	//alert(src);
    // jQuery("#imgBig").attr("src", src);
    jQuery("#aBig").attr("href", href);
    jQuery("#aBig").css("background-image", "url(" + src + ")");
}
 function serializeToJSON(strTxt){
    var strJSON = "{";
    arrTmp = strTxt.split("&");
    for(i =  0; i < arrTmp.length; i++){
       arrTmp2 = arrTmp[i].split("=");
       if (arrTmp2[0] == "quantity")
            continue;

       if (strJSON!="{")
           strJSON+=","
       strJSON +="'"+arrTmp2[0]+"':'"+arrTmp2[1]+"'";
    }
    strJSON+="}";
      return strJSON;
}
 jQuery(document).ready(function() { 
	    var options = { 
	        beforeSubmit:  validateAdd,  // pre-submit callback
	        success:       afterAdd,  // post-submit callback
	        dataType : 'json'
	   }; 
	 
	    // bind form using 'ajaxForm'
//	   / jQuery('#frmProduct').ajaxForm(options); 
	}); 

 function changeColorImage(colorname){
	  if(colorname!=""){
	      jQuery("a[name='"+colorname+"']").mouseover();
	  }

}


