<?php
	if( !is_array($active) ){
		ob_clean();
		header("Location: index.php?id=".$fid);	
	}
	
	//图片延时加载
	echo loader_script(array(VI_BASE."js/lazy.js"),"utf-8",$Project["version"]);
	
?>



<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;
		});
		
		////////////////////
		
		Mo("#form td").bind('click',function( ele, index, event ){		
			
			var o = ele;
			var e = event;			
			
			var input=o.getElementsByTagName('input')[0];
			switch(input.type){
				case 'radio':
					input.checked='true';
				break;
				case 'checkbox':
					var target=(e.srcElement ? e.srcElement : e.target);
					if(target.tagName=='TD'){
						(input.checked?input.checked='':input.checked='true');
					}else{
						return ;
					}
				break;
			}			
			
		});
		
		//加载图片
		LAZY.init();
		LAZY.run();
		
	}
);

</script>

<?php

	echo loader_script(array(VI_BASE."js/lazy.js"),$Project["charset"],$Project["version"]);

	/*******排序函数_开始*******/
	
	//从小到大排序
	function sort_asc($a,$b){
		if( $a["stat"] == $b["stat"] ){
			return 0;
		}else{
			return ($a["stat"]<$b["stat"]) ? -1 : 1;
		}
	}
	
	//从大到小排序
	function sort_des($a,$b){
		if( $a["stat"] == $b["stat"] ){
			return 0;
		}else{
			return ($a["stat"]>$b["stat"]) ? -1 : 1;
		}
	}
	
	/*******排序函数_结束*******/

	echo "<form id='form' name='form' method='post' action='?mode=$mode&action=post&id=".$VICACHE["mod.forms"]["id"]."'>";
	
	echo "<input type='hidden' value='".rand_string(10)."' name='verify' />";
	
	//随机排序
	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"]);
	}
	
	//排名排序
	switch( $config["FORM_SORT"] ){
		
		//从小到大
		case "ASC":
			uasort($VICACHE["mod.forms"]["options"],"sort_asc");
		break;
		
		//从大到小
		case "DES":
			uasort($VICACHE["mod.forms"]["options"],"sort_des");
		break;
		
	}
	
	//隐藏提交按钮
	$hitsub = true;
	
	foreach( $VICACHE["mod.forms"]["groups"] as $gid => $group ){
		
		$item = '';				
		$temp = 'G-'.$gid;
		$config = json_decode($group["config"],true);
		
		//非按钮
		if( $group["type"] != "button" ){
			$hitsub = false;	
		}
		
		//展示模板
		$template = $config["GROUP_STYLE"] ? $Modules["vote"]["style"][$config["GROUP_STYLE"]] : $config["GROUP_TEMPLATE"];
		
		//exit($template);
		
		//表格宽度
		$width = floor(100 / $config["GROUP_BREAK"]) . "%";
			
		//子选项
		$x = 0;
		foreach( $VICACHE["mod.forms"]["options"] as $oid => $option ){
			
			if( $gid == $option["gid"] && $option["status"] ){
				
				$item .= "<td width='".$width."' valign='top'>";
				
				//类型换算
				switch($group["type"]){
					case 'radio':
						$input = "<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'":'')." /></label>";
					break;
						
					case 'checkbox':
						$input = "<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"]."'":'')." /></label>";
					break;
						
					case 'button':
						$input = "<label for='".$temp."-".$oid."'><input type='submit' class='button' name='".$temp."-".$oid."' id='".$temp."-".$oid."' value='投票' /></label>";
					break;
				}
				
				$content = str_replace("{TAG}",$option["name"],$template);
				
				//如果有链接
				if( $option["quote"] ){
					$content = str_replace("{LINK}", $option["quote"] ,$content);
				}else{
					$content = str_replace("_blank", "" ,$content);
					$content = str_replace("{LINK}", "javascript:void(0);" ,$content);
				}
				
				$content = str_replace("{DESC}",$option["description"],$content);
				$content = str_replace("{IMAGE}", ( $option["image"] ? $option["image"] : '' ) ,$content);
				$content = str_replace("{COUNT}",$option["stat"],$content);
				$content = str_replace(chr(13),"<br />",$content);
				
				if(stripos($content,"{INPUT}") !== false ){
					$content = str_replace("{INPUT}",$input,$content);
				}else{
					$content .= $input;
				}
				
				$content = ubb_basic($content);
				
				$item .= $content;
				
				$item .= "</td>";
				
				if ( ($x+1) % $config["GROUP_BREAK"] == 0 ){
					$item .= "</tr>".chr(13).chr(13)."<tr>";
				}
				
				$x ++;
			}
		}				
			
		echo '
		<dl id="group-'.$gid.'" class="'.$group["type"].'">
			<dt>'.$group["name"].'</dt>
			<dd>
				'.($group["description"]?'<p>'.str_replace(chr(13),'<br />',ubb_basic($group["description"])).'</p>':'').'
				<table border="0" cellspacing="0" cellpadding="0">
					<tr>
					'.$item.'
					</tr>
				</table>
			</dd>		
		</dl>	
		';
		
	}
	
	if( !$hitsub ){
		$submit = ( ( $VICACHE["mod.forms"]["start"]<time() && $VICACHE["mod.forms"]["expire"]>time() ) ?"<input type='submit' class='submit' value='提交投票' />": "<input type='submit' class='submit' value='".($VICACHE["mod.forms"]["start"]>time()?"暂未启用":"表单过期")."' disabled='disabled' />");
	}
	
	//使用匿名扩展模式
	//!$_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>
		';
	}
		
	echo "
	<div id='send'>
		".$extra."
		<p>
		".$submit."			
		
		".(!$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>
	";
		
	echo "</form>";

?>