/*
Quick.Cart plugins JavaScript
*/
function changeAttributesPrice( ){
  var fPriceAdd = 0;
  var aSelects = gEBI( 'attributes' ).getElementsByTagName( 'select' );
  for( var i = 0; i < aSelects.length; i++ ){
    var aSelect = aSelects[i];
    if( aSelect.className == 'attributes' ){
      var aSelectValue = aSelect.options[aSelect.options.selectedIndex].value;
      if( aSelectValue != '' )
        var aAttribute = aSelectValue.split( "|" );
      else
        var aAttribute = Array( 0, 0 );
      if( aAttribute[1] != '' ){
        aAttribute[1] = fix( aAttribute[1] ) * 1;
        fPriceAdd += aAttribute[1];
      }
    }
  } // end for
  gEBI( 'price' ).innerHTML = changePriceFormat( fix( fPrice * 1 + fPriceAdd ) );
} // end function changeAttributesPrice

/**
* Counts order summary after selecting state
* @return void
* @param object
*/
function ordersStates( obj ){

  var aState, taxPercent, tax, orderCost;
  if( obj.value != '' )
    aState = obj.value.split( "|" );
  else
    aState = Array( '', '0.00', '' );

  gEBI( "oStateSymbol" ).value = aState[2];
  if(gEBI( "oStateId" )){ //this allows this function to also be run from the usersDiscount plugin (FORM block of users-admin.tpl)
    gEBI( "oStateId" ).value = aState[0];

    orderCost = typeof fOrderCost == 'undefined' ? gEBI( "orderCost" ).innerHTML * 1 : fOrderCost * 1;
    taxPercent = Math.abs( aState[1] );
    tax = orderCost * ( taxPercent / 100 );

    gEBI( "stateTaxCost" ).innerHTML  = changePriceFormat( tax );
    gEBI( "summaryCost" ).innerHTML   = changePriceFormat( orderCost + ( gEBI( "deliveryCost" ).innerHTML * 1 ) + tax );
  }
} // end function ordersStates

/**
* Checks for user's state being set in a hidden input field and sets the select up if found
* @return void
*/
function checkUsersStateValue(){
  var oSel = gEBI('sStateSelect')
    , oSym = gEBI('oStateSymbol')
    ;
  if(oSel && oSym && oSym.value){
    var opl = oSel.options.length, j = 0;
    while(j < opl && oSel.options[j].value.slice(-2) != oSym.value){ j++; }
    oSel.options[(j < opl ? j : 0)].selected = true;
  }
} // end function checkUsersStateValue

