/* $Id : shopping_flow.js 4865 2007-01-31 14:04:10Z paulgao $ */



var selectedShipping = null;

var selectedPayment  = null;

var selectedPack     = null;

var selectedCard     = null;

var selectedSurplus  = '';

var selectedBonus    = 0;

var selectedIntegral = 0;

var selectedOOS      = null;

var alertedSurplus   = false;



var groupBuyShipping = null;

var groupBuyPayment  = null;



/* *

 * 改变配送方式

 */

function selectShipping(obj)

{

  if (selectedShipping == obj)

  {

    return;

  }

  else

  {

    selectedShipping = obj;

  }



  var supportCod = obj.attributes['supportCod'].value + 0;

  var theForm = obj.form;



  for (i = 0; i < theForm.elements.length; i ++ )

  {

    if (theForm.elements[i].name == 'payment' && theForm.elements[i].attributes['isCod'].value == '1')

    {

      if (supportCod == 0)

      {

        theForm.elements[i].checked = false;

        theForm.elements[i].disabled = true;

      }

      else

      {

        theForm.elements[i].disabled = false;

      }

    }

  }



  if (obj.attributes['insure'].value + 0 == 0)

  {

    document.getElementById('ECS_NEEDINSURE').checked = false;

    document.getElementById('ECS_NEEDINSURE').disabled = true;

  }

  else

  {

    document.getElementById('ECS_NEEDINSURE').checked = false;

    document.getElementById('ECS_NEEDINSURE').disabled = false;

  }



  var now = new Date();

  Ajax.call('order.php?act=select_shipping', 'shipping=' + obj.value, orderShippingSelectedResponse, 'GET', 'JSON');

}



/**

 *

 */

function orderShippingSelectedResponse(result)

{

  if (result.need_insure)

  {

    try

    {

      document.getElementById('ECS_NEEDINSURE').checked = true;

    }

    catch (ex)

    {

      alert(ex.message);

    }

  }



  try

  {

    if (document.getElementById('ECS_CODFEE') != undefined)

    {

      document.getElementById('ECS_CODFEE').innerHTML = result.cod_fee;

    }

  }

  catch (ex)

  {

    alert(ex.message);

  }



  orderSelectedResponse(result);

}



/* *

 * 改变支付方式

 */

function selectPayment(obj)

{

  if (selectedPayment == obj)

  {

    return;

  }

  else

  {

    selectedPayment = obj;

  }



  Ajax.call('order.php?act=select_payment', 'payment=' + obj.value, orderSelectedResponse, 'GET', 'JSON');

}



/* *

 * 改变商品包装

 */

function selectPack(obj)

{

  if (selectedPack == obj)

  {

    return;

  }

  else

  {

    selectedPack = obj;

  }



  Ajax.call('order.php?act=select_pack', 'pack=' + obj.value, orderSelectedResponse, 'GET', 'JSON');

}



/* *

 * 改变祝福贺卡

 */

function selectCard(obj)

{

  if (selectedCard == obj)

  {

    return;

  }

  else

  {

    selectedCard = obj;

  }



  Ajax.call('order.php?act=select_card', 'card=' + obj.value, orderSelectedResponse, 'GET', 'JSON');

}



/* *

 * 选定了配送保价

 */

function selectInsure(needInsure)

{

  needInsure = needInsure ? 1 : 0;



  Ajax.call('order.php?act=select_insure', 'insure=' + needInsure, orderSelectedResponse, 'GET', 'JSON');

}





/* *

 * 改变红包

 */

function changeBonus(val)

{

  if (selectedBonus == val)

  {

    return;

  }

  else

  {

    selectedBonus = val;

  }



  Ajax.call('order.php?act=change_bonus', 'bonus=' + val, changeBonusResponse, 'GET', 'JSON');

}



/* *

 * 改变红包的回调函数

 */

function changeBonusResponse(obj)

{

  if (obj.error)

  {

    alert(obj.error);



    try

    {

      document.getElementById('ECS_BONUS').value = '0';

    }

    catch (ex) { }

  }

  else

  {

    orderSelectedResponse(obj.content);

  }

}



/**

 * 验证红包序列号

 * @param string bonusSn 红包序列号

 */

function validateBonus(bonusSn)

{

  Ajax.call('order.php?act=validate_bonus', 'bonus_sn=' + bonusSn, validateBonusResponse, 'GET', 'JSON');

}



function validateBonusResponse(obj)

{



if (obj.error)

  {

    alert(obj.error);

    orderSelectedResponse(obj.content);

    try

    {

      document.getElementById('ECS_BONUSN').value = '0';

    }

    catch (ex) { }

  }

  else

  {

    orderSelectedResponse(obj.content);

  }

}



/* *

 * 改变积分

 */

function changeIntegral(val)

{

  if (selectedIntegral == val)

  {

    return;

  }

  else

  {

    selectedIntegral = val;

  }



  Ajax.call('order.php?act=change_integral', 'points=' + val, changeIntegralResponse, 'GET', 'JSON');

}



/* *

 * 改变积分回调函数

 */

function changeIntegralResponse(obj)

{

  if (obj.error)

  {

    try

    {

      document.getElementById('ECS_INTEGRAL_NOTICE').innerHTML = obj.error;

      document.getElementById('ECS_INTEGRAL').value = '0';

      document.getElementById('ECS_INTEGRAL').focus();

    }

    catch (ex) { }

  }

  else

  {

    try

    {

      document.getElementById('ECS_INTEGRAL_NOTICE').innerHTML = '';

    }

    catch (ex) { }

    orderSelectedResponse(obj.content);

  }

}



/* *

 * 改变余额

 */

function changeSurplus(val)

{

  if (selectedSurplus == val)

  {

    return;

  }

  else

  {

    selectedSurplus = val;

  }



  Ajax.call('order.php?act=change_surplus', 'surplus=' + val, changeSurplusResponse, 'GET', 'JSON');

}



/* *

 * 改变余额回调函数

 */

function changeSurplusResponse(obj)

{

  if (obj.error)

  {

    try

    {

      document.getElementById("ECS_SURPLUS_NOTICE").innerHTML = obj.error;

      document.getElementById('ECS_SURPLUS').value = '0';

      document.getElementById('ECS_SURPLUS').focus();

    }

    catch (ex) { }

  }

  else

  {

    try

    {

      document.getElementById("ECS_SURPLUS_NOTICE").innerHTML = '';

    }

    catch (ex) { }

    orderSelectedResponse(obj.content);

  }

}



/* *

 * 改变发票的方式

 */

function changeNeedInv()

{

  var obj        = document.getElementById('ECS_NEEDINV');

  var objType    = document.getElementById('ECS_INVTYPE');

  var objPayee   = document.getElementById('ECS_INVPAYEE');

  var objContent = document.getElementById('ECS_INVCONTENT');

  var needInv    = obj.checked ? 1 : 0;

  var invType    = obj.checked ? (objType != undefined ? objType.value : '') : '';

  var invPayee   = obj.checked ? objPayee.value : '';

  var invContent = obj.checked ? objContent.value : '';

  objType.disabled = objPayee.disabled = objContent.disabled = ! obj.checked;

  if(objType != null)

  {

    objType.disabled = ! obj.checked;

  }



  Ajax.call('order.php?act=change_needinv', 'need_inv=' + needInv + '&inv_type=' + encodeURIComponent(invType) + '&inv_payee=' + encodeURIComponent(invPayee) + '&inv_content=' + encodeURIComponent(invContent), orderSelectedResponse, 'GET');

}



/* *

 * 回调函数

 */

function orderSelectedResponse(result)

{

  if (result.error)

  {

    alert(result.error);

    location.href = './';

  }



  try

  {

    var layer = document.getElementById("ECS_ORDERTOTAL");



    layer.innerHTML = (typeof result == "object") ? result.content : result;



    if (result.payment != undefined)

    {

      var surplusObj = document.forms['theForm'].elements['surplus'];

      if (surplusObj != undefined)

      {

        surplusObj.disabled = result.pay_code == 'balance';

      }

    }

  }

  catch (ex) { }

}

