var xmlHttp;
var data;
var title;
var modal_width;
var modal_height;

function displayModuleAddCaddyProduct(id,title,width,height) 
{
    var tabFields = new Array();
    tabFields[0] = "id="+id;
    strFields = tabFields.join("&");
    modal_width = width;
    modal_height = height;
    var url="/js/onlineshop/ajax/onlineshop_display_module_add_product.php?"+strFields;
    xmlHttp=GetXmlHttpObject(stateDisplayModuleAddCaddyProduct);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
}

function stateDisplayModuleAddCaddyProduct() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
    if (xmlHttp.responseText) {
        Shadowbox.open({
            content:    xmlHttp.responseText,
            player:     "html",
            title:      title,
            width:      modal_width,
            height:     modal_height
        });
    }
} 
}

function processAddProduct(id) 
{
    var tabFields = new Array();
    tabFields[0] = "id="+id;
    strFields = tabFields.join("&");
    var url="/js/onlineshop/ajax/onlineshop_process_add_product.php?"+strFields;
    xmlHttp=GetXmlHttpObject(stateProcessAddProduct);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
}

function stateProcessAddProduct() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
    if (xmlHttp.responseText) {
        eval(xmlHttp.responseText);
        Shadowbox.close();
    }
} 
}

function deleteLineProduct(id) 
{
    var tabFields = new Array();
    tabFields[0] = "id="+id;
    strFields = tabFields.join("&");
    var url="/js/onlineshop/ajax/onlineshop_process_delete_product.php?"+strFields;
    xmlHttp=GetXmlHttpObject(stateDeleteLineProduct);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
}

function stateDeleteLineProduct() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
    if (xmlHttp.responseText) {
        eval(xmlHttp.responseText);
    }
} 
}

function deleteAllLineProduct() 
{
    document.getElementById("application_caddy_content").innerHTML = "";
    document.getElementById("application_caddy_content").className = "loading";
    var url="/js/onlineshop/ajax/onlineshop_productline_delete_all.php";
    xmlHttp=GetXmlHttpObject(stateLineProduct);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
}


function plusQteLineProduct(id) 
{
    document.getElementById("application_caddy_content").innerHTML = "";
    document.getElementById("application_caddy_content").className = "loading";
    var tabFields = new Array();
    tabFields[0] = "line_id="+id;
    strFields = tabFields.join("&");
    var url="/js/onlineshop/ajax/onlineshop_productline_qte_plus.php?"+strFields;
    xmlHttp=GetXmlHttpObject(stateLineProduct);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
}

function minusQteLineProduct(id) 
{
    document.getElementById("application_caddy_content").innerHTML = "";
    document.getElementById("application_caddy_content").className = "loading";
    var tabFields = new Array();
    tabFields[0] = "line_id="+id;
    strFields = tabFields.join("&");
    var url="/js/onlineshop/ajax/onlineshop_productline_qte_minus.php?"+strFields;
    xmlHttp=GetXmlHttpObject(stateLineProduct);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
}

function stateLineProduct() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
    if (xmlHttp.responseText) {
        $("#application_caddy_content").animate({opacity: 0}, 300,"",function() {
            eval(xmlHttp.responseText);
            document.getElementById("application_caddy_content").className = "";
            $("#application_caddy_content").animate({opacity: 1}, 300);
        });
    }
} 
}

function activateCodePromo(code_saisi) 
{
    var tabFields = new Array();
    tabFields[0] = "code_saisi="+document.getElementById("code_promo_input").value;
    strFields = tabFields.join("&");
    var url="/js/onlineshop/ajax/onlineshop_codepromo_activate.php?"+strFields;
    xmlHttp=GetXmlHttpObject(stateCodePromo);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
}

function desactivateCodePromo() 
{
    var url="/js/onlineshop/ajax/onlineshop_codepromo_desactivate.php";
    xmlHttp=GetXmlHttpObject(stateCodePromo);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
}

function stateCodePromo() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
    if (xmlHttp.responseText) {
        eval(xmlHttp.responseText);
    }
} 
}

//----------------------------------------
//Système de mise de datas par MODAL
//----------------------------------------
//Mise en cache des variables communes
var bloc_to_update;
var title_modal;
var width_modal;
var height_modal;
//------------------------------------------------------------------
//Fonction d'affichage de la modal de changement de coordonnées
function getModalDatasForUpdating(bloc,title,width,height,get_vars) {
    bloc_to_update = bloc;
    title_modal = title;
    width_modal = width;
    height_modal = height;
    var tabFields = new Array();
    tabFields[0] = "bloc_to_update="+bloc_to_update;
    tabFields[1] = get_vars;
    strFields = tabFields.join("&");
    var url="/js/onlineshop/ajax/onlineshop_getform_modal_updating.php?"+strFields;
    xmlHttp=GetXmlHttpObject(stateGetModalDatasForUpdating);
    xmlHttp.open("GET", url , true);
    xmlHttp.send(null);
}
//ETAT
function stateGetModalDatasForUpdating() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
    
    
    if (xmlHttp.responseText) {
        eval(xmlHttp.responseText);
        $("#dialog-alerte").dialog({
            resizable: false,                  
            width:width_modal,
            height:height_modal,
            modal: true,
            title: title_modal,
            show:{
                effect:"drop",
                options:{direction:"left"},
                speed:500
            },
            hide:{
                effect:"drop",
                options:{direction:"left"},
                speed:500
            },
            buttons: {
                'Enregistrer': function() {
                        checkForm(bloc_to_update);
                },
                'Annuler': function() {
                        $(this).dialog('close');
                }
            }
        });
    }
} 
}

function paymentOnlineshop(title,contenu,modal_width,modal_height) {
    if (document.getElementById("accept_cgv").checked == true && document.getElementById("type_paiement_id").value != 0) {
        var tabFields = new Array();
        strFields = tabFields.join("&");
        var url="/js/onlineshop/paybox/insert_transaction.php";
        xmlHttp=GetXmlHttpObject(statePaymentOnlineshop);
        xmlHttp.open("POST", url , true);
        xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
        xmlHttp.send(strFields);
    } else {
        Shadowbox.open({
            content:    contenu,
            player:     "html",
            title:      title,
            width:      modal_width,
            height:     modal_height
        });
    }
}

//ETAT
function statePaymentOnlineshop() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
    if (xmlHttp.responseText) {
        eval(xmlHttp.responseText);
        document.payboxform.submit();
    }
} 
}

//------------------------------------------------------------------
//Fonction d'enregistrement des informations saisie pour le changement de coordonnées
function processModalDatasForUpdating(bloc) {
    bloc_to_update = bloc;
    document.getElementById(bloc_to_update).innerHTML = "";
    document.getElementById(bloc_to_update).className = "loading_auto";
    var tabFields = new Array();
    switch (bloc_to_update) {
        case "contact_adresse_facturation":
            tabFields[0] = "bloc_to_update="+bloc_to_update;
            tabFields[1] = "contact_id="+document.getElementById("f1-id").value;
            tabFields[2] = "adresse1="+document.getElementById("f1-adresse1").value;
            tabFields[3] = "adresse2="+document.getElementById("f1-adresse2").value;
            tabFields[4] = "cp="+document.getElementById("f1-cp").value;
            tabFields[5] = "ville="+document.getElementById("f1-ville").value;
            tabFields[6] = "iso_pays="+document.getElementById("f1-iso_pays").value;
            strFields = tabFields.join("&");
        break;
        case "contact_adresse_livraison":
            tabFields[0] = "bloc_to_update="+bloc_to_update;
            tabFields[1] = "contact_address_id="+document.getElementById("f1-id").value;
            tabFields[2] = "contact_id="+document.getElementById("f1-contact_id").value;
            tabFields[3] = "label_contact="+document.getElementById("f1-label_contact").value;
            tabFields[4] = "adresse1="+document.getElementById("f1-adresse1").value;
            tabFields[5] = "adresse2="+document.getElementById("f1-adresse2").value;
            tabFields[6] = "cp="+document.getElementById("f1-cp").value;
            tabFields[7] = "ville="+document.getElementById("f1-ville").value;
            tabFields[8] = "iso_pays="+document.getElementById("f1-iso_pays").value;
            strFields = tabFields.join("&");
        break;
    }
    var url="/js/onlineshop/ajax/onlineshop_process_modal_updating.php";
    xmlHttp=GetXmlHttpObject(stateProcessModalDatasForUpdating);
    xmlHttp.open("POST", url , true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");                  
    xmlHttp.send(strFields);
}
//ETAT
function stateProcessModalDatasForUpdating() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
    if (xmlHttp.responseText) {
        $("#"+bloc_to_update).animate({opacity: 0}, 300,"",function() {
            eval(xmlHttp.responseText);
            document.getElementById(bloc_to_update).className = "";
            $("#"+bloc_to_update).animate({opacity: 1}, 300);
        });
    }
} 
}

//FIN ------------------------------------
//Système de mise de datas par MODAL
//----------------------------------------

//-----------------------------------------------------
//Fonction de communication globale
//-----------------------------------------------------
function GetXmlHttpObject(handler)
{ 
var objXmlHttp=null

if (navigator.userAgent.indexOf("Opera")>=0)
{
alert("This example doesn't work in Opera") 
return 
}
if (navigator.userAgent.indexOf("MSIE")>=0)
{ 
var strName="Msxml2.XMLHTTP"
if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
{
strName="Microsoft.XMLHTTP"
} 
try
{ 
objXmlHttp=new ActiveXObject(strName)
objXmlHttp.onreadystatechange=handler 
return objXmlHttp
} 
catch(e)
{ 
alert("Error. Scripting for ActiveX might be disabled") 
return 
} 
} 
if (navigator.userAgent.indexOf("Mozilla")>=0)
{
objXmlHttp=new XMLHttpRequest()
objXmlHttp.onload=handler
objXmlHttp.onerror=handler 
return objXmlHttp
}
} 

function number_format(number, decimals, dec_point, thousands_sep) {
    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }    return s.join(dec);
}

