function createErrors() {
  if (document.ieMac) return;

  var c = document.createElement('div');
  c.id = 'errors';
  c.style.display = "none";

  var top = document.createElement('div');
  top.id = 'errors-top';

  var y = document.createElement('div');
  y.id = 'errors-container';

  var bot = document.createElement('div');
  bot.id = 'errors-bot';

  c.appendChild(top)

  var spacer = document.createElement('div');
  spacer.innerHTML = "<img src='images/spacer.gif' width='225' height='1' alt='' class='clear' />";
  y.appendChild(spacer);

  var g = document.createElement('div');
  g.id = 'errorListContainer';

  var u = document.createElement('ul');
  u.id = "errorList";

  // # FutureDateError #
  var li_1 = document.createElement('li');
  li_1.id = "FutureDateError";
  li_1.style.display = "none";
  //var title_1 = document.createTextNode('ensure that the arrival date is a date in the future');
  var title_1 = document.createElement('div');
  var title_link_1 = document.createElement('a');
  title_1.appendChild(document.createTextNode('ensure that the '));
  title_link_1.appendChild(document.createTextNode('arrival date'));
  addEvent(title_link_1, 'click', function () { ScrollTo('CheckInDay') });
  title_link_1.href = "javascript:void(0);";
  title_1.appendChild(title_link_1);
  title_1.appendChild(document.createTextNode(' is a date in the future'));
  li_1.appendChild(title_1);

  // # ArrivalDateError #
  var li_2 = document.createElement('li');
  li_2.id = "ArrivalDateError";
  li_2.style.display = "none";
  var title_2 = document.createTextNode('ensure that the departure date is after the arrival date');
  li_2.appendChild(title_2);

  // # FullnameError #
  var li_3 = document.createElement('li');
  li_3.id = "FullnameError";
  li_3.style.display = "none";
  var title_3 = document.createTextNode('ensure that the "Fullname" field is entered correctly');
  li_3.appendChild(title_3);

  // # EmailError #
  var li_4 = document.createElement('li');
  li_4.id = "EmailError";
  li_4.style.display = "none";
  var title_4 = document.createTextNode('ensure that the "Email Address" field is entered correctly');
  li_4.appendChild(title_4);

  // # CountryError #
  var li_5 = document.createElement('li');
  li_5.id = "CountryError";
  li_5.style.display = "none";
  var title_5 = document.createTextNode('ensure that the "Other Country" field is entered correctly');
  li_5.appendChild(title_5);

  // # TelephoneError #
  var li_6 = document.createElement('li');
  li_6.id = "TelephoneError";
  li_6.style.display = "none";
  var title_6 = document.createTextNode('ensure that the "Telephone" field is entered correctly');
  li_6.appendChild(title_6);

  // # SecurityCodeError #
  var li_7 = document.createElement('li');
  li_7.id = "SecurityCodeError";
  li_7.style.display = "none";
  var title_7 = document.createTextNode('ensure that the security code is entered');
  li_7.appendChild(title_7);

  u.appendChild(li_1);
  u.appendChild(li_2);
  u.appendChild(li_3);
  u.appendChild(li_4);
  u.appendChild(li_5);
  u.appendChild(li_6);
  u.appendChild(li_7);

  g.appendChild(u);
  y.appendChild(g);
  c.appendChild(y)
  c.appendChild(bot);
  //alert(c.id)
  //alert(c.innerHTML)
  $('errors-area').appendChild(c);
}

addEvent(window, 'load', function () { createErrors(); });

var ratesXML;
var ratesAvailable = false;
var requestRates = new Object();
var requestRatesTimeout;
var validPeriod = false;
var validAc = false;
var validAcs = new Array(0);
function getRates () {
  requestRatesTimeout = window.setTimeout("abortRates();", 15000);
  var requestRates = new callAjax("get", "rates.xml", "", processRates, "xml", null);
}

function abortRates() {
  alert("The request has timed out.\nCannot retreve rates.");
  requestRates = false;
}

function processRates(xmldoc) {
  clearTimeout(requestRatesTimeout)
  var xmldoc = xmldoc.documentElement;
  ratesXML = xmldoc;
  var periods = ratesXML.getElementsByTagName('period');
  if (isDefined(periods)) {
    for (var i = 0; i < periods.length; i++) {
	  if (!(validPeriod)) {
        var start = new Date(periods[i].getAttribute('validFromYear'), periods[i].getAttribute('validFromMonth'), periods[i].getAttribute('validFromDay'));
        var end = new Date(periods[i].getAttribute('validTillYear'), periods[i].getAttribute('validTillMonth'), periods[i].getAttribute('validTillDay'));

        if ((start.getTime()<today.getTime()) && (today.getTime()<end.getTime())) {
          validPeriod = periods[i];
        }
	  }
    }
    if (validPeriod) {
	  ratesAvailable = true;
	  addEBAvailable();
	} else {
      ratesAvailable = false;
	}
  } else {
    ratesAvailable = false;
  }

  var ac = ratesXML.getElementsByTagName('ac');
  if (isDefined(ac)) {
    for (var i = 0; i < ac.length; i++) {
	  validAc = true;
	  addACAvailable(ac[i].getAttribute('unitcount'), ac[i].getAttribute('pricevalue'));
    }
  }

  daysOfStay();
}

function addACAvailable (unitcount, pricevalue) {
  var opt = document.createElement('option');
  opt.value = pricevalue;
  opt.rel = unitcount;

  opt.innerHTML = nt(unitcount) + ' unit' + (unitcount == 1 ? '' : 's') + ' (€' + pricevalue + ' per day)';
  $('airconditioning').appendChild(opt);
}

function addEBAvailable () {
  if (!validPeriod) return;
  var apartments = validPeriod.getElementsByTagName('apartment');
  var p = 0;
  $A(apartments).each(function(apartment) {
    p++;
	var opt = document.createElement('option');
	opt.value = p;
	opt.innerHTML = nt(p);
	$('extra_bed').appendChild(opt);
  });
}

var ai = 0;
var calculateDaysProccess;
function daysOfStay(){
  if (!$('CheckOutYear')) return
  calculateDaysProccess = window.setTimeout("daysOfStay()", 500)
  var COY, COM, COD, CIY, CIM, CID, ap_type, aircon, arco, ebed;
  COY = $('CheckOutYear').value;
  COM = getMonthFromText($('CheckOutMonth').value);
  COD = $('CheckOutDay').value;
  CIY = $('CheckInYear').value;
  CIM = getMonthFromText($('CheckInMonth').value);
  CID = $('CheckInDay').value;
  //$A($('acc_id')).each(function(opt){if(opt.selected){if(opt.value==3){ap_type=1}else{ap_type=opt.value}}});
  ap_type = ($('acc_id').value == 3 ? 1 : $('acc_id').value);
  aircon = $('airconditioning').value;
  airopts = $('airconditioning').getElementsByTagName('option');
  var w = 0;
  while (w < airopts.length) {
	if (airopts[w].selected) {
	  var arco = airopts[w].rel;
	}
	w++;
  }

  if (isUndefined(arco)) {
	var arco = 0;
  }

  ebed = $('extra_bed').value;

  var start = new Date(CIY, CIM-1, CID);
  var end = new Date(COY, COM-1, COD);

  if (ratesAvailable) {
    var bedrooms, price, people;
    var apartments = validPeriod.getElementsByTagName('apartment');
	$A(apartments).each(function(apartment) {
	  bedrooms = apartment.getElementsByTagName('bedroom')[0].getAttribute('number');
	  if (bedrooms == ap_type) {
        price = apartment.getElementsByTagName('price')[0].getAttribute('value');
        people = apartment.getElementsByTagName('people')[0].getAttribute('capacity');
	  }
	});
  }
  
  var error = false;
  var result = false;
  if (start.getTime()<today.getTime()) {
    //alert("This should be a date in the future.");
    //$('CheckInDay').focus();
    //return;
    error = "<span>The arrival date should be a date in the future.</"+"span>";
  }

  if ((end - start) < 0) {
    //alert("This should be a date after the arrival.");
    //$('CheckOutDay').focus();
    //return;
    error = "<span>The departure date should be after the arrival date.</"+"span>";
  }
  
  /*alert(start.getDate() + " : " + today.getDate() + "\n" +
        start.getMonth() + " : " + today.getMonth() + "\n" +
        start.getFullYear() + " : " + today.getFullYear())*/

  result = Math.ceil((end - start)/millisOfDay);

  if (result < 0) error = "<span>Please enter valid check in/check out dates.</"+"span>";

  //if (result == 0) result = false;
  if (result > 0) {
    if (ratesAvailable) {
      var nights = result// + 1;
      /*if (result == 1) result = result + ' night';
      else if (result > 1)  result = result + ' nights';
      else return;*/
  
      var cost = nights * price;
	  var fix = nights == 1 ? "night" : "nights";

      if (arco > 0)
	    cost = cost + (aircon * nights);

      var ebl = ((ebed == 1) ? 'extra bed' : 'extra beds');
	  $('extra_bed_label').firstChild.nodeValue = ebl;

      //if (ai < 10) { alert($('extra_bed_label').innerHTML); ai++; }

      if (ebed > 0)
	    cost = cost + ((ebed * validPeriod.getAttribute('extraBed')) * nights);

      result = nights + " " + fix +
	           " at <strong>EUR€" + price + "<" + "/strong>" + 
			   (ebed > 0 ? " + " + nt(ebed) + " " + ebl : '') + 
			   (arco > 0 ? " + " + nt(arco) + " a/c unit" + (arco>1?'s':'') + " <strong>EUR€" + aircon + "<" + "/strong>" : '') +
			   " per night<br " + "/> Total is <strong style='text-decoration: underline'>EUR€" + cost + "<" + "/strong>";
    } else {
      var nights = result// + 1;
	  var fix = nights == 1 ? "night" : "nights";
      result = nights + " " + fix;
    }
  }
  if (result == 0) result = '';

  $('daysOfStay').style.display = "block";
  $('extra_bed_label').style.clear = "none";
  $('extra_bed_label').style.display = "inline";
  if (error) $('daysOfStay').innerHTML = error;
  else $('daysOfStay').innerHTML = result;
}

function extraBeds (n) {

}

addEvent(window, 'load', function () {getRates()})

//addEvent(window, 'load', function () {doValidation()})

function doFormValidation (uniqueid, formid) {
  var COY, COM, COD, CIY, CIM, CID;
  $A($('CheckOutYear')).each(function(opt){if(opt.selected){COY=opt.innerHTML}});
  $A($('CheckOutMonth')).each(function(opt){if(opt.selected){COM=getMonthFromText(opt.innerHTML)}});
  $A($('CheckOutDay')).each(function(opt){if(opt.selected){COD=opt.innerHTML}});
  $A($('CheckInYear')).each(function(opt){if(opt.selected){CIY=opt.innerHTML}});
  $A($('CheckInMonth')).each(function(opt){if(opt.selected){CIM=getMonthFromText(opt.innerHTML)}});
  $A($('CheckInDay')).each(function(opt){if(opt.selected){CID=opt.innerHTML}});

  var start = new Date(CIY, CIM-1, CID);
  var end = new Date(COY, COM-1, COD);
  var validationCorrect = true;

  if (start.getTime()<today.getTime()) {
    //alert("This should be a date in the future.");
    //$('CheckInDay').focus();
    showError('FutureDateError');
    validationCorrect = false;
  } else {
    hideError('FutureDateError');
  }

  if ((end - start) < 0) {
    //alert("This should be a date after the arrival.");
    //$('CheckOutDay').focus();
    showError('ArrivalDateError');
    validationCorrect = false;
  } else {
    hideError('ArrivalDateError');
  }

  if ($('fullname').value.trim() == '') {
    //alert("This should not be empty.");
    //$('fullname').focus();
    showError('FullnameError');
    validationCorrect = false;
  } else {
    hideError('FullnameError');
  }

  if (!checkmail($('email').value.trim())) {
    //alert("This should not be empty.");
    //$('fullname').focus();
    showError('EmailError');
    validationCorrect = false;
  } else {
    hideError('EmailError');
  }

  if ($('country').value == 'other') {
    if ($('other_country_field').value.trim() == '') {
      //alert("This should not be empty.");
      //$('other_country_field').focus();
      showError('CountryError');
      validationCorrect = false;
    } else {
      hideError('CountryError');
    }
  }

  if ($('telephone').value.trim() == '') {
    //alert("This should not be empty.");
    //$('telephone').focus();
    showError('TelephoneError');
    validationCorrect = false;
  } else {
    hideError('TelephoneError');
  }

  if (!isNumeric(removeSpaces($('telephone').value))) {
    //alert("This should contain only numbers.");
    //$('telephone').focus();
    showError('TelephoneError');
    validationCorrect = false;
  } else {
    hideError('TelephoneError');
  }

  if ($('security_code_' + uniqueid).value.trim().length != 5) {
    //alert("This should not be empty.");
    //$('telephone').focus();
    showError('SecurityCodeError');
    validationCorrect = false;
  } else {
    hideError('SecurityCodeError');
  }

  //if ($('telephone').value + 1 == 'NaN') {
    //alert("This should be a number.");
    //$('telephone').focus();
    //return false;
  //}
  
  if (validationCorrect) {
    hideErrorBlock();
    return true;
  } else {
    return false;
  }
}
