var xmlHttp;
var proceed = false;
var isActive = false;

var myAnimationManager = new AnimationManager();

function createRequest(){
  if(window.ActiveXObject){ xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  else if(window.XMLHttpRequest){ xmlHttp = new XMLHttpRequest(); }
}

function StateChange(){
  if(xmlHttp.readyState == 4){
	isActive = false;
    var text = xmlHttp.responseText;
    if(text == "" || text == "\n" || text == " "){
      document.booking.submit();
    } else {
	  var bookingLoader = document.getElementById('JS_Booking_Loader');
	  var bookingContent = document.getElementById('JS_Booking_Content');
	  
	  bookingLoader.style.backgroundPosition = "-50px -50px";
	  
	  myAnimationManager.deleteAnimationByName('booking-form');
	  myAnimationManager.addAnimation(bookingContent, 'alpha', 1, 20, 1, 1, 0, true, bookingContent.style.opacity*100, 100, null, 'booking-form');
      document.getElementById('error').innerHTML = '';
      document.getElementById('error').innerHTML = text;
      document.getElementById('error').style.display = 'block';
      proceed = false;
    }
  }
}

function validateBooking(thisform, vLang){
  if (!isActive) {
	  isActive = true;
	  with(thisform){
		createRequest();
		var url = '/include/check.php';
		var postVar = 'arrival_year='+arrival_year.value;
		postVar += '&arrival_month='+arrival_month.value;
		postVar += '&arrival_day='+arrival_day.value;
		postVar += '&number_of_nights='+number_of_nights.value;
		postVar += '&guests='+guests.value;
		postVar += '&lang='+vLang;
		postVar += '&rooms='+rooms.value;
		xmlHttp.open("POST", url, true);
		xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.onreadystatechange = StateChange;
		xmlHttp.send(postVar);
		document.getElementById('error').style.display = 'none';
		document.getElementById('error').innerHTML = '';
		
	  }
	  
	  var bookingLoader = document.getElementById('JS_Booking_Loader');
	  var bookingContent = document.getElementById('JS_Booking_Content');
	  
	  bookingLoader.style.backgroundPosition = "0px 0px";
	  
	  if (typeof bookingContent.style.opacity != "number") myAnimationManager.setAlpha(bookingContent,100);
	  myAnimationManager.deleteAnimationByName('booking-form');
	  myAnimationManager.addAnimation(bookingContent, 'alpha', -1, -1, -20, -1, 0, false, bookingContent.style.opacity*100, 10, null, 'booking-form');
  }
  return proceed;
}
