function loading(){
	jQuery.jMessageBox.show({
		width : 300,
		message: '<div style="text-align:center;"><img src="' + skin_root_path + 'messagebox/loading.gif" align="absmiddle" /> Processing...</div>'		
	});
}

function messagebox(message, okfun, title, width, height){
	title = title || 'System Message';
	width = width || 400;
	if(height == undefined)height = 0;
	jQuery.jMessageBox.show({
		width : width,
		height: height,
		title: title,
		message: message,
		yesButton : {
			click : function(){
				if(typeof(okfun) == 'function')okfun();
				jQuery.jMessageBox.hide();
			}
		}
	});
}

function search() {
    if ($('#keyword').val() != '') location.href = ('/search/' + ($('#keyword').val().trim().replaceAll(' ', '/') + '/').single('/', 2));
}

function validate(value, pattern){
	var r = new RegExp(pattern,"im");
	return r.test(value);
}

function validateOrEmpty(value, pattern){
	if(value=="") return true;
	return validate(value, pattern);
}

function on_submit_contactus() {
    if ($("#name").val() == '') {
        messagebox('Please input your name.');
        $("#name").focus();
        return false;
    }
    if ($("#email").val() == '') {
        messagebox('Please input your email.');
        $("#email").focus();
        return false;
    }
    if ($("#email").val().indexOf('@') < 3) {
        messagebox('Invalid email addresses.');
        $("#email").focus();
        return false;
    }
    if ($("#title").val() == '') {
        messagebox('Please input title.');
        $("#title").focus();
        return false;
    }
    if ($("#message").val() == '') {
        messagebox('Please input message.');
        $("#message").focus();
        return false;
    }
    if ($("#vcode").length > 0 && $("#vcode").val() == '') {
        messagebox('Please input check code.');
        $("#vcode").focus();
        return false;
    }
    loading();
    var postdata = { ajaxhandler: 'contactus', name: $("#name").val(), email: $("#email").val(), title: $("#title").val(), message: $("#message").val(), vcode: $("#vcode").val() };
    $.post(site_root_path + 'members/memberservice.ashx', postdata, function(data) {
        switch (data) {
            case 'name-empty':
                messagebox('Please input your name.');
                break;
            case 'email-empty':
                messagebox('Please input your email.');
                break;
            case 'email-invalid':
                messagebox('Invalid email addresses.');
                break;
            case 'title-empty':
                messagebox('Please input title.');
                break;
            case 'message-empty':
                messagebox('Please input message.');
                break;
            case 'vcode-invalid':
                messagebox('Invalid check code!.');
                break;
            case 'success':
                messagebox('Your request has been successfully submitted. Please allow 24 hours for our staff to respond to it. Thanks!', function() {
                    self.location.href = site_root_path; ;
                });
                break;
            default:
                messagebox('Send message fail.');
                break;
        }
    }, '');
    return false;
}

function buy_post(postdata, checkout, completed){
	$.post(site_root_path + 'common/ajaxservice.ashx',postdata, function(data){
		switch(data){
			case 'success':
				jQuery.jMessageBox.hide();
				if(checkout){
					self.location.href = site_root_path + 'members/shoppingcart.ashx';
				}else{
					if(typeof(completed)=="function"){
						completed();
					}
				}
				break;
			case 'fail':
				messagebox("Buy fail");
				break;
		}
	});
}
function buy_product_quick(productId) {
    if (typeof (buy_product_completed) == 'function') {
        buy_product(productId, 1, 0, false, buy_product_completed);
    } else {
        buy_product(productId, 1, 0, true, null);
    }
}
function buy_product(productId, quantity, boxset, checkout, completed) {
    if (checkout) loading();
    var postdata = "ajaxhandler=buy&productid=" + productId + "&quantity=" + quantity + "&boxset=" + boxset;
    buy_post(postdata, checkout, completed);
}
function buy_product_form(checkout, completed){
	var flag=true;
	$("#buyform select[required=True]").each(function(i){
		if($(this).val()==""){
			var title=$(this).attr("rel");;
			if(title.indexOf("size")>-1||title.indexOf("Size")>-1){
				messagebox("Please choose size");
			}else if(title.indexOf("color")>-1||title.indexOf("Color")>-1){
				messagebox("Please choose color");
			}else if(title.indexOf("length")>-1||title.indexOf("Length")>-1){
				messagebox("Please choose length");
			}else{
				messagebox(title);
			}
			$("#buyform span[name=peatitle]").each(function(j){
				if($(this).html()==title){
					$(this).attr("style","color:red");
				}
			});
			flag = false;
		}
	 });
	 if(!flag) return false;	 
	var postdata = $('#buyform').serialize();
    buy_post(postdata, checkout, completed);
}
