function utf8_encode ( argString ) {
    // Encodes an ISO-8859-1 string to UTF-8  
    // 
    // version: 1004.2314
    // discuss at: http://phpjs.org/functions/utf8_encode    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman    // +   improved by: Yves Sucaet
    // +   bugfixed by: Onno Marsman
    // +   bugfixed by: Ulrich
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'    var string = (argString+''); // .replace(/\r\n/g, "\n").replace(/\r/g, "\n");
 
    var utftext = "";
    var start, end;
    var stringl = 0; 
    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);        var enc = null;
 
        if (c1 < 128) {
            end++;
        } else if (c1 > 127 && c1 < 2048) {            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc !== null) {            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;        }
    }
 
    if (end > start) {
        utftext += string.substring(start, string.length);    }
 
    return utftext;
}

/**
 * AutoComplete Field - JavaScript Code
 *
 * This is a sample source code provided by fromvega.
 * Search for the complete article at http://www.fromvega.com
 *
 * Enjoy!
 *
 * @author fromvega
 *
 */

// This is for the sidebar search field
// We'll have to use jQuery instead of $ sign to prevent prototype and
// jQuery to conflict.

// global variables
var acListTotal   =  0;
var acListCurrent = -1;
var acDelay		  = 000;
var acURL		  = null;
var acSearchId	  = null;
var acResultsId	  = null;
var acSearchField = null;
var acResultsDiv  = null;

function setAutoComplete(field_id, results_id, get_url){

	// initialize vars
	acSearchId  = "#" + field_id;
	acResultsId = "#" + results_id;
	acURL 		= get_url;

	// create the results div
	jQuery("body").append('<div id="' + results_id + '"></div>');

	// register mostly used vars
	acSearchField	= jQuery(acSearchId);
	acResultsDiv	= jQuery(acResultsId);

	// reposition div
	repositionResultsDiv();
	
	// on blur listener
	acSearchField.blur(function(){ setTimeout("clearAutoComplete()", 200) });

	// on key up listener
	acSearchField.keyup(function (e) {

		// get keyCode (window.event is for IE)
		var keyCode = e.keyCode || window.event.keyCode;
		var lastVal = acSearchField.val();

		// check an treat up and down arrows
		if(updownArrow(keyCode)){
			return;
		}

		// check for an ENTER or ESC
		if(keyCode == 13 || keyCode == 27){
			clearAutoComplete();
			return;
		}

		// if is text, call with delay
		setTimeout(function () {autoComplete(lastVal)}, acDelay);
	});
}

// treat the auto-complete action (delayed function)
function autoComplete(lastValue)
{
	// get the field value
	var part = acSearchField.val();

	// if it's empty clear the resuts box and return
	if(part == ''){
		clearAutoComplete();
		return;
	}

	// if it's equal the value from the time of the call, allow
	if(lastValue != part){
		return;
	}

	// get remote data as JSON
	jQuery.getJSON(acURL + part, function(json){

		// get the total of results
		var ansLength = acListTotal = json.length;

		// if there are results populate the results div
		if(ansLength > 0){

			var newData = '';

			// create a div for each result
			for(i=0; i < ansLength; i++) {
				newData += '<div class="unselected">' + json[i] + '</div>';
			}

			// update the results div
			acResultsDiv.html(newData);
			acResultsDiv.css("display","block");
			
			// for all divs in results
			var divs = jQuery(acResultsId + " > div");
		
			// on mouse over clean previous selected and set a new one
			divs.mouseover( function() {
				divs.each(function(){ this.className = "unselected"; });
				this.className = "selected";
			})
		
			// on click copy the result text to the search field and hide
			divs.click( function() {
				acSearchField.val(this.childNodes[0].nodeValue);
				clearAutoComplete();
			});

		} else {
			clearAutoComplete();
		}
	});
}

// clear auto complete box
function clearAutoComplete()
{
	acResultsDiv.html('');
	acResultsDiv.css("display","none");
}

// reposition the results div accordingly to the search field
function repositionResultsDiv()
{
	// get the field position
	var sf_pos    = acSearchField.offset();
	var sf_top    = sf_pos.top;
	var sf_left   = sf_pos.left;

	// get the field size
	var sf_height = acSearchField.height();
	var sf_width  = acSearchField.width();

	// apply the css styles - optimized for Firefox
	acResultsDiv.css("position","absolute");
	acResultsDiv.css("left", sf_left - 2);
	acResultsDiv.css("top", sf_top + sf_height + 5);
	acResultsDiv.css("width", sf_width - 2);
}


// treat up and down key strokes defining the next selected element
function updownArrow(keyCode) {
	if(keyCode == 40 || keyCode == 38){

		if(keyCode == 38){ // keyUp
			if(acListCurrent == 0 || acListCurrent == -1){
				acListCurrent = acListTotal-1;
			}else{
				acListCurrent--;
			}
		} else { // keyDown
			if(acListCurrent == acListTotal-1){
				acListCurrent = 0;
			}else {
				acListCurrent++;
			}
		}

		// loop through each result div applying the correct style
		acResultsDiv.children().each(function(i){
			if(i == acListCurrent){
				acSearchField.val(this.childNodes[0].nodeValue);
				this.className = "selected";
			} else {
				this.className = "unselected";
			}
		});

		return true;
	} else {
		// reset
		acListCurrent = -1;
		return false;
	}
}



// This is for the types field on the upload page
// global variables
var acaListTotal   =  0;
var acaListCurrent = -1;
var acaDelay		  = 000;
var acaURL		  = null;
var acaSearchId	  = null;
var acaResultsId	  = null;
var acaSearchField = null;
var acaResultsDiv  = null;

function setAutoCompleter(field_id, results_id, get_url){

	// initialize vars
	acaSearchId  = "#" + field_id;
	acaResultsId = "#" + results_id;
	acaURL 		= get_url;

	// create the results div
	$("body").append('<div id="' + results_id + '"></div>');

	// register mostly used vars
	acaSearchField	= $(acaSearchId);
	acaResultsDiv	= $(acaResultsId);

	// reposition div
	repositionResultsDiva();
	
	// on blur listener
	acaSearchField.blur(function(){ setTimeout("clearAutoCompleter()", 200) });

	// on key up listener
	acaSearchField.keyup(function (e) {

		// get keyCode (window.event is for IE)
		var keyCodea = e.keyCode || window.event.keyCode;
		var lastVala = acaSearchField.val();

		// check an treat up and down arrows
		if(updownArrowa(keyCodea)){
			return;
		}

		// check for an ENTER or ESC
		if(keyCodea == 13 || keyCodea == 27){
			clearAutoCompleter();
			return;
		}

		// if is text, call with delay
		setTimeout(function () {autoCompleter(lastVala)}, acaDelay);
	});
}

// treat the auto-complete action (delayed function)
function autoCompleter(lastValue)
{
	// get the field value
	var parta = acaSearchField.val();

	// if it's empty clear the resuts box and return
	if(parta == ''){
		clearAutoCompleter();
		return;
	}

	// if it's equal the value from the time of the call, allow
	if(lastValue != parta){
		return;
	}

	// get remote data as JSON
	$.getJSON(acaURL + parta, function(json){

		// get the total of results
		var ansLengtha = acaListTotal = json.length;

		// if there are results populate the results div
		if(ansLengtha > 0){

			var newDataa = '';

			// create a div for each result
			for(i=0; i < ansLengtha; i++) {
				newDataa += '<div class="unselected">' + json[i] + '</div>';
			}

			// update the results div
			acaResultsDiv.html(newDataa);
			acaResultsDiv.css("display","block");
			
			// for all divs in results
			var divsa = $(acaResultsId + " > div");
		
			// on mouse over clean previous selected and set a new one
			divsa.mouseover( function() {
				divsa.each(function(){ this.className = "unselected"; });
				this.className = "selected";
			})
		
			// on click copy the result text to the search field and hide
			divsa.click( function() {
				acaSearchField.val(this.childNodes[0].nodeValue);
				clearAutoCompleter();
			});

		} else {
			clearAutoCompleter();
		}
	});
}

// clear auto complete box
function clearAutoCompleter()
{
	acaResultsDiv.html('');
	acaResultsDiv.css("display","none");
}

// reposition the results div accordingly to the search field
function repositionResultsDiva()
{
	// get the field position
	var sf_pos    = acaSearchField.offset();
	var sf_top    = sf_pos.top;
	var sf_left   = sf_pos.left;

	// get the field size
	var sf_height = acaSearchField.height();
	var sf_width  = acaSearchField.width();

	// apply the css styles - optimized for Firefox
	acaResultsDiv.css("position","absolute");
	acaResultsDiv.css("left", sf_left - 2);
	acaResultsDiv.css("top", sf_top + sf_height + 5);
	acaResultsDiv.css("width", sf_width - 2);
}


// treat up and down key strokes defining the next selected element
function updownArrowa(keyCodea) {
	if(keyCodea == 40 || keyCodea == 38){

		if(keyCodea == 38){ // keyUp
			if(acaListCurrent == 0 || acaListCurrent == -1){
				acaListCurrent = acaListTotal-1;
			}else{
				acaListCurrent--;
			}
		} else { // keyDown
			if(acaListCurrent == acaListTotal-1){
				acaListCurrent = 0;
			}else {
				acaListCurrent++;
			}
		}

		// loop through each result div applying the correct style
		acaResultsDiv.children().each(function(i){
			if(i == acaListCurrent){
				acaSearchField.val(this.childNodes[0].nodeValue);
				this.className = "selected";
			} else {
				this.className = "unselected";
			}
		});

		return true;
	} else {
		// reset
		acaListCurrent = -1;
		return false;
	}
}

// This is for the operator field on the upload page
// global variables
var acbListTotal   =  0;
var acbListCurrent = -1;
var acbDelay		  = 000;
var acbURL		  = null;
var acbSearchId	  = null;
var acbResultsId	  = null;
var acbSearchField = null;
var acbResultsDiv  = null;

function setAutoCompleterb(field_id, results_id, get_url){

	// initialize vars
	acbSearchId  = "#" + field_id;
	acbResultsId = "#" + results_id;
	acbURL 		= get_url;

	// create the results div
	$("body").append('<div id="' + results_id + '"></div>');

	// register mostly used vars
	acbSearchField	= $(acbSearchId);
	acbResultsDiv	= $(acbResultsId);

	// reposition div
	repositionResultsDivb();
	
	// on blur listener
	acbSearchField.blur(function(){ setTimeout("clearAutoCompleterb()", 200) });

	// on key up listener
	acbSearchField.keyup(function (e) {

		// get keyCode (window.event is for IE)
		var keyCodeb = e.keyCode || window.event.keyCode;
		var lastValb = acbSearchField.val();

		// check an treat up and down arrows
		if(updownArrowb(keyCodeb)){
			return;
		}

		// check for an ENTER or ESC
		if(keyCodeb == 13 || keyCodeb == 27){
			clearAutoCompleterb();
			return;
		}

		// if is text, call with delay
		setTimeout(function () {autoCompleterb(lastValb)}, acbDelay);
	});
}

// treat the auto-complete action (delayed function)
function autoCompleterb(lastValue)
{
	// get the field value
	var partb = acbSearchField.val();

	// if it's empty clear the resuts box and return
	if(partb == ''){
		clearAutoCompleterb();
		return;
	}

	// if it's equal the value from the time of the call, allow
	if(lastValue != partb){
		return;
	}

	// get remote data as JSON
	$.getJSON(acbURL + partb, function(json){

		// get the total of results
		var ansLengthb = acbListTotal = json.length;

		// if there are results populate the results div
		if(ansLengthb > 0){

			var newDatab = '';

			// create a div for each result
			for(i=0; i < ansLengthb; i++) {
				newDatab += '<div class="unselected">' + json[i] + '</div>';
			}

			// update the results div
			acbResultsDiv.html(newDatab);
			acbResultsDiv.css("display","block");
			
			// for all divs in results
			var divsb = $(acbResultsId + " > div");
		
			// on mouse over clean previous selected and set a new one
			divsb.mouseover( function() {
				divsb.each(function(){ this.className = "unselected"; });
				this.className = "selected";
			})
		
			// on click copy the result text to the search field and hide
			divsb.click( function() {
				acbSearchField.val(this.childNodes[0].nodeValue);
				clearAutoCompleterb();
			});

		} else {
			clearAutoCompleterb();
		}
	});
}

// clear auto complete box
function clearAutoCompleterb()
{
	acbResultsDiv.html('');
	acbResultsDiv.css("display","none");
}

// reposition the results div accordingly to the search field
function repositionResultsDivb()
{
	// get the field position
	var sf_pos    = acbSearchField.offset();
	var sf_top    = sf_pos.top;
	var sf_left   = sf_pos.left;

	// get the field size
	var sf_height = acbSearchField.height();
	var sf_width  = acbSearchField.width();

	// apply the css styles - optimized for Firefox
	acbResultsDiv.css("position","absolute");
	acbResultsDiv.css("left", sf_left - 2);
	acbResultsDiv.css("top", sf_top + sf_height + 5);
	acbResultsDiv.css("width", sf_width - 2);
}


// treat up and down key strokes defining the next selected element
function updownArrowb(keyCodeb) {
	if(keyCodeb == 40 || keyCodeb == 38){

		if(keyCodeb == 38){ // keyUp
			if(acbListCurrent == 0 || acbListCurrent == -1){
				acbListCurrent = acbListTotal-1;
			}else{
				acbListCurrent--;
			}
		} else { // keyDown
			if(acbListCurrent == acbListTotal-1){
				acbListCurrent = 0;
			}else {
				acbListCurrent++;
			}
		}

		// loop through each result div applying the correct style
		acbResultsDiv.children().each(function(i){
			if(i == acbListCurrent){
				acbSearchField.val(this.childNodes[0].nodeValue);
				this.className = "selected";
			} else {
				this.className = "unselected";
			}
		});

		return true;
	} else {
		// reset
		acbListCurrent = -1;
		return false;
	}
}

// This is for the location field on the upload page
// global variables
var accListTotal   =  0;
var accListCurrent = -1;
var accDelay		  = 000;
var accURL		  = null;
var accSearchId	  = null;
var accResultsId	  = null;
var accSearchField = null;
var accResultsDiv  = null;

function setAutoCompleterc(field_id, results_id, get_url){

	// initialize vars
	accSearchId  = "#" + field_id;
	accResultsId = "#" + results_id;
	accURL 		= get_url;

	// create the results div
	$("body").append('<div id="' + results_id + '"></div>');

	// register mostly used vars
	accSearchField	= $(accSearchId);
	accResultsDiv	= $(accResultsId);

	// reposition div
	repositionResultsDivc();
	
	// on blur listener
	accSearchField.blur(function(){ setTimeout("clearAutoCompleterc()", 200) });

	// on key up listener
	accSearchField.keyup(function (e) {

		// get keyCode (window.event is for IE)
		var keyCodec = e.keyCode || window.event.keyCode;
		var lastValc = accSearchField.val();
		
		// check an treat up and down arrows
		if(updownArrowc(keyCodec)){
			return;
		}

		// check for an ENTER or ESC
		if(keyCodec == 13 || keyCodec == 27){
			clearAutoCompleterc();
			return;
		}

		// if is text, call with delay
		setTimeout(function () {autoCompleterc(lastValc)}, accDelay);
	});
}

// treat the auto-complete action (delayed function)
function autoCompleterc(lastValue)
{
	// get the field value
	var partc = accSearchField.val();

	// if it's empty clear the resuts box and return
	if(partc == ''){
		clearAutoCompleterc();
		return;
	}

	// if it's equal the value from the time of the call, allow
	if(lastValue != partc){
		return;
	}

	// get remote data as JSON
	$.getJSON(accURL + partc, function(json){

		// get the total of results
		var ansLengthc = accListTotal = json.length;

		// if there are results populate the results div
		if(ansLengthc > 0){

			var newDatac = '';

			// create a div for each result
			for(i=0; i < ansLengthc; i++) {
				newDatac += '<div class="unselected">' + json[i] + '</div>';
			}

			// update the results div
			accResultsDiv.html(newDatac);
			accResultsDiv.css("display","block");
			
			// for all divs in results
			var divsc = $(accResultsId + " > div");
		
			// on mouse over clean previous selected and set a new one
			divsc.mouseover( function() {
				divsc.each(function(){ this.className = "unselected"; });
				this.className = "selected";
			})
		
			// on click copy the result text to the search field and hide
			divsc.click( function() {
				accSearchField.val(this.childNodes[0].nodeValue);
				clearAutoCompleterc();
			});

		} else {
			clearAutoCompleterc();
		}
	});
}

// clear auto complete box
function clearAutoCompleterc()
{
	accResultsDiv.html('');
	accResultsDiv.css("display","none");
}

// reposition the results div accordingly to the search field
function repositionResultsDivc()
{
	// get the field position
	var sf_pos    = accSearchField.offset();
	var sf_top    = sf_pos.top;
	var sf_left   = sf_pos.left;

	// get the field size
	var sf_height = accSearchField.height();
	var sf_width  = accSearchField.width();

	// apply the css styles - optimized for Firefox
	accResultsDiv.css("position","absolute");
	accResultsDiv.css("left", sf_left - 2);
	accResultsDiv.css("top", sf_top + sf_height + 5);
	accResultsDiv.css("width", sf_width - 2);
}


// treat up and down key strokes defining the next selected element
function updownArrowc(keyCodec) {
	if(keyCodec == 40 || keyCodec == 38){

		if(keyCodec == 38){ // keyUp
			if(accListCurrent == 0 || accListCurrent == -1){
				accListCurrent = accListTotal-1;
			}else{
				accListCurrent--;
			}
		} else { // keyDown
			if(accListCurrent == accListTotal-1){
				accListCurrent = 0;
			}else {
				accListCurrent++;
			}
		}

		// loop through each result div applying the correct style
		accResultsDiv.children().each(function(i){
			if(i == accListCurrent){
				accSearchField.val(this.childNodes[0].nodeValue);
				this.className = "selected";
			} else {
				this.className = "unselected";
			}
		});

		return true;
	} else {
		// reset
		accListCurrent = -1;
		return false;
	}
}

// This is for the location field on the upload page
// global variables
var acdListTotal   =  0;
var acdListCurrent = -1;
var acdDelay		  = 000;
var acdURL		  = null;
var acdSearchId	  = null;
var acdResultsId	  = null;
var acdSearchField = null;
var acdResultsDiv  = null;

function setAutoCompleterd(field_id, results_id, get_url){

	// initialize vars
	acdSearchId  = "#" + field_id;
	acdResultsId = "#" + results_id;
	acdURL 		= get_url;

	// create the results div
	$("body").append('<div id="' + results_id + '"></div>');

	// register mostly used vars
	acdSearchField	= $(acdSearchId);
	acdResultsDiv	= $(acdResultsId);

	// reposition div
	repositionResultsDivd();
	
	// on blur listener
	acdSearchField.blur(function(){ setTimeout("clearAutoCompleterd()", 200) });

	// on key up listener
	acdSearchField.keyup(function (e) {

		// get keyCode (window.event is for IE)
		var keyCoded = e.keyCode || window.event.keyCode;
		var lastVald = acdSearchField.val();

		// check an treat up and down arrows
		if(updownArrowd(keyCoded)){
			return;
		}

		// check for an ENTER or ESC
		if(keyCoded == 13 || keyCoded == 27){
			clearAutoCompleterd();
			return;
		}

		// if is text, call with delay
		setTimeout(function () {autoCompleterd(lastVald)}, acdDelay);
	});
}

// treat the auto-complete action (delayed function)
function autoCompleterd(lastValue)
{
	// get the field value
	var partd = acdSearchField.val();

	// if it's empty clear the resuts box and return
	if(partd == ''){
		clearAutoCompleterd();
		return;
	}

	// if it's equal the value from the time of the call, allow
	if(lastValue != partd){
		return;
	}

	// get remote data as JSON
	$.getJSON(acdURL + partd, function(json){

		// get the total of results
		var ansLengthd = acdListTotal = json.length;

		// if there are results populate the results div
		if(ansLengthd > 0){

			var newDatad = '';

			// create a div for each result
			for(i=0; i < ansLengthd; i++) {
				newDatad += '<div class="unselected">' + json[i] + '</div>';
			}

			// update the results div
			acdResultsDiv.html(newDatad);
			acdResultsDiv.css("display","block");
			
			// for all divs in results
			var divsd = $(acdResultsId + " > div");
		
			// on mouse over clean previous selected and set a new one
			divsd.mouseover( function() {
				divsd.each(function(){ this.className = "unselected"; });
				this.className = "selected";
			})
		
			// on click copy the result text to the search field and hide
			divsd.click( function() {
				acdSearchField.val(this.childNodes[0].nodeValue);
				clearAutoCompleterd();
			});

		} else {
			clearAutoCompleterd();
		}
	});
}

// clear auto complete box
function clearAutoCompleterd()
{
	acdResultsDiv.html('');
	acdResultsDiv.css("display","none");
}

// reposition the results div accordingly to the search field
function repositionResultsDivd()
{
	// get the field position
	var sf_pos    = acdSearchField.offset();
	var sf_top    = sf_pos.top;
	var sf_left   = sf_pos.left;

	// get the field size
	var sf_height = acdSearchField.height();
	var sf_width  = acdSearchField.width();

	// apply the css styles - optimized for Firefox
	acdResultsDiv.css("position","absolute");
	acdResultsDiv.css("left", sf_left - 2);
	acdResultsDiv.css("top", sf_top + sf_height + 5);
	acdResultsDiv.css("width", sf_width - 2);
}


// treat up and down key strokes defining the next selected element
function updownArrowd(keyCoded) {
	if(keyCoded == 40 || keyCoded == 38){

		if(keyCoded == 38){ // keyUp
			if(acdListCurrent == 0 || acdListCurrent == -1){
				acdListCurrent = acdListTotal-1;
			}else{
				acdListCurrent--;
			}
		} else { // keyDown
			if(acdListCurrent == acdListTotal-1){
				acdListCurrent = 0;
			}else {
				acdListCurrent++;
			}
		}

		// loop through each result div applying the correct style
		acdResultsDiv.children().each(function(i){
			if(i == acdListCurrent){
				acdSearchField.val(this.childNodes[0].nodeValue);
				this.className = "selected";
			} else {
				this.className = "unselected";
			}
		});

		return true;
	} else {
		// reset
		acdListCurrent = -1;
		return false;
	}
}
