function Validate(f, fields, optionalFields, selectBoxes, tourIds) {
// Custom: add optional fields; replace _ with space
	fields=(fields) ? fields.split(",") : Array();
	optionalFields= (optionalFields) ? optionalFields.split(",") : Array();
    selectBoxes=(selectBoxes) ? selectBoxes.split(",") : Array();
    tourIds=(tourIds!='') ? tourIds.split(",") : Array();
    
	var r = document.forms[f];
	var ErrorStr = "";
    
	for (i=0; i<fields.length; i++) {
		if (fields[i] == "Email") {
			if (r.elements[fields[i]].value.indexOf("@") < 1) {
              if(ErrorStr=="") r.elements[fields[i]].focus();
              ErrorStr += "Please enter a valid email address.\n";
            }
		}
		else if (r.elements[fields[i]].value.length < 1) {
          if(ErrorStr=="") r.elements[fields[i]].focus();
          ErrorStr += "Please enter a value for the \"" + fields[i].replace(/\_/g," ") + "\" field.\n";
        }
	}
    
	var optionalOkay = false;
	if (optionalFields!="") {
       for (i=0; i<optionalFields.length;i++) {
		   if (r.elements[optionalFields[i]].value.length > 0) optionalOkay = true;
	   }
	   if (optionalOkay == false) {
         if(optionalFields.length>0 && ErrorStr=="") r.elements[optionalFields[0]].focus();
         ErrorStr += "Please enter a value for one of these fields: \"" + optionalFields.join("\",\"").replace(/\_/g," ") + "\".\n";
       }
    }
    
    for (i=0; i<selectBoxes.length; i++) {
		if (r.elements[selectBoxes[i]].value==0) {
          if(ErrorStr=="") r.elements[selectBoxes[i]].focus();
          ErrorStr += "Please select a value for the \"" + selectBoxes[i].replace(/\_/g," ") + "\" field.\n";
        }
	}

    if(r.accom_id1.value!='0')
    {
      var tmpval=parseInt(r.Duration_Of_Stay.value);
      if(isNaN(tmpval) || tmpval<1) {
        if(ErrorStr=="") r.Duration_Of_Stay.focus();
        ErrorStr += "Please tell us how many nights you will be staying in your selected accomodation.\n";
      }
      var tmpval=parseInt(r.accom_num1.value);
      if(isNaN(tmpval) || tmpval<1) {
        if(ErrorStr=="") r.accom_num1.focus();
        ErrorStr += "Please tell us the number of rooms you will require in your selected accomodation.\n";
      }
    }   
    
	if (ErrorStr.length > 0) {
		alert(ErrorStr);
		return(false);
	}
    
    for (i=0; i<tourIds.length; i++) {
      tourDate=document.getElementById('tour_' + tourIds[i] + '_date');
      tourAdults=document.getElementById('tour_' + tourIds[i] + '_adults');
      tourChildren=document.getElementById('tour_' + tourIds[i] + '_children');
      if(tourDate.value=='' || tourDate.value=='DD/MM/YY') {
        if(tourAdults)
          if(tourAdults.value!=0 && tourAdults.value!='') {
            alert('Please tell us the date on which you will be taking this tour');
            tourDate.focus();
            return(false);
          }
        if(tourChildren)
          if(tourChildren.value!=0 && tourChildren.value!='') {
            alert('Please tell us the date on which you will be taking this tour');
            tourDate.focus();
            return(false);
          }
      }
	}
    
	return(true);
}


function getVal(el) {
  return el[el.selectedIndex].value
}

function fill_nights(f) {
  var MINUTE = 60 * 1000;
  var HOUR = MINUTE * 60;
  var DAY = HOUR * 24;
  var WEEK = DAY * 7;
  
  var f = document.forms[f];
  
  var from=new Date(getVal(f.Check_In_Date_year),getVal(f.Check_In_Date_month)-1,getVal(f.Check_In_Date_day));
  var till=new Date(getVal(f.Check_Out_Date_year),getVal(f.Check_Out_Date_month)-1,getVal(f.Check_Out_Date_day));
  //var till=new Date(2004,03-1,02);
  
  var nights=(till-from) / DAY;
  
  if(nights>0 && nights <30)
    f.Duration_Of_Stay.value=nights;
  return true;
}