// UI FUNCTIONS //

function text(f) {return document.form1.elements[f].value;}
function settext(f,x) {document.form1.elements[f].value=x;}
function cleantext(box) {box.value=clean(box.value);}

function checked(c)    {return (document.form1.elements[c]) ? document.form1.elements[c].checked : false;}
function setcheck(c,t) {if (document.form1.elements[c]) document.form1.elements[c].checked=t;}
function check(c)      {setcheck(c,true);}
function uncheck(c)    {setcheck(c,false);}
function clickcheck(c) {
	if (!document.form1.elements[c]) return;
	if(document.form1.elements[c].disabled) return;
	document.form1.elements[c].checked=!document.form1.elements[c].checked;
	if(document.form1.elements[c].onclick) document.form1.elements[c].onclick();
	}

function radio(r) {
	if (!document.form1.elements[r]) return false;
	for(x=0; x<document.form1.elements[r].length; x++)
		if(document.form1.elements[r][x].checked) return document.form1.elements[r][x].value;
	return false;
	}
function setradio(r,v) {
	if (!document.form1.elements[r]) return;
	for(x=0; x<document.form1.elements[r].length; x++) {
		if(document.form1.elements[r][x].value!=v) continue;
		document.form1.elements[r][x].checked=true;
		break;
		}
	}
function clickradio(r,num) {
	if (!document.form1.elements[r]) return;
	if(document.form1.elements[r][num].disabled) return;
	document.form1.elements[r][num].checked=true;
	if(document.form1.elements[r][num].onclick) document.form1.elements[r][num].onclick();
	}

function menu(m) {return document.form1.elements[m].options[document.form1.elements[m].selectedIndex].value;}
function setmenu(m,v) {
	for(x=0; x<document.form1.elements[m].options.length; x++) {
		if(document.form1.elements[m].options[x].value!=v) continue;
		document.form1.elements[m].selectedIndex=x;
		return;
		}
	}
function menui(m) {return document.form1.elements[m].selectedIndex;}
function setmenui(m,x) {document.form1.elements[m].selectedIndex=x;}

function setspan(s,x)   {document.getElementById(s).innerHTML=x;}

function setstyle(e,c)  {document.getElementById(e).className=c;}
function setstylef(e,c) {document.form1.elements[e].className=c;}
function getstyle(e)  {return document.getElementById(e).className;}
function getstylef(e) {return document.form1.elements(e).className;}

function selectf(e) {document.form1.elements[e].select();}


// STRING FUNCTIONS //

function clean(x) {  // for ALL input strip whitespace, disallow \
	if(!x) return '';
	f=String.fromCharCode(32,160,9,10,13); // white space
	while (f.indexOf(x.charAt(0))>=0 && x.length>0)
		x=x.substring(1,x.length);  // trim leading
	while (f.indexOf(x.charAt(x.length-1))>=0 && x.length>0)
		x=x.substring(0,x.length-1);  // trim trailing
	while (x.indexOf('\\')>=0)
	  x=x.substring(0,x.indexOf('\\'))+'/'+x.substring(x.indexOf('\\')+1,x.length);  // replace \ with /
	return x;
	}

function captext(box,maxacro,lowerok) {
	box.value=caps(clean(box.value),maxacro,lowerok);
	}
function caps(x,maxacro,lowerok) {  // changes all-caps or all-lower (unless lowerok) to title case
	if(x!=x.toUpperCase() && (x!=x.toLowerCase() || lowerok)) return x;
	if(x==x.toUpperCase() && x.length<=maxacro) return x;  // allow acronyms
	x=x.toLowerCase();
	f='abcdefghijklmnopqrstuvwxyz0123456789';
	y='';
	capnext=true;
	for(k=0; k<x.length; k++) {
		c=x.charAt(k);
		if(f.indexOf(c)<0) capnext=true;
		else if(capnext) {
			c=c.toUpperCase();
			capnext=false;
			}
		y += c;
		}
	return y;
	}
function caplink(l) {  // if email or web is all caps, change to lower; ok if mixed caps
	if(l==l.toUpperCase()) l=l.toLowerCase();
	return l;
	}

function changelink(e) {
	e.value=caplink(clean(e.value));
	}

function changeemail(e,limit) {
	x=e.value+' ';  // input
	a='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-';
	y='';  // final list
	z='';  // one address
	for(k=0; k<x.length; k++) {
		if(a.indexOf(x.charAt(k))>=0)
			z+=x.charAt(k);  // accumulate one address at a time
		else {
			if(z.indexOf('@')>=0) {
				z=caplink(z);
				if(y=='') y=z;
				else y+='\n'+z;
				if(limit) break;  // stop after one
				}
			z='';			
			}
		}
	e.value=y;
	}

function maxlength(box,chars) {  // limit number of characters in textarea
	box.value=box.value.substr(0,chars);
	}

function rep(s0,s1,string) {  // replace s0 with s1 in string
	while(true) {
		k=string.indexOf(s0);
		if(k<0) return string;
		string=string.substring(0,k)+s1+string.substring(k+s0.length,string.length);
		}
	}


// NAVIGATION //

function gopost(url) {
	if(url) document.form1.action=(url);
	document.form1.submit();
	}

function checkcookies() {  // if no cookies redirect to error page
	if(document.cookie) return;
	document.cookie='test=ok';
	if(document.cookie) return;
	location='nocookies.php';
	}
