
function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) { 
            c_start=c_start + c_name.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) 
                c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        } 
    }
    return "";
}

function setCookie(c_name,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" + escape(value) + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getArrVal(arr,idx) {
    if (arr.length >= idx) {
        if (arr[idx] == null) return ""
        if (arr[idx].length <= 0) return ""
        return arr[idx];
    }
    return ""
}

function setVal(id,v) {
    if ($('#'+id).size() > 0)
        $('#'+id).val(v);
}

function loadCustInfo() {

    if (getCookie('ORDERTYPE') == "B")
        $("#cust_ordertype_b").attr("checked","checked");
    else
        $("#cust_ordertype_p").attr("checked","checked");

    var info = getCookie('CUSTINFO');
    if (info != null && info != '') {
        info = info.split('|');

        if (getArrVal(info,1).length > 0)
            setVal('cust_name', getArrVal(info,1));

        if (getArrVal(info,2).length > 0)
            setVal('cust_contact2', getArrVal(info,2).replace(/-/g,''));

        if (getArrVal(info,8).length > 0)
            setVal('cust_guests', getArrVal(info,8));

        if (getArrVal(info,9).length > 0)
            setVal('cust_date', getArrVal(info,9));

        if (getArrVal(info,10).length > 0)
            setVal('cust_time', getArrVal(info,10));
    }
}

function getToday()
{
    var today = new Date();

    var m = new String(today.getMonth());
    while (m.length < 2) m = '0'+m;

    var d = new String(today.getDate());
    while (d.length < 2) d = '0'+d;

    return today.getFullYear()+'-'+m+'-'+d;
}

function useHint(id)
{
    $('#'+id).focus(function() {
        if ($(this).val() == $(this).attr("title"))
            $(this).val('');
    });
    $('#'+id).blur(function() {
        if ($(this).val() == '')
            $(this).val($(this).attr("title"));
    });
}

function initReservation()
{
    useHint('cust_name');
    useHint('cust_guests');

    $('#cust_date').datepicker({
        showButtonPanel: true,
        minDate: new Date(),
        dateFormat: 'yy-mm-dd'
    });
    $('#cust_date_btn').click(function() {
        $('#cust_date').datepicker('show');
    });

    setVal('cust_date', getToday());

    $('#cust_time').removeOption(/./);
    $('#cust_time').addOption({
        '12:00 pm':'12:00 pm',
        '12:30 pm':'12:30 pm',
        '1:00 pm':'1:00 pm',
        '1:30 pm':'1:30 pm',
        '2:00 pm':'2:00 pm',
        '2:30 pm':'2:30 pm',
        '6:00 pm':'6:00 pm',
        '6:30 pm':'6:30 pm',
        '7:00 pm':'7:00 pm',
        '7:30 pm':'7:30 pm',
        '8:00 pm':'8:00 pm',
        '8:30 pm':'8:30 pm',
        '9:00 pm':'9:00 pm',
        '9:30 pm':'9:30 pm',
        '10:00 pm':'10:00 pm',
        '10:30 pm':'10:30 pm'
    },false);

    loadCustInfo();

    $('#cust_submit').click(function() {

        if ($('#cust_ordertype').val() == "B")
            setCookie('ORDERTYPE','B',1);
        else
            setCookie('ORDERTYPE','P',1);

        /* validate entries */
        if ($("#cust_name").val().match(/^Name$/i)) $("#cust_name").val('');
        if (!$("#cust_guests").val().match(/[0-9]+/)) $("#cust_guests").val('');

        var custInfo = $('#cust_title').val() + "|" +
            $('#cust_name').val() + "|" +
            $.trim($('#cust_contact1').val() + '-' + $('#cust_contact2').val()).replace(/-/g,'') + "|" +
            $('#cust_email').val() + "|" +
            $('#cust_address1').val() + "|" +
            $('#cust_address2').val() + "|" +
            $('#cust_postcode').val() + "|" +
            $('#cust_heard_from').val() + "|" +
            $('#cust_guests').val() + "|" +
            $('#cust_date').val() + "|" +
            $('#cust_time').val() + "|" +
            $('#cust_special_req').val() + "|";

        custInfo = custInfo.replace(/undefined/g,'');

        setCookie('CUSTINFO', custInfo, 1);

        window.location.href = "reservation.php";
    });
}

function initENews()
{
    useHint('enews_email');

    $('#enews_submit').click(function() {
    	if (!$("#enews_email").val().match("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,3})$"))
            $('#enews_result').html("Invalid email address!");
        else
        {
            $.blockUI({ 
                message: '<table><tr><td><img src="images/ico_processing.gif"/></td><td>Processing. Please wait...</td></tr></table>',
                css: { width: '200px', padding: '2px', border: '1px solid #666666', backgroundColor: 'white', color: '#000'}
            });
            postAjax('subscribe.php', {email:$('#enews_email').val()}, onSubscribeComplete, onSubscribeError);
        }
    });
}

function onSubscribeComplete(data, textResponse)
{
    $.unblockUI();
    if (data.match(/SUCCESS/i))
        $('#enews_result').html("Subscribed successfully");
    else
        $('#enews_result').html("Failed to subscribe");
}

function onSubscribeError()
{
    $.unblockUI();
    $('#enews_result').html("Failed to subscribe");
}

function initPg()
{
    initReservation();
    initENews();
}

$(document).ready(initPg);
