var http = createRequestObject();
var pos = createRequestObject();
var IsEval = false;
function resetMenu()
{
    if (document.getElementById('cat'))
        document.getElementById('cat').selectedIndex = 0;
    if (document.getElementById('mar'))
        document.getElementById('mar').selectedIndex = 0;
    if (document.getElementById('menu'))
        document.getElementById('menu').selectedIndex = 0;
}

function createRequestObject() 
{
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer")
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    else
        ro = new XMLHttpRequest();
    return ro;
}

function sndReq(action, complete)
{
    if (complete)
    {
        var req = new Request({
            method: 'get',
            url : 'ajax/ajax_search.php',
            onComplete : function (response) {
                update = response.split('^');
                $(update[0]).set({'html' : update[1]});
                complete();
            }
        }).send('action='+action);
        return;
    }
    http.open('get', 'ajax/ajax_search.php?action='+action);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function sndUpdateProdReq(action, update, quantite, idChamp)
{
    if (quantite == 0)
    {
        if (confirm('Effacer ce produit ?'))
        {
            document.getElementById(idChamp).value = "";
            http.open('get', 'ajax/ajax_panier.php?action='+action);
        }
        else
        {
            document.getElementById('qty' + update).value = document.getElementById('checkqty' + update).value;
            document.getElementById('ok' + update).innerHTML = '<img src="images/vide.gif" width="17" />';
            return (0);
        }
    }
    if (quantite == document.getElementById('checkqty' + update).value)
        return (0);
    http.open('get', 'ajax/ajax_panier.php?action='+action);
    //alert(action);
    http.send(null);
    http.onreadystatechange = updateCartStatus;
    return (0);
}

function deleteOrder(id, lg)
{
    if (lg == "fr")
        error = "Voulez vous vraiment effacer cette commande ?";
    else
        error = "Do you really want to delete this order ?";
    if (confirm(error))
        document.location = "home.php?rub=editorders&action=delete&cartID=" + id;
}
function updateCartStatus()
{
    if(http.readyState == 4)
    {
        var response = http.responseText;
        var update = new Array();
        //alert(response);
        if(response.indexOf('^' != -1))
        {
            update = response.split('^');
            document.getElementById(update[0]).innerHTML = update[1] + '<br />';
        }
    }
    window.document.location = 'home.php?rub=cart';
}

function sndProdReq(action, update, quantite, idChamp)
{
    http.open('get', 'ajax/ajax_panier.php?action='+action);
    var tmp = 'ok'+ update;
    if (quantite != 0)
        document.getElementById(tmp).src = 'images/btn_caddie2.gif';
    else
    {
        document.getElementById(idChamp).value = "";
        document.getElementById(tmp).src = 'images/btn_caddie.gif';
    }
    http.onreadystatechange = handleResponse;
    http.send(null);
}


function handleResponse() 
{
    if(http.readyState == 4)
    {
        var response = http.responseText;
        var update = new Array();
        if(response.indexOf('^' != -1))
        {
            update = response.split('^');
            document.getElementById(update[0]).innerHTML=update[1];
            if (IsEval == true)
                eval(document.getElementById(update[0]).innerHTML);
            IsEval = false;
        }
    }
}


function refSearch(keywords, SSID)
{	
    resetMenu();
    if (keywords.length > 2)
    {
        document.getElementById('debug').innerHTML = '<img src="images/loading.gif" height="32" /><br />Recherche en cours...';
        sndReq('search&filter=ref&keywords=' + keywords + '&userId=' + SSID, initZoomDetail);
    } else {
        document.getElementById('debug').innerHTML = '<b>Tapez plus de 3 caract&egrave;res pour lancer une recherche.</b>';
    }
}

function sndDepReq(action)
{
    http.open('get', '/ajax/ajax_user.php?action='+action);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function showLoading()
{
    document.getElementById('debug').innerHTML = '<table width="100%"><tr><td width="37"><img src="images/vide.gif" width="37" height="1" /></td><td align="center" style="font-weight: bold"><a href="home.php?rub=users&tri=i.cpt_g ASC"><img src="images/downarrow.png" /></a><a href="home.php?rub=users&tri=i.cpt_g DESC"><img src="images/uparrow.png" /></a><br/>N&deg;</td><td><img src="images/vide.gif" width="250" height="1" />&nbsp;</td><td><img src="images/vide.gif" width="150" height="1" />&nbsp;</td><td>&nbsp;</td><td align="center" style="font-weight: bold"><a href="home.php?rub=users&tri=CA ASC"><img src="images/downarrow.png" /></a><a href="home.php?rub=users&tri=CA DESC"><img src="images/uparrow.png" /></a><br/>CA</td><td align="center" style="font-weight: bold"><a href="home.php?rub=users&tri=NBCMD ASC"><img src="images/downarrow.png" /></a><a href="home.php?rub=users&tri=NBCMD DESC"><img src="images/uparrow.png" /></a><br/>CMD</td><td align="center" style="font-weight: bold"><a href="home.php?rub=users&tri=i.date_creation ASC"><img src="images/downarrow.png" /></a><a href="home.php?rub=users&tri=i.date_creation DESC"><img src="images/uparrow.png" /></a><br/>CREATION</td><td>&nbsp;</td><td>&nbsp;</td></tr><tr><td colspan="10" align="center"><img src="images/loading.gif" height="32"/><br/>Recherche en cours...</td></tr></table>';
}

function ajaxReq(action, SSID, page)
{
    http.open('get', '/ajax/ajax_'+page+'.php?action='+action+'&SSID='+SSID);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

function depSearch(keywords, SSID, bool)
{
    showLoading();
    IsEval = true;
    if (keywords.length > 1)
    {
        if (!bool || bool == "undefined")
            sndDepReq('search&filter=ref&keywords=' + keywords + '&userId=' + SSID);
        else
            ajaxReq('search&filter=ref&keywords=' + keywords, SSID, 'validation');
    }
}

function usersearch(keywords, SSID, bool)
{
    showLoading();
    IsEval = true;
    if (keywords.length > 1)
    {
        if (!bool || bool == "undefined")
            sndDepReq('search&filter=nom&keywords=' + keywords + '&userId=' + SSID);
        else
            ajaxReq('search&filter=nom&keywords=' + keywords, SSID, 'validation');
    }
}
function powasearch(keywords, SSID) 
{
    var url = document.location.href;
    var reg = new RegExp("[?]+", "g");
    var tab = url.split(reg);
    if (keywords.length > 2)
    {
        resetMenu();
        sndReq('search&filter=nom&keywords='+keywords+'&'+tab[1] + '&userId=' + SSID);
    } else {
        document.getElementById('debug').innerHTML = '<b>Tapez plus de 3 caract&egrave;res pour lancer une recherche.</b>';
    }
		
}

function createOrder(lg)
{	
    tmp = document.getElementById('intitule').value;
    if (tmp != "")
        document.location = 'home.php?rub=editorders&action=create&name=' + tmp;
    else
    if (lg == "FR")
        alert('Entrez un intitule pour la commande');
    else
        alert('Please enter a name');
}

function drawOrderDivConfirm(url)
{
    return ('<form action="'+url+'" method="post">' +
        '<p style="font-size:14px; font-weight:bold">Voulez vous envoyer la commande ?</p>' +
        '<p align="left"><u>Commentaire</u> : </p><textarea name="com" cols="40"></textarea><p>' +
        '<span style="float: left"><input type="image" src="images/FR/envoyer_commande.png" value="envoyer" /></span>' +
        '<span style="float: right"><input type="button" onClick="cache(\'curseur\')" value="annuler" /></span></p>' +
        '</form>');
}

function drawDeleteUser(url)
{
    return ('<form action="'+url+'" method="post">' +
        '<p style="font-size:14px; font-weight:bold">Voulez vous supprimer ce revendeur ?</p>' +
        '<span style="float: left"><input type="image" src="images/FR/envoyer_commande.png" value="envoyer" /></span>' +
        '<span style="float: right"><input type="button" onClick="cache(\'curseur\')" value="annuler" /></span></p>' +
        '</form>');
}

function confirmUrl(func)
{
    i = true;
    var c = GetId("curseur");
    c.style.width = 400 + "px";
    c.style.left = (window.screen.width - 400) / 2;
    c.style.top = (window.screen.height - 500) / 2 + document.body.scrollTop + "px";
    c.style.visibility="visible"; // Si il est cacher (la verif n'est qu'une securitÃ©) on le rend visible.
    c.style.display = "block";
    c.innerHTML = eval(func);
}

function GetId(id)
{
    return document.getElementById(id);
}
function _ghandleResponse() 
{
    if(pos.readyState == 4)
    {
        var response = pos.responseText;
        var update = new Array();
        alert(response);
        if(response.indexOf('^' != -1))
        {
            update = response.split('^');
            document.getElementById(update[0]).innerHTML = update[1] + '<br />';
        } else {
            alert('Erreur.');
        }
    }
}
function sendPos(x, y, bool, id)
{
    //alert('/ajax/ajax_user.php?action=pos&a=1&x='+x+'&y='+y+'id='+id);
    if (bool)
        pos.open('get', '/ajax/ajax_user.php?action=pos&a=2&x='+x+'&y='+y+'&id='+id);
    else
        pos.open('get', '/ajax/ajax_user.php?action=pos&a=1&x='+x+'&y='+y+'&id='+id);
    pos.onreadystatechange = _ghandleResponse;
    pos.send(null);
}
