function listappend(thelist, newval, delim){
	if (newval == ""){
		newval = " ";
	}
	if (! delim){
		delim = ",";
	}

	if (thelist == ""){
		newstring = newval;
	}else{
		newstring = thelist + delim + newval;
	}
	return newstring;
}

function listgetat(thelist, theindex, delim){
	if (! delim){
		delim = ",";
	}

	currpos = 1;
	start = 0;
	end = -1;
	if (theindex == 1){
		setendflag = 1;
	}else{
		setendflag = 0;
	}
	for (var x=1; x <= thelist.length; x++){
		if (thelist.substring(x,x+1) == delim){
			currpos ++;
			if(setendflag == 1){
				setendflag = 0;
				end = x;
			}
			if(currpos == theindex){
				start = x + 1;
				end = thelist.length;
				setendflag = 1;
			}
		}
	}
	if (start == 0 && end == -1){
		returnstring = "";
	}else{
		returnstring = thelist.substring(start,end);
	}
//	alert("thelist = " + thelist)
//	alert("theindex = " + theindex)
//	alert("returnstring = "+ returnstring)
//	alert("start = "+ start)
//	alert("end = "+ end)
	return returnstring;
}

function listdeleteat(thelist, listpos, delim){
	if (! delim){
		delim = ",";
	}
	l = listlen(thelist, delim);

	newlist = '';
	for (var x = 1; x <= l; x++){
		if (x != listpos){
			listelement = listgetat(thelist,x,delim);
			newlist = listappend(newlist,listelement,delim);
		}
	}
	return newlist;
}

function listlen(thelist, delim){
	if (! delim){
		delim = ",";
	}
	listcount = 1;
	for (var x=1; x <= thelist.length; x++){
		if (thelist.substring(x,x+1) == delim){
			listcount ++;
		}
	}
	return listcount;
}

function listcontains(thelist, thevalue, delim){
	if (! delim){
		delim = ",";
	}
	l = listlen(thelist, delim);
	listpos = -1;
	
	for (var x = 1; x < l; x++){
		listelement = listgetat(thelist,x,delim);
		if (listelement.indexOf(thevalue) >= 0){
			listpos = x;
			break;
		}
	}
	return listpos;
}

function listcontainsnocase(thelist, thevalue, delim){
	if (! delim){
		delim = ",";
	}
	l = listlen(thelist, delim);
	listpos = -1;
	uc = thevalue.toUpperCase();
	
	for (var x = 1; x < l; x++){
		listelement = listgetat(thelist,x,delim);
		listelement = listelement.toUpperCase();
		if (listelement.indexOf(uc) >= 0){
			listpos = x;
			break;
		}
	}
	return listpos;
}

function listfind(thelist, thevalue, delim){
	if (! delim){
		delim = ",";
	}
	l = listlen(thelist, delim);
	listpos = -1;
	
	for (var x = 1; x < l; x++){
		listelement = listgetat(thelist,x,delim);
		if (listelement == thevalue){
			listpos = x;
			break;
		}
	}
	return listpos;
}

function listfindnocase(thelist, thevalue, delim){
	if (! delim){
		delim = ",";
	}
	l = listlen(thelist, delim);
	listpos = -1;
	
	for (var x = 1; x < l; x++){
		listelement = listgetat(thelist,x,delim);
		if (listelement.toUpperCase() == thevalue.toUpperCase()){
			listpos = x;
			break;
		}
	}
	return listpos;
}

function listfirst(thelist, delim){
	return listgetat(thelist,1,delim);
}

function listlast(thelist, delim){
	return listgetat(thelist,listlen(thelist,delim),delim);
}

function listprepend(thelist, thevalue, delim){
	return listappend(thevalue, thelist, delim)
}

function listsetat(thelist, listpos, thevalue, delim){
	if (! delim){
		delim = ",";
	}
	l = listlen(thelist, delim);

	newlist = '';
	for (var x = 1; x <= l; x++){
		if (x != listpos){
			listelement = listgetat(thelist,x,delim);
			newlist = listappend(newlist,listelement,delim);
		}else{
			newlist = listappend(newlist,thevalue,delim);
		}
	}
	return newlist;
}

function listsort(thelist, delim){
	if (! delim){
		delim = ",";
	}
	l = listlen(thelist, delim);
	a = new Array(1);

	for (var z = 1; z <= l; z++){
		newl = a.length;
		a[newl] = listgetat(thelist, z, delim);
	}
	
	var v = '';
	var t = '';
	
	for (var x = 1; x < a.length; x++){
		for (var y = x; y < a.length; y++){
			string1 = a[x];
			string1 = string1.toUpperCase();
			string2 = a[y];
			string2 = string2.toUpperCase();
//			alert (string1 + " " + string2);
			if (string2 < string1){
				v = a[x];
				a[x] = a[y];
				a[y] = v;
			}
		}
	}
	
	
	newlist = "";
	for (var x = 1; x <= l; x++){
		newlist = listappend(newlist,a[x],delim);
	}
	return( newlist);
}
