
//**************************************************
//* (c) 2006 by M.E. Koch, http://www.AdOptimize.de
//**************************************************
    function kaufm(x) { 
        var k = (Math.round(x * 100) / 100).toString(); 
            k += (k.indexOf('.') == -1) ? '.00' : '00'; 
        return k.substring(0, k.indexOf('.') + 3); 
    } // ENDE kaufm

    function trim(string) {
        o = string.replace(/^\s*(S)\s*$/, '$1');
        return(o);
    }

    var addprc  = 0;
    var addstat = 0;
    var pprice  = 0;
    var tprice  = 0;
    var calc2   = 0;
    var prozentPrice = 0;
    var prozent = 0;
    var uprop;


    function calculate_selected(inp, form, prop) {
        if(prop) uprop = prop;
        if(!inp) {
            tblc = getlayer('text_price_info', 2);
            tblc.innerHTML = '';
            tform  = document.forms['cart_quantity'];
            tflen  = tform.length;
            for(i=0; i<tflen; i++) {
                if(tform.elements[i].type== 'select-one') calculate_selected(tform.elements[i]);
            }

            return true;

        }
        tform  = document.forms['cart_quantity'];
        tcalc1 = (inp.options[inp.options.selectedIndex].text);
            
        //sernum = /(\d.+\b)/i; // just get the numbers
        sernum = /([0-9,]+)/i; // just get the numbers
        sernum2 = /([\+\-0-9,\.]+)\s([a-z]{3})/i; // just get the numbers
        sernum3 = /([0-9\.]*)(,[0-9]{2})/;
        sernum4 = /([0-9,]+)(\.[0-9]{2})/;
        justnum = /[^a-z\s]+/i;


        if((tprice == '') || (tprice== 0)) tprice = orgprice;
        tprice  = justnum.exec(tprice);
            // convert to a String 
        tStringPrice = new String(tprice);
        tprice  = tStringPrice.replace(',', '.');
            // convert to a number
        tprice  = parseFloat(tprice);
            // get rid of pence


        if(tcalc1.indexOf("%")!= -1) {
            // strippen der ungwollten inhalte
            sernum.exec(tcalc1);
            calc1  = (RegExp.$1).replace(',', '.') / 100;
            prozent      = parseFloat(calc1);
            
        } else if(tcalc1.indexOf("EUR")!= -1 ){
            sernum2.exec(tcalc1);
            calc2 = (RegExp.$1);
                // kommastellen aufheben
            sernum3.exec(calc2);
            calcc =(RegExp.$2);
            if(calcc.length == 3) {
                calcc   = calcc.replace(',', '.');
                sernum3.exec(calc2);
                calc2   = (RegExp.$1);
                calc2   = calc2.replace('.', '');
                calc2   = parseFloat(calc2 + calcc);
            } else {
                // kommastellen werden durch Punkte angezeigt
                sernum4.exec(calc2);
                calcc =(RegExp.$2);
                if(calcc.length == 3) {
                    calcc   = calcc.replace('.', '.');
                    sernum4.exec(calc2);
                    calc2 = parseFloat((RegExp.$1) + calcc);
                }
            }
            //calc2   = (RegExp.$1).replace(',', '.');
            //calc2   = calc2.replace(/\+/, '');
            //calc2   = parseFloat(calc2);

        } else if(tcalc1.indexOf("Standard")!= -1 ){
            prozentPrice = 0;
            prozent      = 0;
        } else {
            calc2 = 0;
        }

        if(parseFloat(prozent)>0) {
           sum1 =(tprice+calc2);
           total = kaufm(sum1 + (sum1 * prozent));
        } else {
            total = kaufm((tprice + calc2));
        }
            // einfuegen der Kommastellen
        
        tform.elements.products_price_new_calc.value= total;
             // convert to a String 
        totalString = new String(total);
        total       = totalString.replace('.', ',');
        
        tblc = getlayer('text_price_info', 2);
        tblc.innerHTML = total + ' EUR';
        // alert(total);

        // nun zur Bild-Verhaeltnisanzeige
        opt_val = (inp.options[inp.options.selectedIndex].innerHTML);
        if(opt_val.indexOf('x')>= 1) {
            r1 = /^[^0-9]+/;
            opt_v = opt_val.split(' ');
            // resize the dummy image
            // resize the dummy image
            if(uprop== 'w') {
                imgw = Math.round(parseFloat(((form == 0) ? opt_v[0] : opt_v[2])) * 1.794);
                imgh = Math.round(parseFloat(((form == 0) ? opt_v[2] : opt_v[0])) * 1.794);
            } else {
                imgh = Math.round(parseFloat(((form == 0) ? opt_v[0] : opt_v[2])) * 1.794);
                imgw = Math.round(parseFloat(((form == 0) ? opt_v[2] : opt_v[0])) * 1.794);
            }
            //imgw = Math.round(parseFloat(((form == 0) ? opt_v[0] : opt_v[2])) * 1.794);
            //imgh = Math.round(parseFloat(((form == 0) ? opt_v[2] : opt_v[0])) * 1.794);
            frm = getlayer('frame_border',1);
            frm.width = parseFloat(imgw + 6) + 'px';
            frm.height= parseFloat(imgh + 6) + 'px';
            img = getlayer('rel_frame',1);
            img.width = imgw + 'px';
            img.height= imgh + 'px';

            mo_pic = getlayer('mo_pic', 2);
            mo_Get = mo_pic.src.split('?');
            mo_Getvals = mo_Get[1].replace('img=', '');
            mo_img = mo_Getvals.split('&');
            //org_img = new Image();
            //org_img.src = mo_img[0];
            repimg = document.createElement('img');
            repimg.setAttribute('width', imgw);
            repimg.setAttribute('height', imgh);
            repimg.setAttribute('src', 'product_thumb.php?img=' + mo_img[0] + '&w=' + imgw + '&h=' + imgh);
            repimg.setAttribute('border', '0');
            imgObj = getlayer('rel_frame',2);
            imgObj.innerHTML = '';
            imgObj.appendChild(repimg);
        }

    }

    function show_background() {

       if(bildvorschau == '') {
            txs = getlayer('show_background_txt',1);
            txs.visibility = 'hidden';
            txs.display    = 'none';
 
            txh = getlayer('hide_background_txt',1);
            txh.visibility= 'visible';
            txh.display   = 'block';

            sbg = getlayer('background', 1);
            adp = getlayer('additional_pics', 1);
            
            bildvorschau = 1;
            
        } else {
            txs = getlayer('hide_background_txt',1);
            txs.visibility = 'hidden';
            txs.display    = 'none';
 
            txh = getlayer('show_background_txt',1);
            txh.visibility= 'visible';
            txh.display   = 'block';


            txl = getlayer('hide_background_txt',1);
            sbg = getlayer('additional_pics', 1);
            adp = getlayer('background', 1);
            bildvorschau = '';
        }
            sbg.visibility= 'visible';
            sbg.display   = 'block';

            adp.visibility = 'hidden';
            adp.display    = 'none';
        
    } // ENDE show_background


    function check_file(alert_msg) {
    // check ob ein file geladen wurde
        df         = document.forms['cart_quantity'];
        dfl        = df.length;

        if(formaction == '') formaction = df.action;
        df.action = '';
        for(i=0;i<dfl;i++) {
            if((df.elements[i].type== 'FILE' || df.elements[i].type== 'file') && df.elements[i].value!= '') {
                df.action = formaction; 
            }
        }
        if(df.action== '') alert(alert_msg);
        else {
            var ntp = document.createElement('input');
            ntp.setAttribute('type' , 'hidden');
            ntp.setAttribute('name' , 'image-test');
            ntp.setAttribute('value', 'test');
            df.appendChild(ntp);
        }
    } // ENDE check_file


