//--------------------------------------------------------------------------
function getListText(list)
{
    return list.options[list.selectedIndex].text;
}

//--------------------------------------------------------------------------
function setup_gift_ecom(frm, delivery)
{
	var descr, code;
	// costruisce il codice completo, da mettere il product ID, in modo che
	// tenga conto delle varianti
	code = frm.Codice.value + frm.occasione.value; 
	descr = frm.Descr.value + " - " + getListText(frm.occasione) + delivery + frm.data_consegna.value;

	frm.ProductID.value = code;
	frm.Description.value = descr;
	frm.SubmitId.value = rand(1000000);

    // alert("ProductID: " + frm.ProductID.value);
    // alert("ItemPrice: " + frm.ItemPrice.value);
    // alert("SubmitId: " + frm.SubmitId.value);
	return 0;
}

//--------------------------------------------------------------------------
// usato per ristemare la gestione delle option list in firefox
function fire_event(element,event)
{
    if (document.createEventObject)
    {
        // dispatch for IE
        var evt = document.createEventObject();
        return element.fireEvent('on'+event,evt)
    }
    else
    {
        // dispatch for firefox + others
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, true, true ); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    }
}
function ff_fixchange(obj)
{
    // gestisce l'evento onkeydown nella select che in FireFox non genera l'evento
    // onChange, mentre lo fa in Explorer ed in Opera
    if (is_firefox) fire_event(obj, 'change');
}
//--------------------------------------------------------------------------
// genera un id random, usato per evitare duplicazioni di inserimento degli oggetti
function rand(n)
{
  return ( Math.floor ( Math.random ( ) * n + 1 ) );
}
//--------------------------------------------------------------------------
String.prototype.trim = function () {  return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");};
//--------------------------------------------------------------------------
function format_currency(val)
{
	val = val.toString().replace("/\€|\./g", "");
	if (isNaN(val))
		val = "0";
	sign = (val == (val = Math.abs(val)));
	val = Math.floor(val*100+0.50000000001);
	cents = val % 100;
	val = Math.floor(val/100).toString();
	if (cents<10)
 	cents = "0" + cents;
	for (var i = 0; i < Math.floor((val.length-(1+i))/3); i++)
		val = val.substring(0,val.length-(4*i+3))+"."+
		      val.substring(val.length-(4*i+3));
	return (((sign)?'':'-') + "€ " + val + "," + cents);
}
function ftm_cur(val)
{
    // come format_currency, ma senza il prefisso della valuta
	val = val.toString().replace("/\€|\./g", "");
	if (isNaN(val))
		val = "0";
	sign = (val == (val = Math.abs(val)));
	val = Math.floor(val*100+0.50000000001);
	cents = val % 100;
	val = Math.floor(val/100).toString();
	if (cents<10)
 	cents = "0" + cents;
	for (var i = 0; i < Math.floor((val.length-(1+i))/3); i++)
		val = val.substring(0,val.length-(4*i+3))+"."+
		      val.substring(val.length-(4*i+3));
	return (((sign)?'':'-') + val + "," + cents);
}
//--------------------------------------------------------------------------
function cvtcur(val)
{
	// restitituisce un float convertito da currency
	if (typeof val == "number") return val;
	if (typeof val != "string") return 0.0;
	val = val.replace("€", "");		// elimina il segno di valuta
	val = val.replace(".", "");		// elimino i separatori delle migliaia
	val = val.replace(",", ".");		// . al posto di ,
	return parseFloat(val);
}
//--------------------------------------------------------------------------
function check_qta(field, default_value)
{
    // controlla che il numero inserito nel campo sia numerico, positivo e non esagerato, in caso
    // contrario inserire il valore di default
    if (isNaN(field.value)) 
        field.value = default_value;
    else
        if (field.value<0) 
            field.value = default_value;
        else
            if (field.value>999) 
                field.value = 999;
}
function check_min_qta(field, min_qta, default_value)
{
    // controlla che il numero inserito nel campo sia numerico, positivo, >= alla quantità minima e non esagerato, in caso
    // contrario inserire il valore di default
    if (isNaN(field.value)) 
        field.value = default_value;
    else
        if (field.value<min_qta) 
            field.value = default_value;
        else
            if (field.value>999) 
                field.value = 999;
}
function check_multiple_qta(field, multiple, default_value, max_value)
{
    // controlla che il numero inserito nel campo sia numerico, positivo e non esagerato, in caso
    // contrario inserire il valore di default
    // inoltre si assicura che sia un multiplo del valore indicato
    var fv = field.value;
    var v = fv;
    if (isNaN(fv)) 
        v = default_value;
    else
        if (fv<0) 
            v = default_value;
        else
            if (fv>max_value) 
                v = max_value;
    v = parseInt(v);
    if ((v%multiple)>0)
        v = v + (multiple - (v%multiple));
    field.value = v;
}

//--------------------------------------------------------------------------
function getViewportHeight()
{
    // document.viewport.getHeight() di prototype (fino alla 1.6.2) non sembra
    // funzionare con Opera 9.5 (funzionava con le versioni precedenti)

    var viewportheight;
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined')
        viewportheight = window.innerHeight;
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
       viewportheight = document.documentElement.clientHeight
    // older versions of IE
    else
       viewportheight = document.getElementsByTagName('body')[0].clientHeight;
    return viewportheight;
}
function getViewportWidth()
{
    var viewportwidth;
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined')
        viewportwidth = window.innerWidth;
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
       viewportwidth = document.documentElement.clientWidth;
    // older versions of IE
    else
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth;
    return viewportwidth;
 }


