var xmlHttp;

function removeNode(id){
	obj = document.getElementById(id);
	obj.parentNode.removeChild(obj);
}

function removeParentRow(id){
	obj = document.getElementById(id);
	row = obj.parentNode;
	while (row.tagName != "TR") {
		row = row.parentNode;
	}
	row.parentNode.removeChild(row);
}

function doRequestInNewRow(url,obj,id){
	if(!document.getElementById(id))
		addRowBelow(obj,id);
	doRequest(url,id);
}

//we add this function as an onclick event handler to an element.
//it will add a row below the row that contains the element
function addRowBelow(obj,text){
	
	
	row = obj.parentNode;
	while (row.tagName != "TR") {
		row = row.parentNode;
	}
	
	table = obj.parentNode;
	while (table.tagName != "TABLE") {
		table = table.parentNode;
	}
	
	var newrow = table.insertRow(row.rowIndex+1);
	
	newrow.className = row.className;
	
	var cell = newrow.insertCell(0);
	var newdiv = document.createElement("div");
	newdiv.id = text;
	newdiv.style.width="100%";
	cell.appendChild(newdiv);
	//cell.appendChild(document.createTextNode(text));
	cell.colSpan = row.cells.length;
	newrow.appendChild(cell);
	
		
}

function doRequest(url, id)
{ 
	tooltip.hide(); 
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	// make the element a twirling loader
	document.getElementById(id).innerHTML="<img src='images/ajax-loader.gif'></img>";
	xmlHttp.onreadystatechange= function(){
		if (xmlHttp.readyState==4)
		{
			if (xmlHttp.status==200)
			{
				
				writeHTML(xmlHttp, id);
				tooltip.init();
			}
		}



	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


//this function captures the textboxes and passes their contents to papersignatures.php
function doPaperSave(id) {
	
	var boxes=document.getElementsByName("tbNum_"+id);
	var queryArray = new Array();
	
	for (var i = 0; i < boxes.length; i++)
	{
		queryArray[i] = boxes[i].id + "=" + boxes[i].value;   
	} 
	
	var queryString = queryArray.join('&');
	
	doRequest("papersignatures.php?action=save&id="+ id +"&"+queryString,"sigDiv_"+id);
	
	
}

function doStatusSave(id) {
	var ddlStatus=document.getElementById("ddlStat_"+id);
	doRequest("petitionstatus.php?action=save&id="+ id +"&status="+ddlStatus.options[ddlStatus.selectedIndex].value,"statDiv_"+id);
}

function doFormatSave(id) {
	var ddlFormat=document.getElementById("ddlFormat_"+id);
	doRequest("petitionformat.php?action=save&id="+ id +"&format="+ddlFormat.options[ddlFormat.selectedIndex].value,"formatDiv_"+id);
}


function writeHTML(req, id) {
	
	document.getElementById(id).innerHTML = req.responseText;
}





function GetXmlHttpObject(){
    var objXMLHttp = null;
    
    if (window.XMLHttpRequest){
        try{
            objXMLHttp = new XMLHttpRequest();
        }catch (e){
            objXMLHttp = false;
        }
    }else if (window.createRequest){
        try{
            objXMLHttp = new window.createRequest();
        }catch (e){
            objXMLHttp = false;
        }
    }else if (window.ActiveXObject){
        try {
            objXMLHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            try {
                objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e){
                objXMLHttp = false;
            }
        }
    }
    
    return objXMLHttp;
}