function calendarPicker(month, year, apt_id, divid, dividfrom, x, y)
{
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    xmlHttp.onreadystatechange=function()
    {
      if(xmlHttp.readyState==4)
      {
        x = x-10;
        y = y-10;
			  document.getElementById('cmd_calendar_small_picker').innerHTML = xmlHttp.responseText;
			  document.getElementById('cmd_calendar_small_picker').style.clear = "both";
        document.getElementById('cmd_calendar_small_picker').style.position = "absolute";
			  document.getElementById('cmd_calendar_small_picker').style.display = "block";
			  document.getElementById('cmd_calendar_small_picker').style.left = x+"px";
			  document.getElementById('cmd_calendar_small_picker').style.top = y+"px";
			  document.getElementById('cmd_calendar_small_picker').style.zIndex = 99;
      }
    }
    url = "modules/apt/frontend/scripts/php/return_calendar.php";
    url += "?calendar="+month+"|"+year+"|"+apt_id+"|"+divid+"|"+dividfrom;
		xmlHttp.open("GET",url,true);
	  xmlHttp.send(null);
}

function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}

function cmdSetDates(date, divid, dividfrom, apt_id)
{
  var divid = document.getElementById(divid);
  divid.style.display = "none";
  var datum = explode(" ", date);
  document.getElementById(dividfrom).value = datum[0]+" "+returnMonth(datum[1])+" "+datum[2];
  modifyPrice(apt_id);
}

/**
 *
 * @access public
 * @return void
 **/
function modifyPrice(apt_id)
{
  var today=new Date();
  var dateFrom=new Date();
  var dateTo=new Date();
  var price = document.getElementById('cmd_priceFiled');
  var from = document.getElementById('cmd_fromDate');
  var to = document.getElementById('cmd_toDate');
  var pay_link = document.getElementById('cmd_paylink');

  var numppl = document.getElementById('cmd_numppl');
  var cmd_numppl = numppl.options[numppl.selectedIndex].value;

  //pay_link.style.display = 'none';

  fromArray = explode(" ", from.value);
  toArray = explode(" ", to.value);
  dateFrom.setFullYear(fromArray[2],returnMonth(fromArray[1])-1,fromArray[0]);
  dateTo.setFullYear(toArray[2],returnMonth(toArray[1])-1,toArray[0]);
  if(dateFrom < today)
  {
    from.value = today.getDate()+" "+returnMonth((today.getMonth()+1))+" "+today.getFullYear();
    dateFrom = today;
  }
  else if(dateTo >= dateFrom)
  {

    var numppl = document.getElementById('cmd_numppl');
    var cmd_numppl = numppl.options[numppl.selectedIndex].value;
    var diff = dateTo-dateFrom;
    diff=Math.round(diff/1000/60/60/24);
    callPriceSetup(fromArray[2]+"-"+returnMonth(fromArray[1])+"-"+fromArray[0], toArray[2]+"-"+returnMonth(toArray[1])+"-"+toArray[0], apt_id, diff, cmd_numppl);
    //pay_link.onClick = "window.location.href='"+pay_link.href+"/"+fromArray[2]+"-"+returnMonth(fromArray[1])+"-"+fromArray[0]+"/"+toArray[2]+"-"+returnMonth(toArray[1])+"-"+toArray[0]+"/"+cmd_numppl+"'";
    pay_link.style.display = 'block';
  }
  else
  {
    price.value = 'none';
    document.getElementById('number_of_nights').value = "0";
    document.getElementById('price_per_night').value = "none";
  }
}


function changebooklink(apt_id)
{
  var today=new Date();
  var dateFrom=new Date();
  var dateTo=new Date();
  var price = document.getElementById('cmd_priceFiled');
  var from = document.getElementById('cmd_fromDate');
  var to = document.getElementById('cmd_toDate');
  var pay_link = document.getElementById('cmd_paylink');

  var numppl = document.getElementById('cmd_numppl');
  var cmd_numppl = numppl.options[numppl.selectedIndex].value;

  //pay_link.style.display = 'none';

  fromArray = explode(" ", from.value);
  toArray = explode(" ", to.value);
  dateFrom.setFullYear(fromArray[2],returnMonth(fromArray[1])-1,fromArray[0]);
  dateTo.setFullYear(toArray[2],returnMonth(toArray[1])-1,toArray[0]);
  if(dateFrom < today)
  {
    from.value = today.getDate()+" "+returnMonth((today.getMonth()+1))+" "+today.getFullYear();
    dateFrom = today;
  }
  else if(dateTo >= dateFrom)
  {

    var numppl = document.getElementById('cmd_numppl');
    var cmd_numppl = numppl.options[numppl.selectedIndex].value;
    var diff = dateTo-dateFrom;
    diff=Math.round(diff/1000/60/60/24);
    callPriceSetup(fromArray[2]+"-"+returnMonth(fromArray[1])+"-"+fromArray[0], toArray[2]+"-"+returnMonth(toArray[1])+"-"+toArray[0], apt_id, diff, cmd_numppl);
    //pay_link.onClick = "window.location.href='"+pay_link.href+"/"+fromArray[2]+"-"+returnMonth(fromArray[1])+"-"+fromArray[0]+"/"+toArray[2]+"-"+returnMonth(toArray[1])+"-"+toArray[0]+"/"+cmd_numppl+"'";
    pay_link.style.display = 'block';
  }
  else
  {
    price.value = 'none';
    document.getElementById('number_of_nights').value = "0";
    document.getElementById('price_per_night').value = "none";
  }



//  var pay_link = document.getElementById('cmd_paylink');
//  var parts_pay_link = explode("params=", pay_link.onClick);
//  var numppl = document.getElementById('cmd_numppl');
//  var cmd_numppl = numppl.options[numppl.selectedIndex].value;
//  var rest = explode("/", parts_pay_link[1]);
//  pay_link.onClick = "window.location.href='"+parts_pay_link[0]+"params=/"+rest[1]+"/"+rest[2]+"/"+rest[3]+"/"+cmd_numppl+"'";
}

function explode( delimiter, string ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: kenneth
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}

    var emptyArray = { 0: '' };

    if ( arguments.length != 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }

    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }

    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }

    if ( delimiter === true ) {
        delimiter = '1';
    }

    return string.toString().split ( delimiter.toString() );
}

function callPriceSetup(from, to, apt_id, diff, cmd_numppl)
{
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    xmlHttp.onreadystatechange=function()
    {
      if(xmlHttp.readyState==4)
      {
        
			  document.getElementById('cmd_priceFiled').value = xmlHttp.responseText;
        document.getElementById('cmd_cover_page').style.display = "none";
        price = document.getElementById('cmd_priceFiled').value;
                        
        if(price == "none")
        {                                                     
          document.getElementById('number_of_nights').value = "0";
          document.getElementById('price_per_night').value = "none";
        }
        else
        {
          document.getElementById('number_of_nights').value = diff;
          document.getElementById('price_per_night').value = price/diff;
        }
        
      }
    }
    document.getElementById('cmd_cover_page').style.display = "block";
    url = "modules/apt/frontend/scripts/php/price.php";
    url += "?price="+from+"|"+to+"|"+apt_id+"|"+diff+"|"+cmd_numppl;
		xmlHttp.open("GET",url,true);
	  xmlHttp.send(null);
}

function changeCountry(divid, dividtowns)
{
  var country = document.getElementById(divid);
  var xmlHttp;
  try
    {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
    }
  catch (e)
    {
    // Internet Explorer
    try
      {
      	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
        	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
      catch (e)
        {
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    xmlHttp.onreadystatechange=function()
    {
      if(xmlHttp.readyState==4)
      {
        x = document.getElementById(dividtowns);
        for(var j=0; j <= 100; j++)
        {
          x.remove(x.j);
        }
			  var response = xmlHttp.responseText;
			  var towns_array = explode(";", response);
			  if(response != "")
			  {
			   	y=document.createElement('option');
          y.text = "Select Town";
          y.value = "";
          try { x.add(y,null); }
          catch(ex){ x.add(y); }
			   for (var i=0; i < towns_array.length-1; i++)
			   {
			     town = explode("|", towns_array[i]);
			     y=document.createElement('option');
			     y.text = town[1];
			     y.value= town[0];
			     try { x.add(y,null); }
           catch(ex) { x.add(y); }
			   }
			  }
			  else
			  {
			    y=document.createElement('option');
			       y.text = "Select Town";
			       y.value = "";
			       try { x.add(y,null); }
             catch(ex){ x.add(y); }
			  }
      }
    }
    url = "modules/apt/frontend/scripts/php/towns.php";
    url += "?country_id="+country.options[country.selectedIndex].value;
		xmlHttp.open("GET",url,true);
	  xmlHttp.send(null);
}

/**
 *
 * @access public
 * @return void
 **/
function bookNow()
{

  var from = document.getElementById('cmd_fromDate');
  var to = document.getElementById('cmd_toDate');

  var fromArray = explode(" ", from.value);
  var toArray = explode(" ", to.value);
  from = fromArray[2] +"-"+ returnMonth(fromArray[1]) + "-" + fromArray[0];
  to = toArray[2] + "-" + returnMonth(toArray[1]) + "-" + toArray[0];

  var numppl = document.getElementById('cmd_numppl');
  var cmd_numppl = numppl.options[numppl.selectedIndex].value;


  var pay_link = document.getElementById('apt_paylink');
  pay_link = explode(".html", pay_link.value);
  var id = explode("params/", window.location.href);
  id = id[1];
  id = explode("#", id);
  id = id[0];                     
  if(document.getElementById('cmd_priceFiled').value != "none")
  window.location.href= pay_link[0]+".html/params/"+id+"/"+from+"/"+to+"/"+cmd_numppl;


}

/**
 *
 * @access public
 * @return void
 **/
function makeDate(divid)
{
  var date = document.getElementById(divid).value;
  var datum = explode("-", date);
  document.getElementById(divid).value = datum[2]+" "+returnMonth(datum[1])+" "+datum[0];
}

/**
 *
 * @access public
 * @return void
 **/
function checkPayment()
{
  if(document.getElementById('first_name').value == "")
  {
    alert("First Name is Empty");
    document.getElementById('first_name').focus();
    return false;
  }

  if(document.getElementById('surname').value == "")
  {
    alert("Surname is Empty");
    document.getElementById('surname').focus();
    return false;
  }

  if(document.getElementById('address1').value == "")
  {
    alert("Address is Empty");
    document.getElementById('address1').focus();
    return false;
  }

  if(document.getElementById('zip').value == "")
  {
    alert("Postal code is Empty");
    document.getElementById('zip').focus();
    return false;
  }

  if(document.getElementById('city').value == "")
  {
    alert("City is Empty");
    document.getElementById('city').focus();
    return false;
  }

  if(document.getElementById('country').value == "")
  {
    alert("Country is Empty");
    document.getElementById('country').focus();
    return false;
  }

  if(document.getElementById('phone').value == "")
  {
    alert("Phone number is Empty");
    document.getElementById('phone').focus();
    return false;
  }

  if(document.getElementById('cellular').value == "")
  {
    alert("Cellular number is Empty");
    document.getElementById('cellular').focus();
    return false;
  }

  if(document.getElementById('mail').value == "")
  {
    alert("Mail is Empty");
    document.getElementById('mail').focus();
    return false;
  }
  if(!isValidEmail(document.getElementById('mail').value)) return false;

  if(document.getElementById('number_of_apartment').value == "")
  {
    alert("Number of apartment is Empty");
    document.getElementById('number_of_apartment').focus();
    return false;
  }
  if(!document.getElementById('tearms_0').checked)
  {
    alert("You need to agree with Terms & Conditions to proceed!");
    document.getElementById('tearms_0').focus();
    return false;
  }
}

function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1)
   {
      alert('A valid e-mail address is required.\nPlease amend and retry');
      return false;
    }
    return true;
}

function picker_change(apt_id,divid,dividfrom, monthid, yearid)
{
  var year = document.getElementById(yearid);
  var month = document.getElementById(monthid);
  calendarPicker(month.options[month.selectedIndex].value,year.options[year.selectedIndex].value,apt_id,divid,dividfrom);
}