
function tic(form, start, end, el) {
 var e = form.elements;
 for(var i = start; i <= end; ++i) {
   if(e[i].type == 'checkbox' ) e[i].checked = el.checked;
 }
}


function valid(form) {// thoroughly validates form
 var e = 0 
 var M = ''
 
 // start checks
 document.getElementById('formmsg').innerHTML = ""; 

 var field = form.max_rooms;  
 var vm = parseInt(field.value, 10);   
 if (!vm && vm !=0) {
  M = M + "<LI>You have to indicate the maximum number of rooms - enter 99 to ensure all properties are included.</LI>";
  field.select();
  e = e + 1
 } else if (vm >= 0 && vm < 1000){
  //do nothing  
 } else {
  M = M + "<LI>You have to indicate the maximum number of rooms - enter 99 to ensure all properties are included.  You entered " + vm + ".</LI>"; 
  field.select();
  e = e + 1;
 }
 
 var field = form.min_rooms;  
 var vi = parseInt(field.value, 10);   
 if (!vi && vi !=0) {
  M = M + "<LI>You have to indicate the minimum number of rooms - enter 0 to ensure ensure all properties are included.</LI>";
  field.select();
  e = e + 1
 } else if (vi >= 0 && vi < 1000){
  //do nothing  
 } else {
  M = M + "<lI>You have to indicate the minimum number of rooms - enter 0 to ensure all properties are included.  You entered " + vi + ".</LI>"; 
  field.select();
  e = e + 1;
 } 
 
 if (vm <= vi){
  M = M + "<LI>You have to make sure that the maximum number of rooms is greater than the minimum number of rooms.</LI>" 
  field.select ();
  e = e + 1
 }

 var field = form.max_rate;  
 var vm = parseInt(field.value, 10);  
 if (!vm  && vm !=0) {
  M = M + "<LI>You have to indicate the maximum tariff in New Zealand dollars (e.g. 300) - enter 99999 to ensure that all properties are included.</LI>";
  field.select();
  e = e + 1;
 } else if (vm >= 0 && vm < 1000000) {
  // do nothing 
 } else {
  M = M + "<LI>You have to indicate the maximum tariff in New Zealand dollars (e.g. 300) - enter 99999 to ensure that all properties are included. You entered " + vm + ".</LI>";
  field.select();
  e = e + 1;
 }
 var field = form.min_rate;  
 var vi = parseInt(field.value, 10);  
 if (!vi  && vi !=0) {
  M = M + "<LI>You have to indicate the maximum tariff in New Zealand dollars (e.g. 300) - enter 99999 to ensure that all properties are included.</LI>";
  field.select();
  e = e + 1;
 } else if (vi >= 0 && vi < 100000) {
  // do nothing
 } else {
  M = M + "<LI>You have to indicate the minimum tariff in New Zealand dollars (e.g. 150) - enter 0 to ensure that all properties are included. You entered " + vi + ".</LI>";
  field.select();
  e = e + 1;
 }
 
 if (vm <= vi){
  M = M + "<LI>You have to make sure that the maximum rate is greater than the minimum one.</LI>" 
  field.select ();
  e = e + 1
 }
 
 if (e < 1) { //no errors occured
  return false;
 } else {
  document.getElementById('formmsg').innerHTML = "please check form: <UL> " + M + "</UL>"; 
  return true;
 }
}  






function check(el, field) {
//checks a field, e.g. check(form.elements[0], "fieldname", "empty", "2num"); el = element number
//controller
 var result={error:""};
 for(var ii=2; ii<arguments.length; ii++)
  if((result=singleCheck(el, field, arguments[ii])).error)
  break;
  return result;
//single check
  function singleCheck(el, field, type) {
   var result={error:""};
   switch(type) {
    case "empty" :
     if(/^\s*$/.test(el.value)) {
      result.error= field+": should be completed.<BR>";
     }
    break;
    case "2num" : 
     if(!/^\d+$/.test(el.value)) {
      result.error=field+": should contain up to two digits only (e.g. 01).<BR>";
     } else {
      el.value=padLeft(el.value.replace(/^\d+(\d\d)$/,"$1"), "0", 2);
     }
    break;
    case "5num" : 
     if(!/^\d+$/.test(el.value)) {
      result.error=field+": should contain up to five digits only (e.g. 01).<BR>";
     } else {
      el.value=padLeft(el.value.replace(/^\d+(\d\d\d\d\d)$/,"$1"), "0", 5);
     }
    break;
   }
  if(el.style){
   el.style.color = result.error ? "#c00" : "";
   el.style.border = result.error ? "1px solid red" : "1px solid green";
  }
  return result;
 }
}


function padLeft(str, pad, count) {  //turns a number into a string
 while(str.length<count)
  str=pad+str;
 return str;
}


