// JavaScript Document

var errorColor = '#ffaaaa';
var okColor = '#eeeeee';

function validateForm() {

  var i,p,q,nm,restr,minChars,errors='',args=validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) {
	//START FOR
	val=findObj(args[i]);	
	minChars=args[i+1];
  	restr=args[i+2];
	try{
		if (val) {
			nm=val.id;
			if ((vl=val.value)!="") {
				if (restr.indexOf('isEmail')!=-1) {
					p=vl.indexOf('@');
					q=vl.indexOf('.');
					if (p<1 || p==(length-1) || q<1 || q==(length-1)) {
						errors+=' - Field \"'+nm+'\" must contain a valid email address.\n';
						val.style.backgroundColor=errorColor;
					}
				}else if (restr.indexOf('isNum')!=-1) {
					p=isNaN(vl);
					if (p){
						errors+=' - Field\"'+nm+'\" must be numeric.\n';
						val.style.backgroundColor=errorColor;
					}
				}else if (vl.length<minChars){
					errors+=' - Field \"'+nm+'\" can\'t have less then '+minChars+' chars .\n';
					val.style.backgroundColor=errorColor;
				}
			}else if (restr.charAt(0) == 'R') {
				errors += ' - Field \"'+nm+'\" is mandatory.\n'; 
				val.style.backgroundColor=errorColor;			
			}
		}
	}catch(e){
		//FOR OPTIONLISTS...
		var lstObjs,k,j,tmpSel=false;
		for(k=0;k<document.forms.length;k++){
			if(lstObjs=document.forms[k].elements[args[i]]) break;
		}		
		for(j=0;j<lstObjs.length;j++){
			tmpSel=tmpSel||lstObjs[j].checked;
		} 
		if(!tmpSel){
			errors += ' - You must choose one of : \"'+lstObjs[0].id+'\".\n';
			lstObjs[0].style.backgroundColor=errorColor;
		}else{
			lstObjs[0].style.backgroundColor=okColor;
		}
	}
	
	if(!(errors.indexOf(nm)>=0)){
		if (val){ if (val.style) {val.style.backgroundColor=okColor;}}		
	}
	//END FOR
  }
  if (errors) alert('The following errors have occured:\n'+errors);

  document.returnValue = (errors == '');
}

function requireAtLeastOne(){
	var args=requireAtLeastOne.arguments;
	var searchStuff="";
	for (i=1; i<args.length; i++) {
		searchStuff+=document.forms[args[0]][args[i]].value;
	}
	if (searchStuff==""){
		for (i=1; i<args.length; i++) {
			document.forms[args[0]][args[i]].style.backgroundColor=errorColor;
		}
		alert("You must enter some text.");		
		document.returnValue=false;
	}else{
		for (i=1; i<args.length; i++) {
			document.forms[args[0]][args[i]].style.backgroundColor=okColor;
		}
		document.returnValue=true;
	}
}

function comparePasswords(pass1,pass2){
	  if (findObj(pass1).value!=findObj(pass2).value){
			alert("Passwords do not match.");
			return false;
			}else return true;		
}
