String.prototype.trim=function(){
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.ltrim=function(){
	return this.replace(/(^\s*)/g,"");
}
String.prototype.rtrim=function(){
	return this.replace(/(\s*$)/g,"");
}

String.prototype.strlen=function(){
	return this.replace(/[^\x00-\xff]/g,"**").length;
}

Array.prototype.inArray=function(val){
	var is_exist = false;
	for(var k in this){
		if(val == this[k]){
			is_exist = true;
			break;
		}
	}
	return is_exist;
}

Array.prototype.hasRepeatElement = function(){
	var len = this.length;
	var t = '';
	for(var k1 in this){
		for(var k2 in this){
			if(k1!=k2 && this[k1]==this[k2]){
				return true;
			}
		}
	}
	return false;
}

Array.prototype.hasRepeatElementLowerCase = function(){
	var len = this.length;
	var t = '';
	var str1 = '';
	var str2 = '';
	for(var k1 in this){
		for(var k2 in this){
			str1 = this[k1];
			if(typeof str1 != 'string'){
				continue;
			}
			str2 = this[k2];
			if(typeof str2 != 'string'){
				continue;
			}
			if( k1!=k2 && str1.toLowerCase()==str2.toLowerCase() ){
				return true;
			}
		}
	}
	return false;
}

function $obj(oid){
	return document.getElementById(oid);
}


function is_enter_down(e){
	var event=document.all?window.event:arguments[0];
	//alert(event.keyCode);
	if(event.keyCode==13){
		return true;
	}
	return false;
}

function refresh_page(){
	location.href = location.href;
}

//获取元素的纵坐标
function getTop(e){
	var offset=e.offsetTop;
	if(e.offsetParent!=null){ offset+=getTop(e.offsetParent);}
	return offset;
}
//获取元素的横坐标
function getLeft(e){
	var offset=e.offsetLeft;
	if(e.offsetParent!=null){ offset+=getLeft(e.offsetParent);}
	return offset;
}

//获取滚动条的位置
function getScroll(){
	var t, l, w, h;
	if (document.documentElement && document.documentElement.scrollTop) {
		t = document.documentElement.scrollTop;
		l = document.documentElement.scrollLeft;
		w = document.documentElement.scrollWidth;
		h = document.documentElement.scrollHeight;
	} else if (document.body) {
		t = document.body.scrollTop;
		l = document.body.scrollLeft;
		w = document.body.scrollWidth;
		h = document.body.scrollHeight;
	}
	return { t: t, l: l, w: w, h: h };
}

//获取元素的纵坐标
function getTop(e){
	var offset=e.offsetTop;
	if(e.offsetParent!=null){ offset+=getTop(e.offsetParent);}
	return offset;
}

//获取元素的横坐标
function getLeft(e){
	var offset=e.offsetLeft;
	if(e.offsetParent!=null){ offset+=getLeft(e.offsetParent);}
	return offset;
}

//获取滚动条的位置
function getScroll(){
	var t, l, w, h;
	if (document.documentElement && document.documentElement.scrollTop) {
		t = document.documentElement.scrollTop;
		l = document.documentElement.scrollLeft;
		w = document.documentElement.scrollWidth;
		h = document.documentElement.scrollHeight;
	} else if (document.body) {
		t = document.body.scrollTop;
		l = document.body.scrollLeft;
		w = document.body.scrollWidth;
		h = document.body.scrollHeight;
	}
	return { t: t, l: l, w: w, h: h };
}

//
function getQueryString( frm_id ){
	var queryString="";
	var frm = document.getElementById(frm_id);
	var numberElements =  frm.elements.length;
	var q = '';
	var name = '';
	for(var i = 0; i < numberElements; i++) {
		name = frm.elements[i].name;
		if(name!='submit' && name!='') {
			queryString += q + name+"="+encodeURIComponent(frm.elements[i].value);
			q = '&';
		}
	}

	return queryString;
}

//创建遮罩层 
function create_global_opacity_div(oid){
	var div = document.createElement("div");
	div.setAttribute('id', oid);
	div.className = 'global_opacity_div_class';
	div.style.height = (document.body.clientHeight+20)+'px';
	document.body.appendChild(div);
}

function template_cancel_div(div_id){
	try{
		var o = document.getElementById(div_id);
		o.parentNode.removeChild(o);
	}
	catch(e) {}
}

function cancel_div(div_id){
	try{
		var o = document.getElementById(div_id);
		o.parentNode.removeChild(o);
	}
	catch(e) {}
}

function setCaretAtEnd(field){
	if(field.createTextRange){
		var r = field.createTextRange();
		r.moveStart('character', field.value.length);
		r.collapse();
		r.select();
	}
}


//评论添加
var movie_comment_flag = true;
function movie_comment(flag){
	if(!movie_comment_flag || document.getElementById("movie_col_box_id")){
		return true;
	}
	movie_comment_flag = false;
	if(arguments.length>=1 & arguments[0]==0){//需要初始化用户和电影的状态
		movie_col._init_cookie_name();
		fav_init_username();
		fav_init_movie_people_data(function(){
			fav_init_movie_people_ajax_back();
			_people_init();
		});
	}
	var tpl = new template('movie_comment',movie_comment_html_callback);
	tpl.setparam('movietitle',movie_title);
	tpl.setparam('movieid',movieid);
	tpl.setparam('username',getCookie('usrname'));
	tpl.get();
	return true;
}
//弹出层
function movie_comment_html_callback(html){
	movie_tpl.div('movie_col_box_id',html.responseText);
	movie_comment_flag = true;
}
var movie_comment_pub='comment';//默认是影评
//发布
function movie_comment_post(){
	if(movie_comment_check()){
		movie_comment_error_hidden();
		var f = $obj('movie_comment_form');
		var content = document.getElementById('movie_comment_text').value;
		if(!content || content.trim().strlen()<200){//讨论区的URL
			f.action = "http://comment.xunlei.com/insert_movie_subject.php";//讨论区的action
			movie_comment_pub='subject';//
		}
		else{
			movie_comment_pub='comment';//
			f.action="http://usercp.movie.xunlei.com/interface/comment.php";//影评
		}
		
		f.submit();
		movie_comment_title('正在发送评论');
	}
	else{
		document.getElementById('movie_comment_button').disabled = false;
	}
}
function movie_comment_title(msg){
	document.getElementById('movie_status_title').innerHTML = msg;
	document.getElementById('movie_status_title').style.display='';
	return true;
}
//检测
function movie_comment_check(){
	var title = document.getElementById('movie_comment_title').value;
	if(!title){
		movie_comment_error('请给您的影评起一个标题')
		return false;
	}
	
	var content = document.getElementById('movie_comment_text').value;
	if(!content || content.trim().strlen()<=0){
		movie_comment_error('内容不能为空')
		return false;
	}
	if(content.trim().strlen()>=200){//大于200的才有评分
		var flag = false;
		for(var i=1;i<=5;i++){
			if(document.getElementById('movie_comment_appraise'+i).checked){
				flag = true;
				break;
			}
		}
		if(!flag){
			movie_comment_error('请您再给 '+movie_title+' 一个总体评价(从差到好)')
			return false;
		}
	}
	return true;
}
//发布回调
var movie_comment_post_cancel_id = '';
function movie_comment_post_back(r){
	if(r){//成功
//		movie_comment_title('发表影评成功');
	}
	else{//失败
		movie_comment_title('发表影评失败');
	}
	var f = $obj('movie_comment_form');
	try {
		f.action = '_blank';
		f.method = 'get';
		f.submit();
	} catch(e) {}
	
	if(movie_comment_pub=='subject'){
			document.location.href="http://comment.xunlei.com/movie/"+movieid;
			return true;
	}
	
	
	if(r){
		movie_comment_post_cancel_id = setTimeout('movie_comment_post_cancel();',10000);
		document.getElementById('movie_comment_pub').style.display='none';
		document.getElementById('movie_comment_pub_success').style.display='';
		
	}
	else{
		setTimeout('movie_comment_post_cancel();',1000);
	}
	
}
function movie_comment_error(msg){
	var obj = document.getElementById('comment_error');
	obj.innerHTML = msg;
	obj.style.display='';
}

function movie_comment_error_hidden(){
	var obj = document.getElementById('comment_error');
	obj.innerHTML = '';
	obj.style.display='none';
}


//取消
function movie_comment_post_cancel(){
	try{
		clearTimeout(movie_comment_post_cancel_id);
	}
	catch(e){}
	template_cancel_div('suopingdiv');
	template_cancel_div('movie_col_box_id');
	if(arguments.length==0){
		if(movie_comment_pub=='comment'){
			document.location.href="http://usercp.movie.xunlei.com/comment.php";
		}
		else{
			document.location.href="http://comment.xunlei.com/movie/"+movieid;
		}
	}
}

//跳转检测
function movie_comment_goto_check(){
	if(document.getElementById('movie_comment_text').value){
		return confirm('您所输入的内容尚未保存，确定要离开当前页面吗？');	
	}
	else{
		return true;
	}
}

function movie_comment_textarea_tishi(){
	return true;
	document.getElementById('comment_num').innerHTML = parseInt(document.getElementById('movie_comment_text').value.trim().strlen()/2);
}

function check_movie_xlcookie(id){
	var flag = false;
	var cookie_key = 'movie_people';
    	var value = getCookie(cookie_key);
    	if(value){
    		var tmp = value.split(',');
    		if(tmp.inArray(id)){
    			flag = true;
    		}
    	}
    	return flag;
}
//加一个ID到COOKIE里面来
function set_movie_xlcookie_id(id){
    var cookie_key = 'movie_people';
    var value = getCookie(cookie_key);
    if(value){
    	var tmp = value.split(",");
    	if(tmp.inArray(id)){
    		return true;
    	}
    	tmp.push(id);
    	if(tmp.length>5){
    		tmp.shift();
    	}
    	var ids = tmp.toString();
    	ids = ids.replace(/,{1,}/g,",");
    	ids = ids.replace(/(^,*)|(,*$)/g,"");
    	setCookie(cookie_key,ids,2);
    }
    else{
    	setCookie(cookie_key,id,2);
    }
}

function check_people_xlcookie(id){
	var flag = false;
	var cookie_key = 'people_username';
    	var value = getCookie(cookie_key);
    	if(value){
    		var tmp = value.split(',');
    		if(tmp.inArray(id)){
    			flag = true;
    		}
    	}
    	return flag;
}
function set_people_xlcookie_id(id){
    var cookie_key = 'people_username';
    var value = getCookie(cookie_key);
    if(value){
    	var tmp = value.split(",");
    	if(tmp.inArray(id)){
    		return true;
    	}
    	tmp.push(id);
    	if(tmp.length>5){
    		tmp.shift();
    	}
    	var ids = tmp.toString();
    	ids = ids.replace(/,{1,}/g,",");
    	ids = ids.replace(/(^,*)|(,*$)/g,"");
    	setCookie(cookie_key,ids,2);
    }
    else{
    	setCookie(cookie_key,id,2);
    }
}