/* HISTORY
	GRK Created 5/10/10 VERSION 0.0 jsListFunctions-0.0.js
	GRK Updated 5/11/10 added more functions VERSIN 0.1 jsListFunctions-0.1.js
*/
/* LIST OF FUNCTIONS
	jsListAppend(list,listItem); // Add listItem to the end of a list
	jsListDeleteItem(list,listItem); // Delete matching listItem from a list
	*** update 5/11 ***
	jsListDeleteAt(list,index); // Delete item at index position from a list (zero based)
	jsListFind(list,listItem); // Find listItem in a list, return the index (zero based)
	jsListInsertAt(list,listItem,index); // Insert listItem into list at the index position
	jsListLen(list); // Return length of a list
	jsListPrepend(list,listItem); // Add listItem to the beginning of a list
	jsListReplaceAt(list,listItem,index); // Replace the item at index position with listItem
	jsListSort(list,type); // Sort a list, ascending; type = "alpha" OR "numeric"
*/
function jsListAppend(list,listItem) {
var tmpIn = new Array();
var tmpOut = new String();
	tmpIn = list.split(","); // split list into array
	tmpIn.push(listItem); // add new item to array
	tmpOut = tmpIn.join(","); // break array into comma list
	tmpOut = cleanCommas(tmpOut);
	return tmpOut;
}
function jsListPrepend(list,listItem) {
var tmpIn = new Array();
var tmpOut = new String();
	tmpIn = list.split(","); // split list into array
	tmpIn.splice(0,0,listItem); // add new item to array
	tmpOut = tmpIn.join(","); // break array into comma list
	// NOT NEEDED tmpOut = cleanCommas(tmpOut);
	return tmpOut;
}
function jsListInsertAt(list,listItem,index) {
var tmpIn = new Array();
var tmpOut = new String();
	tmpIn = list.split(","); // split list into array
	tmpIn.splice(index,0,listItem); // add new item to array
	tmpOut = tmpIn.join(","); // break array into comma list
	// NOT NEEDED tmpOut = cleanCommas(tmpOut);
	return tmpOut;
}
function jsListReplaceAt(list,listItem,index) {
var tmpIn = new Array();
var tmpOut = new String();
	tmpIn = list.split(","); // split list into array
	tmpIn.splice(index,1,listItem); // add new item to array
	tmpOut = tmpIn.join(","); // break array into comma list
	// NOT NEEDED tmpOut = cleanCommas(tmpOut);
	return tmpOut;
}
function jsListFind(list,listItem) {
var tmpIn = new Array();
var tmpOut = new Number(-1);
	tmpIn = list.split(","); // split list into array
	for(x in tmpIn) { // for each item in array
		if(tmpIn[x] == listItem) {
			tmpOut = x; // if match, return at
			break;
		}
	}
	return tmpOut;
}
function jsListDeleteItem(list,listItem) {
var tmpIn = new Array();
var tmpOut = new String();
	tmpIn = list.split(","); // split list into array
	for(x in tmpIn) { // for each item in array
		if((tmpIn[x] == listItem || tmpIn[x] == 0)) tmpIn.splice(x,1); // if match, delete at
	}
	tmpOut = tmpIn.join(","); // break array into comma list
	// NOT NEEDED tmpOut = cleanCommas(tmpOut);
	return tmpOut;
}
function jsListDeleteAt(list,index) {
var tmpIn = new Array();
var tmpOut = new String();
	tmpIn = list.split(","); // split list into array
	tmpIn.splice(index,1); // delete at
	tmpOut = tmpIn.join(","); // break array into comma list
	// NOT NEEDED tmpOut = cleanCommas(tmpOut);
	return tmpOut;
}
function jsListLen(list) {
var tmpIn = new Array();
var tmpOut = new String();
	tmpIn = list.split(","); // split list into array
	tmpOut = tmpIn.length;
	return tmpOut;
}
function jsListSort(list,type) {
var tmpIn = new Array();
var tmpOut = new String();
	tmpIn = list.split(","); // split list into array
	switch(type) {
		case "alpha" : tmpIn = tmpIn.sort(); break;
		case "numeric" : tmpIn = tmpIn.sort(sortNumber); break;		
	}
	tmpOut = tmpIn.join(","); // break array into comma list
	return tmpOut;
}
function sortNumber(a,b) {
	return a - b;
}
function cleanCommas(listIn) {
var listOut = new String();
	listOut = listIn.replace(/^,+|,+$/g,""); // rem leading & trailing commas
	// NOT NEEDED listOut = listOut.replace(/(,)+/g,","); // rem central double commas
	return listOut;
}
