function dontshowerrormessages() {return true;}
window.onerror=dontshowerrormessages;

/*********************************/
/** component buy/pricecalc **/

/*var maxq = 999;*/
var maxq;
function setMaxq(v){
    maxq = v;
}
function inc(prod, rec){
    var el = $('quantity_'+prod);
    var curv =parseInt(el.value,10);
    if(isNaN(curv)){
        curv = 0;
    }

    if(curv >= maxq){
        curv = maxq;
    }else{
        curv++;
    }
    el.value= curv;
    //el(f+'quantity').focus();
    cprice(prod, rec);
}

function dec(prod, rec){
    var el = $('quantity_'+prod);
    var curv =parseInt(el.value,10);
    if(isNaN(curv)){
        curv = 0;
    }
    if(curv <=1){
        curv = 1;
    }else{
        curv--;
    }
    el.value= curv;
    cprice(prod, rec);
}

function fixminmaxbtn(prod){
    var el = $('quantity_'+prod);
    var decbtn = $('dec_buypc_'+prod);
    var incbtn = $('inc_buypc_'+prod);
	
    if(el.value <= 1){
        decbtn.addClassName('minq');
        if(el.value !=""){
            el.value = 1;
        }

    }else{
        decbtn.removeClassName('minq');
        if(el.value >= maxq){
            el.value = maxq;
            incbtn.addClassName('maxq');
        }else{
            incbtn.removeClassName('maxq');
        }
    }
}
function getQty(prod){
    var el = $('quantity_'+prod);
    var q;
    if(el.value==''){
        q=1;
    }else{
        q = parseInt(el.value,10);
    }
    return q;
}
function cprice(prod, rec){
    fixminmaxbtn(prod);
    q=getQty(prod);
//   fixlictype(prod, q);
    try{
        ppl = update_buy_ppl(prod, q);
        update_buy_tp(prod, q, ppl);
    }catch(e){}
    try{
        if(q>=maxq){
            $('tomanylic_'+prod).show();
        }else{
            $('tomanylic_'+prod).hide();
        }
    }catch(e){}
    if(rec){
        recomandNext(prod, q);
    }
}

function update_buy_ppl(prod, q){
    q = parseInt(q,10);
    var pl = $('buypc_pl_'+prod);
	
    preturi=eval('price_p_'+prod);
    step=eval('price_s_'+prod);
    try{
        preturi_discount=eval('price_procent_'+prod);
    }catch(e){
        preturi_discount=0;
    }

    var t = 0;
    var i= 0;
    sl = parseInt(step.length,10);
	
    while((t==0) && i<sl){
        if(q<step[sl-1]){
            if(q>=step[i] && q < step[i+1] ){
                t=1;
            }else{
                i++;
            }
        }else{
            i=sl-1;
            t=1;
        }
    }
    if(preturi_discount instanceof Array){
        pl.innerHTML = preturi[i].toFixed(2);
        pld = $('buypc_pl_discount_'+prod);
        pld.innerHTML = preturi_discount[i].toFixed(2);
        foo = new Array();
        foo['d'] =  preturi_discount[i].toFixed(2);
        foo['f'] =  preturi[i].toFixed(2);
        return foo;
    }else{
        pl.innerHTML = preturi[i].toFixed(2);
        return preturi[i].toFixed(2);
    }

}
function update_buy_tp(prod, q, ppl){
    var tp = $('buypc_tp_'+prod);
    q = parseInt(q,10);
    if(ppl instanceof Array){
        rtpf = q * ppl['f'];
        tp.innerHTML = rtpf.toFixed(2);
		
        rtpd = q * ppl['d'];
        tpd = $('buypc_tp_discount_'+prod);
        tpd.innerHTML = rtpd.toFixed(2);
    }else{
        rtp = q * ppl;
        tp.innerHTML = rtp.toFixed(2);
    }
	/*
    return parseInt(rtp,10);*/
}

function fixlictype(prod, q){
    try{
        el = $('buypc-lictype-'+prod);

        if(q>1){
            el.removeClassName('hide');
        }else{
            el.addClassName('hide');
        }
    }catch(e){}
}

function recomandNext(prod, q){
    qtyRecomand(prod, q);
}
function getNextprices(prod, q){
    q = parseInt(q, 10);
    var pl = $('buypc_pl_'+prod);
	
    preturi=eval('price_p_'+prod);
    step=eval('price_s_'+prod);
    try{
        preturi_discount=eval('price_procent_'+prod);
    }catch(e){
        preturi_discount=0;
    }

    var t = 0;
    var i= 0;
    sl = parseInt(step.length,10);
	
    while((t==0) && i<sl){
        if(q<step[sl-1]){
            if(q>=step[i] && q < step[i+1] ){
                t=1;
            }else{
                i++;
            }
        }else{
            i=sl-1;
            t=1;
        }
    }
    if(i==(sl-1)){
        j=i;
    }else{
        j=i+1;
    }
    if(preturi_discount instanceof Array /*&& i<(sl-1)*/){
        //console.log('avem Preturi cu discount: q='+q+' i='+i+' j='+j);
        pl.innerHTML = preturi[i].toFixed(2);
        pld = $('buypc_pl_discount_'+prod);
        //pld.innerHTML = preturi_discount[i].toFixed(2);
        foo = new Array();
        foo['d'] =  preturi_discount[i].toFixed(2);
        foo['f'] =  preturi[i].toFixed(2);
        foo['nd'] = preturi_discount[j].toFixed(2);
        foo['nf'] =  preturi[j].toFixed(2);
        foo['qn'] = step[j];
        return foo;
    }else{
        //console.log('nu avem discount: q='+q+' i='+i+' j='+j);
        foo = new Array();
        foo['f'] =  preturi[i].toFixed(2);
        foo['nf'] = preturi[j].toFixed(2);
        foo['qn'] = step[j];
        //pl.innerHTML = preturi[i].toFixed(2);
        return foo;
    }
}
function qtyRecomand(prod, q){
    p = getNextprices(prod, q);
    q_next = p['qn'];
	
    if(p['d']){
        a='p[d]';
        pret = p['d']
        pret_next = p['nd'];
    }else{
        a='p[#]';
        pret = p['f'];
        pret_next = p['nf'];
    }
    
    tp = parseFloat(pret*(q+1));
    tp_next = parseFloat(pret_next*q_next);
    tp1 = parseFloat(pret*q);

    if(tp >= tp_next && q < q_next && q > 2){
        $('price_rec_next_qty_'+prod).innerHTML=q_next;
        $('price_rec_next_price_'+prod).innerHTML=(pret_next*q_next).toFixed(2);
        $('price_rec_'+prod).show();
    }else{
        $('price_rec_'+prod).hide();
    }
/*$('price_rec_'+prod).innerHTML=$('price_rec_'+prod).innerHTML+' <hr />buy recomand: q='+q+' q_next='+q_next+
	' total='+(pret*q)+' tp='+tp+' total_next='+(pret_next*q_next)+' pret_1='+pret+' a='+a;*/
}
function acceptRcomand(prod, e){
    $q = $('price_rec_next_qty_'+prod).innerHTML;
    $('quantity_'+prod).value=$q;
    cprice(prod, e);
}

function dosubmitfrm(frmid){
    $(frmid).submit();
}
function slhide(id){
    try{
        $(id).addClassName('hide');
    }catch(e){}
    return true;
}
function slunhide(id){
    try{
        $(id).removeClassName('hide');
    }catch(e){}
    return true;
}

function upgradeS1ContactCancel(){
    slhide('upgradecontact');
    slunhide('upgradesubmit');
    slunhide('linkcontacta');
}

function setupMask(){
    try{
        $$('input.quantity').each(function(e){
            qty_id=e.id;
            setupQtyMask(qty_id);
        });
    }catch(e){}
}
function setupQtyMask(CONTROL){
    cantitateParser = new NumberParser(0, '.', '', false);
    cantitateParser.negativeParenthesis = false;
    cantitateMask = new NumberMask(cantitateParser, CONTROL);
    cantitateMask.allowNegative = false;
    cantitateMask.leftToRight = true;
    return cantitateMask;
}

Event.observe(window, 'load', function() {
    setupMask();
    if(typeof theBTN == 'function') {
        theBTN();
    }
});
/* upgrade step 3 */
function chkMakeSel(id){
    $$('div#upgradeoptions div.option').each(function(e, i){
        $(e).removeClassName('selected');
        if(e.id==id){
            $(id).addClassName('selected') ;
            document.upgradefrm.product[i].checked = true;
        }
    });
	
}

function fixOneWindow(win, event){
    //try{
    Windows.closeAll();
/*console.log(win);
 	try{
 	
 	volume_disc_B4ALD.close();
 	console.log('inchid');
 	}catch(e){
 	console.log('ne def');
 	}
 	
 	
 //}catch(e){}*/
}

function clearTextArea(element,text){
    element=$(element);
    if(text==element.value){
        element.value='';
    }
}
function fillTextArea(element,text){
    element=$(element);
    if(''==element.value){
        element.value=text;
    }
}