/*
+--------------------------------------------------------
+	Copyright (c) 2004-2007 WeSofts
+	JavaScript Project for wespace
+	http://www.wesofts.com
+--------------------------------------------------------
*/
function category_object(){
	this.deflang = '--请选择--';
	this.periods = 1950;
	this.periode = 2010;
	this.simple = function(id,cateid,key,val,ismul){
		
		if (typeof categorys == 'undefined' || typeof categorys[cateid] == 'undefined'){
			return false;
			
		}
		if (!$(id)){
			return false;
			
		}
		var strs = categorys[cateid].split('^');
		if (ismul){
			var opts = '<select name="' + key + '" class="mulsel" size="5">';
		} else {
			var opts = '<select name="' + key + '"><option value="0">' + this.deflang + '</option>';
		}
		for (var i = 0; i < strs.length; i++){
			var tmps = strs[i].split(',');
			if (tmps[0] == val && !ismul){
				opts += '<option value="' + tmps[0] + '" selected>' + tmps[1] + '</option>';
			} else {
				opts += '<option value="' + tmps[0] + '">' + tmps[1] + '</option>';
			}
		}
		opts += '</select>';
		$(id).html(opts);
	}
	//增加选择
		this.radio = function(id,cateid,key,val){
		
		if (typeof categorys == 'undefined' || typeof categorys[cateid] == 'undefined'){
			return false;
			
		}
		if (!$(id)){
			return false;
			
		}
		var strs = categorys[cateid].split('^');
		var opts ='';
		for (var i = 0; i < strs.length; i++){
			var tmps = strs[i].split(',');
			if (tmps[0] == val){
				opts += '<label class="fm-radio checked" for="'+cateid+tmps[0]+'"><input type="radio" id="'+cateid+tmps[0]+'"class="radio" value="' + tmps[0] + '"name="'+key+'" checked="checked"/>' + tmps[1] + '</label>';
			} else {
				opts += '<label class="fm-radio" for="'+cateid+tmps[0]+'"><input type="radio" id="'+cateid+tmps[0]+'"class="radio" value="' + tmps[0] + '"name="'+key+'" />' + tmps[1] + '</label>';
			}
		}
		$(id).html(opts);

	}
//增加 by iwege end

//checkbox  by iwege
this.checkbox = function(id,cateid,key,val){
		
		if (typeof categorys == 'undefined' || typeof categorys[cateid] == 'undefined'){
			return false;
			
		}
		if (!$(id)){
			return false;
			
		}
		var strs = categorys[cateid].split('^');
		var opts ='';
		var vals = val.split('|');
		for (var i = 0; i < strs.length; i++){
			var tmps = strs[i].split(',');
			if (vals.in_array(tmps[0])){
				opts += '<label class="fm-radio checked" for="'+cateid+tmps[0]+'"><input type="checkbox" id="'+cateid+tmps[0]+'"class="checkbox" value="' + tmps[0] + '"name="'+key+'" checked="checked"/>' + tmps[1] + '</label>';
			} else {
				opts += '<label class="fm-radio" for="'+cateid+tmps[0]+'"><input type="checkbox" id="'+cateid+tmps[0]+'"class="checkbox" value="' + tmps[0] + '"name="'+key+'" />' + tmps[1] + '</label>';
			}
		}
		$(id).html(opts);

	}

	//checkbox end


	this.double = function(id,cateid,key,val,ismul){
		if (typeof categorys == 'undefined' || typeof categorys[cateid] == 'undefined'){
			console.debug(1);
			return false;
		}
		if (!$(id)){
			return false;
		}
		ismul = ismul ? 1 : 0;
		var keys = key.split('|');
		var vals = val.split('|');
		var strs = categorys[cateid].split('^');
		
		var ops0 = '<select name="' + keys[0] + '" onchange="category.double(\'' + id + '\', \'' + cateid + '\', \'' + key + '\', this.value + \'|0\',' + ismul + ')" ' + (ismul == 1 ? 'class="mulselall"' : '') + '><option value="0">' + this.deflang + '</option>';
		var ops1 = ismul == 1 ? '<br /><select name="' + keys[1] + '" class="mulsel" size="4">' : '<select name="' + keys[1] + '"><option value="0">' + this.deflang + '</option>';
		for (var i = 0; i < strs.length; i++){
			var tmps = strs[i].split(',');
			if (tmps[0] == 0){
				// First Stage
				if (tmps[1] == vals[0]){
					ops0 += '<option value="' + tmps[1] + '" selected>' + tmps[2] + '</option>';
				} else {
					ops0 += '<option value="' + tmps[1] + '">' + tmps[2] + '</option>';
				}
			} else if (tmps[0] == vals[0]) {
				// Second Stage
				if (tmps[1] == vals[1]){
					ops1 += '<option value="' + tmps[1] + '" selected>' + tmps[2] + '</option>';
				} else {
					ops1 += '<option value="' + tmps[1] + '">' + tmps[2] + '</option>';
				}
			}
		}
		ops0 += '</select> ';
		ops1 += '</select> ';
		$(id).html(ops0+ops1);
	}
	this.mulity = function(id,cateid,key,single){
		if (single){
			var keys = key + '_s';
			this.simple(id,cateid,keys,'0',true);
		} else {
			var keys = key + '_f|' + key + '_s';
			this.double(id,cateid,keys,'0|0',true);
		}
				
	}
	this.period = function(id,key,val){
		if (!$(id)){
			return false;
		}
		var keys = key.split('|');
		var vals = val.split('|');
		var html = '<select name="' + keys[0] + '"><option value="0">年份</option>';
		for (var y = this.periode; y >= this.periods; y--){
			if (y == vals[0]){
				html += '<option value="' + y + '" selected>' + y + '</option>';
			} else {
				html += '<option value="' + y + '">' + y + '</option>';
			}			
		}
		html += '</select>';
		if (keys.length > 1){
			html += ' <select name="' + keys[1] + '"><option value="0">月份</option>';
			for (var m = 1; m <= 12; m++){
				if (m == vals[1]){
					html += '<option value="' + m + '" selected>' + m + '</option>';
				} else {
					html += '<option value="' + m + '">' + m + '</option>';
				}			
			}
			html += '</select>';
			if (keys.length >= 3){
				html += ' <select name="' + keys[2] + '"><option value="0">' + this.deflang + '</option>';
				for (var d = 1; d <= 31; d++){
					if (d == vals[2]){
						html += '<option value="' + d + '" selected>' + d + '</option>';
					} else {
						html += '<option value="' + d + '">' + d + '</option>';
					}			
				}
				html += '</select>';
			}
		}
		$(id).html(html);
	}
	this.muladd = function(theform,fer,ter,noticeid){
		this.maxiums =5;
		this.foundeds = 0;
		this.selectedid = 0;
		fer += '_s';
		for (var i = 0; i < theform.elements.length; i++){
			if (theform.elements[i].name == fer){
				var objleft = theform.elements[i];
			}
			if (theform.elements[i].name == ter){
				var objright = theform.elements[i];
			}
		}
		if (typeof objleft == 'undefined' ||typeof objright == 'undefined'){
			return false;
		}
		if (objright.length >= this.maxiums){
            $(noticeid).html('最多只能选择5个.');
			return false;
		}else{
            $(noticeid).html('您已经选了'+(objright.length+1)+'个,最多只能选择5个');
            $(noticeid).parent().removeClass('fm-error');
        }
		this.leftid = -1;
		this.rightid = 0;
		for (var j = 0; j < objleft.length; j++){
			if (objleft[j].selected == true){
				this.leftid = j;
				this.selectedid = j +1;
				if (this.selectedid < objleft.length){
					objleft[this.selectedid].selected = true;
				}
				this.leftid = j;
				for (var m = 0; m < objright.length;m++){
					if (objright[m].value == objleft[j].value){
						this.foundeds++;
						break;
					}
				}
				break;
			}
		}
		if (this.foundeds > 0){
			$(noticeid).html('该选项已经存在');
			return false;
		}else{
			 if (this.leftid >= 0){
				this.rightid = objright.length;
				objright.options[this.rightid] = new Option(objleft.options[this.leftid].text,objleft.options[this.leftid].value);
			}else if (objleft.length > 0){
				objleft[0].selected = true;
			}
		}
	}
	this.muldel = function(theform,wraper,noticeid){
		this.deleteid = 0;
		for (var i = 0; i < theform.elements.length; i++){
			if (theform.elements[i].name == wraper){
				var objright = theform.elements[i];
			}
		}
		if (typeof objright == 'undefined'){
			return false;
		}
		for (var j = 0; j < objright.length; j++){
			if (objright[j].selected == true){
				this.deleteid = j;	//6,5
				objright.options[j] = null;
				break;
			}
		}
		this.lengther = objright.length;
		if (this.lengther > 0){
			if (this.deleteid >= this.lengther){
				this.deleteid = this.lengther - 1;
			}
			objright[this.deleteid].selected = true;
		}
        $(noticeid).parent().removeClass('fm-error');
        $(noticeid).html('您已经选了'+this.lengther+'个,最多只能选择5个');
	}
	//让所有的下拉框中的元素选中
	this.mulitys = function(selecters){
		this.selecters = selecters.split('|');
        for (var i = 0;i < this.selecters.length;i++ )
        {
          this.selecter = $(this.selecters[i]+' > option');
          if (this.selecter.length == 0) {
               
                        $(this.selecters[i]).parent().parent().addClass('fm-error');
                   
                }else{
                    for (var j = 0; j < this.selecter.length; j++)
                    {
                            this.selecter[j].selected = true;

                    }
                     $(this.selecters[i]).parent().parent().removeClass('fm-error');
             }
        }
       }
	this.mulitysno = function(selecters){
		this.selecters = selecters.split('|');
 
        for (var i = 0;i < this.selecters.length;i++ )
        {
          this.selecter = $(this.selecters[i]+' > option');

						for (var j = 0; j < this.selecter.length; j++)
						{
										this.selecter[j].selected = true;

						}
              $(this.selecters[i]).parent().parent().removeClass('fm-error');
             }
        }
       
			 
}
category = new category_object();
var categorys = new Array();