


Evonux = {};

Evonux.getCheckedCount = function (msg)
{
    var         chk_all = document.getElementById ("chk_all");
    var         chks = document.getElementsByTagName ("input");
    var         chk;
    var         count = 0;

    for (var i = 0; i < chks.length; i++)
    {
        chk = chks[i];
        if ((chk.type == "checkbox") && (chk.id != "chk_all") && (chk.checked == true))
            count++;
    }    
        
    return count;
}

Evonux.gotoState = function (state)
{7
    var		ids;
    var		frm = document.forms.frmList;

    if (ids = Evonux.getCheckedIds ())
    {
	frm.id.value = ids;
	frm.state.value = state;
	frm.submit ();
    }
}

Evonux.getCheckedIds = function ()
{
    var         chk_all = document.getElementById ("chk_all");
    var         chks = document.getElementsByTagName ("input");
    var         chk;
    var         count = 0;
    var		ids = "";

    for (var i = 0; i < chks.length; i++)
    {
        chk = chks[i];
        if ((chk.type == "checkbox") && (chk.id != "chk_all") && (chk.checked == true))
            ids += chk.id.substr (4)  + "|";
    }
    if (i > 0)
    ids = ids.substr (0, ids.length - 1);
    return (ids);
}

Evonux.checkAll = function (msg)
{
    var         chk_all = document.getElementById ("chk_all");
    var         chks = document.getElementsByTagName ("input");
    var         chk;

    for (var i = 0; i < chks.length; i++)
    {
        chk = chks[i];
        if ((chk.type == "checkbox") && (chk.id != "chk_all"))
            chk.checked = chk_all.checked;
    }    
/*    Evonux.updateId ();*/
}

Evonux.T = function (label)
{
    var label = label.toUpperCase ();
    if (translation[label])
    {
	return (translation[label]);
    }
    else
    {
	return ("<< " + label  + " >>");
    }
}

Evonux.Tu = function (label)
{
    var val = Evonux.T (label);
	  
    
    return (val.charAt (0).toUpperCase () + val.substr (1));
}

Evonux.computeMsg = function (frm, field)
{    
    var		len = field.length;
    var		msg = "";

    if (len > 0)
    {	
	for (i = 0; i < len; i++)
	{
	    msg += "<li>" + field[i] + "</li>";
	}
	msg = Evonux.Tu ("some_fields_are_missing") + "<ul>" + msg + "</ul>";
	document.getElementById ("msg").innerHTML = msg;
	new Effect.Appear ("msg_box");
	return false;
    }
    return true;
}


Evonux.setNumberDot = function (element)
{
    element.value = element.value.replace (",", ".");
}

floatRound = function (nb, precision)
{
    var seed = Math.pow (10, precision);
    return ((Math.round (seed * nb) / seed).toFixed(precision));
}



Evonux.updateCart = function (val)
{
    var	quantity = document.getElementById ("quantity_" + val);
    var	quantity_val = Math.round (parseInt (quantity.value));
    var	subtotal = document.getElementById ("subtotal_" + val);
    var	price = document.getElementById ("price_" + val).innerHTML;
    var num_products = document.getElementById ("num_products").value;
    var num_photos = document.getElementById ("num_photos").value;
    var total = 0;
    
    if (isNaN (quantity.value))
    quantity_val = 0;
    else
    quantity_val = quantity_val;

    quantity.value = floatRound (quantity_val, 2);
    subtotal.innerHTML = floatRound (parseFloat (parseFloat (price) * parseFloat (quantity_val)), 2);

    /* Update total */
    for (var j = 0; j < num_photos; j++)   
    for (var i = 0; i < num_products; i++)
    {
	total += parseFloat (document.getElementById ("price_" + j + "_" + i).innerHTML) * parseFloat (document.getElementById ("quantity_" + j + "_" + i).value);
    }
    document.getElementById ("total").innerHTML = total;
}

var	prod = "";
var	selPic = "";
var	selProd = "";



function	setPic (el, prod_id)
{
    if (prod != "")
	{
	    el.src = document.getElementById (prod).src;
	    var reg = /^[^_]*_(.*)$/;
	    reg.exec (el.id);
	    products[prod_id]["pics"][RegExp.$1] = prod;

	    selProd = prod_id;
	    selPic = RegExp.$1;
	    redrawcart ();
	}
}

function	redrawcart ()
{  
    var		i = 0, j;
    var		total = 0;

    for (var id = 0; id < products.length; id++)
	{
	    if (!products[id])
		continue;
	    var price = products[id]["price"];
	    var num = products[id]["num_pics"];
	    var pic_price = price / num;
	    var pics = products[id]["pics"];
	    var div = document.getElementById ("product_" + id);
	    var content = "";	    
	    var subtotal = 0;
	    
	    for (content = "", j = 0; j < pics.length; j++)
		{
		    var x_price = "";
		    
		    if (pics[j])
			{
			    var coeff = document.getElementById ("coeff_" + pics[j]).innerHTML;
			    var new_price = pic_price * coeff;
			    subtotal += new_price;
			    x_price = new_price + " &euro;";
			}
		    else
			x_price = "";
		    pic = (pics[j] ? "/data/photo/xmini/" + pics[j] + ".png" : "pic/photo.png");		    
		    content += "<div style='float: left; margin-right: 4px; margin-bottom: 4px;'><img id='" + id + "_" + j + "' class='viewpic' style='' src='" + pic + "' alt='' onclick='setPic (this, " + id + ");'><br/>" + x_price + "</div>";
		}
	    i++;
	    content += "<img class='viewpic' style='margin-right: 4px; margin-bottom: 4px;' src='pic/photo-add.png' alt='' onclick='addproduct (" + id + ");'/>";	    	
	    content += "<img class='viewpic' style='margin-right: 4px; margin-bottom: 4px;' src='pic/photo-del.png' alt='' onclick='delproduct (" + id + ");'/>";
	    content += "Total " + (subtotal) + " &euro;";
	    div.innerHTML = content;
	    //	    document.getElementById ("price_" + id).innerHTML = subtotal + "&euro;";
	    total += subtotal;
	    if (i >= products_length)
		continue;
	}
    document.getElementById ("total").innerHTML = "Total " + total + " &euro;";
}

function	addproduct (prod_id)
{
    var len = products[prod_id]['pics'].length;

    for (var i = 0; i < products[prod_id]['num_pics']; i++)
	products[prod_id]['pics'].push (0);
    redrawcart ();
}


function	validatecart ()
{
    var		i = 0, j;
    var		total = 0;
    var		cart = "";
    var		prods = "";
    
    for (var id = 0; id < products.length; id++)
	{
	    if (!products[id])
		continue;	    
	    var price = products[id]["price"];
	    var num = products[id]["num_pics"];
	    var pics = products[id]["pics"];
	    
	    for (prods = "", j = 0; j < pics.length; j++)
		{
		    if (pics[j])
			prods += pics[j] + ":";
		    else
			{			    
			    alert ("Vous avez des photos non selectionnees");
			    return ;
			}
		}
	    if (prods != "")
		cart += id + ":" + prods + "|";
	    i++;
	    if (i >= products_length)
		continue;
	}
    if (cart == "")
	{
	    alert ("Votre panier est vide");
	    return;
	}
    document.frmCart.cart.value = cart;
    document.frmCart.submit ();
}

      

function	delproduct (prod_id)
{ 
    if (products[prod_id]["pics"].length > 0)
	products[prod_id]["pics"].length -= products[prod_id]["num_pics"];
    redrawcart ();
}


function	updateCart ()
{
    var		frm = document.frmCart;
    var		i = 0, j;
    var		total = 0;
    var		cart = "";
    var		prods = "";
    

    for (var id = 0; id < products.length; id++)
	{
	    if (!products[id])
		continue;	    
	    var price = products[id]["price"];
	    var num = products[id]["num_pics"];
	    var pics = products[id]["pics"];
	    
	    for (prods = "", j = 0; j < pics.length; j++)
		{
		    if (pics[j])
			prods += pics[j] + ":";
		    else
			{			    
			    alert ("Vous avez des photos non selectionnees");
			    return ;
			}
		}
	    if (prods != "")
		cart += id + ":" + prods + "|";
	    i++;
	    if (i >= products_length)
		continue;
	}
    if (cart == "")
	{
	    alert ("Votre panier est vide");
	    return;
	}
    frm.state.value = "update";
    frm.cart.value = cart;
    frm.submit ();
}


function	validateOrder ()
{ 
    var		frm = document.frmCart;
    var		val = 0;

    if (!frm.cgv.checked)
	{
	    alert ("Vous devez accepter les Conditions Generales de vente");
	    return false;
	}

    for (var i = 0; i < frm.payment_id.length; i++)
	{
	    if (frm.payment_id[i].checked)
		{
		val = frm.payment_id[i].value;
		}
	}
    if (val == 1)
	{
	    frm.state.value = "validate";
	    frm.action = "/banque/sample/call_request.php";
	    frm.submit ();
	}
    if (val == 2)
	{
	    frm.state.value = "payment_check";
	    frm.action = "index.php";
	    frm.submit ();
	}
    if (val == 3)
	{
	    frm.state.value = "payment_virement";
	    frm.action = "index.php";
	    frm.submit ();
	} 
}


function	gotoweek (sel)
{
	if (sel.selectedIndex)
		document.location = sel.value;
}

Evonux.switchPic = function (img, id)
{
        var     prefix = "/data/pic/";

        if (img.className == "off")
        {
            img.className = "on";
            img.src = prefix + "pic-" + id + ".png";
        }
        else
        {
            img.className = "off";          
            img.src = prefix + "pic-mini-" + id + ".png";
        }
}

Evonux.roll = function (id)
{
    document.getElementById (id).style.display = (document.getElementById (id).style.display == "block" ? "none" : "block")
}
