
var isIE = false;
if (window.ActiveXObject){
	isIE = true;
}
function GetText(node){
	if(isIE){
		return node.text;
	}else{
	return node.textContent;
	}
}
function GetByTagName(node, name){
	var childArray = new Array();
	for(var i=0; i<node.childNodes.length; i++){
		if(node.childNodes[i].nodeName == name){
			childArray[childArray.length] = node.childNodes[i];
		}
	}
	return childArray;
}

var xmlDocVersions = new Array("Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "Msxml2.DOMDocument", "Microsoft.DOMDocument");

function push_GetxmlDoc(xml, index){
	var xmlDoc=null;
	if (window.ActiveXObject && index < xmlDocVersions.length){
		try{
			xmlDoc = new ActiveXObject(xmlDocVersions[index]);
			Debug("xml Doc Version: "+xmlDocVersions[index]+"<br>");
			xmlDoc.async="false";
			xmlDoc.loadxml(xml);
		}catch(e){
			return push_GetxmlDoc(xml, index+1);
		}
	}
	if (document.implementation.createDocument){
		var parser = new DOMParser();
		xmlDoc = parser.parseFromString(xml, "text/xml");
	}
	if(xmlDoc == null) alert("xml Doc load Failed");
	return xmlDoc;
} 


// USAGE 
//xmlDoc = GetxmlDoc(xmlSTRINGVARIABLE, 0).documentElement;
//var tags = GetByTagName(xmlDoc, TAGNAMEASSTRING);
//for(var t in tags){
//alert(GetText(tags));
//} 


var xmlhttp;
function loadurl(dest) {
  try {
    // browser detection is bad. object detection works for any browser
    xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e) {
    // browser doesn't support ajax. handle however you want
    alert('ajax not supported');
  }
  xmlhttp.onreadystatechange = triggered;
  xmlhttp.open("GET", dest);  //http.open('get', 'rpc.php?action='+action);
  xmlhttp.send(null);
}
function triggered() {
  if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
    // xmlhttp.responseText object contains the response.
    //document.getElementById("reportName").innerHTML = xmlhttp.responseText;
    //xml = xmlhttp.responseXML;
    //document.getElementById("reportName").innerHTML = xml.getElementsByTagName("reportName")[0].firstChild.nodeValue;
    //document.getElementById("fileName").innerHTML = xml.getElementsByTagName("fileName")[0].firstChild.nodeValue;
		
		alert(xmlhttp.responseText);
		xml = xmlhttp.responseText
		xmlDoc = push_GetxmlDoc(xml, 0).documentElement;
    //alert(xmlhttp.getElementsByTagName("bagtotal")[0].firstChild.nodeValue);
    //addParameters(xml);
  }
}
function addParameters(xml) {
  deleteParameters();
  for (var i=1;i<=xml.getElementsByTagName("fieldParameter").length;i++)
  {
    p = new Parameter(xml.getElementsByTagName("fieldParameter")[i-1].firstChild.nodeValue + i);
    if (xml.getElementsByTagName("datatype")[i-1].firstChild.nodeValue == "datetimehour") {
      tb = new TextBoxObj('param' + i);
      p.td2.appendChild(tb.t);
      a = new AnchorObj('anchor'+i,'#');
      img = new ImageObj("images/calendar.gif");
      a.anc.appendChild(img.im);
      p.td2.appendChild(a.anc);

      a.anc.attachEvent("onclick", getCalendar);

      /** assign key to field attribute   */
      img.im.setAttribute("ass_field", tb.t);
      a.anc.setAttribute("ass_field", tb.t);

      img.im.setAttribute("ass_anc", a.anc);
      a.anc.setAttribute("ass_anc", a.anc);
    //else if (xml.getElementsByTagName("datatype")[i-1].firstChild.nodeValue == "datetimehour") {
    }
  }
  //Add button
  p = new Parameter('');
  b = new ButtonObj();
  p.td2.appendChild(b.but);
}
function TextBoxObj(tbname) {
  this.t = document.createElement("INPUT");
  this.t.type = "text";
  this.t.style.width = '150';
  this.t.className = 'textBox';
  this.t.name = tbname;
  this.t.id = tbname;
  //alert(this.t.name);
}
function Parameter(label, datatype) {
  this.tr = tblParameters.insertRow();
  this.td1 = this.tr.insertCell();
  this.td1.style.width = "190";
  this.td1.innerHTML = label;
  this.td2 = this.tr.insertCell();
  this.td2.style.width = "190";
}
function ButtonObj() {
  this.but = document.createElement("INPUT");
  this.but.type = "submit";
  this.but.value = "View Report";
  this.but.name = "ViewReport";
}
function AnchorObj(aname, linktext) {
  this.anc = document.createElement("A");
  this.anc.name = aname;
  this.anc.id = aname;
  this.anc.href = linktext;
  //alert(this.anc.name);
  //this.anc.appendChild(document.createTextNode("<IMG align=top border=0 height=21 id=dimg1 src=images/calendar.gif style='POSITION:left' width=34>"));
}
function ImageObj(imgsource) {
  this.im = document.createElement("IMG");
  this.im.src = imgsource;
  this.im.border = 0;
}
function deleteParameters() {
  for (var j=tblParameters.rows.length-1;j>=0;j--) {
    tblParameters.deleteRow(j);
  }
}
function getCalendar() {
    var paramx = event.srcElement.getAttribute("ass_field");
    var ancx = event.srcElement.getAttribute("ass_anc");
    new CalendarPopup('calendarDIV').select(paramx,ancx.name,'MM/dd/yyyy hh:mm:ss a');
    return false;
}


