function showHint(el)
{
  var el = el;
  $j(el).siblings("div").css("display", "block");
}

function hideHint(el)
{
  var el = el;
  $j(el).siblings("div").css("display", "none");
}


// Set Browsers Bookmarking / Favourite
function addToFavorites(urlAddress, pageName)
{
    if (window.sidebar)
    { // Mozilla Firefox
      window.sidebar.addPanel(pageName, urlAddress,"");
    }
    else if (window.external)
    { // IE
      window.external.AddFavorite(urlAddress, pageName);
    }
    else if(window.opera && window.print)
    { // Opera
      return true;
    }
}

// Set Browsers Default Homepage
function setDefaultHomepage(urlAddress)
{
  if (window.sidebar)
  { // Mozilla Firefox
    try
    {
      netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    }
    catch(e)
    {
      alert("Apologies but your browser (firefox) is preventing this action. Thank you.");
      return true;
    }
    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
    prefs.setCharPref('browser.startup.homepage',urlAddress);
    return true;
  }
  else if (window.external)
  { // IE
    document.body.style.behavior='url(#default#homepage)';
    document.body.setHomePage(urlAddress);
  }
  else if(window.opera && window.print)
  { // Opera
    return true;
  }
}



function sfOpenWindow(url,h,w)
{
  var newwindow;
  newwindow=window.open(url,'popup',"height="+ h + ",width="+ w  + ",location=1,status=1,scrollbars=1,resizable=1"  );
  if (window.focus) {
    newwindow.focus()
    return false ;
  }
}

// arrayHasValues:
// Replicates the php function for looking up if a value exists in an array
function arrayHasValue(neddle, haystack)
{
  for (var i = 0; i < haystack.length; i++)
  {
    if (haystack[i] == neddle)
    {
      return true;
    }
  }
  return false;
}


// getRadioGroupValue:
// Returns the selected value of a group of radio buttons
function getRadioGroupValue(element_id, form_id)
{

  var radiogroup = document.forms[form_id].elements[element_id];

  for(var i = 0; i < radiogroup.length; i++)
  {
    if( radiogroup[i].checked == true )
    {
      return radiogroup[i].value;
    }
  }
  return false;
}

// numberOnly:
// a onkeypress event that checks that only numbers are entered into an input
function numberOnly(evt)
{
  evt = (evt) ? evt : event;
  var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));

   /*
  var allowKeys=new Array();
  allowKeys[0]=37;  // (left  arrow key)
  allowKeys[1]=38;  // (up arrow key)
  allowKeys[2]=39;  // (right arrow key)
  allowKeys[3]=40;  // (down arrow key)

  allowKeys[4]=8;   // (backspace key)
  allowKeys[5]=9;   // (tab key)
  allowKeys[6]=35;  // (end key)
  allowKeys[7]=36;  // (home key)
  allowKeys[8]=45;  // (insert key)
  allowKeys[9]=46;  // (delete key)
  allowKeys[10]=144; // (numlock)

  allowKeys[11]=48;  // (0 numeric key)
  allowKeys[12]=49;  // (1 numeric key)
  allowKeys[13]=50;  // (2 numeric key)
  allowKeys[14]=51;  // (3 numeric key)
  allowKeys[15]=52;  // (4 numeric key)
  allowKeys[16]=53;  // (5 numeric key)
  allowKeys[17]=54;  // (6 numeric key)
  allowKeys[18]=55;  // (7 numeric key)
  allowKeys[19]=56;  // (8 numeric key)
  allowKeys[20]=57;  // (9 numeric key)

  allowKeys[21]=96;  // (0 numpad numeric key)
  allowKeys[22]=97;  // (1 numpad numeric key)
  allowKeys[23]=98;  // (2 numpad numeric key)
  allowKeys[24]=99;  // (3 numpad numeric key)
  allowKeys[25]=100; // (4 numpad numeric key)
  allowKeys[26]=101; // (5 numpad numeric key)
  allowKeys[27]=102; // (6 numpad numeric key)
  allowKeys[28]=103; // (7 numpad numeric key)
  allowKeys[29]=104; // (8 numpad numeric key)
  allowKeys[30]=105; // (9 numpad numeric key)

  allowKeys[31]=190;  // (period) optional for decimal place
  allowKeys[32]=110;  // (decimal point) optional for decimal place
  allowKeys[33]=188;  // (comma) optional for decimal place
  */

  if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 37 ||  charCode > 40) )
  {
    alert("Enter numerals only in this field.");
    return false;
  }
  return true;

}

// numberOnly:
// a onkeypress event that checks that only numbers and a decimal are entered into an input
// Note: to be used in conjunction with isDecimalRate
function decimalOnly(evt)
{
  evt = (evt) ? evt : event;
  var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));

  if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 37 ||  charCode > 40) && charCode != 46)
  {
    alert("Decimal numerals only in this field.");
    return false;
  }
  return true;
}

function floatOnly(evt, element_id)
{
  var str = $j('#' + element_id).val();

  evt = (evt) ? evt : event;
  var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));

  if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 37 ||  charCode > 40) && charCode != 46 && charCode != 45 )
  {
    alert("Decimal numerals only in this field.");
    return false;
  }
  else if(!isDecimalRate(str, true) || (str.length >= 1 && charCode == 45))
  {
    alert("Decimal numerals only in this field.");
    return false;
  }
  return true;
}








// isNumber:
// validates that the entry is a positive or negative number
function isNumber(str, allow_negative)
{
  var allow_negative = (allow_negative == null || allow_negative == "") ? false : allow_negative ;

  if (allow_negative == true)
  {
    var re = /[-]?\d*$/;
  }
  else
  {
    var re = /\d*$/;
  }

  str = str.toString( );
  if (!str.match(re))
  {
    return false;
  }
  return true;
}


// isElmNumber:
// validates that the element has a positive number value
function isElmNumber(element_id)
{
  var str = $j('#' + element_id).val();
  return isNumber(str, false)
}


function convertToInteger(number_value)
{
  number_value = parseInt(number_value);

  if(isNaN(number_value))
  {
    return false;
  }
  else
  {
    return number_value;
  }
}


// convertElmToInteger:
// forces the element to be a positive integer number
function convertElmToInteger(element_id, default_value, min_value, max_value)
{
  var default_value = (default_value === null || default_value === "" || isNaN(default_value)) ? 0 : convertToInteger(default_value) ;
  var min_value = (min_value === null || min_value === "" || isNaN(min_value)) ? default_value : convertToInteger(min_value) ;
  var max_value = (max_value === null || max_value === "" || isNaN(max_value)) ? false : convertToInteger(max_value) ;

  var number = convertToInteger($j('#' + element_id).val());

  if(number === false)
  {
    return $j('#' + element_id).val(default_value);
  }
  else if (min_value !== false && number < min_value)
  {
    return $j('#' + element_id).val(min_value);
  }
  else if(max_value !== false && number > max_value)
  {
    return $j('#' + element_id).val(max_value);
  }
  else
  {
    return $j('#' + element_id).val(number);
  }
}

// isDecimalRate:
// validates that the string is a positive or negative decimal number
function isDecimalRate(str, allow_negative)
{
  if (allow_negative == true)
  {
    var re = /^[-]?\d*\.?\d*$/;
  }
  else
  {
    var re = /^\d*\.?\d*$/;
  }

  str = str.toString( );
  if (!str.match(re))
  {
    return false;
  }
  return true;
}

// isElmDecimalRate:
// validates that the element has a positive decimal number value

function isElmDecimalRate(element_id)
{
  var str = $j('#' + element_id).val();
  return result = isDecimalRate(str, false)
}

// convertToFloat:
// validates that the value has a positive decimal number value
// and forces the value to be a 2 digit decimal
function convertToFloat(number_value)
{
  number_value = parseFloat(number_value);

  if(isNaN(number_value))
  {
    return false;
  }
  else
  {
    return number_value ;
  }
}

// convertElmToRate:
// validates that the element has a positive decimal number value
// and forces the element value to be a 2 digit decimal
function convertElmToRate(element_id, default_value, min_value, max_value)
{
  var default_value = (default_value === null || default_value === "" || isNaN(default_value)) ? 0 : convertToFloat(default_value) ;
  var min_value = (min_value === null || min_value === "" || isNaN(min_value)) ? default_value : convertToFloat(min_value) ;
  var max_value = (max_value === null || max_value === "" || isNaN(max_value)) ? false : convertToFloat(max_value) ;

  var decimal = convertToFloat($j('#' + element_id).val());


  //alert(default_value + " default_value ");
  //alert(min_value + " min_value ");
  //alert(max_value + " max_value ");
  //alert(decimal + " decimal ");


  if(decimal === false)
  {
    return $j('#' + element_id).val(default_value.toFixed(2)) ;
  }
  else if(min_value !== false && decimal < min_value)
  {
    return $j('#' + element_id).val(min_value.toFixed(2)) ;
  }
  else if(max_value !== false && decimal > max_value)
  {
    return $j('#' + element_id).val(max_value.toFixed(2)) ;
  }
  else
  {
    return $j('#' + element_id).val(decimal.toFixed(2)) ;
  }

}

// isNotEmpty
// validates that a string is not empty
function isNotEmpty(str)
{
  var re = /.+/;
  if(!str.match(re))
  {
    return false;
  }
  return true;
}

// isElmNotEmpty
// validates that an element has a value that is not empty
function isElmNotEmpty(element_id)
{
  var str = $j('#' + element_id).val();
  return isNotEmpty(str);
}



// isLength
// validates that a string is a given length
function isLength(str, length)
{
  if (str.length != length)
  {
    return false;
  }
  return true;
}

// isElmLength
// validates that an element has a value of a given length
function isElmLength(element_id, length)
{
  var str = $j('#' + element_id).val();
  return isLength(str, length);
}

function showNowProcessing()
{
  tb_show(null, 'index.php?TB_inline=true&height=150&width=340&inlineId=modalNowProcessing&modal=true', true);
  return true;
}
