#!/usr/bin/perl
##############################################################################

require "/home/sites/site1/users/configstore/smodels/config.pl";
use Socket;
use DBI;

$dbh = DBI->connect("DBI:mysql:host=localhost;database=$dbname",
						"$dbuser","$dbpass", 
						{PrintError => 0, RaiseError => 1});

###########################################################
### program start

if (!(-e "setdb.lock")){
	print "Content-type: text/html\n\n";
	print "<b>The database has not been configured.</b>";
	exit(0);
}

# get input if any
&get_data;

# check to see if user has established ID.  If not, establish one
if ($FORM{'uid'} eq "") {
	# get the cookie
	@cookievalues=split(/;/,$ENV{'HTTP_COOKIE'});
	foreach $value (@cookievalues) {
		if ($value=~/storecustomer=/) {
			@cookie=split(/=/,$value);
			
			$FORM{'uid'}=$cookie[1];
			chomp($FORM{'uid'});
		}
	}
	$customer_id=$FORM{'uid'};
	
	# no cookie data?  Establish one
	if ($FORM{'uid'} eq "") {
		$FORM{'uid'}=&key_counter;
		print "Set-Cookie: storecustomer=$FORM{'uid'}; path=/; expires=Tue, 04-Oct-2061 12:00:00 GMT;\n";
	}
	
}

# Log in
if ($FORM{'login'} eq "yes") {
	$name=$dbprefix;
	$name.="customer";
	$sth=$dbh->prepare(qq{SELECT name,password FROM $name WHERE email="$FORM{'email'}"});
	$sth->execute();
	my @field=$sth->fetchrow_array();
	if ($FORM{'password'} eq $field[1]){
		print "Set-Cookie: email=$field[0]; path=/; expires=Tue, 04-Oct-2061 12:00:00 GMT;\n";
		print "Set-Cookie: password=$FORM{'password'}; path=/; expires=Tue, 04-Oct-2061 12:00:00 GMT;\n";
	} else {
		$FORM{'login'}="no";
	}	
}

# no affiliate cookie data?  Establish one
if ($FORM{'affiliate_id'} ne "") {
	print "Set-Cookie: affiliate_id=$FORM{'affiliate_id'}; path=/;\n";
	print "Set-Cookie: direct_sku=$FORM{'direct_sku'}; path=/;\n";
	$name=$dbprefix;
	$name.="affiliate";
	$sth=$dbh->prepare(qq{SELECT click_counter FROM $name WHERE affiliate_id="$FORM{'affiliate_id'}"});
	$sth->execute();
	$counter=$sth->fetchrow_array();
	$counter++;
	$dbh->do(qq{UPDATE $name SET click_counter="$counter" WHERE affiliate_id="$FORM{'affiliate_id'}"});
}

if ($FORM{'logout'} eq 'yes'){
		print "Set-Cookie: email=1; path=/; expires=Wed, 25-Sep-2002 12:00:00 GMT;\n";
		print "Set-Cookie: password=1; path=/; expires=Wed, 25-Sep-2002 12:00:00 GMT;\n";
		$FORM{'action'}="";
}

# done shopping? No more cart
if ($FORM{'action'} eq "purchase") {
	print "Set-Cookie: storecustomer=$FORM{'uid'}; path=/; expires=Tue, 01-Jan-1980 12:00:00 GMT;\n";
}

# send content header right away
print "Content-type: text/html\n\n";

$top_page=top_page($template_file);
$middle_page=middle_page($template_file);
$bottom_page=bottom_page($template_file);

if ($FORM{'login_id'} eq ''){
	my ($email, $password);
	@cookievalues=split(/;/,$ENV{'HTTP_COOKIE'});
	foreach $value (@cookievalues) {
		if ($value=~/email=/) {
			@cookie=split(/=/,$value);
			$email=$cookie[1];
			chomp($email);
		}
		if ($value=~/password=/) {
			@cookie=split(/=/,$value);
			$password=$cookie[1];
			chomp($password);
		}
	}
	$name=$dbprefix;
	$name.="customer";
	$sth=$dbh->prepare(qq{SELECT customer_id FROM $name WHERE name="$email" AND password="$password"});
	$sth->execute();
	$FORM{'login_id'}=$sth->fetchrow_array();
}

$menu=&menu;

## If no action is specified, show opening page
if ($FORM{'action'} eq "") {
	open(HOME,"frontpage.html") or @front_page="";
	@front_page=<HOME>;
	close(HOME);
	print qq| $top_page $menu $middle_page 
	@front_page
	$bottom_page |;
}

if ($FORM{'action'} eq "order") {
	open(HOME,"order.html") or $front_page="";
	$front_page=<HOME>;
	close(HOME);
	print qq| $top_page $menu $middle_page 
	$front_page
	$bottom_page |;
}

## if action is login display form
if ($FORM{'action'} eq "login" or $FORM{'login'} eq 'no') {
	if ($FORM{'login'} eq ''){
		print qq| $top_page $menu $middle_page
<h3>Customer Login</h3>
<form method=post action=$script_url>
<table cellpadding=0 cellspacing=0 border=0>
<tr><td><b>E-mail:</b></td><td><input type=text name=email></td></tr>
<tr><td><b>Password:</b></td><td><input type=password name=password></td></tr>
</table>
<input type=submit value="Login">
<input type=hidden name=action value=login>
<input type=hidden name=login value=yes>
</form>
$font <a href="$script_url?action=signup">Not registered yet?</a>
$bottom_page |;
	} elsif ($FORM{'login'} eq 'yes') {
		$name=$dbprefix;
		$name.="customer";
		$sth=$dbh->prepare(qq{SELECT name FROM $name WHERE email="$FORM{'email'}"});
		$sth->execute();
		$welcome=$sth->fetchrow_array();
		print qq| $top_page $menu $middle_page
		$welcome, you have successfully logged in. <a href="$script_url">Continue Shopping</a>.
		$bottom_page |;
	} elsif ($FORM{'login'} eq 'no') {
		print qq| $top_page $menu $middle_page
		Your username or password is incorrect. Please hit the back button on your browser to try again.
		$bottom_page |;
	}
}

## if action is search display search results
if ($FORM{'action'} eq "search") {
	$name=$dbprefix;
	$name.="products";
	$sth = $dbh->prepare(qq{SELECT sku, brand_name, product_name, short_desc, long_desc, category, price, shipping, image, thumbnail, choice1_name, choice1_option1, choice1_price1, choice1_option2, choice1_price2, choice1_option3, choice1_price3, choice1_option4, choice1_price4, choice1_option5, choice1_price5, choice1_option6, choice1_price6, choice1_option7, choice1_price7, choice1_option8, choice1_price8, choice1_option9, choice1_price9, choice1_option10, choice1_price10, choice2_name, choice2_option1, choice2_price1, choice2_option2, choice2_price2, choice2_option3, choice2_price3, choice2_option4, choice2_price4, choice2_option5, choice2_price5, choice2_option6, choice2_price6, choice2_option7, choice2_price7, choice2_option8, choice2_price8, choice2_option9, choice2_price9, choice2_option10, choice2_price10, choice3_name, choice3_option1, choice3_price1, choice3_option2, choice3_price2, choice3_option3, choice3_price3, choice3_option4, choice3_price4, choice3_option5, choice3_price5, choice3_option6, choice3_price6, choice3_option7, choice3_price7, choice3_option8, choice3_price8, choice3_option9, choice3_price9, choice3_option10, choice3_price10, amount_per_case FROM $name});
	$sth->execute();
	while (@field=$sth->fetchrow_array ()){
		if ($FORM{'keyword'} ne "") {
			$FORM{'keyword'}=lc($FORM{'keyword'});
			if (lc($field[1]) =~ /$FORM{'keyword'}/ or lc($field[2]) =~ /$FORM{'keyword'}/ or lc($field[3]) =~ /$FORM{'keyword'}/ or lc($field[4]) =~ /$FORM{'keyword'}/) {
				$results[$z]=&short_display;
				$z++;
				if ($z==1) {$first_sku=$field[0];}
			}
		}
		if ($FORM{'category'} ne "") {
			@category=split(/\//,$field[5]);
			foreach $key (@category){
				if (lc($key) eq lc($FORM{'category'}) and lc($key) ne lc($field[5])) {
					$results[$z]=&short_display;
					$z++;
					if ($z==1) {$first_sku=$field[0];}
				}
			}
			if (lc($FORM{'category'}) eq lc($field[5])){
			   $results[$z]=&short_display;
				$z++;
				if ($z==1) {$first_sku=$field[0];}
			}
		}
		if ($FORM{'listall'} ne "") {
			$results[$z]=&short_display;
			$z++;
			if ($z==1) {$first_sku=$field[0];}
		}
	} # end of while loop
	$z="";
	if ($FORM{'category'} ne "") {
		$results_title="$title_front $FORM{'category'} $title_back ";
	} else {
			$results_title="$title_front  Search Results  $title_back ";
		}
		
	$array_length=@results;
	
	# if there is only one result, display the link, otherwise display the thumbnail page
	if ($array_length==1) {
		$FORM{'sku'}="$first_sku";
		$FORM{'action'}="link";
	} else {
	
		$FORM{'keyword'}=~s/ /+/g;
		$FORM{'category'}=~s/ /+/g;
		$FORM{'skucategory'}=~s/ /+/g;
		
		if ($FORM{'startlist'} eq "") {$FORM{'startlist'}=0;}
		
		$items_per_page=$display_rows * $display_columns;
		$next_page_start=$FORM{'startlist'}+$items_per_page;
		$prev_page_start=$FORM{'startlist'}-$items_per_page;
		if ($prev_page_start >= 0) {
			$previous=qq|<a href="$script_url?action=search&keyword=$FORM{'keyword'}&category=$FORM{'category'}&startlist=$prev_page_start&sku=$FORM{'sku'}&skucategory=$FORM{'skucategory'}&listall=$FORM{'listall'}"><- Previous Page</a> |;
		}
		if ($next_page_start < $array_length) {
			$next=qq|<a href="$script_url?action=search&keyword=$FORM{'keyword'}&category=$FORM{'category'}&startlist=$next_page_start&sku=$FORM{'sku'}&skucategory=$FORM{'skucategory'}&listall=$FORM{'listall'}">Next Page -></a> |;
		}
	
		$number_displayed=$array_length-$FORM{'startlist'};
		if ($number_displayed > $items_per_page) {$number_displayed=$items_per_page;}

		print qq| $top_page $menu $middle_page $results_title Displaying $number_displayed of $array_length <br> 
<table width=$display_width cellpadding=0 cellspacing=0 border=0>
<tr><td align=left>$font $previous</td><td align=right>$font $next</tr>
</table>
<table width=$display_width cellpadding=$display_padding cellspacing=0 border=0>|;

		$y=$FORM{'startlist'};
	
		for($x=0;$x<$display_rows;$x++) {
			print "<tr>";
			for($z=0;$z<$display_columns;$z++) {
				print "$results[$y]";
				$y++;
			}
			print "</tr>";
		}
		print qq|</table> 
	<table width=$display_width cellpadding=0 cellspacing=0 border=0>
<tr><td align=left>$font $previous</td><td align=right>$font $next</tr>
</table>$statgraphic
	$bottom_page|;
	} # end of else
	
}

## if action is link display selected page
if ($FORM{'action'} eq "link") {
	
	@field=&get_sku($FORM{'sku'});
	print qq| $top_page $menu $middle_page |;
	if ($field[9] ne "") {$photo=qq| <img src="$baseurl/$field[8]" border="0" align="right"> |;}
	print qq|$photo $font <h2>$field[1] $field[2]</h2> $field[4] <h2><b>\$$field[6]</b></h2> SKU\#: $field[0]
<form method=post action="$script_url" method=post> <table cellspacing=0 border=0>|;
	if ($field[10] ne "") {
		print qq| <tr><td>$font $field[10]:&nbsp;&nbsp;</td><td><select name="$field[10]"> |;
		for($x=11;$x<31;$x=$x+2){
			if ($field[$x] ne "") {print qq|<option>$field[$x]</option>|;}
		}
		print "</select></td></tr>";
	}
	if ($field[31] ne "") {
		print qq| <tr><td>$font $field[31]:&nbsp;&nbsp;</td><td><select name="$field[31]"> |;
		for($x=32;$x<52;$x=$x+2){
			if ($field[$x] ne "") {print qq|<option>$field[$x]</option>|;}
		}
		print "</select></td></tr>";
	}
	if ($field[52] ne "") {
		print qq| <tr><td>$font $field[52]:&nbsp;&nbsp;</td><td><select name="$field[52]"> |;
		for($x=53;$x<73;$x=$x+2){
			if ($field[$x] ne "") {print qq|<option>$field[$x]</option>|;}
		}
		print "</select></td></tr>";
	}
#	if ($field[73] ne ""){
#		print qq| <tr><td>$font $field[73] items per case.<br> Minimal ordering quantity is 1 case<br>Quantity of cases:&nbsp;&nbsp;</td><td><input type="text" name="quantity" value="1" size="4" maxlength="4"></td></tr></table>|;
#	} else {
		print qq| <tr><td>$font Quantity:&nbsp;&nbsp;</td><td><input type="text" name="quantity" value="1" size="4" maxlength="4"></td></tr></table>|;
#	}
	print qq| <input type="hidden" name="action" value="add_to_cart">
<input type="hidden" name="uid" value="$FORM{'uid'}">
<input type="hidden" name="sku" value="$FORM{'sku'}">
			<input type="image" src="images/addtocart.gif"> </form> 
			<br><a href="$script_url?">Back</a>
			<a href="$script_url?uid=$FORM{'uid'}&action=view_cart">View Your Shopping Cart</a> 
			$bottom_page |; #<input type="submit" value="Add To Cart">
}


## if action is add_to_cart get that taken care of
if ($FORM{'action'} eq "add_to_cart") {
	@field=&get_sku($FORM{'sku'});
	if ($FORM{'quantity'} eq 0){
		print "$top_page $menu $middle_page You have to enter a valid quantity. $bottom_page";
	} else {
		if($field[10] ne "") {
			$option[0]=" $FORM{$field[10]}";
				for($x=11;$x<31;$x=$x+2){
					if ($field[$x] eq $FORM{$field[10]}) {$field[6]=$field[6]+$field[$x+1];}
				}
		}
		if($field[31] ne "") {
			$option[1]=" $FORM{$field[31]}";
				for($x=32;$x<52;$x=$x+2){
					if ($field[$x] eq $FORM{$field[31]}) {$field[6]=$field[6]+$field[$x+1];}
				}
		}
		if($field[52] ne "") {
			$option[2]=" $FORM{$field[52]}";
				for($x=53;$x<73;$x=$x+2){
					if ($field[$x] eq $FORM{$field[52]}) {$field[6]=$field[6]+$field[$x+1];}
				}
		}
		# if ($field[73] ne "") { $FORM{'quantity'}=$FORM{'quantity'}*$field[73]; }
		@months = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
		@date=gmtime(time);
		$date=$months[$date[4]];
		$date.="-";
		$date.=$date[5]+1900;
		$name=$dbprefix;
		$name.="cart";
		&get_affiliate_id;
		if($FORM{'direct_sku'} eq $field[0]){ $sales_type='direct';	} else { $sales_type='indirect'; }
		$dbh->do(qq{INSERT INTO $name (cart_id, status, sku, brand_name, product_name, option1, option2, option3, price, quantity, affiliate_id, commission_type, date, paid) VALUES("$FORM{'uid'}", "open", "$field[0]", "$field[1]", "$field[2]", "$option[0]", "$option[1]", "$option[2]", "$field[6]", "$FORM{'quantity'}", "$FORM{'affiliate_id'}", "$sales_type", "$date", "not yet paid")});
		@summary=&get_cart($FORM{'uid'});
		print qq | $top_page $menu $middle_page 
		$title_front Shopping Cart $title_back  Below is a summary of what you have placed in your shopping cart so far. <br><br><table cellspacing=0 border=0>$summary[0]<tr><td colspan=3 align=right>$font <b>Total:</b>&nbsp;&nbsp;&nbsp;</td><td align=right>$font \$$summary[1]</td></tr></table>

<ul>
<li><a href="$script_url?uid=$FORM{'uid'}&action=finalize">Check Out</a>
<li><a href="$script_url?uid=$FORM{'uid'}">Continue Shopping</a>
<li><a href="$script_url?uid=$FORM{'uid'}&action=clear_cart">Empty The Shopping Cart</a>
</ul>
$bottom_page |;
	}
}

## if action is clear cart then do it
if ($FORM{'action'} eq "clear_cart") {
	
	$name=$dbprefix;
	$name.="cart";
	$dbh->do(qq{DELETE FROM $name WHERE cart_id="$FORM{'uid'}" AND status="open"});
	print qq| $top_page $menu $middle_page $title_front Shopping Cart Emptied $title_back  Your shopping cart has been emptied.  <a href="$script_url?uid=$FORM{'uid'}">Continue shopping.</a> $bottom_page |;
}

##  view the cart
if ($FORM{'action'} eq "view_cart") {
	if ($FORM{'chq'} eq 'yes'){
		$name=$dbprefix;
		$name.="cart";
		if ($FORM{'quantity'} ne ''){
			$dbh->do(qq{UPDATE $name SET quantity="$FORM{'quantity'}" WHERE sku="$FORM{'sku'}" AND cart_id="$FORM{'uid'}" LIMIT 1});
		}
	}
	if ($FORM{'rmi'} eq 'yes'){
		$name=$dbprefix;
		$name.="cart";
		if ($FORM{'quantity'} ne ''){
			$dbh->do(qq{DELETE FROM $name WHERE sku="$FORM{'sku'}" AND cart_id="$FORM{'uid'}" AND quantity="$FORM{'quantity'}" LIMIT 1});
		}
	}	
	@summary=&get_cart($FORM{'uid'});
	if ($summary[2]==1) {
		print qq| $top_page $menu $middle_page $title_front Shopping Cart Empty $title_back  Your shopping cart does not currently have any items in it.  <a href="$script_url?uid=$FORM{'uid'}">Continue shopping.</a> $bottom_page |;
	} else {
	print qq | $top_page $menu $middle_page $title_front Shopping Cart $title_back  Below is a summary of what you have placed in your shopping cart so far. <br><br><table cellspacing=0 width=100% border=0>$summary[0]<tr><td colspan=3 align=right>$font <b>Total:</b>&nbsp;&nbsp;&nbsp;</td><td align=right>$font \$$summary[1]</td></tr></table>
<ul>
<li><a href="$script_url_secure?uid=$FORM{'uid'}&action=finalize">Check Out</a>
<li><a href="$script_url?uid=$FORM{'uid'}">Continue Shopping</a>
<li><a href="$script_url?uid=$FORM{'uid'}&action=clear_cart">Empty The Shopping Cart</a>
</ul>
$bottom_page |; # Fuckity Fuck
	}
}

##########################################################
## Check Out
if ($FORM{'action'} eq "finalize") {
		$sth = $dbh->prepare(qq{SELECT
			method_1,	
			method_1_low_1, method_1_high_1, method_1_rate_1,	
			method_1_low_2, method_1_high_2, method_1_rate_2,
			method_1_low_3, method_1_high_3, method_1_rate_3,
			method_1_low_4, method_1_high_4, method_1_rate_4,
			method_1_low_5, method_1_high_5, method_1_rate_5,
			method_2,
			method_2_low_1, method_2_high_1, method_2_rate_1,	
			method_2_low_2, method_2_high_2, method_2_rate_2,
			method_2_low_3, method_2_high_3, method_2_rate_3,
			method_2_low_4, method_2_high_4, method_2_rate_4,
			method_2_low_5, method_2_high_5, method_2_rate_5,
			method_3,
			method_3_low_1, method_3_high_1, method_3_rate_1,	
			method_3_low_2, method_3_high_2, method_3_rate_2,
			method_3_low_3, method_3_high_3, method_3_rate_3,
			method_3_low_4, method_3_high_4, method_3_rate_4,
			method_3_low_5, method_3_high_5, method_3_rate_5,
			method_4,
			method_4_low_1, method_4_high_1, method_4_rate_1,	
			method_4_low_2, method_4_high_2, method_4_rate_2,
			method_4_low_3, method_4_high_3, method_4_rate_3,
			method_4_low_4, method_4_high_4, method_4_rate_4,
			method_4_low_5, method_4_high_5, method_4_rate_5
			FROM shipping WHERE name="$dbprefix"
	});
	$sth->execute();
	@field=$sth->fetchrow_array();
	
	if ($tax_states[0] ne "" || $field[0] ne "") {
		print qq | $top_page $menu $middle_page $title_front Check Out Info $title_back 
		<form method=post action="$script_url_secure" method=post>|;
		
		if ($tax_states[0]) {
			print qq | Please select your State: <br><SELECT NAME="state"><OPTION><OPTION>Alabama<OPTION>Alaska<OPTION>Arizona<OPTION>Arkansas<OPTION>California<OPTION>Colorado<OPTION>Connecticut<OPTION>Delaware<OPTION>District of Columbia<OPTION>Florida<OPTION>Georgia<OPTION>Hawaii<OPTION>Idaho<OPTION>Iowa<OPTION>Illinois<OPTION>Indiana<OPTION>Kansas<OPTION>Kentucky<OPTION>Louisiana<OPTION>Maine<OPTION>Maryland<OPTION>Massachusetts<OPTION>Michigan<OPTION>Minnesota<OPTION>Mississippi<OPTION>Missouri<OPTION>Montana<OPTION>Nebraska<OPTION>Nevada<OPTION>New Hampshire<OPTION>New Jersey<OPTION>New Mexico<OPTION>New York<OPTION>North Carolina<OPTION>North Dakota<OPTION>Ohio<OPTION>Oklahoma<OPTION>Ontario<OPTION>Oregon<OPTION>Pennsylvania<OPTION>Rhode Island<OPTION>South Carolina<OPTION>South Dakota<OPTION>Tennessee<OPTION>Texas<OPTION>Utah<OPTION>Vermont<OPTION>Virginia<OPTION>Washington<OPTION>West Virginia<OPTION>Wisconsin<OPTION>Wyoming</SELECT><br>
<input type=checkbox name="state" value=""> Outside the United States|;
		}
		
		if ($field[0] ne "") {
			print qq | Please select a shipping option:<br>
			<select name="shipping_option">|;
			if ($field[16] ne "") {print "<option>$field[16]</option>";}
			if ($field[0] ne "") {print "<option>$field[0]</option>";}
			if ($field[32] ne "") {print "<option>$field[32]</option>";}
			if ($field[48] ne "") {print "<option>$field[48]</option>";}

			print qq| </select> |;
		} 

		print qq| <input type="hidden" name="action" value="check_out">
		<input type="hidden" name="uid" value="$FORM{'uid'}">
		<input type="submit" value="Continue">
		</form> $bottom_page |;
	
	} else {
		$FORM{'action'}="check_out";
		$shipping=$field[0];	
	} 	
}

##########################################################
## Finalize an Order
if ($FORM{'action'} eq "check_out") {
	@summary=&get_summary($FORM{'uid'});
	
    # Shipping Info 
	$sth = $dbh->prepare(qq{SELECT
			method_1,	
			method_1_low_1, method_1_high_1, method_1_rate_1,	
			method_1_low_2, method_1_high_2, method_1_rate_2,
			method_1_low_3, method_1_high_3, method_1_rate_3,
			method_1_low_4, method_1_high_4, method_1_rate_4,
			method_1_low_5, method_1_high_5, method_1_rate_5,
			method_2,
			method_2_low_1, method_2_high_1, method_2_rate_1,	
			method_2_low_2, method_2_high_2, method_2_rate_2,
			method_2_low_3, method_2_high_3, method_2_rate_3,
			method_2_low_4, method_2_high_4, method_2_rate_4,
			method_2_low_5, method_2_high_5, method_2_rate_5,
			method_3,
			method_3_low_1, method_3_high_1, method_3_rate_1,	
			method_3_low_2, method_3_high_2, method_3_rate_2,
			method_3_low_3, method_3_high_3, method_3_rate_3,
			method_3_low_4, method_3_high_4, method_3_rate_4,
			method_3_low_5, method_3_high_5, method_3_rate_5,
			method_4,
			method_4_low_1, method_4_high_1, method_4_rate_1,	
			method_4_low_2, method_4_high_2, method_4_rate_2,
			method_4_low_3, method_4_high_3, method_4_rate_3,
			method_4_low_4, method_4_high_4, method_4_rate_4,
			method_4_low_5, method_4_high_5, method_4_rate_5
			FROM shipping WHERE name="$dbprefix"
	});
	$sth->execute();
	@field=$sth->fetchrow_array();
	if ($FORM{'shipping_option'} ne "") {
		if ($FORM{'shipping_option'} eq $field[0]) {
			for($x=1;$x<16;$x=$x+3) {
				if ($summary[1] >= $field[$x] && $summary[1] <= $field[$x+1]) {$shipping=$field[$x+2]; }
			}
		} elsif ($FORM{'shipping_option'} eq $field[16]) {
			for($x=17;$x<32;$x=$x+3) {
				if ($summary[1] >= $field[$x] && $summary[1] <= $field[$x+1]) {$shipping=$field[$x+2]; }
			}
		} elsif ($FORM{'shipping_option'} eq $field[32]) {
			for($x=33;$x<48;$x=$x+3) {
				if ($summary[1] >= $field[$x] && $summary[1] <= $field[$x+1]) {$shipping=$field[$x+2]; }
			}
		} elsif ($FORM{'shipping_option'} eq $field[48]) {
			for($x=49;$x<64;$x=$x+3) {
				if ($summary[1] >= $field[$x] && $summary[1] <= $field[$x+1]) {$shipping=$field[$x+2]; }
			}
		}
	} else {
		$FORM{'shipping_option'}="Shipping";
		$shipping=$file[0];
	} 
	
	$total=$shipping+$summary[1];
	$name=$dbprefix;
	$name.="country";
	$sth = $dbh->prepare(qq{SELECT value,type FROM counter WHERE name="$name"});
	$sth->execute();
	@file=$sth->fetchrow_array();
	$file[0]=sprintf("%.2f", $file[0]);
	$iftax=$file[1];
	$tax=$total*$file[0];
	
	$total=sprintf("%.2f", $total);
	$shipping=sprintf("%.2f", $shipping);
	$order=qq|<table cellspacing=0 border=1 bordercolor=#000000>$summary[0]
<tr><td colspan=4 align=right>$font <b>Subtotal:</b>&nbsp;&nbsp;&nbsp;</td><td align=right>$font \$$summary[1]</td></tr><tr><td colspan=4 align=right>$font <b>Shipping: $FORM{'shipping_option'}</b>&nbsp;&nbsp;&nbsp;</td><td align=right>$font \$$shipping</td></tr> |;
	if ($FORM{'login_id'} eq ''){
		print qq | $top_page $menu $middle_page $title_front Check Out $title_back <br><br>
Registered customers, enter your e-mail and password in the box below to make a purchase. 
<form method=post action=$script_url>
<table cellpadding=0 cellspacing=0 border=0>
<tr><td><b>E-mail:</b></td><td><input type=text name=reg_email></td></tr>
<tr><td><b>Password:</b></td><td><input type=password name=reg_password></td></tr>
</table>
<input type=submit value="Make Purchase">
<input type=hidden name=login value=yes>
<input type="hidden" name="action" value="purchase">
<input type="hidden" name="uid" value="$FORM{'uid'}">
<input type="hidden" name="order" value="$order">
<input type="hidden" name="total" value="$total">
<input type="hidden" name="tax" value="$tax">
<input type="hidden" name="iftax" value="$iftax">
<input type="hidden" name="shipping" value="$shipping">
</form><br>
<br>
If you are not a registered customer of $store_name, please fill out the form below and submit it.
<form method=post action="$script_url_secure" method=post>
$order </table>
<br>
<br>
<table cellspacing=0 border=0>
<tr>
	<td>$font Your Name: &nbsp;&nbsp;</td>
	<td><input type="text" name="name" size="30"#FFFFFF></td>
</tr>
<tr>
	<td>$font Email Address: &nbsp;&nbsp;</td>
	<td><input type="text" name="email" size="30"></td>
</tr>
<tr>
	<td colspan=2>$font <br><h3>Shipping Information</h3></td>
</tr>
<tr>
	<td>$font Ship To: &nbsp;&nbsp;</td>
	<td><input type="text" name="shipping_name" size="30" value="same as above"></td>
</tr>
<tr>
	<td>$font Shipping Address: &nbsp;&nbsp;</td>
	<td><input type="text" name="shipping_address" size="30"></td>
</tr>
<tr>
	<td>$font City: &nbsp;&nbsp;</td>
	<td><input type="text" name="shipping_city" size="30"></td>
</tr>
<tr>
	<td>$font State: &nbsp;&nbsp;</td>
	<td><SELECT NAME="shipping_state"><OPTION><OPTION>Alabama<OPTION>Alaska<OPTION>Alberta<OPTION>Arizona<OPTION>Arkansas<OPTION>British Columbia<OPTION>California<OPTION>Colorado<OPTION>Connecticut<OPTION>Delaware<OPTION>District of Columbia<OPTION>Florida<OPTION>Georgia<OPTION>Hawaii<OPTION>Idaho<OPTION>Iowa<OPTION>Illinois<OPTION>Indiana<OPTION>Kansas<OPTION>Kentucky<OPTION>Louisiana<OPTION>Maine<OPTION>Manitoba<OPTION>Maryland<OPTION>Massachusetts<OPTION>Michigan<OPTION>Minnesota<OPTION>Mississippi<OPTION>Missouri<OPTION>Montana<OPTION>Nebraska<OPTION>Nevada<OPTION>New Brunswick<OPTION>New Hampshire<OPTION>New Jersey<OPTION>New Mexico<OPTION>New York<OPTION>Newfoundland<OPTION>North Carolina<OPTION>North Dakota<OPTION>Northwest Territory<OPTION>Nova Scotia<OPTION>Ohio<OPTION>Oklahoma<OPTION>Ontario<OPTION>Oregon<OPTION>Pennsylvania<OPTION>Prince Edward Island<OPTION>Quebec<OPTION>Rhode Island<OPTION>Saskatchewan<OPTION>South Carolina<OPTION>South Dakota<OPTION>Tennessee<OPTION>Texas<OPTION>Utah<OPTION>Vermont<OPTION>Virginia<OPTION>Washington<OPTION>West Virginia<OPTION>Wisconsin<OPTION>Wyoming<OPTION>Yukon Territory</SELECT>
<br><input type=checkbox name="shipping_state" value="Outside the US"> Outside the United States</td>
</tr>
<tr>
	<td>$font Zip: &nbsp;&nbsp;</td>
	<td><input type="text" name="shipping_zip" size="30"></td>
</tr>
<tr>
	<td>$font Country: &nbsp;&nbsp;</td>
	<td><select name="shipping_country">

<option selected> <option> Albania<option> Algeria<option> American Samoa<option> Andorra<option> Angola<option> Anguilla<option> Antarctica<option> Antigua And Barbuda<option> Argentina<option> Armenia<option> Aruba<option> Australia<option> Austria<option> Azerbaijan<option> Bahamas<option> Bahrain<option> Bangladesh<option> Barbados<option> Belarus<option> Belgium<option> Belize<option> Benin<option> Bermuda<option> Bhutan<option> Bolivia<option> Bosnia and Herzegovina<option> Botswana<option> Bouvet Island<option> Brazil<option> British Indian Ocean Territory<option> Brunei Darussalam<option> Bulgaria<option> Burkina Faso<option> Burma<option> Burundi<option> Cambodia<option> Cameroon<option> Canada<option> Cape Verde<option> Cayman Islands<option> Central African Republic<option> Chad<option> Chile<option> China<option> Christmas Island<option> Cocos (Keeling) Islands<option> Colombia<option> Comoros<option> Congo<option> Congo, the Democratic Republic of the<option> Cook Islands<option> Costa Rica<option> Cote d'Ivoire<option> Croatia<option> Cyprus<option> Czech Republic<option> Denmark<option> Djibouti<option> Dominica<option> Dominican Republic<option> East Timor<option> Ecuador<option> Egypt<option> El Salvador<option> England<option> Equatorial Guinea<option> Eritrea<option> Espana<option> Estonia<option> Ethiopia<option> Falkland Islands<option> Faroe Islands<option> Fiji<option> Finland<option> France<option> French Guiana<option> French Polynesia<option> French Southern Territories<option> Gabon<option> Gambia<option> Georgia<option> Germany<option> Ghana<option> Gibraltar<option> Great Britain<option> Greece<option> Greenland<option> Grenada<option> Guadeloupe<option> Guam<option> Guatemala<option> Guinea<option> Guinea-Bissau<option> Guyana<option> Haiti<option> Heard and Mc Donald Islands<option> Honduras<option> Hong Kong<option> Hungary<option> Iceland<option> India<option> Indonesia<option> Ireland<option> Israel<option> Italy<option> Jamaica<option> Japan<option> Jordan<option> Kazakhstan<option> Kenya<option> Kiribati<option> Korea, Republic of<option> Korea (South)<option> Kuwait<option> Kyrgyzstan<option> Lao People's Democratic Republic<option> Latvia<option> Lebanon<option> Lesotho<option> Liberia<option> Liechtenstein<option> Lithuania<option> Luxembourg<option> Macau<option> Macedonia<option> Madagascar<option> Malawi<option> Malaysia<option> Maldives<option> Mali<option> Malta<option> Marshall Islands<option> Martinique<option> Mauritania<option> Mauritius<option> Mayotte<option> Mexico<option> Micronesia, Federated States of<option> Moldova, Republic of<option> Monaco<option> Mongolia<option> Montserrat<option> Morocco<option> Mozambique<option> Myanmar<option> Namibia<option> Nauru<option> Nepal<option> Netherlands<option> Netherlands Antilles<option> New Caledonia<option> New Zealand<option> Nicaragua<option> Niger<option> Nigeria<option> Niue<option> Norfolk Island<option> Northern Ireland<option> Northern Mariana Islands<option> Norway<option> Oman<option> Pakistan<option> Palau<option> Panama<option> Papua New Guinea<option> Paraguay<option> Peru<option> Philippines<option> Pitcairn<option> Poland<option> Portugal<option> Puerto Rico<option> Qatar<option> Reunion<option> Romania<option> Russia<option> Russian Federation<option> Rwanda<option> Saint Kitts and Nevis<option> Saint Lucia<option> Saint Vincent and the Grenadines<option> Samoa (Independent)<option> San Marino<option> Sao Tome and Principe<option> Saudi Arabia<option> Scotland<option> Senegal<option> Seychelles<option> Sierra Leone<option> Singapore<option> Slovakia<option> Slovenia<option> Solomon Islands<option> Somalia<option> South Africa<option> South Georgia and the South Sandwich Islands<option> South Korea<option> Spain<option> Sri Lanka<option> St. Helena<option> St. Pierre and Miquelon<option> Suriname<option> Svalbard and Jan Mayen Islands<option> Swaziland<option> Sweden<option> Switzerland<option> Taiwan<option> Tajikistan<option> Tanzania<option> Thailand<option> Togo<option> Tokelau<option> Tonga<option> Trinidad<option> Trinidad and Tobago<option> Tunisia<option> Turkey<option> Turkmenistan<option> Turks and Caicos Islands<option> Tuvalu<option> Uganda<option> Ukraine<option> United Arab Emirates<option> United Kingdom<option> United States<option> United States Minor Outlying Islands<option> Uruguay<option> USA<option> Uzbekistan<option> Vanuatu<option> Vatican City State (Holy See)<option> Venezuela<option> Viet Nam<option> Virgin Islands (British)<option> Virgin Islands (U.S.)<option> Wales<option> Wallis and Futuna Islands<option> Western Sahara<option> Yemen<option> Zambia<option> Zimbabwe

</select></td>
</tr>
</table>

<input type="hidden" name="action" value="purchase">
<input type="hidden" name="uid" value="$FORM{'uid'}">
<input type="hidden" name="order" value="$order">
<input type="hidden" name="total" value="$total">
<input type="hidden" name="tax" value="$tax">
<input type="hidden" name="iftax" value="$iftax">
<input type="hidden" name="shipping" value="$shipping">
<input type="submit" value="Make Purchase"><input type="reset" value="Reset">
</form>
$bottom_page |; 
	} else {
		$FORM{'action'}="purchase";
		$FORM{'uid'}=$FORM{'uid'};
		$FORM{'order'}=$order;
		$FORM{'total'}=$total;
		$FORM{'tax'}=$tax;
		$FORM{'iftax'}=$iftax;
		$FORM{'shipping'}=$shipping;
	} 
}

## Make the purchase
if ($FORM{'action'} eq "purchase") {
	if ($FORM{'iftax'} eq $FORM{'shipping_country'}){
		$FORM{'order'}.=qq|<tr><td colspan=4 align=right>$font <b>Sales Tax:</b>&nbsp;&nbsp;&nbsp;</td><td align=right>$font \$$FORM{'tax'}</td></tr>|;
		$FORM{'total'}=$FORM{'total'}+$FORM{'tax'};
		$FORM{'total'}=sprintf("%.2f", $FORM{'total'});
		chomp($FORM{'order'});
	}
	$FORM{'order'}.=qq|<tr><td colspan=4 align=right>$font <b>Total:</b>&nbsp;&nbsp;&nbsp;</td><td align=right>$font \$$FORM{'total'}</td></tr></table> |;
	chomp($FORM{'order'});
	if ($FORM{'login_id'} ne ''){
		$name=$dbprefix;
		$name.="customer";
		$sth=$dbh->prepare(qq{SELECT customer_id, password, name, email, ship_name, ship_address, city, state, country, ZIP FROM $name WHERE customer_id="$FORM{'login_id'}"});
		$sth->execute();
		@field=$sth->fetchrow_array();
		$FORM{'name'}=$field[2];
		$FORM{'email'}=$field[3];
		$FORM{'shipping_name'}=$field[4];
		$FORM{'shipping_address'}=$field[5];
		$FORM{'shipping_city'}=$field[6];
		$FORM{'shipping_state'}=$field[7];
		$FORM{'shipping_country'}=$field[8];
		$FORM{'shipping_zip'}=$field[9];
	} elsif ($FORM{'reg_email'} ne ''){
		$name=$dbprefix;
		$name.="customer";
		$sth=$dbh->prepare(qq{SELECT customer_id, password, name, email, ship_name, ship_address, city, state, country, ZIP FROM $name WHERE email="$FORM{'reg_email'}"});
		$sth->execute();
		@field=$sth->fetchrow_array();
		if ($FORM{'reg_password'} eq $field[1]){
			$FORM{'name'}=$field[2];
			$FORM{'email'}=$field[3];
			$FORM{'shipping_name'}=$field[4];
			$FORM{'shipping_address'}=$field[5];
			$FORM{'shipping_city'}=$field[6];
			$FORM{'shipping_state'}=$field[7];
			$FORM{'shipping_country'}=$field[8];
			$FORM{'shipping_zip'}=$field[9];
		} else {
			$message.="<li>Your e-mail or password is incorrect.<br><br>";
		}
	}	else {
		if ($FORM{'name'} eq "") {
			$message.="<li>You must enter your name.<br><br>";
		}
		if (&check_email($FORM{'email'}) != 1) {
			$message.="<li>You must enter your email address in proper format.<br><br>";
		}
		if ($FORM{'shipping_address'} eq "") {
			$message.="<li>You must enter your shipping address.<br><br>";
		}
		if ($FORM{'shipping_city'} eq "") {
			$message.="<li>You must enter your city.<br><br>";
		}
		if ($FORM{'shipping_state'} eq "") {
			$message.="<li>You must enter your state or indicate you live outside the United States.<br><br>";
		}
		if ($FORM{'shipping_country'} eq "") {
			$message.="<li>You must enter your country.<br><br>";
		}
		if ($FORM{'shipping_zip'} eq "" ) { # or $FORM{'shipping_zip'} !~ /^0-9/
			$message.="<li>You must enter a valid zip code.<br><br>";
		}
	}
	if ($message ne "") {
		print qq| $top_page $menu $middle_page $title_front Sorry! $title_back  The following problems existed with your purchase information: <ul><font color=red>$message</font></ul> Please click your browser's back button to return to the form and complete it properly.<br><br> $bottom_page |;
	} else {	
		$FORM{'comments'}=~s/[\r\n]/<br>/g;
		if ($FORM{'shipping_name'} eq "same as above") {$FORM{'shipping_name'}="$FORM{'name'}";}
				$order_date=&get_date;
				$name=$dbprefix;
				$name.="orders";
				$dbh->do(qq{INSERT INTO $name (cart_id, name, email, ship_name, ship_address, city, state, country, date, ship_date, ZIP, status, total) VALUES("$FORM{'uid'}", "$FORM{'name'}", "$FORM{'email'}", "$FORM{'shipping_name'}", "$FORM{'shipping_address'}", "$FORM{'shipping_city'}", "$FORM{'shipping_state'}", "$FORM{'shipping_country'}", "$order_date", "not yet shipped", "$FORM{'shipping_zip'}", "open", "$FORM{'total'}")});
				$sth=$dbh->prepare(qq{SELECT order_id FROM $name WHERE cart_id="$FORM{'uid'}" AND name="$FORM{'name'}" AND email="$FORM{'email'}" AND ship_name="$FORM{'shipping_name'}" AND ship_address="$FORM{'shipping_address'}" AND city="$FORM{'shipping_city'}" AND state="$FORM{'shipping_state'}" AND country="$FORM{'shipping_country'}" AND date="$order_date" AND ship_date="not yet shipped" AND ZIP="$FORM{'shipping_zip'}" AND status="open" AND total="$FORM{'total'}"});
				$sth->execute();
				$order_number=$sth->fetchrow_array();
				# send email notification that order has taken place
				$to=$customer_service;
				$from=$FORM{'email'};
				$subject="Order Completed";
				$body="An order has been received from the $site_name web site.  Go to $office_url to view the order. ";
				&sendmail($to,$from,$subject,$body);
				
				# send email confirmation to customer
				$to=$FORM{'email'};
				$from=$customer_service;
				$subject="Thank You!";
				$body="Dear $FORM{'name'},
				
Thank you for ordering from $site_name. We appreciate your business!  
Your order number is: $order_number

To check on the status of your order at any time go to:
$order_url?order_number=$order_number

Feel free to contact us with any questions at $customer_service ";
				&sendmail($to,$from,$subject,$body);
					
				
				print qq| $top_page $menu $middle_page $title_front Thank You! $title_back  $FORM{'name'}, here is the summary of your order.<br> 
				<br>To pay by Credit Card <a href="https://www.paypal.com/cgi-bin/webscr?handling=0.00&amount=$FORM{'total'}&no_shipping=1&return=http%3A//$WEB_SERVER/cart/store.cgi&shipping=0.00&item_name=Your+order&submit.y=15&business=$paymail&item_number=1&cancel_return=http%3A//$WEB_SERVER/index.html&cmd=_xclick&shipping2=0.00">click here</a>.
				<br>To pay by Check <a href="$script_url?action=order">click here</a>.<br>
Order Date: $order_date<br><br>
$FORM{'order'} <br>
This order will be shipped to:<br><br>
$FORM{'shipping_name'}<br>
$FORM{'shipping_address'}<br>
$FORM{'shipping_city'}, $FORM{'shipping_state'}<br>
$FORM{'shipping_country'}<br>
$FORM{'shipping_zip'}<br><br>
$bottom_page |;	

			} # end of else
}

#####################################################
## Sign-up Form
if($FORM{'action'} eq "signup"){
		print qq| $top_page $menu $middle_page
<h3>Customer Sign-up</h3>
Fill in the following form. All fields are required.<br>
We will not give away any information to a third party.<br>
<form method=post action=$script_url>
<table cellspacing=0 border=0>
<tr>
	<td>$font Your Name: &nbsp;&nbsp;</td>
	<td><input type="text" name="name" size="30"></td>
</tr>
<tr>
	<td>$font Email Address: &nbsp;&nbsp;</td>
	<td><input type="text" name="email" size="30"></td>
</tr>
<tr>
	<td>$font Password: &nbsp;&nbsp;</td>
	<td><input type="password" name="password" size="30"></td>
</tr>
<tr>
	<td>$font Password (again): &nbsp;&nbsp;</td>
	<td><input type="password" name="password_check" size="30"></td>
</tr>
	<td colspan=2>$font <br><h3>Shipping & Payment Information</h3></td>
</tr>
<tr>
	<td>$font Shipping: &nbsp;&nbsp;</td>
	<td><input type="text" name="shipping_name" size="30" value="same as above"></td>
</tr>
<tr>
	<td>$font Shipping Address: &nbsp;&nbsp;</td>
	<td><input type="text" name="shipping_address" size="30"></td>
</tr>
<tr>
	<td>$font City: &nbsp;&nbsp;</td>
	<td><input type="text" name="shipping_city" size="30"></td>
</tr>
<tr>
	<td>$font State: &nbsp;&nbsp;</td>
	<td><SELECT NAME="shipping_state"><OPTION><OPTION>Alabama<OPTION>Alaska<OPTION>Alberta<OPTION>Arizona<OPTION>Arkansas<OPTION>British Columbia<OPTION>California<OPTION>Colorado<OPTION>Connecticut<OPTION>Delaware<OPTION>District of Columbia<OPTION>Florida<OPTION>Georgia<OPTION>Hawaii<OPTION>Idaho<OPTION>Iowa<OPTION>Illinois<OPTION>Indiana<OPTION>Kansas<OPTION>Kentucky<OPTION>Louisiana<OPTION>Maine<OPTION>Manitoba<OPTION>Maryland<OPTION>Massachusetts<OPTION>Michigan<OPTION>Minnesota<OPTION>Mississippi<OPTION>Missouri<OPTION>Montana<OPTION>Nebraska<OPTION>Nevada<OPTION>New Brunswick<OPTION>New Hampshire<OPTION>New Jersey<OPTION>New Mexico<OPTION>New York<OPTION>Newfoundland<OPTION>North Carolina<OPTION>North Dakota<OPTION>Northwest Territory<OPTION>Nova Scotia<OPTION>Ohio<OPTION>Oklahoma<OPTION>Ontario<OPTION>Oregon<OPTION>Pennsylvania<OPTION>Prince Edward Island<OPTION>Quebec<OPTION>Rhode Island<OPTION>Saskatchewan<OPTION>South Carolina<OPTION>South Dakota<OPTION>Tennessee<OPTION>Texas<OPTION>Utah<OPTION>Vermont<OPTION>Virginia<OPTION>Washington<OPTION>West Virginia<OPTION>Wisconsin<OPTION>Wyoming<OPTION>Yukon Territory</SELECT>
<br><input type=checkbox name="shipping_state" value="Outside the US"> Outside the United States</td>
</tr>
<tr>
	<td>$font Zip: &nbsp;&nbsp;</td>
	<td><input type="text" name="shipping_zip" size="30"></td>
</tr>
<tr>
	<td>$font Country: &nbsp;&nbsp;</td>
	<td><select name="shipping_country">

<option selected> <option> Albania<option> Algeria<option> American Samoa<option> Andorra<option> Angola<option> Anguilla<option> Antarctica<option> Antigua And Barbuda<option> Argentina<option> Armenia<option> Aruba<option> Australia<option> Austria<option> Azerbaijan<option> Bahamas<option> Bahrain<option> Bangladesh<option> Barbados<option> Belarus<option> Belgium<option> Belize<option> Benin<option> Bermuda<option> Bhutan<option> Bolivia<option> Bosnia and Herzegovina<option> Botswana<option> Bouvet Island<option> Brazil<option> British Indian Ocean Territory<option> Brunei Darussalam<option> Bulgaria<option> Burkina Faso<option> Burma<option> Burundi<option> Cambodia<option> Cameroon<option> Canada<option> Cape Verde<option> Cayman Islands<option> Central African Republic<option> Chad<option> Chile<option> China<option> Christmas Island<option> Cocos (Keeling) Islands<option> Colombia<option> Comoros<option> Congo<option> Congo, the Democratic Republic of the<option> Cook Islands<option> Costa Rica<option> Cote d'Ivoire<option> Croatia<option> Cyprus<option> Czech Republic<option> Denmark<option> Djibouti<option> Dominica<option> Dominican Republic<option> East Timor<option> Ecuador<option> Egypt<option> El Salvador<option> England<option> Equatorial Guinea<option> Eritrea<option> Espana<option> Estonia<option> Ethiopia<option> Falkland Islands<option> Faroe Islands<option> Fiji<option> Finland<option> France<option> French Guiana<option> French Polynesia<option> French Southern Territories<option> Gabon<option> Gambia<option> Georgia<option> Germany<option> Ghana<option> Gibraltar<option> Great Britain<option> Greece<option> Greenland<option> Grenada<option> Guadeloupe<option> Guam<option> Guatemala<option> Guinea<option> Guinea-Bissau<option> Guyana<option> Haiti<option> Heard and Mc Donald Islands<option> Honduras<option> Hong Kong<option> Hungary<option> Iceland<option> India<option> Indonesia<option> Ireland<option> Israel<option> Italy<option> Jamaica<option> Japan<option> Jordan<option> Kazakhstan<option> Kenya<option> Kiribati<option> Korea, Republic of<option> Korea (South)<option> Kuwait<option> Kyrgyzstan<option> Lao People's Democratic Republic<option> Latvia<option> Lebanon<option> Lesotho<option> Liberia<option> Liechtenstein<option> Lithuania<option> Luxembourg<option> Macau<option> Macedonia<option> Madagascar<option> Malawi<option> Malaysia<option> Maldives<option> Mali<option> Malta<option> Marshall Islands<option> Martinique<option> Mauritania<option> Mauritius<option> Mayotte<option> Mexico<option> Micronesia, Federated States of<option> Moldova, Republic of<option> Monaco<option> Mongolia<option> Montserrat<option> Morocco<option> Mozambique<option> Myanmar<option> Namibia<option> Nauru<option> Nepal<option> Netherlands<option> Netherlands Antilles<option> New Caledonia<option> New Zealand<option> Nicaragua<option> Niger<option> Nigeria<option> Niue<option> Norfolk Island<option> Northern Ireland<option> Northern Mariana Islands<option> Norway<option> Oman<option> Pakistan<option> Palau<option> Panama<option> Papua New Guinea<option> Paraguay<option> Peru<option> Philippines<option> Pitcairn<option> Poland<option> Portugal<option> Puerto Rico<option> Qatar<option> Reunion<option> Romania<option> Russia<option> Russian Federation<option> Rwanda<option> Saint Kitts and Nevis<option> Saint Lucia<option> Saint Vincent and the Grenadines<option> Samoa (Independent)<option> San Marino<option> Sao Tome and Principe<option> Saudi Arabia<option> Scotland<option> Senegal<option> Seychelles<option> Sierra Leone<option> Singapore<option> Slovakia<option> Slovenia<option> Solomon Islands<option> Somalia<option> South Africa<option> South Georgia and the South Sandwich Islands<option> South Korea<option> Spain<option> Sri Lanka<option> St. Helena<option> St. Pierre and Miquelon<option> Suriname<option> Svalbard and Jan Mayen Islands<option> Swaziland<option> Sweden<option> Switzerland<option> Taiwan<option> Tajikistan<option> Tanzania<option> Thailand<option> Togo<option> Tokelau<option> Tonga<option> Trinidad<option> Trinidad and Tobago<option> Tunisia<option> Turkey<option> Turkmenistan<option> Turks and Caicos Islands<option> Tuvalu<option> Uganda<option> Ukraine<option> United Arab Emirates<option> United Kingdom<option> United States<option> United States Minor Outlying Islands<option> Uruguay<option> USA<option> Uzbekistan<option> Vanuatu<option> Vatican City State (Holy See)<option> Venezuela<option> Viet Nam<option> Virgin Islands (British)<option> Virgin Islands (U.S.)<option> Wales<option> Wallis and Futuna Islands<option> Western Sahara<option> Yemen<option> Zambia<option> Zimbabwe

</select></td>
</tr>
</table>
<input type=submit value="Sign me up">
<input type=hidden name=action value=proc_signup>
</form>
$bottom_page |;
}

#####################################################
## Process Sign-up
if($FORM{'action'} eq "proc_signup"){
	if ($FORM{'name'} eq "") {
		$message.="<li>You must enter your name.<br><br>";
	}
	if (&check_email($FORM{'email'}) != 1) {
		$message.="<li>You must enter your email address in proper format.<br><br>";
	}
	$name=$dbprefix;
	$name.="customer";
	$sth=$dbh->prepare(qq{SELECT email FROM $name});
	$sth->execute();
	while($buffer=$sth->fetchrow_array()){
		if ($FORM{'email'} eq $buffer) {
			$message.="<li>Your email is already registered in our database.<br><br>";
		}
	}
	if ($FORM{'shipping_address'} eq "") {
		$message.="<li>You must enter your shipping address.<br><br>";
	}
	if ($FORM{'shipping_city'} eq "") {
		$message.="<li>You must enter your city.<br><br>";
	}
	if ($FORM{'shipping_state'} eq "") {
		$message.="<li>You must enter your state or indicate you live outside the United States.<br><br>";
	}
	if ($FORM{'shipping_country'} eq "") {
		$message.="<li>You must enter your country.<br><br>";
	}
	if ($FORM{'shipping_zip'} eq "" ) { # or $FORM{'shipping_zip'} !~ /^0-9/
		$message.="<li>You must enter a valid zip code.<br><br>";
	}
	if ($FORM{'password'} ne $FORM{'password_check'} ) {
		$message.="<li>Your passwords do not match.<br><br>";
	}
	if ($message ne "") {
		print qq| $top_page $menu $middle_page $title_front Sorry! $title_back  The following problems existed with your purchase information: <ul><font color=red>$message</font></ul> Please click your browser's back button to return to the form and complete it properly.<br><br> $bottom_page |;
	} else {	
		$FORM{'comments'}=~s/[\r\n]/<br>/g;
		if ($FORM{'shipping_name'} eq "same as above") {$FORM{'shipping_name'}="$FORM{'name'}";}
		#$FORM{'password'}=crypt($FORM{'password'},'KP');
		$name=$dbprefix;
		$name.="customer";
		$dbh->do(qq{INSERT INTO $name (password, name, email, ship_name, ship_address, city, state, country, ZIP) VALUES("$FORM{'password'}","$FORM{'name'}", "$FORM{'email'}", "$FORM{'shipping_name'}", "$FORM{'shipping_address'}", "$FORM{'shipping_city'}", "$FORM{'shipping_state'}", "$FORM{'shipping_country'}", "$FORM{'shipping_zip'}")});
		print qq| $top_page $menu $middle_page
<h3>Customer Sign-up</h3>
$font You are now registered as a customer of $store_name. Click <a href="$script_url?action=login">here</a> to log in, or <a href="$script_url">here</a> to continue shopping.
		$bottom_page|;
	}
}

#####################################################
## Update
if($FORM{'action'} eq "update"){
	if($FORM{'update'} ne 'yes'){
		$name=$dbprefix;
		$name.="customer";
		$sth=$dbh->prepare(qq{SELECT customer_id, password, name, email, ship_name, ship_address, city, state, country, ZIP FROM $name WHERE customer_id="$FORM{'login_id'}"});
		print qq| $top_page $menu $middle_page
<h3>Your Information</h3>
Edit your information using the following form.<br>
<form method=post action=$script_url>
<input type="hidden" name="id" value="$FORM{'login_id'}">
<table cellspacing=0 border=0>
<tr>
	<td>$font Your Name: &nbsp;&nbsp;</td>
	<td><input type="text" name="name" size="30" value="$field[2]"></td>
</tr>
<tr>
	<td>$font Email Address: &nbsp;&nbsp;</td>
	<td><input type="text" name="email" size="30" value="$field[3]"></td>
</tr>
<tr>
	<td>$font Old Password: &nbsp;&nbsp;</td>
	<td><input type="password" name="old_password" size="30"></td>
</tr>
<tr>
	<td>$font New Password: &nbsp;&nbsp;</td>
	<td><input type="password" name="password" size="30"></td>
</tr>
<tr>
	<td>$font New Password (again): &nbsp;&nbsp;</td>
	<td><input type="password" name="password_check" size="30"></td>
</tr>
	<td colspan=2>$font <br><h3>Shipping & Payment Information</h3></td>
</tr>
<tr>
	<td>$font Shipping Name: &nbsp;&nbsp;</td>
	<td><input type="text" name="shipping_name" size="30" value="$field[4]"></td>
</tr>
<tr>
	<td>$font Shipping Address: &nbsp;&nbsp;</td>
	<td><input type="text" name="shipping_address" size="30" value="$field[5]"></td>
</tr>
<tr>
	<td>$font City: &nbsp;&nbsp;</td>
	<td><input type="text" name="shipping_city" size="30" value="$field[6]"></td>
</tr>
<tr>
	<td>$font State: &nbsp;&nbsp;</td>
	<td><SELECT NAME="shipping_state"><OPTION> $field[7]<OPTION>Alabama<OPTION>Alaska<OPTION>Alberta<OPTION>Arizona<OPTION>Arkansas<OPTION>British Columbia<OPTION>California<OPTION>Colorado<OPTION>Connecticut<OPTION>Delaware<OPTION>District of Columbia<OPTION>Florida<OPTION>Georgia<OPTION>Hawaii<OPTION>Idaho<OPTION>Iowa<OPTION>Illinois<OPTION>Indiana<OPTION>Kansas<OPTION>Kentucky<OPTION>Louisiana<OPTION>Maine<OPTION>Manitoba<OPTION>Maryland<OPTION>Massachusetts<OPTION>Michigan<OPTION>Minnesota<OPTION>Mississippi<OPTION>Missouri<OPTION>Montana<OPTION>Nebraska<OPTION>Nevada<OPTION>New Brunswick<OPTION>New Hampshire<OPTION>New Jersey<OPTION>New Mexico<OPTION>New York<OPTION>Newfoundland<OPTION>North Carolina<OPTION>North Dakota<OPTION>Northwest Territory<OPTION>Nova Scotia<OPTION>Ohio<OPTION>Oklahoma<OPTION>Ontario<OPTION>Oregon<OPTION>Pennsylvania<OPTION>Prince Edward Island<OPTION>Quebec<OPTION>Rhode Island<OPTION>Saskatchewan<OPTION>South Carolina<OPTION>South Dakota<OPTION>Tennessee<OPTION>Texas<OPTION>Utah<OPTION>Vermont<OPTION>Virginia<OPTION>Washington<OPTION>West Virginia<OPTION>Wisconsin<OPTION>Wyoming<OPTION>Yukon Territory</SELECT>
<br><input type=checkbox name="shipping_state" value="Outside the US"> Outside the United States</td>
</tr>
<tr>
	<td>$font Zip: &nbsp;&nbsp;</td>
	<td><input type="text" name="shipping_zip" size="30" value="$field[9]"></td>
</tr>
<tr>
	<td>$font Country: &nbsp;&nbsp;</td>
	<td><select name="shipping_country">

<option selected> $field[8]<option> Albania<option> Algeria<option> American Samoa<option> Andorra<option> Angola<option> Anguilla<option> Antarctica<option> Antigua And Barbuda<option> Argentina<option> Armenia<option> Aruba<option> Australia<option> Austria<option> Azerbaijan<option> Bahamas<option> Bahrain<option> Bangladesh<option> Barbados<option> Belarus<option> Belgium<option> Belize<option> Benin<option> Bermuda<option> Bhutan<option> Bolivia<option> Bosnia and Herzegovina<option> Botswana<option> Bouvet Island<option> Brazil<option> British Indian Ocean Territory<option> Brunei Darussalam<option> Bulgaria<option> Burkina Faso<option> Burma<option> Burundi<option> Cambodia<option> Cameroon<option> Canada<option> Cape Verde<option> Cayman Islands<option> Central African Republic<option> Chad<option> Chile<option> China<option> Christmas Island<option> Cocos (Keeling) Islands<option> Colombia<option> Comoros<option> Congo<option> Congo, the Democratic Republic of the<option> Cook Islands<option> Costa Rica<option> Cote d'Ivoire<option> Croatia<option> Cyprus<option> Czech Republic<option> Denmark<option> Djibouti<option> Dominica<option> Dominican Republic<option> East Timor<option> Ecuador<option> Egypt<option> El Salvador<option> England<option> Equatorial Guinea<option> Eritrea<option> Espana<option> Estonia<option> Ethiopia<option> Falkland Islands<option> Faroe Islands<option> Fiji<option> Finland<option> France<option> French Guiana<option> French Polynesia<option> French Southern Territories<option> Gabon<option> Gambia<option> Georgia<option> Germany<option> Ghana<option> Gibraltar<option> Great Britain<option> Greece<option> Greenland<option> Grenada<option> Guadeloupe<option> Guam<option> Guatemala<option> Guinea<option> Guinea-Bissau<option> Guyana<option> Haiti<option> Heard and Mc Donald Islands<option> Honduras<option> Hong Kong<option> Hungary<option> Iceland<option> India<option> Indonesia<option> Ireland<option> Israel<option> Italy<option> Jamaica<option> Japan<option> Jordan<option> Kazakhstan<option> Kenya<option> Kiribati<option> Korea, Republic of<option> Korea (South)<option> Kuwait<option> Kyrgyzstan<option> Lao People's Democratic Republic<option> Latvia<option> Lebanon<option> Lesotho<option> Liberia<option> Liechtenstein<option> Lithuania<option> Luxembourg<option> Macau<option> Macedonia<option> Madagascar<option> Malawi<option> Malaysia<option> Maldives<option> Mali<option> Malta<option> Marshall Islands<option> Martinique<option> Mauritania<option> Mauritius<option> Mayotte<option> Mexico<option> Micronesia, Federated States of<option> Moldova, Republic of<option> Monaco<option> Mongolia<option> Montserrat<option> Morocco<option> Mozambique<option> Myanmar<option> Namibia<option> Nauru<option> Nepal<option> Netherlands<option> Netherlands Antilles<option> New Caledonia<option> New Zealand<option> Nicaragua<option> Niger<option> Nigeria<option> Niue<option> Norfolk Island<option> Northern Ireland<option> Northern Mariana Islands<option> Norway<option> Oman<option> Pakistan<option> Palau<option> Panama<option> Papua New Guinea<option> Paraguay<option> Peru<option> Philippines<option> Pitcairn<option> Poland<option> Portugal<option> Puerto Rico<option> Qatar<option> Reunion<option> Romania<option> Russia<option> Russian Federation<option> Rwanda<option> Saint Kitts and Nevis<option> Saint Lucia<option> Saint Vincent and the Grenadines<option> Samoa (Independent)<option> San Marino<option> Sao Tome and Principe<option> Saudi Arabia<option> Scotland<option> Senegal<option> Seychelles<option> Sierra Leone<option> Singapore<option> Slovakia<option> Slovenia<option> Solomon Islands<option> Somalia<option> South Africa<option> South Georgia and the South Sandwich Islands<option> South Korea<option> Spain<option> Sri Lanka<option> St. Helena<option> St. Pierre and Miquelon<option> Suriname<option> Svalbard and Jan Mayen Islands<option> Swaziland<option> Sweden<option> Switzerland<option> Taiwan<option> Tajikistan<option> Tanzania<option> Thailand<option> Togo<option> Tokelau<option> Tonga<option> Trinidad<option> Trinidad and Tobago<option> Tunisia<option> Turkey<option> Turkmenistan<option> Turks and Caicos Islands<option> Tuvalu<option> Uganda<option> Ukraine<option> United Arab Emirates<option> United Kingdom<option> United States<option> United States Minor Outlying Islands<option> Uruguay<option> USA<option> Uzbekistan<option> Vanuatu<option> Vatican City State (Holy See)<option> Venezuela<option> Viet Nam<option> Virgin Islands (British)<option> Virgin Islands (U.S.)<option> Wales<option> Wallis and Futuna Islands<option> Western Sahara<option> Yemen<option> Zambia<option> Zimbabwe

</select></td>
</tr>
</table>
<input type=submit value="Change Info">
<input type=hidden name=update value=yes>
</form>
$bottom_page |;
	} else {
		$name=$dbprefix;
		$name.="customer";
		$sth=$dbh->prepare(qq{SELECT password FROM $name WHERE customer_id="$FORM{'id'}"});
		$sth->execute();
		$password=$sth->fetchrow_array();
		if ($FORM{'old_password'} eq $password){
			if ($FORM{'name'} eq "") {
				$message.="<li>You must enter your name.<br><br>";
			}
			if (&check_email($FORM{'email'}) != 1) {
				$message.="<li>You must enter your email address in proper format.<br><br>";
			}
			if ($FORM{'shipping_address'} eq "") {
				$message.="<li>You must enter your shipping address.<br><br>";
			}
			if ($FORM{'shipping_city'} eq "") {
				$message.="<li>You must enter your city.<br><br>";
			}
			if ($FORM{'shipping_state'} eq "") {
				$message.="<li>You must enter your state or indicate you live outside the United States.<br><br>";
			}
			if ($FORM{'shipping_country'} eq "") {
				$message.="<li>You must enter your country.<br><br>";
			}
			if ($FORM{'shipping_zip'} eq "" ) { # or $FORM{'shipping_zip'} !~ /^0-9/
				$message.="<li>You must enter a valid zip code.<br><br>";
			}
			if ($FORM{'password'} ne $FORM{'password_check'}) {
				$message.="<li>Your passwords do not match.<br><br>";
			}
			if ($message ne "") {
				print qq| $top_page $menu $middle_page $title_front Sorry! $title_back  The following problems existed with your purchase information: <ul><font color=red>$message</font></ul> Please click your browser's back button to return to the form and complete it properly.<br><br> $bottom_page |;
			} else {	
				$FORM{'comments'}=~s/[\r\n]/<br>/g;
				#$FORM{'password'}=crypt($FORM{'password'},'KP');
				$name=$dbprefix;
				$name.="customer";
				if ($FORM{'password'} eq "") {
					$dbh->do(qq{UPDATE $name SET name="$FORM{'name'}", email="$FORM{'email'}", check_name="$FORM{'shipping_name'}", address="$FORM{'shipping_address'}", city="$FORM{'shipping_city'}", state="$FORM{'shipping_state'}", country="$FORM{'shipping_country'}", ZIP="$FORM{'shipping_zip'} WHERE affiliate_id="$FORM{'affiliate_id'}"});
				} else {
					$dbh->do(qq{UPDATE $name SET password="$FORM{'password'}", name="$FORM{'name'}", email="$FORM{'email'}", check_name="$FORM{'shipping_name'}", address="$FORM{'shipping_address'}", city="$FORM{'shipping_city'}", state="$FORM{'shipping_state'}", country="$FORM{'shipping_country'}", ZIP="$FORM{'shipping_zip'} WHERE affiliate_id="$FORM{'affiliate_id'}"});
				}
				print qq| $top_page $menu $middle_page
<h3>Information Changed</h3>
$font You have successfully changed your information. Click <a href="$script_url">here</a> to continue shopping.
		$bottom_page|;
			}
		} else {
			print qq| $top_page $menu $middle_page
<h3>Error</h3>
$font Your password is incorrect.
			$bottom_page|;
		}
	}
}

#  Increments key counter and returns value
sub key_counter {

my $keycounter;

$sth = $dbh->prepare(qq{SELECT value FROM counter WHERE name="$dbprefix" AND type="cart"});
$sth->execute();
$keycounter=$sth->fetchrow_array();
$keycounter++;
$dbh->do(qq{UPDATE counter SET value="$keycounter" WHERE name="$dbprefix" AND type="cart"});

return ($keycounter);

}

########################################
## Returns all categories in the product file as an array
sub get_categories {
	my ($x, $marker);
	my ($field, @categories);
	$name=$dbprefix;
	$name.="products";
	$sth = $dbh->prepare(qq{SELECT category FROM $name});
	$sth->execute();
	while ($field=$sth->fetchrow_array()) {
		foreach $category (@categories) {
			if ($field eq $category) {$marker=1;}
		}
		if ($marker!=1) {
			$categories[$x]=$field;
			$x++;
		}
		$marker="";
	}
	@categories=sort(@categories);
	return (@categories);
}

#####################################################
## Returns the format of a short display
sub short_display {
	my $return;
	my $photo;
	if ($field[9] ne "") {$photo=qq| <a href="$script_url?action=link&sku=$field[0]&uid=$FORM{'uid'}"><img src="$baseurl/$field[9]" border="0" align=left hspace=10 vspace=8></a><br> |;} # KEY
	$return=qq|<td valign="top">$photo $font <a href="$script_url?action=link&sku=$field[0]&uid=$FORM{'uid'}"><b>$field[1] $field[2]</b></a><br> $field[3] <br><b>$font <font color="#FF0000"> \$$field[6]</font></b><br>$font SKU: $field[0]</td>|;
	return($return);
}

#####################################################
## Returns an array full of values for a SKU
sub get_sku {
	my @return;
	my $sku=$_[0];
	
	$name=$dbprefix;
	$name.="products";
	$sth = $dbh->prepare(qq{SELECT sku, brand_name, product_name, short_desc, long_desc, category, price, shipping, image, thumbnail, choice1_name, choice1_option1, choice1_price1, choice1_option2, choice1_price2, choice1_option3, choice1_price3, choice1_option4, choice1_price4, choice1_option5, choice1_price5, choice1_option6, choice1_price6, choice1_option7, choice1_price7, choice1_option8, choice1_price8, choice1_option9, choice1_price9, choice1_option10, choice1_price10, choice2_name, choice2_option1, choice2_price1, choice2_option2, choice2_price2, choice2_option3, choice2_price3, choice2_option4, choice2_price4, choice2_option5, choice2_price5, choice2_option6, choice2_price6, choice2_option7, choice2_price7, choice2_option8, choice2_price8, choice2_option9, choice2_price9, choice2_option10, choice2_price10, choice3_name, choice3_option1, choice3_price1, choice3_option2, choice3_price2, choice3_option3, choice3_price3, choice3_option4, choice3_price4, choice3_option5, choice3_price5, choice3_option6, choice3_price6, choice3_option7, choice3_price7, choice3_option8, choice3_price8, choice3_option9, choice3_price9, choice3_option10, choice3_price10, amount_per_case FROM $name WHERE sku="$sku"});
	$sth->execute();
	@return=$sth->fetchrow_array();
	
	return(@return);
}

#####################################################
## Returns order summary
sub get_summary {
	my $total_quantity=0;
	my $cart;
	my (@product,@item);
	my ($return,$line_total,$total,$empty);
	my $count=0;
	my $customer_id=$_[0];
	
	$name=$dbprefix;
	$name.="cart";
	$sth=$dbh->prepare(qq{SELECT cart_id, status, sku, brand_name, product_name, option1, option2, option3, price, quantity, affiliate_id FROM $name WHERE status="open" AND cart_id="$customer_id"});
	$sth->execute();
	$return="<tr><td align=center>$font <b>SKU Code</b></td><td align=center>$font <b>Description</b></td><td align=center>$font <b>Price</b></td><td align=center>$font <b>Quantity</b></td><td align=center>$font <b>Total</b></td></tr>";
	while(@item = $sth->fetchrow_array()){
		$line_total=$item[8]*$item[9];
		$line_total=sprintf("%.2f", $line_total);
		$item[8]=sprintf("%.2f", $item[8]);
		$return.="<tr><td>$font $item[2]</td><td>$font $item[3] $item[4] </td><td align=right>$font \$$item[8]</td><td align=right>$font $item[9]</td><td align=right>$font \$$line_total</td></tr>";
		chomp($return);
		$total_quantity=$total_quantity+$item[8];
		$total=$total+$line_total;
		$count++;
	}
	if ($count == 0){$empty=1;}
	
	$total=sprintf("%.2f", $total);
	return($return, $total, $empty, $total_quantity);
}

#####################################################
## Returns order summary
sub get_cart {
	my $total_quantity=0;
	my $cart;
	my (@product,@item);
	my ($return,$line_total,$total,$empty);
	my $count=0;
	my $customer_id=$_[0];
	
	$name=$dbprefix;
	$name.="cart";
	$sth=$dbh->prepare(qq{SELECT cart_id, status, sku, brand_name, product_name, option1, option2, option3, price, quantity, affiliate_id FROM $name WHERE status="open" AND cart_id="$customer_id"});
	$sth->execute();
	$return="<tr><td align=center width=70%>$font <b>Description</b></td><td align=center width=15%>$font <b>Quantity</b></td><td align=center width=10%>$font <b>Delete Item</b></td><td align=center width=5%>$font <b>Total</b></td></tr>";
	while(@item = $sth->fetchrow_array()){
		$line_total=$item[8]*$item[9];
		$line_total=sprintf("%.2f", $line_total);
		$item[8]=sprintf("%.2f", $item[8]);
		$count++;
		$return.="<tr><td>$font $count. <b>$item[3]</b><br> $item[4]<br>SKU code: $item[2]<br>$font <font color=#990000>Price: \$$item[8]</font><br><br></td><form method=post action=$script_url><td align=center>$font <input type=text name=quantity value=$item[9] size=2><input type=hidden name=sku value=$item[2]><input type=hidden name=action value=view_cart><input type=hidden name=chq value=yes><input type=hidden name=uid value=$FORM{'uid'}> &nbsp<input type=image src=\"$image_url/change.gif\" name=submit border=0 align=absmiddle></td></form><form method=post action=$script_url><td align=center>$font <input type=hidden name=quantity value=$item[9] size=2><input type=hidden name=sku value=$item[2]><input type=hidden name=action value=view_cart><input type=hidden name=rmi value=yes><input type=hidden name=uid value=$FORM{'uid'}><input type=image src=\"$image_url/remove.gif\" name=submit border=0 align=absmiddle></td></form><td align=right>$font \$$line_total</td></tr>";
		chomp($return);
		$total_quantity=$total_quantity+$item[8];
		$total=$total+$line_total;
	}
	if ($count == 0){$empty=1;}
	
	$total=sprintf("%.2f", $total);
	return($return, $total, $empty, $total_quantity);
}

######################################################
## Send Email (to,from,subject,body) $SMTP_SERVER must 
## be a global variable assigned with a valid smtp server

sub sendmail {
  local($to, $from, $subject, $body) = @_;
	open(MAIL, "|/usr/lib/sendmail -t");
	print MAIL "To: $to\r\nFrom: $from\r\nSubject: $subject\r\n\r\n$body\r\n\r\n";
	close (MAIL);
}



#####################################################
## Check for valid email address
sub check_email {
    local($email) = $_[0];

    # Check that the email address doesn't have 2 @ signs, a .., a @., a 
    # .@ or begin or end with a .

    if ($email =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\.$)/ || 

        # Allow anything before the @, but only letters numbers, dashes and 
        # periods after it.  Also check to make sure the address ends in 2 or 
        # three letters after a period and allow for it to be enclosed in [] 
        # such as [164.104.50.1]
    
        ($email !~ /^.+\@localhost$/ && 
         $email !~ /^.+\@\[?(\w|[-.])+\.[a-zA-Z]{2,3}|[0-9]{1,3}\]?$/)) {
        return(0);
    }

    # If it passed the above test, it is valid.
    
    else {
        return(1);
    }
}

sub get_affiliate_id {
	# get the cookie
	@cookievalues=split(/;/,$ENV{'HTTP_COOKIE'});
	foreach $value (@cookievalues) {
		if ($value=~/affiliate_id=/) {
			@cookie=split(/=/,$value);

			$FORM{'affiliate_id'}=$cookie[1];
			chomp($FORM{'affiliate_id'});
		}
		if ($value=~/direct_sku=/) {
			@cookie=split(/=/,$value);

			$FORM{'direct_sku'}=$cookie[1];
			chomp($FORM{'direct_sku'});
		}
	}
}

sub get_date {
    my ($time);
    $_[0] ? ($time = $_[0]) : ($time = time());

    my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime ($time);
    $year = $year + 1900;
    ($mday < 10) and ($mday = "0$mday");


        my @months = qw!Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec!;
        return "$mday-$months[$mon]-$year";
    
}