var htmlNode = document.createElement("div");

/*
 * Return the value of a query parameter in the address bar
 */
function getQueryParameter(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split('&');
    for (var i = 0; i < vars.length; i++) {
        var pairs = vars[i].split("=");
        if (pairs[0] == variable) {
            return pairs[1];
        }
    }
    return null;
}

// Acquire AJAX
function getXMLHttpObject() {
  var xmlHttp;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp = new XMLHttpRequest();
  } catch (e) {
    // Internet Explorer
    try {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        alert("Your browser does not support AJAX!");
        return null;
      }
    }
  }
  return xmlHttp;
}

/**
* Send AJAX request
*/
function requestData(xmlHttp, url, data, method, callback) {
	if (xmlHttp) {
		if (method.toUpperCase() == "GET") {
			if (data) {
				xmlHttp.open("GET", url + "?" + data, true);
			} else {
				xmlHttp.open("GET", url, true);
			}
			xmlHttp.onreadystatechange = callback;
			xmlHttp.send("");
		} else {
			xmlHttp.open("POST", url, true);
			xmlHttp.onreadystatechange = callback;
			xmlHttp.send(data);
		}
	}
}


/**
* This function will show a layer
*/
function showLayer(layerID) {
	var layer = document.getElementById(layerID);
	if (!layer) {
		return true;
	}
	layer.style.zIndex = 2;
	layer.style.visibility = "visible";
	return false;
}

/**
* This function will hide a layer
*/
function hideLayer(layerID) {
	var layer = document.getElementById(layerID);
	if (!layer) {
		return true;
	}
	layer.style.zIndex = 0;
	layer.style.visibility = "hidden";
	return false;
}

/*
* Unescape HTML
*/
function unescapeHTML(html) {
    htmlNode.innerHTML = html;
    if(htmlNode.innerText)
        return htmlNode.innerText; // IE

    return htmlNode.textContent; // FF
}

/*
* Create a table
*/
function createTable(tableID, styleClass) {
	var table = document.createElement("table");
	if (tableID) {
		table.id = tableID;
	}
	if (styleClass) {
		table.className = styleClass;
	}
	return table;
}

/*
* Create a table body
*/
function createTableBody(bodyID, styleClass) {
	var tBodyElement = document.createElement("tbody");
	if (bodyID) {
		tBodyElement.id = bodyID;
	}
	if (styleClass) {
		tBodyElement.className = styleClass;
	}
	return tBodyElement;
}

/*
* Create the table data
*/
function generateTableData(tableBodyID, tableData) {
	var tableBody = document.getElementById(tableBodyID);
	var trElem, tdElem, textNode;
	for (i = 0; i < tableData.length; i++) {
		trElem = tableBody.insertRow(tableBody.rows.length);
		for (x = 0; x < tableData[i].length; x++) {
			tdElem = trElem.insertCell(trElem.cells.length);
			textNode = document.createTextNode(tableData[i][x]);
			tdElem.appendChild(textNode);			
		}
	}
}

/*
* Create a table row
*/
function createTableRow(cellData) {
	var row = document.createElement("tr");
	for (var i = 0; i < cellData.length; i++) {
		var cell = document.createElement("td");
		var value = document.createTextNode(colData[i]);
		cell.appendChild(value);
		row.appendChild(cell);
	}
	return row;
}

/*
* Clear the contents of a drop down element
*/
function clearDropDown(dropDownElem) {
	for (var i = dropDownElem.options.length-1; i >=0; i--) {
		dropDownElem.remove(i);
	}
}

/*
* Show processing box
*/
function showProcessing() {
	scroll(0,0);
	var div = document.createElement("div");
	var divInner = document.createElement("div");
	var pageWidth;
	var pageHeight;
	if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        pageWidth = document.body.scrollWidth+'px';
        pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      pageWidth = document.body.offsetWidth+'px';
      pageHeight = document.body.offsetHeight+'px';
    } else {
       pageWidth='100%';
       pageHeight='100%';
    }   
	div.id = "procBox";
	div.style.width = pageWidth;
	div.style.height = pageHeight;
	divInner.id = "innerBox";
	divInner.innerHTML = "Processing";
	div.appendChild(divInner);
	try {
		document.body.appendChild(div);
	} catch(err) {
		alert("Error: " + err);
	}
}

/*
* Hid processing box
*/
function hideProcessing() {
	var div = document.getElementById("procBox");
	if (div) {
		try {
			document.body.removeChild(div);
		} catch(err) {
			alert("Error: " + err);
		}	
	}
}

/*
* Show loading image for AJAX
*/
function showAJAXLoading() {
	var div = document.createElement("div");
	var pageWidth;
	var pageHeight;
	if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        pageWidth = document.body.scrollWidth+'px';
        pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      pageWidth = document.body.offsetWidth+'px';
      pageHeight = document.body.offsetHeight+'px';
    } else {
       pageWidth='100%';
       pageHeight='100%';
    }   
	div.id = "loadingBox";
	div.style.height = pageHeight;
	div.style.width = pageWidth;
	document.body.appendChild(div);
}

/*
* Hide loading image for AJAX
*/
function hideAJAXLoading() {
	var div = document.getElementById("loadingBox");
	if (div)
		document.body.removeChild(div);
}
