function setfocus(){
	var bFound = false;

	if(document.forms == null)
		return;
		
	// for each form
	for (f = 0; f < document.forms.length; f++) {
	    // for each element in each form
	    for (i = 0; i < document.forms[f].length; i++) {
			try{
				document.forms[f][i].focus();
				var bFound = true;
			}
			catch(e){}
			
			// if found in this element, stop looking
			if (bFound == true)
			    break;
		}
		// if found in this form, stop looking
		if (bFound == true)
			break;				
	}
}

function limitText(limitField, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	}
}

function dosort(n){

	var sort = geturlparam('sort');
	var sortdir = geturlparam('sortdir');
	
	if(sort == null || sort != n){
		sort = n;
		sortdir = 'a';
	}
	else{
		sortdir = (sortdir == 'a' ? 'd' : 'a');
	}
	
	var newlocation = window.location.href;
	newlocation = addurlparam(newlocation, 'sort', sort);
	newlocation = addurlparam(newlocation, 'sortdir', sortdir);
		
	window.location = newlocation;	
}

function dopage(n){
	
	var page = n;
	var newlocation = window.location.href;
	newlocation = addurlparam(newlocation, 'pge', page);
		
	window.location = newlocation;
}

function geturlparam(param){
	
	var re = new RegExp(param+'=([^&]*)');
	var match = window.location.href.match(re);
	if(match == null)
		return null;
	else
		return match[1];
}

function addurlparam(url, pname, pvalue){

	var newurl;
	if(url.indexOf(pname+'=') == -1){
		newurl = url;
		newurl += (url.indexOf('?') != -1 ? '&' : '?');
		newurl += (pname+'='+pvalue);
	}
	else{
		var re = new RegExp(pname + '=[^&]*');
		newurl = url.replace(re, pname+'='+pvalue);
	}

	return newurl;
}
