// Conversion functions (taken directly from the old foodcount.com

function ConvertWeight(Weight, UnitOfMeasure) {
	
	var ConvertedWeight;
	// FIXME: Hard coded reference to "kgs" value.
	if ( UnitOfMeasure == "kgs" ) {
		ConvertedWeight = ConvertPoundsToKilograms(Weight);
	} else {
		ConvertedWeight = ConvertKilogramsToPounds(Weight);
	}
	
	return ConvertedWeight;
	
}

function ConvertKilogramsToPounds(Kilograms) {
	
	var Pounds = 0;
	
	if ( Kilograms > 0 ) {
		Pounds = Kilograms * 2.2046;
	}
	
	return Math.round(Pounds);
	
}


function ConvertPoundsToKilograms(Pounds) {
	
	var Kilograms = 0;
	
	if ( Pounds > 0 ) {
		Kilograms = Pounds * 0.4536;
	}
	
	return Kilograms;
	
}

// Takes n arguments which are either the IDs or object references of
// elements whose style display mode will be toggled.

function getObject(param) {
  
  if ( typeof(param) == "object" ) {
    
    return param;
    
  } else {
    
    return document.getElementById(param);
    
  }
  
}

function toggleDisplay() {
  
  for ( var i = 0; i < arguments.length; i++ ) {
    
    var obj = getObject(arguments[i]);
    
    var objStyle = getActiveStyle(obj);
    
    // Since all elements are hidden with a value of "none", whereas each
    // element may have its own display value, we'll check for "none".
    
    if ( objStyle.display == "none" ){
      
      show(obj);
      
    } else {
      
      hide(obj);
      
    }
    
  }
  
}

function show(obj) {
  
  obj.style.display = getDefaultDisplay(obj);
  
  obj.style.visibility = "visible";
  
}


function hide(obj) {
  
  obj.style.display = "none";

  obj.style.visibility = "hidden";
  
}

// Determines the default display value for the object's HTML element type. For
// instance, a DIV would have a default display of "block". This method should
// ensure that we use the correct display value for each browser.

function getDefaultDisplay(obj) {
  
  var value = "";
  
  // Create a new element with a tag name that corresponds to the object.
  
  var objCopy = document.createElement(obj.tagName);
  
  // Unless we add the element to the document, we won't know the actual
  // display value. IE will return an empty string; Firefox, "block".
  
  // By setting the visibility to hidden, we ensure that the user will not
  // see any such changes.
  
  objCopy.visibility = "hidden";
  
  document.body.appendChild(objCopy);
  
  value = getActiveStyle(objCopy).display;
  
  document.body.removeChild(objCopy);
  
  return value;
  
}

function getActiveStyle(obj) {
  
  // Ensure that we have an object and not just the ID of an HTML element.
  
  obj = getObject(obj);
  
  if ( obj.currentStyle ) {
    
    // Internet Explorer
    
    return obj.currentStyle;
    
  } else if ( document.defaultView && document.defaultView.getComputedStyle ) {
    
    // Mozilla/FireFox
    
    return document.defaultView.getComputedStyle(obj, null);
    
  } else {
    
    // Fall back to non-computed style.
    
    return obj.style;
    
  }
  
}


