//====================================================================
//¸¶ÀÌÆäÀÌÁö ÀÌ¹ÌÁö¸®½ºÆÃ ºÎºÐ °øÅë ½ºÅ©¸³Æ®
//===================================================================
var brandcodes      = document.getElementsByName('brandcodes[]');
var branduids       = document.getElementsByName('branduids[]');
var brchks          = document.getElementsByName('brchks[]');
var quantities      = document.getElementsByName('quantity[]');
var order_quantities= document.getElementsByName('order_quantity[]');
var stock_state     = document.getElementsByName('stock_state[]');

//ÀüÃ¼¼±ÅÃ
function all_brand_check() {
    for (var i = 0; i < brchks.length; i++) {
        if (brchks[i].disabled == false) {
            brchks[i].checked = document.list_form.all_brchk.checked; 
        }
    }
}

function all_brchk_check () {
    var c = 0;
    for (var i = 0; i < brchks.length; i++) {
        if (brchks[i].checked === true) {
            c++;
        }
    }

    if (c == brchks.length) {
        document.list_form.all_brchk.checked = true;
    } else {
        document.list_form.all_brchk.checked = false;
    }
}

//¼±ÅÃµÈ »óÇ°µéÀ» Àå¹Ù±¸´Ï¿¡ ´ã´Â´Ù.
function multi_go_basket(language) {
    if (typeof language == 'undefined') {
        language = 'kor';
    }
    if (checkbox_validate(language) == false) {
        return;
    }
    for (var i = 0; i < brchks.length; i++) {
        if (brchks[i].checked == true) {
            alert("´ÙÁß¼±ÅÃ½Ã Àå¹Ù±¸´Ï ´ã±â ±¸ÇöÇØ¾ßÇÔ");
        }
    }
}

// Ãß°¡ ±¸¼º»óÇ° °æ°íÃ¢ÈÄ »óÇ°»ó¼¼·Î ÀÌµ¿ÇÑ´Ù.
function go_basket_extra(uid, mobile_use) {
    var shopdetail_url = mobile_use === '1' ? '/m/product.html' : '/shop/shopdetail.html';
    alert('»ó¼¼ÆäÀÌÁö¿¡¼­ Ãß°¡ ±¸¼º »óÇ°À» ¼±ÅÃÇÏ°í Àå¹Ù±¸´Ï¿¡ ´ã¾ÆÁÖ¼¼¿ä.');
    location.href = shopdetail_url + '?branduid=' + uid;
}

//Àå¹Ù±¸´Ï¿¡ »óÇ°À» ´ã´Â´Ù.
function go_basket(idx, language, op, page_type_chk) {
    var f1 = document.form1;
    var is_unify_opt = f1.is_unify_opt.value;
    let set_list_form_name = 'list_form';

    if (typeof page_type_chk === 'string' && page_type_chk === 'today') {
        f1 = document.forms['today_form1'];
        order_quantities = today_order_quantities;
        brandcodes = document.getElementsByName('today_brandcodes[]');
        branduids = document.getElementsByName('today_branduids[]');
        brchks = today_brchks;
        set_list_form_name = 'today_' + set_list_form_name;
    } else {
        var param_obj = document.getElementById('param_'+idx);
    }

    if (typeof language == 'undefined') {
        language = 'kor';
    }
    if (op == 'get_option') {
        if (wish_product_manager.print_option(set_list_form_name, idx) === false) {
            return;
        }
    } else {
        if (typeof page_type_chk === 'string' && page_type_chk === 'today') {
            if (false === today_quantity_validate(idx)) {
                return;
            }
        } else {
            if (false === quantity_validate(idx, language)) {
                return;
            }
        }
    }

    f1.amount.value = (typeof order_quantities[idx] != 'undefined') ? order_quantities[idx].value : 1;
    f1.brandcode.value = brandcodes[idx].value;
    f1.branduid.value = branduids[idx].value;
    f1.uid.value = branduids[idx].value;
    if(param_obj !=null){
       f1.spcode.value = param_obj.getAttribute('spcode');
       f1.spcode2.value = param_obj.getAttribute('spcode2');
       f1.optcode.value = param_obj.getAttribute('optcode');
       f1.optioncode.value = param_obj.getAttribute('optioncode')+",";
       f1.optiontype.value = param_obj.getAttribute('option_type');
       f1.optionlist.value = param_obj.getAttribute('optionvalue');
       f1.opts.value = param_obj.getAttribute('optcode')+",";
       f1.option_type.value = param_obj.getAttribute('option_type');
       if (f1.wish_id) {
           f1.wish_id.value = param_obj.getAttribute('wish_id');
       }
       f1.mode.value = "ins";
    } else {
       f1.spcode.value = 0;
       f1.spcode2.value = 0;
       f1.mode.value = "";

       if (brchks[idx] && brchks[idx].getAttribute('option_type')) {
           if (brchks[idx].getAttribute('option_type') != 'NO') {
               f1.mode.value = "ins";
           }
       }
    }

    if (op == 'get_option') {
        $.ajax({
            type: 'POST',
            dataType: 'json',
            async: false,
            url: '/shop/wish.action.html',
            data: $('form[name="' + set_list_form_name + '"]').serializeArray(),
            success: function(response) {
                if (response.success === true) {
                    f1.action = "basket.html";
                    common_basket_send(f1, is_unify_opt); 
                    alert((language == 'eng') ? "Add to cart." : "Àå¹Ù±¸´Ï¿¡ »óÇ°À» ´ã½À´Ï´Ù.");
                } else {
                    alert(response.message);
                }
            }
        });
    } else {


        //alert((language == 'eng') ? "Add to cart." : "Àå¹Ù±¸´Ï¿¡ »óÇ°À» ´ã½À´Ï´Ù.");

        f1.action = "basket.html";
        common_basket_send(f1, is_unify_opt); 


    }
}

function package_basket(uid,language,device) {

    if (typeof language == 'undefined') {
        language = 'kor';
    }
    if (typeof device == 'undefined') {
        device = 'pc';
    }
    if (language == 'eng') {
        alert("Products after confirming from the product detail page, if a set of products will be placed in the cart.");
    } else {
        alert((device == 'mobile') ? "¼¼Æ® »óÇ°ÀÇ °æ¿ì »ó¼¼ÆäÀÌÁö¿¡¼­\n»óÇ° º¯°æÀ¯¹« È®ÀÎ ÈÄ\nÀå¹Ù±¸´Ï¿¡ ´ã¾Æ¾ß ÇÕ´Ï´Ù." : "¼¼Æ® »óÇ°ÀÇ °æ¿ì »ó¼¼ÆäÀÌÁö¿¡¼­ »óÇ° º¯°æÀ¯¹« È®ÀÎ ÈÄ\n\nÀå¹Ù±¸´Ï¿¡ ´ã¾Æ¾ß ÇÕ´Ï´Ù.");
    }
    location.href = '/shop/shopdetail.html?branduid='+uid;
}

//ÁÖ¹®¼ö·®À» Ã¼Å©ÇÑ´Ù.
function quantity_validate(idx, language) {
    if (typeof language == 'undefined') {
        language = 'kor';
    }
    var f1 = document.form1;
    var is_mobile = f1.is_mobile ? f1.is_mobile.value : '';
    if (typeof stock_state[idx] != 'undefined' && stock_state[idx].value == 'N') {
        var shopdetail_url = is_mobile === 'Y' ? '/m/product.html' : '/shop/shopdetail.html';
        var uid = branduids[idx].value;
        alert("°ü¸®ÀÚ¿¡ ÀÇÇØ »óÇ° ¿É¼ÇÀÌ º¯°æµÇ¾ú½À´Ï´Ù.\n¿É¼ÇÀ» ´Ù½Ã ¼±ÅÃÇØÁÖ¼¼¿ä."); 
        location.href = shopdetail_url + '?branduid=' + uid;
        return false;
    }

    if (quantities[idx].value == 0) {
        alert((language == 'eng') ? "The selected product is out of stock. You can not order this product." : "¼±ÅÃÇÏ½Å »óÇ°ÀÇ Àç°í°¡ ¾ø¾î ÁÖ¹®ÇÏ½Ç ¼ö ¾ø½À´Ï´Ù.");
        if (typeof order_quantities[idx] != 'undefined') {
            order_quantities[idx].focus();
        }
        return false;
    }

    if (typeof order_quantities[idx] != 'undefined') {
        if (isNaN(order_quantities[idx].value)) {
            alert((language == 'eng') ? "Input numbers in quantity field." : "ÁÖ¹®¼ö·®À» ¼ýÀÚ·Î ÀÔ·ÂÇÏ¼¼¿ä.");
            order_quantities[idx].focus();
            return false;
        }
        if (order_quantities[idx].value.length == 0) {
            alert((language == 'eng') ? "Input quantity." : "ÁÖ¹®¼ö·®À» ÀÔ·ÂÇÏ¼¼¿ä.");
            order_quantities[idx].focus();
            return false;
        }
        if (parseInt(quantities[idx].value) < parseInt(order_quantities[idx].value)) {
            alert((language == 'eng') ? "The order quantity exceeds the remaining stock." : "ÁÖ¹®¼ö·®ÀÌ Àç°í ¼ö·®º¸´Ù ¸¹½À´Ï´Ù.");
            order_quantities[idx].focus();
            return false;
        }
    }
}

function checkbox_validate(language) {
    if (typeof language == 'undefined') {
        language = 'kor';
    }
    var c = 0;
    for (var i = 0; i < brchks.length; i++) {
        if (brchks[i].checked === true) {
            c++;
        }
    }
    if (c == 0) {
        alert((language == 'eng') ? "Please select product." : "»óÇ°À» ¼±ÅÃÇÏ¼¼¿ä.");
        return false;
    }
}

//====================================================================
//ÃÖ±Ùº»»óÇ°°ü·Ã ½ºÅ©¸³Æ®
//===================================================================

//¼±ÅÃµÈ ÃÖ±Ù º» »óÇ°µéÀ» »èÁ¦ÇÑ´Ù.
function multi_del_viewproduct(language, time) {
    if (typeof language == 'undefined') {
        language = 'kor';
    }
    if (checkbox_validate(language) == false) {
        return;
    }
    if (confirm((language == 'eng') ? "Delete selected products from list of recently viewed products." : "¼±ÅÃÇÏ½Å ÃÖ±Ù º» »óÇ°À» »èÁ¦ÇÕ´Ï´Ù.")) {
        brcookie = get_viewproduct_cookie();
        for (var i = 0; i < brchks.length; i++) {
            if (brchks[i].checked == true) {
                brcookie = set_viewproduct(brchks[i].value, brcookie); 
            }
        }
        set_cookie_viewproduct(brcookie, time);
        document.location.reload();
    }
}

//ÃÖ±Ùº» »óÇ°À» »èÁ¦ÇÑ´Ù.
function del_viewproduct(brandcode,language, time) {
    if (typeof language == 'undefined') {
        language = 'kor';
    }
    if (confirm((language == 'eng') ? "Delete from list of recently viewed products." : "ÃÖ±Ù º» »óÇ°¿¡¼­ »èÁ¦ÇÕ´Ï´Ù.")) {
        brcookie = get_viewproduct_cookie();
        set_cookie_viewproduct(set_viewproduct(brandcode, brcookie), time);
        document.location.reload();
    }
}

function all_del_viewproduct(language, time) {
    if (typeof language == 'undefined') {
        language = 'kor';
    }
    if (confirm((language == 'eng') ? "Delete all products from list of recently viewed products." : "ÃÖ±Ù º» »óÇ°À» ÀüÃ¼ »èÁ¦ÇÕ´Ï´Ù.")) {
        set_cookie_viewproduct('', time);
        document.location.reload();
    }
}

//ÃÖ±Ù º» »óÇ° ÄíÅ°·Î »èÁ¦ ¿äÃ» »óÇ°À» ¾ø¾Ö°í Àç±¸¼º  
function set_viewproduct(brandcode, brcookie) {
    brand_arr = brcookie.split(',');
    var set_brands = "";
    for (var i = 1; i < brand_arr.length - 1; i++) {
        if (brandcode !== brand_arr[i]) {
            set_brands += "," + brand_arr[i];
        }
    }
    if (set_brands.length > 0) {
        set_brands += ",";
    }
    return set_brands;
}

//ÃÖ±Ù º» »óÇ° ÄíÅ°¸¦ ±Á´Â´Ù.
function set_cookie_viewproduct(set_brands, time) {
    var hostname = window.location.hostname;
    if (hostname.substr(0,4) === "www.") {
        var hostlen = hostname.length;
        hostname = hostname.substring(3, hostlen);
    }
    var add_time = '';
    if (time != '0') {
        var _d = new Date();
        _d.setTime(_d.getTime() + (time*60*60*1000));
        add_time += 'expires=' + _d.toGMTString() + ';';
    }
    document.cookie = 'viewproduct=' + escape(set_brands) +'; path=/;' + add_time + 'domain=' + hostname + ';';
}

//ÄíÅ°¸¦ °¡Á®¿Â´Ù
function get_viewproduct_cookie() {        
    return getCookiefss("viewproduct"); 
}

//====================================================================
//wish list °ü·Ã ½ºÅ©¸³Æ®
//===================================================================

function link_basket_all(language) {
    if (typeof language == 'undefined') {
        language = 'kor';
    }
    var f = document.form1;
    f.action = "basket.html";
    f.mode.value = "basket_all";
    var brandcode_val = check_brand(',','code','',language);
    if (brandcode_val == false) {
        return;
    } else {
        f.branduid.value = brandcode_val;
        f.spcode.value = check_brand(',','code','spcode',language);
        f.spcode2.value = check_brand(',','code','spcode2',language);
        f.optcode.value = check_brand(',','code','optcode',language);
        f.brandcode.value = check_brand(',','code','brandcode',language);
        f.aramount.value = check_brand(',','amount','amount',language);
        f.optioncode.value = check_brand(',','code','optioncode',language);
        if (f.wish_id) {
            f.wish_id.value = check_brand(',','code','wish_id',language);
        }
        f.product_type.value = check_brand(',','code','product_type',language);
        if(f.product_type.value.indexOf('PACKAGE') != -1) {
            alert('¼±ÅÃÇÑ »óÇ° Áß ¼¼Æ® »óÇ°ÀÌ Æ÷ÇÔµÇ¾î ÀÖ½À´Ï´Ù.\n¼¼Æ® »óÇ°ÀÇ °æ¿ì ÇØ´ç »óÇ°ÀÇ »ó¼¼È­¸é¿¡¼­\nÁ÷Á¢ Àå¹Ù±¸´Ï ´ã±â·Î ÀÌ¿ëÇÏ½Ã±â ¹Ù¶ø´Ï´Ù.');
            return;
        }
    }
    is_unify_opt = f.is_unify_opt.value;
    common_basket_send(f, is_unify_opt); 
}
//wishlist »óÇ°À» »èÁ¦ÇÑ´Ù
function del_wishlist(branduid,idx) {
    if (!confirm("Wish List¿¡¼­ »óÇ°À» »èÁ¦ÇÏ½Ã°Ú½À´Ï±î?")) {
        return;
    }
    var f = document.form1;
    var param_obj = document.getElementById('param_'+idx);

    var spcode = param_obj.getAttribute('spcode');
    var spcode2 = param_obj.getAttribute('spcode2');
    var optcode = param_obj.getAttribute('optcode');
    var optioncode = param_obj.getAttribute('optioncode');
    var wish_id = '';
    if (f.wish_id) {
        wish_id = param_obj.getAttribute('wish_id');
    }

    jQuery.ajax({
        url  : 'wish.action.html',
        type : 'POST',
        data: {
            action_type: 'delete',
            page_type: 'mypage',
            data_type: 'ones',
            uid: branduid,
            wish_id: wish_id,
            spcode: spcode,
            spcode2: spcode2,
            optcode: optcode,
            optioncode: optioncode
        },
        dataType: 'json',
        success: function(response) {
            if (response.success == true) {
                //alert("Wish List »óÇ°ÀÌ »èÁ¦ µÇ¾ú½À´Ï´Ù.");
                location.reload();
            } else {
                if (response.message != '') {
                    alert(response.message);
                }
                return;
            }
        }
    });
    return;
}
function multi_del_wishlist(language) {
    if (!confirm("Wish List¿¡¼­ »óÇ°À» »èÁ¦ÇÏ½Ã°Ú½À´Ï±î?")) {
        return;
    }
    if (typeof language == 'undefined') {
        language = 'kor';
    }
    var f = document.form1;
    var brandcode_val = check_brand(',','code','',language);
    if (brandcode_val == false) {
        return;
    }
    var spcode = check_brand(',','code','spcode',language);
    var spcode2 = check_brand(',','code','spcode2',language);
    var optcode = check_brand(',','code','optcode',language);
    var optioncode = check_brand(',','code','optioncode',language);
    var wish_id = '';
    if (f.wish_id) {
        wish_id = check_brand(',','code','wish_id',language);
    }
    jQuery.ajax({
        url  : 'wish.action.html',
        type : 'POST',
        data: {
            action_type: 'delete',
            page_type: 'mypage',
            data_type: 'multi',
            uid: brandcode_val,
            wish_id: wish_id,
            spcode: spcode,
            spcode2: spcode2,
            optcode: optcode,
            optioncode: optioncode
        },
        dataType: 'json',
        success: function(response) {
            if (response.success == true) {
                //alert("Wish List »óÇ°ÀÌ »èÁ¦ µÇ¾ú½À´Ï´Ù.");
                location.reload();
            } else {
                if (response.message != '') {
                    alert(response.message);
                }
                return;
            }
        }
    });
    return;
}
//¸ð¹ÙÀÏ¿¡¼­¸¸ »ç¿ë. À§½Ã¸®½ºÆ® ÀüÃ¼ »èÁ¦
function all_del_wishlist() {

    if(!confirm("Wish List¸¦ ºñ¿ì½Ã°Ú½À´Ï±î?")) {
        return;
    }
    jQuery.ajax({
        url  : 'wish.action.html',
        type : 'POST',
        data: {
            action_type: 'delete',
            page_type: 'mypage',
            data_type: 'all'
        },
        dataType: 'json',
        success: function(response) {
            if (response.success == true) {
                //alert("Wish List »óÇ°ÀÌ »èÁ¦ µÇ¾ú½À´Ï´Ù.");
                location.reload();
            } else {
                if (response.message != '') {
                    alert(response.message);
                }
                return;
            }
        }
    });
    return;
}
function importune_pop(temp) {
    win = window.open('importune_pop.html?brandcode=' + temp, 'Á¶¸£±â', 'width=507, height=700,scrollbars=auto');
    win.focus();
}
function check_brand(division,type,obj,language){
    if (typeof language == 'undefined') {
        language = 'kor';
    }
    var temp_r = "";
    var temp_brandcodes = "";
    if (checkbox_validate(language) == false) {
        return false;
    }
    var alertflag = false;
    var cnt = 0 ;
    for (var i = 0; i < brchks.length; i++) {
        if (brchks[i].checked == true) {
            if (type == 'uid') {
                if ( branduids[i].value.length == 0 ) {
                    alertflag = true;
                } else {
                    if(!obj){
                        temp_brandcodes += branduids[i].value + division; 
                    }else{
                        temp_r += document.getElementById('param_'+i).getAttribute(obj) + "()"; 
                    }
                }
            } else if (type == 'amount') {
                if(!obj) {
                    temp_brandcodes += ((typeof order_quantities[i] != 'undefined') ? order_quantities[i].value : 1) + division;
                } else {
                    temp_r += ((typeof order_quantities[i] != 'undefined') ? order_quantities[i].value : 1) + "()";
                }
            } else {
                if(!obj){
                    temp_brandcodes += branduids[i].value + division; 
                }else{
                    temp_r += document.getElementById('param_'+i).getAttribute(obj) + "()"; 
                }
            }
        }
    }
    if ( type == 'uid' && alertflag == true ){
        // Ç°Àý »óÇ° Æ÷ÇÔÇßÀ» °æ¿ì
        if (temp_brandcodes.length > 0 ){
            alert((language == 'eng') ? "Out-of-stock products are excluded." : "Ç°Àý »óÇ°Àº Á¦¿ÜµË´Ï´Ù.");
        } else {
            alert((language == 'eng') ? "You can not use out-of-stock products." : "Ç°Àý »óÇ°Àº »ç¿ëÇÒ ¼ö ¾ø½À´Ï´Ù.");
        }
    }

    if(!obj){
        return temp_brandcodes.substring(0, temp_brandcodes.length - 1);
    }else{
        return temp_r.substring(0, temp_r.length - 2);
    }
}
function importune_select(language){
    if (typeof language == 'undefined') {
        language = 'kor';
    }
    var obj = document.getElementById('allimportune');
    var senddata = check_brand('|','uid','',language);
    if( senddata != false){
        win = window.open('about:blank', 'importune_pop', 'width=507, height=700,scrollbars=auto');
        win.focus();
        obj.brandcode.value = senddata + "|";
        obj.target = 'importune_pop';
        obj.submit();
    }
}
function importune_all(language){
    if (typeof language == 'undefined') {
        language = 'kor';
    }
    var obj = document.getElementById('allimportune');
    if (obj.total_cnt.value > obj.alluid.value.split('|').length - 1 ) {
        alert((language == 'eng') ? "Out-of-stock products and products not available for request are excluded." : "Ç°Àý »óÇ° ¶Ç´Â Á¶¸£±â ¼³Á¤ÀÌ ¾ÈµÈ »óÇ°Àº Á¦¿ÜµË´Ï´Ù.");
    }
    win = window.open('about:blank', 'importune_pop', 'width=507, height=700,scrollbars=auto');
    win.focus();
    obj.brandcode.value = obj.alluid.value;
    obj.target = 'importune_pop';
    obj.submit();
}
