if (typeof(objAnnoViewer) == "undefined") {
	var objAnnoViewer = new Object();
	objAnnoViewer.booIsReady = true;
	objAnnoViewer.objBookData = new Object();
	objAnnoViewer.strPgSid = "";
}
var Init = function(){
	if (location.href.indexOf(".myhyperbook.com") == -1 && top.location.href.indexOf(".myhyperbook.com") == -1 && location.href.indexOf("//localhost") == -1 && location.href.indexOf("//snoopy-nb") == -1 && location.href.indexOf("//test-server") == -1) {
		// top.location.replace("http://www.myhyperbook.com"); // Modify by EuroStar - 2009/8/5
	}	
	var objKeyword = $("txtKeyword");
	var objDDListNP = $("DDListNP");
	if (objKeyword.addEventListener) {
		objKeyword.addEventListener("keydown", txtKeyword_OnKeyDown, false);
		objDDListNP.addEventListener("change", DDListNP_OnChange, false);
		window.addEventListener("resize", Window_OnResize, false);
		window.addEventListener("scroll", Window_OnScroll, false);
	} else if (objKeyword.attachEvent) {
		objKeyword.attachEvent("onkeydown", txtKeyword_OnKeyDown);
		objDDListNP.attachEvent("onchange", DDListNP_OnChange);
		window.attachEvent("onresize", Window_OnResize);
		window.attachEvent("onscroll", Window_OnScroll);
	} else {
		objKeyword.onkeydown = txtKeyword_OnKeyDown;
		objDDListNP.onchange = DDListNP_OnChange;
		window.onresize = Window_OnResize;
		window.onscroll = Window_OnScroll;
	}
	Window_OnResize();
	try {
		if (objKeyword) {
			objKeyword.focus();
		}
	} catch (e) {
	}
};
var OpenBook = function(_strBkSid){
	try {
		if (!openWindow.closed) {
			openWindow.close();
		}
	} catch (e) {
	}
	var _strUrl = "/annotation.aspx?bksid=" + _strBkSid;
	location.href = _strUrl;
};
var GoMyTools = function(_tp) {
   //alert("_intLevel:"+_intLevel+",_tp:"+_tp); // Modify by EuroStar - 2009/8/6
   if (_intLevel == 11) {
      alert("Please Login first.");
      return;
   } else {
      switch (_tp) {
         case 1: /*我讀過的書*/
            location.href = "member/read.aspx";
            break;
         case 2: /*我的美學收藏*/
            location.href = "member/collection.aspx";
            break;
         case 3: /*我的推薦*/
            location.href = "member/commend.aspx";
            break;
         case 4: /*我提供的註解*/
            location.href = "member/annotation.aspx";
            break;
      }
   }
};
var CheckCode = function(_objSrc, _args){
	var _strRange = "0123456789- ";
	var _strValue = _args.Value.split("-").join("").split(" ").join("");
	var _strISSN;
	if ($("HFieldType").value == "3") {
		if (_strValue.length < 1) {
		   alert("Please enter newspaper number.");
			_args.IsValid = false;
			return;
		}
		for (var i = 0; i < _strValue.length; i++) {
			if (_strRange.indexOf(_strValue.substr(i, 1)) == -1) {
			   alert("Only numerical number acceptable, please re-enter.");
				_args.IsValid = false;
				return;
			}
		}
	}
	switch ($("HFieldType").value) {
		case "0": /*超書碼*/
			if (_strValue.length < 1) {
			   alert("Please enter Hyperbook number.");
				_args.IsValid = false;
				return;
			}
			break;
		case "1": /*ISBN*/
			if (_strValue.length == 13) {
				if (!checkISBN13(_strValue)) {
				   alert("Error ISBN number,  please check and retry.");
					_args.IsValid = false;
					return;
				}
			} else if (_strValue.length == 10) {
				if (!checkISBN10(_strValue)) {
				   alert("Error ISBN number,  please check and retry.");
					_args.IsValid = false;
					return;
				}
			} else {
			alert("ISBN number should be 10 or 13 digits, please re-enter.");
				_args.IsValid = false;
				return;
			}
			break;
		case "2": /*ISSN*/
			if (_strValue.length >= 8) {
				_strISSN = _strValue.substr(0, 8);
				if (!checkISSN(_strISSN)) {
				   alert("Error ISSN number,  please check and retry.");
					_args.IsValid = false;
					return;
				}
			} else {
			alert("ISSN number should be more than 8 digits, please re-enter.");
				_args.IsValid = false;
				return;
			}
			break;
		case "9": /*書名*/
			if (_strValue.length < 2) {
			   alert("Book name should be more than 2 words.");
				_args.IsValid = false;
				return;
			}
			break;
	}
	//$("txtKeyword").value = _strValue;
	_args.IsValid = true;
};
var SetType = function(_tp) {
    var _strType, _obj = $("HFieldType"), _objBtn = $("btnSearch"), _objBtn2 = $("btnSearch2"), _objDDList = $("DDListNP");
    var _objDiv = $("bookCalendarDiv"), _objText = $("txtKeyword");
    if ($("HFieldMode")) {
        $("HFieldMode").value = "0";
    }
    if (_obj && _objBtn && _objBtn2 && _objDDList && _objDiv && _objText) {
        if ($("typeA" + _obj.value)) {
            $("typeA" + _obj.value).className = "umba_type_normal";
        }
        _obj.value = _tp.toString();
        if ($("typeA" + _obj.value)) {
            $("typeA" + _obj.value).className = "umba_type_focus";
        }
        switch (_obj.value) {
            case "0":
               _objText.title = "Please enter Hyperbook number";
               _objBtn.value = "Search";
                _objBtn2.style.display = "none";
                _objDDList.style.display = "none";
                _objDiv.style.display = "none";
                break;
            case "1":
               _objText.title = "Please enter ISBN number";
               _objBtn.value = "Search";
                _objBtn2.style.display = "none";
                _objDDList.style.display = "none";
                _objDiv.style.display = "none";
                break;
            case "2":
               _objText.title = "Please enter ISSN number";
               _objBtn.value = "Search(current issue)";
               _objBtn2.value = "Search(past issue)";
                _objBtn2.style.display = "block";
                _objDDList.style.display = "none";
                _objDiv.style.display = "none";
                break;
            case "3":
               _objText.title = "Please enter newspaper number";
               _objBtn.value = "Search(today)";
               _objBtn2.value = "Search(past)";
                _objBtn2.style.display = "block";
                _objDDList.style.display = "block";
                _objDiv.style.display = "block";
                break;
            case "9":
               _objText.title = "Please enter book name";
               _objBtn.value = "Search";
                _objBtn2.style.display = "none";
                _objDDList.style.display = "none";
                _objDiv.style.display = "none";
                break;
        }
        try {
            _objText.select();
        } catch (e) { }
        _objText.focus();
    }
};

var checkISSN = function(_code){
	_code = _code.substr(0, 8);
	_code = (_code + '').replace(/[-\s]/g, '');
	if (!/^\d{7}[\dxX]?$/.test(_code)) {
		return false;
	}
	var i = 0, c = 0; /*c:checksum*/
	for (i = 0; i < 7; i++) {
		c += _code.charAt(i) * (8 - i);
	}
	c %= 11;
	c = 11 - c;
	if (c == 11) {
		c = 0;
	}
	if (c == 10) {
		c = 'X';
	}
	return c == (i = _code.charAt(7)) || c == 'X' && i == 'x';
};

var checkISBN10 = function(_code){
	_code = (_code + '').replace(/[-\s]/g, '');
	if (!/^\d{9}[\dxX]?$/.test(_code)) {
		return false;
	}
	var i = 0, c = 0; /*c:checksum*/
	for (i = 0; i < 9; i++) {
		c += _code.charAt(i) * (10 - i);
	}
	c %= 11;
	c = 11 - c;
	if (c == 11) {
		c = 0;
	}
	if (c == 10) {
		c = 'X';
	}
	return c == _code.charAt(9).toUpperCase();
};
var checkISBN13 = function(_code){
	_code = (_code + '').replace(/[-\s]/g, '');
	if (!/^\d{12,13}$/.test(_code)) {
		return false;
	}
	var i = 1, c = 0;
	for (; i < 12; i += 2) {
		c += Math.floor(_code.charAt(i));
	}
	for (c *= 3, i = 0; i < 12; i += 2) {
		c += Math.floor(_code.charAt(i));
	}
	c = (220 - c) % 10;
	return c == _code.charAt(12);
};

var txtKeyword_OnKeyDown = function(e){
	var _intKeyCode;
	if (typeof(event) == "undefined") {
		_intKeyCode = e.which;
	} else {
		_intKeyCode = event.keyCode;
	}
	if (_intKeyCode == 13) {
		$("HFieldMode").value = "1";
	} else {
		$("HFieldMode").value = "0";
	}
};

var DDListNP_OnChange = function(e){
	var _objText = $("txtKeyword");
	var _objList = $("DDListNP");
	var _strValue;
	if (_objText && _objList) {
		_strValue = _objList.options[_objList.selectedIndex].value;
		if (_strValue != "") {
			_strValue = parseInt(_strValue, 10).toString();
		}
		_objText.value = _strValue;
	}
};

var Window_OnResize = function(e){
	var _intTop = 0, _intLeft = 0;
	var _objMask = $("maskDiv");
	var _objMask2 = $("maskDiv2");
	var _objChangePwd = $("loginPageDiv");
	if (_objMask && _objChangePwd && _objMask2) {
		_objMask.style.height = _objMask2.offsetHeight + "px";
		_objMask.style.width = "100%";
		_objMask2.style.width = "100%";
		_objMask.style.left = "0px";
		_objMask.style.top = "0px";
		_intTop = Math.floor((_objMask2.offsetHeight - _objChangePwd.offsetHeight) / 2 + document.body.scrollTop);
		_intLeft = Math.floor((_objMask2.offsetWidth - _objChangePwd.offsetWidth) / 2 + document.body.scrollLeft);
		_objChangePwd.style.top = _intTop + "px";
		_objChangePwd.style.left = _intLeft + "px";
	}
};

var Window_OnScroll = function(e){
	var _intTop = 0, _intLeft = 0;
	var _objMask = $("maskDiv");
	var _objMask2 = $("maskDiv2");
	var _objChangePwd = $("loginPageDiv");
	if (_objMask && _objChangePwd && _objMask2) {
		_objMask.style.height = document.body.scrollHeight + "px";
		_objMask.style.width = document.body.scrollWidth + "px";
		_objMask2.style.width = "100%";
		_objMask.style.left = "0px";
		_objMask.style.top = "0px";
		_intTop = Math.floor((_objMask2.offsetHeight - _objChangePwd.offsetHeight) / 2 + document.body.scrollTop);
		_intLeft = Math.floor((_objMask2.offsetWidth - _objChangePwd.offsetWidth) / 2 + document.body.scrollLeft);
		_objChangePwd.style.top = _intTop + "px";
		_objChangePwd.style.left = _intLeft + "px";
	}
};

var OnSuccess2 = function(_strResult, _objContent){
	switch (_objContent) {
		case "BookCalendarData":
			if (_strResult != "") {
				eval("objBookListData=" + _strResult);
			} else {
				objBookListData.arrBkSid = null;
				objBookListData.arrDay = null;
				objBookListData.arrPhase = null;
				objBookListData.intCount = 0;
			}
			objBookCalendar.create(objBookListData, booBookCalendarHasClose);
			break;
		case "BookPeriodicalData":
			if(_strResult != "") {				
				eval("objPeriodicalData="+_strResult);
			} else {
				objPeriodicalData = null;				
			}
			objPeriodical.create(objPeriodicalData, booBookCalendarHasClose, "");
			break;
	}
	objAnnoViewer.booIsReady = true;
};
var OnError = function(_objResult){
alert("sorry, a serious error occurred! Error message is : " + _objResult.get_message() +
	"\nStack trace result is : " +
	_objResult.get_stackTrace() +
	"\nStatus Code is : " +
	_objResult.get_statusCode() +
	"\nException pattern is : " +
	_objResult.get_exceptionType() +
	"\nTime out status : " +
	_objResult.get_timedOut() +
	"\n\nPlease try later.");
	objAnnoViewer.booIsReady = true;
};

