﻿function MM_swapImgRestore() { //v3.0
	var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d = document; if (d.images) {
		if (!d.MM_p) d.MM_p = new Array();
		var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
			if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; }
	}
}

function MM_findObj(n, d) { //v4.01
	var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
		d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
	for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
	if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
		if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

jQuery(function () {
        jQuery('.showinfo').each(function () {
            var distance = 0;
            var time = 200;
            var hideDelay = 500;

            var hideDelayTimer = null;

            var beingShown = false;
            var shown = false;
            var trigger = jQuery('.trigger', this);
            var info = jQuery('.popup', this).css('opacity', 0);


            jQuery([trigger.get(0), info.get(0)]).mouseover(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                if (beingShown || shown) {
                    // don't trigger the animation again
                    return;
                } else {
                    // reset position of info box
                    beingShown = true;

                    info.css({
                        top: 30,
                        left: 0,
                        display: 'block'
                    }).animate({
                        top: '-=' + distance + 'px',
                        opacity: 1
                    }, time, 'swing', function() {
                        beingShown = false;
                        shown = true;
                    });
                }

                return false;
            }).mouseout(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                hideDelayTimer = setTimeout(function () {
                    hideDelayTimer = null;
                    info.animate({
                        top: '-=' + distance + 'px',
                        opacity: 0
                    }, time, 'swing', function () {
                        shown = false;
                        info.css('display', 'none');
                    });

                }, hideDelay);

                return false;
            });
        });
    });
    
    function CheckNumeric(e) 
{
 if(!e)
    e = window.event;
	var key;
	
    if(e.keyCode)
	{
       	//alert(e.keyCode);	
		key = e.keyCode;
	}		
    else
    {			
		//alert(e.charCode);
		key=e.charCode;
	}
	//alert(key);
	if ( key > 47 && key < 58 || (key == 9 || key == 8))     
	{
		//alert (key);
		return
	}
	else if (window.event) //IE       
	{
		window.event.returnValue = null
	}
	else //Firefox       
	{
		e.preventDefault()
	}
}


// Hàm chặn phím enter
// Các dùng: bắt sự kiện onkeypress = "return preventEnter(event)"
function preventEnter(e) 
{   
	var key //= (window.event) ? event.keyCode : e.which;   
	if (window.event)     
	{
		key = event.keyCode   
	}
	else     
	{
		key = e.which   		
	}
	
	if ( key != 13 )     
	{
		return
	}	
	else if (window.event) //IE       
	{
		window.event.returnValue = null
	}
	else //Firefox       
	{
		e.preventDefault()
	}
}
function isEmpty(s) {
    return (s == null || s.length == 0); //false isnot empty
}
function isAlpha(str) {
    var re = /[^a-zA-Z]/g
    if (re.test(str)) return false;
    return true;
}
// returns true if the string only contains characters 0-9
function isNumber(s) {
    var r = /^\d+$/; return r.test(s);
}
// returns true if the string only contains characters A-Z, a-z or 0-9
function isAlphanumeric(s) {
    var r = /^[a-zA-Z0-9]+$/; return r.test(s);
}
// Trả về giá trị True nếu địa chỉ Email hợp lệ
function isValidEmail(str)
{
    return str.match(/^([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$/)
}

// Hàm cắt khoảng trắng 2 ở đầu chuỗi
function sTrim(s)
{	
	while(s.charCodeAt(0) <= 32)
	{
		s = s.substr(1)
	}
	while(s.charCodeAt(s.length - 1) <= 32)
	{
		s = s.substr(0, s.length - 1)
	}
	return s
}	

// Hàm kiểm tra ngày hợp lệ
function isDate(day, month, year)
{
	if ((month < 1) || (month > 12)) return false
	var dt = new Date(year, month-1, day)
	if (dt.getDay() != day) 
		return false
	else
		return true
}
function ResizeImages(img) 

{

    if(img.width > 540) 

    {

        img.height = parseInt(img.height * 540 / img.width);

        img.width = 540; 

    }

}

function loadXMLFile(dname) {
    try //Internet Explorer
        {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    }
    catch (e) {
        try //Firefox, Mozilla, Opera, etc.
            {
            xmlDoc = document.implementation.createDocument("", "", null);
        }
        catch (e) { alert(e.message) }
    }
    try {
        xmlDoc.async = false;
        xmlDoc.load(dname);
        return (xmlDoc);
    }
    catch (e) { alert(e.message) }
    return (null);
}

// XmlHttp object class
function XmlHttp() {
    this.array = new Array(1);

    this.setValue = function(v) { this.array[0] = v; }
    this.getValue = function() { return this.array[0]; }
}

function loadXMLDoc(xmlHttp, url, callback, content) {
    xmlHttp.setValue(createXMLHttpRequest())
    if (xmlHttp.getValue()) {
        xmlHttp.getValue().onreadystatechange = function() { eval(callback) }
        xmlHttp.getValue().open('POST', url, true)
        xmlHttp.getValue().setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
        xmlHttp.getValue().send(content)
        return true
    }
    else {
        alert('Trình duyệt của bạn không hỗ trợ AJAX!\nBạn hãy sử dụng trình duyệt IE 4.0 trở lên hoặc Mozilla FireFox 1.0 trở lên')
        return false
    }
}

function createXMLHttpRequest() {
    var xmlHttp = null
    try {	// Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest()
    }
    catch (e) {	// Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")
            }
            catch (e) {
                return null
            }
        }
    }
    return xmlHttp
}

