/******************************************************************
 * This page is a compilation of javascript functions that I have 
 * found useful in developing my web applications.
 * 
 * These functions have usually been taken from other sources
 * and compiled into one handy file.
 *
 * Functions have come from other sources unless otherwise
 * specified.
 *
 * File created by Joe Chrzanowski
 *****************************************************************/

// Creates an ajax request handler
function make_req() {
		var req = false;
      if (window.XMLHttpRequest) {
        try {
          req = new XMLHttpRequest();
        } catch (e) {
          req = false;
        }
      } else if (window.ActiveXObject) {
        try {
          req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
          try {
            req = new ActiveXObject("Microsoft.XMLHTTP");
          } catch (e) {
            req = false;
          }
        }
      }
	  return req;
}

// Perform an ajax request. 
// Pass a url and it returns the contents of the page.
function request(url) {
	req = make_req();
    if (req) {
        req.open('GET', url, false);
        req.send(null);
        response = req.responseText;
        if (response.length > 0)
        	return response;
        else
        	return "No Results Found";
    } 
	else {
      	return "No Results Found";
    }
}

// Alias for document.getElementById('elementid')
// I think this came from prototype.js, but i'm not sure
function $() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}

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

// Put in the quotes while a user is typing
// Also prevents double slashing (\\" instead of \")

// Its better to just do it in PHP with the function
// escape_quotes() - found in /common/functions.php

// (c) Joe Chrzanowski
function sqlsafe(string) {
    outstr = "";
    
    for (i = 0; i < string.length; i++) {
        thischar = string.charAt(i);
        if (thischar == "'" || thischar == '"') {
            if (i >= 1) {
                if (string.charAt(i-1) != "\\")
                    outstr += "\\";
                outstr += thischar;
            }
            else {
                outstr += "\\";
                outstr += thischar;
            }
        }
        else {
            outstr += thischar
        }
    }
    outstr = outstr.replace(/\\/,"\\");
    return outstr;
}

// Uses the above function, but allows for using the textarea as a dummy, while the actual
// data is stored in a hidden field.  This prevents confusion from the \"
// (c) Joe Chrzanowski
function sqlsafe_hidden(string, field) {
    safestring = sqlsafe(string);
    field.value = safestring;
}

// Returns a string with only numbers.
// Optionally allows a decimal.
// (c) Joe Chrzanowski
function numbers_only(string, allowdecimal) {
    validchars = "0123456789";
    outstr = "";
    if (allowdecimal)
        validchars += ".";
        
    for (i = 0; i < string.length; i++) {
        if (validchars.indexOf(string.charAt(i)) >= 0)
            outstr += string.charAt(i);
    }
    return outstr;
}

// Returns a string with only numbers and letters.
// (c) Joe Chrzanowski
function alpha_numeric(string) {
    validchars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    outstr = "";

    for (i = 0; i < string.length; i++) {
        if (validchars.indexOf(string.charAt(i)) >= 0)
            outstr += string.charAt(i);
    }
    return outstr;
}

// Sets a field to a given background color.
// Useful for form validation.
// (c) Joe Chrzanowski
function turn_color(fieldname, color) {
    $(fieldname).style.backgroundColor = color;
}

function setCharAt(str, index, ch) {
    var result = str.slice(0, index - 1) + ch + str.slice(index);
    return result;
}