$(function () {
    $.superbox();
    var curD = new Date();
    var curDay = curD.getDate();


    $("#hotelCheckin").datepicker({
        minDate: new Date(curD.getFullYear(), addZero((parseInt(curD.getMonth()))), addZero(curD.getDate())),
        dateFormat: 'yy-mm-dd', 
        onSelect: function (dateText, inst) {
            var m = new Date($('#hotelCheckin').datepicker('getDate'));
            var s = new Date($('#hotelCheckin').datepicker('getDate'));
            m.setDate(m.getDate() + 1);
            s.setDate(s.getDate() + 1);
            $('#hotelCheckout').datepicker('option', 'minDate', m);
            $('#hotelCheckout').datepicker('setDate', s)
        }
    });
    curD.setDate(curDay + 1);
    $("#hotelCheckout").datepicker({
        minDate: new Date(curD.getFullYear(), addZero((parseInt(curD.getMonth()))), addZero(curD.getDate())),
        dateFormat: 'yy-mm-dd'
    });
    /*$('#compareHotel').click(function () {
    });

    $('#compareMulHotel').click(function () {
        $("#monteCarlosearchFormMp").submit();
    });
    */

    $("#AddReview").submit(function () {
        var uid = GetUid();
        $("<input type='hidden' name='monteCarlouid' id='monteCarlouid'/>").attr("value", uid).appendTo(this);
        var params = $(this).serialize();
        $.post("/AddReview.php", $(this).serialize(), renderResult, "html");
        return false;
    });

    $("#monteCarlosearchForm").submit(function () {
	    if ($('#hotelCheckin').val().length == 0 || $('#hotelCheckout').val().length == 0) {
            alert('Choose a date');
            return false;
        }
        var uid = GetUid();
        $("<input type='hidden' name='monteCarlouid' id='monteCarlonuid'/>").attr("value", uid).appendTo(this);
        var params = $(this).serialize();
        $.post("/SearchSp.php", $(this).serialize(), renderPrice, "json");
        return false;
    });
    $('.rating').each(function (i) {
        var star = $(this).attr('title');
        $(this).text(rating_stars(star));
    });
});


function rating_stars(rating) {
    var stars = [];
    for (i = 0; i < 5; i++) {
        if (i < rating) {
            stars += String.fromCharCode(parseInt("2605", 16));
        } else {
            stars += String.fromCharCode(parseInt("2606", 16));
        }
    }
    return stars;
}

function addZero (num){
	if (num.toString().length == 1){
		return '0'+parseInt(num);
	}
	return num;
}

function GetUid() {
    temp_uid = guid();
    return temp_uid;
}

function renderResult(data, textStatus) {
    if (textStatus == "success") {
        alert('Thank you.');
    }
    else {
        alert('error');
    }
    return false;
}

function renderPrice(data, textStatus) {
    if (textStatus == "success") {
        var tmpHref = data.href;
        if (data.label != undefined) {
            tmpHref += '&label='+data.label;
        }
        window.location = tmpHref;
    } else {
        alert("error");
    }
    return false;
}


function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
}
function guid() {
    return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}



