
  // <![CDATA[


Calendar.setup(
    {
      inputField  : "from",         // ID of the input field
      ifFormat    : "%Y-%m-%d",    // the date format
      button      : "from",
      onUpdate	  : test,
      dateStatusFunc : dateStatus
    }
  );
  Calendar.setup(
    {
      inputField  : "to",         // ID of the input field
      ifFormat    : "%Y-%m-%d",    // the date format
      button      : "to",
      onUpdate	  : test,
      dateStatusFunc : dateStatus
    }
  );

  Calendar.setup(
		    {
		      inputField  : "from",         // ID of the input field
		      ifFormat    : "%Y-%m-%d",    // the date format
		      button      : "fromb",
		      onUpdate	  : test,
		      dateStatusFunc : dateStatus
		    }
		  );
		  Calendar.setup(
		    {
		      inputField  : "to",         // ID of the input field
		      ifFormat    : "%Y-%m-%d",    // the date format
		      button      : "tob",
		      onUpdate	  : test,
		      dateStatusFunc : dateStatus
		    }
		  );

function dateStatus(date) {
	var min = new Date();
	if (date.getTime() < min.getTime() - 1*24*60*60*1000)
		return true; // true says "disable"
	else
		return false; // leave other dates enabled
}

function test(cal) { // End date must be after from date
       var date = cal.date;
       var from = document.getElementById("from");
       var to = document.getElementById("to");
       // Because it take wrong format
       var first = to.value.split("-");
       var second = from.value.split("-");
       var field = new Date(first[0], first[1]-1, first[2]);
       var field2 = new Date(second[0], second[1]-1, second[2]);
       if (field.getTime() < field2.getTime() || (from.value == "" || to.value =="")) {
       		var time = date.getTime();
		var date2 = new Date(time);
		if (from.value == "") {
		var date3 = new Date(time - Date.DAY);
       		from.value = date3.print("%Y-%m-%d");
		to.value = date2.print("%Y-%m-%d");
		} else {
			var date3 = new Date(time + Date.DAY);
			to.value = date3.print("%Y-%m-%d");
			from.value = date2.print("%Y-%m-%d");
		}
	}
}

function validateBooking() {
    var storno = new Array('location', 'from', 'to', 'people');
    var el;
    var noerror = true;
    for ( var i=storno.length-1; i>=0; --i ){
        el = document.getElementById(storno[i]);
        if(el.value == '') {
            el.style.backgroundColor = 'red';
            noerror = false;
        } else {
            el.style.backgroundColor = '';
        }
    }
    if (noerror == true) {
        if (isDate(document.getElementById('from').value) && isDate(document.getElementById('to').value)) {
            if (isDateInPast()) {
              alert('Dates cannot be in the past.');
              document.getElementById('from').style.backgroundColor = 'red';
              document.getElementById('to').style.backgroundColor = 'red';
              return false;
            } else {
              return true;
            }
        } else {
          alert('Please fill in the dates in the YYYY-MM-DD format.');
          document.getElementById('from').style.backgroundColor = 'red';
          document.getElementById('to').style.backgroundColor = 'red';
          return false;
        }
    } else {
        alert('Please fill in all obligatory fields.');
        return noerror;
    }
}

function isDate(sDate) {
   var re = /^\d{1,4}\-\d{1,2}\-\d{1,2}$/
   return re.test(sDate);
}

function isDateInPast() {
       var from = document.getElementById("from");
       var to = document.getElementById("to");

       var first = to.value.split("/");
       var second = from.value.split("/");
       var field = new Date(first[2], first[1]-1, first[0]);
       var field2 = new Date(second[2], second[1]-1, second[0]);
       var today = new Date();
       var today2 = new Date(today.getFullYear(), today.getMonth(), today.getDate());
       //alert(today2);
       today = today2.getTime();
       //alert(today + ' ' + field2.getTime());
       return (field.getTime() < today || field2.getTime() < today);
}

function ownSubmit() {
  if (document.getElementById('location').value == 1337) {
    document.resform.submit();
  } else {
    var ONE_DAY = 1000 * 60 * 60 * 24;
    var startDate = document.getElementById('from').value;
    var from = document.getElementById('from').value;
    var parts = from.split('-');
    var date1 = new Date(parts[0], parts[1]-1, parts[2]);
    var date1_ms = date1.getTime();
    parts = document.getElementById('to').value.match(/(\d+)/g);
    var date2 = new Date(parts[0], parts[1]-1, parts[2]);
    var date2_ms = date2.getTime();
    var days = Math.round(Math.abs(date2_ms - date1_ms)/ONE_DAY);
    var guests = document.getElementById('people').value;
    var town = document.getElementById('location')[document.getElementById('location').selectedIndex].value;
    var rooms = 1;
    var queryString = 'startDate=' + startDate + '&days=' + days + '&guests=' + guests + '&rooms='+rooms+'&currencyID=1&type=2&tid='+town+'&affid=223';
    if(town!="0"&&town!="-1"){
      window.open('http://partners.hotelcalculator.com/en/search.aspx?' + queryString,"_blank");
    }
  }
}

function GetXmlHttpObject(handler)
{
   var objXMLHttp=null
   if (window.XMLHttpRequest)
   {
       objXMLHttp=new XMLHttpRequest()
   }
   else if (window.ActiveXObject)
   {
       objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
   }
   return objXMLHttp
}

function stateChanged()
{
   if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
   {
	  var element = document.getElementById('location');
	  while(element.hasChildNodes()){
	    element.removeChild(element.childNodes[0])
	  }
           if (xmlHttp.responseText != '') {
	      var split = xmlHttp.responseText.split(';');
	      var tmp;
	      for(i = 0; i < split.length; i++) {
		tmp = split[i].split('|');
		element.options[element.length] = new Option(tmp[1], tmp[0]);
	      }
	      element.disabled = false;
	      /*document.getElementById("location").innerHTML=xmlHttp.responseText;
	      document.getElementById("location").disabled = false;*/
	      
	   } else {
	    element.options[element.length] = new Option('Select city', '');
	    element.disabled = true;
	    /*document.getElementById("location").innerHTML = '<option value="">Select city</option>';
	    document.getElementById("location").disabled = true;*/
	   }
   }
   else {
           //alert(xmlHttp.status);
   }
}

// Will populate data based on input
function getCity(url, qStr)
{
   if (url.length==0)
   {
       //document.getElementById("location").innerHTML="";
       return;
   }
   xmlHttp=GetXmlHttpObject()
   if (xmlHttp==null)
   {
       alert ("Browser does not support HTTP Request");
       return;
   }

   url=url+"?"+qStr;
   url=url+"&sid="+Math.random();
   xmlHttp.onreadystatechange=stateChanged;
   xmlHttp.open("GET",url,true) ;
   xmlHttp.send(null);
}




  // ]]>
