/*******************************************************************************************
Copyright (C) 2007  Derek Jones derek@derekjulie.com

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*******************************************************************************************/
/**********************
Begin Request Class
**********************/
function Request() {
  try {
    this._request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      this._request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        this._request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        this._request = null;
      }
    }
  }
  if (this._request == null) {
    alert("Error creating request object!");
  }
  return this._request;
}

Request.prototype._request;

Request.prototype.getRequest = function(){
	return this._request;	
}
//end class


/********************************************
Text tools
********************************************/
function replaceText(el, text) {
  if (el != null) {
    clearText(el);
    var newNode = document.createTextNode(text);
    el.appendChild(newNode);
  }
}

function clearText(el) {
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        el.removeChild(childNode);
      }
    }
  }
}

function getText(el) {
  var text = "";
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        if (childNode.nodeValue != null) {
          text = text + childNode.nodeValue;
        }
      }
    }
  }
  return text;
}

function stripslashes(str) {
str=str.replace(/\\'/g,'\'');
str=str.replace(/\\"/g,'"');
str=str.replace(/\\\\/g,'\\');
str=str.replace(/\\0/g,'\0');
return str;
}

function get_node(my_node){
	var elDoc = document.getElementById(my_node).lastChild;
	if(elDoc != null && elDoc != undefined){
		return elDoc.nodeValue;
	}
}
	
/*****Use this for changing text in a span*****/
function my_replace(label,newText){
	if(document.getElementById(label)){
		if(newText=="NA"){
			newText="";
		}
		var oldText= document.getElementById(label);
		replaceText(oldText,newText);
	}else{
		alert("Text replace error: " + label);
	}
}
/*****Use this for changing text in a text box*****/
function set_text(field, value){
	if(document.getElementById(field)){
		if(value=="NA"){
			value="";	
		}
		document.getElementById(field).value=value;
	}else{
		alert("Text box replace error: " + field);	
	}
}

function get_XML(req,field){
	var xmlDoc=req.responseXML;
	var xmlMSG="";
	var newMSG="";
	var xmlMSG = xmlDoc.getElementsByTagName(field)[0];
	if(xmlMSG.hasChildNodes()){
		newMSG = xmlMSG.firstChild.nodeValue;
	}
	return newMSG;
}
/***********************
Indicators
***********************/
function hide_progress() {
    document.getElementById("indicator").style.visibility = "hidden";
	document.getElementById("prog_text").style.visibility = "hidden";
}
           
function disp_progress() {
	document.getElementById("indicator").style.visibility = "visible";
    document.getElementById("prog_text").style.visibility = "visible";
}

function check_progress(){
	if(document.getElementById("indicator").style.visibility =="visible"){
		alert("Still working, please wait for previous job to complete.");
		return 0;
	}else{
		return 1;	
	}
}
/*********************************
Validation
**********************************/
var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function isValid(parm,val) {
	if (parm == ""){
		return true;
	}
	if (parm == null){
		return false;
	}
	for (i=0; i<parm.length; i++) {
		if (val.indexOf(parm.charAt(i),0) == -1) return false;
	}
	return true;
}

function isNum(parm) {return isValid(parm,numb + '.'+ '-');}
function isLower(parm) {return isValid(parm,lwr+' ');}
function isUpper(parm) {return isValid(parm,upr+' ');}
function isAlpha(parm) {return isValid(parm,lwr+upr+' ');}
function isAlphanum(parm) {return isValid(parm,lwr+upr+numb+' ');}

function response_error_check(request){
	var newText = request.responseText;
	var loc = newText.search(/xml version/);
	if(loc != 2){
		alert(stripslashes("ERROR: " + newText));
	}
}
/************************************
Miscellaneous Functions - Requires JQuery
************************************/

