//ajax common function 2006-5-29
//var Arithmometer = function(){};
//Arithmometer.prototype.Get(articleid)=function{}
var CMS = {
	confirm:{
		d:'确定删除吗？',
		s:'请选中要操作的记录！'
	},
	arrSelectedID: [],
	win: null,
	windownConfig: {
		className: 'alphacube',
		title: '',
		width:650,
		height:480,
		//draggable: false,
		//minimizable: false,
		showEffect:Element.show,
		hideEffect:Element.hide,
		onShow: function(){
		    if(CMS.win.isMaximized()){
		        CMS.win.maximize()
		    }
		}
	},
	GetWin: function(){
		if(CMS.win == null){
			CMS.win = new Window('1', CMS.windownConfig)
		}
		return CMS.win
	},
	Checkblank: function(array){
		for(var i=0; i<array.length; i++){
			var o = $(array[i])
			if(o && o.value.length==0){
				alert(o.title + "不能为空")
				o.focus()
				return false
			}
		}
		return true
	}
}

var tree = {
	id: 0,
	win: null,
	url: '',
	callback: null,
	SetCallback: function(f){
		tree.callback = f
	},
	LoadRoot: function(e){
		var e = $('divLeftR3')
		var paras = 'SiteID=' + tree.siteid
		update(e, paras, tree.url)
	},
	LoadSub: function(parentID){
	   
		if(parentID){
			this.id = parentID
		}
		if(this.span){
			this.span.className = ''
		}
		this.span = $('sp_'+parentID)
		this.span.className = 'selected'
		var e = $('ul_' + parentID)
		if(e == null){
			return
		}
		if(e.loaded == 1){
			e.className = e.className=='show'?'hide':'show'
			return
		}else{
			e.className = 'show'
		}
		var paras = 'parentID=' + parentID
		paras += '&SiteID=' + tree.siteid
		e.className = 'show'
		update(e, paras, tree.url)
	},
	ReLoad: function(){
	    if(this.id == 0){
	        this.LoadRoot()
	        return
	    }
	    var e = $('ul_' + this.id)
	    if(e){
	        //alert('当前选中节点：'+this.id)
            e.loaded = 0
            this.span.click()
            this.span.click()
            
	    }else{
	        var pe = $('sp_' + this.span.pid)
	        if(pe){
	            //alert('父节点：'+pe.chid)
	            var ul = $('ul_' + this.span.pid)
	            ul.loaded = 0
	            this.span = pe
	            
	            this.span.click()
	            this.span.click()
	        }else{
	            tree.LoadRoot()
	        }
	    }
	},
	
	Add: function(url){
	    var win =  CMS.GetWin()
	    url += '&pdeptid=' + tree.id
	    url += '&id=' + tree.id + '&' + Math.random()
		win.setURL(url)
		win.showCenter(true)
		win.refresh()
	},
	
	Edit: function(url){
	    if(tree.id == 0){
	        alert(CMS.confirm.s)
			return
		}
		var win = CMS.GetWin()
		url += '&bap_sys_id=' + tree.id
		url += '&pdeptid=' + tree.id
		url += '&id=' + tree.id + '&' + Math.random()
		win.setURL(url)
		win.showCenter(true)
		win.refresh()
	},
	Delete: function(url){
		if(!tree.id){
			alert(CMS.confirm.s)
			return
		}
		if(!confirm(CMS.confirm.d)){
			return
		}
		var paras = 'ids=' + tree.id
		paras += '&bap_sys_id=' + tree.id
		request(url, paras, tree.callback||function(request){
		    var e = $('li_' + tree.id) || $(tree.span.parentElement)
		    e.remove()
		})
		//清空记录 
		tree.id = 0
	}
}

var list = {
	url: '',
	parentID: '',
	pageno: 1,
	arrSelectedID:[],
	win:null,
	TurnPageParameter:{},
	Reset: function(){
		list.url = ''
		list.parentID = ''
		list.pageno = 1
		list.arrSelectedID = []
	},
	SelectAll: function(){
		var e = Event.element(event)
		var t = $('tdRight')
		var arr = t.getElementsByTagName('input')
		for(var i=0; i<arr.length; i++){
			if(arr[i].disabled || arr[i]==e || arr[i].checked==e.checked || arr[i].type=='button'){
				continue
			}
			arr[i].click()
		}
	},
	Select: function(){
	  
		var checkbox = Event.element(event)
		list.checkbox = checkbox
		var chid = checkbox.name.replace('Object_', '')
		var index = list.arrSelectedID.indexOf(chid)
		
		if(checkbox.checked){
			if(index == -1){
				list.arrSelectedID.push(chid)
			}
		}else{
			list.arrSelectedID[index] = 0
		}
		var arr = []
		for(var i=0; i<list.arrSelectedID.length; i++){
			if(list.arrSelectedID[i] != 0){
				arr.push(list.arrSelectedID[i])
			}
		}
		list.arrSelectedID = arr
		//alert(arr)
	},
	TurnPage: function(parentID, pageno, url, turnPageParameter, divContainer){
	    //alert(arguments.length)
	    //alert(list.url)
	    if(parentID){
			list.parentID = parentID
		}
		if(pageno){
			list.pageno = pageno
		}
		if(url){
			list.url = url
		}
		var e = $('tdRight')
		if(divContainer){
		    e = $(divContainer)
		}
		var paras = 'parentID=' + list.parentID + '&pageno=' + list.pageno
		paras += '&channelid=' + list.parentID
		paras += '&SiteID=' + tree.siteid
		if(arguments.length > 3){
		    list.TurnPageParameter = arguments[3]
		}
		var o = list.TurnPageParameter
	    for(p in o){
	        paras += '&' + p + '=' + o[p]
	    }
		
		//alert(list.url)
		//alert(list.TurnPageCallback)
		update(e, paras, list.url, list.TurnPageCallback)
		list.arrSelectedID = []
	},
	Add: function(url){
	    //alert(url)
		var win = CMS.GetWin()
		url += '&parentID=' + list.parentID
		win.setURL(url)
		win.showCenter(true)
		win.refresh()
	},
	Edit: function(url){
	    if(list.arrSelectedID.length==0){
			alert(CMS.confirm.s)
			return
		}
		var win = CMS.GetWin()
		url += '&parentID=' + list.arrSelectedID[list.arrSelectedID.length-1]
		url += '&bap_sys_id=' + list.arrSelectedID[list.arrSelectedID.length-1]
		url += '&id=' + list.arrSelectedID[list.arrSelectedID.length-1]
		url += '&' + Math.random()
		win.setURL(url)
		win.showCenter(true)
		win.refresh()
	},
	Delete: function(url){
		if(list.arrSelectedID.length==0){
			alert(CMS.confirm.s)
			return
		}
		if(!confirm(CMS.confirm.d)){
			return
		}
		var paras = 'ids=' + list.arrSelectedID.join(',')
		//alert(paras)
		request(url, paras, function(request){list.TurnPage(list.parentID, list.pageno, list.url)})
		//list.arrSelectedID = []
	}
}

function request(url, paras, onComplete){
	var myajax = new Ajax.Request(
		url,
		{method:'post', parameters:paras+'&'+Math.random(), onComplete:onComplete?onComplete:function(request){}}
		//{method:'post', parameters:paras+'&'+Math.random(), onComplete:function(request){alert(request.responseText)}}
	)
}

function update(e, paras, url, onComplete){
	var myajax = new Ajax.Updater(
		{success:e},
		url,
		{method:'get', parameters:paras+'&'+Math.random(), onComplete:onComplete?onComplete:function(request){
			e.loaded = 1
			//alert(request.responseText)
		}}
	)
}

function siteSelectAll(){
    var e = Event.element(event)
    var t = $('tdTree')
    var arr = t.getElementsByTagName('input')
    for(var i=0; i<arr.length; i++){
        arr[i].checked = e.checked
	}
}

function reportError(request){
    var errorHtml
    if(request.responseText)
        errorHtml = request.responseText
    else
	    errorHtml = request
	var blankwindow = window.open()
	blankwindow.document.write('<html><body>'+errorHtml+'</body></html>')
	blankwindow.document.onkeydown = function(){
	    if(blankwindow.event.keyCode == window.Event.KEY_ESC) blankwindow.close()
	}
}

Ajax.Responders.register({
	onCreate: function() {
	    var arr = arguments
	    //var request = arr[0]
	    //the tracks should below to the current request
	    //setTimeout(Track(request), 2000)
		Element.show('divStatus')
		//window.status = '正在处理，请耐心等候...'
	},
	onComplete: function() {
		Element.hide('divStatus')
		//window.status = ''
	}
})

function Track(request){
    //alert(request.url)
    //alert(Object.keys(request.options))
    //function(){if(Element.visible('divStatus')){alert('there is something wrong in the process ...')}}
}


function CallMethod(methodName, callback){
    var url = '/bdmcs/ajax/paste.do?template=/ajax/component/callmethod.htm'
    var paras = 'methodName=' + methodName
    request(url, null, callback)
}