var page = null;
function CP() {
    var t=this;
    $('#submit-link').click(function() {t.form_submit();});
    $('#reset-link').click(function() {
          ($("#booking-form"))[0].reset();
       });
}
CP.prototype.form_submit=function() { 
    this.clear_errors();   
    
    var name = jQuery.trim($('#customer-name').val());
    var email = jQuery.trim($('#email').val());
    
    if (this.isEmpty(name)) {this.empty_error('Name');return false;}
    if (this.isEmpty(email)) {this.empty_error('Email');return false;}
    
    var this_scope = this;
    $.post("/cgi-bin/signup.pl",
            $("#booking-form").serialize(),
            function(r) {
                var resp = r; // jQuery.parseJSON(r);
                if (resp.success) {
                    this_scope.message('Saved');
		    ($("#booking-form"))[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.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>");
}

