﻿// สระ         = pool, vowel, wash
// พยัญชนะ  = consonant
// วรรณยุกต์ = tone marks
function isKeyThaiCharacter(pKeyCode) {
    // ก - ฮ (3585-3630) สระ 3631-3675
    // %~+!@#$%^&*()_+฿=-/\|]}[{๐":;'?.>,<
    if (pKeyCode == null) return false;
    var thai_char = /^[ก-๛0-9 ]+$/;
    var eng_char = /^[A-Za-z]+$/;
    if (thai_char.test(getChar(pKeyCode))) {
        return true;
    }
    else if (eng_char.test(getChar(pKeyCode))) {
        window.event.keyCode = 0;
        return false;
    }
    else {
        return true;
    }
}

function isKeyEnglishCharacter(pKeyCode) {
    if (pKeyCode == null) return false;
    var eng_char = /^[A-Za-z0-9 ]+$/;

    if (eng_char.test(getChar(pKeyCode))) {
        return true;
    }
    else {
        window.event.keyCode = 0;
        return false;
    }
}


function isKey(evt) {
    var allowedEng = false; //อนุญาตให้คีย์อังกฤษ
    var allowedThai = true; //อนุญาตให้คีย์ไทย
    var allowedNum = false; //อนุญาตให้คีย์ตัวเลข
    var k = event.keyCode; /* เช็คตัวเลข 0-9 */
    if (k >= 48 && k <= 57) { return allowedNum; }

    /* เช็คคีย์อังกฤษ a-z, A-Z */
    if ((k >= 65 && k <= 90) || (k >= 97 && k <= 122)) { return allowedEng; }

    /* เช็คคีย์ไทย ทั้งแบบ non-unicode และ unicode */
    if ((k >= 161 && k <= 255) || (k >= 3585 && k <= 3675)) { return allowedThai; }
}


function isNumberStringKey(InString) {
    if (InString.length == 0) return (false);

    var RefString = "1234567890";
    for (Count = 0; Count < InString.length; Count++) {
        TempChar = InString.substring(Count, Count + 1);
        if (RefString.indexOf(TempChar, 0) == -1)
            return (false);
    }
    return (true);
}
function isCorrectEnglishName(name) {
    var reg = /^[A-Za-z ]+$/;
    return reg.test(name);
}

function isCorrectThaiName(name) {
    var reg = /^[ก-๛A-Za-z0-9 ]+$/;
    return reg.test(name);
}


function getChar(pKeyCode) {
    if (pKeyCode == 0) return '';
    else return String.fromCharCode(pKeyCode);
}

function isKeyCurrency(pValue, pKeyCode) {
    if (IsNull(pKeyCode)) return false;
    if (pKeyCode == 46) {
        if (pValue.indexOf(".") > -1) {
            window.event.keyCode = 0;
            return false;
        } else {
            return true;
        }
    } else if (pKeyCode == 44 || pKeyCode > 47 && pKeyCode < 58 || pKeyCode == 8 || pKeyCode == 13) {
        return true;
    }
    else {
        window.event.keyCode = 0;
        return false;
    }
}

function fnOnBlurOnly(o) {
    try {
        o.value = formatCurrency(o.value, 1, 2, '1');
        if ((parseFloat(o.value) > 4) || (parseFloat(o.value) <= 0)) {
            //alert("คุณระบุคะแนนเฉลี่ยไม่ถูกต้อง");
            o.value = "";
        }
    } catch (ex) {
        //nothing
    }

}

function checkID(id) {
    if (id.length != 13) return false;
    for (i = 0, sum = 0; i < 12; i++)
        sum += parseFloat(id.charAt(i)) * (13 - i); 
    if ((11 - sum % 11) % 10 != parseFloat(id.charAt(12)))
        return false; 
    return true;
}


function fnOnblur(o) {
    //return true;
    return CommaFormatted(o);
}
function IsNull(obj) {
    return (obj == null) ? true : false;
}
function CommaFormatted(amount) {
    var delimiter = ","; // replace comma if desired
    var a = amount.value.split('.', 2)
    var d = a[1];
    var i = parseInt(a[0]);
    if (isNaN(i)) { return ''; }
    var minus = '';
    if (i < 0) { minus = '-'; }
    i = Math.abs(i);
    var n = new String(i);
    var a = [];
    while (n.length > 3) {
        var nn = n.substr(n.length - 3);
        a.unshift(nn);
        n = n.substr(0, n.length - 3);
    }
    if (n.length > 0) { a.unshift(n); }
    n = a.join(delimiter);
    if (d.length < 1) { amount = n; }
    else { amount = n + '.' + d; }
    amount = minus + amount;
    amount.value = amount;
    return true;
}

function formatCurrency(num, pIntMaxScale, pIntPrecision, pBoolFullFormat) {
    if (num.length == 0) return "";
    var intPrecisionDevider = Math.pow(10, pIntPrecision);
    var satangs;
    num = num.toString().replace(/\,/g, '');
    num = num.replace(/ /gi, "");
    if (isNaN(num)) num = "0";

    var arrNum = num.split(".");
    var tmpNum = '';
    var tmpSatangs = '';
    tmpNum = arrNum[0];
    if (arrNum.length > 1) {
        tmpSatangs = arrNum[1];
    }
    if (tmpNum.toString().length > pIntMaxScale) {
        num = tmpNum.toString().substring(0, pIntMaxScale);
    } else {
        num = tmpNum;
    }
    if (tmpSatangs.toString().length > 0) {
        num = num + "." + tmpSatangs;
    }

    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * intPrecisionDevider + 0.50000000001);
    if (intPrecisionDevider > 0) {
        satangs = num % intPrecisionDevider;
        var intBasePrecision = intPrecisionDevider / 10;
        if (satangs < intBasePrecision) {
            satangs = satangs / intPrecisionDevider;
            if (satangs == 0) {
                satangs = intPrecisionDevider.toString().substring(1);
            }
            else {
                satangs = satangs.toString().split(".")[1];
            }
        }
        num = Math.floor(num / intPrecisionDevider).toString();
    } else satangs = "";
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) {
        num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    }
    var strResult = ((sign) ? '' : '-') + num;
    if (pIntPrecision > 0) {
        if (pBoolFullFormat == '1') {
            strResult += '.' + satangs;
        } else {
            if (parseInt(satangs, 10) == 0) {
                strResult;
            } else {
                if (satangs.toString().substring(0, 1) == '0') {
                    strResult += '.' + satangs;
                } else if (satangs.toString().substring(1, 2) == '0') {
                    strResult += '.' + satangs.toString().substring(0, 1);
                } else {
                    strResult += '.' + parseInt(satangs, 10).toString();
                }
            }
        }
    }
    return strResult;
}

function devContentRight_setHeight() {
    var devContentRight = $('#devContentRight');
    var devContentLeft = $('#devContentLeft');

    BrowserDetect.init();
    if (devContentRight.height() >= devContentLeft.height()) {
        switch (BrowserDetect.browser) {
            case "Chrome":
                {
                    //                    alert(devContentRight.height() + 200);
                    //devContentLeft[0].style.height = +'2000px';
                    devContentLeft.css('height', devContentRight.height() -18);
                    break;
                }
            default:
                devContentLeft.css('height', devContentRight.height() - 70);
//                switch (BrowserDetect.browser) {
//                    case "Explorer":
//                        {
//                            switch (BrowserDetect.version) {
//                                case 6:
//                                    {
//                                        alert("dfd");
//                                        devContentRight.offset({ top: 10, left: 300 });
//                                        break;
//                                    }
//                                default:

//                            }
//                            break;
//                        }
//                    default:
//                        
//                }
        }

    } else {
        devContentRight.css('height', devContentLeft.height()-20);
    }
}

function devContentRight_setHeight2(value) {
    var devContentRight = $('#devContentRight');
    var devContentLeft = $('#devContentLeft');

    BrowserDetect.init();
    if (devContentRight.height() >= devContentLeft.height()) {
        switch (BrowserDetect.browser) {
            case "Chrome":
                {
                    devContentLeft.css('height', devContentRight.height() +20);
                    break;
                }
//            case "Firefox":
//                {
//                    $("#devContentLeft").css('height', parseInt($(document).height() - $("#divfooter").height() - 300, 10) + "px");
//                    break;
//                }
            default:
                devContentLeft.css('height', devContentRight.height()+20);
        }

    } else {
        devContentRight.css('height', devContentLeft.height() - 20);
    }
}

function devContentRight_setHeight3(value) {
    var devContentRight = $('#devContentRight');
    var devContentLeft = $('#devContentLeft');

    BrowserDetect.init();
//    alert(BrowserDetect.browser);
//    alert(devContentLeft.height());
    ///if (devContentLeft.height()>100) {
        switch (BrowserDetect.browser) {
            case "Chrome":
                {
                    $("#devContentLeft").css('height', parseInt( $(document).height() - $("#divfooter").height()+9,10) + "px");
                    break;
                }
            case "Firefox":
                {
                    $("#devContentLeft").css('height', parseInt($(document).height() - $("#divfooter").height()-6, 10) + "px");
                    break;
                }
            case "Explorer":
                {
                    $("#devContentLeft").css('height', parseInt($(document).height() - $("#divfooter").height() -163, 10) + "px");
                    break;
                }
            default:
                devContentLeft.css('height', parseInt($(document).height() - $("#divfooter").height(), 10));
        }

   // } else {
   //     devContentRight.css('height', devContentLeft.height() - 20);
   // }
}

function devContentRight_setHeight4(value, Chrome, Firefox, Explorer) {
    var devContentRight = $('#devContentRight');
    var devContentLeft = $('#devContentLeft');

    BrowserDetect.init();
    //    alert(BrowserDetect.browser);
    //    alert(devContentLeft.height());
    ///if (devContentLeft.height()>100) {
    switch (BrowserDetect.browser) {
        case "Chrome":
            {
                parent.$("#devContentLeft").css('height', parseInt(value + 22 + Chrome) + "px");
                parent.$("#devContentRight").css('height', parseInt(value + 1 + Chrome) + "px");
                break;
            }
        case "Firefox":
            {
                parent.$("#devContentLeft").css('height', parseInt(value - 37 + Firefox) + "px");
                parent.$("#devContentRight").css('height', parseInt(value - 55 + Firefox) + "px");
                break;
            }
        case "Explorer":
            {
                parent.$("#devContentLeft").css('height', parseInt(value + Explorer) + "px");
                parent.$("#devContentRight").css('height', parseInt(value - 19 + Explorer) + "px");
                break;
            }
        default:
            parent.$("#devContentLeft").css('height', value + "px");
            parent.$("#devContentRight").css('height', value +  "px");
    }

    // } else {
    //     devContentRight.css('height', devContentLeft.height() - 20);
    // }
}

function devContentRight_setHeight_Normal() {
    var devContentRight = $('#devContentRight');
    var devContentLeft = $('#devContentLeft');

    BrowserDetect.init();
    if (devContentRight.height() >= devContentLeft.height()) {
        switch (BrowserDetect.browser) {
            case "Chrome":
                {
                    devContentLeft.css('height', devContentRight.height() +20);
                    break;
                }
            default:
                devContentLeft.css('height', devContentRight.height() +20);
        }

    } else {
        devContentRight.css('height', devContentLeft.height() - 20);
    }
}

function CheckNull(obj, checkLength) {
    if (checkLength) {
        return obj.length == 0;
    } else
        if (obj == "0" || obj == "00" || obj == "-1" || obj == "" || obj.length == 0 || obj == "0000" || obj == "--") {
            return true;
        }
    return false;
}

function AlertMessageSyn(msg) {
    setTimeout("alert('" + msg + "');", 100);
    return false;
}


var UserBlockObj = {
    blockUIObjTime: function (timer) {
        UserBlockObj.blockUIObj();
        setTimeout($.unblockUI, timer);
    },
    unblockUIObj: function () {
        $.unblockUI
    },
    blockUIObj: function () {
        $.blockUI({ css: {
            border: 'none',
            padding: '15px',
            // backgroundColor: '#000',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            opacity: .5,
            color: '#000'

        },
            message: "<h1><img src='../Images/busy.gif' >โปรดรอ....</h1>"
        });
    },
sleep:function (milliseconds) {
      var start = new Date().getTime();
      for (var i = 0; i < 1e7; i++) {
        if ((new Date().getTime() - start) > milliseconds){
          break;
        }
      }
    }
}

var BrowserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
		{
		    string: navigator.userAgent,
		    subString: "Chrome",
		    identity: "Chrome"
		},
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari",
		    versionSearch: "Version"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
    dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.userAgent,
		    subString: "iPhone",
		    identity: "iPhone/iPod"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

};

