<?
require_once('includes/configure.php');
require_once "../admin/include/function.php";
require_once "../admin/include/parameter.php";
require_once "includes/$lang/reservappredit.php";
require_once "includes/formvalidator.php";

if($btnCancel){
	$newstamp=nextid('n_stamp');
	$sql="
		update `ivc_reservation` set irs_status='C',irs_cancel_date=now(), irs_stamp='$newstamp', 
		`irs_cancelby`='".$_SESSION["egoMemberID"]."', irs_cancel_remark='Cancel by member' where irs_id='$id' and irs_stamp='$stamp'";

	@mysql_query($sql) or die(mysql_error());

	if(mysql_affected_rows()==0)$ainfo=urlencode(RESERV_FAIL);
	else{
		 $ainfo=urlencode(RESERV_CANCEL);

	}
	print("<script type='text/javascript'>location.href='index.php?ai=$ainfo'</script>");	

}


if(isset($_POST['btnAdd'])){

    $validator = new FormValidator();
	if($reserv=="Guest"){
	    $validator->addValidation("gname","req",REQUIRED_GUEST_NAME);
		$validator->addValidation("gic","req",REQUIRED_GUEST_IC);	
		$validator->addValidation("gcontact","req",REQUIRED_GUEST_CONTACT);	
		$validator->addValidation("gcontact","regexp=/^[0-9 () -]+$/",REQUIRED_GUEST_CONTACT);		
	}
    $validator->addValidation("adult","req",REQUIRED_ADULT);	
    $validator->addValidation("cin1","req",REQUIRED_CIN);		
    $validator->addValidation("cout1","req",REQUIRED_COUT);	
	if($cin1 && $cout1){
		$validator->addValidation("cout1","dategreaterthan=$cin1",REQUIRED_DATE1);	

		$nonight=get_days(system_date($cin1),system_date($cout1));
		$tot=$nonight * $rooms;
		$validator->addValidation("bal","gt=$tot",REQUIRED_BALANCE);	
		
		//yearly
		$validator->addValidation("ye","dategreaterthan=$cout1",REQUIRED_EXPYEARLY);			

	}
	
	if($cin2 || $cout2){
	    $validator->addValidation("cin2","req",REQUIRED_CIN2);	
		$validator->addValidation("cout2","dategreaterthan=$cin2",REQUIRED_DATE2);	


		$validator->addValidation("cin2","dategreaterthan=$adv",REQUIRED_ADVANCE_BOOKING);		
		$nonight=get_days(system_date($cin2),system_date($cout2));
		$tot=$nonight * $rooms;
		$validator->addValidation("bal","gt=$tot",REQUIRED_BALANCE);	

		//yearly
		$validator->addValidation("ye","dategreaterthan=$cout2",REQUIRED_EXPYEARLY);			
	}

	if($cin3 || $cout3){
	    $validator->addValidation("cin3","req",REQUIRED_CIN3);	
		$validator->addValidation("cout3","dategreaterthan=$cin3",REQUIRED_DATE3);	

		$validator->addValidation("cin3","dategreaterthan=$adv",REQUIRED_ADVANCE_BOOKING);		
		$nonight=get_days(system_date($cin3),system_date($cout3));
		$tot=$nonight * $rooms;
		$validator->addValidation("bal","gt=$tot",REQUIRED_BALANCE);	

		//yearly
		$validator->addValidation("ye","dategreaterthan=$cout3",REQUIRED_EXPYEARLY);			
	}
	
	

	if($validator->ValidateForm())
    {
		$newstamp=nextid('n_stamp');

		$sql="
			update `ivc_reservation` set `irs_for` ='$reserv',
			`irs_guestname` =\"$gname\" ,
			`irs_guestic` ='$gic',
			`irs_checkin1`  = '".system_date($cin1)."',
			`irs_checkout1` = '".system_date($cout1)."',
			`irs_adult1`='$adult' ,
			`irs_child1` ='$child' ,
			`irs_checkin2` = '".system_date($cin2)."',
			`irs_checkout2` ='".system_date($cout2)."',
			`irs_checkin3` = '".system_date($cin3)."',
			`irs_checkout3` = '".system_date($cout3)."',
			`irs_remark` = \"$remark\" , irs_stamp='$newstamp' 
			 where irs_id='$id' and irs_memberno='".$_SESSION["egoMemberNo"]."' and irs_stamp='$stamp'";

		@mysql_query($sql) or die(mysql_error());
		if(mysql_affected_rows()==0)$ainfo=urlencode(RESERV_FAIL);
		else $ainfo=urlencode(RESERV_UPDATED);
		

		print("<script type='text/javascript'>location.href='index.php?ai=$ainfo'</script>");	
	}else {
	    $action_msg= "<br><B>Validation Errors:</B><br>";

        $error_hash = $validator->GetErrors();
        foreach($error_hash as $inpname => $inp_err)
        {
           $action_msg.= "$inp_err<br>";
        }        
    }
}
$sql="select * from ivc_reservation where irs_id='$id' and irs_status='N' and irs_memberno='".$_SESSION["egoMemberNo"]."'";
$result=mysql_query($sql);	
while($myrow=MySQL_fetch_array($result,MYSQL_ASSOC))
{
	$reserv=$myrow["irs_for"];
	$gname=$myrow["irs_guestname"];
	$gic=$myrow["irs_guestic"];
	$gcontact=$myrow["irs_guestcontact"];
	$gemail=$myrow["irs_guestemail"];
	$rooms=$myrow["irs_room1"];
	
	$adult=$myrow["irs_adult1"];
	$child=$myrow["irs_child1"];

	if($myrow["irs_checkin1"]<>"0000-00-00"){
		$cin1=window_date($myrow["irs_checkin1"]);
		$cout1=window_date($myrow["irs_checkout1"]);
	} 

	if($myrow["irs_checkin2"]<>"0000-00-00"){
		$cin2=window_date($myrow["irs_checkin2"]);
		$cout2=window_date($myrow["irs_checkout2"]);
	}
	
	if($myrow["irs_checkin3"]<>"0000-00-00"){	
		$cin3=window_date($myrow["irs_checkin3"]);
		$cout3=window_date($myrow["irs_checkout3"]);
	}


	$remark=$myrow["irs_remark"];
	$stamp=$myrow["irs_stamp"];
}
$vc=explode("|",vacation_info($_SESSION["egoMemberNo"]));
?>
<!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" <?= HTML_PARAMS ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?= CHARSET ?>" />
<title><?=TITLE?></title>
<!-- SmartMenus 6 config and script core files -->
<? if($_SESSION["egoLang"]=="ENG"){ ?>
<script type="text/javascript" src="c_config.js"></script>
<? }else if($_SESSION["egoLang"]=="CH"){ ?>
<script type="text/javascript" src="ch_config.js"></script>
<? } ?>
<script type="text/javascript" src="c_smartmenus.js"></script>

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


<link rel="stylesheet" type="text/css" href="../styles.css" >
</head>

<body>
<table width="1000" height="600" border="0" align="center" cellpadding="0" cellspacing="0" id="Table_01">
  <tr>
    <td height="82" background="../images/iconic_01.jpg"><table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td width="91%" height="30" align="right" valign="bottom" class="topmenu"><? include_once "includes/topmenu.php"; ?></td>
        <td width="9%" rowspan="2">&nbsp;</td>
      </tr>
      <tr>
        <td height="4"></td>
      </tr>
      <tr>
        <td height="14" align="right">
          <? include_once "includes/menu.php"; ?>
</td>
        <td height="29" rowspan="2"></td>
      </tr>
      <tr>
        <td height="3" align="right"></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="500" valign="top" background="../images/memberlogin_bg.jpg" style="background-repeat:no-repeat">
      <table width="76%" border="0" align="center" cellpadding="0" cellspacing="0">
      <tr>
        <td width="789" height="340" valign="bottom" class="title1"><?=RESERV_APPR;?></td>
      </tr>
      
      <tr>
        <td >
		<form name="form" method="post" action="<?=$PHP_SELF ?>" >
	   <table width="820" height="250" border="0" align="center" cellpadding="0" cellspacing="0">
      
      <tr>
        
        <td valign="top"><table width="800" height="250" border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td valign="top"><table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
         
                <tr>
                  <td class="require"><? echo "<div class='Msg'>$action_msg</div>" ?>&nbsp;</td>
                </tr>
              </table>
                <table width="100%" border="0" cellpadding="0" cellspacing="0">
                  <tr>
                    <td width="53%" valign="top"><table border="0" cellpadding="2" width="100%" cellspacing="1" class="small">
                      <tr>
                        <td width="172" class="text1"><strong>
                          <?=TYPE_PRODUCT; ?>
                          :</strong></td>
                        <td width="240" class="text1"><strong><? echo $vc[0]; ?></strong></td>
                      </tr>
                      <tr>
                        <td class="text1"><strong>
                          <?=ROOMS; ?>
                          :</strong></td>
                        <td class="text1"><strong><? echo $vc[1]; ?></strong></td>
                      </tr>
                      <tr class="editTbl">
                        <th height="23" class="text1"><div align="left"><strong>
                          <?=PROGRAM; ?>
                          : </strong></div></th>
                        <td class="text1"><strong><? echo $vc[7] ?> Years</strong></td>
                      </tr>
                      <tr class="editTbl">
                        <th height="23" class="text1"><div align="left"><strong>
                          <?=MEMBERSHIP_EXP; ?>
                          : </strong></div></th>
                        <td class="text1"><strong><? echo window_date($vc[4]); ?>
                          <input name="expdate" type="hidden" id="expdate" value="<? echo window_date($vc[4]); ?>"/>
                        </strong></td>
                      </tr>
                      <tr class="editTbl">
                        <th height="23" class="text1"><div align="left"><strong>
                          <?=ANNUAL_ENTITLEMENT; ?>
                          : </strong></div></th>
                        <td class="text1"><strong> <? echo $vc[2]; ?>&nbsp;
                          <?=NIGHT; ?>
                        </strong></td>
                      </tr>
                      <tr class="editTbl">
                        <th height="23" class="text1"><div align="left"><strong>
                          <?=ANNUAL_EXPIRY; ?>
                          : </strong></div></th>
                        <td class="text1"><strong><? echo window_date($vc[6]); ?>
                          <input name="ye" type="hidden" id="ye" value="<? echo window_date($vc[6]);  ?>"/>
                        </strong></td>
                      </tr>
                      <tr>
                        <td class="text1"><strong>
                          <?=FREE_MAINT; ?>
                          :</strong></td>
                        <td class="text1"><strong> <? echo $vc[3]; ?>&nbsp;
                          <?=YEAR;?>
                        </strong></td>
                      </tr>
                      <tr class="editTbl">
                        <th height="23" class="text1"><div align="left"><strong>
                          <?=BAL_NIGHT; ?>
                          : </strong></div></th>
                        <td class="text1"><strong><? echo $vc[5]; ?>&nbsp;
                          <?=NIGHT; ?>
                          <input name="bal" type="hidden" id="bal" value="<? echo $vc[5]; ?>"/>
                        </strong></td>
                      </tr>
                      <tr class="editTbl">
                        <th height="23" class="TextH1">&nbsp;</th>
                        <td class="TextH1">&nbsp;</td>
                      </tr>
                    </table>
                      <table border="0" cellpadding="2" width="100%" cellspacing="1" class="small">
                        <tr>
                          <td width="412" class="text1"><strong><?=RESRV_REQUEST; ?>
                            <strong><strong><strong><strong><strong><strong><strong><strong>
                            <input name="id" type="hidden" id="id" value="<? echo $id ?>" />
                            <strong><strong><strong><strong><strong><strong><strong><strong>
                            <input name="stamp" type="hidden" id="stamp" value="<? echo $stamp ?>" />
                            </strong></strong></strong></strong></strong></strong></strong></strong>                            </strong></strong></strong></strong></strong></strong></strong></strong></strong></td>
                        </tr>
                        <tr>
                          <td class="text4"><input name="reserv" type="radio" value="Personal" <?=($reserv=="Personal" ? "CHECKED" : " ")?>/>
                          <b>  <?=PERSONAL_USE; ?></b>
                            <input name="reserv" type="radio" value="Guest" <?=($reserv=="Guest" ? "CHECKED" : " ")?>/>
                          <b>  <?=GUEST_USE; ?> </b></td>
                        </tr>
                        <tr>
                          <td bgcolor="#FFFFCC" class="text1"><?=GUEST_INFO; ?> :</td>
                        </tr>
                        <tr>
                          <td bgcolor="#FFFFCC" class="text1"><?=GUEST_NAME; ?> : <span class="require">*</span>
                              <input name="gname" type="text" class="small" id="gname" value="<? echo $gname ?>" maxlength="50" /></td>
                        </tr>
                        <tr>
                          <td bgcolor="#FFFFCC" class="text1"><?=GUEST_IC; ?> : <span class="require">*</span>
                              <input 
      name="gic" id="gic" style="WIDTH: 180px" value="<? echo $gic ?>" size="20" maxlength="20" /></td>
                        </tr>
                        <tr>
                          <td bgcolor="#FFFFCC" class="text1"><?=GUEST_CONTACT; ?> : <span class="require">*</span>
                              <input 
      name="gcontact" id="gcontact" style="WIDTH: 180px" value="<? echo $gcontact ?>" size="20" maxlength="20" /></td>
                        </tr>
                        <tr>
                          <td bgcolor="#FFFFCC" class="text1"><?=GUEST_EMAIL; ?> :
                            <input 
      name="gemail" id="gemail" style="WIDTH: 180px" value="<? echo $gemail ?>" size="20" maxlength="20" /></td>
                        </tr>
                        <tr>
                          <td class="TextH1"><div align="center"></div></td>
                        </tr>
                        <tr class="editTbl">
                          <th height="23" class="TextH1">&nbsp;</th>
                          </tr>
                    </table></td>
                    <td width="47%" valign="top"><table width="100%" border="0" cellspacing="1" cellpadding="2">
                      <tr>
                        <td class="text1"><span class="TextH1">
                          <?=NO_VILLAS; ?>
                          : <span class="require">*</span></span></td>
                        <td class="text1"><span class="TextH1">
                          <?
echo "<select name='rooms'>";
for($i=1;$i<=$vc[8] * $vc[2];$i++){
	echo '<option value="'.$i.'"';
	if($rooms==$i)echo ' selected';
	echo '>'.$i.'</option>\n';
} echo '</select> ';

?>
                        </span></td>
                      </tr>

                        <tr>
                          <td class="text1"><?=NO_ADULT; ?> : <span class="require">*</span></td>
                          <td class="text1"><?
echo "<select name='adult'>";
for($i=1;$i<=$vc[8];$i++){
	echo '<option value="'.$i.'"';
	if($adult==$i)echo ' selected';
	echo '>'.$i.'</option>\n';
} echo '</select> ';

?>
                            &nbsp; <?=NO_CHILD; ?> :<span class="require">
                            <?
echo "<select name='child'>";
for($i=0;$i<=$vc[9];$i++){
	echo '<option value="'.$i.'"';
	if($child==$i)echo ' selected';
	echo '>'.$i.'</option>\n';
} echo '</select> ';

?>
                            </span></td>
                        </tr>
                        <tr>
                          <td colspan="2" bgcolor="#FF9900" class="text1"><div align="center"><strong><?=CHOICE_1; ?> </strong></div></td>
                        </tr>
                        <tr>
                          <td width="37%" class="text1"><?=CHECK_IN; ?> : <span class="require">*</span></td>
                          <td width="63%" class="text1"><input name="cin1" type="text" class="small" id="cin1"   value="<? echo $cin1 ?>" size="15" maxlength="10" />
                              <button id="trigger_cin1">...</button>
                            <script type="text/javascript">//<![CDATA[
      Zapatec.Calendar.setup({
        weekNumbers       : false,
        electric          : false,
        inputField        : "cin1",
        button            : "trigger_cin1",
        ifFormat          : "%d/%m/%Y",
        daFormat          : "%Y/%m/%d"
      });
    //]]></script>
                              <span class="subItem"> <?=DATE_FORMAT; ?></span></td>
                        </tr>
                        <tr>
                          <td class="text1"><?=CHECK_OUT; ?> : <span class="require">*</span></td>
                          <td class="text1"><input name="cout1" type="text" class="small" id="cout1"   value="<? echo $cout1 ?>" size="15" maxlength="10" />
                              <button id="trigger_cout1">...</button>
                            <script type="text/javascript">//<![CDATA[
      Zapatec.Calendar.setup({
        weekNumbers       : false,
        electric          : false,
        inputField        : "cout1",
        button            : "trigger_cout1",
        ifFormat          : "%d/%m/%Y",
        daFormat          : "%Y/%m/%d"
      });
    //]]></script>
                              <span class="subItem"> <?=DATE_FORMAT; ?></span></td>
                        </tr>
                        <tr>
                          <td class="text1">&nbsp;</td>
                          <td class="text1">&nbsp;</td>
                        </tr>
                        <tr>
                          <td colspan="2" bgcolor="#FF9966" class="text1"><div align="center"><strong><?=CHOICE_2; ?> </strong></div></td>
                        </tr>
                        <tr>
                          <td class="text1"><?=CHECK_IN; ?> : <span class="require">*</span></td>
                          <td class="text1"><input name="cin2" type="text" class="small" id="cin2"   value="<? echo $cin2 ?>" size="15" maxlength="10" />
                              <button id="trigger_cin2">...</button>
                            <script type="text/javascript">//<![CDATA[
      Zapatec.Calendar.setup({
        weekNumbers       : false,
        electric          : false,
        inputField        : "cin2",
        button            : "trigger_cin2",
        ifFormat          : "%d/%m/%Y",
        daFormat          : "%Y/%m/%d"
      });
    //]]></script>
                              <span class="subItem"> <?=DATE_FORMAT; ?></span></td>
                        </tr>
                        <tr>
                          <td class="text1"><?=CHECK_OUT; ?> : <span class="require">*</span></td>
                          <td class="text1"><input name="cout2" type="text" class="small" id="cout2"   value="<? echo $cout2 ?>" size="15" maxlength="10" />
                              <button id="trigger_cout2">...</button>
                            <script type="text/javascript">//<![CDATA[
      Zapatec.Calendar.setup({
        weekNumbers       : false,
        electric          : false,
        inputField        : "cout2",
        button            : "trigger_cout2",
        ifFormat          : "%d/%m/%Y",
        daFormat          : "%Y/%m/%d"
      });
    //]]></script>
                              <span class="subItem"> <?=DATE_FORMAT; ?></span></td>
                        </tr>
                        <tr>
                          <td class="text1">&nbsp;</td>
                          <td class="text1">&nbsp;</td>
                        </tr>
                        <tr>
                          <td colspan="2" bgcolor="#FF9999" class="text1"><div align="center"><strong><?=CHOICE_3; ?></strong></div></td>
                        </tr>
                        <tr>
                          <td class="text1"><?=CHECK_IN; ?> : <span class="require">*</span></td>
                          <td class="text1"><input name="cin3" type="text" class="small" id="cin3"   value="<? echo $cin3 ?>" size="15" maxlength="10" />
                              <button id="trigger_cin3">...</button>
                            <script type="text/javascript">//<![CDATA[
      Zapatec.Calendar.setup({
        weekNumbers       : false,
        electric          : false,
        inputField        : "cin3",
        button            : "trigger_cin3",
        ifFormat          : "%d/%m/%Y",
        daFormat          : "%Y/%m/%d"
      });
    //]]></script>
                              <span class="subItem"> <?=DATE_FORMAT; ?></span></td>
                        </tr>
                        <tr>
                          <td class="text1"><?=CHECK_OUT; ?> : <span class="require">*</span></td>
                          <td class="text1"><input name="cout3" type="text" class="small" id="cout3"   value="<? echo $cout3 ?>" size="15" maxlength="10" />
                              <button id="trigger_cout3">...</button>
                            <script type="text/javascript">//<![CDATA[
      Zapatec.Calendar.setup({
        weekNumbers       : false,
        electric          : false,
        inputField        : "cout3",
        button            : "trigger_cout3",
        ifFormat          : "%d/%m/%Y",
        daFormat          : "%Y/%m/%d"
      });
    //]]></script>
                              <span class="subItem"> <?=DATE_FORMAT; ?></span></td>
                        </tr>
                        <tr>
                          <td colspan="2" class="text1">&nbsp;</td>
                        </tr>
                        <tr>
                          <td colspan="2" class="text1"><?=REMARK; ?> :</td>
                        </tr>
                        <tr>
                          <td colspan="2" class="TextH1"><textarea name="remark" cols="50" rows="4" id="remark" ><? echo $remark ?></textarea></td>
                        </tr>
                    </table></td>
                  </tr>
                </table>
              <table border="0" cellpadding="2" width="100%" cellspacing="1" class="small">
                  <tr>
                    <td height="30" class="text12"><p class="text1"><strong>Disclaimer:</strong></p>
                      <p class="text1">Thank you for your reservation, we are in the midst of  processing your reservation. We will revert when your reservation is confirmed.  Kindly check your status of reservation under the &lsquo;Confirmation Record&rsquo;.</p>
                      <p>&nbsp;</p></td>
                  </tr>
                  <tr>
                    <td height="1" bgcolor="#999999"></td>
                  </tr>
                  <tr>
                    <td height="30" class="TextH1"><span class="require">
                      <?=FORM_REQUIRED_INFORMATION?>
                    </span></td>
                  </tr>
                  <tr>
                    <td class="text12"><input name="btnAdd" type="submit" class="btn" id="btnAdd" value="<?=BUTTON_OK; ?>" />                      <input name="btnCancel" type="submit" class="btn" id="btnCancel" value="<?=BUTTON_CANCEL; ?>" />
                      <input name="btnBack" type="button" class="btn" id="btnBack" onclick="location.href='m_reservAppr.php'" value="<?=BUTTON_BACK; ?>" />                      
                      &nbsp;</td>
                  </tr>
              </table></td>
          </tr>
        </table>
<!--        <div class="scroll"> --></td>
      </tr>
    </table>
		</form>		</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><? include_once "includes/bottom.php" ?></td>
  </tr>
</table>
</body>
</html>