﻿

function shareFacebook(url)
{
    var wnd = window.open("http://www.facebook.com/sharer.php?u="+url, 'sharetofb');
    wnd.focus();
}

function shareTwitter(msg)
{
    var wnd = window.open("http://twitter.com/home?status="+msg, 'sharetotw');
    wnd.focus();
}
  

function getFBPermission(apikey, baseUrl, permissions, nextPage, width, height, popupType)
{
    //popupType must be one of this page, popup, wap, touch
    if(undefined == popupType || null == popupType){
        popupType = 'popup';
    }
    if(undefined == width || null == width){
        width = 761;
    }
    if(undefined == height || null == height){
        height = 460;
    }
    var redirecURL = 'http://www.facebook.com/login.php?api_key=' + apikey + '&'+
                        'cancel_url=&'+
                        'display=' + popupType + '&'+
                        'fbconnect=1&'+
                        'locale=en_US&'+
                        'method=auth.login&'+
                        'next=' + baseUrl + nextPage + '&'+
                        'req_perms=' + permissions + '&'+
                        'return_session=1&'+
                        'sdk=joey&'+
                        'session_version=3&'+
                        'v=1.0';

    var win = window.open(redirecURL, 'permissionwindow', 'menubar=no,toolbar=no,location=no,scrollbars=no,status=no,resizable=no,width=' + width + ',height=' + height);
    win.focus();
}

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 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 > 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;
}

function queryString(ji)    //Request QueryString
{
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) 
    {
        ft = gy[i].split("=");
        if (ft[0] == ji) 
        {
            return ft[1];
        }
    }
}
