﻿this.iframe = document.getElementById("hiddenIFrame");

/** form validation **/

// validate email
function EDEN_validateEmail(email) {
    var res;
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    if (filter.test(email))
        res = true;
    else {
        res = false;
    }
    return (res);
}
// validate key as numeric
function EDEN_isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}
function EDEN_validateSearch(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode == 13) {
        var vlu = document.getElementById("inputSokord").value;
        window.location = "produkter.aspx?sokord=" + escape(vlu);
    }
}

// validate max number of carachters
function EDEN_maxLength(evt, objname, len) {
    var vlu = document.getElementById(objname).value;
    if (vlu.length > len)
        return false;
    return true;
}
// validate amount
function EDEN_validateBuy(pid,price) {
    var _obj = document.getElementById(pid + 'amount'); // _obj.name = {pid}amount
    var _amount = _obj.value;

    if (_amount == '' || _amount == '0' || _amount < 0) {
        alert('Angivet antal är ogiltig!');
        return false;
    }
    else {
        // remove prefix zero
        while (_amount.substr(0, 1) == '0' && _amount.length > 1) {
            _amount = _amount.substr(1, 9999);
        }
        if (_amount.length > 1 || _amount != '0') {
            EDEN_add2Cart(pid, price, _amount);
        }
        else {
            alert('Angivet antal är ogiltig!');
        }
    }

}

/** popup hlp **/
function EDEN_getScrollTop() {
    var D = document;
    return Math.max(D.body.scrollTop, D.documentElement.scrollTop);
}
function EDEN_getScrollHeight() {
    var D = document;
    return Math.max(D.body.scrollHeight, D.documentElement.scrollHeight);
}
function EDEN_getOffsetHeight() {
    var D = document;
    return Math.max(D.body.offsetHeight, D.documentElement.offsetHeight);
}
function EDEN_getClientHeight() {
    var D = document;
    return Math.max(D.body.clientHeight, D.documentElement.clientHeight);
}
function EDEN_getClientWidth() {
    var D = document;
    return Math.max(D.body.clientWidth, D.documentElement.clientWidth);
}
function EDEN_getMaxHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}
function EDEN_getMaxWidth() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
        Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
        Math.max(D.body.clientWidth, D.documentElement.clientWidth)
    );
}

function EDEN_popupIMG(filename, name) {
    EDEN_createPopup("Laddar bild...");
    var img = new Image();
    img.onload = (function() { setTimeout("___loadIMG('" + filename + "','" + name + "', '" + img.width + "', '" + img.height + "')", 500); }).bind(this);
    img.src = filename;
}
function ___loadIMG(filename, name, width, height) {
    //alert('w:' + width + ',h:' + height);
    if (height > width) {
        if (height > 800) {
            width = 800 * (width / height);
            height = 800;
        }
    }
    else {
        if (width > 800) {
            height = 800 / (width / height);
            width = 800;
        }
    }

    document.getElementById("eden_popup").innerHTML = "<a href=\"javascript:EDEN_closePopup();\" title=\"" + name + " - Klicka för att stänga\"><img src=\"" + filename + "\" alt=\"" + name + "\" width=\""+width+"\" height=\""+height+"\" onload=\"javascript:EDEN_resetPopup();\"/></a>";
}


/** popup **/
function EDEN_createPopup(waitmsg) {

    var html = "<div style=\"font-size:12px;text-align:center;\">" + waitmsg + "</div>";
    html += "<div style=\"margin-top:5px;\"><img src=\"img/ajax-loader.gif\" border=\"0\" alt=\"laddar...\"/></div>";

    var bg = document.getElementById("eden_popupbg");
    var popup = document.getElementById("eden_popup");
    //var closebtn = document.getElementById("eden_popup_close");

    if (bg == null) {
        // create the default background semitransparent layer
        bg = document.createElement("div");
        bg.setAttribute('id', 'eden_popupbg');
        bg.style.zIndex = '50';
        bg.style.display = 'block';
        bg.style.position = 'absolute';
        bg.style.width = EDEN_getMaxWidth() + 'px';
        bg.style.height = EDEN_getMaxHeight() + 'px';
        bg.style.left = '0px';
        bg.style.top = '0px';
        bg.style.backgroundColor = '#ccc';
        bg.style.textAlign = 'center';
        bg.style.verticalAlign = 'middle';
        bg.style.overflow = 'hidden';
        bg.style.opacity = '.7';
        bg.style.filter = 'alpha(opacity=70)';

        // create the actual popup
        popup = document.createElement("div");
        popup.setAttribute('id', 'eden_popup');
        popup.style.zIndex = '51';
        popup.style.display = 'block';
        popup.style.position = 'absolute';
        popup.style.borderStyle = 'solid';
        popup.style.borderWidth = '7px';
        popup.style.borderColor = '#eee';
        popup.style.backgroundColor = '#fff';
        popup.style.textAlign = 'center';
        popup.style.verticalAlign = 'middle';
        popup.style.padding = '20px 20px 20px 20px';
        popup.style.top = EDEN_getScrollTop() + "px";

        /*closebtn = document.createElement("div");
        closebtn.setAttribute("id", "eden_popup_close");
        closebtn.style.zIndex = "52";
        closebtn.style.display = "none";
        closebtn.style.position = "absolute";
        closebtn.style.margin = "10px 0px 0px -9px";
        closebtn.innerHTML = "<a id=\"btnClose\" name=\"btnClose\" href=\"javascript:EDEN_closePopup();\" title=\"Stäng\"><img src=\"img/popup-close.gif\" border=\"0\" alt=\"Stäng\"/></a>";
*/
        // add new layers to the doc
        document.getElementsByTagName('body')[0].appendChild(bg);
        document.getElementsByTagName('body')[0].appendChild(popup);
        //document.getElementsByTagName('body')[0].appendChild(closebtn);

    }

    popup.innerHTML = html;
    EDEN_resetPopup();
}

// reset popup position, place it center/middle
function EDEN_resetPopup() {
    var bg = document.getElementById('eden_popupbg');
    if (bg != null) {
        bg.style.width = EDEN_getMaxWidth() + 'px';
        bg.style.height = EDEN_getMaxHeight() + 'px';
        var popup = document.getElementById('eden_popup');
        popup.style.left = Math.max(14, (EDEN_getClientWidth() - (popup.offsetWidth + 14)) / 2) + 'px';
        popup.style.top = Math.max(14, (EDEN_getClientHeight() - (popup.offsetHeight + 14)) / 2 + EDEN_getScrollTop()) + 'px';
        /*var closebtn = document.getElementById("eden_popup_close");
        closebtn.style.display = "block";
        closebtn.style.top = Math.max(14, (EDEN_getClientHeight() - (popup.offsetHeight + 14)) / 2) + 'px';
        closebtn.style.left = Math.max(14, (EDEN_getClientWidth() - (popup.offsetWidth + 14)) / 2) + popup.offsetWidth - 20 + 'px';*/
    }
}

function EDEN_closePopup() {
    EDEN_removeDIV('eden_popupbg');
    EDEN_removeDIV('eden_popup');
    //EDEN_removeDIV('eden_popup_close');
}


/** cart **/

function EDEN_miniCart(amount) {
    var cartContent = "<p class=\"input_field hideIfInactive\"><span>Varukorgen inneh&aring;ller <a class=\"red\" href=\"#\">?amount</a></span></p>";
    cartContent += "<a class=\"button_black hideIfInactive\" href=\"javascript:EDEN_getCart();\"><span>Visa Varukorgen</span></a>";
    cartContent += "<a class=\"button_black hideIfInactive\" href=\"javascript:EDEN_confirmCart();\"><span>Till Kassan</span></a>";

    var res = "";
    if (amount == "0") {
        res = "inga produkter";
        cartContent = cartContent.replace(/button_black/gi, "button_black_disabled");
        cartContent = cartContent.replace("javascript:EDEN_getCart();", "#");
        cartContent = cartContent.replace("javascript:EDEN_confirmCart();", "#");
    }
    else if (amount == "1")
        res = "1 produkt";
    else
        res = amount + " produkter";

    return cartContent.replace("?amount", res);
}

function EDEN_add2Cart(pid, price, amount) {
    document.getElementById("hiddenIFrame").src = "hiddenframe.html?callbackby=add2cart&id=" + escape(pid) + "&price=" + escape(price) + "&amount=" + escape(amount);
}

function EDEN_removeFromCart(pid) {
    document.getElementById("updtcart-ajax-loader").style.display = "block";
    document.getElementById("hiddenIFrame").src = "hiddenframe.html?callbackby=rem2cart&pid=" + escape(pid);
}

function EDEN_updateCart() {
    document.getElementById("updtcart-ajax-loader").style.display = "block";

    var popup = document.getElementById("eden_popup");
    var amounts = popup.getElementsByTagName("input");

    var i = 0;
    var cart = "";
    for (i = 0; i < amounts.length; i++)
        cart += amounts.item(i).id.replace("cartamount", "") + "," + amounts.item(i).value + ";";

    document.getElementById("hiddenIFrame").src = "hiddenframe.html?callbackby=updt2cart&cart=" + escape(cart);
}

function EDEN_getCart() {
    EDEN_createPopup("Laddar varukorg...");
    document.getElementById("hiddenIFrame").src = "hiddenframe.html?callbackby=cart";
}

function EDEN_confirmCart() {
    EDEN_createPopup("Laddar kassa...");
    document.getElementById("hiddenIFrame").src = "hiddenframe.html?callbackby=confirmcart";
}

/** other **/

// loginFrm, login
// registerCustomer->custFrm, regCust
// newPassword->pwdFrm, sendmePwd


// viewOrder

// registerOrder
// updateOrder

// present login popup
function EDEN_loginFrm(page) {
    EDEN_createPopup("Laddar...");
    document.getElementById("hiddenIFrame").src = "hiddenframe.html?callbackby=loginfrm&page=" + page;
}

// present register customer form popup
function EDEN_custFrm() {
    EDEN_createPopup("Laddar...");
    document.getElementById("hiddenIFrame").src = "hiddenframe.html?callbackby=regcustfrm";
}

// present forgott your password form popup
function EDEN_resetpwdFrm() {
    EDEN_createPopup("Laddar...");
    document.getElementById("hiddenIFrame").src = "hiddenframe.html?callbackby=resetpwdfrm";
}

function EDEN_shopinactive() {
    EDEN_createPopup("Laddar...");
    document.getElementById("hiddenIFrame").src = "hiddenframe.html?callbackby=shopinactive";
}

function EDEN_updtpwdFrm() {
    EDEN_createPopup("Laddar...");
    document.getElementById("hiddenIFrame").src = "hiddenframe.html?callbackby=updtpwdfrm";
}

function EDEN_updtPwd() {
    // present loader
    document.getElementById("popup-ajax-loader").style.display = "block";
    // disable buttons
    document.getElementById("btnUpdatePwd").href = "#";
    document.getElementById("btnCancel").href = "#";
    //document.getElementById("btnClose").href = "#";

    // validate input
    var err = "false";
    var errmsg = "";
    var customerid = document.getElementById("customerID").value;
    var oldpwd = document.getElementById("oldpassword").value;
    if (oldpwd.length <= 0) {
        err = "true";
        errmsg += "\n - Ange ditt gamla lösenord";
    }
    var pwd1 = document.getElementById("password1").value;
    var pwd2 = document.getElementById("password2").value;
    if (pwd1.length < 6 && pwd2.length < 6) {
        err = "true";
        errmsg += "\n - Det nya lösenordet måste vara över sex tecken lång";
    } else if (pwd1 != pwd2) {
        err = "true";
        errmsg += "\n - De nya lösenorden stämmer inte överens!";
    }
    var params = "pwd=" + escape(oldpwd) + "&newpwd=" + escape(pwd1) + "&id=" + customerid;

    if (err == "false") {
        parent.engine.updatePwd(params);
    }
    else {
        document.getElementById("popup-ajax-loader").style.display = "none";
        // enable buttons
        document.getElementById("btnUpdatePwd").href = "javascript:EDEN_updtPwd();";
        //document.getElementById("btnClose").href = "javascript:EDEN_closePopup();";
        document.getElementById("btnCancel").href = "javascript:EDEN_closePopup();";
        alert("Följande fel upptäcktes:" + errmsg);
    }
}

function EDEN_resetPwd() {
    // present loader
    document.getElementById("popup-ajax-loader").style.display = "block";
    // disable buttons
    document.getElementById("btnResetPwd").href = "#";
    //document.getElementById("btnClose").href = "#";

    // validate input
    var err = "false";
    var errmsg = "";

    var email = document.getElementById("eMail").value;
    if (!EDEN_validateEmail(email)) {
        err = "true";
        errmsg += "\n - Angiven e-post är felaktig";
    }

    var params = "email=" + escape(email);

    if (err == "false") {
        parent.engine.resetPwd(params);
    }
    else {
        document.getElementById("popup-ajax-loader").style.display = "none";
        // enable buttons
        document.getElementById("btnResetPwd").href = "javascript:EDEN_resetPwd();";
        d//ocument.getElementById("btnClose").href = "javascript:EDEN_closePopup();";
        alert("Följande fel upptäcktes:" + errmsg);
    }
}

function EDEN_registerCustomer() {
    // present loader
    document.getElementById("popup-ajax-loader").style.display = "block";
    // disable buttons
    document.getElementById("btnRegister").href = "#";
    //document.getElementById("btnClose").href = "#";

    // validate input
    var err = "false";
    var errmsg = "";
    var fname = document.getElementById("firstName").value;
    if (fname.length <= 1) {
        err = "true";
        errmsg += "\n - Förnamn saknas";
    }
    var lname = document.getElementById("lastName").value;
    if (lname.length <= 1) {
        err = "true";
        errmsg += "\n - Efternamn saknas";
    }
    var pno = document.getElementById("pno").value;
    if (pno.length != 10) {
        err = "true";
        errmsg += "\n - Felaktigt personnummer";
    }
    var address = document.getElementById("deliveryAddress").value;
    if (address.length <= 1) {
        err = "true";
        errmsg += "\n - Leveransadress saknas";
    }
    var zip = document.getElementById("zipCode").value;
    if (zip.length <= 1) {
        err = "true";
        errmsg += "\n - Postnummer saknas";
    }
    var area = document.getElementById("zipArea").value;
    if (area.length <= 1) {
        err = "true";
        errmsg += "\n - Postort saknas";
    }
    var phone = document.getElementById("homePhone").value;
    var mobile = document.getElementById("mobilePhone").value;
    if (phone.length <= 1 && mobile.length <= 1) {
        err = "true";
        errmsg += "\n - Ange antingen hemtelefon eller mobil";
    }
    var email = document.getElementById("eMail").value;
    if (!EDEN_validateEmail(email)) {
        err = "true";
        errmsg += "\n - Angiven e-post är felaktig";
    }
    var pwd1 = document.getElementById("password1").value;
    var pwd2 = document.getElementById("password2").value;
    if (pwd1.length < 6 && pwd2.length < 6) {
        err = "true";
        errmsg += "\n - Lösenordet måste vara över sex tecken lång";
    } else if (pwd1 != pwd2) {
        err = "true";
        errmsg += "\n - Lösenorden stämmer inte överens!";
    }

    var params = "fname=" + escape(fname) + "&lname=" + escape(lname) + "&pno=" + escape(pno) + "&address=" + escape(address) + "&zip=" + escape(zip) + "&area=" + escape(area) + "&phone=" + escape(phone) + "&mobile=" + escape(mobile) + "&email=" + escape(email) + "&pwd=" + escape(pwd1);

    if (err == "false") {
        parent.engine.regCust(params);
    }
    else {
        document.getElementById("popup-ajax-loader").style.display = "none";
        // enable buttons
        document.getElementById("btnRegister").href = "javascript:EDEN_registerCustomer();";
        //document.getElementById("btnClose").href = "javascript:EDEN_closePopup();";
        alert("Följande fel upptäcktes:" + errmsg);
    }
}

function EDEN_updateCustomer() {
    EDEN_createPopup("Sparar...");
    //document.getElementById("btnClose").style.display = "none";

    // validate
    var err = "false";
    var errmsg = "";
    var customerid = document.getElementById("customerID").value;
    
    var fname = document.getElementById("firstName").value;
    if (fname.length <= 1) {
        err = "true";
        errmsg += "\n - Förnamn saknas";
    }
    var lname = document.getElementById("lastName").value;
    if (lname.length <= 1) {
        err = "true";
        errmsg += "\n - Efternamn saknas";
    }
    var pno = document.getElementById("pno").value;
    if (pno.length != 10) {
        err = "true";
        errmsg += "\n - Felaktigt personnummer";
    }
    var address = document.getElementById("deliveryAddress").value;
    if (address.length <= 1) {
        err = "true";
        errmsg += "\n - Leveransadress saknas";
    }
    var zip = document.getElementById("zipCode").value;
    if (zip.length <= 1) {
        err = "true";
        errmsg += "\n - Postnummer saknas";
    }
    var area = document.getElementById("zipArea").value;
    if (area.length <= 1) {
        err = "true";
        errmsg += "\n - Postort saknas";
    }
    var phone = document.getElementById("homePhone").value;
    var mobile = document.getElementById("mobilePhone").value;
    if (phone.length <= 1 && mobile.length <= 1) {
        err = "true";
        errmsg += "\n - Ange antingen hemtelefon eller mobil";
    }
    var email = document.getElementById("eMail").value;
    if (!EDEN_validateEmail(email)) {
        err = "true";
        errmsg += "\n - Angiven e-post är felaktig";
    }

    var params = "fname=" + escape(fname) + "&lname=" + escape(lname) + "&pno=" + escape(pno) + "&address=" + escape(address) + "&zip=" + escape(zip) + "&area=" + escape(area) + "&phone=" + escape(phone) + "&mobile=" + escape(mobile) + "&email=" + escape(email) + "&id=" + escape(customerid);

    if (err == "false") {
        parent.engine.updateCust(params);
    }
    else {
        EDEN_closePopup();
        alert("Följande fel upptäcktes:" + errmsg);
    }
}


function EDEN_validateLogin(page) {
    // present loader
    document.getElementById("popup-ajax-loader").style.display = "block";
    // disable buttons
    document.getElementById("btnLogin").href = "#";
    //document.getElementById("btnClose").href = "#";

    // validate
    var err = "false";
    var errmsg = "";

    var email = document.getElementById("eMail").value;
    if (!EDEN_validateEmail(email)) {
        err = "true";
        errmsg += "\n - Angiven e-post är felaktig";
    }
    var pwd1 = document.getElementById("password1").value;
    if (pwd1.length < 6) {
        err = "true";
        errmsg += "\n - Lösenordet måste vara över sex tecken lång";
    }

    var params = "email=" + escape(email) + "&pwd=" + escape(pwd1) + "&page=" + page;

    if (err == "false") {
        parent.engine.validateLogin(params);
    }
    else {
        document.getElementById("popup-ajax-loader").style.display = "none";
        // enable buttons
        document.getElementById("btnLogin").href = "javascript:EDEN_validateLogin();";
        //document.getElementById("btnClose").href = "javascript:EDEN_closePopup();";
        alert("Följande fel upptäcktes:" + errmsg);
    }

}


function EDEN_sendMsg() {

    EDEN_createPopup("Ditt meddelande skickas...");


    // validate form
    var n = document.getElementById('txtNamn');
    var e = document.getElementById('txtEpost');
    var m = document.getElementById('txtMeddelande');

    var err = 'false';
    var errmsg = '';

    // name not empty
    if (n.value.length == 0) {
        err = 'true';
        errmsg += '\n- Namn saknas.';
    }
    // valid email format
    if (!EDEN_validateEmail(e.value)) {
        err = 'true';
        errmsg += '\n- Angiven e-post är felaktig.';
    }
    // message not empty
    if (m.value.length == 0) {
        err = 'true';
        errmsg += '\n- Meddelande saknas.';
    }

    // send message to server if no errors where found
    if (err == 'false') {
        // send message
        parent.engine.sendMessage(n.value, e.value, m.value);
    }
    else {
        // inform of the errors
        EDEN_closePopup();
        alert('Följande fel upptäcktes:' + errmsg);
    }
}
function EDEN_removeDIV(_obj) {
    var _child = document.getElementById(_obj);
    document.getElementsByTagName('body')[0].removeChild(_child);
}

function EDEN_myAccount() {
    document.getElementById("hiddenIFrame").src = "hiddenframe.html?callbackby=account";
}

function EDEN_viewWebOrder(id) {
    EDEN_createPopup("Laddar order...");
    document.getElementById("hiddenIFrame").src = "hiddenframe.html?callbackby=weborder&id=" + id;
}
