﻿function setDefaultButton(panelID, buttonID) {
	$j('#'+panelID+' :text').keypress(
        function(e) {
		    var btnID = buttonID;
            if (e.which == 13) {
                $j('#' + btnID).click();
            }
        });
}

function getFlashMovie(movieName) {
    var flashObj = window.document[movieName];
    if(null == flashObj){
        eval('flashObj = window.document.' + movieName);
    }
    return flashObj;
} 

function isNumberKey(evt)
{
     var charCode = (evt.which) ? evt.which : event.keyCode
     if (charCode == 46 || (charCode > 31 && (charCode < 48 || charCode > 57)) )
        return false;

     return true;
}

function isNumericKey(evt)
{
     var charCode = (evt.which) ? evt.which : event.keyCode
     if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

     return true;
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function isValidEmail(email){
    var  re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    return email.match(re);
}

function isValidMobile(mobile){
    var  re = /^08\d{8}$/;
    return mobile.match(re);
}

function log(msg){
    if(window.console){
        console.log(msg);
    }
}

function isAvailableEmail(email, accountID)
{
    if(accountID == undefined || accountID == null){
        accountID = '';
    }
    var isAvailable = false;
    $j.ajax({
        url: "handler/AccountHandler.ashx",
        type: "POST",
        async: false,
        dataType: 'xml',
        data: ({ commandname: 'checkavailableemail', 'email': email, 'accountid': accountID }),
        success: function(msg) {
            var result = $j(msg);
            log(msg);
            if(result.find('result').text() == 'true'){
                isAvailable = true;
            }
        },
        error: function(er) {
            log(er.message);
        }
        
    });

    return isAvailable;
}

function validateEmailv2(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 validMobileAndHomePhone(phone) {
    var valid = phone.match("(^(08)[0-9]{8}$)|(^02[0-9]{7}$)");
    if (valid == null) {
        return false;
    }
    return true;
}

function validMobile(mobile) {
    var valid = mobile.match("^(01|03|04|05|06|07|08|09)[0-9]{8}$");
    if (valid == null) {
        return false;
    }
    return true;
}

function validHomePhone(homephone) {
    var valid = homephone.match("^02[0-9]{7}$");
    if (valid == null) {
        return false;
    }
    return true;
}

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;
}

function urlencode(str) {
    str = escape(str);
    str = str.replace('+', '%2B');
    str = str.replace('%20', '+');
    str = str.replace('*', '%2A');
    str = str.replace('/', '%2F');
    str = str.replace('@', '%40');
    return str;
}

function urldecode(str) {
    str = str.replace('+', ' ');
    str = unescape(str);
    return str;
}

function isNumericDot(strText) {
    var blnStatus = true;
    var blnDotExist = false;
    strText = strText.replace(",", "");
    for (i = 0; i < strText.length; i++) {
        intText = parseInt(strText.charAt(i))
        if (isNaN(intText)) {
            if (strText.charAt(i) != ".") {
                blnStatus = false;
                break;
            }
            else {
                if (blnDotExist) {
                    blnStatus = false;
                    break;
                }
                else {
                    blnDotExist = true;
                }
            }
        }
    }
    return blnStatus;
}

function isNumeric(strText) {
    var ValidChars = "0123456789";
    var IsNumber = true;
    var Char;

    strText = strText.replace(",", "");

    for (i = 0; i < strText.length && IsNumber == true; i++) {
        Char = strText.charAt(i);
        if (ValidChars.indexOf(Char) == -1) {
            IsNumber = false;
            //alert("กรุณากรอกเป็นตัวเลข");
        }
    }
    return IsNumber;
}
