/*
ajax分类联动菜单处理JS
ajaxCategory({id:0,elms:'parentid',selected:1})
函数名 ajaxCategory 
参数为对象
id:0 表示初始化从ID为0下的子类
elms:'parentid' 表示在ID为parentid中写入选中项目的值
selected:1 表示初始化后定位到项目值为selected的位置
*/
function ajaxCategory(objs){
		/*
		假设设置了selected值，表示初始选择一个分类
		*/
		if (objs.selected > 0 && objs.id==0){
			$.get("/categorys/ajax/",
           { cateid: objs.selected, act:objs.action },
            function(data){
				array = data.split(',');
				//alert(array);
				for(i=0;i<array.length;i++){
					ajaxCategory({id:array[i],pos:i,elms:objs.elms,lable:array})
				}
			}
			)
		}else{
			$.get("/categorys/ajax/",
           { cateid: objs.id, act: "showin" },
            function(data){
				var category = eval(data);
				var i;
				var content;
				var title = objs.pos ? '' : '';
				var elmsname = objs.elms ? objs.elms : 'parentid[]';	//判断是否设置input名称未设置则设置为默认
				var action = objs.action ? objs.action : 'add';
				var obj = document.getElementById('categorys').getElementsByTagName('select');
				var selects = obj.length;
				objs.lable = objs.lable ? objs.lable : 0;
				objs.pos = objs.pos ? objs.pos : 0;
				while( objs.pos < selects ){
					selects = selects-1;
					document.getElementById('categorys').removeChild(obj[selects]);
				}
				if ( (data.length>0 && objs.id>0 ) || obj.length==0){	//服务器有返回数据时
					content = '<select name="'+elmsname+'" onChange="ajaxCategory({id:this.value,pos:'+(objs.pos+1)+'})">'
					content = content + '<option value="0">'+title+'</option>';
					if (typeof(category) != 'undefined'){

						for(i=0;i<category.length;i++){
							if (inArray(objs.lable, category[i].id)==true ){
								content = content + '<option value="'+category[i].id+'" selected="selected" >'+category[i].title+'</option>';
							}else{
								content = content + '<option value="'+category[i].id+'">'+category[i].title+'</option>';
							}
						}
					}
					content = content + '</select>';
					$("#categorys").append(content);
				}
			}
        ) 
		}
}