<%@ Page Language="C#" AutoEventWireup="true"  Codebehind="selectMultiArea.aspx.cs" Inherits="PPFOR.Admin.CustomView.selectMultiArea"    Title="地区列表" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1">
    <title>地区选择</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
    <link href="../Css/global.css" rel="stylesheet" type="text/css" />
<script src="../JS/ajax.js" type="text/javascript"></script> 
<script type="text/javascript">
    function showLoadingMsg()
    {
        var objMsg=docEle('msg');
        if(objMsg)
        {
             objMsg.style.display='';
             var _scrollTop = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
             var _scrollLeft= Math.max(document.body.scrollLeft,document.documentElement.scrollLeft);
             objMsg.style.margin=(_scrollTop - 60) + "px 0 0 " + (_scrollLeft - 102) + "px";   
        }   
    }     
    function hideLoadingMsg()
    {
        var objMsg=docEle('msg');
        if(objMsg)
        {
            objMsg.style.display='none';
        }        
    }     
    function showChildAreaList(objImage,aId)
    {
        var objTarget=docEle("child_" + aId);
        if(objImage.getAttribute("statue")!=null && objImage.getAttribute("statue")=="expand"){
            objImage.setAttribute("statue","collapse");
            objImage.src="/Admin/Images/collapse.gif"; 
            if(objTarget){
                objTarget.style.display='';
            }else{
                showLoadingMsg();
                DoAjax("/Admin/Sys/WebConfig/GetAreaList.aspx?type=3&bid=" + aId,"GET",null,fetchChildAreaList,aId);
            }
        } else {
            objImage.setAttribute("statue","expand");
            objImage.src="/Admin/Images/expand.gif"; 
            if(objTarget){
                objTarget.style.display='none';
            }
        }
    }
    function fetchChildAreaList(xmlHttp,bId)
    {
        if(xmlHttp.responseText!=null)
        {  
            var objParentRow=docEle('area_' + bId);             
            var newRow=objParentRow.parentNode.parentNode.insertRow(objParentRow.rowIndex+1); 
            newRow.id="child_" + bId;
            var objNewTd=newRow.insertCell(0);
            objNewTd.innerHTML=xmlHttp.responseText;    
            var area=docEle('select_'+bId);
            if(area){               
                changeAreaSelected(area,area.getAttribute('checkedChild'));
            }
        }
        setTimeout("hideLoadingMsg()",300);
    }
    //修改时，子级地区显示不正常，父级选中，孙子级以下会丢失的问题，未修改
    function setChildAreaChecked(pObj,isCheckedChild)
    { 
        var objs=document.getElementsByName('areaSelected'); 
        isCheckedChild=isCheckedChild!=null && (isCheckedChild==true || isCheckedChild=='true');
        if(objs!=null && objs.length>0){
            var belongId=pObj.value;            
            for(var i=0;i<objs.length;i++){
                var bid=objs[i].getAttribute('belongArea');              
                if(bid!=null && bid==belongId){  
                    var aid=parseInt(objs[i].value);                   
                    if(aid>0 && selectedAreaArray[aid]!=null){                   
                        var isLoadSelected=objs[i].getAttribute('isLoadSelected');
                        isLoadSelected=isLoadSelected!=null && (isLoadSelected==true || isLoadSelected=='true');
                        if(!isLoadSelected){                            
                            objs[i].checked=true;                                                                      
                            if(!pObj.checked){
                                pObj.checked=true;
                            }
                            if(selectedAreaArray[aid].isAllChild){
                                objs[i].setAttribute('checkedChild',true);   
                            }
                        }else{
                            objs[i].checked=pObj.checked;  
                        } 
                        objs[i].setAttribute('isLoadSelected',true);   
                    }else{                                    
                        objs[i].checked= isCheckedChild && pObj.checked;                                    
                        objs[i].setAttribute('checkedChild',isCheckedChild);           
                    }                  
                    setChildAreaChecked(objs[i],isCheckedChild);
                }
            }
        }
    }
    function changeAreaSelected(obj,isCheckedChild)
    {  
        if(!obj) return;
        if(obj.checked){                  
            var pObj=null;            
            var bid=obj.getAttribute('belongArea');
            while(bid!=null && bid>0){
                pObj=docEle('select_'+bid);                    
                if(pObj){
                    bid=pObj.getAttribute('belongArea');   
                    if(!pObj.checked){
                        pObj.checked=true;
                    }         
                    obj.setAttribute('checkedChild',false);          
                }else{
                    bid=0;
                }            
            }      
            if(isCheckedChild){
                obj.setAttribute('checkedChild',true);      
            }
        }else{
            obj.setAttribute('checkedChild',false);
        }
        setChildAreaChecked(obj,obj.getAttribute('checkedChild'));
    }
    var objParentAreaIdList=false;
    var objParentAreaNameList=false;
    var selectedAreaArray={};
    function initControls()
    {
        var hid=location.href.match(/(?!=\?|&)(hid=)([^=&?,]*)/i);  
        var sid=location.href.match(/(?!=\?|&)(sid=)([^=&?,]*)/i);  
        var selCount=0;
        if(hid!=null && hid.length>2){
            objParentAreaIdList=parent.document.getElementById(hid[2]) || false;         
            if(objParentAreaIdList && objParentAreaIdList.value!=''){
                var ids=objParentAreaIdList.value.split('|');
                if(ids!=null && ids.length>0){
                    for(var i=0;i<ids.length;i++){
                        var area=ids[i].split(':');
                        if(area!=null && area.length>1){
                            var aid=parseInt(area[0]);
                            var bid=parseInt(area[1]);
                            var isAllChild=area.length>2 && parseInt(area[2])==1;                        
                            if(aid>0 && bid>=0 && selectedAreaArray[aid]==null){
                                selCount++;
                                selectedAreaArray[aid]={'id':aid,'belong':bid,'isAllChild':isAllChild};
                            }              
                        }
                    }
                }
            }
        }      
        if(selCount>0 && selectedAreaArray){
            var areas=document.getElementsByName('areaSelected');
            if(areas!=null && areas.length>0){
                for(var i=0;i<areas.length;i++){
                    var aid=areas[i].value;
                    if(aid>0 && selectedAreaArray[aid]!=null){
                        areas[i].checked=true;
                        if(selectedAreaArray[aid].isAllChild){
                            areas[i].setAttribute('checkedChild',true);
                        }
                    }
                }
            }
        }
        if(sid!=null && sid.length>2){
            objParentAreaNameList=parent.document.getElementById(sid[2]) || false;         
        }
    }
    function copyChildArray(bid,toArray){    
        for(var a in selectedAreaArray){        
            if(selectedAreaArray[a]&&selectedAreaArray[a].belong==bid){
                toArray[a]=selectedAreaArray[a];
                toArray=copyChildArray(toArray[a].id,toArray);
            }
        }
        return toArray;
    }
    function checkAreaSeletedChanged()
    {
        if(objParentAreaIdList || objParentAreaNameList){                        
            var areas=document.getElementsByName('areaSelected');
            if(areas!=null && areas.length>0){
                var selAreaArray={};
                var nameList='';
                for(var i=0;i<areas.length;i++){
                    if(areas[i].checked){     
                        var aid=areas[i].value;
                        var bid=areas[i].getAttribute('belongArea')||-1;  
                        var checkedChild=areas[i].getAttribute('checkedChild');
                        checkedChild=checkedChild!=null && (checkedChild==true || checkedChild=='true');
                        var hasChild=areas[i].getAttribute('hasChild');      
                        hasChild=hasChild!=null && (hasChild==true || hasChild=='true');                
                        var isOpenChild=false;
                        if(aid>0 && bid>=0 && selAreaArray[aid]==null){                             
                            if(!hasChild){                      
                                isOpenChild=true;                                
                            }else{
                                for(var j=0;j<areas.length;j++){
                                    var cbid=areas[j].getAttribute('belongArea')||-1;  
                                    if(j!=i && cbid==aid){
                                        isOpenChild=true;            
                                        break;
                                    }
                                }                                
                            }        
                            selAreaArray[aid]={'id':aid,'belong':bid,'isAllChild':checkedChild && !isOpenChild};                   
                            if(!selAreaArray[aid].isAllChild && selectedAreaArray[aid]!=null){                                  
                                if(selectedAreaArray[aid].isAllChild && !isOpenChild){
                                    selAreaArray[aid].isAllChild=true;
                                }else if(!isOpenChild){
                                    selAreaArray=copyChildArray(selAreaArray[aid].id,selAreaArray);
                                }
                            }      
                            var title=''                 
                            if(bid==0){
                                title=areas[i].getAttribute('title')||''; 
                            }
                            while(bid>0 && selAreaArray[bid]==null){                             
                                var pObj=docEle('select_'+bid);                    
                                if(pObj){
                                    bid=pObj.getAttribute('belongArea')||-1;         
                                    if(bid>=0){
                                        if(bid==0){
                                            title=pObj.getAttribute('title')||''; 
                                        }                 
                                        var id=parseInt(pObj.value);                           
                                        selAreaArray[id]={'id':id,'belong':bid,'isAllChild':false};                                                            
                                    }else{
                                        break;
                                    }
                                }else{
                                    bid=0;
                                }           
                            } 
                            if(title!=''){
                                nameList+=(nameList==''?'': ',') + title;
                            }
                        }                        
                    }else if(selectedAreaArray[aid]!=null){
                        selectedAreaArray[aid]=null;
                    }
                }                
                if(objParentAreaIdList){
                    var ids='';
                    for(var id in selAreaArray){
                        ids+=(ids==''?'':'|')+id+':'+selAreaArray[id].belong+(selAreaArray[id].isAllChild?':1':'');
                    }
                    objParentAreaIdList.value=ids;                  
                }
                if(objParentAreaNameList){
                    objParentAreaNameList.value=nameList;                    
                }
            }
        }
        parent.popup_close();
        return false;
    } 
</script>
</head>
<body onload="initControls();">
<form action="selectMultiArea.aspx" id="form1" method="post">  
	<table id="showAreaList" style="margin:0;" border="0" cellpadding="0" cellspacing="0" class="forarea">	    
	<tbody>		    
	    <%=PPFOR_showAreaList %>
	    <tr>
            <td align="center" style="height:30px; display:none;">            
                <input type="button" id="select" value="确定" onclick="checkAreaSeletedChanged();return false;" class="inputbutton" onmouseover="this.className='inputbutton_hover'" onmouseout="this.className='inputbutton'"/>
                <input type="button" id="cancel" onclick="parent.popup_close();" value="取消" class="inputbutton" onmouseover="this.className='inputbutton_hover'" onmouseout="this.className='inputbutton'"/>               
                <script type="text/javascript"> 
                    parent.popup_clearButtons();
                    parent.popup_addButton(document.getElementById('select'));
                    parent.popup_addButton(document.getElementById('cancel'));
                </script>
            </td>
        </tr> 
	</tbody>
	</table>   
</form>        
<div id="msg" style="position: absolute; z-index: 300; height: 120px; width: 204px; left: 50%; top: 50%; margin-left: -100px; margin-top: -80px; display:none;">
	<div id="Layer2" style="position: absolute; z-index: 300; width: 190px; height: 90px; background-color: #ffffff; border: 1px solid #79BDD3; font-size: 14px;">
		<div id="Layer4" style="height: 26px; background: none repeat scroll 0% 0% rgb(241, 245, 250); line-height: 26px; padding: 0px 3px; font-weight: bold; color:#0099CC">正在加载...</div>
		<div id="Layer5" style="height: 64px; line-height: 150%; padding: 0px 3px; text-align:center"><br/><img src="/Admin/Images/loading.gif" alt=""/><br/></div>
	</div>
	<div id="Layer3" style="position: absolute; width: 190px; height: 90px; z-index: 299; left: 4px; top: 5px; background-color: rgb(232, 232, 232);"></div>
	</div>
</body>
</html>