// Wrapper function to get a cross browser XMLHttp object
/***************************************************************************************************************************/
function getXMLHTTP() {
	// function to create an XmlHttp object
	var xmlHttp = null;

	try {
		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e) {
		try {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(oc) {
			xmlHttp = null;
		}
	}

	if(!xmlHttp && typeof XMLHttpRequest != "undefined") {
		xmlHttp = new XMLHttpRequest();
	}

	return xmlHttp;
}

function Selection(mySel) {
	Msg=""
	var count=0;
	if (mySel==undefined) {
		return "";
	} else if (mySel.length==undefined) {
		if (mySel.checked==true) {
		return mySel.value;
		}
		else if (mySel.checked==false) {
			} 
	} else {
		count=0;
			for (var i=0;i<mySel.length; i++) {
				if(mySel[i].checked==true) {
           	  	Msg= Msg + mySel[i].value +","; 	
				count ++;
				 }
			}
		return Msg;
		} 
}

function SelAll(mySel) {
	Msg=""
	if (mySel.length==undefined) {
     	mySel.checked=true;
      } 
	else {
		for (i = 0; i <mySel.length; i++) {
		mySel[i].checked = true;
		Msg= Msg + mySel[i].value +" \n "; 	
		} //alert(Msg);
	 } 
}

function Unselect(mySel) {
	if (mySel.length==undefined) {
     	mySel.checked=false;
     } 
	else {
		for (i = 0; i <mySel.length; i++) {
		mySel[i].checked = false;
		}
	 } 
}

function Inverse(mySel) {
	Msg=""
	if (mySel.length==undefined) {
		mySel.checked=!mySel.checked;
	} 
	else {
		for (i = 0; i <mySel.length; i++) {
		mySel[i].checked = !mySel[i].checked;
		}
	 } 
}

function Delete(mySel) {
var f= document.forms[0];
var thisPath= f.ThisPath.value;
var url="/" + thisPath + "/" + "ajxDeleteSelected" + "?OpenAgent";
//alert("Name:" +f.MySel);
var selectionList=Selection(f.MySel);
//alert("test:" + selectionList);
if (selectionList=="") {
	alert("You must select at least one document to delete");
} else {
	var go =confirm("Are u sure you want to delete the selected document");
	if (go) {
		//alert(selectionList);
		deleteDocs(selectionList,url);
	}
}	
}	
	
function QueryObj(q) {
	if(q.length > 1) this.q = q; //this.q = q.substring(1, q.length);
		else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}

	this.getKeyValuePairs = function() { return this.keyValuePairs; }

	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0].toLowerCase() == s.toLowerCase())
			return this.keyValuePairs[j].split("=")[1];
		}
		return "";
	}

	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}

	this.getLength = function() { return this.keyValuePairs.length; } 
}


/************************** Validation Error Messages handling ******************************/
/*Searching using java script validation form*/
function errorHandler(errorStr) {
	var f = document.forms[0];
	var queryObj = new QueryObj(errorStr);
	var errFields = queryObj.getParameters();
	var length = queryObj.getLength();
	var errDiv;

	for(var j=1; j < length; j++) {
		errDiv = document.getElementById("Err"+errFields[j]);
		errDiv.innerHTML = unescape(queryObj.getValue(errFields[j]));
		errDiv.style.display = "block";
	}
}

function removeErrors() {
	var divs = document.getElementsByTagName("DIV");
	for(var j=0; j < divs.length; j++) {
		if (divs[j].id.indexOf('Err') == 0) {
			divs[j].style.display = "none";
		}
	}
}

/************************** AJX Status Message handling ******************************/
function writeStatusById(id, str, display, vTimer) {
	var f = document.forms[0];
	var obj = document.getElementById(id);
	if (!(obj == undefined)) {
		obj.innerHTML = str;
		obj.style.display = display;
		if (vTimer > 0) {
			setTimeout("removeStatusById('" + id + "')",vTimer * 1000);
		}
	}
}
	
function removeStatusById(id) {
	var f = document.forms[0];
	var obj = document.getElementById(id);
	obj.innerHTML = "";
	obj.style.display = "none";
}

