function generateOptions(json_arr, first_key, first_value)
{
  var options = '';
    if (first_value) {
        first_key = typeof(first_key) != 'undefined' ? first_key : '';
        options += '<option value="' + first_key + '">' + first_value + '</option>';
    }
  $.each(json_arr, function(i, n) {
    options += '<option value="' + i + '">' + n + '</option>';
  });

  return options;
}

function trim(stringToTrim)
{
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function go_there(link, message)
{
    if (!message) {
      message = "Are you sure?";
    }
    var where_to = confirm(message);
    if (where_to == true) {
        window.location = link;
    }
}

function sureSubmit(el, frm, action, save_selection)
{
  var where_to = confirm("Are you sure?");
    if (where_to == true) {
         if (action) {
           frm.action.value = action;
         }
        frm.submit();
    } else {
      if (!save_selection) {
        el.selectedIndex = 0;
      }
      return false;
    }
}

function sureSubmitFn(fn)
{
  var where_to = confirm("Are you sure?");
  if (where_to == true) {
    fn.call();
  } else {
    return false;
  }
}

function confirmAction(msg)
{
  if (!msg) {
    msg = "Are you sure?";
  };
  var where_to = confirm(msg);
  if (where_to == true) {
    return true;
  } else {
    return false;
  }
}

function goImgWin(myImage, myWidth, myHeight, origLeft, origTop)
{
    myHeight += 24;
    myWidth += 24;

    TheImgWin = window.open(myImage,'image','height=' +
    myHeight + ',width=' + myWidth +
    ',toolbar=no,directories=no,status=no,' +
    'menubar=no,scrollbars=no,resizable=no');

    TheImgWin.resizeTo(myWidth+2, myHeight + 30);
    TheImgWin.moveTo(origLeft, origTop);
    TheImgWin.focus();
}

function in_array(needle, haystack)
{
    var i = haystack.length;
    if(i > 0) {
        do {
            if (haystack[i] === needle)
                return true;
        } while (i--);
    }

    return false;
}

function empty( mixed_var ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Philippe Baumann
    // *     example 1: empty(null);
    // *     returns 1: true

    return ( mixed_var === "" || mixed_var === 0   || mixed_var === "0" || mixed_var === null  || mixed_var === false  ||  ( is_array(mixed_var) && mixed_var.length === 0 ) );
}

function is_array( mixed_var ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Legaev Andrey
    // +   bugfixed by: Cord
    // *     example 1: is_array(['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: is_array('Kevin van Zonneveld');
    // *     returns 2: false

    return ( mixed_var instanceof Array );
}

// {{{ stristr
function stristr( haystack, needle, bool )
{
    // Case-insensitive strstr()
    //
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_stristr/
    // +       version: 804.1712
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: stristr('Kevin van Zonneveld', 'Van');
    // *     returns 1: 'van Zonneveld'
    // *     example 2: stristr('Kevin van Zonneveld', 'VAN', true);
    // *     returns 2: 'Kevin '

    var pos = 0;

    pos = haystack.toLowerCase().indexOf( needle.toLowerCase() );
    if( pos == -1 ){
        return false;
    } else{
        if( bool ){
            return haystack.substr( 0, pos );
        } else{
            return haystack.slice( pos );
        }
    }
}// }}}

function operateCheckboxes(primary, aclass)
{
  if (aclass) {
    var selector = "." + aclass;
  } else {
    var selector = ":checkbox";
  }

  if (primary.checked) {
    $(primary).parents("form:first").find(selector).attr("checked", true);
  } else {
    $(primary).parents("form:first").find(selector).attr("checked", false);
  }
}

function queryParams()
{
  var qsParm = new Object();
  var query = window.location.search.substring(1);
  var parms = query.split('&');
  for (var i=0; i < parms.length; i++) {
    var pos = parms[i].indexOf('=');
    if (pos > 0) {
      var key = parms[i].substring(0, pos);
      var val = parms[i].substring(pos + 1);
      qsParm[key] = val;
    }
  }

  return qsParm;
}

function changePageLimit(page_limit)
{
  page_limit = parseInt(page_limit);

  if (page_limit > 0 && page_limit <= 100) {
    if (window.location.search) {
      query = '?';

      query_params = queryParams();
      for (var param in query_params) {
        if (param != "page_limit") {
          query +=  param + '=' + query_params[param] + '&';
        }
      }

      query += 'page_limit=' + page_limit;
    } else {
      query = '?page_limit=' + page_limit;
    };
  }

  window.location = window.location.pathname + query;
}

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

function insertAtCursor(myField, myValue) {
  var doc = myField.ownerDocument;
  //IE support
  if (doc.selection) {
    myField.focus();
    sel = doc.selection.createRange();
    sel.text = myValue;
  }
  //FF, hopefully others
  else if (myField.selectionStart || myField.selectionStart == '0') {
    var startPos = myField.selectionStart;
    var endPos = myField.selectionEnd;
    myField.value = myField.value.substring(0, startPos) + 
                    myValue + myField.value.substring(endPos, myField.value.length);
  } 
  // fallback to appending it to the field
  else {
    myField.value += myValue;
  }
}
