/**
 * AJAX操作方法集合
 *
 * @author: gao shangyong <shangyong@staff.sina.com.cn>
 * @version: 1.0
 *
 *  Example:
 *  
 *  ajax = new class_ajax();
 *	ajax.init();
 *	ajax.set_method('GET');
 *	ajax.set_event(func);   //参数不能加引号
 *	ajax.set_url(url);
 *	ajax.add_param("classId", itemId);
 *	ajax.add_param("layer", layer);
 *	ajax.submit();
 *
 *  var text = ajax.get_content();
 */
function class_ajax()
{
	this.ID = "ajax";
	/**
	 * xml请求对象
	 */
	this.xmlHttp = null;
	/*
	 * 请求的目标url
	 */
	this.url = "";
	/**
	 * http请求的方法 GET or POST
	 */
	this.method = "";
	/**
	 * 状态改变的事件触发函数
	 */
	this.event = "";
	/**
	 * 是否为异步连接
	 */
	this.async = true;
	/**
	 * REQUEST参数
	 */
	this.params = new Array;
	
	
	/**
	 * 初始化xmlHttpRequest对象
	 *
	 */
	this.init = function()
	{
		if(this.xmlHttp == null)
		{
			if (window.XMLHttpRequest)
		    {
		        this.xmlHttp = new XMLHttpRequest();
		    }
		     else if (window.ActiveXObject)
		     {
		        this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		        if(!this.xmlHttp)
		        {
		        	this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		        }
		    }
		}
		if(this.xmlHttp == null)
		{
			alert("invalid xmlHttpRequest!");
			return false;
		}
	}//end function
	
	/**
	 * 设置目标请求方法
	 */
	this.set_method = function(method)
	{
		if(method != 'POST' && method != 'GET')
		{ 
			alert("invalid method!");
			return false;
		}
		this.method = method;
	}//end function
	
	/**
	 * 获取目标请求方法
	 */
	this.get_method = function()
	{
		return this.method;
	}//end function
	
	/**
	 * 设置状态该表要触发的事件
	 */
	this.set_event = function(event)
	{
		this.event = event;
	}//end function
	
	/**
	 * 获取 event
	 */
	this.get_event = function()
	{
		return this.event;
	}//end function
	
	/**
	 * 设置要请求的目标url
	 */
	this.set_url = function(url)
	{
		this.url = url;
	}//end function
	
	/**
	 * 获取 url
	 */
	this.get_url = function()
	{
		return this.url;
	}//end function
	
	/**
	 * 添加REQUEST 参数
	 */
	this.add_param = function(key, val)
	{
		var tmpCount = this.params.length;
		this.params[tmpCount] = key + "=" + val; 
 	}//end function
 	
 	/**
 	 * 设置请求为异步连接
 	 */
 	this.set_async = function()
 	{	
 		this.async = true;
 	}//end function
 	
 	/**
 	 * 取消异步连接，即设置为同步连接
 	 */
 	this.set_sync = function()
 	{
 		this.async = false;
 	}//end function
 	
 	/**
	 * 发送请求
	 */
	this.submit = function()
	{
		this.xmlHttp.onreadystatechange = this.event;
     	var send_params = null;
		if(this.params.length > 0)
		{
			send_params = this.params.join('&');
			if(this.method == 'POST')
			{
				this.xmlHttp.open(this.method, this.url, this.async);
				//this.xmlHttp.setRequestHeader("Method", this.method + this.url + " HTTP/1.1");
				this.xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				this.xmlHttp.send(send_params);
			}else
			{
				this.xmlHttp.open(this.method, this.url + "?" + send_params, this.async);
				this.xmlHttp.send(null);
			}
		}else
		{
		  this.xmlHttp.open(this.method, this.url, this.async);
		  this.xmlHttp.send(send_params);
		}
		
	}//end function
	
	/**
	 * return Data
	 * @param string type : "xml" or "text"
	 */
	this.get_content = function(type)
	{
		if(type == 'xml')
		{
			return this.xmlHttp.responseXML.documentElement;
		}else
		{
			return this.xmlHttp.responseText;
		}
	}
	
	/**
	 * 检测事件是否完成
	 */
	this.complete = function()
	{
		if(this.xmlHttp.readyState == 4)
		{
			return true;
		}
		return false;
	}//end function
	
	/**
	 * 检测是否处在数据读取状态
	 */
	this.isSending = function()
	{
		if(this.xmlHttp.readyState == 1)
		{
			return true;
		}
		return false;
	}//end function
	
	/*
	 * 检测请求是否成功
	 */
	this.success = function()
	{
		if (this.complete() && this.xmlHttp.status == 200) {
 			return true;       	
        }
        return false;
	}//end function
	
	/**
	 * 获取消息提示
	 */
	this.get_statusText = function()
	{
		return this.xmlHttp.statusText;
	}//end function
}//end class



/**
 * File:        global_function.js
 *
 * JS 函数集
 *
 * @copyright   SINA.COM
 * @author      shangyong Gao <shangyong@staff.sina.com.cn>
 * @package     基础类库
 * @version     1.0 
 */

/**
 * 获取元素句柄
 * @param objName:	元素名称
 * @return	resource
 */
function GetObj(objName)
{
  var v = document.getElementById(objName);
  if(v == null)
  {
    var e1 = document.getElementsByName(objName);
    v = e1[0];
    if(v == null)
    {
      eval("v = document.all." + objName);
    }
  }
  if(v == null)
  {
    alert( 'cannot find ' + objName + ' ! ' );
  }
  return eval(v);
}

/**
 * 去除字符串前后的空格
 * 使用方法:   str.trim();
 * @return string
 */
String.prototype.trim = function()
{
// 用正则表达式将前后空格
// 用空字符串替代。
	return this.replace(/(^\s*)|(\s*$)|　/g, "");
}

/**
 * 精确统计字符串字节数
 *
 * return	integer
 */
String.prototype.ByteCount = function(trim)
{
	txt = this.replace(/(<.*?>)/ig,'');
	if(trim == true)txt = this.replace(/[\s\t\r\n]/g, '');
	txt = txt.replace(/([\u0391-\uFFE5])/ig, '11');
	var count = txt.length;
	return count;
}

/**
 * 精确统计字符串字节数
 *
 * return	integer
 */
String.prototype.ByteCountPro = function()
{
	txt = this.replace(/[\s\r\n　]/ig,'');
	var count = txt.length;
	return count;
}

/**
 * 检测字符串是否满足EMAIL地址格式
 *
 * @param str:	string
 *
 * @return	bool
 */
function is_email(str)
{
	var reg_email = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	if(!reg_email.test(str.trim()))
	{
		return false;
	}
	return true;
}

/**
 * 检测字符串是否为手机号码格式
 *
 * @param str:	string
 *
 * @return	bool
 */
 function is_mobile(str)
 {
	var reg_mobile = /^1[0-9]\d{9}$/gi;
	if (!reg_mobile.test(str.trim()))
	{
		return false;
	}
	return true;
 }


function check_length(item, name, min, max)
{
	var l = item.value.trim().ByteCount();
	if (min !='' && min >0 && l < min)
	{
		alert(name + "的长度不能少于" + min + "个字符");
		item.focus();
		return false;
	}
	if (max !='' && max >0 && l > max)
	{
		alert(name + "的长度不能多于" + max + "个字符");
		item.focus();
		return false;
	}
	return true;
}

/**
 * 检测字符串是否是数字字符串
 *
 * @param str:	string
 *
 * @return	bool
 */
function is_plus(str)
{
	var reg = /^\d+$/;
	return reg.test(str.trim());
}

/**
 * 检测字符串是否是数字
 *
 * @param str:	string
 *
 * @return	bool
 */
function is_number(str, plus)
{
	if(plus)
	{
			var reg = /^\d+(.\d+)?$/;
	}
	else
	{
			var reg = /^-?\d+(.\d+)?$/;
	}
	return reg.test(str);
}

/**
 * 检测字符串是否是整数字符串
 *
 * @param str:	string
 *
 * @return	bool
 */
function is_int(str)
{
	var reg = /^[1-9]\d*$/;
	return reg.test(str);
}

/**
 * 检测字符串是否为汉字或字符形式
 *
 * @param str:	string
 *
 * @return	bool
 */
function is_hz_char(str, space)
{
	var reg_char = /^[A-Za-z0-9_\-\|\u4e00-\u9fa5]+$/;
	if(space == true)reg_char = /^[A-Za-z0-9_\s\-\|\u4e00-\u9fa5]+$/;
	//var reg_char = /(\W|[^\u4e00-\u9fa5])+/g;
	if(!reg_char.test(str))
	{
		return false;
	}
	return true;
}

/**
 * 检测字符串是否含有重复长度的字符
 *
 * @param str:	string
 * @param max_length:	int
 *
 * @return	bool
 */
function valid_repeat_chars(str, max_length)
{
	var reg = new RegExp("\\w{" + max_length + ",}");
	if(reg.exec(str) == null)
	{
		return true;
	}
	return false;
}

/**
 * 检测字符串是否有效
 *
 * @return	bool
 */
String.prototype.avail = function()
{
	str = this.trim();
	if(str == '' || str == 0 || str == NULL)
	{
		return false;
	}
	return true;
}

/**
 * 隐藏/显示某个元素
 * @param itemId: 元素name或者ID
 */
function switch_display(itemId, imgid, open_icon, close_icon)
{
	if(GetObj(itemId).style.display == 'none')
	{
		GetObj(itemId).style.display = "";
		if(imgid != '' && imgid != null)
		{
			document.images[imgid].src = open_icon;
		}
	}else
	{
		GetObj(itemId).style.display = "none";
		if(imgid != '' && imgid != null)
		{
			document.images[imgid].src = close_icon;
		}
	}
}//end function

/**
 * 精确截取字符串
 * @param start:	开始位置
 * @param offset:	截取的长度
 */
String.prototype.substr = function(start, offset)
{
		//===add by jinhong to fix the bug of "the function return undefined when this.length-start<offset", notice:please don't cover the native function
		if (start >= this.length)
		{
			return '';	
		}
		
		var scurlen = this.length-start;
		offset = (scurlen < offset) ? scurlen : offset;
		//===end add
		
		var r = '';
		for(i = start; i < this.length; i++)
		{
			if((offset - r.ByteCount() < 2) && this.substring(i, i+1) > '\u0391' && this.substring(i, i+1) < '\uFFE5')
			{
					return r;
			}
			r = r + this.substring(i, i+1);
			if(r.ByteCount() >= offset)
			{
				return r;
			}
		}
}//end function

String.prototype.cn_substr = function(start, len, replace)
{
	var s = this.replace(/([\u4e00-\u9fa5])/g,"\xff\$1");
	if(s.length > len)
	{
		if(s.length == this.length)return this.substring(start, len);
		return (s.substring(start, len).replace(/\xff/g, '') + replace);
	}
	return this;
	//return s.substring(start, len).replace(/\xff/g, '');
}

/**
 * @param item object
 * 
 * 统计复选框被选择的个数
 */
function CheckedCount(item)
{
	var sum = 0;
	for(var i =0; i < item.length; i++)
	{
		if(item[i].checked)
		{
			sum++;
		}
	}
	return sum;
}//end function

/**
 * @param item object
 * 
 * 检查复选框是否被选中
 */
function IsChecked(item)
{
	for(var i =0; i < item.length; i++)
	{
		if(item[i].checked)
		{
			return true;
		}
	}
	return false;
}//end function

function setCheckedAll(name, checked)
{
	var obj = document.getElementsByName(name);
	for(i = 0; i < obj.length; i++)
	{
		obj[i].checked = checked;
	}
}

/**
 * 返回单选按钮的值
 *
 * @param object	object	元素名称
 *
 * @return	none
 */
function get_sel_value(object)
{
	object = document.getElementsByName(object);
	for(i = 0; i < object.length; i++)
	{
		if(object[i].checked)
		{
			return object[i].value;
		}
	}
	return null;
}

//输入框字数统计
//field,输入框对象， showtext,显示框对象, maxlimit,最大字数限制
function textCounter(field, numtext, maxlimit) {
	if (field.value.length > maxlimit) 
		field.value = field.value.substring(0, maxlimit); 
	
		numtext.innerHTML = maxlimit - field.value.length;
}

function countText(field, numtext, maxlimit) {
	txt = field.value;
	//txt = txt.replace(/(<.*?>)/ig,'');
	//txt = txt.replace(/[\s\t\r\n]/g, '');
	txt = txt.replace(/<IMG.+?>/ig, '');
	var len = txt.ByteCountPro();
	/*if (len > maxlimit)
	{
		field.value = field.value.cn_substr(0, maxlimit); 
	}*/
	//document.getElementById(numtext).innerHTML = Math.ceil(len / 2);
	document.getElementById(numtext).innerHTML = len;
}

//过滤特殊字符，标题，名称用
function check_special_chr(val)
{
	if((/>|<|,|\[|\]|\{|\}|\?|\/|\+|=|\||\'|\\|\"|:|;|\~|\!|\@|\#|\*|\$|\%|\^|\&|\(|\)|`/i).test(val))
	{
		return false
	}
	return true;
}

/**
 * 从页面地址获取参数的值
 *
 * @param name	string	参数名称
 *
 * @return	string
 */
function getUrlParam(name)
{
	var Param = document.URL.split("?");
	if(Param.length < 2) return null;
	Param = Param[1].split("&");
	for(var i = 0; i < Param.length; i++)
	{
		var tmp = Param[i].split("=");
		tmp[1] = tmp[1].trim();
		if(tmp[0] == name && tmp[1] != "" && tmp[1].search(/#/) == -1)
		{
			return tmp[1];
		}
	}
	return null;
}


//显示默认图片
function show_img(obj, mod)

{

	switch(mod)
	{
		case 'userphoto':	//用户头像
			obj.src="http://www.sinaimg.cn/book/vipbook/none-80.gif";
			break;
		case 'big_userphoto':	//用户头像
			obj.src="http://www.sinaimg.cn/book/vipbook/none-130.gif";
			break;
		case 'new_userphoto':	//用户头像
			obj.src="http://www.sinaimg.cn/book/vipbook/pic1.jpg";
			break;
		case 'bookcover':	//作品封面
			obj.src="http://www.sinaimg.cn/book/vipbook/nonecover-240.gif";
			break;
		case 'medium_bookcover':	//作品封面
			obj.src="http://www.sinaimg.cn/book/vipbook/nonecover-119.gif";
			break;
		case 'small_bookcover':
			obj.src="http://www.sinaimg.cn/book/vipbook/nonecover-88.gif";
			break;
	}
	

}

function check_search_form(tform)
{
	
	k = tform.k.value.trim();
	k = k.replace(/(<.*?>)/ig,'');
	k = k.replace(/\r|\t|\n| |　/,'');
	if(k == "" ||k =="请输入查询条件")
	{
		alert("请输入搜索关键字");
		tform.k.focus();
		return false;
	}
	return true;
}


function global_survey_Opener(survey_id, obj)

{
	if(obj != "view")
	{
		newWindow = window.open("","survey_server","toolbar,resizable,scrollbars,dependent,width=500,height=420,left=150,top=80");
		newWindow.focus();
		obj.submit();
	}else{
		
		newWindow = window.open ("/survey/view.php?survey_id=" + survey_id,"survey_server","toolbar,resizable,scrollbars,dependent,width=500,height=420,left=150,top=80");
		//newWindow.focus();
	}
	return false;
}

function comment_response()
{
	if(ajax.complete())
	{
		if(ajax.success())
		{
			response  = ajax.get_content('xml');
			r = response.getElementsByTagName('return')[0].firstChild.data;
			res = response.getElementsByTagName('res')[0].firstChild.data;
			if (r == "ok")
			{
				var _v = document.getElementById("vcode").value;
				if(_v == "0000")
				{
					window.open("http://comment4.news.sina.com.cn/comment/skin/default.html?channel=dushu&amp;newsid=" + cmnt_newsid, "评论成功");
				}
				else
				{
					window.open("/postbar.html?book=" + thebook_id + "&_v=" + _v, "发帖成功");
				}
				;//alert("发表成功!");
			}else
			{
				res = res.split("|");
				if(res[0] == "009")
				{
					alert(res[1]);
				}
				;//alert(res);
			}
		}
	}
}
function send_comment(tform)
{

	channel = tform.channel.value;
	newsid = tform.newsid.value;
	title = tform.title.value;
	style = tform.style.value;
	face = tform.face.value;
	user = tform.user.value.trim();
	url = location.href;
	vcode = tform.vcode.value;
	if (user == "会员名/手机/UC号")
	{
		user = "";
	}
	password = tform.password.value.trim();
	cmsg_content = tform.cmsg_content.value.trim();
	anonymous_value = 0;
	if(!tform.anonymous.checked)
	{
		if(user== "" || password== "")
		{
			alert( "请核选“匿名”或者填写“登录名”及“密码”" );
			return false;
		}
	}else
	{
		anonymous_value = 1;
	}
	
	//送新浪贴吧校验
	if(!VipBookBook.sendBar(tform))
	{
		return false;
	}
	
	if(vcode.trim() == "")
	{
		alert("请输入验证码！");
		tform.vcode.focus();
		return false;
	}
	if (cmsg_content == "" ) {
		alert( "请输入您的留言" );
		tform.cmsg_content.focus();
		return false;
	}
	
	//return false;
	
	ajax = new class_ajax();
	ajax.init();
	ajax.set_method('POST');
	ajax.set_event(comment_response);   //参数不能加引号
	ajax.set_url("/iframe/comment_server.php");
	ajax.add_param("channel", channel);
	ajax.add_param("newsid", newsid);
	ajax.add_param("title", title);
	ajax.add_param("style", style);
	ajax.add_param("face", face);
	ajax.add_param("user", user);
	ajax.add_param("password", password);
	ajax.add_param("anonymous", anonymous_value);
	ajax.add_param("cmsg_content", cmsg_content);
	ajax.add_param("url", url);
	ajax.add_param("vcode", vcode);
	ajax.add_param("book", tform.book.value);
	ajax.add_param("ccode", tform.ccode.value);
	ajax.submit();
	
	tform.submit_btn.disabled = true;
	//tform.cmsg_content.value = "";
	
	//alert("评论发表成功!");
	//window.open("/postbar.html?book=" + tform.book.value, "发帖成功");
	
	return false;
}
function copyurl(url)
{
	try{
		clipboardData.setData('Text',url);
		alert('复制成功，请粘贴到你的UC/QQ/MSN上推荐给你的好友')
	}
	catch(e){}
}
function global_ajax_response()
{
	if(ajax.complete())
	{
		if(ajax.success())
		{
			response  = ajax.get_content('xml');
			r = response.getElementsByTagName('return')[0].firstChild.data;
			res = response.getElementsByTagName('res')[0].firstChild.data;
			if (r == "ok")
			{
				
			}else
			{
				
			}
		}
	}
}
function book_share()
{
	s = new String(window.location);
	reg = /#/g;
	l = s.replace(reg, "");
	t = new Date().getTime();
	k1 = RandomStringUtils.randomAlphanumeric(16);
	k2 = MD5(k1 + t);
	reg1 = /html$/i;
	r = l.search(reg1);
	if(r == -1)
	{
		url = l + "&k=" + k2;
	}else
	{
		url = l + "?k=" + k2;
	}
	
	copyurl(url);
	ajax = new class_ajax();
	ajax.init();
	ajax.set_method('GET');
	ajax.set_event(global_ajax_response);   //参数不能加引号
	ajax.set_url("/iframe/share_server.php");
	ajax.add_param("url", l);
	ajax.add_param("k", k2);
	ajax.add_param("act", "add");
	ajax.submit();
	return false;
}

function get_request_value(Name)
{
	var search = Name+"=";
	if (location.href.length > 0)
	{
		offset = location.href.indexOf(search);
		if (offset != -1)
		{
			offset += search.length;
			end = location.href.indexOf("&", offset)
			if (end == -1)
			{
				end = location.href.length;
			}
			var returnvalue=unescape(location.href.substring(offset, end))
		}
	}
	return returnvalue;
}

function book_share_check()
{
	s = new String(window.location);
	reg1 = /\d+.html/i;
	r = s.search(reg1);
	if(r == -1)
	{
	
		k = get_request_value('&k');
	}else
	{
		k = get_request_value('?k');
	}
	if(typeof(k) == 'undefined' || k == '')
	{
		return false
	}
	if (k.length != 32)
	{
		return false;
	}
	ajax = new class_ajax();
	ajax.init();
	ajax.set_method('GET');
	ajax.set_event(global_ajax_response);   //参数不能加引号
	ajax.set_url("/iframe/share_server.php");
	ajax.add_param("url", s);
	ajax.add_param("act", "check");
	ajax.submit();
	return false;
}
function vote_response()
{
	if(ajax.complete())
	{
		if(ajax.success())
		{
			response  = ajax.get_content('xml');
			r = response.getElementsByTagName('return')[0].firstChild.data;
			res = response.getElementsByTagName('res')[0].firstChild.data;
			if (r == "ok")
			{
				alert("投票成功!");
			}else
			{
				alert(res);
			}
		}
	}
}

function book_vote(book_id)
{
	ajax = new class_ajax();
	ajax.init();
	ajax.set_method('GET');
	ajax.set_event(vote_response);   //参数不能加引号
	ajax.set_url("/iframe/vote_server.php");
	ajax.add_param("book_id", book_id);
	ajax.add_param("act", "vote");
	ajax.submit();
	return false;
}

	function chg_img()
	{
		GetObj('authimg').src="/iframe/authimg.php?t=" + new Date().getTime();
		GetObj('checkwd').value = "";
		GetObj('checkwd').focus();	
	}


	function callFlash(){
	  GetObj('authimg').src = "http://www.sinaimg.cn/dy/survey/audio.jpg";
	  window.document.mp3_player.SetVariable("isPlay", "1");
	  GetObj('checkwd').value = "";
	  GetObj('checkwd').focus();
	}

	function set_submit_btn()
{
	var btn = document.forms["post_form"].submit_btn;
	if(btn.disabled)
	{
		btn.disabled = !btn.disabled;
	}
}