<? 
require_once "include/application.php"; 
require_once "include/function.php";
require_once "include/newvalue_lib.php";


if($btnAdd){
	$err=false;

	if($err==false){
		if(count($upcomplementary)>0)$comp=implode(",", $upcomplementary);	
		
		$ret=get_data("ip_program,ip_night","ivc_products","where ip_id='$upscheme'");
	
		list($d,$m,$y)=split("/",$joindate);
		$exp=date("Y-m-d",mktime(0,0,0,$m,$d,$y+$ret["ip_program"]));
		
		$sql="UPDATE `ivc_members` SET im_scheme='$upscheme',im_complementary='$comp',im_freemaint='$upmaint',
		im_entnight='".$ret["ip_night"]."',im_expired='$exp',im_program='".$ret["ip_program"]."'  WHERE `im_id` ='$id'";
		@mysql_query($sql) or die(mysql_error());
	
		for($i=0;$i<$ret["ip_program"];$i++){
			$j=$i+1;
			$yent=date("Y-m-d",mktime(0,0,0,$m,$d,$y+$j));			
			$sql="INSERT INTO `ivc_yearly_ent` (
			  `ye_memberno` ,
			  `ye_expiry` ,
			  `ye_night` 
			  )
			  VALUES (
			  '$formno', '$yent', '".$ret["ip_night"]."'
			  );
			";
			@mysql_query($sql) or die(mysql_error());		
		}
		
		$sql="
		INSERT INTO `ivc_upgrade_members` (
		`ium_memberno` ,
		`ium_newscheme` ,
		`ium_newfreemaint` ,
		`ium_newcomplementary` ,
		`ium_orgscheme` ,
		`ium_orgfreemaint` ,
		`ium_orgcomplementary` ,
		`ium_orgentnight` ,
		`ium_user` ,
		`ium_date` 
		)
		VALUES ('$formno', '$upscheme', '$upmaint', '$comp','$scheme', '$maint', '$orgcomp', '$entnights', '".$_SESSION["egoUser"]."','".system_date($joindate)."'
		);
		";
		@mysql_query($sql) or die(mysql_error());	
	
		print("<script type='text/javascript'>alert('Membership is upgraded.')</script>");
		print("<script type='text/javascript'>location.href='members.php'</script>");
	}
}else{
	$sql="select ivc_members.*,ip_name,ip_code from ivc_members left join ivc_products on ip_id=im_scheme where im_id='$id'";
	$result=mysql_query($sql);	
	while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC))
	{
		$formno=$myrow["im_formno"];
		$scheme=$myrow["ip_code"]." - ".$myrow["ip_name"];
		$category=$myrow["im_category"];		
		$name=$myrow["im_name"];
		$title=$myrow["im_title"];
		$ic=$myrow["im_ic"];
		$cardno=$myrow["im_cardno"];
		$cardname=$myrow["im_cardname"];
		$nationality=$myrow["im_nationality"];		
		$phome=$myrow["im_phome"];
		$mobile=$myrow["im_mobile"];
		$fax=$myrow["im_fax"];
		$email=$myrow["im_email"];	
		$orgcomp=$myrow["im_complementary"];	
		$complementary=explode(",",$myrow["im_complementary"]);
		$entnights=$myrow["im_entnight"];
		$freemaint=$myrow["im_freemaint"];
		$exp=window_date($myrow["im_expired"]);

	}

}

if(system_date($exp) <  date("Y-m-d")){
	print("<script type='text/javascript'>alert('Membership is already expired ! Cant upgrade.')</script>");
	print("<script type='text/javascript'>location.href='members.php'</script>");

}

if(!$joindate)$joindate=date("d/m/Y");
?>

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="sddm.css" >


<script src="include/validation.js" type="text/javascript"></script>
  <link rel="stylesheet" href="datepicker/themes/winter.css" />
<!-- Loading Calendar JavaScript files -->
    <script type="text/javascript" src="datepicker/zapatec.js"></script>
    <script type="text/javascript" src="datepicker/calendar.js"></script>
<!-- Loading language definition file -->
    <script type="text/javascript" src="datepicker/calendar-en.js"></script>

<title><? echo TITLE?></title></head>
<body>

<table width="1024" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#f1efd8">
  <tr>
    <td  colspan="2" align="center"><? require_once "include/header.php"; ?></td>
  </tr>
  <tr>
    <td colspan="2" align="center">&nbsp;</td>
  </tr>
  <tr>
    <td width="185" valign="top" bgcolor="e6f3f9">
	
<? require_once "include/columnLeft.php"; 
if(array_search("Membership",$menulist)==false)
		print("<script type='text/javascript'>location.href='index.php'</script>");

?>	</td>
    <td width="834" valign="top">
	<form name="form" method="post" action="<?=$PHP_SELF ?>"  ONSUBMIT="return ValidateUpgradeMember(this)">
	   <table width="820" height="250" border="0" align="center" cellpadding="0" cellspacing="0">
      
      <tr>
        
        <td valign="top"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="2">
          <tr>
            <td height="30" class="TitleH3">Membership Upgrade 
              <input name="id" type="hidden" id="id" value="<? echo $id ?>" />
</td>
          </tr>
          <tr>
            <td height="1" bgcolor="#B8ED83"></td>
          </tr>
          <tr>
            <td ><? echo "<div class='Msg'>$action_msg</div>" ?>&nbsp;</td>
          </tr>
          <tr>
            <td valign="top" ><table width="100%" border="0" 
  cellpadding="0" cellspacing="0" class="editTbl" id="tbl-tabMainContact">
              <tr>
                <th height="23" class="TextH1"><div align="left">Form No :</div></th>
                <td class="TextH1"><? echo $formno ?><span class="TitleH3">
                  <input name="formno" type="hidden" id="formno" value="<? echo $formno ?>" />
                </span></td>
              </tr>
              <tr>
                <th height="23" class="TextH1"><div align="left">Type of Product :</div></th>
                <td class="TextH1"><? echo $scheme ?><span class="TitleH3">
                  <input name="scheme" type="hidden" id="scheme" value="<? echo $scheme ?>" />
                </span></td>
              </tr>
              <tr>
                <th height="23" class="TextH1"><div align="left">Category of Membership :</div></th>
                <td class="TextH1"><? echo $category ?></td>
              </tr>
              <tbody>
                <tr>
                  <th width="235" height="23" class="TextH1"><div align="left">Name of Applicant :</div></th>
                  <td width="581" class="TextH1"><? echo $name ?></td>
                </tr>
                <tr>
                  <th height="23" class="TextH1"><div align="left">Card No. :</div></th>
                  <td class="TextH1"><? echo $cardno ?></td>
                </tr>
                <tr>
                  <th height="23" class="TextH1"><div align="left">Name on the Card :</div></th>
                  <td class="TextH1"><? echo $cardname ?>                 </td>
                </tr>
                <tr>
                  <th height="23" class="TextH1"><div align="left">NRIC/Passport :</div></th>
                  <td class="TextH1"><? echo $ic ?></td>
                </tr>
                <tr>
                  <th height="23" class="TextH1"><div align="left">Nationality : </div></th>
                  <td class="TextH1"><? echo $nationality ?></td>
                </tr>
                <tr>
                  <th height="23" class="TextH1"><div align="left"> Home No : </div></th>
                  <td class="TextH1"><? echo $phome ?></td>
                </tr>
                <tr>
                  <th height="23" class="TextH1"><div align="left">Mobile No : </div></th>
                  <td class="TextH1"><? echo $mobile ?></td>
                </tr>
                <tr>
                  <th height="12" class="TextH1"><div align="left">Faxo : </div></th>
                  <td class="TextH1"><? echo $fax ?></td>
                </tr>

                <tr>
                  <th height="23" class="TextH1"><div align="left">Email : </div></th>
                  <td class="TextH1"><? echo $email ?></td>
                </tr>
                <tr>
                  <th height="23" class="TextH1"><div align="left"> Annual Entitlement : </div></th>
                  <td class="TextH1"><span id="txtEntNights"><? echo $entnights ?> Night(s) <span class="TitleH3">
                    <input name="entnights" type="hidden" id="entnights" value="<? echo $entnights ?>" />
                  </span></span></td>
                </tr>
                <tr class="small">
                  <td class="TextH1">Free Maintenance Fees :</td>
                  <td class="TextH1"><span id="txtFreeMaint"></span> <? echo $freemaint ?>&nbsp; Year(s ) <span class="TitleH3">
                    <input name="freemaint" type="hidden" id="freemaint" value="<? echo $freemaint ?>" />
                  </span></td>
                </tr>
                <tr class="small">
                  <td class="TextH1">Complementary :</td>
                  <td class="TextH1"><span id="txtFreeMaint"></span> <?
				  foreach ($complementary as $value) {
					if($comp)$comp.=", ".$nv[$value];
					else $comp=$nv[$value];
				  } 
				   echo $comp;
				   ?>
                    <span class="TitleH3">
                    <input name="orgcomp" type="hidden" id="orgcomp" value="<? echo $orgcomp ?>" />
                    </span></td>
                </tr>
                <tr>
                  <th height="23" class="TextH1"><div align="left">Membership Expired : </div></th>
                  <td class="TextH1"><span id="txtExp"><? echo $exp ?><span class="TitleH3">
                    <input name="exp" type="hidden" id="exp" value="<? echo $exp ?>" />
                  </span></span></td>
                </tr>
                <tr>
                  <th height="23" bgcolor="#FFFF99" class="TextH1"><div align="left">Upgrade Type of Product : <span class="require">*</span></div></th>
                  <td bgcolor="#FFFF99" class="TextH1"><select name="upscheme" id="upscheme" style="WIDTH: 180px">
                      <option value=""></option>
                      <?
			 
			 	$sql="select * from ivc_products order by ip_name";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["ip_id"]."' ";
					if($scheme==$myrow["ip_id"])echo "selected";
					echo ">".$myrow["ip_code"]." - ".$myrow["ip_name"]."</option>";
				}
			
?>
                  </select></td>
                </tr>
                <tr class="small">
                  <td bgcolor="#FFFF99" class="TextH1">Free Maintenance Fees :</td>
                  <td bgcolor="#FFFF99" class="TextH1"><input name="upmaint" type="text" class="small" id="upmaint" value="<? echo $maint ?>" size="2" maxlength="2" />
                    Years </td>
                </tr>
                <tr class="small">
                  <td valign="top" bgcolor="#FFFF99" class="TextH1">Complementary :</td>
                  <td bgcolor="#FFFF99" class="TextH1"><select name="upcomplementary[]" size="7" multiple="multiple" id="complementary">
                      <?
			 	$sql="select * from newvalue where nv_type='Complementary' order by nv_name";
				$result=mysql_query($sql) or die(mysql_error());				
				while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC)){
					echo "<option value='".$myrow["nv_id"]."'>".$myrow["nv_name"]." </option>";
				}
			
?>
                                      </select>
                      <? staymultifocus("form","upcomplementary",$upcomplementary) ?></td>
                </tr>
                <tr class="small">
                  <td bgcolor="#FFFF99" class="TextH1">Apply Date :</td>
                  <td bgcolor="#FFFF99" class="TextH1"><input name="joindate" type="text" class="small" id="joindate"   value="<? echo $joindate ?>" size="15" maxlength="10" />
                    <button id="trigger_joindate">...</button>
                    <script type="text/javascript">//<![CDATA[
      Zapatec.Calendar.setup({
        weekNumbers       : false,
        electric          : false,
        inputField        : "joindate",
        button            : "trigger_joindate",
        ifFormat          : "%d/%m/%Y",
        daFormat          : "%Y/%m/%d"
      });
    //]]></script>
                    <span class="subItem"> dd/mm/yyyy</span></td>
                </tr>
              </tbody>
              <tr>
                <td>                            
            </table></td>
            </tr>
          <tr>
            <td valign="top" >&nbsp;</td>
          </tr>
          
          <tr class="small">
            <td height="1" bgcolor="#999999"></td>
          </tr>
          <tr class="small">
            <td height="30" class="TextH1"><span class="require">* Denotes a require_onced field</span></td>
          </tr>
          <tr>
            <td valign="top" ><span class="text12">
              <input name="btnAdd" type="submit" class="btn150" id="btnAdd" value="UPGRADE MEMBERSHIP " onclick="return confirm('Are you sure ?')"/>
&nbsp;
<input name="btnBack" type="button" class="btn" id="btnBack" onclick="location.href='members.php'" value="BACK" />
            </span></td>
            </tr>
          

        </table>	</td>
      </tr>
    </table><br />
	</form>	</td>
  </tr>
  <tr>
    <td colspan="2" bgcolor="#333333" class="Footer" align="center"><br />
    <? require_once "footer.php"; ?></td>
  </tr>
</table>
</body>
</html>
