function slideContent(){
    $('#content').slideDown(1000);
}

function clubDialog(){
    var $dialog = $("<div id='club-card-dialog'></div>");
    var dialogContent = "<p style='text-align:left;padding:5px;border-bottom:1px solid #333;margin:0 0 10px 0;'>Fill in the form below to register as a Mosskito Platinum Club Card Member, which will entitle you to 25% off at Mosskito's when you present your card</p>";
    dialogContent += "<table cellspacing=5 cellpadding=5>";
    dialogContent += "<tr><td><label>Name</label></td><td><select id='salutation' type='text' name='salutation'><option>Mr</option><option>Mrs</option><option>Ms</option><option>Miss</option></select><input id='name' type='text' name='name'/></td>";
    dialogContent += "<td><label>Email</label></td><td><input id='email' type='text' name='email'/></td></tr>";
    dialogContent += "<tr><td><label>Address</label></td><td><input id='address' type='text' name='address'/></td>";
    dialogContent += "<td><label>Postal Code</label></td><td><input id='postal_code' type='text' name='postal_code'/></td></tr>";
    dialogContent += "<tr><td><label>Suburb</label></td><td><input id='suburb' type='text' name='suburb'/></td>";
    dialogContent += "<td><label>State</label></td><td><select name='state' id='state'><option>VIC</option><option>NSW</option><option>NA</option><option>QLD</option><option>WA</option><option>TAS</option><option>SA</option><option>ACT</option></select></td></tr>";
    dialogContent += "<tr><td><label>Mobile</label></td><td><input id='mobile' type='text' name='mobile'/></td>";
    dialogContent += "<td><label>Telephone</label></td><td><input id='telephone' type='text' name='telephone'/></td></tr>";
    dialogContent += "<tr><td><label>Date of Birth</label></td><td>";
    //day
    dialogContent += "<select id='dob-day' type='text' name=''>";
    for(var i = 1;i<32;i++){
        dialogContent += "<option>"+i+"</option>";
    }
    dialogContent += "</select>";
    //month
    dialogContent += "<select id='dob-month' type='text' name=''>";
    dialogContent += "<option>January</option>";
    dialogContent += "<option>February</option>";
    dialogContent += "<option>March</option>";
    dialogContent += "<option>April</option>";
    dialogContent += "<option>May</option>";
    dialogContent += "<option>June</option>";
    dialogContent += "<option>July</option>";
    dialogContent += "<option>August</option>";
    dialogContent += "<option>September</option>";
    dialogContent += "<option>October</option>";
    dialogContent += "<option>November</option>";
    dialogContent += "<option>December</option>";
    dialogContent += "</select>";
    //year
    dialogContent += "<select id='dob-year' type='text' name=''>";
    for(var q = 1930;q<2011;q++){
        dialogContent += "<option>"+q+"</option>";
    }
    dialogContent += "</select>";
    dialogContent += "</td></tr>";
    dialogContent += "<input type='hidden' name='list_id' value='4'/>";
    dialogContent += "<input type='hidden' name='client' value='mosskito'/>";
    dialogContent += "</table>";

    dialogContent += 'Terms' +
			'\n1. Our Platinum Club Card entitles you 25% off your total bill.' +
			'\n2. Valid Monday through to Wednesday Only.' +
			'\n3. Card not valid for the month of December.' +
			'\n4. Card and cardholder needs to be present at time of use.' +
			'\n5. Valid for one bill only.6. Minimum spend of $40.00 to use card.' +
			'\n7. Mosskito at their discretion can change the terms and conditions of this offer without notice.';

    dialogContent += "<div id='club-card-submit'><span class='club-card-status'><img src='images/ajax-loader.gif'/> <span></span></span><br /><button id='club-card-submit-button'>Submit</button> <button id='club-card-cancel-button'>No Thanks</button></div>";

    $dialog.append(dialogContent);

    $('#club-card-submit button').button();

    $dialog.dialog({
        modal: true,
        title: 'Become a Mosskito Platinum Club Card Member!',
        resizable: false,
        width: '600px'
    });
}

function clubRequest(){
    var status = 1;
    var dataObj = {};
    $('#club-card-dialog input:not(input[type=button])').each(function(){
        if($(this).attr('name') != "name"){
            var value = '';
            if($(this).attr('name') == "suburb"){
                value = $(this).val().toUpperCase();
            } else {
                value = $(this).val();
            }
            dataObj[$(this).attr('name')] = value;
        }
        if($(this).val() != 'email' && $(this).val() != 'telephone'){
           if($(this).val() == ''){
               status = 0;
           }
        }
    })

    var name = $('#salutation').val()+" "+$('#name').val();
    var day = '';
    if($('#dob-day').val().length == 1){
        day = '0'+$('#dob-day').val();
    } else {
        day = $('#dob-day').val();
    }
    var dob = day+"-"+$('#dob-month').val()+"-"+$('#dob-year').val();

    dataObj['name'] = name;
    dataObj['date_of_birth'] = dob;
    dataObj['state'] = $('#state').val().toUpperCase();

    if(status == 0){
        alert("Please fill in all of the required form fields");
    } else {
        $('#club-card-submit button').attr('disabled', 'disabled');
        $('.club-card-status img').attr('src','images/ajax-loader.gif');
        $('.club-card-status span').html('Submitting your membership request, please wait');
        $('.club-card-status').show();
        $.ajax({
            url: 'http://ipressprinting.com.au/ipress_database_application/scripts/remoteAddUserJsonP.php',
            data: dataObj,
            jsonp: 'jsonp_callback',
            dataType: 'jsonp',
            success: function(data){
                if(data){
                    $('.club-card-status img').attr('src','images/ajax-success.png');
                    if(dataObj['email'] != ''){
                        $('.club-card-status span').html('Thank you '+dataObj['name']+', you have been registered as a Mosskito Club Card Member. Your membership card will be sent to the address you provided in the coming days. You will receive an email shortly to confirm this. This window will close in a few seconds.');
                    } else {
                        $('.club-card-status span').html('Thank you '+dataObj['name']+', you have been registered as a Mosskito Club Card Member. Your membership card will be sent to the address you provided in the coming days. This window will close in a few seconds.');
                    }
                    window.setTimeout('clubClose()',6000);
                } else {
                    $('.club-card-status img').attr('src','images/ajax-fail.png');
                    $('#club-card-submit buttons').removeAttr('disabled');
                }
            }
        })
    }
}

function clubClose(){
    $('#club-card-dialog').dialog('close');
}

$(document).ready(function(){
    if($(location).attr('pathname').indexOf('index.html') != -1 || $(location).attr('pathname') == '') {
        if(undefined == $.cookie('mosskito-club-card')){
           clubDialog();
           $.cookie('mosskito-club-card','true');
        }
        
    }
    window.setTimeout(slideContent,600);

    $('#club-card-link').click(function(event){
        event.preventDefault();
        clubDialog();
    })

    var navHeight = $('#header-nav').height();

    $('#club-card-submit-button').live('click',function(){
        clubRequest();
    })
    $('#club-card-cancel-button').live('click',function(){
        clubClose();
    })
})


