/*******************************************************************************
 *
 *  Copyright 1998-2008 Today & Tomorrow. All rights reserved.
 *  Page: TTMallBuilder
 *  Date: 2007-02-06
 *
 *  Logs : »óÇ° °ü·Ã JavaScript Function's
 *
 *******************************************************************************/

/*******************************************************
 * Goods Global Function's
 *******************************************************/
function on_page(page) {
    document.ListFrm.action = 'mall.cgi?skin=list2&category_code='+rCategoryCode+'&hCategory=' + hCategory +'&page='+page;
    document.ListFrm.submit();
}

function onPage(frm, page) {
    frm.action = 'mall.cgi?skin=list2&category_code='+rCategoryCode+'&page='+page;
    frm.submit();
}

// ¼îÇÎ°è¼Ó ÇÏ±â
function onBack() {
   var return_url = unescape(view_GetCookie('view_return_path'));
   if (return_url == '')
      history.back();
   else
      document.location.href = return_url;
}


/*******************************************************
 * ÀÏ¹Ý »óÇ°
 *******************************************************/
function onGoodsList(isLeaf, categorycode) {
    if (isLeaf == "0")
      document.location.href = 'mall.cgi?skin=list1&category_code=' + categorycode;
    else
      document.location.href = 'mall.cgi?skin=list2&category_code=' + categorycode + '&hCategory=' + categorycode;
}

function onGoodsList2(isLeaf, categorycode, hCategory) {
    if (isLeaf == "0")
      document.location.href = 'mall.cgi?skin=list1&category_code=' + categorycode;
    else
      document.location.href = 'mall.cgi?skin=list2&category_code=' + categorycode + '&hCategory=' + hCategory;
}

function onGoodsView(rPath, goodscode, categorycode) {
    view_SetCookie('view_return_path', rPath);
    document.location.href = 'mall.cgi?skin=view&goods_code=' + goodscode + '&category_code=' + categorycode;
}
function onGoodsView2(rPath, goodscode, categorycode, hCategory) {
    view_SetCookie('view_return_path', rPath);
    document.location.href = 'mall.cgi?skin=view&goods_code=' + goodscode + '&category_code=' + categorycode + '&hCategory=' + hCategory;
}

function onChangeSimilarItem(f,apply_category_code)
{
   //var f = document.ViewForm;
   var similar_goods_code;

   if( typeof(f.similar_goods_code)=="object" ) {
      similar_goods_code=f.similar_goods_code.options[f.similar_goods_code.selectedIndex].value;
      document.location.href = "mall.cgi?skin=view&category_code=" + apply_category_code + "&goods_code="+similar_goods_code;
   }
}

// Å«ÀÌ¹ÌÁö º¸±â
function on_zoom(large_image, image_width, image_height, goodscode) {
    if (large_image == '')
        return;

    var wstyle = 'width='+image_width+',height='+image_height+',top=0,left=0';
    var popWin = window.open('mall.cgi?skin=goods_large_image_view&goods_code='+goodscode+'', 'LargeImageViewWin', wstyle);
    popWin.focus();
}

// ´Ù¸¥°¢µµ º¸±â
function on_different_angle_next() {
   while (1) {
      angle_num++;

      if (angle_num > 3) angle_num = 0;
      if (angle_images[angle_num] != '') {
         div_p_medium_image.innerHTML = angle_images[angle_num];
         break;
      }
   }
}

function on_different_angle_prev() {
   while (1) {
      angle_num--;

      if (angle_num < 0) angle_num = 3;
      if (angle_images[angle_num] != '') {
         div_p_medium_image.innerHTML = angle_images[angle_num];
         break;
      }
   }
}

// ÄÁÅÙÃ÷ º¸±â
function onConView(goodscode, cwidth, cheight, ctype) {
    if(ctype == "") alert('ÄÁÅÙÃ÷ Å¸ÀÔÀ» ÀÔ·Â ÇØÁÖ¼¼¿ä.');
    var wstyle = 'top=0,left=0';

    if (cwidth > 0) {
        var width = cwidth + 20;
        if (wstyle == '')
            wstyle += ' width=' + width;
        else
            wstyle += ' ,width=' + width;
    }
    if ( cheight > 0) {
        var height = cheight + 20;
        if (wstyle == '')
            wstyle += ' height=' + height;
        else
            wstyle += ' ,height=' + height;
    }

    var popWin = window.open('mall.cgi?skin=goods_'+ctype+'_view&goods_code='+goodscode, 'FlashViewWin', wstyle);
    popWin.focus();
}

function onSort(frm, opt, categorycode, hCategory) {
    if (opt == '') return;
    frm.action = 'mall.cgi?skin=list2&category_code=' +categorycode + '&hCategory=' + hCategory + '&page=1';
    frm.submit();
}

function onGoodsCartIn(frm, rPath, goodscode) {
    var qty = Number(frm.order_amount.value);
    if (qty <= 0) {
      alert('ÁÖ¹® ¼ö·®Àº 1°³ ÀÌ»óÀÌ¾î¾ß ÇÕ´Ï´Ù.');
      frm.order_amount.focus();
      return;
    }
    checkMultivalueItem(frm);
    frm.action = 'mall.cgi?act=CartReg&skin=cart_reg&return_path=' + rPath + ']&goods_code=' + goodscode + '&order_amount=1';
    frm.submit();
}

function onGoodsWishIn(frm, rPath, goodscode) {
    var qty = Number(frm.order_amount.value);
    if (qty <= 0) {
      alert('ÁÖ¹® ¼ö·®Àº 1°³ ÀÌ»óÀÌ¾î¾ß ÇÕ´Ï´Ù.');
      frm.order_amount.focus();
      return;
    }
    checkMultivalueItem(frm);
    frm.action = 'mall.cgi?act=WishListReg&skin=wishlist_reg&return_path=' + rPath + '&goods_code=' + goodscode + '&order_amount=1';
    frm.submit();
}

//  »óÇ°ÀÇ ¿É¼Ç°ú ¼ö·®¿¡ µû¸¥ ÆÇ¸Å°¡¸¦ °è»êÇÏ¿© Àû¿ëÇÑ´Ù. [ ÆÄ¶ó¸ÞÅÍ : Æû°´Ã¼(document.ViewForm) ]
function getGoodsOrderCost(frm) {
    var qty = frm.order_amount.value;
    var unit_price = frm.goods_cost.value;
    var order_cost = 0;
    
    // ±Ý¾×¿¡¼­ `,` Á¦°Å
    for ( i = 0; i < 4; i++)
        unit_price = unit_price.replace(',', '');

    unit_price = parseInt(unit_price);
    order_cost = unit_price;

    for (var i = 0; i < frm.length; i++) {
        var elem = frm.elements[i];
        var field_name = elem.name;
        var field_value;

        if (elem.type == 'select-one') {
            field_value = elem.options[elem.selectedIndex].value;
        }
        else {
            field_value = elem.value;
        }

        if (field_value.indexOf('=+') != -1) {
            var add_price = field_value.substr(field_value.indexOf('=+')+2);
            order_cost += parseInt(add_price);
        }
        else if (field_value.indexOf('=-') != -1) {
            var sub_price = field_value.substr(field_value.indexOf('=-')+2);
            order_cost -= parseInt(sub_price);
        }
    }

    order_cost *= qty;
    document.getElementById('div_goods_cost').innerHTML = commify(order_cost);
}

// Mulit option check
function checkMultivalueItem(frm) {
  for (var i = 0; i < frm.elements.length; i++) {
    var elm = frm.elements[i];
    var regex = /multivalue_item_data_.*/
    if (elm.name.search(regex) != -1) {
      if (typeof(elm.options) == 'object') {
        if (elm.options[elm.selectedIndex].value == '') {
          var item_obj = elm.name;
          item_obj = item_obj.replace('multivalue_item_data', 'multivalue_item_name');
          var item_name = eval('frm.' + item_obj).value;
          alert(item_name + 'À»(¸¦) ¼±ÅÃÇØÁÖ¼¼¿ä.');
          elm.focus();
          return false;
        }
      }
    }
  }
  return true;
}

// ÁÖ¹®¼ö·® Áõ°¡
function onOrderAmountIncrease() {
    var OrderAmount;
    var frm = document.ViewForm;
    if (frm.order_amount.value == "")
        OrderAmount = 0;
    else
        OrderAmount = Number(frm.order_amount.value);

    OrderAmount++;
    frm.order_amount.value = "" + OrderAmount;

    // ÆÇ¸Å°¡°Ý °è»ê
    getGoodsOrderCost(frm);
}

// ÁÖ¹®¼ö·® °¨¼Ò
function onOrderAmountDecrease() {
  var OrderAmount;
  var frm = document.ViewForm;
  if (frm.order_amount.value == "")
    OrderAmount = 0;
  else
    OrderAmount = Number(frm.order_amount.value);
  OrderAmount--;
  if (OrderAmount <= 1) OrderAmount = 1;
  frm.order_amount.value = "" + OrderAmount;

  // ÆÇ¸Å°¡°Ý °è»ê
  getGoodsOrderCost(frm);
}

// °ü½ÉÇ×¸ñ µî·Ï
function AddToList(frm, rPath, goodscode) {
    if (!checkMultivalueItem(frm)) { return; }
    var amount = Number(frm.order_amount.value);
    if (amount < 1) { alert('ÁÖ¹® ¼ö·®Àº 1°³ ÀÌ»óÀÌ¾î¾ß ÇÕ´Ï´Ù.'); frm.order_amount.focus(); return; }
    frm.action = 'mall.cgi?act=WishListReg&skin=wishlist_reg&return_path='+rPath+'&goods_code='+goodscode+'&order_amount='+amount+'&sum_step=1&goods_code_1='+goodscode+'&order_amount_1='+amount;
    frm.submit();
}

// »óÇ° ÀÌ¸ÞÀÏ ¹ß¼Û
function SendEmail(rskin, rfile, rgoodscode, rPath) {
    var url = 'mall.cgi?skin=goods_mailing';
    url += '&receiver_email=' + document.ViewForm.receiver_email.value
    url += '&goods_view_skin=' + rskin + '&goods_view_file=' + rfile + '&goods_code=' + rgoodscode + '&goods_view_path=' + rPath;
    window.open(url, '', 'width=610,height=400,scrollbars=1,resizable=1');
}

// Àå¹Ù±¸´Ï ³Ö±â
function AddToCart(frm, rPath, stock_amount, goodscode) {
  if (!checkMultivalueItem(frm)) { return; }
  if ( Number(stock_amount) == 0 ) { alert('Ç°Àý »óÇ° ÀÔ´Ï´Ù.'); return; }
  var qty = Number(frm.order_amount.value);
  if (qty < 1) { alert('ÁÖ¹® ¼ö·®Àº 1°³ ÀÌ»óÀÌ¾î¾ß ÇÕ´Ï´Ù.'); frm.order_amount.focus(); return; }
  frm.action = 'mall.cgi?act=CartReg&skin=cart_reg&return_path='+rPath+'&goods_code='+goodscode+'&order_amount='+qty+'&sum_step=1&goods_code_1='+goodscode+'&order_amount_1='+qty;
  frm.submit();
}

// ¹Ù·Î±¸¸Å
function BuyNow(frm, rPath, stock_amount, goodscode) {
  if (!checkMultivalueItem(frm)) { return; }
  if (  Number(stock_amount) == 0 ) { alert('Ç°Àý »óÇ° ÀÔ´Ï´Ù.'); return; }
  var frm = document.ViewForm;
  var qty = Number(frm.order_amount.value);
  if (qty < 1) { alert('ÁÖ¹® ¼ö·®Àº 1°³ ÀÌ»óÀÌ¾î¾ß ÇÕ´Ï´Ù.'); frm.order_amount.focus(); return; }
  frm.action = 'mall.cgi?act=GoodsBuyDelivery&skin=goods_buy_delivery&return_path='+rPath+'&sum_step=1&goods_code_1='+goodscode+'&order_amount_1=' + qty;
  frm.submit();
}




/*******************************************************
 * °øµ¿±¸¸Å »óÇ°
 *******************************************************/
function onCobuyGoodsView(rPath, goodscode,  categorycode) {
  view_SetCookie("view_return_path", rPath);
  document.location.href = "mall.cgi?skin=cobuy_view&goods_code=" + goodscode + "&category_code=" + categorycode;
}


/*******************************************************
 * »óÇ°Æò °ü·Ã Function's
 *******************************************************/
function on_review_reg()
{
   var f = document.review_form;
   var CookieRegExp=/^\$.*/;
   CookieUID="[cookie.uid]"
   if(CookieUID=="" || CookieUID.search(CookieRegExp)!=-1)
   {
      if( f.name.value=="" )
      {
         alert('»óÇ°Æò ÀÛ¼ºÀÚ¸¦ ÀÔ·ÂÇØ ÁÖ½Ê½Ã¿À.');
         f.name.focus();
         return;
      }
      if( f.pwd.value=="" )
      {
         alert('»óÇ°Æò ºñ¹Ð¹øÈ£¸¦ ÀÔ·ÂÇØ ÁÖ½Ê½Ã¿À.');
         f.pwd.focus();
         return;
      }
   }

   if( f.point.value=="" )
   {
      alert('»óÇ°Æò ÆòÁ¡À» ¼±ÅÃÇØ ÁÖ½Ê½Ã¿À.');
      f.point.focus();
      return;
   }
   if( f.comment.value=="" )
   {
      alert('»óÇ°Æò ³»¿ëÀ» ÀÔ·ÂÇØ ÁÖ½Ê½Ã¿À.');
      f.comment.focus();
      return;
   }

   f.action = "mall.cgi?act=GoodsReviewReg&skin=goods_review_reg&return_path=" + rPath;
   f.submit();
}

function on_review_modify(goodscode, code) {
  var url = 'mall.cgi?skin=goods_review_pwd_modify&return_path='+rPath+'&goods_code='+goodscode+'&code='+code;
  window.open(url, '', 'width=500,height=300,scrollbars=0,resizable=1');
}

function on_review_del(goodscode, code) {
  var url = 'mall.cgi?skin=goods_review_pwd_del&return_path='+rPath+'&goods_code='+goodscode+'&code='+code;
  window.open(url, '', 'width=500,height=300,scrollbars=0,resizable=1');
}

function on_review_modify_(goodscode, code) {
  var url = 'mall.cgi?act=GoodsReviewModify&skin=goods_review_modify&return_path='+rPath+'&goods_code='+goodscode+'&code='+code;
  window.open(url, '', 'width=500,height=300,scrollbars=0,resizable=1');
}

function on_review_del_(goodscode, code) {
  if (!confirm('»óÇ°ÆòÀ» »èÁ¦ÇÏ½Ã°Ú½À´Ï±î?')) return;
  var url = 'mall.cgi?act=GoodsReviewDel&skin=goods_review_del_member&return_path='+rPath+'&goods_code='+goodscode+'&code='+code;
  document.location.href = url;
}


