/**
 * 장바구니 담기 및 주문
 * @param arg
 * @param product_no
 * @param main_cate_no
 * @param display_group
 * @param has_option
 * @param product_min
 * @param basket_type
 * @return
 */
function add_basket( arg, product_no, main_cate_no, display_group, has_option, product_min, ship_prepaid, basket_type)
{
	var f = document.frm;
	if(has_option == 'T')
	{
		alert('옵션있는 상품입니다.\n상품상세화면에서 옵션선택 후에 장바구니에 담아주십시오.');

	}else{

		eval('var itemValue = ' + document.getElementById('aPrdInfo_' + product_min).value);
        if (itemValue.p_set_prd_flag == 'T') {
            if (confirm('세트상품은 옵션을 다시 선택해 주셔야 합니다.\n\n상품페이지로 이동하시겠습니까?') === true) {
                document.location.href="/Front/Product/?url=Product&product_no="+product_no+"&main_cate_no="+main_cate_no+"&display_group="+display_group;
            }
            return;
        }
        
        // 담기일경우 관심상품삭제
        if (arg == '0') {
            del_wish_product(product_no, itemValue.prd_item_code);
        }
        
		f.command.value = 'add';
		f.redirect.value = arg ;
		f.product_no.value = product_no;
		f.main_cate_no.value = main_cate_no;
		f.display_group.value = display_group;
		//f.product_min.value = product_min;
		f.prd_item_code.value = itemValue.prd_item_code;
		f.product_price.value = itemValue.product_price;
		if (f.delv_payment && (ship_prepaid=='C' || ship_prepaid=='P')) { // 스킨 수정 안했을 시에도 호환성 확보를 위해 검사합니다. -smyeo
		    f.delv_payment.value = ship_prepaid;
		}
		f.action="/front/php/basket.php?redirect_url=" + document.location.href;
		f.submit();
	}
}

/**
 * 관심상품 삭제처리
 * @param int iIdx
 * @param bool bType
 */
function del_wish_product(sPrdCode, sItemCode)
{
    var oIframe = document.createElement('iframe');
    oIframe.setAttribute('id', 'sWishDelIframe');
    oIframe.setAttribute('frameBorder', '0');
    oIframe.setAttribute('border', '0');
    oIframe.setAttribute('scrolling', 'no');
    oIframe.style.width = '0px';
    oIframe.style.height = '0px';
    oIframe.src = '/Front/Order/?url=WishDel&mode=prd_code&prd_code='+sPrdCode+'&item_code='+sItemCode;
    oIframe.style.display = 'none';
    document.getElementsByTagName('body')[0].appendChild(oIframe);
}


/**
 * wishlistFrm의 input hidden disabled 값 설정 - smyeo
 * @param int iIdx
 * @param bool bType
 */
function itemDisabled (iIdx, bType)
{
    var wishF = document.wishlistFrm;
    
    if (wishF['itemSel[]']) {
        if (wishF['itemSel[]'].length) {
            countCheckbox = wishF['itemSel[]'].length;
        } else {
            countCheckbox = 1;
        }
    } else {
        countCheckbox = 0;
    }
    
    if (countCheckbox > 1) {
        wishF['a_arg[]'][iIdx].disabled = bType;
        wishF['a_product_no[]'][iIdx].disabled = bType;
        wishF['a_main_cate_no[]'][iIdx].disabled = bType;
        wishF['a_display_group[]'][iIdx].disabled = bType;
        wishF['a_has_option[]'][iIdx].disabled = bType;
        wishF['a_product_min[]'][iIdx].disabled = bType;
        wishF['a_prd_item_code[]'][iIdx].disabled = bType;
        wishF['a_product_price[]'][iIdx].disabled = bType;
        wishF['a_delv_payment[]'][iIdx].disabled = bType;
    } else if (countCheckbox > 0) {
        wishF['a_arg[]'].disabled = bType;
        wishF['a_product_no[]'].disabled = bType;
        wishF['a_main_cate_no[]'].disabled = bType;
        wishF['a_display_group[]'].disabled = bType;
        wishF['a_has_option[]'].disabled = bType;
        wishF['a_product_min[]'].disabled = bType;
        wishF['a_prd_item_code[]'].disabled = bType;
        wishF['a_product_price[]'].disabled = bType;
        wishF['a_delv_payment[]'].disabled = bType;
    }
}

/**
 * 선택항목 장바구니 담기 및 주문
 */
function sel_add_basket(arg)
{
    var wishF = document.wishlistFrm;
    var main_cate_no = document.wishlistFrm.main_cate_no.value;
    var offset = document.wishlistFrm.offset.value;
    var page = document.wishlistFrm.page.value;
    var count=0;
    var countCheckbox = 0;
    
    var bAlertFlag = true;
    
    if (wishF['itemSel[]']) {
        if (wishF['itemSel[]'].length) {
            countCheckbox = wishF['itemSel[]'].length;
        } else {
            countCheckbox = 1;
        }
    } else {
        countCheckbox = 0;
    }
    
    if (countCheckbox == 1) {
        if (wishF['itemSel[]'].checked == false) {
            itemDisabled(i,true);
        } else {
            itemDisabled(i,false);
            eval('var itemValue =' + document.getElementById('aPrdInfo_' + wishF['a_product_min[]'].value).value);
            if(wishF['a_has_option[]'].value == 'T')
            {
                alert('옵션있는 상품입니다.\n상품상세화면에서 옵션선택 후에 장바구니에 담아주십시오.');
            } else if (itemValue.p_set_prd_flag == 'T') {
                if (confirm('세트상품은 옵션을 다시 선택해 주셔야 합니다.\n\n상품페이지로 이동하시겠습니까?') === true) {
                    bAlertFlag = false;
                    document.location.href="/Front/Product/?url=Product&product_no="+wishF['a_product_no[]'].value+"&main_cate_no="+wishF['a_main_cate_no[]'].value+"&display_group="+wishF['a_display_group[]'].value;
                    return false;
                } else {
                    itemDisabled(i,true);
                }
            } else {
                wishF['a_prd_item_code[]'].value = itemValue.prd_item_code;
                wishF['a_product_price[]'].value = itemValue.product_price;
                count++;
            }
        }
    } else {
        for (var i=0; i<countCheckbox; i++) {
            if (wishF['itemSel[]'][i].checked == false) {
                itemDisabled(i,true);
            } else {
                itemDisabled(i,false);
                eval('var itemValue =' + document.getElementById('aPrdInfo_' + wishF['a_product_min[]'][i].value).value);
                if(wishF['a_has_option[]'][i].value == 'T')
                {
                    alert('옵션있는 상품입니다.\n상품상세화면에서 옵션선택 후에 장바구니에 담아주십시오.');
                } else if (itemValue.p_set_prd_flag == 'T') {
                    if (confirm('세트상품은 옵션을 다시 선택해 주셔야 합니다.\n\n상품페이지로 이동하시겠습니까?') === true) {
                        bAlertFlag = false;
                        document.location.href="/Front/Product/?url=Product&product_no="+wishF['a_product_no[]'][i].value+"&main_cate_no="+wishF['a_main_cate_no[]'][i].value+"&display_group="+wishF['a_display_group[]'][i].value;
                        return false;
                    } else if (bAlertFlag === true) {
                        alert('해당 상품을 제외하고 장바구니에 담습니다.');
                        itemDisabled(i,true);
                    }
                } else {
                    wishF['a_prd_item_code[]'][i].value = itemValue.prd_item_code;
                    wishF['a_product_price[]'][i].value = itemValue.product_price;
                    count++;
                }
            }
        }
    }

    if (count == 0 && bAlertFlag === true) {
        alert('선택된 상품이 없습니다.');
        return false;
    }
    
    wishF.command.value = 'add';
    wishF.redirect.value = arg;
    
    wishF.action='/front/php/basket.php?redirect_url=' + document.location.href;
    wishF.submit();
}

/**
 * 선택항목 관심상품 삭제 
 */
function sel_del_mywish()
{
    var wishF = document.wishlistFrm;
    var main_cate_no = document.wishlistFrm.main_cate_no.value;
    var offset = document.wishlistFrm.offset.value;
    var page = document.wishlistFrm.page.value;
    var count=0;
    var countCheckbox = 0;
    if (wishF['itemSel[]']) {
        if (wishF['itemSel[]'].length) {
            countCheckbox = wishF['itemSel[]'].length;
        } else {
            countCheckbox = 1;
        }
    } else {
        countCheckbox = 0;
    }
    if (countCheckbox == 1) {
        if (wishF['itemSel[]'].checked == false) {
            itemDisabled(i,true);
        } else {
            itemDisabled(i,false);
            count++;
        }
    } else {
        for (var i=0; i<countCheckbox; i++) {
            if (wishF['itemSel[]'][i].checked == false) {
                itemDisabled(i,true);
            } else {
                itemDisabled(i,false);
                count++;
            }
        }
    }
    
    if (count==0) {
        alert('선택된 상품이 없습니다.');
        return false;
    }
    if (!confirm('선택하신 상품을 삭제하시겠습니까?')) {
        return false;
    }
    
    wishF.action='/front/php/myshop/myshop_wishlist_a.php?main_cate_no='+main_cate_no+'&offset='+offset+'&page='+page;
    wishF.submit();
}

/**
 * 모든 항목 선택/해제
 */
function all_select(item)
{
    var wishF = document.wishlistFrm;
    var countCheckbox = 0;
    if (wishF['itemSel[]']) {
        if (wishF['itemSel[]'].length) {
            countCheckbox = wishF['itemSel[]'].length;
        } else {
            countCheckbox = 1;
        }
    } else {
        countCheckbox = 0;
    }
    if (countCheckbox == 1) {
        if (item.checked == true) {
            wishF['itemSel[]'].checked=true;
        } else {
            wishF['itemSel[]'].checked=false;
        }
    } else if (countCheckbox > 0) {
        if (item.checked == true) {
            for (var i=0; i<wishF['itemSel[]'].length; i++) {
                wishF['itemSel[]'][i].checked=true;
            }
        } else {
            for (var i=0; i<wishF['itemSel[]'].length; i++) {
                wishF['itemSel[]'][i].checked=false;
            }
        }
    }
}