// Ajax object needs this. Borrowed from who knows where. This should work under both IE and Firefox
function getHTTPObject() {

  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  	xmlhttp = false;
  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
	alert("xmlhttp failed");
      xmlhttp = false;
    }
  }
  return xmlhttp;
}

// Define our Ajax object

function Ajax(objname,object)
{
	this.objname = objname;
	this.json_send = '';
	this.json_recv = '';
	this.controldivref = -1;
	this.divref = -1;
}

// Set the URL of the Ajax service
Ajax.prototype.setURL = function (url)
{
	this.url = url;
	//alert("setURL: " + this.url);
}

// Select the "main DIV" that this Ajax widget works within
Ajax.prototype.setDiv = function (divname)
{
	this.divref = document.getElementById(divname);
}

Ajax.prototype.setControls = function (divname)
{
	this.controldivref = document.getElementById(divname);
}

Ajax.prototype.setResponse = function (funcname)
{
	this.responseref = funcname;
}

Ajax.prototype.minimize = function ()
{
	this.divref.style.display = 'none';
}

Ajax.prototype.maximize = function ()
{
	if (this.divref != -1) {
		this.divref.style.display = 'block';
	}
}

// Set the status of a particular item to
// open (val=1) or closed (val=0)
Ajax.prototype.setItem = function (id,val) {
	var el_div = document.getElementById('xx' + id);
	var el_img = document.getElementById('im' + id);
	if (val == 0) {
		el_div.style.display = 'none';
		el_img.src = 'flag_1.png';
	} else {
		el_div.style.display = 'block';
		el_img.src = 'flag_0.png';
	}
}

// User-interface handlers
// user clicks the hideall button
Ajax.prototype.hideall = function () {
 	var arr = this.divref.getElementsByTagName('DIV');
	for(var i=0; i<arr.length;i++){
		var tn = arr[i].id;
		if (tn.substr(0,2) == 'xx') {
			var id = tn.substr(2);
			this.setItem(id,0);
		}
	} 
	return(false);
}
// user clicks the showall button
Ajax.prototype.showall = function () {
 	var arr = this.divref.getElementsByTagName('DIV');
	for(var i=0; i<arr.length;i++){
		var tn = arr[i].id;
		if (tn.substr(0,2) == 'xx') {
			var id = tn.substr(2);
			this.setItem(id,1);
		}
	} 
	return(false);
}
// user clicks the open/close widget
Ajax.prototype.clickaction = function (id) {
	var elnm = 'xx' + id;
	var elim = 'im' + id;

	// this really ought to scan the divref but apparently getElementById has 
	// limitations - or we did a passbyvalue instead of pass by reference
 	//var elmt = this.divref.getElementById(elnm);
 	var elmt = document.getElementById(elnm);
	var elimage = document.getElementById(elim);
	if (elmt.style.display == 'block') {
		elmt.style.display = 'none';
		elimage.src='flag_1.png';
	} else {
		elmt.style.display = 'block';
		elimage.src='flag_0.png';
	}
	return(false);
}

// http object callback - when response is done
Ajax.prototype.httpResponseGCE = function () { 

	if (this.http.readyState == 4) {
		// get the response
		var jsontxt;
		if ( this.http.status == 200 ) {
			//alert("reply: " + this.http.responseText);
			jsontxt = this.http.responseText;
			//this.json_recv = eval( "(" + jsontxt  + ")" );
			//alert('response:'+jsontxt);
			this.json_recv = JSON.parse(jsontxt);
		} else if ( this.http.status == 0) {
			// timeout?
		} else {
			alert( "Ajax.httpResponseGCE: There was a problem with the URL." + this.http.status);
		}
		//this.divref.innerHTML = this.json_gen_div(this.json_recv);
		//this.controldivref.innerHTML = this.json_gen_controls();
		// pass the json variables back to the callback function
		this.responseref(this.json_recv);
		this.maximize();

		//alert(this.divref.innerHTML);
		// format the xml doc into human-readable text
		// document.getElementById('listxmldump').innerHTML = jsontxt;
	} else {
		// alert(http.readyState);
	}
}

Ajax.prototype.sendmsg = function (object) {
	//alert("calling GET " + this.url);
	//this.http = new XMLHttpRequest();
	this.json_send = JSON.stringify(object);
	//alert(this.json_send);

	this.http = getHTTPObject();
        this.http.open("POST", this.url, true);
	var obj = this;
        this.http.onreadystatechange = function(){obj.httpResponseGCE()};
	this.http.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
	//alert(this.json_send);
	this.http.send(this.json_send);
        //this.http.send(null);
}

// This function's purpose is to create a DIV object
// from the returned Ajax data and diplay it
Ajax.prototype.json_gen_div = function (strarr) {
	var nm = this.objname;
	var leng = strarr.length;
	var str = '';
	if (leng > 0) {
		if (this.controldivref == -1) {
			str += this.json_gen_controls();
		}
		for (i = 0; i < leng; i++) {
			//str += '<DIV onClick="' + nm + '.clickaction('+strarr[i].ipdb_id+');" class="ipdb_entry" id="id' + strarr[i].ipdb_id + '">';
			// if no DIV specified, just put it in here
			str += '<DIV class="ipdb_entry" id="id' + strarr[i].ipdb_id + '">';
			str += '<IMG onClick="' + nm + '.clickaction('+strarr[i].ipdb_id+');" id="im' + strarr[i].ipdb_id + '" src="flag_1.png"> ';
			str += strarr[i].base_address + '/' + strarr[i].prefix_length;
			str += '<DIV onClick="' + nm + '.edit_div(' + strarr[i].ipdb_id + ');" class="ipdb_info" id="xx'+strarr[i].ipdb_id+'">' + strarr[i].description;
			str += '<BR>' + strarr[i].location_id;
			str += '</DIV>';
			str += "</DIV>";
			//alert(str);
		}
	} else {
		str += 'No records found.';
	}
	return str;
}

Ajax.prototype.json_gen_controls = function () {
	var nm = this.objname;

	var str ='';
	str += '<A HREF="" onClick="' + nm + '.showall(); return false ;">Expand All</A>';
	str += '&nbsp;';
	str += '<A HREF="" onClick="' + nm + '.hideall(); return false ;">Shrink All</A>';
	str += '&nbsp;';
	if (this.controldivref != -1) {
		str += '<IMG onClick="' + nm + '.minimize();" src="minmax_0.png"> ';
		str += '<IMG onClick="' + nm + '.maximize();" src="minmax_1.png"> ';
	}
	//alert(str);
	return str;
}

Ajax.prototype.edit_div = function (divid) {
	var divref = document.getElementById('xx' + divid);
	var txt = divref.innerHTML;
	var str = '';
	str += '<TEXTAREA>';
	str += txt;
	str += '</TEXTAREA>';
	divref.innerHTML = str;
	divref.onclick = function() { return false; };
	return false;
}


Ajax.prototype.abort = function () {
	this.http.abort();
}

