/* Form Stuff */
function ltrim(s) { return s.replace( /^\s*/, "" ); }
function rtrim(s) { return s.replace( /\s*$/, "" ); }
function trim(s) { return rtrim(ltrim(s)); }
function putCursor(obj) { if(window.focus)obj.focus(); }
function putSelection(obj) { putCursor(obj); if(obj.select)obj.select(); }
function setCursor(field,value) { if(field.value==value) { field.value=""; } }
function checkCursor(field,value) { if(isBlank(field.value)) { field.value=value; } }

function checkedRadio(set) {
	checked=false;
	for(var n=0; n<set.length; n++) {
		if(set[n].checked) {
			checked=true;
			break;
		}
	}
	return checked;
}
function getRadioValue(set) {
	for(var n=0; n<set.length; n++) {
		if(set[n].checked) {
			return set[n].value;
		}
	}
	return "";
}
function getRadioIndex(set,value) {
	for(var n=0; n<set.length; n++) {
		if(set[n].value==value) {
			return n;
		}
	}
	return -1;
}
function getCheckedRadioIndex(set) {
	for(var n=0; n<set.length; n++) {
		if(set[n].checked) {
			return n;
		}
	}
	return -1;
}
function setRadioIndex(set,value,now) {
	for(var n=0; n<set.length; n++) {
		if(set[n].value.toLowerCase()==value.toLowerCase()) {
			set[n].checked=now;
			break;
		}
	}
}
function getIndexFromValue(menu,value) {
	for(var n=0; n<menu.options.length; n++) {
		if(menu.options[n].text==value || menu.options[n].value==value) {
			return n;
		}
	}
	return 0;
}
function isBlank(s) {
	if(s.length==0)return true;
	for(var i=0; i<s.length; i++) {
		if(s.charAt(i)!=" ")return false;
	}
	return true;
}
function isEmpty(field) {
	if(isBlank(field.value)) {
		str=(isEmpty.arguments[1])?arguments[1]:'Field';
		err(str + " cannot be left blank");
		field.value="";
		putCursor(field);
		return true;
	}
}
function isNumeric(str) {
	str=str+"";
	digits="0123456789";
	for (var i=0; i<str.length; i++) {
		if (digits.indexOf(str.charAt(i))==-1) {
			return false;
		}	
	}
	return true;
}
function isEmail(e) {
	email=e.value;

	//check if email field is blank
	if(isEmpty(e, "E-mail Address")) {
		return false;
	}
	//make sure the field has an '@' and a '.'
	if(email.indexOf('@')==-1 || email.indexOf('.')==-1) {
		err("Please supply a valid e-mail address");
		putSelection(e);
		return false;
	}
	//then make sure the '@' is before the last occurence of the '.'
	if (email.indexOf('@') > email.lastIndexOf('.')) {
		err("Please supply a valid e-mail address");
		putSelection(e);
		return false;
	}
	// make sure there's at least one char before the '@' symbol
	if (email.indexOf('@') < 1) {
		err("Please supply a valid e-mail address");
		putSelection(e);
		return false;
	}
	// check if there's at least 2 chars between the '@' and the '.'
	if ((email.lastIndexOf('.') - email.indexOf('@')) < 3) {
		err("Please supply a valid e-mail address");
		putSelection(e);
		return false;
	}
	// and make sure there are at least 2 chars between the '.' and the end
	if ((email.length - email.lastIndexOf('.')) < 3) {
		err("Please supply a valid e-mail address");
		putSelection(e);
		return false;
	}
	return true;
}

/* DHTML Stuff */
function getLayer(id) {
	if(document.getElementById) return document.getElementById(id);
    if(document.layers) return document.layers[id];
    if(document.all) return document.all[id];
    return null;
}
function getLyrObj(lyr) {
	if(document.getElementById) return document.getElementById(lyr).style;
	if(document.layers) return document.layers[lyr];
	if(document.all) return document.all[lyr].style;
	return null;
}
function swapClass(id,name) {
    lyr=getLayer(id);
    if(lyr!=null) { lyr.className=name; }
}
function toggle(lyr) {
	document[lyr+"_arw"].src=(document[lyr+"_arw"].src.indexOf("arw.gif")!=-1)?"images/arw-dwn.gif":"images/arw.gif";
	lyr=getLyrObj(lyr);
	lyr.display=(lyr.display!="none" && lyr.display!="")?"none":"block";
}
function write(lyr,html) {
	lyr=getLayer(lyr);
	if (document.layers) {
		with(lyr.document) { open(); write(html); close(); }
	} else {
		lyr.innerHTML=html;
	}
}

/* Regular Stuff */
function pop(file,w,h) {
	win=window.open(file,'win','width='+w+',height='+h+',menubar=yes,toolbar=no,scrollbars=yes,resizable=yes,location=no,status=no');
	if(window.resizeTo)win.resizeTo(w,h);
	if(win.focus)win.focus();
}
function getPath(loc) {
    loc=loc+"";
    normal=loc.lastIndexOf("/");
    reverse=loc.lastIndexOf("\\");
    return (reverse>normal)?loc.substring(0,reverse+1):loc.substring(0,normal+1);
}

function setStatus(now,url) {
	done=(navigator.appName=="Netscape")?"Document: Done":"Done";
	//window.status=(now)?getPath(window.location)+url:done;
	window.status=(now)?url:done;
	return true;
}

function swap(name,img,now) {
    if(document.images) { 
		st=(now)?'_on':'_off';
		document[name].src="images/"+img+st+".gif";
	}
}
