// JavaScript Document

Array.prototype.unique = function () {
	var r = new Array();
	o:for(var i = 0, n = this.length; i < n; i++)
	{
		for(var x = 0, y = r.length; x < y; x++)
		{
			if(r[x]==this[i])
			{
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}


function inArray(theArray, theItem){
	var found = false;
	
	for(var i = 0; i < theArray.length; i++){
		if(theArray[i] == theItem){
			found = true;
			break;
		}
	}
	return found;
}

function arrayIndex(theArray, theItem){
	var found = false;
	
	for(var i = 0; i < theArray.length; i++){
		if(theArray[i] == theItem){
			found = i;
			break;
		}
	}
	return found;
}

function xmlFileDom(url){
	
	if (window.XMLHttpRequest){
  		this.xhttp=new XMLHttpRequest();
  	}else{ // IE 5/6
  		this.xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	
	// parse the credit_list_template.xml file and set the creditTables variable to the content of the <category></category> tags
	this.xhttp.open("GET",url,false);
	this.xhttp.send(null);
	this.xmlDoc=this.xhttp.responseXML;
}

function xmlTextDom(text){
	if (window.DOMParser){
  		this.parser=new DOMParser();
  		this.xmlDoc=this.parser.parseFromString(text,"text/xml");
  }else{ // Internet Explorer
  	this.xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  	this.xmlDoc.async="false";
 	this.xmlDoc.loadXML(text);
  } 	
}

function removeChildren(object){// a general function to remove all the child elements of the object parameter
	if (object.hasChildNodes()){
    	while (object.childNodes.length >= 1){
        	object.removeChild(object.firstChild);       
    	} 
	}
}


function xmlSubmitDom(url,formData){
	if (window.XMLHttpRequest){
  		this.xhttp=new XMLHttpRequest();
  	}else{ // IE 5/6
  		this.xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  	}
	
	// parse the credit_list_template.xml file and set the creditTables variable to the content of the <category></category> tags
	//this.xhttp.open("POST",url,true);
	var parent = this;
	this.processed = false;
	
	
	this.xhttp.onreadystatechange = function() {
		if(this.readyState==4 && this.status==200){
			
			parent.processRequest();
			
    	}
	}
	
	this.xhttp.open("POST",url,true);
	this.xhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	this.xhttp.send(formData);
	this.processRequest = function(){this.xmlDoc=this.xhttp.responseXML; this.xmlText = this.xhttp.responseText; this.processed = true;};
}

function getRequestBody(theForm) {
	var p = [];
	for (var i = theForm.elements.length-1; i >= 0; i--) {
		var fld = theForm.elements[i];
		switch (fld.type) {
			case "button": case "submit": case "reset": break;
			case "checkbox": case "radio": if (!fld.checked) break;
			default:
				if ("select" == fld.tagName.toLowerCase()){
					p.push(ncode(fld.name,fld.options[fld.selectedIndex].value));
				}else{ 
					p.push(ncode(fld.name,fld.value));
				}
		}
	}
	return p.join('&');
}
 
function ncode(n,v) {
	
	//alert(encodeURIComponent(n) + '=' + encodeURIComponent(v));
	return encodeURIComponent(n) + '=' + encodeURIComponent(v);
}

