// methods in this file are to escape problems of
// presence of xml designated characters '<', '>', and '&' in
// HTML file's <script> code segments.  All occurences of these
// xml characters in script must be substituted with the following
// method calls.  Stand-alone JavaScript files, however, does not
// need this substitution.

function MATH_Lt(a, b)
{
  return (a < b);
}

function MATH_Gt(a, b)
{
  return (a > b);
}

function MATH_LtEq(a, b)
{
  return (a <= b);
}

function MATH_GtEq(a, b)
{
  return (a >= b);
}

function MATH_Eq(a, b)
{
  return (a == b);
}

function MATH_And(a, b)
{
  return (a && b);
}

function MATH_Or(a, b)
{
  return (a || b);
}

function MATH_BitAnd(a, b)
{
  return (a & b);
}

function MATH_BitOr(a, b)
{
  return (a | b);
}

function MATH_IsNumber(value)
{
  var len = value.length;
  var dec_point = false;
  
  for(i = 0; i < len; i++)
  {
    switch(value.charAt(i))
	{
	  case '0':
	  case '1':
	  case '2':
	  case '3':
	  case '4':
	  case '5':
	  case '6':
	  case '7':
	  case '8':
	  case '9':
	    break;
	  case '+':	// allows + only at the beginning
	  case '-':	// allows - only at the beginning
	    if(i > 0)
		  return false;
		break;
	  case '.':	// allows decimal point only once
	    if(dec_point)
		  return false;
		dec_point = true;
		break;		
	  default:	// all others mean this is not a number
	    return false;
	}
  }
  
  return true;
}

function MATH_SortIgnoreQuotes(js_array)
{
  js_array.sort(
    function(ain, bin)
	{
	  // remove double-quotes
	  var a = ain.replace(/\"/g, "");
	  var b = bin.replace(/\"/g, "");
	  
	  if(a.length == 0)
	    return -1;
	  if(b.length == 0)
	    return 1;
		
	  // if both are numbers, compare values	
	  var an = MATH_IsNumber(a);
	  var bn = MATH_IsNumber(b);
	  
	  if(an && bn)
	    return a - b;	  
	  if(an && !bn)
	    return -1;
	  if(!an && bn)
	    return 1;
		
	  // compare strings case insensitively	
	  var au = a.toUpperCase();
	  var bu = b.toUpperCase();
	  
	  if(au < bu)
	    return -1;
	  else if(au > bu)
	    return 1;
	  else if(a < b)
	    return -1;
	  else if(a > b)
	    return 1;
	  return 0;
	});
}
