
today = new Date();

function OpenCheckRates(form) {
  if (Validate_dates(form)) {
    url = "http://www.lastminute-accommodation.cz/checkrates_en.html";
    url += "?day_in="+form.day_in.value;
    url += "&month_in="+form.month_in.options[form.month_in.selectedIndex].value;
    url += "&year_in="+form.year_in.options[form.year_in.selectedIndex].value;
    url += "&day_out="+form.day_out.value;
    url += "&month_out="+form.month_out.options[form.month_out.selectedIndex].value;
    url += "&year_out="+form.year_out.options[form.year_out.selectedIndex].value;
    url += "&product_id="+form.product_id.value;
    window.open(url,'check','scrollbars=1,width=600,height=350,top=20,left=20')
  }
}

function GetCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function SetNow(form) {
if (!form || !form.day_in) 
  return;
// dnesek nebo cookie checkin
var checkin = GetCookie("checkin");
var chin_m, chin_d, chin_y;
if (checkin!="") {
  var split = checkin.split("/");
  chin_m = parseInt(split[0])-1;
  chin_d = parseInt(split[1]);
  chin_y = parseInt(split[2]);
}

if ( (checkin=="") || ( (new Date(chin_y, chin_m, chin_d, 23, 59,59)).getTime()<today.getTime() )  ) {
  chin_m = today.getMonth();
  chin_d = today.getDate();
  chin_y = today.getFullYear();
}

var checkout = GetCookie("checkout");
var chout_m, chout_d, chout_y;
if (checkout!="") {
  var split = checkout.split("/");
  chout_m = parseInt(split[0])-1;
  chout_d = parseInt(split[1]);
  chout_y = parseInt(split[2]);
}

if ( (checkout=="") || ( (new Date(chout_y, chout_m, chout_d-1, 23, 59,59)).getTime()<today.getTime() )  ) {
  chout = new Date(today.getFullYear(), today.getMonth(), today.getDate()+3, 0, 0, 0);
  chout_m = chout.getMonth();
  chout_d = chout.getDate();
  chout_y = chout.getFullYear();
}

form.day_in.value = chin_d;
form.month_in.selectedIndex = chin_m;
form.year_in.selectedIndex = chin_y-parseInt(form.year_in.options[0].value);
	
form.day_out.value = chout_d;
form.month_out.selectedIndex = chout_m;
form.year_out.selectedIndex = chout_y-parseInt(form.year_out.options[0].value);
}

  function OpenLargeView(type, typeID, photoID) {
var wsize = WindowSize();
window.open ('http://www.lastminute-accommodation.cz/large_view_en.html?'+type+"="+typeID+"&photoID="+photoID+"&res="+wsize, '_blank','resizable=0,top=0,left=0,menubar=0,scrollbars=1,width='+(wsize+80)+',height='+(wsize+120));
  }


 function WindowSize() {
   if (!document.body.offsetWidth) //pro NS natvrdo 300
     return 300;
  else {
       if (document.body.offsetWidth) {
         if (document.body.offsetWidth<=620)
           return 300;
         if (document.body.offsetWidth<=780)
           return photo_size=450;
         return photo_size=600;
       }
       else
         return 300;
  }
}


function Validate_dates(form) {
	var d = parseInt(form.day_in.value);
	if (isNaN(d) || (d<1) || (d>31)) {
	 alert("Number format error of checkin-in day: "+form.day_in.value);
	 form.day_in.focus();
	 return false;
	}
	else
	 form.day_in.value = d;	
	 
	var m = parseInt(form.month_in.options[form.month_in.selectedIndex].value);
	var y = parseInt(form.year_in.options[form.year_in.selectedIndex].value);
	
	if (!Valid_date(d,m,y)) {
	 alert("Checkin "+d+"/"+m+"/"+y+" is wrong date!");
	 form.day_in.focus();
	 return false;
	}
	
	if (isOutdated(d,m,y)) {
	 alert("Checkin "+d+"/"+m+"/"+y+" is out of date!");
	 form.day_in.focus();
	 return false;
	}

	 var d1 = new Date(y, m-1, d, 0, 0, 0);

	 d = parseInt(form.day_out.value);
	 if (isNaN(d) || (d<1) || (d>31)) {
		 alert("Number format error of checkout day: "+form.day_out.value);
		 form.day_out.focus();
		 return false;
	 }
	 else
		 form.day_out.value = d;	
		 
	 var m = parseInt(form.month_out.options[form.month_out.selectedIndex].value);
	 var y = parseInt(form.year_out.options[form.year_out.selectedIndex].value);

	 if (!Valid_date(d,m,y)) {
		 alert("Checkout "+d+"/"+m+"/"+y+" is wrong date!");
		 form.day_out.focus();
		 return false;
	 }

	 var d2 = new Date(y, m-1, d, 0, 0, 0);
	 if (d2.getTime()<=d1.getTime()) {
		 alert("Checkout is not greater then checkin!");
		 return false;
	 }

         // kontrola checkout-today>21
         if (d2.getTime() - today.getTime() > 21*86400000) {
            var url = "http://www.lastminute-accommodation.cz/redirect_en.html";
            url += "?day_in="+form.day_in.value;
            url += "&month_in="+form.month_in.options[form.month_in.selectedIndex].value;
            url += "&year_in="+form.year_in.options[form.year_in.selectedIndex].value;
            url += "&day_out="+form.day_out.value;
            url += "&month_out="+form.month_out.options[form.month_out.selectedIndex].value;
            url += "&year_out="+form.year_out.options[form.year_out.selectedIndex].value;
            if (form.product_id)            
              url += "&product_id="+form.product_id.value;
            window.open(url, '_self');
            return false; 
         }


  return true;	
}


// funkce overuje platnost data
function Valid_date(d,m,y) {
  month_length = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

  if ( ((y % 4)==0) && ((y % 100)!=0) || ((y % 400)==0) )
    month_length[1] = 29; //prestupny rok

  if (isNaN(y))
    return false;

  if ( (m<1) || (m>12) || isNaN(m) )
    return false;

  if ( (d>month_length[m-1]) || (d<1) || isNaN(d) )
    return false;

  return true;
}

function isOutdated(d, m, y) {
  dt = new Date(y, m-1, d, 23, 59, 59);
  if (dt.getTime()<today.getTime())
    return true;
  else
    return false;
}



  function Validate(form) {
     // kontrola quantity
     if (isNaN(form['qnt[]'].length)) {
       if (isNaN(parseInt (form['qnt[]'].value)) || parseInt(form['qnt[]'].value)<=0) {
         alert("Number format error of quantity: "+form['qnt[]'].value);
         form['qnt[]'].focus();
         return false;
       }
     }
     else {
       var total = 0;
       for (i=0; i < form['qnt[]'].length; i++)
         if (isNaN(parseInt (form['qnt[]'][i].value)) || parseInt(form['qnt[]'][i].value)<0) {
           alert("Number format error of quantity: "+form['qnt[]'][i].value);
           form['qnt[]'][i].focus();
           return false;
         }
         else {
           form['qnt[]'][i].value = parseInt(form['qnt[]'][i].value);
           total += parseInt(form['qnt[]'][i].value);
         }
       if (total == 0) {
         alert("You must choose some service!");
         return false;
       }
     }

     var d = parseInt(form.day_in.value);
     if (isNaN(d) || (d<1) || (d>31)) {
       alert("Number format error of checkin-in day: "+form.day_in.value);
       form.day_in.focus();
       return false;
     }
     else
       form.day_in.value = d;	
       
     var m = parseInt(form.month_in.options[form.month_in.selectedIndex].value);
     var y = parseInt(form.year_in.options[form.year_in.selectedIndex].value);

     if (!Valid_date(d,m,y)) {
       alert("Checkin "+d+"/"+m+"/"+y+" is wrong date!");
       form.day_in.focus();
       return false;
     }

     if (isOutdated(d,m,y)) {
       alert("Checkin "+d+"/"+m+"/"+y+" is out of date!");
       form.day_in.focus();
       return false;
     }

     var d1 = new Date(y, m-1, d, 0, 0, 0);

     d = parseInt(form.day_out.value);
     if (isNaN(d) || (d<1) || (d>31)) {
       alert("Number format error of checkout day: "+form.day_out.value);
       form.day_out.focus();
       return false;
     }
     else
       form.day_out.value = d;	
       
     var m = parseInt(form.month_out.options[form.month_out.selectedIndex].value);
     var y = parseInt(form.year_out.options[form.year_out.selectedIndex].value);

     if (!Valid_date(d,m,y)) {
       alert("Checkout "+d+"/"+m+"/"+y+" is wrong date!");
       form.day_out.focus();
       return false;
     }

     var d2 = new Date(y, m-1, d, 0, 0, 0);
     if (d2.getTime()<=d1.getTime()) {
       alert("Checkout is not greater then checkin!");
       return false;
     }

      // kontrola checkout-today>21
      if (d2.getTime() - today.getTime() > 21*86400000) {
         var url = "http://www.lastminute-accommodation.cz/redirect_en.html";
         url += "?day_in="+form.day_in.value;
         url += "&month_in="+form.month_in.options[form.month_in.selectedIndex].value;
         url += "&year_in="+form.year_in.options[form.year_in.selectedIndex].value;
         url += "&day_out="+form.day_out.value;
         url += "&month_out="+form.month_out.options[form.month_out.selectedIndex].value;
         url += "&year_out="+form.year_out.options[form.year_out.selectedIndex].value;
         window.open(url, '_self');
         return false; 
      }


     if (isNaN(parseInt(form.adults.value)) || parseInt(form.adults.value)<1) {
       alert("Number format error of adults: "+form.adults.value);
       form.adults.focus();
       return false;
     }  
     else
       form.adults.value = parseInt(form.adults.value);
 
     if (isNaN(parseInt(form.children.value)) || parseInt(form.children.value)<0) {
       alert("Number format error of children: "+form.children.value);
       form.children.focus();
       return false;
     }  
     else
       form.children.value = parseInt(form.children.value);

     if (form.first_name.value.length == 0) {
       alert("First name is required to fill-in");
       form.first_name.focus();
       return false;
     }

     if (form.surname.value.length == 0) {
       alert("Surname is required to fill-in");
       form.surname.focus();
       return false;
     }

     if (form.city.value.length == 0) {
       alert("City is required to fill-in");
       form.city.focus();
       return false;
     }
 
     if ( (form.phone.value.length == 0) && (form.fax.value.length == 0) && (form.mail.value.length == 0) ) {
       alert("Phone or fax or e-mail is required to fill-in");
       form.phone.focus();
       return false;
     }

     if ( form.mail2.value != form.mail.value)  {
       alert("Different values of entered e-mails");
       form.mail.focus();
       return false;
     }

   if ( form.mail2.value.length == 0)  {
       alert("Please re-enter email e-mails");
       form.mail2.focus();
       return false;
     }
    
    return true;
  }
