﻿function lib_popupDivC(o,n,hiddenN,dataArrN,pObj,genHTMLFN,onclickFN,onclickFN_p,onclickFN_c,showSelectedFN,bSaveFN){
	this.ClientID = o;
	this.checkboxN = n;
	this.d = document.getElementById('popupDiv_'+this.ClientID);
	this.d2 = document.getElementById('itemDiv_'+this.ClientID);
	this.d3 = document.getElementById('cacheItemDiv_'+this.ClientID);
	this.b = document.getElementById('button_'+this.ClientID);
	this.s = document.getElementById('pSel_'+this.ClientID);
	this.sCache = document.getElementById('cache_'+this.ClientID);
	this.selectedItemCon = document.getElementById('selItem_'+this.ClientID);
	this.bSave = document.getElementById('buttonSave_'+this.ClientID);
	this.h = eval(hiddenN);
	this.da = eval(dataArrN);
	this.p = pObj?pObj:null;
	this.c = new Array();
	this.genHTML = genHTMLFN?'this.'+genHTMLFN+'()':null;
	this.fnClick = onclickFN;
	this.fnClickP = onclickFN_p?onclickFN_p:onclickFN;
	this.fnClickC = onclickFN_c?onclickFN_c:onclickFN;
	this.showSelected = showSelectedFN?showSelectedFN+'()':null;
	this.config = new Object();
	var T = this;
	if(this.p!=null) this.p.c[this.p.c.length] = T;
	this.B = function(e){T.clickB(e);};
	this.A(this.b,'click',this.B);
	if(this.s){
		this.S = function(){eval('T.'+genHTMLFN+'()');};
		this.A(this.s,'change',this.S);
	}
	if(this.bSave){
		this.BSave = function(){if(bSaveFN) eval('T.'+bSaveFN+'()');else T.clickBSave();};
		this.A(this.bSave,'click',this.BSave);
	}
}

lib_popupDivC.prototype.clickBSave = function(){
	this.saveItem(true);
	hideCurrentPopup();
	if(this.showSelected!=null) eval('this.'+this.showSelected);
		
	if(this.config && this.config.linkfn) eval(this.config.linkfn+'('+this.h.value+')');
}

lib_popupDivC.prototype.clickB = function(e){
	this.saveItem(false);
	if(this.showSelected!=null) eval('this.'+this.showSelected);
	if(document.getElementById('popupDivImg_'+this.ClientID)) this.i = document.getElementById('popupDivImg_'+this.ClientID);
	else this.i = null;
	if(this.d.style.visibility=='hidden'){
		this.sSelected();
		this.showCacheItem();
		if(this.genHTML!=null) eval(this.genHTML);
	}
	var x=null,y=null;
	if(this.i!=null || this.config.x!='undefined'){
		x = ((this.i!=null)?getXY(this.i).x:0) + ((this.config.x!='undefined')?this.config.x:0);
		y = ((this.i!=null)?getXY(this.i).y:0) + ((this.config.y!='undefined')?this.config.y:0);
	}
	showPopup(this.d.id,e,x,y);
}

lib_popupDivC.prototype.sSelected = function(){
	if(this.s && this.s.style.display!='none' && this.h){
		if(this.h.value!=''){
			var arrId = this.h.value.toString().split(',');
			var lastId = (arrId.length-1>-1)?arrId[0]:null;
			if(lastId!=null) selectDefaultValue(this.s,lastId,this.da);
		}
		else{
			if(this.ClientID=='locationF'){
				try{this.s.value=547;} //默认泰安 
				catch(e){}
			}
		}
	}
}

lib_popupDivC.prototype.clickCheckbox = function(c){
	this.saveCacheItem(c);
}

lib_popupDivC.prototype.clickCheckboxP = function(c){
	this.saveCacheItem(c);
	this.changeDivItem4pItemChanged(c);
	//if(this.genHTML!=null) eval(this.genHTML);
}

lib_popupDivC.prototype.clickCheckboxC = function(c){
	this.saveCacheItem(c);
	this.changeDivItem4cItemChanged(c.value);
	//if(this.genHTML!=null) eval(this.genHTML);
}

lib_popupDivC.prototype.writeDivItem = function(){
	if(this.s) var pId = this.s.value;
	else var pId = '0';
	var arrData = this.da;
	if(this.d2 && this.sCache){
		var arrPId = new Array(),pText='',str='',strTr='',strP='',i,j,t,u=-1,tempPId,tempPIndex,pAdd=false,gpAdd=false,add=false,tdWF=this.config.tdWidthF?this.config.tdWidthF:'auto',tdTL=this.config.tdTextL?this.config.tdTextL:16,itemL=0;
		var tdWidth=parseInt(100/this.config.col)+'%';
		tempPId = pId;
		while(tempPId!='0'){
			arrPId[arrPId.length] = tempPId;
			tempPIndex = getIndexByValue(tempPId,arrData);
			if(arrPId.length==1) pText = arrData[tempPIndex][2];
			tempPId = arrData[tempPIndex][1];
		}
		for(i=0;arrPId[i];i++){
			for(j=0;j<this.sCache.length;j++)
				if(this.sCache.options[j].value==arrPId[i]){
					if(i==0) {pAdd=true;break;}
					else {gpAdd=true;break;}
				}
			if(pAdd || gpAdd) break;	
		}
		if(pId!='0') strP = '<div style="margin:5 2;font-size:14px;"><input type="checkbox" name="'+this.checkboxN+'" value="'+pId+'" alt="'+pText+'" onClick="'+this.ClientID+'.'+this.fnClickP+'(this)"'+(gpAdd?' disabled':'')+(pAdd?' checked':'')+'>'+pText+'&nbsp;&nbsp;<span class="black12">(选择此大类，将包括以下所有小类)</span></div>';
		for(i=0;arrData[i];i++)
			if(arrData[i][1]==pId){
				add=false;
				if(!pAdd && !gpAdd) for(j=0;j<this.sCache.length;j++) if(this.sCache.options[j].value==arrData[i][0]) {add=true;break;}
				u++;
				t=u%this.config.col;
				if(t==0) strTr += '<tr>';
				if(tdWF=='fixed') itemL=GetRealLength(arrData[i][2]);
				if(itemL>tdTL && t==this.config.col-1){strTr += '<td width="'+tdWidth+'"></td>';i--;}
				else strTr += '<td'+(tdWF=='fixed'?' width="'+tdWidth+'"':'')+((tdWF=='fixed' && itemL>tdTL)?' colSpan="2"':'')+'><input type="checkbox" name="'+this.checkboxN+'" value="'+arrData[i][0]+'" alt="'+arrData[i][2]+'" onClick="'+this.ClientID+'.'+this.fnClick+'(this)"'+((pAdd || gpAdd)?' disabled':'')+(add?' checked':'')+'>'+arrData[i][2]+'</td>';
				if(tdWF=='fixed' && itemL>tdTL && t<this.config.col-1) {u++;t++;}
				if(t==this.config.col-1) strTr += '</tr>';
			}
		str = strP+'<table width="100%" cellspacing="0" cellpadding="0" border="0">'+strTr+'</table>';
		if(typeof changeToggleSelectVisibility=='function') var objConDiv=this.d;
		if(objConDiv && objConDiv.style.visibility!='hidden') showAllHiddenToggleSelect();
		this.d2.innerHTML = str;
		if(this.d.shim) this.d.shim.setStyle('visibility','visible');
		if(objConDiv && objConDiv.style.visibility!='hidden') changeToggleSelectVisibility(objConDiv,'hidden');
	}
}

lib_popupDivC.prototype.saveCacheItem = function(c){
	var arrData = this.da,i,status,flag=true;;
	 if(c.checked){
	    if(arrData && arrData.length)
		for(i=this.sCache.length-1;i>-1;i--){
			status = statusByValue(c.value,this.sCache.options[i].value,arrData);
			switch(status){
				case 2 : this.sCache.options[i] = null;break;        //ascendant  :  delete descendant & add
				case 4 : break;                                 	 //ok         :  add
				case 1 :                                        	 //self       :  
				case 3 :                                        	 //descendant :  do nothing
				default :flag=false;c.checked=false;break;
			}
		}
		if(flag){
			if(this.sCache.options.length<this.config.max) this.sCache.options[this.sCache.options.length]=new Option(c.alt,c.value);
	    	else{alert('最多可添加 '+this.config.max+' 个类别！');c.checked=false;return;}
		}
	 }
	 else for(i=0;i<this.sCache.options.length;i++) if(this.sCache.options[i].value==c.value){this.sCache.options[i]=null;break;}
	 
	 if(this.config.col) this.showCacheItem();
}

lib_popupDivC.prototype.showCacheItem = function(){
	var str='',i,j=-1,t;
	if(this.d3)
	if(this.sCache.length){
		var strLine = '<div class="greyDottedLine3" style="margin:5 0;"><img src="http://images.zhaopin.com/new2/images/blank.gif" width="1" height="1"></div>';
		var tdWF=this.config.tdWidthF?this.config.tdWidthF:'auto',tdTL=this.config.tdTextL?this.config.tdTextL:16,itemL=0;
		var tdWidth=parseInt(100/this.config.col)+'%';
		str += strLine + '<table width="100%" cellspacing="0" cellpadding="0" border="0">';
		for(i=0;i<this.sCache.length;i++){
			j++;
			t=j%this.config.col;
			if(t==0) str += '<tr>';
			if(tdWF=='fixed') itemL=GetRealLength(this.sCache.options[i].text);
			if(itemL>tdTL && t==this.config.col-1){str += '<td width="'+tdWidth+'"></td>';i--;}
			else str += '<td'+(tdWF=='fixed'?' width="'+tdWidth+'"':'')+((tdWF=='fixed' && itemL>tdTL)?' colSpan="2"':'')+'><input type="checkbox" name="'+this.checkboxN+'_cache" value="'+this.sCache.options[i].value+'" alt="'+this.sCache.options[i].text+'" checked onClick="'+this.ClientID+'.'+this.fnClickC+'(this)">'+this.sCache.options[i].text+'</td>';
			if(tdWF=='fixed' && itemL>tdTL && t<this.config.col-1) {j++;t++;}
			if(t==this.config.col-1) str += '</tr>';
		}
		str += '</table>';
		if(typeof changeToggleSelectVisibility=='function') var objConDiv=this.d;
		if(objConDiv && objConDiv.style.visibility!='hidden') showAllHiddenToggleSelect();
		this.d3.innerHTML = str;
		if(this.d.shim) this.d.shim.setStyle('visibility','visible');
		if(objConDiv && objConDiv.style.visibility!='hidden') changeToggleSelectVisibility(objConDiv,'hidden');
	}
	else{
		if(typeof changeToggleSelectVisibility=='function') var objConDiv=this.d;
		if(objConDiv && objConDiv.style.visibility!='hidden') showAllHiddenToggleSelect();
		this.d3.innerHTML = '';
		if(this.d.shim) this.d.shim.setStyle('visibility','visible');
		if(objConDiv && objConDiv.style.visibility!='hidden') changeToggleSelectVisibility(objConDiv,'hidden');
	}
}

lib_popupDivC.prototype.changeDivItem4cItemChanged = function(v){
	var arrC = new Array(),c = eval('document.aspnetForm.'+this.checkboxN),status,arr=this.da;
	if(c.length) arrC = c;
	else arrC[0] = c;
	for(var i=0;arrC[i];i++){
		if(arrC[i].value==v) arrC[i].checked=false;
		if(arrC[i].disabled){
			status = statusByValue(v,arrC[i].value,arr);
			if(status==1 || status==2) arrC[i].disabled=false;
		}
	}
}

lib_popupDivC.prototype.changeDivItem4pItemChanged = function(c){
	var arrC = new Array();checkbox = eval('document.aspnetForm.'+this.checkboxN);
	if(checkbox.length) arrC = checkbox;
	else arrC[0] = checkbox;
	for(var i=1;arrC[i];i++){
		if(c.checked){arrC[i].checked=false;arrC[i].disabled=true;}
		else arrC[i].disabled=false;
	}
}

lib_popupDivC.prototype.showItem = function(){
     var arrData=this.da,arrId=new Array(),i,j,displayTxt='';
	 if(this.h.value.toString().length>0) arrId=this.h.value.toString().replace(/\s/g,'').split(',');
	 if(this.selectedItemCon){
	    if(arrId.length){
		   var str='';
		   for(i=0;i<arrId.length;i++){
		       for(j=0;j<arrData.length;j++) if(arrData[j][0]==arrId[i]) {displayTxt=arrData[j][2];break;}
			   if(displayTxt!=''){
			      if(i==arrId.length-1) str+=displayTxt;
			      else str+=displayTxt+'&nbsp; &nbsp; &nbsp; '
			   }
		   }
		   this.selectedItemCon.innerHTML=str;
		}
		else{
		   this.selectedItemCon.innerHTML='';
		}
	 }
}

lib_popupDivC.prototype.saveItem = function(flag){
     var i,j,arrData=this.da,arrId=new Array(),displayTxt='';
	 if(!flag){
	    clearSelect(this.sCache);
		if(this.h.value.toString().length>0) arrId=this.h.value.toString().replace(/\s/g,'').split(',');
		for(i=0;i<arrId.length;i++){
		    for(j=0;j<arrData.length;j++) if(arrData[j][0]==arrId[i]) {displayTxt=arrData[j][2];break;}
			if(displayTxt!='') this.sCache.options[i]=new Option(displayTxt,arrId[i]);
	    }
	 }
	 else{
		this.h.value='';
		for(i=0;i<this.sCache.options.length;i++) this.h.value+=this.sCache.options[i].value+',';
		this.h.value=this.h.value.replace(/(.*?),*$/, "$1");
	 }
	 //if(this.showSelected!=null) eval('this.'+this.showSelected);
}

lib_popupDivC.prototype.A = myAttachEvent;


/**************************************************************
        only for jobtype(interest) & location(interest)
**************************************************************/
lib_popupDivC.prototype.saveCity = function(){
	this.saveItem(true);
	this.op4Child();
	hideCurrentPopup();
	if(this.showSelected!=null) eval('this.'+this.showSelected);
	
	if(this.config && this.config.linkfn) eval(this.config.linkfn+'('+this.h.value+')');
}

lib_popupDivC.prototype.op4Child = function(){
	this.op4childButton();
	this.op4JobtypeHV(this.specCitySelected());
	this.op4JobtypeSel(this.specCitySelected());
}

var specCity = ['505','538'];
lib_popupDivC.prototype.specCitySelected = function(){
	var flag=false;
	for(var i=0;specCity[i];i++) if(this.h.value.toString().indexOf(specCity[i])>-1){flag=true;break;}
	return flag;
}

lib_popupDivC.prototype.op4childButton = function(){
	var flag = false;
	if(this.h.value=='') flag=true;
	for(var i=0;this.c[i];i++) this.c[i].b.disabled=flag;
}

lib_popupDivC.prototype.op4JobtypeSel = function(flag){
	var i,newOption;
	for(var i=0;this.c[i];i++){
		if(this.c[i].s){
			if(flag){//have special city:538,505
				if(this.c[i].s.options[this.c[i].s.length-1].value==0) this.c[i].s.options[this.c[i].s.length-1]=null;
				this.c[i].s.style.display='';
			}
			else{//no special city:538,505
				newOption=new Option('',0);
				this.c[i].s.options[this.c[i].s.length]=newOption;
				this.c[i].s.selectedIndex=this.c[i].s.length-1;
				this.c[i].s.style.display='none';
			}
		}
	}
}

lib_popupDivC.prototype.op4JobtypeHV = function(flag){
	var i,j,hValue,arrV=[],arrIndex=[],arrNewV=[],hNewVStr=',';
	for(i=0;this.c[i];i++){
		if(this.h.value=='') this.c[i].h.value='';
		else if(!flag){//no special city:538,505
			hValue=this.c[i].h.value;
			arrV=hValue.toString().split(',');
			for(j=0;arrV[j];j++){
				arrIndex=getIndexByValue(arrV[j],this.c[i].da);
				if(this.c[i].da[arrIndex][1]==0) arrNewV[arrNewV.length]=arrV[j];
				else arrNewV[arrNewV.length]=this.c[i].da[arrIndex][1];
			}
			for(j=0;arrNewV[j];j++){
				if(hNewVStr.indexOf(','+arrNewV[j]+',')<0) hNewVStr+=arrNewV[j]+',';
			}
			hNewVStr=hNewVStr.replace(/^,*(.*?),*$/,'$1');
			this.c[i].h.value=hNewVStr;
		}
		this.c[i].showItem();
	}
}

/*********************************
        utility routines
*********************************/
function selectDefaultValue(s,v,data){
	var i = 0;
	for(i=0;s.options[i];i++){
		if(s.options[i].value==v){
			s.options[i].selected = true;
			break;
		}
	}
	if(i==s.options.length){
		var pV = null;
		for(i=0;data[i];i++) if(data[i][0]==v) {pV=data[i][1];break;}
		if(pV!=null) selectDefaultValue(s,pV,data);
	}
}

//arr[i] = [postValue,parentId,displayText]
//'parentId=0' denotes top level
function statusByValue(valueA,valueB,arr){//A = 1:self 2:ascendant 3:descendant 4:ok
	if(valueA==valueB) return 1;
	var indexA = getIndexByValue(valueA,arr);
	var indexB = getIndexByValue(valueB,arr);
	while(arr[indexB][1]!='0'){
		if(valueA==arr[indexB][1]) return 2;
		indexB=getIndexByValue(arr[indexB][1],arr);
	}
	while(arr[indexA][1]!='0'){
		if(arr[indexA][1]==valueB) return 3;
		indexA=getIndexByValue(arr[indexA][1],arr);
	}
	return 4;
}
function getIndexByValue(value,arr){
	var i;
	for(i=0;i<arr.length;i++) if(value==arr[i][0]){return i;break;}
	return null;
}

function clearSelect(s){
    if(s) for(var i=s.options.length-1;i>-1;i--) s.options[i]=null;
}

function GetRealLength(strTemp){
 var i,sum=0;
 for(i=0;i<strTemp.length;i++){if ((strTemp.charCodeAt(i)>=0) && (strTemp.charCodeAt(i)<=255)) sum=sum+1; else sum=sum+2;}
 return sum;
}

function myAttachEvent(d,e,f){
	try {
		if(d.attachEvent) d.attachEvent("on"+e,f);
		else if(d.addEventListener) d.addEventListener(e,f,false);
		else{
			var oldF = eval('d.on'+e);
			if(typeof oldF!='function') eval('d.on'+e+'=f');
			else eval('d.on'+e)=function(){oldF();f();}
		}
	}
	catch (error){}
}

function dCity_residence(id){
	if(id!='' && typeof(residenceF)!='undefined' && residenceF.h && residenceF.h.value==''){
		var firstid = id.toString().split(',')[0];
		var index = null,i;
		for(i=0;arrCity[i];i++) if(arrCity[i][0]==firstid) {index=i;break;}
		if(index!=null){
			switch(true){
				case arrCity[index][0]==489 : break;
				case isOversea(firstid)     : residenceF.clickL3P(arrCity[0][0],arrCity[0][2]);break;
				case arrCity[index][1]==489 : residenceF.clickL3P(arrCity[index][0],arrCity[index][2]);break;
				default                     : residenceF.clickL3(arrCity[index][0],arrCity[index][2],arrCity[index][1]);break;
			}
		}
	}
}
function isOversea(id){
	var flag = false,i;
	for(i=0;arrOversea[i];i++) if(arrOversea[i][0]==id) {flag=true;break;}
	return flag;
}
