function messagebox(message, okfun, title, width, height) {
    title = title || 'System Message';
    width = width || 420;
    if (height == undefined) height = 0;
    jslib.messagebox.show(width, 0, title, message, function() {
        jslib.messagebox.hidden();
        if (typeof (okfun) == 'function') okfun();
    });
}

function hidemessagebox() {
    jslib.messagebox.hidden();
}

function confirmmessagebox(width, height, title, content, funok) {
    title = title || 'System Message';
    width = width || 400;
    if (height == undefined) height = 0;
    jslib.messagebox.show(width, height, title, content, function() {
        if (funok != null) {
            funok();
        }
    }, null, function() {
        hidemessagebox();
    });
}

function loading() {
    jslib.messagebox.show(150, 30, null, '<img src="' + skin_root_path + 'messagebox/loading.gif" align="absmiddle" /> Loading...');
}

function processing() {
    jslib.messagebox.show(150, 30, null, '<img src="' + skin_root_path + 'messagebox/loading.gif" align="absmiddle" /> Processing...');
}

function ajax_submit_form(form, completed, error) {
    if (!jslib.form.validator.isvalid(form)) return false;
    processing();
    return jslib.ajax.form.submit(form, completed, error);
}

function addbundle_buynow(code, quantity) {
    quantity = quantity || 1;
    loading();
    $.get(site_root_path + 'common/ajaxservice.ashx?AjaxHandler=buy&quantity=' + quantity + '&onlinelessonbundlecode=' + code, function(data) {
        if (data == 'success') {
            window.document.location.href = "/members/shoppingcart.ashx";
        }
        jslib.messagebox.hidden();
    });
}

function addextraorder_buynow(itemname, coursetype, coursetrack, number, price, remark, quantity, onSuccess) {
    quantity = quantity || 1;
    loading();
    $.get(site_root_path + 'common/ajaxservice.ashx?AjaxHandler=buy&quantity=' + quantity + '&itemname=' + itemname + '&coursetype=' + coursetype + '&coursetrack=' + coursetrack + '&number=' + number + '&price=' + price + '&remark=' + remark, function(data) {
        if (data == 'success') {
            if (onSuccess != null) {
                onSuccess();
            } else {
                window.document.location.href = "/members/shoppingcart.ashx";
            }
        }
        jslib.messagebox.hidden();
    });
}

function inquiry_contactus(form) {
    return ajax_submit_form(form, function(request) {
        switch (request.responseText) {
            case 'name-empty':
                messagebox('Please input your name.', function() {
                    hidemessagebox();
                })
                break;
            case 'email-empty':
            case 'email-invalid':
                messagebox('Please input a valid email address.', function() {
                    hidemessagebox();
                })
                break;
            case 'turnumber-invalid':
                messagebox('Turing number is incorrect.', function() {
                    hidemessagebox();
                    self.location.href = self.location.href;
                })
                break;
            case 'success':
                messagebox('Thank you for your inquiry!<br /><br />Your requst will be attended to within 1 business day.', function() {
                    hidemessagebox();
                    self.location.href = self.location.href;
                })
                break;
            default:
                hidemessagebox();
                break;
        }
    });
}
