function ValidateForm(formName, fieldsToValidate, ErrorDiv){
	//this function expects the name of a form
	// as well as an array of the names of all the fields that need validation
	if(ErrorDiv != "none"){
		var ErrorDivName = ErrorDiv || 'FormError';
	}
	
	var fields= document.getElementById(formName).elements;
	var  i, field, error=0;
	
	var div = gE('display_custom'); //Checks to see if picker is included
	if(div && formName != "addPerson"){		//This is to check that if the picker is included, whether there are people in it
		error = 1;
		for(person in people){
			if(people[person].deleted != 1){		//For everyone in the picker, if they havent been deleted, dont error
				error = 0;
			}
		}
		if(error){
			document.getElementById(ErrorDivName).innerHTML="<p class='redtext'>You must add a person</p>";
			return false;
		}
	}

	for(i=0; i<fields.length; i++){
		field=fields[i];
		if(indexOf(fieldsToValidate, field.name) != -1){	
			if(field.type=="radio"){
				var buttons = document.getElementsByName(field.name);
			
				var radioerror = 1;
				for(choice = 0; choice < buttons.length; choice++){
					if(buttons[choice].checked){
						radioerror = 0;
					}
				}
				if(radioerror == 1){
					error = 1;
				}
				//XXX fix this
				//document.getElementById("FormError").innerHTML+="<span class='faded'>"+i+" "+field.name+" "+field.type+" "+field.value+"</span><br>";
				//checked=0;
				//for(choice in fields[field.name]){
				//	if(choice.checked)checked=1;
				//}
				//if(!checked){
				//	field.style.borderColor="red";
				//	error=1;
				//}
				//else{
				//	field.style.borderColor="";
				//}
			}
			
			else if(field.type.match(/select\-\w*/i)){
				if(field.value==""){
					field.style.borderColor="red";
					error=1;
				}
				else{
					field.style.borderColor="";
				}
			}
			
			else if(field.type=="checkbox"){
				//do nothing
				//The user doesn't necessarily have to answer something with
				//a checkbox.
				//if they do, it's case by case.
			}
			
			else if(field.type=="text" || field.type=="textarea" || field.type=="password"){
				if(field.value==""){
					field.style.borderColor="red";
					error=1;
				}
				else{
					field.style.borderColor="";
				}
			}
			
			else if(field.type=="hidden"){
				if(field.value==""){
					error=1;
				}
			}
			
			else if(field.type=="div" && field.wigetid){
				field.style.borderColor="red";
			}
			
		}//end if we should validate this field
	}//end for each field
	//only do this if there's an error div
	if(ErrorDivName){
		if(error){
			document.getElementById(ErrorDivName).innerHTML="<p class='redtext'>You must fill in all required fields to continue.</p>";
		}
		else{
			document.getElementById(ErrorDivName).innerHTML="";
		}
	}
	if(error){
		return false;
	}
	else{
		return true;
	}
}

function indexOf(ValidateFields, fieldname){
	var pos;
	for(field in ValidateFields){
		pos++;
		if(fieldname == ValidateFields[field]){
			return pos;
		}
	}
	return -1;
}


function checkComplete(formName, fieldsToValidate, fieldIdToMarkComplete){
	var fields= document.getElementById(formName).elements;
	var  i, field;
	var allComplete=1;
	for(i=0; i<fields.length; i++){
		field=fields[i];
		if(indexOf(fieldsToValidate, field.name) != -1){
			if(field.value==""){
				//alert(field.name);
				allComplete=0;
			}
		}
	}
	if(allComplete){
		document.getElementById(fieldIdToMarkComplete).value=1;
	}
	else{
		document.getElementById(fieldIdToMarkComplete).value=0;
	}
	
	return ValidateForm(formName, fieldsToValidate);
	//return true;
}

//http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml
function submitForm(formID){
	var thisform=document.getElementById(formID);
	//if there's no validation function, skip it.
	if(thisform.onsubmit==undefined){
		    document.getElementById(formID).submit();
	}
	else{
		//if they supplied a validation function
    if(document.getElementById(formID).onsubmit){
      //else if there it one, validate then go on.
      var valid=document.getElementById(formID).onsubmit()
      if(valid){
        document.getElementById(formID).submit();
      }
    }
  }
}

function submitDojoForm(formID, divName){							//This function just checks to see if there is a dijit error and doesnt let the form submit if there is
	if(divName==null){divName="FormError"}							//this includes any errors like incorrect dates
	var txtboxs = document.getElementsByTagName("div");				 
	var errorPatt = new RegExp("dijitError");
	
	for(txtbox in txtboxs){
		if(errorPatt.exec(txtboxs[txtbox].className)){			
			gE(divName).innerHTML = "<span class='redtext'>You need to correct all mistakes before proceeding</span><br><br>";
			return false;									
		}
	}
	return submitForm(formID);
}

