﻿

function Messagebox(message, option) {
    option = $j.extend({
		title: 'Information',
        showOK: true,
        showCancel: false,
        okHandler: null,
        cancelHandler: null
    }, option);

	var okText = option.okText;
	var cancelText = option.cancelText;
	
    var msgbox = 
    $j('<div class="dialog" style="display:none;">'+
        '<h2>'+ message + '</h2>' +
    '</div>');
	
	option = $j.extend({
		messagebox: msgbox
	}, option);
	
	var buttons = {};
    if(option.showOK == true)
    {
		buttons = $j.extend({
			'OK' : function(){
				if(option.okHandler){
					option.okHandler(option);
				}else{
					msgbox.removeDialog();
				}
			}
		}, buttons);
    }
    if (option.showCancel == true) {
        buttons = $j.extend({
			'Cancel' : function(){
				if(option.cancelHandler){
					option.cancelHandler(option);
				}else{
					msgbox.removeDialog();
				}
			}
		}, buttons);
    }
    $j('body').append(msgbox);
    msgbox.dialog({
        draggable: true,
        autoOpen: false,
        resizable: false,
        modal: true,
        width: 450,
        height: 150,
		'buttons': buttons,
		title: option.title
    }).parent().appendTo($j("form:first"));
    msgbox.openDialog();
}

function SetDefaultButton(panelID, buttonID) {
	$j('#'+panelID+' :text').keypress(
        function(e) {
			var btnID = buttonID;
            if (e.which == 13) {
                $j('#' + btnID).click();
            }
        });
    }

    function validateEmail(email) {
        // a very simple email validation checking. 
        // you can add more complex email checking if it helps 
        if (email.length <= 0) {
            return true;
        }
        var splitted = email.match("^(.+)@(.+)$");
        if (splitted == null) return false;
        if (splitted[1] != null) {
            var regexp_user = /^\"?[\w-_\.]*\"?$/;
            if (splitted[1].match(regexp_user) == null) return false;
        }
        if (splitted[2] != null) {
            var regexp_domain = /^[\w-\.]*\.[A-Za-z]{2,4}$/;
            if (splitted[2].match(regexp_domain) == null) {
                var regexp_ip = /^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
                if (splitted[2].match(regexp_ip) == null) return false;
            } // if
            return true;
        }

        return false;
    }

    function trimAll(sString) {
        while (sString.substring(0, 1) == ' ') {
            sString = sString.substring(1, sString.length);
        }
        while (sString.substring(sString.length - 1, sString.length) == ' ') {
            sString = sString.substring(0, sString.length - 1);
        }
        return sString;
    }
