﻿/* Common Javascript Functions là các hàm điều khiển tòan bộ thư viện 
* dựa trên nền chương trình AJAX_Request của tác giả Matt Kruse <matt@ajaxtoolbox.com> WWW: http://www.AjaxToolbox.com
* Và chương trình DHTML Window Widget- của Dynamic Drive tại địa chỉ: http://www.dynamicdrive.com
* Việc sử dụng hay tham khảo các đọan mã này hòan tòan miễn phí, nhưng với các tác giả được chú thích
* người dùng cần tuân thủ các yêu cầu sử dụng của riêng họ
* Tác giả chương trình: Quảng Nhân Trương Hữu Đức - Người phát triển codes cho Thư viện IT */

function Search_Result(intPageNum,intRowNum,intSortBy){
var thamchieu;
var searchin = document.getElementById("search_in");
var      selectroom = searchin.options[searchin.selectedIndex].value;
var searchinput = trim(document.getElementById("txtSearch").value);
if (selectroom==""){
alert("Xin chọn Chuyên mục cần tìm");
document.getElementById("search_in").focus();
return;
}
if (searchinput.length >= 2 && selectroom !="" ){
thamchieu  = 'text=';
thamchieu  += encodeURI(searchinput.split(' ').join('+'));
thamchieu  += '&p=';
thamchieu  += intPageNum;
thamchieu  += '&rownum=';
thamchieu  += intRowNum;
thamchieu  += '&orderby=';
thamchieu  += intSortBy;
if (selectroom == 1)           {search_url='/home/tinhoc/includes/search_result.asp?'+thamchieu;}
else if (selectroom == 2) {search_url='/home/ngoaingu/includes/search_result.asp?'+thamchieu;}
else if (selectroom == 3) {search_url='/home/service/phongsach_searchresult.asp?'+thamchieu;}
else if (selectroom == 4) {search_url='/home/service/giaitri_searchresult.asp?'+thamchieu;}
else if (selectroom == 5) {search_url='/home/forum/forum_top_searchresult.asp?'+thamchieu;}
else if (selectroom == 6)     {search_url='/home/service/test_searchresult.asp?'+thamchieu;}
preloading1('display_search_result');

AjaxRequest.get(
{
'url':''+search_url,
'onSuccess':function(req) { document.getElementById("display_search_result").innerHTML = req.responseText; }
}
);
}
}

function Close_Search_Result(){document.getElementById("display_search_result").innerHTML ='' }

function User_Agreement(){
top.window.scrollTo(0,40);
var agreewin=dhtmlmodal.open("agreewin", "iframe", "/home/service/thuvien_agreement.asp", "Thỏa thuận sử dụng - Terms of service", "width=520px,height=460px,center=1,top=10,resize=1,scrolling=0");
agreewin.onclose = function(){return true; };}

function Member_Registration(){
top.window.scrollTo(0,20);
var registerwin=dhtmlmodal.open("registerwin", "iframe", "/home/service/thuvien_register.asp", "Đăng ký thành viên - Member registration","width=600px,height=460px,center=1,resize=1,scrolling=0");
registerwin.onclose = function(){return true; };}

function Member_Library_WhatIs(){top.window.scrollTo(0,20);var mylibrary_whatis_win=dhtmlmodal.open("mylibrary_whatis_win", "iframe", "/home/service/thuvien_mylibrary_whatis.asp", "Thư viện Thành viên là gì? - What is My Library?", "width=540px,height=300px,center=1,resize=1,scrolling=0");mylibrary_whatis_win.onclose = function(){return true; };} 
function Contact_Admin(intRoomID,intThreadID,intAdminID){
var rid = intRoomID;
var contact_url;
thamchieu ='threadid=';
thamchieu +=intThreadID;
thamchieu +='&adminid=';
thamchieu +=intAdminID;
if (rid == 1)        {contact_url='/home/service/tinhoc_contact_admin.asp?'+thamchieu;}
else if (rid == 2) {contact_url='/home/service/nn_contact_admin.asp?'+thamchieu;}
else if (rid == 3) {contact_url='/home/service/phongsach_contact_admin.asp?'+thamchieu;}
else if (rid == 4) {contact_url='/home/service/giaitri_contact_admin.asp?'+thamchieu;}

top.window.scrollTo(0,20); 
var contact_adminwin=dhtmlmodal.open("contact_adminwin", "iframe", ""+contact_url, "Liên lạc quản trị viên - Contact Admin", "width=400px,height=360px,center=1,resize=1,scrolling=0");
contact_adminwin.onclose = function(){return true; };}

function Send_Login_Info(){
top.window.scrollTo(0,20);
var infowin=dhtmlmodal.open("infowin", "iframe", "/home/service/thuvien_request_info.asp", "Gửi lại thông tin - Resend Information", "width=300px,height=100px,center=1,resize=0,scrolling=0");
infowin.onclose = function(){return true; };}

function About_Us(){
top.window.scrollTo(0,20);
var aboutwin=dhtmlmodal.open("aboutwin", "iframe", "/home/service/thuvien_aboutus.asp", "Giới thiệu Thư viện - About Us", "width=520px,height=460px,center=1,resize=1,scrolling=0");
aboutwin.onclose = function(){return true; };}

function Contact_Us(){
top.window.scrollTo(0,20);
var contactwin=dhtmlmodal.open("contactwin", "iframe", "/home/service/thuvien_contactus.asp", "Liên lạc Thư viện - Contact Us", "width=520px,height=470px,center=1,resize=0,scrolling=0");
contactwin.onclose = function(){return true; };}

function Invite_Friend(){
top.window.scrollTo(0,20);
var invitewin=dhtmlmodal.open("invitewin", "iframe", "/home/service/thuvien_gioithieu.asp", "Giới thiệu bạn bè - Invite Friends", "width=520px,height=470px,center=1,resize=0,scrolling=0");invitewin.onclose = function(){return true; };}

function Member_Login(){
var loginwindow=dhtmlmodal.open("loginwindow", "iframe", "/home/service/thuvien_login.asp", "Đăng nhập thư viện - Member login", "width=510px,height=170px,center=1,resize=0,scrolling=0");
loginwindow.onclose = function(){return true;};}

function Member_Register_Login_Window(){var reg_login_window=dhtmlmodal.open("reg_login_window", "iframe", "/home/service/thuvien_reg_login_window.asp", "Đăng ký, Đăng nhập - Register, Login", "width=460px,height=80px,center=1,resize=0,scrolling=0");
reg_login_window.onclose = function(){return true;};}




function Log_Out(){
preloading1('user_logout');
AjaxRequest.get(
{
'url':'/home/service/thuvien_logoff.asp',
'onSuccess':function(req) {parent.window.location.reload();}
});}



function Show_Random_Quiz(intRoomID,question_type,question_id,submit_status,form_id)
/*id: Giữ QuizID, status: 1: Hiện Quiz Form, 2-Submit Quiz, 3 - Xem kết quả bình chọn. form_id: trường hợp nhiều Form, question_type: 1-Có đáp án, 2: thăm dò
*Hàm này dùng 1 file vừa hiện Quiz vừa hợp lệ cho 4 phòng, vừa xác định đó là  Quiz dạng có đáp án hoặc Quiz thăm dò ý kiến
*Lưu ý rằng kích thước Form hiện tại là giống nhau về chiều ngang
*/
{
var thamchieu;
var intStatus;
intStatus= submit_status;
if (intStatus == 2) {
myOption = -1;
var opt;
for (i=1; i<= document.forms["random_quizform"+form_id].quiz_choice.length; i++) {
opt=document.getElementById('quiz_choice' + i+form_id);    
if (opt.checked) { 
var user_select = document.getElementById('quiz_choice' + i+form_id).value;
myOption = i + 1; 
break;
}
}
if (myOption == -1) {
document.getElementById('show_quiz_alert'+form_id).innerHTML="<img src=\"/home/image/alert.jpg\" border=0>&nbsp;<i><font color=red>Xin bấm vào <img src=\"/home/image/unchecked.gif\" border=0> để chọn!</font></i>";
return;
}
}
thamchieu = 'roomid=';
thamchieu += intRoomID;
thamchieu += '&type=';
thamchieu += question_type;
thamchieu += '&questionid=';
thamchieu += question_id;
thamchieu += '&status=';
thamchieu += submit_status;
thamchieu += '&formid=';
thamchieu += form_id;
thamchieu +='&choice=';
thamchieu += user_select;
if (intStatus == 2 || intStatus == 3){
preloading1('show_quiz_alert'+form_id);}
AjaxRequest.get(
{
'url':'/home/service/thuvien_random_quiz.asp?'+thamchieu,
'onSuccess':function(req){document.getElementById('show_random_quiz'+form_id).innerHTML = req.responseText; }
});}

function mark_quiz_selectbox(choice,form_id){
for(i=1;i<=document.forms["random_quizform"+form_id].quiz_choice.length;i++){
document.getElementById('quiz_choice' + i+form_id).checked=false;
}	
document.getElementById(choice+form_id).checked=true;
}



function Send_Quiz_Result(questionid,choice)
{
var thamchieu;
 var email= trim(document.getElementById("sender_email_quizresult").value);
 if ((email==' ') || (email==null) || (email.indexOf(".") == -1) || (email.indexOf("@") == -1) || (email.length <6) ) 
 { 
alert("Địa chỉ email không hợp lệ!");
return (false);
 }
thamchieu='id=';
thamchieu+=questionid;
thamchieu+='&choice=';
thamchieu+=choice;
thamchieu+='&email=';
thamchieu+=email;
preloading('send_quiz_result');
AjaxRequest.get(
{
'url':'/home/service/thuvien_send_quizresult.asp?'+thamchieu,
'onSuccess':function(req){
document.getElementById("send_quiz_result").innerHTML =req.responseText;  }
}
);
}


function Disable_Enter_Key(evt) {
   var evt = (evt) ? evt : ((event) ? event : null);
   var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
   if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}
document.onkeypress = Disable_Enter_Key; 



function Help(intHelpID) {
var h_id = intHelpID;
var help_url;
if (h_id == 1)       {help_url='/home/testcenter/help_howtouse.asp';}
else if (h_id == 2) {help_url='/home/testcenter/help_userform.asp';}
preloading('Show_Help');
AjaxRequest.get(
{
'url':''+help_url,
'onSuccess':function(req){document.getElementById("Show_Help").innerHTML = req.responseText; }
}
);
}



function showDropDown(parentEle, dropDownEle, dropDownWidth, offSetRight){

	parentElement = document.getElementById(parentEle);
	dropDownElement = document.getElementById(dropDownEle)

	//position
	dropDownElement.style.left = (getOffsetLeft(parentElement) - offSetRight) + 'px';
	dropDownElement.style.top = (getOffsetTop(parentElement) + parentElement.offsetHeight + 3) + 'px';

	//width
	dropDownElement.style.width = dropDownWidth + 'px';

	//display
	hideDropDown();
	dropDownElement.style.visibility = 'visible';


	//Event Listener to hide drop down
	if(document.addEventListener){ // Mozilla, Netscape, Firefox
		document.addEventListener('mouseup', hideDropDown, false);
	} else { // IE
		document.onmouseup = hideDropDown;
	}
}

//Hide drop downs
function hideDropDown(){
	hide('div');
	hide('iframe');
	function hide(tag){
		var classElements = new Array();
		var els = document.getElementsByTagName(tag);
		var elsLen = els.length;
		var pattern = new RegExp('(^|\\s)dropDown(.*\)');

		for (i = 0, j = 0; i < elsLen; i++){
			if (pattern.test(els[i].className)){
				els[i].style.visibility='hidden';
				j++;
			}
		}
	}
}

//Top offset
function getOffsetTop(elm){
	var mOffsetTop = elm.offsetTop;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent){
		mOffsetTop += mOffsetParent.offsetTop;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetTop;
}

//Left offset
function getOffsetLeft(elm){
	var mOffsetLeft = elm.offsetLeft;
	var mOffsetParent = elm.offsetParent;
	while(mOffsetParent){
		mOffsetLeft += mOffsetParent.offsetLeft;
		mOffsetParent = mOffsetParent.offsetParent;
	}
	return mOffsetLeft;
}



function createCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function readCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

function eraseCookie(name) {
	createCookie(name,"",-1);

}


function Check_UserID(intUserID, strReturnLink) {
if (intUserID == 0)       {
alert('Bạn phải đăng nhập hoặc đăng ký để làm bài này!');
Member_Register_Login_Window(); 
return false;

}
else {window.location.href=strReturnLink;
}
}

/************************************************************************/
/* Rainbow Links Version 1.03 (2003.9.20)                               */
/* Script updated by Dynamicdrive.com for IE6                           */
/* Copyright (C) 1999-2001 TAKANASHI Mizuki                             */
/* takanasi@hamal.freemail.ne.jp                                        */
/************************************************************************/

// Setting
var rate = 20;  // Increase amount(The degree of the transmutation)


// Main routine

if (document.getElementById)
window.onerror=new Function("return true")

var objActive;  // The object which event occured in
var act = 0;    // Flag during the action
var elmH = 0;   // Hue
var elmS = 128; // Saturation
var elmV = 255; // Value
var clrOrg;     // A color before the change
var TimerID;    // Timer ID


if (document.all) {
    document.onmouseover = doRainbowAnchor;
    document.onmouseout = stopRainbowAnchor;
}
else if (document.getElementById) {
    document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
    document.onmouseover = Mozilla_doRainbowAnchor;
    document.onmouseout = Mozilla_stopRainbowAnchor;
}


function doRainbowAnchor()
{
    if (act == 0) {
        var obj = event.srcElement;
        while (obj.tagName != 'A' && obj.tagName != 'BODY') {
            obj = obj.parentElement;
            if (obj.tagName == 'A' || obj.tagName == 'BODY')
                break;
        }

        if (obj.tagName == 'A' && obj.href != '') {
            objActive = obj;
            act = 1;
            clrOrg = objActive.style.color;
            TimerID = setInterval("ChangeColor()",100);
        }
    }
}


function stopRainbowAnchor()
{
    if (act) {
        if (objActive.tagName == 'A') {
            objActive.style.color = clrOrg;
            clearInterval(TimerID);
            act = 0;
        }
    }
}


function Mozilla_doRainbowAnchor(e)
{
    if (act == 0) {
        obj = e.target;
        while (obj.nodeName != 'A' && obj.nodeName != 'BODY') {
            obj = obj.parentNode;
            if (obj.nodeName == 'A' || obj.nodeName == 'BODY')
                break;
        }

        if (obj.nodeName == 'A' && obj.href != '') {
            objActive = obj;
            act = 1;
            clrOrg = obj.style.color;
            TimerID = setInterval("ChangeColor()",100);
        }
    }
}


function Mozilla_stopRainbowAnchor(e)
{
    if (act) {
        if (objActive.nodeName == 'A') {
            objActive.style.color = clrOrg;
            clearInterval(TimerID);
            act = 0;
        }
    }
}



function ChangeColor()
{
    objActive.style.color = makeColor();
}


function makeColor()
{
    // Don't you think Color Gamut to look like Rainbow?

    // HSVtoRGB
    if (elmS == 0) {
        elmR = elmV;    elmG = elmV;    elmB = elmV;
    }
    else {
        t1 = elmV;
        t2 = (255 - elmS) * elmV / 255;
        t3 = elmH % 60;
        t3 = (t1 - t2) * t3 / 60;

        if (elmH < 60) {
            elmR = t1;  elmB = t2;  elmG = t2 + t3;
        }
        else if (elmH < 120) {
            elmG = t1;  elmB = t2;  elmR = t1 - t3;
        }
        else if (elmH < 180) {
            elmG = t1;  elmR = t2;  elmB = t2 + t3;
        }
        else if (elmH < 240) {
            elmB = t1;  elmR = t2;  elmG = t1 - t3;
        }
        else if (elmH < 300) {
            elmB = t1;  elmG = t2;  elmR = t2 + t3;
        }
        else if (elmH < 360) {
            elmR = t1;  elmG = t2;  elmB = t1 - t3;
        }
        else {
            elmR = 0;   elmG = 0;   elmB = 0;
        }
    }

    elmR = Math.floor(elmR).toString(16);
    elmG = Math.floor(elmG).toString(16);
    elmB = Math.floor(elmB).toString(16);
    if (elmR.length == 1)    elmR = "0" + elmR;
    if (elmG.length == 1)    elmG = "0" + elmG;
    if (elmB.length == 1)    elmB = "0" + elmB;

    elmH = elmH + rate;
    if (elmH >= 360)
        elmH = 0;

    return '#' + elmR + elmG + elmB;
}


function setScreen(){
var defaultwidth = readCookie('screenres');
var width = screen.width;
if ((!defaultwidth) || (defaultwidth!=width)) {
createCookie('screenres',width,365);
}
}

