var xmlHttp

function validateWorkshopForm(theForm) {
  var reason = "";

  reason += validateText(1,theForm.ContactName);
  reason += validatePhone(0,theForm.ContactPhone);
  reason += validateEmail(1,theForm.ContactEmail);

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
//  alert("All fields are filled correctly");

  return true;
}


function validateFormOnSubmit(theForm) {
  var reason = "";

  reason += validateText(1,theForm.ContactName);
  reason += validateText(0,theForm.ContactOrg);
  reason += validatePhone(0,theForm.ContactPhone);
  reason += validateEmail(1,theForm.ContactEmail);

  reason += validateText(1,theForm.EventTheme);

  reason += validateText(0,theForm.EventTime);
  reason += validateNum(0,theForm.EventDuration);

  reason += validateText(0,theForm.VenueName);
  reason += validateText(0,theForm.VenueAddress);
  reason += validateNum(0,theForm.VenueWidth);
  reason += validateNum(0,theForm.VenueDepth);
  reason += validateText(0,theForm.VenueWet);

  reason += validateNum(0,theForm.CostDonation);
  reason += validateNum(0,theForm.CostTransport);

  reason += validateText(0,theForm.GeneralAbout);
  reason += validateText(0,theForm.GeneralQuestions);

  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
//  alert("All fields are filled correctly");

  return true;
}

function closeForm(myForm) {
  var id=document.getElementById("myId");
  if ( validateFormOnSubmit(myForm)) {
    if (id.value == '') {
      return true;
    }
    else {
      opener.location.reload();
      self.close();
      return true;
    }
  }
  else {
    return false;
  }
}

function validateNum(compulsory,fld) {
  var error = "";
  var illegalChars = /[^0-9]/; // allow numbers only

  fld.style.background = 'Yellow'; 
 
  if (compulsory && (fld.value == "")) {
    error = fld.name + ": Empty field.\n";
  }
  else if (illegalChars.test(fld.value)) {
    error = "Field contains illegal characters.\n";
  }
  else {
    fld.style.background = 'White';
  }

  return error;
}

function validateText(compulsory,fld) {
  var error = "";
  var illegalChars = /[^A-Za-z0-9_\-\,\ \.]/; // allow letters, numbers, underscores and spaces

  fld.style.background = 'Yellow'; 
 
  if (compulsory && (fld.value == "")) {
    error = fld.name + ": Empty field.\n";
  }
  else if (illegalChars.test(fld.value)) {
    error = "Field contains illegal characters.\n";
  }
  else {
    fld.style.background = 'White';
  }

  return error;
}


function trim(s) {
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(compulsory,fld) {
  var error="";
  var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
  var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
  var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
  fld.style.background = 'Yellow';

  if (compulsory && (fld.value == "")) {
    error = fld.name + ": Empty field.\n";
  }
  else if (!emailFilter.test(tfld)) {              //test email for illegal characters
    error = "Please enter a valid email address.\n";
  }
  else if (fld.value.match(illegalChars)) {
    error = "The email address contains illegal characters.\n";
  }
  else {
    fld.style.background = 'White';
  }

  return error;
}

function validatePhone(compulsory,fld) {
  var error = "";
  var stripped = fld.value.replace(/[\+\(\)\.\-\ ]/g, '');

  fld.style.background = 'Yellow';

  if (compulsory && (fld.value == "")) {
    error = fld.name + ": Empty field.\n";
  }
  else if (isNaN(parseInt(stripped))) {
    error = "Field contains illegal characters.\n";
  }
  else {
    fld.style.background = 'White';
  }

  return error;
}

function mouseOver(id) {
  var cssString='background-color:lime';
  var el=document.getElementById(id);
  el.style.background='red';
//  el.style.cssText=cssString;
//   alert("mouse over");
//  document.obj.bgcolor='red';
}

function mouseOut(id) {
  var cssString='background-color:white';
  var el=document.getElementById(id);
  el.style.background='black';
//  el.style.background='white';
//  el.style.cssText=cssString;
//  alert("mouse out");
//  document.obj.bgcolor='blue';
}

function selectBookings(str) {
  xmlHttp=GetXmlHttpObject();
  if (xmlHttp==null) {
    alert ("Your browser does not support AJAX!");
    return;
  } 
  var url="http://www.waitaiko.com/getBookings.php";
  url=url+"?q="+str;
  url=url+"&sid="+Math.random();
  xmlHttp.onreadystatechange=stateChanged;
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

function stateChanged() { 
  if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
    document.getElementById("ListBookings").innerHTML=xmlHttp.responseText;
  }
}

function GetXmlHttpObject() {
  var xmlHttp=null;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

