var page = null;
function CP() {
    var t=this;
    $('#send-link').click(function() {t.form_submit();});
    this.months = {
        jan  :  1, feb  :  2, mar  :  3,
        apr  :  4, may  :  5, jun  :  6,
        jul  :  7, aug  :  8, sep  :  9,
        oct  :  10, nov  :  11, dec  :  12
    };
}
CP.prototype.form_submit=function() { 
    this.clear_errors();   
    var address = jQuery.trim($('#address').val());
    var destination = jQuery.trim($('#destination-address').val());
    var name = jQuery.trim($('#customer-name').val());
    var email = jQuery.trim($('#email').val());
    var phone = jQuery.trim($('#phone').val());
    var num_people = jQuery.trim($('#num-of-people').val());
    var num_bags = jQuery.trim($('#num-of-bags').val());
    var month = jQuery.trim($('#month').val());
    var day = jQuery.trim($('#day').val());
    var year = jQuery.trim($('#year').val());
    var hr = jQuery.trim($('#hour').val());
    var minute = 0;
    var ampm = jQuery.trim($('#ampm').val());

    if (this.isEmpty(name)) {this.empty_error('Name');return false;}
    if (this.isEmpty(email)) {this.empty_error('Email');return false;}
    if (this.isEmpty(phone)) {this.empty_error('Phone');return false;}
    if (this.isEmpty(month)) {this.empty_error('Month');return false;}
    if (this.isEmpty(day)) {this.empty_error('Day');return false;}
    if (this.isEmpty(year)) {this.empty_error('Year');return false;}
    if (this.isEmpty(hr)) {this.empty_error('Hour');return false;}
    if (this.isEmpty(ampm)) {this.empty_error('ampm');return false;}
    if (this.isEmpty(address)) {this.empty_error('Address');return false;}
    if (this.isEmpty(destination)) {this.empty_error('Destination Address');return false;}
    if (this.isEmpty(num_people)) {this.empty_error('Number of People'); return false;}
    if (this.isEmpty(num_bags)) {this.empty_error('Number of Bags'); return false;}

    month = month.replace(/^[\d\s-]+/,'');    
    month = this.months[month.toLowerCase()];    
    if(!this.is_date(year,month,day)) {this.error("Pick up date is invalid");return false}
    if(!this.is_time(hr,minute,ampm)) {this.error("Pick up time is invalid");return false}
    if(!isNaN(Number(num_people)) && Number(num_people) < 1) { this.error("Number of People must be more than 0"); return false}
    if(!isNaN(Number(num_bags)) && Number(num_bags) < 1) { this.error("Number of Bags must be more than 0"); return false}
    var this_scope = this;
    $.post("/cgi-bin/bookings.pl",
            $("#booking-form2").serialize(),
            function(r) {
                var resp = jQuery.parseJSON(r);
                if (resp.success) {
                    this_scope.message('Booking Saved');
		    ($("#booking-form2"))[0].reset();
                } else {
		    this_scope.combine_errors(resp.errors);
		}
            });
}
CP.prototype.isEmpty=function(val) {
    return (val == null || val == undefined || String(val).length == 0);
}
CP.prototype.is_date=function(year,month,day) {
    try {
        var supplied=new Date(year, month - 1, day);
        return year == supplied.getFullYear() && month == (supplied.getMonth()+1) && day == supplied.getDate();
    } catch (e) {
        return false;
    }
    return true;

}
CP.prototype.is_time=function(hr,min,ampm) {
    hr = Number(hr);
    
    if(ampm == 'pm') {
        hr += 12;
    }
    if (hr == 12) { hr = 0; } // midnight
    if (hr == 24) { hr = 12; } // noon
    //console.log(hr+':'+min+':'+ampm);
    return (hr >= 0 && hr <= 23 && min >= 0 && min <= 59);

}
CP.prototype.empty_error=function(what) {    
    this.error(what + ' is  missing');
}
CP.prototype.error=function(msg) {
    alert(msg); return;
    var err = $('#error-div');
    if(err) {
        err.html(msg);
    }    
}
CP.prototype.clear_errors=function() {
    //this.error('');
}
CP.prototype.message=function(msg) {
    this.error(msg);
}
CP.prototype.combine_errors=function(errors) {
    var field,msg='';
    
    for (field in errors) {
	msg += "<li>"+errors[field]+"</li>";
    }
    this.error("<ul>"+msg+"</ul>");
}

