//    Documentation: 
//    	1) prepend 'req_' to the required fields' names        name="req_phone"
//    	2) give the field's label an id the same as the field's original name  id="phone"
// 		3) make an error span or div  ---  <span id="optionalformname_alert" style="display:none">You must complete</span>
// 		4) make an anchor for the error message --- <a id="formname_anchor" name="formname_anchor"/>
//
//

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
}



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


//LOOP OVER ALL FORM ELEMENTS
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 = '';    

  //TEXT OR TEXTAREA
  if (theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button"){

	   //IS TEXT AREA EMPTY??
	   if(theForm.elements[i].value == ""){
    		 document.getElementById(idname).style.color = '#cc0000';
            valid = 'false'
         }


		//SPECIAL CHECKNG FOR FIELD CALLED REQ_EMAIL	   
         if  (theForm.elements[i].name == "req_from") {      
            if (!(checkemail(theForm.elements[i].value))){ //IS IT A VALID EMAIL ADDRESS?
			valid= 'false';
               document.getElementById(idname).style.color = '#cc0000';
		  }	
	    }

		//SPECIAL CHECKNG FOR FIELD CALLED REQ_EMAIL_CONFIRM	    	    
    	    if (theForm.elements[i].name == "req_email_confirm") {    //DOES THE CONFIRM FIELD MATCH?
			  if (theForm.req_email.value != theForm.elements[i].value){
				valid= 'false';
	               document.getElementById(idname).style.color = '#cc0000';				
			}
		}

   }


     //PASSWORD   
     if (theForm.elements[i].type == "password"){
	   	//IS PASSWORD AREA EMPTY??
	   	if(theForm.elements[i].value == ""){
    		 	document.getElementById(idname).style.color = '#cc0000';
            	valid = 'false'
         	}
		//SPECIAL CHECKNG FOR FIELD CALLED REQ_EMAIL_CONFIRM	    	    
    	    	if (theForm.elements[i].name == "req_password_confirm") {    //DOES THE CONFIRM FIELD MATCH?
			  if (theForm.req_password.value != theForm.elements[i].value){
				valid= 'false';
	               document.getElementById(idname).style.color = '#cc0000';				
			}
		}
	}

	//FILE UPLOAD
     if (theForm.elements[i].type == "file	"){
		if(theForm.elements[i].value == ""){
    			document.getElementById(idname).style.color = '#d8007e';
          	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';
	document.getElementById(error2).style.visibility = 'visible';
       valid = 'false';
}

if (valid == 'true') {
 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
   if (theForm.from_month)	 
      theForm.evt_date_from.value = theForm.from_month.options[theForm.from_month.selectedIndex].value + '/' + theForm.from_day.value + '/' + theForm.from_year.value;
   if (theForm.evt_date_to) 
      theForm.evt_date_to.value = theForm.to_month.value + '/' + theForm.to_day.value + '/' + theForm.to_year.value;

    theForm.submit();

}

if (valid == 'false'){
  if (document.getElementById(formname + '_alert'))	   
	  document.getElementById(formname + '_alert').style.display = "block";
  else 
	if (document.getElementById('alert'))    	
	  document.getElementById('alert').style.visibility = "visible";

  if (document.getElementById(formname + '_anchor'))	
	window.location.hash= formname+ '_anchor'; 
  else
	window.scrollTo(0,510);
  
}



}

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;
}


function checkemail(str){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
		testresults=true
	else{
		testresults=false
	}
	return (testresults)
}

function gotoAnchor(anchorName) {
  if (document.layers) {
    var anchor = document.anchors[anchorName];
    window.scrollTo(anchor.x, anchor.y);
    return;
  }
  else if (document.getElementById) {
    var anchor = document.anchors[anchorName];
    var coords = {x: 0, y: 0 };
    while (anchor) {
      coords.x += anchor.offsetLeft;
      coords.y += anchor.offsetTop;
      anchor = anchor.offsetParent;
    }
    window.scrollTo(coords.x, coords.y);
  }
}
  

