<?php
//Create a dropdown and fill country name
//Argument: $mastertableid=mastertable id for which the dropdown will populate.
function create_shipping_dropdown($mastertableid)
{
	$sqlco = "SELECT id,name FROM single_field_table  where mastertableid='$mastertableid'";
	$result_shipping_charge = mysql_query($sqlco);
	$row_shipping_charge  = mysql_num_rows($result_shipping_charge );
	if($row_shipping_charge > 0)
	{  
		$field = "<select name='country' id='country' onChange='sbmt()' class='selectinputBox'";
		$field .= '>';
		while($data_shipping_charge = mysql_fetch_object($result_shipping_charge))
		{			
			   $values=$data_shipping_charge->id;
			   $field .= '<option value="' . $values. '"';
			   if(isset($_POST['country']))
			   {
			     $country=$_POST['country'];
			   }	
			   else
			   {
			     $country=0;
			   }
			   if($values==$country) 
			   {
				  $field .= ' selected';
			   }	
			   $field .= '>' .$data_shipping_charge->name . '</option>';
		 }
		 $field .= '</select>';
		 echo  $field;
	}
	else
	{
	   echo 'FREE SHIPPING';
	}
}

//Get the amount charged for the selected zone
function get_shipping_charge($zid)
{

		 
		  $sql="select price from  zone_rate  where zone='$zid'";  
		    $res_shipping_charge = mysql_query($sql);
		   $count_shipping_charge  = mysql_num_rows($res_shipping_charge );
		   if($count_shipping_charge > 0)
		   {  
				$row=mysql_fetch_object($res_shipping_charge);
				return $row->price;
		   }
		   else
		      return 0; 
		   
  
   
}

function create_dropdown($mastertableid)
{
	 $sqlco = "SELECT name FROM single_field_table  where mastertableid='$mastertableid'";
	$result_shipping_charge = mysql_query($sqlco);
	$row_shipping_charge  = mysql_num_rows($result_shipping_charge );
	if($row_shipping_charge > 0)
	{  
		$field = "<select name='drpdwn' id='drpdwn' class=seletedinputBox";
		$field .= '>';
		while($data_shipping_charge = mysql_fetch_object($result_shipping_charge))
		{			
			   $values=$data_shipping_charge->name;
			   $field .= '<option value="' . $values. '"';
			   if(isset($_POST['drpdwn']))
			   {
			     $country=$_POST['drpdwn'];
			   }	
			   else
			   {
			     $country=0;
			   }
			   if($values==$country) 
			   {
				  $field .= ' selected';
			   }	
			   $field .= '>' .$data_shipping_charge->name . '</option>';
		 }
		 $field .= '</select>';
		 echo  $field;
	}
}


function create_dropdownmenu($mastertableid,$name)
{
	 $sqlco = "SELECT name FROM single_field_table where mastertableid='$mastertableid'";
	$result_shipping_charge = mysql_query($sqlco);
	$row_shipping_charge  = mysql_num_rows($result_shipping_charge );
	if($row_shipping_charge > 0)
	{  
		$field = "<select name='$name' id='$name'";
		$field .= '>';
		while($data_shipping_charge = mysql_fetch_object($result_shipping_charge))
		{			
			   $values=$data_shipping_charge->name;
			   $field .= '<option value="' . $values. '"';
			   if(isset($_POST['$name']))
			   {
			     $country=$_POST['$name'];
			   }	
			   else
			   {
			     $country=0;
			   }
			   if($values==$country) 
			   {
				  $field .= ' selected';
			   }	
			   $field .= '>' .$data_shipping_charge->name . '</option>';
		 }
		 $field .= '</select>';
		 echo  $field;
	}
}


function write_to_db()
{
	$fname=$_POST['firstName'];
	$lname=$_POST['lastName'];
	$phone=$_POST['phone'];
	$phone_ext=$_POST['phone_ext'];
	$evening_phone=$_POST['evening_phone'];
	$evening_phone_ext=$_POST['evening_phone_ext'];
	$email=$_POST['email'];
	$attn_name=$_POST['attn_name'];
	$address1=$_POST['address1'];
	$address2=$_POST['address2'];
	$city=$_POST['city'];
	$shipping_country=$_POST['shipping_country'];
	$postalCode=$_POST['postalCode'];
	
	$bill_address1=$_POST['bill_address1'];
	$bill_address2=$_POST['bill_address2'];
	$bill_city=$_POST['bill_city'];
	$bill_state=$_POST['billing_country'];
	$bill_postalCode=$_POST['bill_postalCode'];
	$subtotal=$_POST['subtotal'];
	$shipping_charge=$_POST['shipping'];
	$taxprice=$_POST['tax'];
	$surcharge=$_POST['surcharge'];
	$order_processing_charge=$_POST['orderprocessing'];
	$grandtotal=$_POST['grandtotal'];
	$info_source=$_POST['hearAbout'];
	$email_preference=$_POST['opt_in_email'];
	$order_date=date('Y-m-d');
	
	$cookid = session_id();
	$sql_data_array = array(	
	'cart_sessionkey'=>$cookid,					     
	'firstname' =>$fname,
	'lastname' =>$lname, 
	'phone' =>$phone, 
	'phone_ext' =>$phone_ext, 
	'evening_phone' =>$evening_phone, 
	'evening_phone_ext' =>$evening_phone_ext,
	'email' =>$email, 
	'address1' =>$address1, 
	'address2' =>$address2,
	'city' =>$city,
	'state' =>$shipping_country,
	'postalCode' =>$postalCode,
	'attn_name' =>$attn_name, 
	'bill_address1' =>$bill_address1,
	'bill_address2' =>$bill_address2,
	'bill_city' =>$bill_city, 
	'bill_state' =>$bill_state, 
	'bill_postalCode' =>$bill_postalCode, 
	'subtotal' =>$subtotal, 
	'shipping_charge' =>$shipping_charge,
	'taxprice' =>$taxprice, 
	'surcharge' =>$surcharge, 
	'order_processing_charge' =>$order_processing_charge, 
	'grandtotal' =>$grandtotal,
	'hearAbout' =>$info_source, 
	'email_preference' =>$email_preference, 
	'order_date' =>$order_date   
	);
	tep_db_perform("orders", $sql_data_array,"insert");
}

            
function xyz()
{			
			$req = 'cmd=_notify-validate';
			$i = 0;
			foreach ($_POST as $key => $value) 
			{
			$value = urlencode(stripslashes($value));
			$req .= "&$key=$value";
			}
			
			// post back to PayPal system to validate
			$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
			$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
			$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
			$fp = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
			// assign posted variables to local variables
			$item_name = $_POST['item_name'];
			$item_number = $_POST['item_number'];
			$payment_status = $_POST['payment_status'];
			$payment_amount = $_POST['mc_gross'];
			$payment_currency = $_POST['mc_currency'];
			$txn_id = $_POST['txn_id'];
			$receiver_email = $_POST['receiver_email'];
			$payer_email = $_POST['payer_email'];
			$customdata = $_POST['custom'];
			$first_name = $_POST['first_name'];
			$last_name= $_POST['last_name'];
			$email = $_POST['payer_email'];
			
			$data=explode("_",$customdata);
			$month_year=$data[2];
			$duration=$data[1];
			$listdate=$data[3];
			
			if($month_year=="Month")
			{
			  $add_days=$duration*30;
			}
			else if($month_year=="Year")
			{
			$add_days=$duration*365;
			
			}
			if (!$fp) 
			{
			// HTTP ERROR
			} 
			else
			{
				fputs ($fp, $header . $req);
				while (!feof($fp)) 
				{
					$res = fgets ($fp, 1024);
					if (strcmp ($res, "VERIFIED") == 0) 
					{
					$paid = "yes";					
					}
				 }
			}	 
				 //if paid
				 if(isset($paid))
				 {
				
					$query_del="UPDATE realestate SET expdate =DATE_ADD('$listdate' , INTERVAL $add_days DAY ) WHERE id = '$customdata'";
					//$query_del="UPDATE service_providers SET is_paid = 'Yes' WHERE id = '$customdata'";
					$exec_query=mysql_query($query_del);			
					echo "Thank you for the payment";
				 }
				else
				{
					echo "Fradulent activity detected";
				}	
					
}			
?>



