/*------------------ global js (Front End) ----------------------------*/

/*--------------- Change style css ----------------------*/
function celOver(obj) {
  var s = obj.className;
  s = s.replace(/-over/g, "");
  obj.className = s+"-over";
}

function celOut(obj) {
  var s = obj.className;
  s = s.replace(/-over/g, "");
  obj.className = s;
}

/*--------------Global functions----------------*/

function verifyEmail(val) {
  var msg = true;
  
  regex = new RegExp("^[a-z0-9_]([.-]?[a-z0-9_]+)+@[a-z0-9_]([.-]?[a-z0-9_]+)+\.([a-z]{2,4}|[a-z]{6})$", "gi");
  if (!regex.exec(val)) {
    msg = false;
  }				
  return msg;
}

function numberok(valeur) {
	if (isNaN(valeur)) {
		return false;
	} else {
		return true;
	}
}


function uploadFile(sFile) {

  var msg = "";
  
  var Extensions = ".gif .jpg .jpeg .png";
  
  if (sFile == "") {
	//msg += "- No file to upload\n";
  } else {

    var sExt = sFile.match( /\.[^\.]*$/ ) ;
    sExt = sExt ? sExt[0].toLowerCase() : "." ;

    if ( Extensions.indexOf( sExt ) < 0 ) {
      msg += "- This file type is not allowed for uploading ("+sExt+")\n"; 
	}
  }
  return msg;

}



/*------------- Search field ----------------*/
function searchinfo() {
  var errors = '';
  var form = document.SearchFrm
  if (form.Keywords.value == "Search" || form.Keywords.value == "") {
    errors = "True";
  }
  if (errors) {
    //alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
}

function searchfocus(val) {
  if (val.value == "Search") {
    val.value = "";
  }
}

function searchblur(val) {
  if (val.value == "") {
    val.value = "Search";
  }
}


/*-------gallery system (item)----------*/
function popup(idimg,width,height) { 
  xp = (screen.width-width)/2;
  if (xp <= 0) {
    xp = 0;
  }
  yp = (screen.height-height)/2;
  if (yp <=0) {
    yp=0;
  }

  fen = window.open(idimg,'','toolbar=0,location=0,directories=0,status=0,scrollbars=0,resizable=0,copyhistory=0,menuBar=0,top='+yp+',left='+xp+',width=' + width + ',height=' + height);
  fen.focus();
}

/*-------gallery system (itempopup)----------*/
function checksize() {
 if (document.images[0].complete) {
  window.resizeTo(document.images[0].width+12+20,document.images[0].height+30+60);
  window.focus();
 }
}

function getSize() {
	
	img = new Image();
    img.src = source;
    imgWidth = img.width;
    imgHeight = img.height;
	return imgWidth;
	
}

/*------------------end global js----------------------------*/


var minChar = 6;
var maxCuisines = 3;

/*-----------------print version -------------------------------*/

function menuPrint(idimg) {
  var width=620; 
  var xp = (screen.width-width)/2;
  if (xp <= 0) {
    xp = 0;
  }
  var yp=0;
  var height = screen.height-200;

  fen = window.open(idimg,'','toolbar=0,location=0,directories=0,status=0,scrollbars=1,resizable=0,copyhistory=0,menuBar=1,top='+yp+',left='+xp+',width=' + width + ',height=' + height);
  fen.focus();
}



/*-----------------------validate image edit----------------------------*/

function MM_validateImage()
{
  var form = document.form1;
  var errors = "";
  
  errors += uploadFile();
  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
}

/*------------------------------------------------------------*/

function openNew(poll,vote) {
	 
  var form = document.formPoll;
  var errors = "";
  
  var id = 0;
  var bouton = document.getElementsByName("AnsID");
  if (vote) { 
    for (var i=0; i<bouton.length;i++) {
      if (bouton[i].checked) {
        id =  form.elements[i].value;
        break;
      }
    }  
    if (id == 0) {
	 errors += " - Please check one Answer to vote.";  
    }
  }
  
  //alert(id + " - " +poll);
  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  } else {
	popup("poll.asp?ansID="+id+"&PollID="+poll,800,400);
  }
}


/*--------------------- MemberADD -------------------*/
function MM_validateMemberAdd() {

  var form = document.form1;
  var errors = "";
  
  if (form.Name.value.length == 0) {
	errors += "- Restaurant Name is required\n";
  }

  if (form.ContactFirstName.value.length == 0) {
	errors += "- First Name is required\n";
  }

  if (form.ContactLastName.value.length == 0) {
	errors += "- Last Name is required\n";
  }

  if (form.ContactAddress.value.length == 0) {
	errors += "- Address is required\n";
  }

  if (form.CountyID.selectedIndex == 0) {
	errors += "- County is required\n";
  }

  if (form.ContactTel.value.length == 0) {
	errors += "- Tel is required\n";
  }
  
  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
	
}

/*--------------------- CompEntry -------------------*/
function MM_validateCompEntry() {

  var form = document.form1;
  var errors = "";
  
  if (form.Answer.value.length == 0) {
	errors += "- Answer is required\n";
  }

  if (form.Name.value.length == 0) {
	errors += "- Name is required\n";
  }

  if (form.Address.value.length == 0) {
	errors += "- Address is required\n";
  }

  if (form.Phone.value.length == 0) {
	errors += "- Phone is required\n";
  }
  
  if (form.Email.value.length == 0) {
	errors += "- Email is required\n";
  } else {
    if (!verifyEmail(form.Email.value)) {
       errors += "- Email is invalid\n";
    }
  }

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
	
}


function MM_validateNewsItem() {

  var form = document.form1;
  var errors = "";
  
  if (form.News.value.length == 0) {
	errors += "- News title is required\n";
  }
  
  if (form.News.value.length >= 100) {
	errors += "- News title is too long\n";
  }
  
  if (form.Short_Description.value.length == 0) {
	errors += "- Short description is required\n";
  }
  
  if (form.Short_Description.value.length >= 255) {
	errors += "- Short description is too long\n";
  }
 
  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
	
}


function MM_validateImg(form) {

  var errors = "";

  if (form.file.value == "") {
    errors += "- File is required\n"
  } else {
    errors += uploadFile(form.file.value);
  }

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');

}


function MM_validateImgEdit(form) {
  var errors = "";
  errors += uploadFile(form.file.value);

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');

}


function MM_validateMemberInfo() {

  var form = document.form1;
  var errors = "";
  
  if (form.Name.value.length == 0) {
	errors += "- Restaurant Name is required\n";
  }

  if (form.Address.value.length == 0) {
	errors += "- Address is required\n";
  }

  if (form.CountyID.selectedIndex == 0) {
	errors += "- County is required\n";
  }

  if (form.TownID.selectedIndex == 0) {
	errors += "- Town is required\n";
  }

  if (form.Tel.value.length == 0) {
	errors += "- Tel is required\n";
  }

  //if (form.Fax.value.length == 0) {
	//errors += "- Fax is required\n";
  //}

  //if (form.Web.value.length == 0) {
	//errors += "- Web Address is required\n";
  //}

  //if (form.Url.value.length == 0) {
	//errors += "- URL Address is required\n";
  //}

  if (form.ContactFirstName.value.length == 0) {
	errors += "- Contact First Name is required\n";
  }

  if (form.ContactLastName.value.length == 0) {
	errors += "- Contact Last Name is required\n";
  }

  if (form.Established.value.length == 0) {
	//errors += "- Established Last Name is required\n";
  }



  /*if (form.Capacity.value.length == 0) {
	errors += "- Capacity is required\n";
  }
*/  
  if (form.currency.value == 0) {
	errors += "- Currency is required\n";
  }
/*
  var flagRadio = true;
  for (i=0; i<form.PriceID.length; i++) {
    if (form.PriceID[i].checked) {
       flagRadio = false;
       break;
    }
  }

  if (flagRadio) {
	errors += "- Price is required\n";
  }*/

  if (form.Password.value.length == 0) {
	errors += "- Password is required\n";
  } else {
    if (form.Password.value.length < minChar) {
	  errors += "- Password must contain more than "+ (minChar-1) +" characters\n";
    }
  }
 
  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');
}

function MM_validateAwards() {

  var form = document.form1;
  var errors = "";
  
  if (form.Name.value.length == 0) {
	errors += "- Name is required\n";
  }
  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');	
}

function MM_validatePromotions() {

  var form = document.form1;
  var errors = "";

  if (form.Name.value.length == 0) {
	errors += "- Title is required\n";
  }

  if (form.Header.value.length == 0) {
	errors += "- Short Description is required\n";
  }

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');	
}

function MM_validateCritics() {

  var form = document.form1;
  var errors = "";

  if (form.CriticDate.value.length == 0) {
	errors += "- Date is required\n";
  }
  
  if (form.CriticName.value.length == 0) {
	errors += "- Name is required\n";
  }

  if (form.Published.value.length == 0) {
	errors += "- Publication is required\n";
  }

  if (form.ShortDesc.value.length == 0) {
	errors += "- Short Comment is required\n";
  }

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');	
}


function MM_validateCuisine() {

  var form = document.form1;
  var errors = "";
  var count = 0;

  for (var i=0; i < form.chkCuisine.length; i++)
  {
    if (form.chkCuisine[i].checked)
      {
        count++;
      }
  }

  if(count > maxCuisines) {
    errors += '- Please only enter 3 types of cuisine, you entered: ' + count + '\n';
  }
   	
  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');	
}

function MM_validateFood(val1,val2) {
  var form = document.form1;
  var errors = "";

  if (form.foodParent.type == "select-one") {
    if (form.foodParent.value == 0) {
	  errors += "- " + val1 + " is required\n";
    }  
  }

  if (form.Title.value.length == 0) {
	errors += "- " + val2 + " is required\n";
  }

  if (form.Price.value.length != 0) {
    if (!numberok(form.Price.value)) {
	  errors += "- Price must be a Number\n";
   }
  }

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');	
}

function MM_validateWineRec() {
  var form = document.form1;
  var errors = "";

  var tabLabel = new Array();
  tabLabel[1] = "Recommanded by owner"
  tabLabel[2] = "Wine of the month"
  tabLabel[3] = "Wine by the glass"
  tabLabel[4] = "Wine by the 1/2 glass"
  tabLabel[5] = "Wine by the 1/4 glass"
  tabLabel[6] = "House Wines (1)"
  tabLabel[7] = "House Wines (2)"
  

  for (i=1; i<tabLabel.length; i++) {
    if (form.elements["WinRec"+i] != undefined) {
       if (form.elements["WinRec"+i].value == "0") {
          errors += "- " + tabLabel[i] + ": Wine OR Any is require\n";
       }
    }
  }

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');	
}


function MM_validateRelated() {
  var form = document.form1;
  var errors = "";
  var i;
  var flagBtn = true;
  
  if (form.relation.length != undefined) {
    for (i=0; i<form.relation.length; i++) {
      if (form.relation[i].checked) {
        flagBtn = false;
        break;
      }
    }
  } else {
	flagBtn = !form.relation.checked;
  }

  if (flagBtn) {
    errors += " - Restaurant is required\n"
  }

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');	
}

function MM_validateForm() {
  var form = document.form1;
  var errors = "";

  if (form.Name.value.length == 0) {
    errors += " - Your Name is required\n";
  }

  if (form.Email.value.length == 0) {
    errors += " - Your E-mail is required\n";
  } else {
    if (!verifyEmail(form.Email.value)) {
      errors += " - Your E-mail is invalid\n";
    }
  }

  if (form.Enquiry.value.length == 0) {
    errors += " - A comment or query is required\n";
  }

  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');	
}

function MM_validateFormFriend() {
  var form = document.form1;
  var errors = "";

  if (form.nameFrom.value.length == 0) {
    errors += " - Your Name is required\n";
  }

  if (form.from.value.length == 0) {
    errors += " - Your E-mail is required\n";
  } else {
    if (!verifyEmail(form.from.value)) {
      errors += " - Your E-mail is invalid\n";
    }
  }

  if (form.nameTo.value.length == 0) {
    errors += " - Friend's Name is required\n";
  }

  if (form.to.value.length == 0) {
    errors += " - Friend's E-mail is required\n";
  } else {
    if (!verifyEmail(form.to.value)) {
      errors += " - Friend's E-mail is invalid\n";
    }
  }


  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');	
}


function MM_validateClientAdd() {
  var form = document.form1;
  var errors = "";

  if (form.ClientName.value.length == 0) {
    errors += " - Name is required\n";
  }

  if (form.ClientEmail.value.length == 0) {
    errors += " - E-mail is required\n";
  } else {
    if (!verifyEmail(form.ClientEmail.value)) {
      errors += " - E-mail is invalid\n";
    }
  }
  
  if (form.ClientCity.value.length == 0) {
    errors += " - Town/City is required\n";
  }

  if (form.ClientUserName.value.length == 0) {
    errors += " - Username is required\n";
  } else if (form.ClientUserName.value.length < minChar) {
	  errors += " - Username must contain more than "+ (minChar-1) +" characters\n";
  }

  if (form.ClientPassword.value.length == 0) {
    errors += " - Password is required\n";
  } else if (form.ClientPassword.value.length < minChar) {
	  errors += " - Password must contain more than "+ (minChar-1) +" characters\n";
  }

  if (form.ClientPassword2.value.length == 0) {
    errors += " - Repeat Password is required\n";
  } else if (form.ClientPassword2.value.length < minChar) {
	  errors += " - Repeat Password must contain more than "+ (minChar-1) +" characters\n";
  }

  if (form.ClientPassword.value != form.ClientPassword2.value && (form.ClientPassword.value.length >= minChar  && form.ClientPassword2.value.length >= minChar)) {
	  errors += " - Passwords do not match\n";
  }


  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');	
}

function MM_validateClientAddcms() {
  var form = document.form1;
  var errors = "";

  if (form.ClientName.value.length == 0) {
    errors += " - Name is required\n";
  }
  
  if (form.ClientAge.value.length == 0) {
    errors += " - Age is required\n";
  }
  
  if (isNaN(form.ClientAge.value)) {
    errors += " - Age must be numeric\n";
  }

  if (form.ClientEmail.value.length == 0) {
    errors += " - E-mail is required\n";
  } else {
    if (!verifyEmail(form.ClientEmail.value)) {
      errors += " - E-mail is invalid\n";
    }
  }


  if (form.ClientPassword.value.length == 0) {
    errors += " - Password is required\n";
  } else if (form.ClientPassword.value.length < minChar) {
	  errors += " - Password must contain more than "+ (minChar-1) +" characters\n";
  }


  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');	
}


function stars(m,n) {
  var objStr;
  for (i=1; i<6; i++) {
    objStr = document.getElementById("imgstr"+m+i);
    if (i<=n) {
      objStr.src = "siteready/strselected.gif";
    } else {
      objStr.src = "siteready/str.gif";
    }
  }
  objStr = document.getElementById("imgstr"+m+n)
  //if (document.addEventListener) {
    objStr.onfocus = objStr.blur;
  //}
  document.form1.elements["strval"+m].value = n;
  
  var chp = 1;
  var avg = 0;
  while (document.form1.elements["strval"+chp] != undefined) {
    avg += parseFloat(document.form1.elements["strval"+chp].value);
    chp++;
  }
  avg = Math.round(avg*10/(chp-1))/10;
  document.getElementById("avg").innerHTML = avg + "/5";
  document.form1.elements["avgchp"].value = avg;
}

function MM_validateRate() {
  var form = document.form1;
  var errors = "";
  var editor1, content

  if (form.elements["Editor1"]) {
    editor1 = obj_Editor1;
    content = editor1.getHTML();
    if (content.length == 0) {
      errors += " - No Rating will be submitted without a Review\n"
    }
  } else if (form.elements["Editor1"]) {
     
	 editor1 = obj_Editor1;
     content = editor1.getHTML();
    if (content.length == 0) {
      errors += " - Review is required\n"
    }
  } else if (form.elements["avgchp"]) {
    if (form.elements["avgchp"].value.length == 0) {
      errors += " - Rating is required\n"
    }
  }

  if (errors) {
   alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');	
}

function MM_validateCompetitions() {

  var form = document.form1;
  var errors = "";

  if (form.Name.value.length == 0) {
	errors += "- Title is required\n";
  }

  if (form.Introduction.value.length == 0) {
	errors += "- Introduction is required\n";
  }
  
  if (form.Prizes.value.length == 0) {
	errors += "- Prize Details are required\n";
  }
  
  var nb = 1;
  var flagit = false;
  while(it = form.elements["Item"+nb]) {
	  if (it.value.length > 0) {
		  flagit = true;
		  break;
	  }
	  nb++;
  }
  
  if ((form.Interest_Title.value.length > 0 && !flagit) || (form.Interest_Title.value.length == 0) && (flagit)) {
	errors += "- Intro text or Item(s) are required\n";
  } 


  if (errors) {
    alert('The following error(s) occurred:\n'+errors);
  }
  document.MM_returnValue = (errors == '');	
}


function showRest2(val1, val2, val3, val4, val5, val6, val7) {

  ol_offsetx=-208;

  var pr = "";

  pr += "<table class='leg2Tab' cellpadding='0' cellspacing='10' width='206'><tr><td>"
  pr += "<table cellspacing='0' cellpadding='0'>"
  pr += "<tr><td valign='top' width='75'>Name:</td><td><b>"+ val1 +"</b></td></tr>"
  pr += "<tr><td valign='top'>Address:</td><td>"+ val2

  pr += "<br>"+ val3 +"</td></tr>"
  if (val4 != "") {
    pr += "<tr><td valign='top'>Post Code:</td><td>"+ val4 +"</td></tr>"
  }
  if (val6 != "") {
    pr += "<tr><td valign='top'>Tel:</td><td>"+ val6 +"</td></tr>"
  }

  if (val5 != "") {
    pr += "<tr><td colspan='2'><img src='imagemember/"+ val5 +"' /></td></tr>"
  }
  
  
  pr += "</table>"
  pr += "</td></tr></table>"
  return pr
}

function showRest(val1, val2, val3, val4, val5, val6, val7) {

  ol_offsetx=10;

  var pr = "";

  pr += "<table class='leg2Tab' cellpadding='0' cellspacing='10' width='206'><tr><td>"
  pr += "<table cellspacing='0' cellpadding='0'>"
  pr += "<tr><td valign='top' width='75'>Name:</td><td><b>"+ val1 +"</b></td></tr>"
  pr += "<tr><td valign='top'>Address:</td><td>"+ val2

  pr += "<br>"+ val3 +"</td></tr>"
  if (val4 != "") {
    pr += "<tr><td valign='top'>Post Code:</td><td>"+ val4 +"</td></tr>"
  }
  if (val6 != "") {
    pr += "<tr><td valign='top'>Tel:</td><td>"+ val6 +"</td></tr>"
  }

  if (val5 != "") {
    pr += "<tr><td colspan='2'><img src='imagemember/"+ val5 +"' /></td></tr>"
  }
  
  
  pr += "</table>"
  pr += "</td></tr></table>"
  return pr
}

function ShowHelp(val1) {

  var pr = "";

  pr += "<table class='leg2Tab' cellpadding='0' cellspacing='10' width='206'><tr><td>"
  pr += "<table cellspacing='0' cellpadding='0'>"
  pr += "<tr><td>"+ val1 +"</td></tr>"
  pr += "</table>"
  pr += "</td></tr></table>"
  return pr

}


function phonoff(val) {
  var tabObj, qqChose

  var btnOn = document.getElementById("btnOn");
  var btnOff = document.getElementById("btnOff");

  if (tabObj = document.getElementById("rlTabid")) {
     if (val == 1) {
       tabObj.className = "rlTab";         
       btnOn.style.color="#ccc";
       btnOff.style.color="#407070";
     } else {
       tabObj.className = "rlTabhide";
       btnOn.style.color="#407070";
       btnOff.style.color="#ccc";
     }
  }

  var dateExpire = new Date();
  dateExpire.setMonth(dateExpire.getMonth()+1);
  qqChose = "photoswitch=" + val;
  qqChose += ";expires=" + dateExpire.toGMTString;
  document.cookie = qqChose;

}

/*----------------------ajax function for list -----------------------*/

function sendData(param, page, target) {

  if(document.all) {
    //IE
    var XhrObj = new ActiveXObject("Microsoft.XMLHTTP") ;
  } else {
    //Mozilla
    var XhrObj = new XMLHttpRequest();
  }

  var content = document.getElementById(target);
  XhrObj.open("POST", page);

  XhrObj.onreadystatechange = function() {
    if (XhrObj.readyState == 4 && XhrObj.status == 200) {
	  content.innerHTML = XhrObj.responseText;
    }
  }

  XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  XhrObj.send(param);
}



/*---------------------------------------------------------------------*/

function newTown() {

  var form = document.form1;
  var id
  if (form.CountyID.selectedIndex == 0) {
    alert('The following error(s) occurred:\n- County is required');
  } else {
    id = form.CountyID.value;
    popup('memberadtown.asp?CountyID='+id,300,200);
  }
}

//page memberadtown.asp
function sendDataPopup(param, page, target, param2) {

  if(document.all) {
    var XhrObj = new ActiveXObject("Microsoft.XMLHTTP") ;
  } else {
    var XhrObj = new XMLHttpRequest();
  }

  XhrObj.open("POST", page);

  XhrObj.onreadystatechange = function() {
    if (XhrObj.readyState == 4 && XhrObj.status == 200) {
	  target.innerHTML = XhrObj.responseText;
      
      
      var sel = target.firstChild.getAttribute('name');
      var opt = target.getElementsByTagName('option');
      var indexSelect = 0;
      //sel.option[3]="selected";
      for (i=0; i< opt.length; i++) {
        if (opt[i].getAttribute('value') == param2) {
           indexSelect = i;
        }
      }
      //----- > form name =-> 'form1'
      window.opener.document['form1'].elements[sel].options[indexSelect].selected = "selected";
      location.replace('closepopup.asp');
      //do not use window.close(); pb with firefoxe
    }
  }
  XhrObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  XhrObj.send(param);
}


/*-------------------------search---------------------------------*/

function advSearch(val) {
  if (val) {
    document.getElementById("tabAdvSearch").style.display="block";
  } else {
    document.getElementById("tabAdvSearch").style.display="none";
  }

  document.getElementById("FacID").style.width="100%";
  document.getElementById("SubFacID").style.width="100%";
}

//obj.className = s+"-over";


function XMLHttpBrouser() {

  if(document.all) {
    var XhrObj = new ActiveXObject("Microsoft.XMLHTTP") ;
  } else {
    var XhrObj = new XMLHttpRequest();
  }

  return XhrObj;
}


function getVal(cas, i) {
  var val = 0;
  if (cas <= i) {
    val = document.getElementById("list"+i).firstChild.value;   
  }
  return val
}


function sendDataSearch(nb, flg, v1, v2, v3, v4, v5) {

  var XhrObj2 = XMLHttpBrouser();
  var XhrObj3 = XMLHttpBrouser();
  var XhrObj4 = XMLHttpBrouser();
  var XhrObj5 = XMLHttpBrouser();

  var val1, val2, val3, val4;
  var flgv2 = true;
  var flgv3 = true;
  var flgv4 = true;
  var flgv5 = true;
  

  switch (nb) {
    case 1:
      val1 = document.forms["search"].elements["CountyID"].value;
      val2 = 0;
      val3 = 0;
      val4 = 0;
    break;
    case 2:
      val1 = document.forms["search"].elements["CountyID"].value;
      val2 = document.forms["search"].elements["TownID"].value;
      val3 = 0;
      val4 = 0;
    break;
    case 3:
      val1 = document.forms["search"].elements["CountyID"].value;
      val2 = document.forms["search"].elements["TownID"].value;
      val3 = document.forms["search"].elements["CuisineID"].value;
      val4 = 0;
    break;
    case 4:
      val1 = document.forms["search"].elements["CountyID"].value;
      val2 = document.forms["search"].elements["TownID"].value;
      val3 = document.forms["search"].elements["CuisineID"].value;
      val4 = document.forms["search"].elements["FacID"].value;
    break;
  }

  //Change Town
  if ((nb < 2 && flg == 0) || (nb == 1 && flg==1)) {
 
    XhrObj2.open("POST", "s2.asp");
    XhrObj2.onreadystatechange = function() {
      if (XhrObj2.readyState == 4 && XhrObj2.status == 200) {
        eval(XhrObj2.responseText);
        sendDataSearch((nb+1), flg, v1, v2, v3, v4, v5);
      }
    }
    XhrObj2.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    XhrObj2.send("id2="+val1+"&flg="+flg+"&v2="+v2);
  }

  //Change Cuisine
  if ((nb < 3 && flg == 0) || (nb == 2 && flg==1)) {

    XhrObj3.open("POST", "s3.asp");
    XhrObj3.onreadystatechange = function() {
      if (XhrObj3.readyState == 4 && XhrObj3.status == 200) {
        eval(XhrObj3.responseText);
        if (flg > 0) {
            sendDataSearch((nb+1), flg, v1, v2, v3, v4, v5);
        }
      }
    }
    XhrObj3.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    XhrObj3.send("id3="+val2+"&id2="+val1+"&flg="+flg+"&v3="+v3);
  }

  //Change Facilities level1
  if ((nb < 4 && flg == 0) || (nb == 3 && flg==1)) {

    XhrObj4.open("POST", "s4.asp");
    XhrObj4.onreadystatechange = function() {

      if (XhrObj4.readyState == 4 && XhrObj4.status == 200) {
          eval(XhrObj4.responseText);
        if (flg > 0) {
            sendDataSearch((nb+1), flg, v1, v2, v3, v4, v5);
        }
      }
    }
    XhrObj4.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    XhrObj4.send("id3="+val3+"&id2="+val2+"&id1="+val1+"&flg="+flg+"&v4="+v4);
  }

  //Change Facilities level2
  if ((nb < 5 && flg == 0) || (nb == 4 && flg==1)) {
    XhrObj5.open("POST", "s5.asp");
    XhrObj5.onreadystatechange = function() {
      if (XhrObj5.readyState == 4 && XhrObj5.status == 200) {
          eval(XhrObj5.responseText);
      }
    }
    XhrObj5.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    XhrObj5.send("id4="+val4+"&id3="+val3+"&id2="+val2+"&id1="+val1+"&flg="+flg+"&v5="+v5);
  }
}


function initsearch() {
  sendDataSearch(1, 1, 0, 0, 0, 0, 0);
  document.forms["search"].elements["CountyID"].options[0].selected = true;
}