function validate(formname) //Function with a parameter representing a form name.
{

var error = 'blank'
var error2 = 'blank'
var valid = 'true'
var radchecked = false
var radname = 'blank'
var boxchecked = false
var boxname = 'blank'
radios = new Array("blank")
rad_val = new Array();

theForm = document.getElementById(formname);

//GET ALL RADIOS AND PUT THEM IN 2 ARRAYS
for(i=0; i<theForm.elements.length; i++){
     if(theForm.elements[i].name.substring(0,4) == "req_" ){
            if (theForm.elements[i].type == 'radio') {
                radios.push(theForm.elements[i].name)
            }
     }
}

radios = unique(radios)
radios.shift();

for(i=0; i<radios.length; i++) {
 rad_val[i] = false
}

//LOOP OVER ALL FORM ELEMENTS

//TURN CONCATENATED DATE LABELS BLACK
if (document.getElementById('from_dates'))
	document.getElementById('from_dates').style.color = '#000000';
if (document.getElementById('to_dates'))
	document.getElementById('to_dates').style.color = '#000000';

for(i=0; i<theForm.elements.length; i++){

 if(theForm.elements[i].name.substring(0,4) == "req_" ){
      var idname  = theForm.elements[i].name.substring(4,theForm.elements[i].name.length)

      document.getElementById(idname).style.color = '#000000';    

  //TEXT OR TEXTAREA
  if (theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button")
        if(theForm.elements[i].value == ""){
            document.getElementById(idname).style.color = '#cc0000';
            valid = 'false'
         }
    
     //CHECKBOX
     if (theForm.elements[i].type == "checkbox"){
       
        if (boxname == theForm.elements[i].name){
         if (boxchecked == false)
            boxchecked = theForm.elements[i].checked
      }
      else {
         boxname = theForm.elements[i].name
         boxchecked = theForm.elements[i].checked
       }
      if (boxchecked == false)
         error2 = idname
      if (boxchecked == true)
        error2 = 'blank'
      }

       //SELECT
       if(theForm.elements[i].type == "select-one")
        if (theForm.elements[i].selectedIndex == 0){

//CHANGE COLOR OF CONCATENATED DATES			 
		  if  (theForm.elements[i].name.substring(0,8) == "req_from")
			document.getElementById('from_dates').style.color = '#cc0000';
		  else if (theForm.elements[i].name.substring(0,7) == "req_to_")
			document.getElementById('to_dates').style.color = '#cc0000';
		  else 
            	document.getElementById(idname).style.color = '#cc0000';
            valid = 'false'
        }

   //RADIO
   if (theForm.elements[i].type == 'radio') {
     rad_index = get_index(theForm.elements[i].name,radios);
     if (rad_val[rad_index] == false)
         rad_val[rad_index] = theForm.elements[i].checked;
      }

 }
}

//ARE ALL RADIOS CHECKED?
for(i=0; i<radios.length; i++) {
 if (rad_val[i] == false){
   valid = 'false';
    document.getElementById(radios[i].substring(4,radios[i].length)).style.color = '#cc0000';
 }
    
}

//IS CHECKBOX FILLED IN?
if (error2 != 'blank'){
       document.getElementById(error2).style.color = '#cc0000';
       valid = 'false'
}

if (valid == 'true') {

   if (theForm.evt_date_to) 
      theForm.evt_date_to.value = theForm.req_to_month.options[theForm.req_to_month.selectedIndex].value + '/' + theForm.req_to_day.value + '/' + theForm.req_to_year.value;
   if (theForm.evt_date_from)	 
      theForm.evt_date_from.value = theForm.req_from_month.options[theForm.req_from_month.selectedIndex].value + '/' + 
		    					theForm.req_from_day.value + '/' + 
							theForm.req_from_year.value;


 for(i=0; i<theForm.elements.length; i++)
   if(theForm.elements[i].name.substring(0,4) == "req_" )
      theForm.elements[i].name  = theForm.elements[i].name.substring(4,theForm.elements[i].name.length);
//ADDING CODE TO CONCATENATE DATE DROPDOWNS INTO SINGLE DATE FIELD

    theForm.submit();

}

if (valid == 'false'){
  document.getElementById('alert').style.display = "block";
   window.scrollTo(0,0);
}

}

function get_index(n,h){
 for(z=0;z<h.length;z++){
   if (h[z] == n)
     return z;
  }
return -1;
}

/**
 * Removes duplicates in the array 'a'
 * @author Johan Känngård, http://dev.kanngard.net
 */
function unique(a) {
	tmp = new Array(0);
	for(i=0;i<a.length;i++){
		if(!contains(tmp, a[i])){
			tmp.length+=1;
			tmp[tmp.length-1]=a[i];
		}
	}
	return tmp;
}

/**
 * Returns true if 's' is contained in the array 'a'
 * @author Johan Känngård, http://dev.kanngard.net
 */
function contains(a, e) {
	for(j=0;j<a.length;j++)if(a[j]==e)return true;
	return false;
}
