// selects option in select box by its value
function SelectOptionInList (lstSelectList, intID)
{
  var intIndex = 0;
  // Loop through all the options
  for( intIndex = 0; intIndex < lstSelectList.options.length; intIndex++ )
  {
    // Is this the ID we are looking for?
    if( lstSelectList.options[intIndex].value == intID ) 
    {
      // Select it
      lstSelectList.selectedIndex = intIndex;
      // Yes, so stop searching
      break;
    }
  }
}

// PHP number_format analog
function number_format (number, decimals, dec_point, thousands_sep)
{
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "")
  {
	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}

// shows or hides div
function toggle_div(div)
{
  element = document.getElementById(div);
  if(element.style.display == 'none')
  {
    element.style.display = '';
  }
  else
  {
    element.style.display = 'none';
  }
}

function toggleVer2(target, iNo, pics_ar) {
  var obj;
  var pic;

  obj = document.getElementById(target + String(iNo));

  if (obj.style.display == '') {
    pic = pics_ar[0];
  } 
  else {
    pic = pics_ar[1];
  }

  obj.style.display = ((obj.style.display == '') ? 'none' : '');

  obj = document.getElementById('info_img_' + String(iNo));
  if (obj != null) {
    obj.src = pic;
  }
}

function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber;
}

function clearForm(id) { 
  $$('#' + id + ' input[type!=submit]', '#' + id + ' select').invoke('clear');
  $$('#' + id + ' input[type=checkbox]').each(function(s) {
    s.checked = '';
  });
}

function upload(iframe_name, width, height, url) { // Function for AJAX-like file upload. Hides an old iframe and shows a new empty one
                                                   // Parameters are Iframe ID to hide, its width and height and Iframe URL
  // hide old iframe
  var par = window.parent.document.getElementById(iframe_name);
  var num = par.getElementsByTagName('iframe').length - 1;
  var iframe = par.getElementsByTagName('iframe')[num];
  iframe.className = 'hidden';

  // create new iframe
  var new_iframe = window.parent.document.createElement('iframe');
  new_iframe.width = width;
  new_iframe.height = height;
  new_iframe.src = url;
  new_iframe.frameBorder = '0';
  window.parent.document.getElementById(iframe_name).appendChild(new_iframe);

  document.iform.submit();	
}

//////////////////////////
// Cabinfield functions //
//////////////////////////

// Custom function for automatic price calculation in admin after filling Cost and Percent
function calculatePrice(cost_id,percent_id,price_id) {
  var price;
  cost_box = document.getElementById(cost_id);
  percent_box = document.getElementById(percent_id);
  price_box = document.getElementById(price_id);
  if(price_box && percent_box && cost_box && cost_box.value && percent_box.value) {
    cost = parseFloat(cost_box.value);
    percent = parseFloat(percent_box.value);
    price = Math.round(cost + cost*percent/100);
    price_box.value = price;
  }
}

// Custom function for populating categories (2nd level cats) selectbox after selecting a section (1st level cat). Used in admin.
function populateCatList(selectbox,section_id) {
  var cat_array;
  if(arguments.length == 2) {
    cat_array = categories;
    parent_entity = 'section';
  } else if(arguments.length == 3) {
    cat_array = arguments[2];
    parent_entity = 'category';
  }
  for(i=selectbox.options.length-1;i>=0;i--) {
    selectbox.remove(i);
  }
  if(cat_array[section_id]) {
    var optn = document.createElement("OPTION");
    optn.text = '';
    optn.value = '';
    selectbox.options.add(optn);

    for(cat_id in cat_array[section_id]) {
      if(!isNaN(cat_id)) { // skipping prototype stuff
        var optn = document.createElement("OPTION");
        optn.text = cat_array[section_id][cat_id];
        optn.value = cat_id;
        selectbox.options.add(optn);
      }
    }
    var options = $A(selectbox.options).sortBy(function(o) { return o.innerHTML });
    selectbox.innerHTML = "";
    options.each(function(o) { selectbox.insert(o); } );
    selectbox.options.selectedIndex = 0;
  } else {
    var optn = document.createElement("OPTION");
//    optn.text = 'Please select ' + parent_entity;
    optn.text = '';
    optn.value = '';
    selectbox.options.add(optn);
  }
}

