//*************************************************
// Generic Validation and Data collection functions
//*************************************************
function get_check_value(chkname) {
  var c_value = new Array();
  var e = 0;
  for (var i=0; i < chkname.length; i++) {
    if (chkname[i].checked) {
      c_value[e] = chkname[i].value.split(",");
      e++;
    }
  }
  return c_value;
}

//empties all of the values for a given div id
function clearInputs(object_id) { 
  var object, elements, i, elm; 
  object = document.getElementById(object_id);

	if (document.getElementsByTagName) {
		elements = object.getElementsByTagName('input');
		for( i=0, elm; elm=elements.item(i++); ) {
			if (elm.getAttribute('type') == "text") {
				elm.value = '';
			}
		}
		elements = object.getElementsByTagName('select');
		for( i=0, elm; elm=elements.item(i++); ) {
			elm.options.selectedIndex=0;
		}
	}
}

//checks for valid numeric strings
function IsNumeric(strString){
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++){
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1){
         blnResult = false;
      }
  }
  return blnResult;
}

//*************************************************
// Functions for executing AJAX elements
// highly modified from the w3schools.com script
//*************************************************

function runAJAXphp(script_url, post_array, object_id, append, element){
  var xmlHttp = GetXmlHttpObject(); //creats the getxmlhttp object
  if (!xmlHttp){ //throw an error if the page does not support ajax
    alert ("Browser does not support HTTP Request");
    return
  }
  script_url += "&sid="+Math.random(); //creates a random number to ensure that the page looks unique and loads fresh
  //get rid of or modify the above line if your script doesn't suppor get variables.
     
  xmlHttp.onreadystatechange=function(){ //sub function that checks if the external script has completed
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
      if (append == "element"){
        document.getElementById(object_id).setAttribute(element, xmlHttp.responseText); 
      } else {
        if (append){ //if the ajax script will APPEND to the object content
          document.getElementById(object_id).innerHTML= document.getElementById(object_id).innerHTML + xmlHttp.responseText;
        } else { //if the ajax script will REPLACE the object content
          document.getElementById(object_id).innerHTML=xmlHttp.responseText;
        }
      }
			var div = document.getElementById(object_id);   
			var x = div.getElementsByTagName("script"); //find any script tags in the html
			for(var i=0;i<x.length;i++){ //foreach script tag found
				eval(x[i].text); //execute it
			}
    }
  };

  xmlHttp.open("POST", script_url, true); //opens a new http connection
  if (post_array == null){ //if there is no post data to be send send a null header
    xmlHttp.send(null);
  } else { //if there is post data being sent create header info and prep the post data
    var post_string = ''; //create a var for the post string
    for (i in post_array) { //loop through the post array and create a string 
      post_string += i + '=' + escape(encodeURI(post_array[i])) + '&';
    }
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //set request header
    post_string = post_string.substring(0,post_string.length-1); //trims off the trailing & character of the post string
    //send the http request with the post data.
    xmlHttp.send(post_string);
  }
}

//this function creates the xmlhttp object for the appropriate browser
function GetXmlHttpObject(){ 
var objXMLHttp=null;

     if (window.XMLHttpRequest){
          objXMLHttp=new XMLHttpRequest();
     }else if (window.ActiveXObject){
          objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     return objXMLHttp;
}

