<?php
/*
  www.oscGold.com (C) Kamelia-Net Jacek Krysiak
  modul oscGold wer 4.0
*/
?>

<script language="javascript" type="text/javascript"><!--

  function setShipping(method) {
    if (document.getElementById("ot_shipping[title]")) {
      document.getElementById("ot_shipping[title]").value = document.getElementById("update_shipping["+method+"][title]").value;
      document.getElementById("ot_shipping[value]").value = document.getElementById("update_shipping["+method+"][value]").value;
      document.getElementById("ot_shipping[id]").value = document.getElementById("update_shipping["+method+"][id]").value;
    }
  }


  function currency(value) {
    var selObj = document.getElementById("update_info_payment_currency");
    var selIndex = selObj.selectedIndex;
    var currency;
    var currency_value;
    <?php
    $currency_query_raw = "SELECT code, value FROM " . TABLE_CURRENCIES . "";
    $currency_query = tep_db_query($currency_query_raw);
    while ($currency = tep_db_fetch_array($currency_query)) {
      echo '                  if (selObj.options[selIndex].value == \'' . $currency['code'] . '\') {' . "\n";
      echo '                  document.getElementById("update_info_payment_currency_value").value = \'' . $currency['value'] . '\';' . "\n";
      echo '                  currency = \'' . $currency['code'] . '\'' . "\n";
      echo '                  currency_value = \'' . $currency['value'] . '\'' . "\n";
      echo '                  }' . "\n";
      echo "\n";
    }
    ?>
  }


///begin standard JavaScript for edit_orders.php file

   function setAddressVisibility(szDivID, checkID) {
     var obj = document.getElementById(szDivID);

      if (checkID.checked) {
        obj.style.visibility = "hidden";
        obj.style.display = "none";
      } else {
        obj.style.visibility = "visible";

		//fix IE, since Microsoft isn't going to
		if (window.navigator.userAgent.indexOf('MSIE') != -1) {
		   obj.style.display = "inline"; //IE is not standards compliant, apparently not even IE7
		} else {
		   obj.style.display = "table-row";
		}//this should all just be obj.style.display = "table-row"; but IE is not standards compliant

      }
    }


    function update_zone(countryID, zoneID, inputID, menuID) {
    var theForm = document.edit_order;
	var NumState = theForm[countryID].options.length;
    var SelectedCountry = '';

    while(NumState > 0) {
      NumState--;
      theForm[zoneID].options[NumState] = null;
    }

    SelectedCountry = theForm[countryID].options[theForm[countryID].selectedIndex].value;

<?php echo oe_js_zone_list('SelectedCountry', 'theForm', 'zoneID', 'inputID', 'menuID'); ?>
  }


     function setStateVisibility(ID, vis, ID2) {

      var obj = document.getElementById(ID);
      var obj2 = document.getElementById(ID2);

      if (vis == "hidden") {
        obj.style.visibility = "hidden";
        obj.style.display = "none";
        obj2.style.visibility = "visible";
        obj2.style.display = "inline";
      } else {
        obj.style.visibility = "visible";
        obj.style.display = "inline";
        obj2.style.visibility = "hidden";
        obj2.style.display = "none";

         } //end if vis == hidden
     } //end function


  function openWindow(file, windowName) {
    msgWindow = window.open(file, windowName,'top=250, left=250, width=550, height=450, location=0, status=1, toolbar=0, resize=1');
    if (!msgWindow.opener) msgWindow.opener = self;
  }


  function selectRowEffect(object, buttonSelect) {
    document.getElementById("shipping_radio_" + buttonSelect).checked=true;
  }


  function rowOverEffect(object) {
    object.className = 'dataTableRowSelected';
  }

  function rowOutEffect(object, oldClass) {
    object.className = oldClass;
  }

  function setVisibility(id, checkID) {
    var obj;

	obj = document.getElementById(id);

    if (obj != null) {
      if (obj.style) {
        obj = obj.style;
      }
      if (checkID.checked == true) {
        obj.visibility = 'visible';
      } else {
        obj.visibility = 'hidden';
      }
    }
  }



  function setCustomOTVisibility(ID, vis, ID2) {

      var obj = document.getElementById(ID);
      var obj2 = document.getElementById(ID2);

      if (vis == "hidden") {
        obj.style.visibility = "hidden";
        obj.style.display = "none";
        obj2.style.visibility = "visible";
        obj2.style.display = "inline";
      } else {
        obj.style.visibility = "visible";

		  //fix IE, since Microsoft can't
		  if (window.navigator.userAgent.indexOf('MSIE') != -1) {
		  obj.style.display = "inline"; //IE is not standards compliant, apparently not even IE7
		  } else {
		  obj.style.display = "table-row";
		  }//this should all just be obj.style.display = "table-row", however IE is not standards compliant

        obj2.style.visibility = "hidden";
        obj2.style.display = "none";

	  } //end if (vis == "hidden") {
   } //end  function setCustomOTVisibility(ID, vis, ID2) {


  function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
} // end function addLoadListener(fn)


<?php echo "ORDER_EDITOR_PAYMENT_DROPDOWN = '" . ORDER_EDITOR_PAYMENT_DROPDOWN . "';"; ?>


addLoadListener(init);

function init()
{
  var optional = document.getElementById("optional");
  if (optional) {
  optional.className = "hidden";
  }
  //START dropdown option for payment method by quick_fixer
      if (ORDER_EDITOR_PAYMENT_DROPDOWN == 'true') {
            var selObj = document.getElementById('update_info_payment_method');
            if (selObj) { var selIndex = selObj.selectedIndex; }

        //text in lieu of value supported by firefox and mozilla but not others SO MAKE SURE text and optional value are the same (in the payment dropdown they are)
            if (selObj.options[selIndex].text) {
                var paymentMethod = selObj.options[selIndex].text;
            }
            else {
                var paymentMethod = selObj.options[selIndex].value;
            }
        }
        else {
            //if you only use an input field to display payment method use this
            var selObj = document.getElementById('update_info_payment_method');
            if (selObj) { var paymentMethod = selObj.value; }
        }

    //END dropdown option for payment method by quick_fixer

  if (optional) {
  if (paymentMethod == "<?php echo ORDER_EDITOR_CREDIT_CARD ?>") {
  optional.className = "";
  return true;
  } else {
  optional.className = "hidden";
  return true;
  }
  }

} // end function init()

  function updatePrices(action, pid, taxdescription) {
  //calculates all the different values as new entries are typed
    var qty = document.getElementById("update_products[" + pid + "][qty]").value;
	var taxRate = document.getElementById("update_products[" + pid + "][tax]").value;
	var attValue = getAttributesPrices(pid);

	if ((action == 'qty') || (action == 'tax') || (action == 'att_price') || (action == 'price')) {

	var finalPriceValue = document.getElementById("update_products[" + pid + "][price]").value;
	var priceInclValue = document.getElementById("update_products[" + pid + "][price]").value;
	var totalInclValue = document.getElementById("update_products[" + pid + "][price]").value;
	var totalExclValue = document.getElementById("update_products[" + pid + "][price]").value;

	finalPriceValue = Number(attValue) + Number(finalPriceValue);
	priceInclValue = ( Number(attValue) + Number(priceInclValue) ) * ((taxRate / 100) + 1);
	totalInclValue = ( Number(attValue) + Number(totalInclValue) ) * ((taxRate / 100) + 1) * qty;
	totalExclValue = ( Number(attValue) + Number(totalExclValue) ) * qty;
	taxValue = taxRate * finalPriceValue / 100 * qty;

	}

	if (action == 'final_price') {

	var priceValue = document.getElementById("update_products[" + pid + "][final_price]").value;
	var priceInclValue = document.getElementById("update_products[" + pid + "][final_price]").value;
	var totalInclValue = document.getElementById("update_products[" + pid + "][final_price]").value;
	var totalExclValue = document.getElementById("update_products[" + pid + "][final_price]").value;

	priceValue = Number(priceValue) - Number(attValue);
	priceInclValue = priceInclValue * ((taxRate / 100) + 1);
	totalInclValue = totalInclValue * ((taxRate / 100) + 1) * qty;
	totalExclValue = totalExclValue * qty;

	} //end if ((action == 'qty') || (action == 'tax') || (action == 'final_price'))

	if (action == 'price_incl') {

	var priceValue = document.getElementById("update_products[" + pid + "][price_incl]").value;
	var finalPriceValue = document.getElementById("update_products[" + pid + "][price_incl]").value;
	var totalInclValue = document.getElementById("update_products[" + pid + "][price_incl]").value;
	var totalExclValue = document.getElementById("update_products[" + pid + "][price_incl]").value;

	priceValue = Number(finalPriceValue / ((taxRate / 100) + 1)) - Number(attValue);
	finalPriceValue = finalPriceValue / ((taxRate / 100) + 1);
	totalInclValue = totalInclValue * qty;
	totalExclValue = totalExclValue * qty / ((taxRate / 100) + 1);

	} //end of if (action == 'price_incl')

	if (action == 'total_excl') {

	var priceValue = document.getElementById("update_products[" + pid + "][total_excl]").value;
	var finalPriceValue = document.getElementById("update_products[" + pid + "][total_excl]").value;
	var priceInclValue = document.getElementById("update_products[" + pid + "][total_excl]").value;
	var totalInclValue = document.getElementById("update_products[" + pid + "][total_excl]").value;

	priceValue = ( Number (finalPriceValue / qty) ) - Number (attValue);
	finalPriceValue = finalPriceValue / qty;
	priceInclValue = priceInclValue * ((taxRate / 100) + 1) / qty;
	totalInclValue = totalInclValue * ((taxRate / 100) + 1);

	} //end of if (action == 'total_excl')

	if (action == 'total_incl') {

	var priceValue = document.getElementById("update_products[" + pid + "][total_incl]").value;
	var finalPriceValue = document.getElementById("update_products[" + pid + "][total_incl]").value;
	var priceInclValue = document.getElementById("update_products[" + pid + "][total_incl]").value;
	var totalExclValue = document.getElementById("update_products[" + pid + "][total_incl]").value;

	priceValue = Number (finalPriceValue / ((taxRate / 100) + 1) / qty) - Number(attValue)
	finalPriceValue = finalPriceValue / ((taxRate / 100) + 1) / qty;
	priceInclValue = priceInclValue / qty;
	totalExclValue = totalExclValue / ((taxRate / 100) + 1);

	} //end of if (action == 'total_incl')


	if ((action != 'qty') && (action != 'tax') && (action != 'att_price') && (action != 'price')) {
	document.getElementById("update_products[" + pid + "][price]").value = doFormat(priceValue, 4);
	}

	if (action != 'final_price') {
	document.getElementById("update_products[" + pid + "][final_price]").value = doFormat(finalPriceValue, 4);
	}

	if ((action != 'qty') && (action != 'price_incl')) {
	document.getElementById("update_products[" + pid + "][price_incl]").value = doFormat(priceInclValue, 4);
	}

	if ((action != 'tax') && (action != 'total_excl')) {
	document.getElementById("update_products[" + pid + "][total_excl]").value = doFormat(totalExclValue, 4);
	}

	if (action != 'total_incl') {
	document.getElementById("update_products[" + pid + "][total_incl]").value = doFormat(totalInclValue, 4);
	}

	} //end function updatePrices(action, pid)

   function getAttributesPrices(pid){ //get any attributes prices that may exist
    var sum =0;
    var el=document.getElementsByTagName('input');//all the input elements
      for(var i=0;i<el.length;i++){
       if(el[i].id.indexOf(pid)>-1){
        var aid=el[i].id.replace(pid,'').replace('p', '').replace('a', '');//extract the attribute id
        var p=el[i].id.replace(pid,'').replace(/\d/g,'');
          if((p=='pa') && (document.getElementById('p' + pid + '_' + aid + '_prefix')) && (document.getElementById('p' + pid + '_' + aid + '_prefix').value) == '-') {
           sum-=Number(el[i].value);
          }
          if((p=='pa') && (document.getElementById('p' + pid + '_' + aid + '_prefix')) && (document.getElementById('p' + pid + '_' + aid + '_prefix').value) == '+') {
           sum+=Number(el[i].value);
          }
         }
        }
      return sum
     } //end function getAttributePrices(pid)

	function doRound(x, places) {  //we only have so much space
     return Math.round(x * Math.pow(10, places)) / Math.pow(10, places);
    }

    function doFormat(x, places) { //keeps all calculated values the same length
    var a = doRound(x, places);
    var s = a.toString();

    var decimalIndex = s.indexOf(".");

	  if (places > 0 && decimalIndex < 0) {
         decimalIndex = s.length;
         s += '.';
       }

	  while (decimalIndex + places + 1 > s.length) {
         s += '0';
      }

    return s;

    } // end function doFormat

//--></script>

<?php ?>