<?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 type="text/javascript">
$(document).ready(function(){
		$("#category2august").hide();
$("#event").change(function(){

   
        if($('#event option:selected').filter('#August').length) {
        //$('#category').children('option[value="select"]').remove();
         $("#category").hide();
          $("#category2august").show();
       
     
     
}
if($('#event option:selected').filter('#27july').length) {
         $("#category").show();
          $("#category2august").hide();
   
}
if($('#event option:selected').filter('#26july').length) {
           $("#category").show();
          $("#category2august").hide();
    
}
});
});
</script>
<script>
function resetForm() { 	
  //$('#signupForm').reset();
document.getElementById("signupForm").reset();
$("#moreBuyer").empty();
}
</script>

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

</script>

<script type="text/javascript">

 $(document).ready(function(){
   var buyers = $("#buyers").val();
    for ( var i = 0; i < buyers; i++ ) {
	      $('<table><tr><td><span>First Name: </span></td><td><input type="text" name="fname[]" size="15" maxlength="50" class="inputbox" value="<?php echo $_POST['fname'][$i] ?>"  />' +
		    '<span class="boldtext error">*&nbsp;</span><br/></td></tr>'+
		    '<tr><td>Middle Name:</td><td><input type="text" name="mname[]" size="15" class="inputbox" value="<?php echo $_POST['mname'][$i] ?>"  />'+
		    '<span class="boldtext error">&nbsp;</span><br/></td></tr>'+
		    '<tr><td>Last Name: </td><td><input type="text" name="lname[]" size="15" class="inputbox" value="<?php echo $_POST['lanem'][$i] ?>"  />'+
		    '<span class="boldtext error">*&nbsp;</span><br/></td></tr>'+		
		    '<tr><td>Age: </td><td><input type="text" name="age[]" size="7" class="inputbox" value="<?php echo $_POST['age'][$i] ?>" />'+
		    '<span class="boldtext error">*&nbsp;</span><br/></td></tr>'+		
		    '<tr><td>Email: </td><td><input type="text" name="email[]" size="15" class="inputbox" value="<?php echo $_POST['email'][i] ?>"  />'+
		      '<span class="boldtext error">&nbsp;</span><br/></td></tr></table>'+
		      ' <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'])) {      
 
	$errorFlag = 0;
    if ($_POST['fname'] == "" || $_POST['lname'] == "" || $_POST['age'] == "" || $_POST['email'] == "" || $_POST['address1'] == "" ||
     $_POST['city'] == "" || $_POST['state'] == "" || $_POST['zip'] == "" || $_POST['telephone'] == "" || $_POST['event'] == "" || $_POST['category'] == "") { 
	    $errorMsg = "Please fill all fields with *";
    } else { 
		
		$buyers = $_POST['buyers'];
		if($_POST['buyers'] > 0) {
			for($i=1;$i<=$buyers;$i++){ 
				if($_POST['fname'][$i] == "" || $_POST['lname'][$i] == "" || $_POST['age'][$i] == "") {
					$errorFlag = 1;	 
					$errorMsg = "Please fill all fields with * for additional buyers also";
				}
			}
		}
		if (!$errorFlag) {
		
			if($_POST['event'] == 2){
			//echo "Selected Event Is 2nd";
			//print_r($_POST['category']);
			//exit;
			$donationAmount = 0;

            if($_POST['category'] == "vvip") {
                $donationAmount = "149";
            } else if($_POST['category'] == "vip") {
                $donationAmount = "99";
            } else if($_POST['category'] == "Honor"){
            	$donationAmount = "79";
            } else if($_POST['category'] == "general") {
                $donationAmount = "49";
            } else if($_POST['category'] == "balcony") {
                $donationAmount = "30";
            }
            $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 = "";
                for($i=0;$i<$totalEntries;$i++) {
                
                //Add entry to salsa
        
        	 $firstName_s = $_POST['fname'][$i];
              $mname_s = $_POST['mname'][$i];
              $lname_s = $_POST['lname'][$i];
              $email_s = $_POST['email'][$i];
              $telephone_s = $_POST['telephone'];
              $city_s = $_POST['city'];
              $state_s =  $_POST['state'];
              $zip_s = $_POST['zip'];
        
           $map = array(
              'name_first' => 'First_Name',
              'name_middle' => 'MI',
              'name_last' => 'Last_Name',
              'email' => 'Email',
              'phone' => 'Cell_Phone',
              'city' => 'City',
              'state' => 'State',
              'zip' => 'Zip',
              );
             
            //Supporter data
            $supporter = array(
              'name_first' => $firstName_s,
              'name_middle' => $mname_s,
              'name_last' => $lname_s,
              'email' => $email_s,
              'phone' => $telephone_s,
              'city' => $city_s,
              'state' => $state_s,
              'zip' => $zip_s,
              );
              
            $authUrl = "hq-salsa3.salsalabs.com/api/authenticate.sjs?email=".urlencode("webmaster@unitedsikhs.org")."&password=".urlencode("dev007singh");
            //The Request URL so far...
              $url = 'https://hq-salsa3.salsalabs.com/save?json&object=supporter&organization_KEY=50846&link=groups&linkKey=128636';


            //Complete the Request URL with supporter data
            foreach ( $supporter as $key => $value ) {
              if ( isset($map[$key]) ) {
                $url .= sprintf('&%s=%s', $map[$key], urlencode($value));
              }
            }
             
            //Send the request
            //echo "Sending..<br/>";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $authUrl);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
            curl_setopt($ch, CURLOPT_POSTFIELDS, '');
            $lastResponse = curl_exec($ch);
            //echo $lastResponse;
            $lastResponseHeaders = curl_getinfo($ch);
            //print_r($lastResponseHeaders);

            //echo "<br/>Saving...";
            curl_setopt($ch, CURLOPT_URL, $url);
            $lastResponse = curl_exec($ch);
            //echo $lastResponse;
            $lastResponseHeaders = curl_getinfo($ch);
            //print_r($lastResponseHeaders);

            curl_close($ch);
            //End of inserting entry into salsa DB
        //end code of salsa
               
                    $donation = $donationAmount;
                    if($_POST['age'][$i] <=7) {
                     $donation = 0;
                    }
                    $uniqueKey = unique_id(16);
					$insertedDate = date('Y-m-d H:i:s');
                    $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."',
							                    '".$insertedDate."',
							                    '".$userId."'
						                    )";
            		$fireQuery = mysql_query($sql);
	                if($i==0){
	                    $userId = mysql_insert_id();
	                    mysql_query("UPDATE user_information SET parent_id = '".$userId."'  WHERE id= '".$userId."'");
	                }

            	}
				//echo "sucess";exit;
            	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 if($_POST['event'] == 26 || $_POST['event'] == 27){
			
			//echo "Selected Event Is Both";
			//print_r($_POST['category_26']);
			//exit;
			
			
			//Insert User information in database
			//echo "--->2";exit;
            $donationAmount = 0;

            if($_POST['category_26'] == "vvip") {
                $donationAmount = "250";
            } else if($_POST['category_26'] == "vip") {
                $donationAmount = "150";
            } else if($_POST['category_26'] == "general") {
                $donationAmount = "100";
            }
            $totalEntries 		= count($_POST['fname']);                
            $receiptGenerated   = mysql_query("select * from ticket_master 
												WHERE tkt_type='".$_POST['category_26']."' 
												AND event_number = '".$_POST['event']."'");
            $receiptLimit       = mysql_fetch_array($receiptGenerated);               
            $lastID 			= $receiptLimit['tkt_current'] + $totalEntries;

            if($lastID <= $receiptLimit['tkt_end']){
                $userId = "";
                for($i=0;$i<$totalEntries;$i++) {
                
                //Add entry to salsa
        
        	 $firstName_s = $_POST['fname'][$i];
              $mname_s = $_POST['mname'][$i];
              $lname_s = $_POST['lname'][$i];
              $email_s = $_POST['email'][$i];
              $telephone_s = $_POST['telephone'];
              $city_s = $_POST['city'];
              $state_s =  $_POST['state'];
              $zip_s = $_POST['zip'];
        
           $map = array(
              'name_first' => 'First_Name',
              'name_middle' => 'MI',
              'name_last' => 'Last_Name',
              'email' => 'Email',
              'phone' => 'Cell_Phone',
              'city' => 'City',
              'state' => 'State',
              'zip' => 'Zip',
              );
             
            //Supporter data
            $supporter = array(
              'name_first' => $firstName_s,
              'name_middle' => $mname_s,
              'name_last' => $lname_s,
              'email' => $email_s,
              'phone' => $telephone_s,
              'city' => $city_s,
              'state' => $state_s,
              'zip' => $zip_s,
              );
              
            $authUrl = "hq-salsa3.salsalabs.com/api/authenticate.sjs?email=".urlencode("webmaster@unitedsikhs.org")."&password=".urlencode("dev007singh");
            //The Request URL so far...
              $url = 'https://hq-salsa3.salsalabs.com/save?json&object=supporter&organization_KEY=50846&link=groups&linkKey=128636';


            //Complete the Request URL with supporter data
            foreach ( $supporter as $key => $value ) {
              if ( isset($map[$key]) ) {
                $url .= sprintf('&%s=%s', $map[$key], urlencode($value));
              }
            }
             
            //Send the request
            //echo "Sending..<br/>";
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $authUrl);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: 0'));
            curl_setopt($ch, CURLOPT_POSTFIELDS, '');
            $lastResponse = curl_exec($ch);
            //echo $lastResponse;
            $lastResponseHeaders = curl_getinfo($ch);
            //print_r($lastResponseHeaders);

            //echo "<br/>Saving...";
            curl_setopt($ch, CURLOPT_URL, $url);
            $lastResponse = curl_exec($ch);
            //echo $lastResponse;
            $lastResponseHeaders = curl_getinfo($ch);
            //print_r($lastResponseHeaders);

            curl_close($ch);
            //End of inserting entry into salsa DB
        //end code of salsa
               
                    $donation = $donationAmount;
                    if($_POST['age'][$i] <=10) {
                     $donation = $donation - 50;
                    }
                    $uniqueKey = unique_id(16);
					$insertedDate = date('Y-m-d H:i:s');
                    $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_26']."',
							                    '".$_POST['email'][$i]."',
							                    '0',
							                    '".$donation."',
							                    '0',
							                    '',
							                    '".$uniqueKey."',
							                    '".$insertedDate."',
							                    '".$userId."'
						                    )";
            		$fireQuery = mysql_query($sql);
	                if($i==0){
	                    $userId = mysql_insert_id();
	                    mysql_query("UPDATE user_information SET parent_id = '".$userId."'  WHERE id= '".$userId."'");
	                }

            	}
				//echo "sucess";exit;
            	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 {
		
			//No operation here
		}
		
	} 

}

?>



<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/>
		<h4>For Washington DC and NYC events :- </h4></br> 
		<h3> General Admission : $100,    VIP : $150,   VVIP Seat : $250 </h3><br/><br/>
		<h4>For California event :- </h4></br> 
		<h3> General Admission : $49,     Honor : $79,     VIP : $99,      VVIP : $149</h3><br/>
		<h4 style="color:#934993">* Tickets running out, Don't miss last chance to BUY 3 GET 1 FREE. Must provide proof of purchase of 3 tickets at door to gain entry.</h4><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>State<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 id="event" name="event" id="event" style="width:50%">
					<option value="">Select Event </option>
					<option id="26july" value="26" <?php if($_POST['event']=="26"){ echo "selected";} ?>>A Night With Rabbi Shergill - DC (26th July 2014: 7:30pm)</option>
					<option id="27july" value="27" <?php if($_POST['event']=="27"){ echo "selected";} ?>>A Night With Rabbi Shergill - NYC (27th July: 6:30pm)</option>
					<option id="August" value="2" <?php if($_POST['event']=="2"){ echo "selected";} ?>>A Night With Rabbi Shergill - CA (2rd August: 6:00pm)</option>
				</select>
			</td>			
		</tr>
				<tr><td colspan="2">&nbsp;</td></tr>
		
		<tr>
			<td id="category_selected">Category Selected<span class="error">*</span></td>
			<td>
				<select id="category" name="category_26" id="category" style="width:50%">
					<option value="select"<?php if($_POST['category_26']=="select"){echo "selected";}?>>Select Cateory </option>	
					<option id = "vvip" value="vvip" <?php if($_POST['category_26']=="vvip"){ echo "selected";} ?>>VVIP - $250/ticket</option>
					<option id = "vip" value="vip" <?php if($_POST['category_26']=="vip"){ echo "selected";} ?>>VIP - $150/ticket</option>
					<option id = "general" value="general" <?php if($_POST['category_26']=="general"){ echo "selected";} ?>>General - $100/ticket</option>
					
				</select>
			
				<select id="category2august" name="category" id="category" style="width:50%">
					<option id = "select_2" value="select_2"<?php if($_POST['category']=="select_2"){echo "selected";}?>>Select Cateory </option>
					<option id = "vvip_2" value="vvip" <?php if($_POST['category']=="vvip"){ echo "selected";} ?>>VVIP - $149/ticket</option>
					<option id = "vip_2" value="vip" <?php if($_POST['category']=="vip"){ echo "selected";} ?>>VIP - $99/ticket</option>
					<option id = "Honor_2" value="Honor" <?php if($_POST['category']=="Honor"){ echo "selected";} ?>>Honor - $79/ticket</option>
					<option id = "general_2" value="general" <?php if($_POST['category']=="general"){ echo "selected";} ?>>General - $49/ticket</option>
					<option id = "balcony_2" value="balcony" <?php if($_POST['category']=="balcony"){ echo "selected";} ?>>Balcony - $30/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">(Note: If you want to purchase more tickets, simply put how many numbers you want in above text area.)</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"/>
			<input type="button" value="Reset Form" onclick="resetForm()"/>
			</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";?>  
