///////////////////////////////////////////////////////
// Javascript for basic actions in website
///////////////////////////////////////////////////////

// function to set the table being acted upon
function dbupon($upon, $action, $detail, $confirm)
{
  // set the upon
  if ($('upon')) { $('upon').value = $upon; }
  // run the action
  return dbaction($action, $detail, $confirm);
}

// function to set the action and detail being performed on the table
function dbaction($action, $detail, $confirm)
{
  // make sure that we have a default detail
  $detail = ($detail == null) ? 0 : $detail;
  // make sure that we have a default confirm
  $confirm = ($confirm == null) ? 0 : $confirm;
  // set the action
  if ($('action')) { $('action').value = $action; }
  // set the detail
  if ($('detail')) { $('detail').value = $detail; }
  // if the action is not delete, or the user confirms a delete, or a confirmation is requested
  if (($action != 'delete' && $confirm == 0) || ($confirm && confirm("Are you sure you want to do this?\n\n(if in doubt, click Cancel)")) || ($action == 'delete' && confirm("Do you want to delete?\n\n(if in doubt, click Cancel)")))
  {
    // check if there is an onsubmit section
    if ($('dbform').onsubmit())
    {
      // do nothing
    }
    // post no matter what
    $('dbform').submit();
  }
  // kill the effect of the Submit button
  return false;
}

// function to disable buttons (preventing multiple clicks)
function submitonce(myform)
{
  // loop through all the elements
  for (i = 0; i < myform.length; i++)
  {
    // get each element
    var tempobj = myform.elements[i];
    // is it a submit or reset or plain button
    if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset" || tempobj.type.toLowerCase() == "button" || tempobj.type.toLowerCase() == "file")
    {
      // disable it
      tempobj.disabled = true;
    }
  }
}

// function to load an xml string into an xml dom object
function loadxmlstring(txt)
{
  // is it non-IE
  if (window.DOMParser)
  {
    // create the XML parser object
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(txt,"text/xml");
  }
  else // Internet Explorer
  {
    // create the XML active-x parser object
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = "false";
    xmlDoc.loadXML(txt);
  }
  // return the result
  return xmlDoc;
}

// serialize a-la PHP
function serialize()
{
  // get the type of the passed argument
  var t = typeof(arguments[0]);
  // local variable
  var hold = '';
  // if its object
  if (t == 'object')
  {
    // is there a constructor
    if (arguments[0].constructor)
    {
      var c = arguments[0].constructor.toString();
      var m = c.match(/(\w+)\(/);
      t = m[1].toLowerCase();
    }
  }
  // what is the type
  switch (t)
  {
    case "boolean":
      // serialize the boolean
      hold = "b:" + arguments[0] + ";";
      break;
    case "number":
      // serialize the number (as integer or decimal)
      hold = ((Math.round(arguments[0]) == arguments[0]) ? 'i' : 'd') + ":" + arguments[0] + ";";
      break;
    case "string":
        // serialize the string
        hold = "s:" + arguments[0].length + ':"' + arguments[0] + '";';
      break;
    case "array":
      var count = 0;
      var key;
      for (key in arguments[0])
      {
        // is the key a number (force to number)
        if (!isNaN(key)) { key *= 1; }
        // serialize and add together
        hold += arguments.callee(key) + arguments.callee(arguments[0][key]);
        // add to the count of array elements
        count++;
      }
      // make it an array
      hold = "a:" + count + ":{" + hold + "}";
      break;
    default:
      // unsupported
      alert("Unsupported type for serialization!\n\n(Only booleans, numbers, strings, and arrays are supported.)")
      break;
  }
  // return the type
  return hold;
}

// unserialize a-la PHP
function unserialize(value)
{
  // sub-function to get a length of the string (to the end of this part)
  var gc = function() {
    // hold the passed value
    var value = arguments[0];
    // local variable
    var hold;
    // whats the first character
    switch (value.charAt(0))
    {
      case 's':
        // split the string on colons
        var guts = value.split(/:/);
        // return the value
        hold = +guts[1] + 6 + guts[1].length;
        break;
      case 'i':
      case 'd':
      case 'b':
        // return the first semicolon location
        hold = value.indexOf(';') + 1;
        // alert("Size: " + value + '/' + hold);
        break;
      case 'a':
        // loop through the string
        var tmp = 1;
        for (x = value.indexOf('{') + 1; x < value.length; x++)
        {
          // is this {
          if (value.charAt(x) == '{') { tmp++; }
          // is this }
          if (value.charAt(x) == '}') { tmp--; }
          // if the tmp = 1, return this position
          if (tmp == 0) { return x + 1; }
        }
        break;
    }
    // return the result
    return hold;
  }

  // variables
  var ret;
  var temp;
  var tmp;
  var num;
  // do stuff depending on what it is
  switch (value.charAt(0))
  {
    case "s":
      // chunk it
      temp = value.split(/:/, 2);
      // get the string to return
      ret = value.substr(value.indexOf(':', 2) + 2, temp[1]);
      break;
    case "i":
    case "d":
    case "b":
      // chunk it
      temp = value.split(/:|;/, 2);
      //alert("PP: " + value);
      ret = temp[1];
      break;
    case "a":
      // the return is an array
      ret = new Array();
      // grab out this chunk
      temp = value.substring(value.indexOf('{') + 1, gc(value));
      // loop while we have temp
      while (temp.length)
      {
        // get the index
        num = gc(temp);
        tmp = arguments.callee(temp.substring(0, num));
        // remove it
        temp = temp.substring(num);
        // get the index
        num = gc(temp);
        // add to the array
        ret[tmp] = arguments.callee(temp.substring(0, num));
        // remove it
        temp = temp.substring(num);
        // if the next one is a }
        if (temp.charAt(0) == '}') { temp = temp.substring(1); }
        //alert("Temp: " + temp);
      }
      break;
    default:
      // unsupported
      alert("Unsupported type '" + value.charAt(0) + "' for unserialization!\n\n(Only numbers, strings, and arrays are supported.)\n\nValue: " + value);
      ret = '';
      break;
  }
  // return the result
  return ret;
}

// check if a string is in an array
function in_array(value, arr)
{
  // make sure that value and arr are set
  if (!value)
  {
    // return false
    return false;
  }
  if (arr.constructor.toString().indexOf('Array') == -1)
  {
    // not an array - return false
    return false;
  }
  // okay, we have an array and value - convert the array to an object
  var o = {};
  for (var x = 0; x < arr.length; x++)
  {
    // add to the object
    o[arr[x]] = arr[x];
  }
  // now thats done, check if an item is in it and return the result
  return (value in o);
}


