var ctx="/hinhnen/";
var ca = new Array("a", "à.á.ạ.ả.ã.â.ầ.ấ.ậ.ẩ.ẫ.ă.ằ.ắ.ặ.ẳ.ẵ.À.Á.Ạ.Ả.Ã.Â.Ầ.Ấ.Ậ.Ẩ.Ẫ.Ă.Ằ.Ắ.Ặ.Ẳ.Ẵ");
var ce = new Array("e", "è.é.ẹ.ẻ.ẽ.ê.ề.ế.ệ.ể.ễ.È.É.Ẹ.Ẻ.Ẽ.Ê.Ề.Ế.Ệ.Ể.Ễ");
var ci = new Array("i", "ì.í.ị.ỉ.ĩ.Ì.Í.Ị.Ỉ.Ĩ");
var co = new Array("o", "ò.ó.ọ.ỏ.õ.ô.ồ.ố.ộ.ổ.ỗ.ơ.ờ.ớ.ợ.ở.ỡ.Ò.Ó.Ọ.Ỏ.Õ.Ô.Ồ.Ố.Ộ.Ổ.Ỗ.Ơ.Ờ.Ớ.Ợ.Ở.Ỡ");
var cu = new Array("u", "ù.ú.ụ.ủ.ũ.ư.ừ.ứ.ự.ử.ữ.Ù.Ú.Ụ.Ủ.Ũ.Ư.Ừ.Ứ.Ự.Ử.Ữ");
var cy = new Array("y", "ỳ.ý.ỵ.ỷ.ỹ.Ỳ.Ý.Ỵ.Ỷ.Ỹ");
var cd = new Array("d", "đ.Đ");
var bangkitucoso = new Array(ca, ce, ci, co, cu, cy, cd);
function mLocDau(bv)
{
    var markhongdau = new Array();
    markhongdau[0] = bv;
    for (var j = 0; j < bangkitucoso.length; j++) // max j = 8
    {
        var marthaythe = bangkitucoso[j][1].split('.'); // tach cac chuoi co dau thanh tung phan tu rieng biet
        for (var i = 0; i < marthaythe.length; i++)
        {
            markhongdau[i + 1] = mThayThe(markhongdau[i], marthaythe[i], bangkitucoso[j][0]);
            markhongdau[0] = markhongdau[i + 1].toLowerCase();
        }
    }

    return markhongdau[0];
}
function mThayThe(ccha, cbithay, cthaythe)
{
    var ccon = "/" + cbithay + "/gi";
    return ccha.replace(eval(ccon), cthaythe);
}

var hnWindow;
var width, height, left, wtop, styleStr;
function XemHinh(imgId) {
    width = 650;
    height = 656;
    left = (screen.width / 2) - width / 2;
    //left = 20;
    wtop = (screen.height / 2) - height / 2;
    //wtop = ((screen.height / 2) - height / 2);
    styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=0,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ',top=' + wtop + ',screenX=' + left + ',screenY=' + wtop;
    hnWindow = window.open(ctx+'popup.jsp?id=' + imgId, "hnWindow", styleStr);
    hnWindow.focus();
}

function XemVideo(imgId) {
    width = 650;
    height = 485;
    left = (screen.width / 2) - width / 2;
    //left = 20;
    wtop = (screen.height / 2) - height / 2;
    //wtop = ((screen.height / 2) - height / 2);
    styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=0,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ',top=' + wtop + ',screenX=' + left + ',screenY=' + wtop;
    hnWindow = window.open(ctx+'popup-v.jsp?id=' + imgId, "hnWindow", styleStr);
    hnWindow.focus();
}

function XemAlbum(imgId) {
    width = "650";
    height = "656";
    left = (screen.width / 2) - width / 2;
    wtop = (screen.height / 2) - height / 2;
    styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=0,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ',top=' + wtop + ',screenX=' + left + ',screenY=' + wtop;
    hnWindow = window.open(ctx+'popup-a.jsp?id=' + imgId, "hnWindow", styleStr);
    hnWindow.focus();
}

var keepIt = false;

function MM_preloadImages() { //v3.0
    checkImages();
    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];
            }
    }
}

var alertTimerId;
function IsImageOk(img) {
    try {
        if (!img.complete) {
            return false;
        }

        if ((typeof img.naturalWidth != "undefined")
                && (img.naturalWidth == 0)) {
            return false;
        }

        return true;
    } catch(e) {
        return true;
    }
}

function checkImages() {
    try {
        if (!document.images) return;
        var failCount;
        failCount = 0;
        var newImg;
        var oldImg;
        for (var i = 0; i < document.images.length; i++) {
            oldImg = document.images[i];
            if (!IsImageOk(oldImg)) {
                newImg = new Image();
                newImg.src = oldImg.src;
                newImg.alt = oldImg.alt;
                newImg.id = oldImg.id;
                newImg.onclick = oldImg.onclick;
                oldImg.parentNode.insertBefore(newImg,oldImg);
                oldImg.parentNode.removeChild(oldImg);
                failCount = failCount + 1;
            }
        }
        if (failCount > 0) {
            alertTimerId = setTimeout("checkImages()", 1250);
        }
    } catch(e) {
//        document.getElementById('search').value = e + '';
    }
}

function NChim(str) {
    var whitespace = ' \n\r\t\f';
    var counter;
    counter = 0;
    for (var i = 0; i < str.length; i++) {
        if (whitespace.indexOf(str.charAt(i)) == -1) {
            str = str.substring(i);
            break;
        }
        counter++;
    }
    if (counter == str.length)return '';

    for (i = str.length - 1; i >= 0; i--) {
        if (whitespace.indexOf(str.charAt(i)) == -1) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return whitespace.indexOf(str.charAt(0)) == -1 ? str : '';
}


function showVote(newVote, sId) {
    var elm;
    for (var i = 1; i <= 5; i++) {
        elm = document.getElementById(sId + '_' + i);
        elm.className = ((i <= newVote) ? 'saocam' : 'saoden');
    }
}

function restoreVote(originVote, sIdh) {
    var elm;
    for (var i = 1; i <= 5; i++) {
        elm = document.getElementById(sIdh + '_' + i);
        elm.className = ((i <= originVote) ? 'saovang' : 'saoden');
    }
}

//0: System error
//1: Success
//2: Name too large
//3: Comment too large
//4: Name null
//5: Comment null
function showCmResult(obj) {
    var result = NChim(obj.responseText);
    var commentResult = document.getElementById('commentResult');
    var enableSend;
    enableSend = true;
    if (result == '1') {
        commentResult.innerHTML = '<span class="trang11">Lời bình của bạn đã được gửi thành công!</span>';
        document.getElementById('comment').value = '';
    } else if (result == '2') {
        commentResult.innerHTML = '<span class="error">Tên người gửi quá dài<span>';
        document.getElementById('commenter').focus();
        document.getElementById('commenter').select();
    } else if (result == '3') {
        commentResult.innerHTML = '<span class="error">Lời bình quá dài<span>';
        document.getElementById('comment').focus();
        document.getElementById('comment').focus();
    } else if (result == '4') {
        commentResult.innerHTML = '<span class="error">Chưa nhập người gửi<span>';
        document.getElementById('commenter').focus();
        document.getElementById('commenter').select();
    } else if (result == '5') {
        commentResult.innerHTML = '<span class="error">Chưa nhập lời bình<span>';
        document.getElementById('comment').focus();
        document.getElementById('comment').focus();
    } else if (result == '6') {
        commentResult.innerHTML = '<span class="error">Bạn chỉ được phép gửi 3 lời bình cho 1 bài<span>';
        enableSend = false;
    } else {
        commentResult.innerHTML = '<span class="error">Hiện tại hệ thống chưa sẵn sàng, mời bạn thử lại sau<span>';
    }

    document.getElementById('sendbt').enabled = enableSend;
}

function commonFailure() {
    //Do nothing
}

function showResult(obj) {
    var result = obj.responseText.split(',');
    if (result.length == 2) {
        var voteStar = NChim(result[0]);
        var voteCount = NChim(result[1]);
        document.getElementById('voteImg').innerHTML = '<div class="saovang' + voteStar + '">&nbsp;</div>';
        document.getElementById('voteCount').innerHTML = '<span class="ghi11" style="padding-left:5px;">(' + voteCount + ' lượt)</span>';
        setTimeout("alert('Cảm ơn bạn đã đánh giá!')", 500);
    }
}

function sendVote(v, sid) {
    document.getElementById("voteImg").innerHTML = '<img src="'+ctx+'images/vote/loading1.gif" alt="Đang xử lý..." border="0"/>';
    YAHOO.util.Connect.asyncRequest("POST", ctx+"vote.jsp",
    {
        success: showResult,
        failure: commonFailure
    }, "v=" + v + "&sid=" + sid + "&cache=0"
            );
}

function sendVoteAlbum(v, sid) {
   document.getElementById("voteImg").innerHTML = '<img src="'+ctx+'images/vote/loading1.gif" alt="Đang xử lý..." border="0"/>';
    YAHOO.util.Connect.asyncRequest("POST", ctx+"vote.jsp",
    {
        success: showResult,
        failure: commonFailure
    }, "a=1&v=" + v + "&sid=" + sid + "&cache=0"
            );
}

function sendComment(songId) {
    var commentResult = document.getElementById('commentResult');
    commentResult.innerHTML = '<img src="'+ctx+'images/vote/loading1.gif" alt="Đang xử lý..." border="0"/>';
    var sendBt = document.getElementById('sendbt');
    sendBt.enabled = false;

    var commenter = document.getElementById('commenter').value;
    var comment = document.getElementById('comment');
    YAHOO.util.Connect.asyncRequest("POST", ctx+"commenter.jsp",
    {
        success: showCmResult,
        failure: commonFailure
    }, "cache=0&songId=" + songId + "&commenter=" + encodeURIComponent(commenter)+"&comment="+encodeURIComponent(comment.value)
            );
}

function sendAlbum(songId) {
    var commentResult = document.getElementById('commentResult');
    commentResult.innerHTML = '<img src="'+ctx+'images/vote/loading1.gif" alt="Đang xử lý..." border="0"/>';
    var sendBt = document.getElementById('sendbt');
    sendBt.enabled = false;

    var commenter = document.getElementById('commenter').value;
    var comment = document.getElementById('comment');
    YAHOO.util.Connect.asyncRequest("POST", ctx+"commenter.jsp",
    {
        success: showCmResult,
        failure: commonFailure
    }, "cache=0&a=1&songId=" + songId + "&commenter=" + encodeURIComponent(commenter)+"&comment="+encodeURIComponent(comment.value)
            );
}

function checkInput() {
    var commenter = document.getElementById('commenter');
    if (NChim(commenter.value) == '') {
        alert('Chưa nhập người gửi');
        commenter.focus();
        return false;
    }

    var comment = document.getElementById('comment');
    if (NChim(comment.value) == '') {
        alert('Chưa nhập lời bình');
        comment.focus();

        return false;
    }

    return true;
}

function showQuiz(quizId) {
    popup(ctx+'quizResult.jsp?quizId=' + quizId, 240, 500);
}

function checkAndSubmit(theForm, quizId) {
    var chked;
    chked = false;
    var q;
    q = '';
    var checkedValue;
    checkedValue = '';
    for (var i = 0; i < theForm.length; i++) {
        var e = theForm.elements[i];
        if ((e.type.toLowerCase() == "checkbox") && e.checked) {
            checkedValue = e.value;
            chked = true;
            if (q != '')q = q + ',';
            q += e.value;
        }
    }
    if (!chked) {
        alert('Hãy chọn một trong các mục trước khi biểu quyết');
        return false;
    }

    popup(ctx+'quizing.jsp?quizId=' + quizId + '&cache=0&q=' + q, 240, 500);

    return true;
}

var quizWindow;
function popup(url, height, width) {
    left = (screen.width / 2) - width / 2;
    wtop = (screen.height / 2) - height / 2;
    styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=0,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ',top=' + wtop + ',screenX=' + left + ',screenY=' + wtop;
    quizWindow = window.open(url, "QuizWindow", styleStr);

    quizWindow.focus();
}

function popup2(url, height, width) {
    left = (screen.width / 2) - width / 2;
    wtop = (screen.height / 2) - height / 2;
    styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=0,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ',top=' + wtop + ',screenX=' + left + ',screenY=' + wtop;
    quizWindow = window.open(url, "QuizWindow", styleStr);

    quizWindow.focus();
}

function chooseElement(theForm, chooseId, single) {
    var chooseCb = document.getElementById(chooseId);
    if (chooseCb.checked && single) {
        for (var i = 0; i < theForm.length; i++) {
            var e = theForm.elements[i];
            if ((e.type.toLowerCase() == "checkbox") && (e.id != chooseId)) {
                e.checked = false;
            }
        }
    }
}

var gopyWindow;
function gopy() {
    width = "1004";
    height = "310";
    left = (screen.width / 2) - width / 2;
    wtop = (screen.height / 2) - height / 2;
    styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=0,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ',top=' + wtop + ',screenX=' + left + ',screenY=' + wtop;
    gopyWindow = window.open(ctx+'gopy.jsp', "gopyWindow", styleStr);

    gopyWindow.focus();
}

var loading;
function showLoading() {
    loading = document.getElementById('loading');
    loading.style.visibility = 'visible';
    loading.style.right = '1px';
    loading.style.zIndex = '20';
}

function hideLoading() {
    loading = document.getElementById('loading');
    loading.style.visibility = 'hidden';
    loading.style.right = '-350px';
    loading.style.zIndex = '0';
}


