<?php
	/*
	** File: order_billing
	** Description: get billing info
	** Version: $Revision: 1.26 $
	** Created: 06/25/1999
	** Author: Leon Atkinson
	** Email: leon@clearink.com
	** CVS Author: $Author: leon $
	** Last Revised: $Date: 1999/12/17 22:08:31 $
	** Log : $Log: order_billing,v $
	** Log : Revision 1.26  1999/12/17 22:08:31  leon
	** Log : Added function to print invoice totals, consequently updated printTableRow
	** Log : to allow passing of styles.
	** Log :
	** Log : Revision 1.25  1999/12/16 21:58:45  leon
	** Log : Refactored calculateTotal.  Now with fewer queries! Gift Wrap was being
	** Log : refered to as GiftWrap in the array returned by calculateTotal, so I changed
	** Log : it in the few places it was.  That allowed me to use a general strategy
	** Log : for storing fee totals.
	** Log :
	** Log : Revision 1.24  1999/12/08 00:55:10  leon
	** Log : Pulled functions for creating table rows out of addressFunctions and
	** Log : generalized them.  I'm leaving wrappers for the old functions, but I
	** Log : will removed them in the coming weeks.
	** Log :
	** Log : Revision 1.23  1999/12/06 21:17:27  leon
	** Log : ECML compliance
	** Log : Overhauled address functions to use ECML form field names.  Ditto with
	** Log : the billing fields on the checkout forms.  Note the change to build.sql.
	** Log : Yes, the address table changed to accomodate new fields.  The billing
	** Log : table changed too.
	** Log :
	** Log : Revision 1.22  1999/11/08 22:32:27  leon
	** Log : Added prepareText() function to use everywhere we were using htmlentities.
	** Log :
	** Log : Revision 1.21  1999/11/05 18:17:53  leon
	** Log : Reorganized coupon code.
	** Log :
	** Log : Revision 1.20  1999/11/04 21:35:43  leon
	** Log : Cleaned up the use of paths.  Paths are now all set in index.php3 as
	** Log : constants.  Example settings for Apache/Unix, Apache/Win32, and IIS.
	** Log :
	** Log : Revision 1.19  1999/11/02 20:49:43  leon
	** Log : Added constant to control use of coupons
	** Log :
	** Log : Revision 1.18  1999/11/02 18:23:13  chris
	** Log : Added support for coupons.
	** Log :
	** Log : Revision 1.17  1999/10/25 22:41:35  leon
	** Log : Made constant for privacy statement, printed statement on relavant pages.
	** Log :
	** Log : Revision 1.16  1999/10/13 22:19:04  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.15  1999/10/13 00:42:31  chris
	** Log : Moving text into constants in preparation of localization support.
	** Log :
	** Log : Revision 1.14  1999/10/11 23:04:10  leon
	** Log : Removed all require()'s, added block to stop double include()'s
	** Log :
	** Log : Require wasn't giving any performance advantage because it was always
	** Log : being called with a variable.  Futhermore, using return inside a
	** Log : require()'d file does not work in PHP4.
	** Log :
	** Log : Revision 1.13  1999/10/04 22:09:56  leon
	** Log : Removed getInvoiceID.  The active invoice is now added to $UserInfo
	** Log : by the check sid functionality in init.
	** Log :
	** Log : Revision 1.12  1999/09/10 00:21:09  jj
	** Log : Changed include("addressfunctions") -> require("addressfunctions");
	** Log :
	** Log : Revision 1.11  1999/09/09 22:19:38  jj
	** Log : No longer any need to use the variable $addressFunctionsDefined.
	** Log :
	** Log : Revision 1.10  1999/08/23 23:56:54  jj
	** Log : Three levels of defaults in order_billing: previous values, $UserInfo, shipping values.
	** Log :
	** Log : Revision 1.9  1999/08/18 22:32:18  leon
	** Log : Returning opening PHP tag to XML-friendly tag
	** Log :
	** Log : Revision 1.8  1999/08/18 16:23:12  jj
	** Log : These screens now use addressFunctions for all of their address needs.
	** Log :
	** Log : Revision 1.7  1999/08/17 17:56:42  jj
	** Log : Modularized everything to do with addresses. Please see include/addressFunctions
	** Log :
	** Log : Revision 1.6  1999/08/11 19:43:32  jj
	** Log : Replaced all instances of eMail with email or Email (as fitting).
	** Log :
	** Log : Revision 1.5  1999/08/10 23:16:42  jj
	** Log : Used $UserInfo for getting the user's address information rather than hitting the database.
	** Log :
	** Log : Revision 1.4  1999/08/10 21:15:26  jj
	** Log : Put the CVS variables back into the code (they were lost when I copied stuff from a live project).
	** Log :
	**
	** Copyright (c) 1999 Working Dogs.  All rights reserved.
	*/

	/* Address related functions. */
	include(APPLICATION_ROOT . "/modules/include/addressFunctions");

	/* calculateTotal function. */
	include(APPLICATION_ROOT . "/modules/include/calculateTotal");

	/* displayCoupon function. */
	if(USE_COUPONS)
	{
		/* make $coupon blank if user isn't logged in, to prevent hacking */
		if((isset($UserInfo["ID"]) == 0) AND ($UserInfo["ID"] == 0))
		{
			$coupon = "";
		}
	}
	

	/*
	** Output the totals
	*/
	$Total = calculateTotal($UserInfo['Invoice']);
	
	print("<TABLE>\n");

	print("<TR><TD COLSPAN=\"2\"><B>" . L_ORDERBILLING_SUBTOTALS . "</B></TD></TR>\n");
	
	showTotals($Total);

	print("</TABLE>\n");



	/* 
	** Get the user's billing information. Rather than have the text fields
	** start blank, we'll see if they already had values (this is the second
	** time around for the user) or we'll give them default values based on 
	** either $UserInfo or the shipping address info.
	*/
	if(isset($Ecom_BillTo_Postal_Name_First))
	{
		$billingAddress = getAddressFromEnvironment("Ecom_BillTo_");
	}
	elseif(isset($UserInfo["FirstName"]))
	{
		$billingAddress = $UserInfo["Address"];
	}
	else
	{
		//try to get the address from posted variables
		//for a shipping address
		$billingAddress = getAddressFromEnvironment("Ecom_ShipTo_");
	}	
	
	

	/* 
	** Okay, time to actually get the billing information. 
	*/ 		
	print(StartForm("order_confirm", 'post', 'VALIDATE_ORDER', TRUE, 
			array("Ecom_SchemaVersion"=>"http://www.ecml.org/version1.0")));



	print("<TABLE BORDER=\"0\">\n");
	print("<TR><TD COLSPAN=\"2\"><B>" . L_ORDERBILLING_BILLINGINFO . "</B></TD></TR>\n");


	printAddressFields(
		$billingAddress, 
		"Ecom_BillTo_", 
		"ALL", 
		0, 
		0, 
		FALSE);


	/* Billing method. */
	print("<TR>\n");
	print("<TD>" . L_ORDERBILLING_BILLINGMETHOD . "</TD>\n");
	print("<TD>");
	
	print("<SELECT NAME=\"Ecom_Payment_Card_Type\">\n");

	$Query = "SELECT ID, Name ";
	$Query .= "FROM billing_method ";
	$Query .= "ORDER BY DisplayPrecedence ";
	$DatabaseResult = mysql_query($Query, $DatabaseLink);
	while($DatabaseRow = mysql_fetch_row($DatabaseResult))
	{
		print("<OPTION VALUE=\"$DatabaseRow[0]\"");
		if(isset($Ecom_Payment_Card_Type) AND 
			($DatabaseRow[0] == $Ecom_Payment_Card_Type))
		{
			print(" SELECTED");
		}
		print(">" . prepareText($DatabaseRow[1]) . "\n");
	}
	
	print("</SELECT>\n");
	
	print("</TD>\n");
	print("</TR>\n");

	printEditRow(L_ORDERBILLING_CREDITCARDNUMBER, "Ecom_Payment_Card_Number", 
		$Ecom_Payment_Card_Number);

	/* Credit card expiration = month pull down + year pull down. */
	print("<TR>\n");
	
	print("<TD>" . L_ORDERBILLING_EXPIRATIONDATE . "</TD>\n");
	
	print("<TD><SELECT NAME=\"Ecom_Payment_Card_ExpDate_Month\">\n");
	$monthName = array(1=>"January", "February", "March",
		"April", "May", "June", "July", "August",
		"September", "October", "November", "December");
	for ($i=1 ; $i<=12 ; $i++)
	{
		print("<OPTION VALUE=\"$i\"" . 
			($Ecom_Payment_Card_ExpDate_Month == $i ? " SELECTED" : "") . 
			">$monthName[$i]\n");
	}
	print("</SELECT>\n");
	$currentDate = getdate();
	print("<SELECT NAME=\"Ecom_Payment_Card_ExpDate_Year\">\n");
	
	/* Output something like <OPTION VALUE="2001" SELECTED>2001 */
	for ($i=0 ; $i<=5 ; $i++)
	{
		$year = $currentDate["year"]+$i;
		print("<OPTION VALUE=\"$year\"" . 
			($Ecom_Payment_Card_ExpDate_Year == $year ? " SELECTED" : "") . 
			">$year\n");
	}
	print("</SELECT>\n");	
	
	print("</TD>\n");
	print("</TR>\n");

	// If the user is logged in, let them enter a coupon
	if(USE_COUPONS AND (isset($UserInfo["ID"])) AND ($UserInfo["ID"] > 0))
	{
		print("<TR>\n");
		print("<TD>Coupon Code</TD>\n");
		print("<TD>");
		print("<INPUT TYPE=\"TEXT\" NAME=\"bill_coupon\" VALUE=\"$bill_coupon\" SIZE=\"40\">\n");
		print("</TD></TR>\n");
	}

	/* Finish up the table and the form. */
	print("<TR><TD COLSPAN=\"2\">\n");
	print("<INPUT TYPE=\"SUBMIT\" VALUE=\"" . L_ORDERBILLING_SETBILLINGINFO . "\">\n");
	print("</TD></TR>\n");
	print("</TABLE>\n");
	print("</FORM>\n");

	print(PRIVACY_STATEMENT);

?>

