/**
 *Created on 2008-03-14
 *@author anwx<a href="mailto:luckyanzi@china.com.cn">An Weixiao</a>
 */
 var CompareTool = Class.create();
 CompareTool.prototype = {
     initialize:function(){},
     $cookie:function(){
         return document.cookie;
     },
	 name:function(){
	     return "chsi_compare_cSpace_code_Trim";
	 },
     maxItem:function(){ //最多只能选择5个
	     return 5;
	 },
	 minItem:function(){ //最少得选择两个，否则还比较个什么哦呢？
	     return 2;
	 },
	 $dataElement:function(){
	     return $("compareDatas");
	 },
	 $dataForm:function(){
	     return $("choose_compare_form");
	 },
     $managerDataFrom:function(){
	     return $("managerDataForm");
	 },
	 $searchCondForm:function(){
	     return $("navForm");
	 },
     $timer:function(){
         return new Date();
     },
     showError:function(errorMsg){
         alert(errorMsg);
     },
     setCookie:function(name, value, path){
        var  cookieTime = this.$timer();
		cookieTime.setMonth(cookieTime.getMonth()+1);
		document.cookie = name + "=" + escape(value) +";expires="+ cookieTime.toGMTString()/*+ ";path=" + path*/;
     },
	 getCookie: function(name) { //get the cookie by key
         var dc = document.cookie;
         var prefix = name + "=";
         var begin = dc.indexOf("; " + prefix);
         if (begin == -1) {
             begin = dc.indexOf(prefix);
             if (begin != 0) return null;
         } else {
             begin += 2;
         }
         var end = document.cookie.indexOf(";", begin);
         if (end == -1) {
             end = dc.length;
         }
         return unescape(dc.substring(begin + prefix.length, end));
     },
     delCookie:function(name){
         var exp = this.$timer();
         exp.setTime(exp.getTime() - 1);
    	 var cval=this.getCookie(name);
         if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
     },
     onloadSetDefaultValue:function(){
         var compareCodes = this.$dataElement();
		 if(compareCodes.value.strip() != ""){
			 setTimeout(function(){compareTool.managerCompareDatas({o:$('opt_list_btn'), c:''});}, '1000')
		     $("compare_tool_div_content").show();
		 }
	     spaceList = this.getCookie(this.name());
	     if(spaceList!=null){
		    if(compareCodes!=null){
			    compareCodes.value = spaceList;
		    }
		    var ary =spaceList.split(",");
		    var i,j;
		    var len=this.$dataForm().elements.length;
		    for(j=0;j<ary.length;j++){
		        for (i=0;i<len;i++){
		            if (this.$dataForm().elements[i].type=="checkbox" && this.$dataForm().elements[i].value==ary[j]){
		                this.$dataForm().elements[i].checked=true;				
			        }
		        }
		    }
	     } else {
		     if(compareCodes!=null){
			     compareCodes.value ="";
		     }
         }
	     this.setCookie(this.name(),'','/fzck/byszy/');
     },	 
     clickManagerItemToChange: function(checkboxobj){
	     this.clickItemToChange_(checkboxobj);
         var len=this.$dataForm().elements.length;
		 for (i=0;i<len;i++){
		     if (this.$dataForm().elements[i].type=="checkbox" && this.$dataForm().elements[i].value== checkboxobj.value){
				 if(checkboxobj.checked)
				     this.$dataForm().elements[i].checked=true;
				 else
					 this.$dataForm().elements[i].checked=false;
			 }
		 }
	 },
     clickItemToChange:function(checkboxobj){
	     this.clickItemToChange_(checkboxobj);
		 var form_ = this.$managerDataFrom();
		 if(form_){
             var len = form_.elements.length;
		     for(var i = 0; i < len; i++){
		         if (form_.elements[i].type=="checkbox" && form_.elements[i].value== checkboxobj.value){
				     if(checkboxobj.checked)
				         form_.elements[i].checked=true;
				     else
				    	 form_.elements[i].checked=false;
			     }
		     }
		 }
	 },
     clickItemToChange_:function(checkboxobj){
        var cSpace_code=this.$dataElement().value; //
        if (!checkboxobj.checked){
            if (cSpace_code != ""){
		        cSpace_code = cSpace_code + ",a";
				if(cSpace_code.indexOf(','+checkboxobj.value+',')>=0){
				    cSpace_code = cSpace_code.replace(','+checkboxobj.value+',',',');							
				}
			} 
			if (cSpace_code == ",a") 
			    cSpace_code = "";
			else
		        cSpace_code = cSpace_code.replace(',a','');		
	    } else {
	        if (cSpace_code.length > 0 ){
			    if (cSpace_code.split(",").length > this.maxItem()){
				    this.showError("你选择待比较数据过多，最多只能选择:"+this.maxItem()+"条!");
					checkboxobj.checked = false;
					return false;
				}
			}
	    	cSpace_code = cSpace_code + this.compareSpaceCode(cSpace_code,checkboxobj.value)
	   }
	   this.$dataElement().value=cSpace_code;
     },
	 getParamValues:function(){
		 var paramValues = this.$dataElement().value;
         var paramArrays = paramValues.split(",");
		 var returnDatas = [];
		 for(var i = 0; i < paramArrays.length; i++){
		     if(paramArrays[i].strip() != ""){
			     returnDatas.push([paramArrays[i], 'yxdm']);
			 }
		 }
		 var otherParams_ = this.getOtherParamInfoValues_();
		 for( var i = 0; i <otherParams_.length; i++ ){
		     returnDatas.push(otherParams_[i]);
		 }
		 return returnDatas;
	 },
	 getOtherParamInfoValues_:function(){
		 var otherParams = [];
		 var condForm_ = this.$searchCondForm();
         var belongTo_ = condForm_.elements['belongTo'].value;
		 otherParams.push([belongTo_, 'belongTo']);

		 var type_ = condForm_.elements['type'].value;
		 otherParams.push([type_, 'type']);
         
		 var schType_ = condForm_.elements['schType'].value;
		 otherParams.push([schType_, 'schType']);

		 var province_ = condForm_.elements['province'].value;
		 otherParams.push([province_, 'province']);
		 
		 var schNature_ = condForm_.elements['schNature'].value;
		 otherParams.push([schNature_, 'schNature']);

         if(condForm_.elements['schName']){
             var yxmc_ = condForm_.elements['schName'].value;
		     otherParams.push([yxmc_, 'schName']);
		 }

		 var profLevel_ = condForm_.elements['profLevel'].value;
		 otherParams.push([profLevel_, 'profLevel']);
         return otherParams;
	 },
	 doCompare:function(obj){
	     var cSpace_inPage = "";
	     var errorMsg = "请选择两个或两个以上的数据信息，进行比较。";
	     var cspaceobj = this.$dataElement();
		 if(cspaceobj != null){
		     cSpace_inPage = cSpace_inPage+cspaceobj.value;
         }
		 if(cSpace_inPage== ""){
			 this.showError(errorMsg);
		     return false;
		 }
		 var spcNum = cSpace_inPage.split(",");
		 if(spcNum.length <= this.minItem()){
		    this.showError(errorMsg);
			return false;
		 }
		 var wrappedValues = this.getParamValues();
		 util.fzckCompareBySch(obj, wrappedValues);
		 return false;
	 },
     delSpaceList: function(checkboxobj){
         var valueStr=this.$dataElement().value;
        if (!checkboxobj.checked){
		    if (valueStr != ""){
		        valueStr = valueStr + ",a"
		        if(valueStr.indexOf(','+checkboxobj.value+',')>=0){
		            valueStr = valueStr.replace(','+checkboxobj.value+',',',');							
		        }
		    } 
		    if (valueStr == ",a") 
		        valueStr = "";
		    else
		        valueStr = valueStr.replace(',a','');		
	    } 	
	    this.$dataElement().value=valueStr;
    },
    compareSpaceCode:function(spaceStr,spaceCode){
		if (spaceStr != ""){
			spaceStr = spaceStr + ',';
			if (spaceStr.indexOf(','+spaceCode+',') >= 0)
				return "";
			else
				return ','+spaceCode;
		} else
			return ','+spaceCode;
	},
	unSelectAllCheckBoxByFormObject_:function(fm_){
		if(!fm_){
		   return false;
		}
	    for(var p = 0; p < fm_.elements.length; p++){
			var te_ = fm_.elements[p];
		    if(te_.type == 'checkbox' && te_.checked){
			   te_.checked = false;
			}
		}
	},
	clearAllSelectedDatas:function(){
	    this.delCookie();
		this.$dataElement().value = "";
        var fm_ = this.$dataForm();
		var fm1_ = this.$managerDataFrom();
		this.unSelectAllCheckBoxByFormObject_(fm_);
        this.unSelectAllCheckBoxByFormObject_(fm1_);
        $("compare_tool_div_content").innerHTML = "";
	},
    managerCompareDatas:function(obj){
        var datas = this.$dataElement();
		if(datas.value.strip() == ""){
		    return false;
		}
		var params = this.getParamValues();
		if($("link_form_mg_compareBySch__")){
		   Element.remove("link_form_mg_compareBySch__");
		}
		var actionStr = obj.c+'/fzck/byszy/callbackInfo.do';
	    util.toForm(obj.o,{id:'mg_compareBySch__', action:actionStr, target:'_blank', useAjax:true, to:'compare_tool_div_content',params:params});
        $("compare_tool_div_content").show();
	}
}
var compareTool = new CompareTool();
