function validateTour(theForm) {
var reason = "";
var browser = navigator.appName;
var clients = document.forms['ordertour'].elements['nclients'].value;

  reason += validateDay(theForm.daya);
  reason += validateMonth(theForm.montha);
  reason += validateYear(theForm.yeara);
  reason += validateClients(theForm.nclients);
  reason += validatePriceType(theForm.price); 
  reason += validatePayment(theForm.payment);
  
  if (clients > 1)
  {
  reason += validateRoomType();
  reason += validateGuestNames();
  }
  reason += validateName(theForm.fname);
  reason += validateEmail(theForm.email);
  reason += validatePhone(theForm.phone);

  reason += validateCountry(theForm.country);

  

  //reason += validateDays(theForm.ndays);
  
  
  if (reason != "") {
    alert("Some required fields are not filled out:\n" + reason);
    return false;
  }

   
}

function validateGuestNames()
{
var error = "";
var x = document.getElementById("ni").value; 
var i = 1;

for (i=1;i<=x;i++)
{
var fld = document.forms['ordertour'].elements['clientIE'+i];
 if (fld.value.length == 0)
 {
  fld.style.background = '#fcbf03'; 
  error = "- No names in rooming list\n";
 }
 else 
 {
  fld.style.background = 'White';
 }
}
 return error;
}

function validateRoomType()
{
var x = document.getElementById("ni").value; 
var i = 1;
  var error = "";
for (i=1;i<=x;i++)
{
var fld = document.forms['ordertour'].elements['roomlistIE'+i];
  if (fld.value == "no")
  {
   fld.style.background = '#fcbf03'; 
   error = "- Room type for rooming list\n";
  }
  else 
 {
  fld.style.background = 'White';
 }
}
return error;
}

function validateName(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = '#fcbf03'; 
        error = "- Full Name\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validatePriceType(fld) {
    var error = "";
 
    if (fld.value == 0) {
        fld.style.background = '#fcbf03'; 
        error = "- No price type selected\n";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validatePayment(fld) {
    var error = "";
 
    if (fld.value == "no") {
        fld.style.background = '#fcbf03'; 
        error = "- No payment selected\n";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateDay(fld) {
    var error = "";
 
    if (fld.value == "no") {
        fld.style.background = '#fcbf03'; 
        error = "- No day selected\n";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateMonth(fld) {
    var error = "";
 
    if (fld.value == "no") {
        fld.style.background = '#fcbf03'; 
        error = "- No month selected\n";
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateYear(fld) {
    var error = "";
    
    if (fld.value == "no") {
        fld.style.background = '#fcbf03'; 
        error = "- No year selected\n";
    } 
    else {
        fld.style.background = 'White';
    }
    return error;  
}
function validateCountry(fld) {
    var error = "";
    var country = document.getElementById("c").value;
    var bg = document.getElementById("c");
    if (country == "no") {
        bg.style.background = '#fcbf03'; 
        error = "- No country selected\n";
    } 
    else 
    {
        bg.style.background = 'White';
    }
    return error;  
}
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = '#fcbf03';
        error = "- Email address\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#fcbf03';
        error = "- Please enter a valid email address\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#fcbf03';
        error = "- The email address contains illegal characters\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');
    var illegalChars = /[\a-z]/;
    if (fld.value == "") {
        fld.style.background = '#fcbf03';
        error = "- Phone\n";
    }  else if (isNaN(parseInt(stripped))) 
    {
        error = "- Phone must contain digits and + only\n";
        fld.style.background = '#fcbf03';
    }
    else if  (illegalChars.test(fld.value))
    {
        error = "- Phone must contain digits and + only\n";
        fld.style.background = '#fcbf03';
    }
    else if (stripped.length < 9) {
        error = "- Make sure you included country and city code.\n";
        fld.style.background = '#fcbf03';
    }
    else {
        fld.style.background = 'White';
    }
   return error;
}
function validateDays(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    
    var illegalChars = /[\W_a-z]/;
   if (fld.value == "") {
        error = "- Number of Days\n";
        fld.style.background = '#fcbf03';
    }  
    else if (illegalChars.test(fld.value))
    {
      error = "- #Days should be a number\n";
      fld.style.background = '#fcbf03';
    }
    else if (isNaN(parseInt(stripped))) {
        error = "- #Days should be a number\n";
        fld.style.background = '#fcbf03';
    }
    else {
        fld.style.background = 'White';
    }

    return error;

}
function validateClients(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    
    var illegalChars = /[\W_a-z]/;
   if (fld.value == "") {
        error = "- Number of Tourists\n";
        fld.style.background = '#fcbf03';
    }  
    else if (illegalChars.test(fld.value))
    {
      error = "- #Tourists should be a number\n";
      fld.style.background = '#fcbf03';
    }

    else if (isNaN(parseInt(stripped))) {
        error = "- #Tourists should be a number\n";
        fld.style.background = '#fcbf03';
    }
    else {
        fld.style.background = 'White';
    }

    return error;

}

function showRoomList()
{
var browser=navigator.appName;
var clients = document.forms['ordertour'].elements['nclients'].value;
var roomlist = document.getElementById('rlist');
var fulln = document.getElementById('fulln');
var opts = document.getElementById('opts');
var link = document.getElementById('link');
var oguest = document.getElementById('oguest');
var notIE = document.getElementById('notIE');
if (browser != 'Microsoft Internet Explorer') 
    {

if (clients > 1)
{
document.forms["ordertour"].elements["roomlist"].value="Yes";
document.forms["ordertour"].elements["ni"].value=1;
roomlist.style.display = "block";
link.style.display = "block";
oguest.style.display = "block";
notIE.style.display = "block";
}
}
else //this is IE 
{
if (clients > 1)
{
document.forms["ordertour"].elements["roomlist"].value="Yes";
document.forms["ordertour"].elements["ni"].value=1;
document.getElementById("IE").value='Yes';
roomlist.style.display = "block";
fulln.style.display = "block";
opts.style.display = "block";
link.style.display = "block";
oguest.style.display = "block";
}
}
}


