// /root/js/ajax_utl.js
// Last Updated : 29 November 2006

// Creates a href image inside a parent
function ax_button(parentobj,href,url) {
	a = document.createElement("a");
	a.href = href;
	
	img = document.createElement("img");
	img.setAttribute("src",url);
	img.setAttribute("border","0");
	
	a.appendChild(img);
	parentobj.appendChild(a);
}

function ax_entity(str, mode) {
	str = (str) ? str : "";
	mode = (mode) ? mode : "string";

	var e = document.createElement("div");
	e.innerHTML = str;

	if (mode == "numeric") {
		return "&#" + e.innerHTML.charCodeAt(0) + ";";
	}
	else if (mode == "utf16") {
		var un = e.innerHTML.charCodeAt(0).toString(16);
		while (un.length < 4) un = "0" + un;
		return "\\u" + un;
	}
	else return e.innerHTML;
}

// Creates a tag with an id
function ax_tagwithid(tag,id) {
	var e = document.createElement(tag);
	e.id = id;
	return e
}

// Creates a text tag inside a parent
function ax_texttag(parent,tag,text) {
	var e = document.createElement(tag);
	e.appendChild(document.createTextNode(text));
	parent.appendChild(e);
}
// Creates a text tag inside a parent with an id
function ax_texttagwithid(parent,tag,text,id) {
	var e = document.createElement(tag);
	e.id = id;
	e.appendChild(document.createTextNode(text));
	parent.appendChild(e);
	
}
  
function StringBuffer() { 
  this.buffer = []; 
} 

StringBuffer.prototype.append = function append(string) { 
   this.buffer.push(string); 
   return this; 
}; 

StringBuffer.prototype.toString = function toString() { 
   return this.buffer.join(""); 
}; 

// Functions for the control of breadcrumbs

// This function will add a breadcrumb to the array
function bc_push(id, text) {
	arrBCText[ptrA] = text;
	arrBCID[ptrA] = id;
	
	ptrA++;
}

// This will remove the latest breadcrumb from the array
function bc_pop() {
	ptrA--;
	arrBCText[ptrA] = null;
	arrBCID[ptrA] = null;
}

// This will return true if the breadcrumb needs a link. Will be false
// if it is a top level breadcrumb.
function bc_need_link() {
	if (ptrA <= 1) {
		return false;
	} else {
		return true;
	}
}

// This function returns the current breadcrumb trail, with a link to the
// previous step should it need one.
function bc_get() {
	// As long as we have some breadcrumbs
	if (ptrA != 0) {
		// This will be the string we eventually return
		strReturn = "";
		
		// Go through the array and get the breadcrumb trail
		strBC = "";
		for (i=0; i < ptrA; i++) {
			strBC = strBC + arrBCText[i] + " > ";
		}
		
		// Now turn it into a link if it needs to be
		if (bc_need_link()) {
			//js = "javascript:SearchCategory(" + arrBCID[ptrA-2] + ",'" + arrBCText[ptrA-2] + "');";
			js = "javascript:bc_go_back();";
			link = "<a href=\"" + js + "\">" + strBC + "</a>";
			strReturn = link;
		} else {
			strReturn = strBC;
		}
		
		return strReturn;		
	} else { // Otherwise return an empty string
		return "";
	}
}

// This function returns the current breadcrumb trail, without the link
function bc_get_nolink() {
	// As long as we have some breadcrumbs
	if (ptrA != 0) {
		
		// Go through the array and get the breadcrumb trail
		strBC = "";
		for (i=0; i < ptrA; i++) {
			strBC = strBC + arrBCText[i] + " > ";
		}
		
		return strBC;
	} else { // Otherwise return an empty string
		return "";
	}
}


// This function will go back one step
function bc_go_back() {
	// Get the values
	id = arrBCID[ptrA-2];
	text = arrBCText[ptrA-2];
	
	// Pop last breadcrumb
	bc_pop();
	bc_pop();
	
	// Go back
	SearchCategory(id,text);
}

// This will reset the array pointer.
function bc_reset() {
	ptrA = 0;
}

// Used to debug the breadcrumbs
function bc_debug() {
	for(i=0; i < arrBCID.length; i++) {
		alert(arrBCID[i] + " - " + arrBCText[i]);
	}
}

