//Globals...
var editId = 0;
var activeWindow;
var dezi;
var leftCurr = null;
var actAgePrice = "standard";
var nbsp = String.fromCharCode(160);
var one4all = false;
function openProds(id) {
    //var h=parseInt(id)==-2?560:500;
    var h = 560;
    h += document.all ? 0 : 10;
    activeWindow = openWin('Products', 'tableproducts.aspx?res=' + id, 700, h, 1);
}

function openInfo(id) {

    if (id.length == 0) return;
    switch (id) {
        case -1:
            openWin('Skitype', 'skitype.aspx', 700, 300, 1);
            break;
        case -2:
            openWin('Snowboard', 'snobo.aspx', 460, 150, 1);
            break;
        default:
            openWin('Hotel', 'hotel.aspx?id=' + id, 300, 200, 1);
    }
    return;
}

function kickFromCart(who) {

    var what = getElem("cp_"+who);
    try {
        //alert(typeof(who));
        var Cart = what.parentNode;
        try {
            //var priceNode = getElem("T" + who + "crtprc");
            //if (priceNode==null || typeof(priceNode) == "undefined") { priceNode = getElem(who + "crtprc"); }
            //var price = getPrice(priceNode.firstChild.nodeValue) * -1;

            var price = 0;
            $(what).find(".price").each(function (i) {
                
                price += getPrice($(this).text()) * -1;
            });
           // var resPrice = (Cart.id == "Prod_0") ? getNextElemByIdEnd(Cart.parentNode, "lblPrice") : getNextElemByIdEnd(Cart.parentNode, "subPrice");
            var resPrice = getElem(Cart.id.substr(5) + "subPrice");
            if(resPrice!=null)setResPrice(resPrice, price);
           // Cart = getElem("shopCart");
            resPrice = getElem("lblPriceTotal");
            setResPrice(resPrice, price);
            //setDiscPrice(price);
        } catch (e) { alert(who + "!" + Cart.id + ":" + e); }
        what.parentNode.removeChild(what);
    } catch (e) { alert(who + "!!" + what + ":" + e); }
    try {
        var c = parseInt("0" + $("#summaryCount").text()) - 1;
        c = Math.max(0, c);
        $("#summaryCount").text(c);
        $("#menuCount").text(c);
    } catch (e) { alert(c + "!!" + e); }

    setCartTotal();
    try {
       // var p =  $("#lblPriceTotal").text();
     //   $("#summaryPrice").text(p);
    } catch (e) { alert(p + "!!" + e); }
    return false;
}

function setDiscPrice(price) {
    try {
        if (myDisc > 0) {
            var elem = getElem("PResTotal");
            if (elem) {
                price -= (price * myDisc / 100);
                 try {
                    var mTax = getTaxTotal(price);
                    if (!taxInc) price += mTax;
                    var taxElem = getElem("TaxVal");
                    if (taxElem) setResPrice(taxElem, mTax);
                } catch (n) { alert("setDiscPrice(inner)" + n); /**/ }
                setResPrice(elem, price);
                elem = getElem("lblTotalPrc");
                if (elem) setResPrice(elem, price);
            }
        }
    } catch (e) { alert("setDiscPrice(outer)" + e); }
}

function getPrice(tx) {
    var i = 0;
    var retVal = 0;
    try {
        tx = tx.replace(/\s/g, "");
        while ("-0123456789".indexOf(tx.charAt(i)) < 0) {
            i++;
            if (i >= tx.length) break;
        }
        tx = tx.substr(i);
        var point = tx.indexOf(".");
        var comma = tx.indexOf(",");
        if (point > 0 && comma > 0) {
            if (point > comma) {
                tx = tx.replace(",", "");
            } else {
                tx = tx.replace(".", "");
                tx = tx.replace(",", ".");
            }
        } else {
            if (comma > 0) tx = tx.replace(",", ".");
        }
        retVal = parseFloat(tx);
        if (isNaN(retVal)) retVal = 0;
    } catch (e) {
        retVal = 0;
    } //alert(e);}
    return retVal;
}

function setPrice(priceNode, price) {

    try {
        if (typeof priceNode != "undefined") {
            if (priceNode.firstChild != null) {
                priceNode.firstChild.nodeValue = priceNode.firstChild.nodeValue.replace(/-?(\d+\D\d+)|(\d+)/, frmCur(price));
            } else {
                var tx = frmCur(price);
                if (leftCurr) { tx = curSign + tx; }
                else { tx = tx + curSign; }
                var txn = document.createTextNode("" + tx);
                txn.data = txn.data.replace(" ", String.fromCharCode(160));
                priceNode.appendChild(txn);
            }
        }
    } catch (e) {
        alert("setPrice()" + e);
    }
}

function frmCur(Num) {
    var mDezi = (!dezi || dezi == "") ? "." : dezi;
    var retVal = "0" + mDezi + "00";
    if (!isNaN(Num)) {
        if (Num.toFixed) {
            retVal = Num.toFixed(2);
            retVal = retVal.replace(".", mDezi);
        } else {
            retVal = Num.toString(10);
            retVal = retVal.replace(".", mDezi);
            var n = retVal.indexOf(mDezi)
            if (n == -1) {
                retVal += mDezi;
                n = retVal.indexOf(mDezi);
            }
            while (n > (retVal.length - 3)) { retVal += "0"; }
        }
        //alert (retVal+"!"+dezi);
    }
    return retVal;
}

function makeDezi(PrTx) {
    try {
        if (!dezi || leftCurr == null) {
            curSign = curSign.replace(/[&#;]/g, "");
            RX = /(\d+)/;
            RX.exec(curSign);
            if (RegExp.$1) {
                curSign = curSign.replace(RegExp.$1, String.fromCharCode(0 + RegExp.$1));
            }
            var curPos = PrTx.indexOf(curSign);
            var needle = PrTx.search(/\d\D\d/);
            if (needle !== false) {
                dezi = PrTx.charAt(needle + 1);
                if (!isNaN(dezi)) dezi = null;
                leftCurr = ((curPos > -1) && (curPos < needle));
            } else {
                needle = PrTx.search(/\d/);
                if (needle !== false) {
                    leftCurr = ((curPos > -1) && (curPos < needle));
                }
            }
        }
    } catch (e) {
    }
}

function setResPrice(resPrice, price) {
    var cResVal = 0.00;
    var rPrTx = "";
    try {
        rPrTx = resPrice.firstChild.nodeValue;
        makeDezi(rPrTx);
    } catch (e) {
        rPrTx = "0.00";
    }
    try {
        cResVal = getPrice(rPrTx);
    } catch (e) {
        cResVal = 0.00;
        leftCurr = null;
    }
    if (isNaN(price)) price = 0.00;
    cResVal += price;
    setPrice(resPrice, cResVal);
   // if (cResVal == 0) return;
}

function getNodePrice() {


}


function add2Cart(who, Res, isPack) {
    if (one4all) Res = 0;
    var Cart = $("#Prod_" + Res);
   // alert(Cart.length +"//"+ Res);
   // var resPrice = Res > 0 ? getNextElemByIdEnd(Cart.parentNode, "subPrice") : getNextElemByIdEnd(Cart.parentNode, "lblPrice");
    var resPrice = getElem(Res + "subPrice");
    //if (resPrice == null) resPrice = getElem("lblPriceTotal");
   // alert(Res+ ":"+resPrice);
    if (!who.parentNode) { who = getElem(who); }
    var what = null;
    var price = 0.00;
    var priceTotal = 0.00;
    var myVal = who.id.substr(who.id.indexOf("_")+1);

    var myPrice = "-";
    var prodId = Res + "_" + myVal;
    if ($("#cp_" + prodId).length) return;
    var prodName = $("#h_" + myVal).text();
    var prodInf = $("#inf_" + myVal).text();
    var prodLink = $("#dtl_" + myVal).attr("href");
    var prodPic = $("#img_" + myVal).attr("src");
    var prcElem = isPack ? $("#pr" + myVal.substr(myVal.indexOf("_") + 1)).find(".active") : $("#pr" + myVal).find(".active");
    if (!prcElem.length) prcElem = isPack ? $("#pr" + myVal.substr(myVal.indexOf("_") + 1)).find("span") : $("#pr" + myVal).find("span");
    var myPrice = prcElem.text();
    var selPrc = prcElem.attr("id");
    var tx = "";
    var arrVal = new Array();
    var itemArr = new Array();
    var totalInf = "";
    myPrice = myPrice.replace(" ", nbsp);
    makeDezi(myPrice);
    price = getPrice(myPrice);

    /* return;alert(myVal);*/


    if (isPack) {
        what = $("#pgl_" + myVal);
        arrVal[0] = "S" + myVal;
        if (what.length) {
            try {
                var tds = what.find(".pricegroupName");
                if (tds.length) {
                    for (var t = 0; t < tds.length; t++) {
                        var p = tds.eq(t);
                        //tx += " " + p.text();
                        var PPg = "" + p.attr("id");
                        var cb=$("#cb"+PPg);
                        if (cb.attr("checked")) {
                            var it = itemArr.push(new Object({
                                id: PPg,
                                text: p.text(),
                                price: ""
                            }));
                            try {
                                //var myG = "G" + myVal + "|" + PPg;
                                var myG = "G" + PPg;
                                try {
                                    var model = p.next(".modelName").find("select option:selected");
                                    if (model.length) {
                                        var m = model.val()
                                        if (m != "") {
                                            tx += " " + model.text();
                                           // myG = ("W" + myVal + "|" + PPg + "|" + m);
                                            myG = ("W" +  + PPg + "|" + m);
                                        }
                                    }
                                } catch (ex) {
                                    alert(ex);
                                }
                                arrVal.push(myG);
                            }
                            catch (eex) {
                                alert(eex);
                                arrVal.push("irr1" + ex + "irr2" + eex);
                            }
                            try {
                                var pr = $("#wl_" + PPg).find("span.active");
                                if (!pr.length) {
                                    pr = $("#wl_" + PPg).find("span");
                                   // alert(it + " " + pr.text());
                                }
                                itemArr[it - 1].price = pr.text();
                            } catch (ex) {
                                alert(ex);
                            }
                        }
                    }
                    if (itemArr.length > 0) {
                        var tpr = $("#wg" + myVal.substr(0,myVal.indexOf("_"))).find("span.active");
                        if (!tpr.length) tpr = $("#tl_" + myVal.substr(0,myVal.indexOf("_"))).find("span.active");
                        if (!tpr.length) tpr = $("#tl_" + myVal.substr(0,myVal.indexOf("_")));
                         if (tpr.length){
                             var tl = tpr[0].text();
                            totalInf = getPackTotalDummy("T" + prodId, tl);
                        }
                    }
                }else{
                    arrVal.push("noGroups");
                }
            } catch (e) { arrVal.push("err" + e); }
        } else {
            arrVal.push("no:" + "pgl_" + myVal);
        }
    } else {
        arrVal[0] = "P" + myVal.replace("_", "|");
        what = $("#cont_" + myVal);
        try {
            var model = what.find("select option:selected");
            if (model.length) {
                var m = model.val();
                if (m != "") {
                    tx += " " + model.text();
                    arrVal[0] = "M" + m;
                }
            }
        } catch (e) {
            alert(e);
         arrVal.push("err" + e); }
    }
    do {

        var newNode = getProductDummy(prodId, prodName + " " + tx, myPrice, prodPic, prodInf, prodLink);
        //prodId, prodName, prodPrId, prodPrice, prodPic, infShort, infLink
        var pItems = "";
        for (var n = 0; n < itemArr.length; n++) {
            var pNode = getPackItemDummy(prodId + itemArr[n].id, itemArr[n].text, itemArr[n].price);
            price += getPrice(itemArr[n].price);
            pItems += pNode;
        }
        newNode = newNode.replace("<!--X-->", pItems);
        newNode = newNode.replace("<!--T-->", totalInf);
        Cart.find(".total_price").before(newNode);
        setProdSlide($("#" + prodId).find("div.product_head"));

        var now = new Date();
        var ident = now.getTime() + "Res" + Res + "CartItem";
        var pItems = "";

        for (var n = 0; n < arrVal.length; n++) {
            var nVal = arrVal[n].replace("_","|");
            var valNode = document.createElement("input");
            valNode.setAttribute("type", "hidden");
            valNode.setAttribute("value", nVal);

            valNode.setAttribute("name", n + ident);
            valNode.setAttribute("id", n + ident);
            $("#cp_" + prodId).append(valNode);
        }
        ident = "0" + ident;
        priceTotal += price;
        //alert (price);
        if (resPrice != null) setResPrice(resPrice, price);

        if (one4all) {
            Res++;
            try {
                Cart = $("#Prod_" + Res);
                //resPrice = getNextElemByIdEnd(Cart.parentNode, "subPrice");
                resPrice = getElem(Res+ "subPrice");
            } catch (e) { break; }
        } else {
            break;
        }
    } while (Cart.length);

    Cart = getElem("shopCart");
   // resPrice = getNextElemByIdEnd(Cart.firstChild, "lblPriceTotal");
    resPrice = getElem( "lblPriceTotal");
    setResPrice(resPrice, priceTotal);
   // setDiscPrice(priceTotal);
    setCartTotal();
    try {
        var c = parseInt("0" + $("#summaryCount").text()) +1;
        $("#summaryCount").text(c);
        $("#menuCount").text(c);
    } catch (e) { alert(c + "!!" + e); }
    return false;
}


function setCartTotal() {
    try {
        var Cart = getElem("shopCart");
        //resPrice = getNextElemByIdEnd(Cart.firstChild, "lblPriceTotal");
        var resPrice = getElem("lblPriceTotal");
        var priceTotal = getPrice(resPrice.firstChild.nodeValue);
        var insPrice = 0;
        try {
            resPrice = getElem("insP");
            if (resPrice != null) {
                insPrice = priceTotal * myInsure / 100;
                resPrice.firstChild.nodeValue = resPrice.firstChild.nodeValue.replace(/-?(\d+\D\d+)|(\d+)/, frmCur(insPrice));
            }
        
        } catch (n) { alert(n); }
        try {
            disc = getElem("discV");
            if (disc!=null) {
                disc.firstChild.nodeValue = disc.firstChild.nodeValue.replace(/-?(\d+\D\d+)|(\d+)/, frmCur(priceTotal * myDisc / 100));
            }
            priceTotal -= (priceTotal * myDisc / 100);
        } catch (n) { }
        try {
            var mTax = getTaxTotal(priceTotal);
            if (!taxInc) priceTotal += mTax;
            resPrice = getElem("TaxVal");
            if (resPrice != null) resPrice.firstChild.nodeValue = resPrice.firstChild.nodeValue.replace(/-?(\d+\D\d+)|(\d+)/, frmCur(mTax));
        } catch (n) { alert(n); }
        // resPrice = getNextElemByIdEnd(Cart.firstChild, "PDiscTotal");
        priceTotal += insPrice;
        resPrice = getElem("PResTotal");
        if (resPrice!=null) resPrice.firstChild.nodeValue = resPrice.firstChild.nodeValue.replace(/-?(\d+\D\d+)|(\d+)/, frmCur(priceTotal));
       // resPrice = getNextElemByIdEnd(Cart.firstChild, "lblTotalPrc");
        resPrice = getElem( "lblTotalPrc");
        if (resPrice != null) resPrice.firstChild.nodeValue = resPrice.firstChild.nodeValue.replace(/-?(\d+\D\d+)|(\d+)/, frmCur(priceTotal));
        try {
            var p = $("#PResTotal").text();
            $("#summaryPrice").text(p);
        } catch (n) { alert("#PResTotal"+p + "!!" + e); }
    } catch (e) {
        alert("setCartTotal!!" + e);
     }
}

function getTaxTotal(priceTotal) {
    try {
        var retval = 0;
        if (tax > 0) {
            var prozTot = 100 + (taxInc ? tax : 0);
            retval = (priceTotal * tax / prozTot);
        }
        return retval;
    } catch (e) {
        return 0;
    }
}

function getNextElemByIdEnd(firstSib, idEnd) {
    var nextSib;
    try {
        nextSib = firstSib.nextSibling;
        var regeX = "/" + idEnd + "$/";
        while (nextSib != null) {
            if (nextSib.id) {
                if (nextSib.id.match(eval(regeX))) {
                    // alert(nextSib.id);
                    break;
                }
            }
            nextSib = nextSib.nextSibling;
        };
        nextSib = nextSib.id.match(eval(regeX)) ? nextSib : null;
    } catch (e) { nextSib = null; }
    return nextSib;
}

function checkname() {
    var cn = "";
    var sx = "";
    var fn = "";
    var ln = "";
    var elem;
    var fc = document.forms[0].elements.length;
    for (i = 0; i < fc; i++) {
        elem = document.forms[0].elements[i];
        en = elem.name;
        if (en.indexOf("sex") > -1) {

            if (elem.selectedIndex > -1) {
                sx = elem.options[elem.selectedIndex].value;
            }
        } else {
            if (en.indexOf("lastname") > -1) {
                ln = elem.value;
            } else {
                if (en.indexOf("firstname") > -1) {
                    fn = elem.value;
                }
            }
        }
    }
    cn = ((fn != "") && (ln != "")) ? ", " : "";
    //sx+ln+cn+fn
    var txn = document.createTextNode((selRes + 1) + ". " + ln + cn + fn);
    elem = getElem("btnClient" + selRes);
    if (elem) {
        elem = elem.getElementsByTagName("span")[0];
        if (elem) {
            if (elem.firstChild) {
                elem.replaceChild(txn, elem.firstChild);
            } else {
                elem.appendChild(txn);
            }
        }
        elem = getElem("hdnClient" + selRes);
        if (elem) { elem.value = sx + "|" + fn + "|" + ln }
    }
}

function setPrices(what) {
    var prcs;
    var who = getElem("priceSelect");
    if (who) {
        if (ageArr[selRes] > 0) {
            if (who.style) who.style.display = "none";
            actAgePrice = what;
            // what="_";
        } else {
            if (who.style) who.style.display = "";
            if (actAgePrice != what) {
                who = getElem("pSel" + what);
                var prices = who.parentNode;

                var nextSib = prices.firstChild;
                while (nextSib != null) {
                    try {
                        nextSib.setAttribute("class", "tabItem");
                        nextSib.className = "tabItem";
                    } catch (e) { }
                    nextSib = nextSib.nextSibling;
                };
                who.setAttribute("class", "tabItem activ");
                who.className = "tabItem activ";
                actAgePrice = what;
            }
        }
    }
    // var cdOff= pgArr.length>1?pgArr[1].indexOf("cd")+2:0;
    for (var i = 1; i < pgArr.length; i++) {
        var pg = pgArr[i]
        if (pg != "") {
            for (var p = 0; p < 2; p++) {
                prcs = getElem(pg);
                if (prcs) {
                    prcs.style.display = "";
                    var nono = true;
                    var def;
                    if (prcs.childNodes) {
                        for (var fc in prcs.childNodes) {
                            var fcn = prcs.childNodes[fc];
                            if (fcn.id) {
                                $("#" + fcn.id).removeClass("active");
                                if (fcn.id.indexOf(what) > -1) {
                                    $("#" + fcn.id).addClass("active");
                                    //$("#" + fcn.id).attr("class", "active");
                                } else {
                                    //alert(fc);
                                    $("#" + fcn.id).addClass("none");
                                   // $("#" + fcn.id).attr("class", "none");
                                }
                            }
                        }
                    }
                }
                pg = "d" + pg;
            }
        }
    }
}

function setAktRes(who, sendIt) {
    if (sendIt) {
        if (document.forms[0].action.indexOf("?") <= 0) {
            document.forms[0].action += "?res=" + (who - 1);
        } else {
            var i = document.forms[0].action.indexOf("res=");
            if (i <= 0) {
                document.forms[0].action += "&res=" + (who - 1)
            } else {
                urlArr = document.forms[0].action.split("?");
                actArr = urlArr[1].split("&");
                var l = actArr.length;
                for (i = 0; i < l; i++) {
                    if (actArr[i].substr(0, 4) == "res=") {
                        actArr[i] = "res=" + (who - 1);
                        break;
                    }
                }
                urlArr[1] = actArr.join("&");
                document.forms[0].action = urlArr.join("?");
            }
        }
        document.forms[0].submit();
    } else {
        var CN = getElem("btnClient" + selRes);
        CN.parentNode.setAttribute("class", "disResCart");
        CN.parentNode.className = "disResCart";
        CN = CN.firstChild;
        CN.style.color = "";
        selRes = who;
        CN = getElem("btnClient" + selRes);
        CN.parentNode.setAttribute("class", "actResCart");
        CN.parentNode.className = "actResCart";
        CN = CN.firstChild;
        CN.style.color = "red";
        try {
            CN = getElem("hdnClient" + selRes);
            if (CN) {
                var namArr = CN.value.split("|");
                var fc = document.forms[0].elements.length;
                for (i = 0; i < fc; i++) {
                    elem = document.forms[0].elements[i];
                    en = elem.name;
                    if (en.indexOf("sex") > -1) {
                        for (var j = 0; j < document.forms[0].elements[i].length; j++) {
                            if (document.forms[0].elements[i].options[j].value == namArr[0]) document.forms[0].elements[i].selectedIndex = j;
                        }
                    } else {
                        if (en.indexOf("lastname") > -1) {
                            elem.value = namArr[2];
                        } else {
                            if (en.indexOf("firstname") > -1) {
                                elem.value = namArr[1];
                            }
                        }
                    }
                }
            }
        } catch (e) {
        }
        try { setDate(); } catch (e) { }
        try { setPrsData(); } catch (e) { }
        // if(pgArr){setPrices(actAgePrice);}
    }
}

function loadMe(url) {
    try {
        showHide("sender", false);
        showHide("loader", false);
        if (document.cookie == "") { showHide("nocookies", true); } else { showHide("nocookies", false); }
        if (document.forms[0]["sex"]) document.forms[0]["sex"].focus();
    } catch (e) { }
}

function checkCartHeight() {
    try {
        var mHold = getElem("ScHolder");
        if (mHold) {
            var iSize = window.innerHeight || (mHold.top);
            var mCart = getElem("shopCart");
            if (mCart) {
            }
        }
    } catch (e) { }
}


function getmyLoc() {
    return location.href;
}
function goBack() {
    history.back();
}
function openSubReservation(id) {
    activeWindow = openWin('Reservierung', 'subReservation.aspx?res=' + id, 580, 460, 1);
}

/***********************************************************************************************************************************************
****************************************************************** end of script! ****************************************************************
************************************************************************************************************************************************/

