function searchFormSubmit(){
  if (!document.getElementById("checkInDateM").value) {
    $("#searcherrorText").html("Please select a new date. Hotels can not be reserved after check-in date.");
    return false;
  }
  
    container.setSearchFormUrl({
      cityValue : document.getElementById("city").value,
      checkInDateValue : document.getElementById("checkInDateM").value,
      checkOutDateValue : document.getElementById("checkOutDateM").value,
      nightsValue : document.getElementById("nights").value,
      roomsValue : document.getElementById("rooms").value,
      currencyValue : document.getElementById("currency").value,
      adultQuantityFieldNameFormat : "adults%roomNr",
      childQuantityFieldNameFormat : "children%roomNr",
      childAgeFieldNameFormat : "childage%roomNr%childNr",
      searchOnMap : true
    });
    container.searchHotel(map.getBounds());
    gizleGoster('search-box');
    return false;
}

function changeRoomSize()
{
    var size = document.getElementById("rooms").value;
    for(var i = 1; i < 6; i++ )
    {
        if(i <= size)
            document.getElementById("room" + i).style.display = "block";
        else
            document.getElementById("room" + i).style.display = "none";
    }
    checkHeaders();

}

function changeChildNumber(box, a, b)
{
    var num = document.getElementById(box).value;
    if(num == 1)
    {
        document.getElementById(a).style.display = "block";
        document.getElementById(b).style.display = "none";
    }
    else if(num == 2)
    {
        document.getElementById(a).style.display = "block";
        document.getElementById(b).style.display = "block";
    }
    else if(num == 0)
    {
        document.getElementById(a).style.display = "none";
        document.getElementById(b).style.display = "none";
    }
    checkHeaders();
}

function checkHeaders()
{
    var letters = ['a', 'b', 'c', 'd', 'e'];
    var dump;
    var rs = 0;
    for(var i = 1; i < 6; i++)
    {
        dump = document.getElementById("room" + i).style.display;

        if(dump == "block")
        {

            for(k = 1; k < 3; k++)
            {
                if((checkChildStatus(1, i) == 1) && (checkChildStatus(2, i) == 1))
                {
                    document.getElementById("hChild1").style.display = "block";
                    document.getElementById("hChild2").style.display = "block";
                    return;
                }

                if(rs == 0)
                {
                    if(checkChildStatus(k, i) == 1)
                    {
                        document.getElementById("hChild" + k).style.display = "block";
                        rs = 1;

                    }
                    else if(checkChildStatus(k, i) == 0)
                    {
                        document.getElementById("hChild" + k).style.display = "none";
                    }
                }
            }
        }
    }
}

function checkChildStatus(type, room)
{
    var letters = ['a', 'b', 'c', 'd', 'e'];
    var dump = 0;
    var status = document.getElementById(letters[room - 1] + "-" + type).style.display;
    if(status == "block") dump = 1;

    return dump;
}


function getDate(d)
{
    var currDay = d.getDate();
    var currMonth = d.getMonth()+1;
    var currYear = d.getFullYear();

    if(currDay < 10) { currDay = "0" + currDay; }
    if(currMonth < 10) { currMonth = "0" + currMonth; }

    var dateStr = currDay + "/" + currMonth + "/" + currYear;

    return dateStr;
}

function setDate()
{
    var checkin = concatDate();
    var days = document.getElementById('nights').selectedIndex + 1;
    var display = document.getElementById('dateLabel');

    var b = checkin.split("/");

    if(!b[0]) b[0] = 1;
    if(!b[1]) b[1] = 1;
    if(!b[2]) b[2] = 2009;                                      

    var i, k;

    for(k = 0; k < 2; k++)
    {
        for(i = 0; i < 10; i++)
        {
            if(b[k] == "0" + i)
            {
                b[k] = i;
            }
        }
    }

    var newcheckin = new Date(eval(b[2]), eval(b[1]) - 1 , eval(b[0]) + eval(days));
    var newDate = getDate(newcheckin);

    display.innerHTML = newDate;
    document.getElementById("checkoutdate").value = newDate;
    document.getElementById("checkindate").value = checkin;

}

function concatDate()
{
    var day = document.getElementById('chinday').value;
    var month = document.getElementById('chinmonth').value;
    var year = document.getElementById('chinyear').value;

    var dateStr = day + "/" + month + "/" + year;

    return dateStr;

}

function validateField(day, month, year, splitDate, _date)
{
    if(splitDate)
    {
        var b = _date.split("/");

        validateField(b[0], b[1], b[2], false, null);
    }
    else
    {
        if(day.length < 2 || day.length > 2 || day == '' || isNaN(day))
        {
            return false;
        }

        if(month.length < 2 || month.length > 2 || month == '' || isNaN(month))
        {
            return false;
        }

        if(year.length < 4 || year.length > 4 || year == '' || isNaN(year))
        {
            return false;
        }

        return true;
    }

    return true;
}

function checkFormInputs()
{
    var cityField = document.getElementById("destination_city").value;
    var day = document.getElementById('chinday').value;
    var month = document.getElementById('chinmonth').value;
    var year = document.getElementById('chinyear').value;
    var cout = document.getElementById("checkoutdate").value;
    var cin = document.getElementById("checkindate").value;
    var days = document.getElementById('nights').selectedIndex;

    if(cityField == '')
    {
        alert("City can not be empty!");
        return false;
    }
    if(!validateField(day, month, year, false, null))
    {
        alert("Invalid date!");
        return false;
    }
    if(!validateField(null, null, null, true, cout))
    {
        alert("Invalid date!");
        return false;
    }
    if(!validateField(null, null, null, true, cin))
    {
        alert("Invalid date!");
        return false;
    }
    if(days.length < 1 || days.length > 2 || isNaN(days))
    {
        alert("Invalid day!");
        return false;
    }

    return true;

}
