<?php include "../header.php";?>   
<?php include "config.php"; ?> 

<meta charset="utf-8">


<style>
#main {
 margin-top:30px;
}
.error {
 color: red;
 font-size:11px;
}
.displayError {
 color: red;
 font-size:14px;
}
</style>

<script>
 $(document).ready(function(){
    $("#addBuyer").click(function () {
   var buyers = $("#buyers").val();
    for ( var i = 0; i < buyers; i++ ) {
	      $('<input type="text" name="fname[]" size="15" maxlength="50" class="inputbox" value="<?php echo $_POST['fname'][i]?>" placeholder="First name" />' +
		    '<span class="boldtext error">*&nbsp;</span>'+
		    '<input type="text" name="mname[]" size="15" class="inputbox" value="<?php echo $_POST['mname'][$i] ?>" placeholder="Middle name" />'+
		    '<span class="boldtext error">*&nbsp;</span>'+
		    '<input type="text" name="lname[]" size="15" class="inputbox" value="<?php echo $_POST['lname'][$i] ?>" placeholder="Last name" />'+
		    '<span class="boldtext error">*&nbsp;</span>'+		
		    '<input type="text" name="age[]" size="7" class="inputbox" value="<?php echo $_POST['age'][$i] ?>" placeholder="Age" />'+
		    '<span class="boldtext error">*&nbsp;</span>'+		
		    '<input type="text" name="email[]" size="15" class="inputbox" value="<?php echo $_POST['email'][$i] ?>" placeholder="Email" />'+
		    '<span class="boldtext error">*&nbsp;</span>'+
		    '<br />').appendTo("#moreBuyer");
	   }   
    });
   
  });
  

</script>

<script>
 $(document).ready(function(){
   var buyers = $("#buyers").val();
    for ( var i = 0; i < buyers; i++ ) {
	      $('<input type="text" name="fname[]" size="15" maxlength="50" class="inputbox" value="<?php echo $_POST['fname'][$i] ?>" placeholder="First name" />' +
		    '<span class="boldtext error">*&nbsp;</span>'+
		    '<input type="text" name="mname[]" size="15" class="inputbox" value="<?php echo $_POST['mname'][$i] ?>" placeholder="Middle name" />'+
		    '<span class="boldtext error">&nbsp;</span>'+
		    '<input type="text" name="lname[]" size="15" class="inputbox" value="<?php echo $_POST['lanem'][$i] ?>" placeholder="Last name" />'+
		    '<span class="boldtext error">*&nbsp;</span>'+		
		    '<input type="text" name="age[]" size="7" class="inputbox" value="<?php echo $_POST['age'][$i] ?>" placeholder="Age" />'+
		    '<span class="boldtext error">*&nbsp;</span>'+		
		    '<input type="text" name="email[]" size="15" class="inputbox" value="<?php echo $_POST['email'][i] ?>" placeholder="Email" />'+
		      '<span class="boldtext error">*&nbsp;</span>'+
		      ' <br />').appendTo("#moreBuyer");
	   }   
   
  });
  

</script>

<?php



 function unique_id($l = 8) {
    return substr(md5(uniqid(mt_rand(), true)), 0, $l);
}

function search_array($needle, $haystack) {
     if(in_array($needle, $haystack)) {
          return true;
     }
     foreach($haystack as $element) {
          if(is_array($element) && search_array($needle, $element))
               return true;
     }

   return false;
}



if(isset($_POST['submit'])) {
  
    //print_r($_POST);exit;
   //if(!search_array("", $_POST)) {       

   /* echo $fnamecount = count($_POST['fname']);
    echo $mnamecount = count($_POST['mname']);
    echo $lnamecount = count($_POST['lname']);
    echo $agecount = count($_POST['age']);
    echo $emailcount = count($_POST['email']);

    if($fnamecount== $mnamecount && $mnamecount == $lnamecount && $lnamecount == $agecount && $agecount== $emailcount){
        echo "ok";
    }
    echo "--->".exit;*/

    
	    if ($_POST['fname'] == "" || $_POST['lname'] == "" || $_POST['age'] == "" || $_POST['email'] == "" || $_POST['address1'] == "" ||
	     $_POST['city'] == "" || $_POST['state'] == "" || $_POST['zip'] == "" || $_POST['telephone'] == "" || $_POST['event'] == "" || $_POST['category'] == "") { echo "-->2";
		    $errorMsg = "Please fill all fields with *";
        } else if ($_POST['buyers']!= "") { echo "-->1";
				$ab = $_POST['buyers'];
				for($i=1;$i<=$ab;$i++){ echo "-->4";
					if ($_POST['fname'][$i] == "" || $_POST['lname'][$i] == "" || $_POST['age'][$i] == "" || $_POST['email'][$i] == "" ) {
						$errorMsg = "Please fill all fields with * for additional buyers also";
					}
			}

		} else { echo "-->3";
echo "sucess";exit;
                $donationAmount = 0;
                if($_POST['category'] == "vvip") {
	                $donationAmount = "250";
                } else if($_POST['category'] == "vip") {
	                $donationAmount = "150";
                } else if($_POST['category'] == "general") {
	                $donationAmount = "100";
                }
                $totalEntries = count($_POST['fname']);
                
                $receiptGenerated   = mysql_query("select * from ticket_master WHERE tkt_type='".$_POST['category']."' AND event_number = '".$_POST['event']."'");
                $receiptLimit       = mysql_fetch_array($receiptGenerated);

               
                $lastID = $receiptLimit['tkt_current'] + $totalEntries;

                if($lastID <= $receiptLimit['tkt_end']){
                        $userId ="";
                //Code to insert details in database           
                    for($i=0;$i<$totalEntries;$i++){
                   
                        $donation = $donationAmount;
                        if($_POST['age'][$i] <=10){
                         $donation = $donation - 50;
                        }
                        $uniqueKey = unique_id(16);
                        $sql = "INSERT INTO user_information (`first_name`, `middle_name`, `last_name`, `age`, `address1`, `address2`, `city`, `state`,
                             `zip_code`, `telephone`, `event_selected`, `category_selected`, `email_address`,
                              `is_donated`,`donation_amount`,`is_ticket_generated`,`ticket_number`,`unique_number`,`inserted_date`,parent_id ) 
						                    VALUES ('".$_POST['fname'][$i]."',
								                    '".$_POST['mname'][$i]."',
								                    '".$_POST['lname'][$i]."',
								                    '".$_POST['age'][$i]."',
								                    '".$_POST['address1']."',
								                    '".$_POST['address2']."',
								                    '".$_POST['city']."',
								                    '".$_POST['state']."',
								                    '".$_POST['zip']."',
								                    '".$_POST['telephone']."',
								                    '".$_POST['event']."',
								                    '".$_POST['category']."',
								                    '".$_POST['email'][$i]."',
								                    '0',
								                    '".$donation."',
								                    '0',
								                    '',
								                    '".$uniqueKey."',
								                    '',
								                    '".$userId."'
							                    )";
                    $fireQuery = mysql_query($sql);
                    if($i==0){
                        $userId = mysql_insert_id();
                        mysql_query("UPDATE user_information SET parent_id = '".$userId."'  WHERE id= '".$userId."'");
                    }

                }

                if($fireQuery) { 
                ?>
            <script type="text/javascript">

                window.location = "pay.php?id=<?php echo $userId?>"

            </script>
                <?php	
	
                } else {
	                //Error
                }
            }else {
                $errorMsg = "All seats are booked!!!";
            }
	    }
	
	//}else {

	    //$errorMsg = "Please fill all fields";
	//}
	


}

?>



<div  class="">
<div id="main">
	<div class="displayError"><?php if($errorMsg) { echo $errorMsg;}?></div>
<form id="signupForm" method="POST" name="signupForm" action="">
		<h3> Fill the following form to buy Gala 2014 tickets</h3><br/><br/>
		<h3> General Admission : $100,    VIP : $150,   VVIP Seat : $250 </h3><br/><br/>
	<table>
		<tr>
			<td>First Name<span class="error">*</span></td>
			<td><input type="text" name="fname[]" value="<?php echo $_POST['fname'][0] ?>"></td>			
		</tr>
		<tr><td colspan="2">&nbsp;</td></tr>
		<tr>
			<td>Middle Name</td>
			<td><input type="text" name="mname[]" value="<?php echo $_POST['mname'][0] ?>"></td>			
		</tr>
				<tr><td colspan="2">&nbsp;</td></tr>
		<tr>
			<td>Last Name<span class="error">*</span></td>
			<td><input type="text" name="lname[]" value="<?php echo $_POST['lname'][0] ?>"></td>			
		</tr>
				<tr><td colspan="2">&nbsp;</td></tr>
		<tr>
			<td>Age<span class="error">*</span></td>
			<td><input type="text" name="age[]" value="<?php echo $_POST['age'][0] ?>"></td>			
		</tr>
				<tr><td colspan="2">&nbsp;</td></tr>
		<tr>
			<td>Email Address<span class="error">*</span></td>
			<td><input type="text" name="email[]" value="<?php echo $_POST['email'][0] ?>"></td>			
		</tr>
				<tr><td colspan="2">&nbsp;</td></tr>
		<tr>
			<td>Address<span class="error">*</span></td>
			<td><input type="text" name="address1" value="<?php echo $_POST['address1'] ?>"></td>			
		</tr>
				<tr><td colspan="2">&nbsp;</td></tr>
		<tr>
			<td>Street Address</td>
			<td><input type="text" name="address2" value="<?php echo $_POST['address2'] ?>"></td>			
		</tr>
				<tr><td colspan="2">&nbsp;</td></tr>
		<tr>
			<td>City<span class="error">*</span></td>
			<td><input type="text" name="city" value="<?php echo $_POST['city'] ?>"></td>			
		</tr>
				<tr><td colspan="2">&nbsp;</td></tr>
		<tr>
			<td>Province<span class="error">*</span></td>
			<td><input type="text" name="state" value="<?php echo $_POST['state'] ?>"></td>			
		</tr>
				<tr><td colspan="2">&nbsp;</td></tr>
		<tr>
			<td>Zip<span class="error">*</span></td>
			<td><input type="text" name="zip" value="<?php echo $_POST['zip'] ?>"></td>			
		</tr>
				<tr><td colspan="2">&nbsp;</td></tr>
		<tr>
			<td>Telephone<span class="error">*</span></td>
			<td><input type="text" name="telephone" value="<?php echo $_POST['telephone'] ?>"></td>			
		</tr>
				<tr><td colspan="2">&nbsp;</td></tr>

		<tr>
			<td>Select Event<span class="error">*</span></td>
			<td>
				<select name="event" id="event" style="width:50%">
					<option value="">Select Event </option>
					<option value="26" <?php if($_POST['event']=="26"){ echo "selected";} ?> >A Night With Rabbi Shergill - DC (26th July 2014: 7:30pm)</option>
					<option value="27" <?php if($_POST['event']=="27"){ echo "selected";} ?>>A Night with Rabbi Shergill - NYC (27th July: 6:30pm)</option>
				</select>
			</td>			
		</tr>
				<tr><td colspan="2">&nbsp;</td></tr>
		<tr>
			<td>Category Selected<span class="error">*</span></td>
			<td>
				<select name="category" id="category" style="width:50%">
					<option value="">Select Cateory </option>	
					<option value="vvip" <?php if($_POST['category']=="vvip"){ echo "selected";} ?>>VVIP - $250/ticket</option>
					<option value="vip" <?php if($_POST['category']=="vip"){ echo "selected";} ?>>VIP - $150/ticket</option>
					<option value="general" <?php if($_POST['category']=="general"){ echo "selected";} ?>>General - $100/ticket</option>
				</select>

			</td>			
		</tr>
				<tr><td colspan="2">&nbsp;</td></tr>
		<tr>
            <td> Additional Buyers </td>
			<td>
			    <input type="text" name="buyers" id="buyers" placeholder="Number of buyers" value="<?php echo $_POST['buyers'] ?>">			
			    <input class="button" type="button" name="addBuyer" value="Bring More Guests" id="addBuyer"/>
			</td>
        </tr>
		
		<tr><td colspan="2">&nbsp;</td></tr>
		<tr>

			<td colspan="2" id="moreBuyer">
			</td>
        </tr>
        <tr><td colspan="2">&nbsp;</td></tr>
		<tr>
			<td style="text-align:center" colspan="2">		
			
			<input class="submit" type="submit" name="submit" value="Submit"/>
			</td>
	<!--		<img src="http://unitedsikhs.org/images/btn_donate_LG.gif" id="submit_donate"></td>			-->
		</tr>
		<tr><td colspan="2">&nbsp;</td></tr>
		
	</table>

		<!--<p>
			
		</p>-->
	
</form>

</div>
</div>




<?include "../footer.php";?>  
