﻿//==========================Common End====================
//放在此文件最前面
try{
OspodLanguage.languageCheck();
}catch(err){};

function doZoom(num){
	var obj = document.getElementById("zoom");
	obj.style.fontSize=num + "px";
	obj.style.lineHeight=num*2 + "px";
}

function closeMe(){
	window.close(); 
}

/**
* 获得浏览器类型
*/
function Browser() {

  var ua, s, i;
  this.isIE    = false;  // Internet Explorer
  this.isIE7 = false;
  this.isOP    = false;  // Opera
  this.isNS    = false;  // Netscape
  this.version = null;
  ua = navigator.userAgent;

  s = "Opera";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isOP = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }

  s = "MSIE";
  if ((i = ua.indexOf(s))) {
    this.version = parseFloat(ua.substr(i + s.length));
    if(this.version == 7){
      this.isIE7 = true;
    }
    this.isIE = true;

    return;
  }

}

var browser = new Browser();

function doPagePrint(obj) {
	window.print();
}

function $N(name){
	return document.getElementsByName(name);
}


function getWindowEvent(){
	if(browser.isIE){
		return window.event;
	}else{
		var o = arguments.callee.caller; 
		var e; 
		while(o != null){ 
			e = o.arguments[0]; 
			if(e && (e.constructor == Event || e.constructor == MouseEvent)) {return e;} 
				o = o.caller; 
			} 
		return null;
	}
}

function getEventSrcElement(){
	var event = getWindowEvent();
	if(event != null){
		var e = event.target || event.srcElement;
		return e;
	}
	return null;
}

function getRequestCookie(name){
   return getCookie(name);
}

function setCookie(name,value,duration)//两个参数，一个是cookie的名子，一个是值,一个是期限,单位为天，默认30天{
    var Days = duration ? duration : 30; //此 cookie 将被保存 30 天    var exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
function getCookie(name)//取cookies函数        
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;

}
function delCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

var __keyDown = false;
function keyLimit(event){
	if(!event){
		event = getWindowEvent();
	}
	var keyCode = event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode);
	if((keyCode==116)||                                   //F5   
              (event.ctrlKey   &&   keyCode==82)){   //Ctrl+R 
		var l=getCookie("_cmsf5");
		if(cookieEnable() && !l && !__keyDown && !_forbidF5){
			__keyDown = true;
		    //setCookie("_cmsf5","anything",3);
			var exp  = new Date();
			exp.setTime(exp.getTime() + 3 * 1000);
			document.cookie = "_cmsf5=anything;expires=" + exp.toGMTString();
		}else{
			//forbid
			__keyDown = true;
			if(document.addEventListener){
				event.preventDefault();
			}else{
				event.keyCode=0; 
			}
		    event.returnValue=false;
		}  
	} 
}   

function keyRelease(event){
  __keyDown = false;
}

function cookieEnable() 
{ 
	var result=false; 
	if(navigator.cookiesEnabled) 
	  return true; 
	var cookieStr = "testcookie=yes";
	document.cookie = cookieStr; 
	var cookieSet = document.cookie; 
	if (cookieSet.indexOf(cookieStr) > -1) 
	  result=true; 
	//document.cookie = ""; 
	var date = new Date();   
	date.setTime(date.getTime() - 1000);   
	document.cookie = cookieStr + "; expires=" + date.toGMTString();
	return result; 
} 
/*
function forbidF5(){
	if(window.addEventListener){ 
	   document.addEventListener("keydown",keyLimit,false);
	   document.addEventListener("keyup",keyRelease,false);
	}else{
	   document.attachEvent("onkeydown",keyLimit);
	   document.attachEvent("onkeyup",keyRelease);
	}
}

forbidF5();
*/
if   (window.event)     
      document.captureEvents(Event.MOUSEUP);     

function forbidContextMenu(event){

	if(document.addEventListener){
		if(event.button   ==   2   ||   event.button   ==   3){
			event.preventDefault();
			event.returnValue   =   false;   
		}
	}else{
		if(event.button   ==   2   ||   event.button   ==   3){
			event.keyCode=0;
			event.button = 0;
			event.cancelBubble   =   true  
			event.returnValue   =   false; 
		}
	}
}

function forbidCopy(event){
	if(document.addEventListener){
			event.preventDefault();
			event.returnValue   =   false;  
	}else{
			event.keyCode=0;
			event.cancelBubble   =   true  
			event.returnValue   =   false; 
	}
	return false;
}

//var _forbidContextMenu = true;
function forbid(){
	if(window.addEventListener){
	   document.addEventListener("keydown",keyLimit,false);
	   document.addEventListener("keyup",keyRelease,false);
	   if(_forbidContextMenu){
		  //document.addEventListener("contextmenu",function(){return false;},false);
	      document.addEventListener("contextmenu",forbidContextMenu,false);
	   }
	   if(_forbidCopy){
		   //-moz-user-select='none' need to be set for forbid firefox selection.
		  document.addEventListener("selectstart",forbidCopy,false);
		  document.addEventListener("copy",forbidCopy,false);
	   }
	}else{
	   document.attachEvent("onkeydown",keyLimit);
	   document.attachEvent("onkeyup",keyRelease);
	   if(_forbidContextMenu){
		  document.attachEvent("oncontextmenu",function(){return false;});
	   }
	   if(_forbidCopy){
		  document.attachEvent("onselectstart",function(){return false;});
		  document.attachEvent("oncopy",forbidCopy);
	   }
	}
}

try{
forbid();
}catch(e){}
//==========================Common End====================

//==============评论JS开始======================
function doCommentSubmit(id) {
	var content = $N("comment.content")[0].value;
	if( content == null || content == "") {
		alert(I18n.commentContentEmpty);
		return;
	}
	var title = $N("comment.title")[0];
	if( title.value == null  || title.value == "" ) {
		title.value = I18n.commentDefaultTitle;
	}
	var url = _contextPath + "/cfn?cmsfn=0101&ID=" + id;
	new Ajax.Request(url, {
		method: 'post',
		parameters: $('commentForm').serialize(false),
		onSuccess: function(transport) {
			var xmldoc = transport.responseXML;
			var root = xmldoc.getElementsByTagName('root')[0];
			var root_node = root.getElementsByTagName('status')[0];
			var status = root_node.firstChild.data;	
			var msg_node = root.getElementsByTagName('message')[0];
			var msg = msg_node.firstChild.data;	

			if(status == "0"){
				if( msg=="NULL" ) 
						msg=I18n.commentSuccess;
				else 
						msg=I18n.commentSuccess + "<br><br>" +msg; 
					_error_msg_show(msg,"window.location=window.location",5,"");
			} else {
				if( msg=="NULL" ) 
					msg=I18n.commentFailure ;
				else
					msg=I18n.commentFailure + "<br><br>" + msg;
				_error_msg_show(msg,"",3,"");   
			}
		}
	});
 
}


function doSupport(id){
	doCommentValueSubmit("0102" , id);
}

function doDisagree(id) {
	doCommentValueSubmit("0103" , id);
}

function doNoCare(id) {
	doCommentValueSubmit("0104" , id);
}

function doCommentValueSubmit(fn, id){
	var url = _contextPath + "/cfn?cmsfn="+fn +"&umID="+id ;
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport) {
			var xmldoc = transport.responseXML;
			var root = xmldoc.getElementsByTagName('root')[0];
			var root_node = root.getElementsByTagName('status')[0];
			var status = root_node.firstChild.data;	
			if(status == "0"){
				_error_msg_show(I18n.commentValueSuccess,"windowReload()",5,"");
			} else {
				_error_msg_show(I18n.commentValueFailure,"",3,"");
			}
		}
	 });
}


function windowReload() {
	  window.location =  window.location;
}

//==============全文检索JS开始 ======================
//全文检索 
function doSearchSubmit(id){
	if(!id){
		id = "searchForm";
	}
	var form = document.forms[id];
	//var keyword = $N("search.keyword")[0].value;
	var keyword = $(form)["search.keyword"].value;
	if( keyword == null || keyword.replace(/(^\s*)|(\s*$)/g, "")==""){
		alert(I18n.searchKeywordEmpty);
		return;
	}
	form.action = _contextPath + "/cc?url=_search";
	form.method = "post";
	
	var e = getEventSrcElement();
	if(e.tagName.toLowerCase() == "input" && (e.type=="image" || e.type=="submit")){
		//do nothing
	}else{
		form.submit();
	}
}

function doAdvancedSearchSubmit(id){
	if(!id){
		id = "searchForm";
	}
	var form = document.forms[id];
	//var keyword = $N("search.keyword")[0].value;
	var keyword = $(form)["search.keyword"].value;
	if( keyword == null || keyword.replace(/(^\s*)|(\s*$)/g, "")==""){
		alert(I18n.searchKeywordEmpty);
		return;
	}

	form.action = _contextPath + "/cc?url=_advSearch";
	form.method = "post";
	
	var e = getEventSrcElement();
	if(e.tagName.toLowerCase() == "input" && (e.type=="image" || e.type=="submit")){
		//do nothing
	}else{
		form.submit();
	}
}

//==============留言板JS开始 ======================
function doBoardSubmit(id) {
	if( isNullValue( "board.content" , I18n.boardContentEmpty )) return ; 
	if( isNullValue( "board.title" , I18n.boardTitleEmpty)) return ; 

	var formValueStr ="boardID=" + id;
	var url =_contextPath + "/cfn?cmsfn=0201&" + formValueStr;
	new Ajax.Request(url,{
		method: 'post',
		parameters: $($N('commentForm')[0]).serialize(false),
		onSuccess: function(transport){			
			var xmldoc = transport.responseXML;
			var root = xmldoc.getElementsByTagName('root')[0];
			var status_node = root.getElementsByTagName('status')[0];
			var status = status_node.firstChild.data;
			var msg_node = root.getElementsByTagName('message')[0];
			var msg = msg_node.firstChild.data;	

			if(status == "0"){
				if( msg=="NULL" )
					   msg=I18n.boardCommitSuccess;
				else 
					msg=I18n.boardCommitSuccess + "<br><br>" + msg;
					_error_msg_show(msg,"window.location=window.location",5,"");
			} else {
				if( msg=="NULL" ) 
					msg=I18n.boardCommitFailure ;
				else
					msg=I18n.boardCommitFailure + "<br><br>" + msg;
					_error_msg_show(msg,"",3,"");
				}
			}
	});
   
}

//==============公用JS开始 ======================

function getHttpRequestObject() {
	
	var http_request = false ; 

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		  if (http_request.overrideMimeType) {
			   http_request.overrideMimeType('text/xml');
      }
   } else if (window.ActiveXObject) { // IE
     try {
         http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
         try {
             http_request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (e) {}
     }
   }
	 if (!http_request) {
      alert(I18n.unsupportedHttpProtocol);
      return false;
   }
	return http_request; 
}

function getAjaxSubmitStr(name , firstFlag ) {
	if($N(name).length==0){
		return "";
	}
	var value = $N(name)[0].value;
	if( !value  || value ==null || value == "" ) {
		return "";	
	}
	if(firstFlag )  
		return name + "=" + value;
	else 
		return "&"+name + "=" + value;
}

function isNullValue(name , chineseName) {
	if($N(name).length==0){
		_error_msg_show( chineseName + I18n.nullValueTipSuffix,"",3,"");
		return true;
	}
	var value = $N(name)[0].value;
	if ( !value || value==null || value=="")  	{
		_error_msg_show( chineseName + I18n.nullValueTipSuffix,"",3,"");
		return true;
	}
	return false;
}

function openWindow( link , width , height , windowParam){
	var widthvalue = (window.screen.width-width)/2;
	var heightvalue = (window.screen.height-height)/2;
	var PopWindow;
	PopWindow = window.open(link,"","width="+ width + ",height=" + height+ "," + windowParam   +",left="+widthvalue+",top="+heightvalue,true);
	PopWindow.focus();
}

