
var xmlhttp,alerted,isParentDoc,currentURL, closeParent,isRefresh
var ajaxType = "";
var serverDomainPrefix = Proto_Prefix+greyboxPath(window.location.href);
isParentDoc = false
closeParent = false
isRefresh = false
var ajax_prefix = "ctl00_Main_";


function RunAJAX(Key,Args){
	if(Args){
		Args = "&"+Args;
	}
	go(serverDomainPrefix+'Admin/Code/AJAX/DMSAjaxProcess.aspx?Key='+Key+Args)
}



function parseAjaxData(xml,thisType){

	try {
		var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(xml);
		}catch(e){
			try{
				var parser=new DOMParser();
				var xmlDoc=parser.parseFromString(xml,"text/xml");
				}catch(e){
					alert(e.message);
					return;
				}
		}

// get a node list of fields to iterate over
var FieldsObj = xmlDoc.getElementsByTagName("Field")

	try {
		var t_ = FieldsObj[0].childNodes[0].firstChild.value;
	}catch(e){
		alert('Error:\n\n'+e.message+'\n\n'+xml.substring(xml.indexOf('body'),xml.length));
		return false;
	}
	
for(j=0;j<FieldsObj.length;j++){

	var returnColumn = FieldsObj[j].childNodes[0].firstChild.nodeValue;
	var returnType = FieldsObj[j].childNodes[1].firstChild.nodeValue;
	valueObj = FieldsObj[j].getElementsByTagName("Value")

	if(!document.getElementById(returnColumn)){
		returnColumn = ajax_prefix + returnColumn;
	}
	if(document.getElementById(returnColumn)){
			obj = document.getElementById(returnColumn);

			switch(returnType)
			{
				case "VALUE":
					ajaxPopulate_Value(obj,valueObj);
					break;
				case "NAME/VALUE":
					ajaxPopulate_NameValue(obj,valueObj);
					break;				
				case "VALUE/VALUE":
					ajaxPopulate_ValueValue(obj,valueObj);
					break;
			} // end switch			
		} // end return column check
	} // end for j
	

} // end function


function ajaxPopulate_Value(obj,valueObj){
	
	if(obj.src){
		obj.src=valueObj[0].firstChild.nodeValue;
	}else if(obj.type=="select"||obj.type=="select-one"){
		obj.value = valueObj[0].firstChild.nodeValue;
	}else if(obj.type=="image"){
		obj.src=valueObj[0].firstChild.nodeValue;
	}else if(obj.type=="checkbox"){
		obj.checked=valueObj[0].firstChild.nodevalue;
	}else if(!obj.type){
		obj.innerHTML=valueObj[0].firstChild.nodeValue;
	}else{
		obj.value = valueObj[0].firstChild.nodeValue;
	}
}


function ajaxPopulate_ValueValue(obj,valueObj){
	if(!valueObj) return false;
	RemoveOptions(obj.id);
	var a = valueObj[0].childNodes;
	for(j=0;j<a.length;j++){
		addOption(obj,a[j].childNodes[0].childNodes[0].nodeValue,a[j].childNodes[0].childNodes[0].nodeValue);
	}
}


function ajaxPopulate_NameValue(obj,valueObj){
	if(!valueObj) return false;
	RemoveOptions(obj.id);
	var a = valueObj[0].childNodes;
	for(j=0;j<a.length;j++){
		addOption(obj,a[j].childNodes[0].childNodes[0].nodeValue,a[j].childNodes[1].childNodes[0].nodeValue);
	}
}


function addOption(field,name,value){
	var anOption = document.createElement("OPTION") 
	field.options.add(anOption)
	anOption.value = value;
	anOption.innerText = name;
	anOption.textContent = name;
}




function go(thisUrl,parent) {
if (xmlhttp) {


	// append time stamp
	var currentDateTime = new Date()
	thisUrl = thisUrl + '&zz='  + currentDateTime
	
	if (thisUrl.substring(0,7) != 'http://'){
		thisUrl = serverDomainPrefix+thisUrl
	}

	// add date time stamp
	  xmlhttp.open("GET", thisUrl,true);
	  xmlhttp.onreadystatechange=RSchange
	  xmlhttp.send(null)
 }
}





if (!xmlhttp && !alerted) {
 try {
		xmlhttp=new XMLHttpRequest()
  } catch (e) {
	try{
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		} catch(e){
	}
 }
}

function RSchange() {

	if (xmlhttp.readyState==4) {
		if(xmlhttp.responseText != ''){
			if(isParentDoc){
				if(isParentDoc == true){
	  				parseAjaxData(xmlhttp.responseText,ajaxType);
					isParentDoc = false; // turn off
					closeParent = true;
				}else{
	  				parseAjaxData(xmlhttp.responseText,ajaxType);
				}
			}else{
	  			parseAjaxData(xmlhttp.responseText,ajaxType);
			}
		}
 	}
}




function RemoveOptions(FieldID){
	if(document.getElementById(FieldID)){
		var L_=document.getElementById(FieldID).options.length;
		if(document.getElementById(FieldID).options.length > -1){
			for(z=0;z<L_;z++){
				document.getElementById(FieldID).remove(0);
			}
		}
	}
}
