<!---
/*
    JavaScript Functions
    
*/


function isArray(obj) {
    return toString.call(obj) === "[object Array]";
 }



// Grab the specified Element(s)
//   (If the type is not 'id' an array is returned.)
// 
//   function   Type  Element
//      |        |      |
// grabElement('id','footer');          grabElement('tag','h1');
// grabElement('name','emailForm');     grabElement('class','topOfThe');
// grabElement('class','topOfThe left');
// 
function grabElement(type,item) {
  var Element=""; // Clear Element variable in case there is a lingering value. (overkill)
  var str = "document.getElement"; // This begins the command that will be run later.
  if (type=="id") str+="ById";     // Continue to build the string to represent the function to be run later.
  else if (type=="tag" || type=="class") str+="sByTagName";  // (document.getElementByClassName does not work in IE)
  else if (type=="name") str+="sByName";
  else {
    alert("grabElement error!\n\ngrabElement was called by " + (grabElement.caller).caller + "\n\n'type' is incorrect: it should be 'id'  'tag'  'class' or 'name'");
    return false;}
  if (type=="class") { //Until .getElementsByClassName is used by IE we grab all tags to check them for the class
    var name = item; // Move the the object title over so that the string can be built properly.
    item = "*";} // Asterisk selects all objects on the page.
  str+="('"+item+"');"; // The object(s) title
  if (type=="class") { // Parse all the elements and store the ones with the matching class to an array.
    var allElem = eval(str);
    var Element = new Array();
    
    for (i=0; i<allElem.length; i++) {
      var temp  = allElem[i].className;
      if (temp.search(name)!=-1) Element.push(allElem[i]);
    }
    str="Element";
  }
  return eval(str);
}



// Set the style of an element to the given value
//   (Reqs. function grabElement)
//   
//        function  Type    Element    JS Style   Value
//            |      |         |          |         | 
//        setStyle('class','table_row','width','300px');   
//
function setStyle(type,item,jsstyle,value) {
  var err_str = "setStyle error!\n\nsetStyle was called by " +setStyle.caller+ "\n\n";
  
  if (type!="id" && type!="tag" && type!="class" && type!="name") {
    alert(err_str + "'type' (the first passed argument) is incorrect: it should be 'id'  'tag'  'class' or 'name'");
    return false;}
  else {
    var Element = grabElement(type,item);
    if (type=="id") {
      str = "Element.style." + jsstyle + " = \"" + value + "\";";
      eval(str);}
    else {
      for (i=0; i<Element.length; i++) {
        str = "Element["+i+"].style." + jsstyle + " = \"" + value + "\";";
        eval(str);}
      //for (i=0; i<Element.length; i++) {
        //str = "Element["+i+"].setAttribute(\""+jsstyle+"\",\""+value+"\");";
        //eval(str);}
    }
  }
}



// Set the numerical value of multiple elements to the greatest value among the given elements
//   All elements must be of the same type (id,class,name)
//   (Reqs. functions grabElement & setStyle)
//   
//                                                          Elements for comparison                  If there is only one Element it is treated as id.
//        function    Type   JS Property  JS Style     _____________________________________           (The type is collected to pass along to other functions)
//            |        |         |          |         |          |          |               |          
//       MatchObjSize('id','offsetWidth','width','Element1','Element2','Element3', ... 'ElementX');   
//
function MatchObjSize() {
  var err_str = "MatchObjSize error!\n\nMatchObjSize was called by " +MatchObjSize.caller+ "\n\n";
  var type=arguments[0];
  if (type!="id" && type!="tag" && type!="class" && type!="name") {
    alert(err_str + "'type' (the first passed argument) is incorrect: it should be 'id'  'tag'  'class' or 'name'");
    return false;}
  else {
    var property=arguments[1]; //Element characteristic
    var jsstyle=arguments[2]; //JS style jsstyleattribute
    var NumOfElem = (arguments.length);
    var topVal = 0;
    
    if (type=='class' || type=='name' || type=='tag') err_str += "Element run as an array.\n\n";
    else if (type=='id') err_str += "Element run as a single object (not an array).\n\n";
    
    for (z=3; z<NumOfElem; z++) { // Element to be checked
      var Element = grabElement(type,arguments[z]);
      if (type=='id') {
        value = eval("Element." + property+";");
        if (value==null) {
          alert(err_str + "The second argument in MatchObjSize('..',SECOND,'..','..') is not a valid JavaScript property.");
          return false;}
        else if (value==undefined) {
          alert(err_str + "The '"+arguments[z]+"' Element in MatchObjSize('..','..','..','..') does not have a value.");
          return false;}
        else if (topVal<value) topVal=value;}
      else {
        for (n=0; n<Element.length; n++) {
          value = eval("Element["+n+"]." + property+";");
          if (value==null) {
            alert(err_str + "The second argument in MatchObjSize('..',SECOND,'..','..') is not a valid JavaScript property.");
            return false;}
          else if (value==undefined) {
            alert(err_str + "The '"+arguments[z]+"' Element in MatchObjSize('..','..','..','..') does not have a value.");
            return false;}
          else if (topVal<value) topVal=value;}
      }
    }
    topVal = topVal + 'px';
    for (n=3; n<NumOfElem; n++) {
      setStyle(type,arguments[n],jsstyle,topVal);}
  }
}
 
 
 
// Call AJAX function. (This is used to submit dynamic requests to server-side functions.)
// Usage: var x = new ajaxOpen();
function ajaxOpen(){
  var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
  if (window.ActiveXObject) { //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
    for (var i=0; i<activexmodes.length; i++) {
      try{
        return new ActiveXObject(activexmodes[i])}
      catch(e){}
    }
  }
  else if (window.XMLHttpRequest) // if Mozilla, Safari etc
    return new XMLHttpRequest()
  else {
    alert('XMLHTTPRequest (AJAX) could be initialized.');
    return false;}
}



// Grab AJAX response. This is for basic AJAX requests.
// Usage: qkAjax('/folder/file.php','first=Michael&last=Jordan');
// 
//     Check for a positive response to an AJAX request, then do something. (Can be used to check if a file exists.)
//     var ajaxreq = qkAjax('/folder/file.php','first=Michael&last=Jordan');
//     if (ajaxreq) document.write(qkAjax('/folder/file.php','first=Michael&last=Jordan'));
// 
function qkAjax(file,params,method,sync,host) {
  if (!file || !params) {
    alert('A filename and parameters must be provided.');
    return false;}
  else {
    if (!method) method = 'GET'; // The PHP script will get the variable values from the requested file.
    if (!host) host = 'http://'+window.location.hostname+'/'; // Will return subdomain.domain.com
    if (!sync) sync = 'false';
    if (method!='GET' && method!='POST') {
      alert('AJAX method must be either GET or POST.');
      return false;}
    else {
      var ajaxConnection = new ajaxOpen();
      if (!ajaxConnection) return false;
      else {  // Successful AJAX connection and workable parameters.
        if (method=='GET') {
          if (sync=='false') ajaxConnection.open('GET',host+file+'?'+params,false);
            else ajaxConnection.open('GET',host+file+'?'+params,true);
          ajaxConnection.send(null);} // Send GET request
        else if (method=='POST') {
          // A form post will likely involve more complicated strings that include spaces and punctuations.
          // There will be an ongoing issue with '&'  ---this still needs to be resolved.
          /*var str = params.split('&');
          for (n=0; n<str.length; n++) {
            var val = str[n].split('=');
            str[n] = val[0] +'=' + escape(val[1]);
            if (n==0) params=str[n];
            else params += '&'+str[n];}*/
          if (sync=='false') ajaxConnection.open('POST',host+file,false);
            else ajaxConnection.open('POST',host+file,true);
          ajaxConnection.setRequestHeader('Content-type','application/x-www-form-urlencoded');
          ajaxConnection.setRequestHeader("Content-length", params.length);
          ajaxConnection.setRequestHeader("Connection", "close");
          ajaxConnection.send(params);}
        // Check if the file was inaccessible
        if (ajaxConnection.status != 200) {
          alert('A '+ajaxConnection.status+': '+ajaxConnection.statusText+' error was returned when making an AJAX request.');
          return false;}
        else {
          // Check the requested content type and employ the corresponding property
          var contentType = ajaxConnection.getResponseHeader('Content-Type')
          if (contentType=="text/xml") var ajaxResponse = ajaxConnection.responseXML;
          else if (contentType=="text/json") var ajaxResponse = eval(ajaxConnection.responseText); //(beware of cross domain security issues)
          else var ajaxResponse = ajaxConnection.responseText;
          return ajaxResponse;}
      }
    }
  }
}
//--->