var domainStr  = ".movoweb.com";
var suserLogon = {
	email             : null,
	password          : null,
	butn            : null,
   	emailEmptyMessage : null,
   	emailErrorMessage : null,
   	passwordEmptyMessage : null,
   	waitMsg              : null,
   	throughtMsg          : null,
   	nothroughtMsg        : null,
   	noapplyMsg           : null,
   	contextPath       :  null,

   	isIndex           :  false,
   	context 		  : "/compass",
   	init  : function(emailEmptyMsg , emailErrorMsg , passwordEmptyMsg , contextPath , waitmsg , throughtmsg ,nothroughtmsg ,noapplymsg,compassid){
   		var self      = this;
   		this.emailEmptyMessage    = emailEmptyMsg;
   		this.emailErrorMessage    = emailErrorMsg;
   		this.passwordEmptyMessage = passwordEmptyMsg;
   		this.contextPath          = contextPath;
   		this.waitMsg              = waitmsg;
   		this.throughtMsg          = throughtmsg;
   		this.nothroughtMsg        = nothroughtmsg;  
   		this.noapplyMsg           = noapplymsg;
   		this.compassid            = compassid
   	},
   	check : function(emailId ,passwordId , btn){

   		var self     = this;
   		
   		self.email    = $.trim($("#"+emailId).val());
		self.password = $.trim($("#"+passwordId).val());
   		self.butn   = $("#"+btn);
   		self.butn.attr("disabled",true);
   		if(!self.email){
   		
   			alert(this.emailEmptyMessage);
   			self.butn.attr("disabled",false);
   			return false;
   		}else if(!/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/.test(self.email)){
			alert(this.emailErrorMessage);
			self.butn.attr("disabled",false);
   			return false;
   		}
   		if(!self.password){
   			alert(this.passwordEmptyMessage);
   			self.butn.attr("disabled",false);
   			return false;
   		}
   		self.logon();
   	},
   	changeLanguage : function(lng){
   	  var self   = this;
   		$.ajax({
   			type: "POST",
   			data: "currentLanguage="+lng,
   			url: self.contextPath+"/base!changeLanguage.do",
   			success: function(resMsg){
   			  location.reload();	   		  	
   			}   		
	   });
   	},
   	logon : function(){
   		var self   = this;
   		$.ajax({
   			type: "POST",
   			data: "suser.email="+self.email+"&suser.password="+self.password+"&compass.compassid="+this.compassid,
   			url: self.contextPath+"/suser!userLogon.do",
   			success: function(resMsg){  
   						  	  
	   		  if(resMsg.indexOf("&") != -1){
	   		  	
	   		    $.cookie('suserinfo', resMsg, {expires: 7, path: '/', domain: domainStr}); 
	   		    var info = $.cookie('suserinfo'); 
	   		    //alert('resMsg'+info);	 		    
	   		    if($("#login_s"))$("#login_s").show();
	   		    if($("#login"))$("#login").hide();
	   		    	
	   		   // if(resMsg == null){resMsg = $.cookie('suserinfo');}   		    
	   		    var result =  resMsg.split("&");
	   			if($("#nickname"))$("#nickname").html(result[3]);
	   			//$.cookie('suserid', result[1], {expires: 7, path: '/', domain: domainStr});
	   			//$.cookie('susercompassid', result[2], {expires: 7, path: '/', domain: domainStr});
	   			//$.cookie('susernn', result[3], {expires: 7, path: '/', domain: domainStr});	   			
	   			if(result[4] == 'adminPower' || result[4] == 'commonPower' || result[4] == 'superPower'){
	   				if($("#admin"))$("#admin").show();
	   				//$.cookie(result[1]+'suserpower', result[1]+result[4], {expires: 7, path: '/', domain: domainStr});
	   			}else{
	   				if($("#admin"))$("#admin").hide();
	   			}
	   			if(result[4] == 'adminPower' || result[4] == 'commonPower' || result[4] == 'superPower'){
	   				if($("#jiaru"))$("#jiaru").hide();	   			  
	   				if($("#applystate"))$("#applystate").html(self.throughtMsg);
	   				//$.cookie('suserst', 0, {expires: 7, path: '/', domain: domainStr});
	   			}else if(result.length == 7){
	   				//$.cookie('suserst', result[5], {expires: 7, path: '/', domain: domainStr});
	   			    if($("#jiaru"))$("#jiaru").hide();	   			  
	   			    if(result[5] == 1){	    
	   					if($("#applystate"))$("#applystate").html(self.waitMsg);
	   				}else if(result[5] == 0){
	   					if($("#applystate"))$("#applystate").html(self.throughtMsg);
	   				}else if(result[5] == 2){
	   					if($("#applystate"))$("#applystate").html(self.nothroughtMsg);	
	   				}
	   			}else{
	   				//alert(result[2]);
		   		  	if(result[2] != 56){
		   				alert(self.noapplyMsg);
	   		  	     }
	   			    if($("#applystate"))$("#applystate").html(self.noapplyMsg);
	   			}
	   		  if(result[0] == 'compassproxy'){
	   		     location.href = self.contextPath+'/compass!addCompassInit.do';
	   		  }		   		  
	   		  if(self.isIndex == "login"){location.reload();}	
	   		  }else{
	   		  		alert(resMsg);
	   		  	self.butn.attr("disabled",false);
	   		  }			
   			}   		
	   });
   	},
   	
   	logoff : function(){
   		var self   = this;
   		$.ajax({
   			type: "POST",
   			data: "",
   			url: self.contextPath+"/suser!userLogoff.do",
   			success: function(resMsg){   		  
	   		  if(resMsg == "success"){
	   		    $("#login").show();
	   		    $("#login_s").hide();
	   		    $("#admin").hide();
	   		    $("#jiaru").show(); 
	   		    $.cookie('suserinfo', '', {expires: 7, path: '/', domain: domainStr});
	   		    //$.cookie('suserst', '', {expires: 7, path: '/', domain: domainStr});
	   		    //$.cookie('susernn', '', {expires: 7, path: '/', domain: domainStr});	   		    
	   			//alert(resMsg);
	   		  }else{
	   		  	alert(resMsg);	   		  
	   		  }			
	   		  $("#logonButton").attr("disabled",false);
   			}   		
	   });
   	}
}
function getCookie(name)     
{
    var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
     if(arr != null) return unescape(arr[2]); return null;

}

function delCookie(name)
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}
/*
* add article
*/

var addArticleFn = {		
	isthrough             : 0,
	isEdit                : null,
	
	deleteUploadFile      : function(it){
	    var param = "filePath="+it.id;
		$.ajax({
	   			type: "POST",
	   			data: param,
	   			url: "/movoupload/uploadInfo!deleteUpload.do",
	   			success: function(resMsg){   		  
		   		  alert(resMsg);		
	   			}   		
		   });
	},
	rebuildFileServerPath : function(btn,isEdit){
	    btn.disabled  = true;
		var self  = this;
		self.isEdit  = isEdit;
		
		/*var param = new Array();
		$("img",$("#manage_list_pic")).each(function(i){
			 param.push("spaths=" + this.src.toString());				
		});
		if(param.join("&") != null && param.join("&") != "" ){
			$.ajax({
	   			type: "POST",
	   			data: param.join("&"),
	   			url: "/movoupload/uploadInfo!convertTempTo.do",
	   			success: function(resMsg){   		  
		   		  if(resMsg == "success"){
					document.getElementById("content").value = document.getElementById("content").value.replace(/\/temp\/img\//gim,'/completed/img/');
		   			self.onSubmit(btn);
		   		  }else{
		   		  	alert(resMsg);	
		   		  	btn.disabled  = false;   		  	
		   		  }			
	   			}   		
		   });
	   }else{	   		
	   		self.onSubmit(btn);
	   }*/
	   self.onSubmit(btn);
	},
	onSubmit : function(btn){
		var self  = this;
		self.isthrough = 0 ;		
		var	category  = $("#categoryId");
		var title     = $("#articleTitle");
		$("#content").val(__EditorWindow.document.body.innerHTML);  
		var content   = $("#content");
		var topnumber = $("#topnumber");
		var createTime= $("#createTime");
		var form      = $("#addArticleForm");
		//alert(category.attr(""));
		var now = new Date();
		createTime.val(now.getYear()+"-"+(now.getMonth()+1)+"-"+now.getDate()+" "+now.getHours()+":"+now.getMinutes()+":"+now.getSeconds());
		
		if(!self.isEdit){		  
			if(category.val() == "" || category.val() == null){
				$("#categoryMessage").html($("#catEmpty").html());
				btn.disabled  = false;
				//self.isthrough = false;			
			}else{
				$("#categoryMessage").html("");						
				self.isthrough++;
			}
		}else{
			self.isthrough++;
		}
		if(title.val() == "" || title.val() == null){
			$("#titleMessage").html($("#titalEmpty").html());
			btn.disabled  = false;
			//self.isthrough = false;			
		}else{
			$("#titleMessage").html("");
			self.isthrough++;
		}				
		if(allContent() == null || allContent() == "" || content.val() == "" || content.val() == null){			
			$("#contentMessage").html($("#contextEmpty").html());
			btn.disabled  = false;
		}else{
			$("#contentMessage").html("");
			self.isthrough++;
		}
		if($("#isTopabledBu").is(":checked")){
			if( parseInt(topnumber.val()) < 1 || parseInt(topnumber.val()) > 255 ){
				$("#topnumberMessage").html($("#topNumRange").html());
				btn.disabled  = false;
				//self.isthrough = false;			
			}else{
				$("#topnumberMessage").html("");
				self.isthrough++;
			}
		}
		if(createTime.val() == "" || createTime.val() == null){
			$("#creatertimeMessage").html($("#create_time").html());
			btn.disabled  = false;
			//self.isthrough = false;			
		}else{
			$("#creatertimeMessage").html("");
			self.isthrough++;
		}		
		if((self.isthrough == 5) || (!$("#isTopabledBu").is(":checked") && self.isthrough == 4)){			
			$("img",$("#manage_list_pic")).each(function(i){
			var oPath = this.src.replace(/_s/,"").replace(/temp/,"completed");
			//alert(oPath);
			if(this.className == "nontransparent"){
			$("#imageItems").append("<input type='hidden' name='artimagemangerList["+i+"].recomstatus' value='1'/>");
			}else{
			$("#imageItems").append("<input type='hidden' name='artimagemangerList["+i+"].recomstatus' value='2'/>");
			}
			$("#imageItems").append("<input type='hidden' name='artimagemangerList["+i+"].imageorder' value='"+i+"'/>");
		
			$("#imageItems").append("<input type='hidden' name='artimagemangerList["+i+"].imagepath' value='"+oPath+"'/>");
			});
			form.submit();
		}
	}
};
var changeClassName = function(e){	
	$("img.nontransparent").removeClass().addClass("translucent");
	e.className = "nontransparent";
}
var hasSelectedImg = function(){
	return $("img.nontransparent").attr("id");
}
var showSmallImg   = function(it){
	   $(it).mousedown(function(e){
      		$('#showSmallImg').show().css({ top: (e.pageY - 10), left: (e.pageX - 105)});
      		//alert(e.pageX +', '+ e.pageY);
   		});
}

var applyJoin  = {
    msg  : null,
    open : function(notLogon){
    	var userinfo 	= $.cookie('suserinfo');
    	if(userinfo){
	    	var userinfos  = userinfo.split('&');
			if(userinfos[1] != null && userinfos[1] != ""){
			    $('#applyreviewDiv').show();
			 }
		}else{
			alert(notLogon);
		}
    },
	join : function(it,applyConEmpty){
	    var userinfo = $.cookie('suserinfo');
	    if(userinfo){
	    var userinfos  = userinfo.split('&');
	    it.disabled = true;		
		   if(userinfos[1] != null && userinfos[1] != ""){
			var applyreview = $("#applyreview").val();
			if(!applyreview || applyreview == ''){
				alert(applyConEmpty);
				it.disabled = false;
				return ;
			}
			$.ajax({
  			type: "POST",
  			data: "uservice.applyReview="+applyreview,
  			url: suserLogon.context+"/suser!addApplyUser.do",
  			success: function(resMsg){   		  
	   		  if(resMsg == "success"){
				$("#jiaru").hide();
				$("#applyreviewDiv").hide();
				$("#applystate").html(suserLogon.waitMsg);
	   		  }else{
	   		  	alert(resMsg);	 
	   		  	$('#applyreviewDiv').show();  		  	
	   		  }			
	   		  it.disabled = false;
  			}   		
   		   });
   		 }
		}else{				
			alert(notLogon);
			$("#applyreviewDiv").hide();
		}
	}	
}

var batchDelete = function(){
    var param 		= new Array();
    var pageNo 		= null;
    var categoryId 	= null;
    $(":checkbox:not(:last)").each(function(i){     	
    	pageNo 		= $(this).attr('pageNo');    	
    	categoryId	= $(this).attr('categoryId');
    	if($(this).is(':checked'))param.push("articleIds=" +$(this).val());
    });
    if(param.join("&") == ''){
		alert("empty");
		return;
	}
	this.disabled = true;
	$.ajax({
  			type: "POST",
  			data: param.join("&"),
  			url: suserLogon.context+"/article!batchDelete.do",
  			success: function(resMsg){   		  
	   		  if(resMsg == "success"){
				location.href = suserLogon.context+"/article!listArticleByCategory.do?categoryId="+categoryId+"&pageNo="+(pageNo-1);
	   		  }else{
	   		  	alert(resMsg);	   		  	
	   		  }			
	   		  this.disabled = false;
  			}   		
   });
}

var batchchange = function(sCateId){
    var param 		= new Array();
    var pageNo 		= null;
    var categoryId 	= null;
    param.push("categoryId="+sCateId);
    $(":checkbox:not(:last)").each(function(i){     	
    	pageNo 		= $(this).attr('pageNo');    	
    	categoryId	= $(this).attr('categoryId');
    	if($(this).is(':checked'))param.push("articleIds=" +$(this).val());
    });
    if(param.join("&") == ''){
		alert("empty");
		return;
	}
	this.disabled = true;
	$.ajax({
  			type: "POST",
  			data: param.join("&"),
  			url: suserLogon.context+"/article!batchChange.do",
  			success: function(resMsg){   		  
	   		  if(resMsg == "success"){
				location.href = suserLogon.context+"/article!listArticleByCategory.do?categoryId="+categoryId+"&pageNo="+(pageNo-1);
	   		  }else{
	   		  	alert(resMsg);	   		  	
	   		  }			
	   		  this.disabled = false;
  			}   		
   });
}
var batchAddExtract = function(){
	var param 		= new Array();
    var pageNo 		= null;
    var categoryId 	= null;
    $(":checkbox:not(:last)").each(function(i){     	
    	pageNo 		= $(this).attr('pageNo');    	
    	categoryId	= $(this).attr('categoryId');
    	if($(this).is(':checked'))param.push("articleIds=" +$(this).val());
    });
    if(param.join("&") == ''){
		alert("empty");
		return;
	}
	this.disabled = true;
	$.ajax({
  			type: "POST",
  			data: param.join("&"),
  			url: suserLogon.context+"/article!batchAddExtract.do",
  			success: function(resMsg){   		  
	   		  if(resMsg == "success"){
				location.href = suserLogon.context+"/article!listArticleByCategory.do?categoryId="+categoryId+"&pageNo="+(pageNo-1);
	   		  }else{
	   		  	alert(resMsg);	   		  	
	   		  }			
	   		  this.disabled = false;
  			}   		
   });	
}

var batchCancelExtract = function(){
	var param 		= new Array();
    var pageNo 		= null;
    var categoryId 	= null;
    $(":checkbox:not(:last)").each(function(i){     	
    	pageNo 		= $(this).attr('pageNo');    	
    	categoryId	= $(this).attr('categoryId');
    	if($(this).is(':checked'))param.push("articleIds=" +$(this).val());
    });
    if(param.join("&") == ''){
		alert("empty");
		return;
	}
	this.disabled = true;
	$.ajax({
  			type: "POST",
  			data: param.join("&"),
  			url: suserLogon.context+"/article!batchCancelExtract.do",
  			success: function(resMsg){   		  
	   		  if(resMsg == "success"){
				location.href = suserLogon.context+"/article!listArticleByCategory.do?categoryId="+categoryId+"&pageNo="+(pageNo-1);
	   		  }else{
	   		  	alert(resMsg);	   		  	
	   		  }			
	   		  this.disabled = false;
  			}   		
   });	
}

var changeArticleLanguage = function(language){	
	$("#articleTitle").attr('name',language+'.title');
    $("#content").attr('name',language+'.context');
}

var addArticle = {
	add  : function(msg,msg2){
		var suserinfo = $.cookie('suserinfo');					
		if(suserinfo == null || suserinfo == ""){		
				alert(msg);
				$("#suserEmail").focus();
				return false;				 
		}
		var comidcookie   = $.cookie('compass');
		var suserinfos 	  = suserinfo.split("&");	
		if(suserinfos != null && suserinfos[2] != comidcookie){	
				alert(msg);
				$("#suserEmail").focus();
				return false;
		}
		var isauding = null;
		if(suserinfos.length == 6){
			isauding = suserinfos[5];
		}
		if(suserinfos.length == 7){
			isauding = suserinfos[6];
		}
		//alert("the proms is "+isauding+"--"+suserinfos.length+"--"+suserinfos[5]+suserinfos[4]);
		if(isauding == 3){alert(msg2); return false;}
		if(isauding == 2){return true;}
		if(isauding == 1 && suserinfos.length == 7 && suserinfos[5] == 0){
			
			return true;
		}else if(isauding == 1 && suserinfos[4] == "adminPower" || suserinfos[4] == "superPower"){
			return true;
		}else{alert(msg2); return false;}
		return true;
	}
}

function addBookmark(title,url) {
	if (window.sidebar){ 
		window.sidebar.addPanel(title,url,""); 
	}else if(document.all) {
		window.external.AddFavorite(url,title);
	}else if(window.opera && window.print){
		return true;
	}
}

function search(msg){
	if($.trim($("#searchKey").val()) == ""){
	    alert(msg);
		return false;
	}else{
		return true;
	}
}
//设为首页
function setHomepage(obj)
{
    if (document.all)
    {
        document.body.style.behavior='url(#default#homepage)';
        document.body.setHomePage(obj);
    } else if (window.sidebar) {
        if(window.netscape)
        {
            try
            {
                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
            }
            catch (e)
            {
                alert("Firefox暂无此功能，请手动设置。" );
            }
        }
    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
    prefs.setCharPref('browser.startup.homepage',obj);
    }
}
