<?php
session_start();
include('conn.php');
define("PAGE_NAME","vote");

//判斷按鈕動作
switch($_POST['btnflag']){
    case 1:
        if( count($_POST['chkvote']) != 5 ){
        	print('<script>alert("請選擇5項支持的作品哦! 目前選擇了 '.count($_POST['chkvote']).' 項");</script>');
        }else if( $_POST['name'] == "" || $_POST['yy'] == "" || $_POST['tele'] == "" || $_POST['e-mail'] == "" ){
        	print('<script>alert("個人資料尚未填寫完成哦!");</script>');
        }else if( !preg_match('/^.+@.+\..{2,3}$/',$_POST['e-mail']) ){
        	print('<script>alert("請輸入有效之email位址!");</script>');
        }else if( strtoupper($_POST['code']) != $_SESSION['verifystr'] ){
        	print('<script>alert("驗證碼錯誤!");</script>');
        }else{
    	    $qs = 'Select VOTENO From lym_vote ';
		    $qs.= 'Where (VOTENAME = '.$adodb->Quote($_POST['name']).' ';
		    $qs.= 'Or VOTETEL = '.$adodb->Quote($_POST['tele']).' ';
		    $qs.= 'Or VOTEMAIL = '.$adodb->Quote($_POST['e-mail']).') ';
		    $qs.= 'And VOTEDATE Like '.$adodb->Quote(getToday().'%').' ';
		    $rs = $adodb->Execute($qs);
		    if( $rs && !$rs->EOF ){
        		print('<script>alert("今天已經投票過了哦!");</script>');
        	}else{
	        	$str_vote = '';
	            foreach( $_POST['chkvote'] as $key => $value ){
	                $str_vote .= $value.'、';
	                
					$qs = 'Select VOTECOUNT ';
					$qs.= 'From lym_vote_pic ';
					$qs.= 'Where SNO='.$adodb->Quote($value);
					$rs = $adodb->Execute($qs);
					if( $rs && !$rs->EOF ){
						$votecount = $rs->fields[0] + 1;
					}
					
				    $qs = 'Update lym_vote_pic Set ';
				    $qs.= 'VOTECOUNT='.$adodb->Quote($votecount).' ';
					$qs.= 'Where SNO='.$adodb->Quote($value);
				    $adodb->Execute($qs);
	            }
	            $str_vote = substr($str_vote,0,strlen($str_vote)-1);
	            	
			    $qs = 'Select VOTENO From lym_vote ';
			    $qs.= 'Order by VOTENO DESC Limit 0,1 ';
			    $rs = $adodb->Execute($qs);
			    if( $rs && !$rs->EOF ){
			        $voteno = sprintf('%010s',$rs->fields[0]+1);
			    }else{
			       	$voteno = sprintf('%010s',1);
			    }
		        $qs = 'Insert into lym_vote (VOTENO,SNO,VOTENAME,VOTEY,VOTETEL,VOTEMAIL,VOTEDATE) ';
		        $qs.= 'Values ('.$adodb->Quote($voteno).',';
		        $qs.= $adodb->Quote($str_vote).',';
		        $qs.= $adodb->Quote($_POST['name']).',';
		        $qs.= $adodb->Quote($_POST['yy']).',';
		        $qs.= $adodb->Quote($_POST['tele']).',';
		        $qs.= $adodb->Quote($_POST['e-mail']).',';
		        $qs.= $adodb->Quote(getToday()).')';
		        $adodb->Execute($qs);
		        
	        	print('<script>alert("投票完成!");</script>');
	        	print('<script>location="gift.html";</script>');
	        }
        }
        	//header('Location: '.PAGE_NAME.'_mod.php');
        break;
}

//List
$qs = 'Select SNO,VCLASS,IMGURL,IMGURL_S,IMGALT,VOTECOUNT,AUTHOR,TITLE,SIZE,MATERIAL,AMOUNT,DES ';
$qs.= 'From lym_vote_pic ';
$qs.= 'Order by cast(SNO As Signed) ';
//$qs.= 'Limit '.$begrows.','.$getrows;
$rs = $adodb->Execute($qs);

$sno = array();
$vclass = array();
$imgurl = array();
$imgurl_s = array();
$imgalt = array();
$votecount = array();
$author = array();
$title = array();
$size = array();
$material = array();
$amount = array();
$des = array();

//各分類序號
$aryclass1 = array();
$aryclass2 = array();
$aryclass3 = array();
$i = 0;

while( $rs && !$rs->EOF ){
    $sno[] = $rs->fields[0];
	$vclass[] = $rs->fields[1];
	$imgurl[] = $rs->fields[2];
	$imgurl_s[] = $rs->fields[3];
	$imgalt[] = $rs->fields[4];
	$votecount[] = $rs->fields[5];
	$author[] = $rs->fields[6];
	$title[] = $rs->fields[7];
	$size[] = $rs->fields[8];
	$material[] = $rs->fields[9];
	$amount[] = $rs->fields[10];
	$des[] = $rs->fields[11];

	//分類
	switch($rs->fields[1]){
		case 1:
			$aryclass1[] = $i;
			break;
		case 2:
			$aryclass2[] = $i;
			break;
		case 3:
			$aryclass3[] = $i;
			break;
	}
	$i++;
    $rs->MoveNext();
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  lang="zh-TW">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>手創蘭陽  宜蘭生活美學特展</title>
<link href="reset-min.css" rel="stylesheet" type="text/css" />
<link href="yilan.css" rel="stylesheet" type="text/css" />
<script	type="text/javascript" src="js/jquery-1.4.2.min.js"> </script> 
<script	type="text/javascript" src="js/events.js"> </script> 
<script type="text/javascript">

</script>
</head>
<body>
<div class="wrapper">
  <div class="topnav"> 
  <a href="#" accesskey="U" title="外部連結選單區" style="color:#FFFFFF">:::</a>
  <img src="images/icon0.png" alt="首頁ICON"/><a href="index.html" title="回網站首頁">首頁</a>| 
  <img src="images/icon1.png" alt="FACEBOOK ICON"/><a href="" title="Facebook粉絲頁">Facebook粉絲頁</a>| 
  <img src="images/icon2.png" alt="PLURK ICON"/><a href="" title="Plurk連結">Plurk</a>| 
  <img src="images/icon3.png" alt="蘭陽博物館 ICON"/><a href="" title="蘭陽博物館網站">蘭陽博物館</a> </div>
  
  <div class="nav">
        <p><a href="#" accesskey="R" title="主連結選單區">:::</a>
          <a class="main" href="sitemap.html" title="網站導覽"></a></p>
            <a class="concept" href="concept.html" title="策展理念" ></a>
            <a class="intro" href="intro.html"title="展覽介紹"></a>
            <a class="program" href="program.html" title="推廣活動"></a>
            <a class="event" href="event.html" title="設計師到家徵選活動"></a>
            <a class="vote" href="vote.php" title="展品票選"></a>
          <a class="information" href="information.html" title="設計師創作小物"></a></p>
  </div>
  <div class="subnav 2"> 
      <a href="#" accesskey="L" title="次連結選單區"  style="padding:0px; margin:0px; color:#FFFFFF;">:::</a>
  <a href="vote.php" title="作品票選">作品票選</a>
  <a href="gift.html" title="票選辦法及贈品">票選辦法及贈品</a>
  <a href="winner.html" title="得獎名單">得獎名單</a>
  </div>
  <div class="container2">
  <a href="#" accesskey="C" title="內容區" style=" color:#FFFFFF">:::</a>
   <img src="images/01_12.png" alt="*" /><span class="style2"> 設計師+工藝師</span>
   
        <form method="post" name="vote" id="Login_vote" action="<?php print(PAGE_NAME); ?>.php">
<input type="hidden" id="btnflag" name="btnflag">
<fieldset>
<?
	foreach($aryclass2 as $key => $value){
    print('<div class="vote_item">');
        print('<img src="images/vote_pic/'.$imgurl[$value].'" alt="'.$imgalt[$value].'"/>');
      print('<div class="item_intro">');
        print('<p>作者：'.$author[$value].'</p>');
        print('<p>&nbsp;</p>');
        print('<p>尺寸：'.$size[$value].'</p>');
        print('<p>材質：'.$material[$value].'</p>');
        print('<p>數量：'.$amount[$value].'</p>');
        print('<p>&nbsp;</p>');
        print('<p>說明文字：'.$des[$value].'</p>');
      print('</div>');
      $chked = '';
      if( count($_POST['chkvote']) > 0 ){
		for( $i=0; $i<count($_POST['chkvote']); $i++ ){
			if( $_POST['chkvote'][$i] == $sno[$value] ){
				$chked = 'checked';
			}
		}
	  }
        print('<div class="select"> <input type="checkbox" name="chkvote[]" value="'.$sno[$value].'" '.$chked.' /><label>投他一票</label></div>');
    print('</div>');
	}
?>
   <div class="title2"><img src="images/01_12.png" alt="*"/>
      <p><strong>設計師+社區</strong></p>  </div>
<?
	foreach($aryclass3 as $key => $value){
    print('<div class="vote_item">');
        print('<img src="images/vote_pic/'.$imgurl[$value].'" alt="'.$imgalt[$value].'"/>');
      print('<div class="item_intro">');
        print('<p>作者：'.$author[$value].'</p>');
        print('<p>&nbsp;</p>');
        print('<p>尺寸：'.$size[$value].'</p>');
        print('<p>材質：'.$material[$value].'</p>');
        print('<p>數量：'.$amount[$value].'</p>');
        print('<p>&nbsp;</p>');
        print('<p>說明文字：'.$des[$value].'</p>');
      print('</div>');
      $chked = '';
      if( count($_POST['chkvote']) > 0 ){
		for( $i=0; $i<count($_POST['chkvote']); $i++ ){
			if( $_POST['chkvote'][$i] == $sno[$value] ){
				$chked = 'checked';
			}
		}
	  }
        print('<div class="select"> <input type="checkbox" name="chkvote[]" value="'.$sno[$value].'" '.$chked.' /><label>投他一票</label></div>');
    print('</div>');
	}
?>
    <div class="title2"><img src="images/01_12.png" alt="*"/><p><strong>個人資料填續寫</strong></p>
    </div>

    <div class="input">

<div class="step">
  <label for="name">1. 姓名　</label>
<input type="text" name="name" id="name" value="<?php print($_POST['name']); ?>" /></div>
<div class="step"><label for="yes">2. 是否為宜蘭人  </label>
<?php
	$chked1 = '';
	$chked2 = '';
if( $_POST['yy'] == "Y" ){
	$chked1 = 'checked';
}else if( $_POST['yy'] == "N" ){
	$chked2 = 'checked';
}
?>
 <input type="radio" name="yy" value="Y" <?php print($chked1); ?> >
  <label for="yes">是</label>
<input type="radio" name="yy" value="N" <?php print($chked2); ?> >
<label for="no">否</label>
</div>

<div class="step"> <label for="tele">3. 電話</label>　
    <input type="text" name="tele" id="tlel" value="<?php print($_POST['tele']); ?>" />
</div>
<div class="step"><label for="e-mail">4. E-mail</label>
<input type="text" name="e-mail" id="e-mail" value="<?php print($_POST['e-mail']); ?>" /></div>
<div class="step"><label for="code">5. 請輸入驗證碼</label>
<input type="text" name="code" id="code" />
</div><div class="step"></div>
<div class="code"><img src="vertifyimg.php" alt="vertify img" width="99" height="49" /></div>
<a class="send" title="送出票選" href="#" onclick="clickEvent('1');return false;"></a>

</div>
</fieldset>
</form>
  </div>
  <div class="footer">
    <p>宜蘭縣立蘭陽博物館 版權所有<br />
      All Right Reservedn
    </p>
  </div>
</div>
</body>
</html>
