var num_lic=0;
var unit_price=0.0;
var the_bonus=0.0;
var the_total=0.0;
var prodnameArray=   ("MP2S;FLSL;MP2FSL").split(";");
var produpr1Array=   ("16;15;26").split(";");
var produpr2Array=   ("15;14;25").split(";");
var produpr10Array=  ("12;13;23").split(";");//13
var produpr20Array=  ("10;12;22").split(";");//12
var produprsiteArray=("300;300;530").split(";");
var produprwwArray=  ("1000;900;1600").split(";");
var formobj=document.ppal_it;

function formChoiceHandler(theform)
{
 if (theform == 'ppal_it')
    { formobj=document.ppal_it; }
  else
    { formobj=document.ppal_us; }
 var prdChoiceNum = formobj.prd.selectedIndex;
 var prdChoice = formobj.prd.options[prdChoiceNum].value;
 
 var prdLicType = formobj.lictype.selectedIndex;
 unit_price=getUpr(prdLicType, prdChoiceNum);

 num_lic=checkNlic(prdLicType, parseInt(formobj.nlic.value));
 formobj.nlic.value=num_lic;

 the_bonus=parseFloat(formobj.bonus.value);
 if ((the_bonus < 0) || isNaN(the_bonus)) { the_bonus = 0; }
 formobj.bonus.value=the_bonus;
    
 the_total= the_bonus + num_lic * unit_price;
 formobj.amount.value=the_total;
 if (theform == 'ppal_it') {
        setContent("upr_divIT", "Prezzo unitario: <b>" + unit_price + " &euro;</b>");
        setContent("tot_divIT", the_total + " &euro; (Euro)");
         formobj.item_name.value=num_lic + " licenz"+(num_lic > 1?"e":"a")
        + (prdLicType<4?"":(prdLicType==5?" (Worldwide)":" (Site)"))
        + " per " + prdChoice + (the_bonus>0?" e donazione extra.":"");
     }
 else {
        setContent("upr_divUS", "Unit price: <b>$ " + unit_price + "  USD</b>");
        setContent("tot_divUS", "&#36; "+ the_total + " USD");
         formobj.item_name.value=num_lic + " license"+(num_lic > 1?"s":"")
        + (prdLicType<4?"":(prdLicType==5?" (Worldwide)":" (Site)"))
        + " for " + prdChoice + (the_bonus>0?" and extra donation.":"");
    }
}

function checkNlic(licKind, lics)
{ //licKind: 0=single, 1=2-9, 2=10-19, 3=20+, 4=site, 5=wwide
    if (isNaN(lics)) { return(checkNlic(licKind, 0)); }
    switch (licKind){
        case 0: return(1); break;
        case 1: if (lics < 2)  { return(2); }
                if (lics > 9)  { return(9); }
                break;
        case 2: if (lics < 10)  { return(10); }
                if (lics > 19)  { return(19); }
                break;
        case 3: if (lics < 20)  { return(20); }
                break;
        case 4: return(1); break;
        case 5: return(1); break;
        default: return(1); break;
    }
    return(lics);
}

function getUpr(licKind, idx)
{
    switch (licKind){
        case 0: return(parseFloat(produpr1Array[idx])); break;
        case 1: return(parseFloat(produpr2Array[idx])); break;
        case 2: return(parseFloat(produpr10Array[idx])); break;
        case 3: return(parseFloat(produpr20Array[idx])); break;
        case 4: return(parseFloat(produprsiteArray[idx])); break;
        case 5: return(parseFloat(produprwwArray[idx])); break;
        default: return(0); break;
    }
}

function validate(theform)
{
 formChoiceHandler(theform);
 if (theform == 'ppal_it') {
    if (the_total > 0) {
        if ( confirm("Stai acquistando " + num_lic + " licenz" + (num_lic > 1?"e":"a")
            +"\n  per " + formobj.prd.options[formobj.prd.selectedIndex].text
            +"\n  (prezzo unitario: Euro " + unit_price +")"
            + (the_bonus>0?"\nFai una donazione :) di Euro " + the_bonus:"")
            + "\nPer un totale di Euro " + the_total
            + "\n\nProcedo con l'acquisto?") )
            { formobj.submit();
              return(true); }
        else
            { formobj.nlic.focus(); 
              return(false); }
    }
    else { alert("Non posso procedere. Verifica i dati inseriti."); }
    formobj.nlic.focus();
    return(false);
 }
 else {
    if (the_total > 0) {
        if ( confirm("You're buying " + num_lic + " license" + (num_lic > 1?"s":"")
            +"\n  for " + formobj.prd.options[formobj.prd.selectedIndex].text
            +"\n  (unit price: $ " + unit_price +" USD)"
            + (the_bonus>0?"\nand making a donation :) of $ " + the_bonus+" USD":"")
            + "\nFor a sum of $ " + the_total
            + "\n\nDo you want to proceed?") )
            { formobj.submit();
            return(true); }
        else
            { formobj.nlic.focus(); 
              return(false); }
    }
    else { alert("Cannot proceed. Verify inserted data."); }
    formobj.nlic.focus();
    return(false);
    }
}

// Dynamic Content
// (c) 2002 Premshree Pillai
// http://premshree.resource-locator.com
var ie4=document.all&&navigator.userAgent.indexOf("Opera")==-1;
var ns6=document.getElementById&&navigator.userAgent.indexOf("Opera")==-1;
var ns4=document.layers;
function setContent(fieldname, content)
{
  switch (fieldname){
    case "upr_divUS":if(ie4) { showContentObj=document.all.upr_divUS; }
                     if(ns4)
                     { document.nsupr_divUS.document.write(content);
                       document.nsupr_divUS.document.close(); }
                    break;
    case "upr_divIT":if(ie4) { showContentObj=document.all.upr_divIT; }
                     if(ns4)
                     { document.nsupr_divIT.document.write(content);
                       document.nsupr_divIT.document.close(); }
                    break;
    case "tot_divUS":if(ie4) { showContentObj=document.all.tot_divUS; }
                     if(ns4)
                     { document.nstot_divUS.document.write(content);
                       document.nstot_divUS.document.close(); }
                    break;
    case "tot_divIT": if(ie4) { showContentObj=document.all.tot_divIT; }
                     if(ns4)
                     { document.nstot_divIT.document.write(content);
                       document.nstot_divIT.document.close(); }
                    break;
  }

 if(ns6) { showContentObj=document.getElementById(fieldname); }
 if(ie4||ns6)
  { if(showContentObj.innerHTML!=content) { showContentObj.innerHTML=content; }
    else { showContentObj.innerHTML=""; }
   }
}
