/********************************************
	  fooBar v2.x xajax common functions
*********************************************/
// mooTools required

function clearOptionList(selectId){
	$(selectId).empty();
}

function pauseComp(millis){
	date = new Date();
	var curDate = null;
	do { var curDate = new Date(); }
	while(curDate-date < millis);
} 

function addOption_old(selectId, txt, val, defSel){
	// if Safari/Konqueror
	if(window.webkit) {	
		var element = $(selectId);
		var newone = new Option(txt,val,defSel);
		element.add(newone,element.options[element.options.length]);
	// if Internet Explorer (any)
	} else if(window.ie) {
		var objOption = new Option(txt, val, defSel, defSel);
		$(selectId).options.add(objOption);
	// if Mozilla/Gecko/Opera
	} else {
		var objOption = new Option(txt, val, defSel);
		$(selectId).options.add(objOption);
	}
}

function addOption(selectId, txt, val, defSel){
	var e = new Element('option',{'value':val,'title':txt}).injectInside($(selectId)).setHTML(txt);
	if(defSel) e.setProperty('selected','selected');
}

function deleteOption(id,i){ 
	var items = $(id).length;
	if(items>0){
		$(id).options[i] = null;
	}
}

function deleteSelectedOptions(id){
	var items = $(id).length;
	var i; for(i=items-1; i>=0; i--){
		if($(id).options[i].selected){
			deleteOption(id, i);
		}
	}
}

function moveOptions(from,to){
	var items = $(from).length;
	var txt; var val; var a = new Array();
	for (i=0; i<items; i++){
		if($(from).options[i].selected){
			txt = $(from).options[i].text;
			val = $(from).options[i].value;
			a[i] = $(from).options[i];
			addOption(to, txt, val, false);
		}
	}
	deleteSelectedOptions(from);
	sortOptions(to);
}

function moveAllOptions(from,to){
	var items = $(from).length;
	var txt; var val;
	for (i=0; i<items; i++){
		txt = $(from).options[i].text;
		val = $(from).options[i].value;
		addOption(to, txt, val, false);
		
	}
	sortOptions(to);
	clearOptionList(from);
}

function compareOptionText(a,b){
  /*
   * return >0 if a>b
   *         0 if a=b
   *        <0 if a<b
   */
  // textual comparison
  return a.text!=b.text ? a.text<b.text ? -1 : 1 : 0;
  // numerical comparison
//  return a.text - b.text;
}

function sortOptions(id){
	var items = $(id).length;
	var list = $(id);
	// create array and make copies of options in list
	var tmpArray = new Array(items);
	for ( i=0; i<items; i++ ){
		tmpArray[i] = new Option(list.options[i].text,list.options[i].value);
	}
	// sort options using given function
	tmpArray.sort(compareOptionText);
	// make copies of sorted options back to list
	for ( i=0; i<items; i++ ) {
		list.options[i] = new Option(tmpArray[i].text,tmpArray[i].value);
	}
}

function selectAllOptions(id){
	var items = $(id).length;
	for (i=0; i<items; i++){
		$(id).options[i].selected = true;
	}
}

/* base64_encode && base64_decode */

//First things first, set up our array that we are going to use.
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + //all caps
"abcdefghijklmnopqrstuvwxyz" + //all lowercase
"0123456789+/="; // all numbers plus +/=
//Heres the encode function
function base64_encode(inp){
	var out = ""; //This is the output
	var chr1, chr2, chr3 = ""; //These are the 3 bytes to be encoded
	var enc1, enc2, enc3, enc4 = ""; //These are the 4 encoded bytes
	var i = 0; //Position counter
	do { //Set up the loop here
	chr1 = inp.charCodeAt(i++); //Grab the first byte
	chr2 = inp.charCodeAt(i++); //Grab the second byte
	chr3 = inp.charCodeAt(i++); //Grab the third byte
	//Here is the actual base64 encode part.
	//There really is only one way to do it.
	enc1 = chr1 >> 2;
	enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	enc4 = chr3 & 63;
	
	if (isNaN(chr2)) {
	enc3 = enc4 = 64;
	} else if (isNaN(chr3)) {
	enc4 = 64;
	}
	//Lets spit out the 4 encoded bytes
	out = out + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) +
	keyStr.charAt(enc4);
	// OK, now clean out the variables used.
	chr1 = chr2 = chr3 = "";
	enc1 = enc2 = enc3 = enc4 = "";
	} while (i < inp.length); //And finish off the loop
	//Now return the encoded values.
	return out;
}
//Heres the decode function
function base64_decode(inp){
	var out = ""; //This is the output
	var chr1, chr2, chr3 = ""; //These are the 3 decoded bytes
	var enc1, enc2, enc3, enc4 = ""; //These are the 4 bytes to be decoded
	var i = 0; //Position counter
	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	var base64test = /[^A-Za-z0-9\+\/\=]/g;
	if (base64test.exec(inp)) { //Do some error checking
	alert("There were invalid base64 characters in the input text.\n" +
	"Valid base64 characters are A-Z, a-z, 0-9, ?+?, ?/?, and ?=?\n" +
	"Expect errors in decoding.");
	}
	inp = inp.replace(/[^A-Za-z0-9\+\/\=]/g, "");
	do { //Hereâ€™s the decode loop.
	//Grab 4 bytes of encoded content.
	enc1 = keyStr.indexOf(inp.charAt(i++));
	enc2 = keyStr.indexOf(inp.charAt(i++));
	enc3 = keyStr.indexOf(inp.charAt(i++));
	enc4 = keyStr.indexOf(inp.charAt(i++));
	//Heres the decode part. Thereâ€™s really only one way to do it.
	chr1 = (enc1 << 2) | (enc2 >> 4);
	chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
	chr3 = ((enc3 & 3) << 6) | enc4;
	//Start to output decoded content
	out = out + String.fromCharCode(chr1);
	if (enc3 != 64) {
	out = out + String.fromCharCode(chr2);
	}
	if (enc4 != 64) {
	out = out + String.fromCharCode(chr3);
	}
	//now clean out the variables used
	chr1 = chr2 = chr3 = "";
	enc1 = enc2 = enc3 = enc4 = "";
	} while (i < inp.length); //finish off the loop
	//Now return the decoded values.
	return out;
}
