
function LoadBody()
{
	BuildHTML();
}

function GetFromXML(s_name)
{
	var re = new RegExp("<"+s_name+">((.|\n)*?)</"+s_name+">");
	re.exec($('txtclass').value);
	return 	RegExp.$1;
}

function GetValue(i_index)
{
	var s_name=GetFromXML("name"+i_index);
	var re = new RegExp("<"+s_name+">((.|\n)*?)</"+s_name+">");
	re.exec($P("C_Compare").value);
	return 	RegExp.$1;
}

function BuildHTML()
{
	var s_text=$('txtsource').value;
	var s_temp="";
	var i=0;
	var i_max=parseInt(GetFromXML("class"));
	var s_trtype="";
	
	
	//天数
	s_temp="";
	for(i=0;i<i_max;i++)
	{
		var s_input="";
		var s_type=GetFromXML("select"+i);
		var s_check="0";
		var s_default="";
		
		if($P("C_Compare").value=="")
		{
			//默认值
			s_default=GetFromXML('value'+i).replace(/，/g,",");
		}
		else
		{
			s_default=GetValue(i);
			
		}
			
		
		
		switch(s_type)
		{
			case "1"://文本
			{
				s_input="<input type=\"text\" class=\"inputbox\" id=\"txt"+i+"\" value=\""+s_default+"\"  />";
				break;
			}
			case "2"://选择
			case "5"://选择
			{
				
				var s_con=GetFromXML('detail'+i).split(/[\r\n]/g);
				
				var s_value="";
				for(j=0;j<s_con.length;j++)
				{
					var s_select="";
					s_default=s_default.replace(' ',',');
					var s_spt=s_default.split(',')
					for(k=0;k<s_spt.length;k++)
					{
						if(s_con[j]==s_spt[k])
						{
							s_select=" selected='true'";
							break;
						}	
					}	
					
					s_value+="<option value='"+s_con[j]+"' "+s_select+">"+s_con[j]+"</option>"; 
				
				}
				
				s_input="<input type=\"text\" class=\"inputbox\" id=\"txt"+i+"\" value=\""+s_default.replace(/,/g,' ')+"\"  /><br />";
				s_input+="<select style='margin:5px 0px 3px 0px;' "+(s_type=="5"?"multiple='true' size='6'":"")+" id=\"sel"+i+"\">"+s_value+"</select><br /><input type='button' class='button' value='添加' onclick='SetValue("+i+")' />";
				break;
			}
			case "3"://大文本
			{
				s_input="<textarea id=\"txt"+i+"\" class=\"inputbox\">"+s_default+"< /textarea>"
				break;
			}
		}
		
		if(GetFromXML('check'+i)=="on")
		{
			s_check="1";
		}
		
		
		
		if(s_trtype!=GetFromXML('type'+i))
		{
			s_trtype=GetFromXML('type'+i);

			s_temp+="<tr><td colspan='2' class='tleft'><b>"+s_trtype+"</b></td></tr>";
		}
		
		s_temp+="<tr><td class='tright' id='tr"+i+"' type='"+GetFromXML("select"+i)+"' check='"+s_check+"'>"+GetFromXML("name"+i)+"：</td><td class='tleft'>"+s_input+"</td></tr>";
	}
	
	s_text=s_text.replace("@daydetal@",s_temp);
	
	$('divdetail').innerHTML=s_text;	
	
	//之前保存的要能够读取
	
}

function SetValue(i_index)
{
	var s_value="";
	for(j=0;j<$('sel'+i_index).options.length;j++)
	{
		if($('sel'+i_index).options[j].selected)
		{
			s_value+=$('sel'+i_index).options[j].value+" ";
		}
	}
	$('txt'+i_index).value=s_value;
}

function SubmitData()
{
	var i_max=$('itemtable').rows.length
	
	var s_text="";
	
	
	var i_number=0;
	for(i=0;i<i_max;i++)
	{
		var trobj=$('itemtable').rows[i];
		var s_value="";
		
		var s_name=trobj.cells[0].innerHTML.replace("：","");
		var td=trobj.cells[0];
		
		if(td.getAttribute("type")!=null)
		{
			switch(td.getAttribute("type"))
			{
				case "1":
				case "4":
				case "3":
				case "2":
				case "5"://多选
				{
					//文本
					s_value=$('txt'+i_number).value;
					i_number++;
					break;
				}
				
				
			}
			s_text+="<"+s_name+">"+ s_value +"</"+s_name+">";
		}
	}
	
	
	$P("C_Compare").value=s_text;
	parent.popup_close();
}