<?php
	if( !is_array($active) ){
		ob_clean();
		header("Location: index.php?id=".$fid);	
	}
?>
<script type="text/javascript">
Mo.ready(
	function(){
		Mo("#form").bind('submit',function(){
			var result= Mo.Form( this, function(i){
				/*未加载 Effect 组件*/
				if(!Mo.plugins["effect"]){
					alert(i);
				}else{
					Mo.Message('error','error',i,3);
				}

			});
			return result;
		});
	}
);
</script>

<?php
	
	echo loader_script(array(VI_BASE."js/mo.calendar.js"),"utf-8",$Project["version"]);

	$form = "<form id='form' name='form' method='post' action='?action=post&id=".$VICACHE["mod.forms"]["id"]."'>";
	
	$form .= "<input type='hidden' value='".rand_string(10)."' name='verify' />";
	$form .= "<input type='hidden' value='".$invite."' name='invite' />";
	
	//随机排序
	if( $config["FORM_RAND_GROUPS"] == "Y" ){	
		$VICACHE["mod.forms"]["groups"] = rand_array($VICACHE["mod.forms"]["groups"]);
	}
	
	if( $config["FORM_RAND_OPTION"] == "Y" ){	
		$VICACHE["mod.forms"]["options"] = rand_array($VICACHE["mod.forms"]["options"]);
	}
	
	foreach( $VICACHE["mod.forms"]["groups"] as $gid => $group ){
		
		$item = '';				
		$temp = 'G-'.$gid;
		$config = json_decode($group["config"],true);
		
		if ( !in_array($group["type"],array("text","textarea","file")) ){
			
			switch($group["type"]){
				case 'select':
					$item .= "<select id='".$temp."' name='".$temp."' fix_name='".$group["name"]."' ".($config["GROUP_MUST"]?"fix_null='yes'":'').">";
				break;
			}
			
			//子选项
			$x = 1;
			foreach( $VICACHE["mod.forms"]["options"] as $oid => $option ){
				
				if( $gid == $option["gid"] && $option["status"] ){
					
					//类型换算
					switch($group["type"]){
						case 'radio':
							$item .= "<label for='".$temp."-".$oid."'><input type='radio' class='radio' name='".$temp."' id='".$temp."-".$oid."' value='".$oid."' fix_name='".$group["name"]."' ".($config["GROUP_MUST"]?"fix_null='yes'":'')." /> ".$option["name"]."</label>";
						break;
							
						case 'checkbox':
							$item .= "<label for='".$temp."-".$oid."'><input type='checkbox' class='checkbox' name='".$temp."[]' id='".$temp."-".$oid."' value='".$oid."' fix_name='".$group["name"]."' ".($config["GROUP_MAX"]?"fix_max='".$config["GROUP_MAX"]."'":'')." ".($config["GROUP_MIN"]?"fix_min='".$config["GROUP_MIN"]."'":'')." /> ".$option["name"]."</label>";
						break;
							
						case 'select':
							$item .= "<option value='".$oid."'>".$option["name"]."</option>";
						break;
					}
					
					if( $config["GROUP_BREAK"] && $config["GROUP_BREAK"] == $x ){
						$item .= "<br />";
						$x = 1;
					}else{
						$x ++;
					}						
					
				}				
			}				
			
			switch($group["type"]){
				case 'select':
					$item .= "</select>";
				break;
			}
			
		}else{
			
			//类型换算
			switch($group["type"]){
					
				case 'text':
				
					$v = array('idcard'=>"fix_card='yes'",'email'=>"fix_email='yes'",'number'=>"fix_number='yes'",'datetime'=>"fix_date='yes' readonly='true' onclick='return showCalendar(this.id,\"y-mm-dd\");' title='年-月-日'",''=>'');
				
					$item .= "<input type='text' name='".$temp."' id='".$temp."' value='' fix_name='".$group["name"]."' ".(isset($config["GROUP_MUST"])?"fix_null='yes'":'')." ".(isset($config["GROUP_VERIFY"])?$v[$config["GROUP_VERIFY"]]:"")." ".(isset($config["GROUP_SIZE"])?"size='".$config["GROUP_SIZE"]."'":'')." />";
				break;
					
				case 'textarea':
					$item .= "<textarea name='".$temp."' id='".$temp."' cols='50' rows='4' fix_name='".$group["name"]."' ".($config["GROUP_MUST"]?"fix_null='yes'":'')."></textarea>";
				break;
					
				case 'file':
				
					//上传表单
					$form = str_replace("name='form' method='post'","name='form' enctype='multipart/form-data' method='post'",$form);
					
					$item .= "<input name='".$temp."' id='".$temp."' type='file' size='50' value='' fix_name='".$group["name"]."' ".(isset($config["GROUP_MUST"])?"fix_null='yes'":'')." ".($config["GROUP_UPLOAD"]?"fix_extension='".$config["GROUP_UPLOAD"]."'":'')." /><p>支持的文件类型：".$config["GROUP_UPLOAD"]."</p>";
				break;
			}
			
			
		}
		
		
		$form .= '
		<dl id="group-'.$gid.'" class="'.$group["type"].'">
			<dt>'.$group["name"].' '.($config["GROUP_MUST"]?'*':'').'</dt>
			<dd>
				'.($group["description"]?'<p>'.str_replace(chr(13),'<br />',ubb_basic($group["description"])).'</p>':'').'
				'.$item.'
			</dd>		
		</dl>	
		';
		
	}
	
	//使用匿名扩展模式
	//!$_SESSION["Member"]["id"] &&
	if(  $config['USER_MODE'] == "ANY" && ( array_key_exists( "ANY_ACCOUNT", $config ) || array_key_exists( "ANY_NAME", $config ) || array_key_exists( "ANY_IDCARD", $config ) || array_key_exists( "ANY_PHONE" , $config ) ) ){
		
		$member = $_SESSION["Member"]["id"] ? passport_member( $_SESSION["Member"]["id"] ) : array();
		
		$extra = '
		<p>
			'.( $config["ANY_ACCOUNT"] ? '用户名：<input type="text" size="20" name="ACCOUNT" class="text" value="'.$member["account"].'" fix_name="您的用户名" fix_null="yes" />' : '' ).'
			'.( $config["ANY_NAME"] ? '姓名：<input type="text" size="20" name="NAME" class="text" value="'.$member["name"].'" fix_name="您的姓名" fix_null="yes" />' : '' ).'
			'.( $config["ANY_PHONE"] ? '电话：<input type="text" size="20" name="PHONE" class="text" value="'.$member["phone"].'" fix_name="您的电话" fix_null="yes" fix_number="yes" />' : '' ).'
			'.( $config["ANY_IDCARD"] ? '身份证：<input type="text" size="20" name="IDCARD" class="text" value="'.$member["idcard"].'" fix_name="您的身份证" fix_card="yes" />' : '' ).'
		</p>
		';
	}
	
	$form .=  "
	<div id='send'>
		".$extra."
		<p>
		".( ( $VICACHE["mod.forms"]["start"]<time() && $VICACHE["mod.forms"]["expire"]>time() && $VICACHE["mod.forms"]["status"] ) ?"<input type='submit' class='submit' value='提交表单' />": "<input type='submit' class='submit' value='".($VICACHE["mod.forms"]["start"]>time()?"暂未启用":"表单过期")."' disabled='disabled' />") ."			
		
		".(empty($config["HIDE_RESULT"])?"<input type='button' class='submit' name='button' onclick='location.href=\"?mode=$mode&action=result&id=".$VICACHE["mod.forms"]["id"]."&skin=".$skin."#top\";' value='查看结果' />":"")."			
		</p>
	</div>";
		
	$form .=  "</form>";
	
	echo $form;

?>