<?php
include "includes/config.php";
include "includes/session.php";
include "includes/functions.php";

/*if (get_magic_quotes_gpc()) {
    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
}*/


/*display the active template*/

$active_template = displayTemplate();

include "includes/htmltop.php";

?>

	<body>

	<center>

	<div align="left" id="layout">
	
		<div align="left" id="header">
		<!--header area start -->
				
				<div align="left" id="logo"><img src="<?php echo $logofolder?>/<?php echo $logoimage?>" alt="" width="125" height="39" border="0" /></div>
				
				<div align="right" id="header_right">
				<!--
				 <a href="javascript:;">Contact Us</a>&nbsp;|&nbsp;
				 -->
				 <a href="javascript:;">&nbsp;</a>
				


		</div>		
				
		<!--header area end -->
		</div>
<?php

include_once("shipping.php");
include_once('http_client.php');
				
$ups_action_code = '4';
$text_shipping_weight = '&nbsp;lbs';


if(!isset($_POST['country']))
{
	$destination_country = 'US';
}
else
{
	$destination_country = $_POST['country'];
}
	
$destination_postal = $_POST['zip'];

$select_country = "SELECT * FROM ".$tableprefix."countries WHERE 1";
$result_country = mysql_query($select_country) or die(mysql_error());

$select_shipping_settings = "SELECT * FROM ".$tableprefix."shipping_settings WHERE 1";
$result_shipping_settings = mysql_query($select_shipping_settings) or die(mysql_error());

/*values for calculating shipping*/

while($shipping_settings_row = mysql_fetch_array($result_shipping_settings))
{
		
	switch($shipping_settings_row['vcode'])
	{
	
		case 'shipping_box_display':
			
			$shipping_box_weight_display = $shipping_settings_row['vvalue'];
			
		break;
		case 'shipping_box_weight':
			
			$shipping_box_weight = $shipping_settings_row['vvalue'];
			$za_tare_array = split("[:,]" , $shipping_box_weight);
			$zc_tare_percent= $za_tare_array[0];
			$zc_tare_weight= $za_tare_array[1];
			
		break;
		case 'shipping_box_padding':
			
			$shipping_box_padding = $shipping_settings_row['vvalue'];
			$za_large_array = split("[:,]" , $shipping_box_padding);
			$zc_large_percent= $za_large_array[0];
			$zc_large_weight= $za_large_array[1];
			
		break;
		case 'shipping_max':
			
			$shipping_max_weight = $shipping_settings_row['vvalue'];
						
		break;
		/*case 'shipping_origin':
			
			$origin_country = $shipping_settings_row['vvalue'];
						
		break;
		case 'postal_code':
			
			$origin_postal = $shipping_settings_row['vvalue'];
						
		break;*/
		case 'ups_rate':
			
			$rate_chart = $shipping_settings_row['vvalue'];
						
		break;
		case 'ups_container':
			
			$container_code = $shipping_settings_row['vvalue'];
						
		break;
		case 'shipping_handling':
			
			$module_shipping_handling = $shipping_settings_row['vvalue'];
						
		break;
		case 'address_quote':
			
			$res_code = $shipping_settings_row['vvalue'];
						
		break;
		case 'shipping_num_boxes':
			
			$shipping_num_boxes = $shipping_settings_row['vvalue'];
						
		break;
		
			
	}


}



if($destination_country == 'CA')
{
  $prod = 'STD';
 
}
else
{
  $prod = 'GNDRES';
}

/*---------------different ups types-------------------*/

$module_shipping_ups_types = '1DM, 1DML, 1DA, 1DAL, 1DAPI, 1DP, 1DPL, 2DM, 2DML, 2DA, 2DAL, 3DS, GND, STD, XPR, XPRL, XDM, XDML, XPD';

/*-----------Selecting the shipping calculation details of products in cart----------------*/

if(isset($_POST['submit']))
{ // if submit start


		$shipping_output = "";
				
		$select_product_details = "SELECT prd.product_id,prd.product_name,prd.nproduct_shipping_weight,art.country,art.zip
								   FROM ".$tableprefix."artists art,".$tableprefix."products prd,"
												  .$tableprefix."cart crt
								   WHERE art.artist_id = prd.product_artist_id
									AND crt.user_id = ".$_SESSION["sess_userid"].
								  " AND crt.product_id = prd.product_id";
		
		$result_product_details = mysql_query($select_product_details) or die(mysql_error());
		
		if(mysql_num_rows($result_product_details) > 0)
		{
		
			while($product_details_row = mysql_fetch_array($result_product_details))
			{
			
				$shipping_weight = $product_details_row['nproduct_shipping_weight'];
				$origin_country  = $product_details_row['country']; 
				$origin_postal   = $product_details_row['zip']; 
				$product_name    = $product_details_row['product_name'];
				$product_id		 = $product_details_row['product_id']; 
				
				$ups_shipping_weight = ($shipping_weight < 0.1 ? 0.1 : $shipping_weight);
				
				
				switch (true)
				{
					// large box add padding
					case($shipping_max_weight <= $ups_shipping_weight):
					$ups_shipping_weight = $ups_shipping_weight + ($ups_shipping_weight*($zc_large_percent/100)) + $zc_large_weight;
					break;
					default:
					// add tare weight < large
					$ups_shipping_weight = $ups_shipping_weight + ($ups_shipping_weight*($zc_tare_percent/100)) + $zc_tare_weight;
					break;
				}	
				
				if ($ups_shipping_weight > $_SESSION['SESS_shipping_maxweight'])
				{ // Split into many boxes
					  $shipping_boxes = ceil($ups_shipping_weight/$_SESSION['SESS_shipping_maxweight']);
							  
					  $ups_shipping_weight = $ups_shipping_weight/$shipping_boxes;
				}
		
		
				$calculate_shipping_cost = passValues($product_id,$product_name,$ups_shipping_weight,$shipping_box_weight_display,$shipping_num_boxes,
										$prod,$ups_action_code,$origin_country,$origin_postal,$destination_country,
										$destination_postal,$rate_chart,$container_code,$res_code,$text_shipping_weight,
										$module_shipping_handling,$module_shipping_ups_types,1);
	
				if($calculate_shipping_cost == 1)
				{
				
					$check_shipping_cost = "<fieldset><b>Product Name&nbsp;:&nbsp;$product_name<b><br><br><span class='text_information'>No UPS Shipping Available !!</span></fieldset>";
							
										  
				}
				else
				{
				
					$check_shipping_cost = "<fieldset><b>Product Name&nbsp;:&nbsp;$product_name<b><br><br><span class='text_information'>Available Shipping</span><br><br>".$calculate_shipping_cost."</fieldset>";
				
				}
				
				$shipping_output .= "<tr>
										<td align='left' valign='top' class='maintext'>".$check_shipping_cost."</td>
									</tr>";
						
			}
		
		}

		

} // if submit end


?>
<script src="./js/javascript_check_userside.js"></script>
<script>
	function checkShippingForm()
	{
	
	  var form_name = new Array('zip');		
	  var display_name = new Array('Post/Zip Code');
	  var count = 1;	
	
	  if(check_fieldsUser('frmShipping',form_name,display_name,count))	
	  {
	  
		
			   
	  }
	  else
	  {
	  
			return false;
	  
	  }
	   
	}
</script>


			<div align="left" class="main_body">
			<!--mainbody area start -->
			
			
				
				
			<form name="frmShipping" action="#" method="POST" onSubmit="return checkShippingForm();">
			
			<div align="left" id="item_details">
	
	
			<!--items display area start -->
		
			<div align="left" id="items_top_area">
					<img src="<?php echo $active_template[3]?>/top_title_left.gif" alt="" class="items_top_area_img" />
					<div align="left" id="items_top_area_ttl">Shipping Calculation</div>
					<img src="<?php echo $active_template[3]?>/top_title_right.gif" alt="" class="items_top_area_img" />
			</div>
			
			<div>&nbsp;</div>
						
		
			<div>
			
			
				<table cellpadding="2" cellspacing="2" width="100%" border=0>
					
					<tr>
						<td width="31%" align="left">Destination Country</td>
						<td width="69%">
						<select name="country" >
						
<?php
						while($country_row = mysql_fetch_array($result_country))	
						{
						
							if($destination_country == $country_row['country_iso_code_2'])
							{
							
								$country_selected = "selected";
							
							}
							else
							{
							
								$country_selected = "";
							
							}
						
?>																											
							<option value="<?php echo $country_row['country_iso_code_2']?>" <?php echo $country_selected?>><?php echo $country_row['country_name']?></option>
							
<?php
	
						}																													

?>																												
							</select>
						</td>
					</tr>
					<tr>
						<td width="31%" align="left">Destination Post/Zip Code</td>
						<td width="69%">
							<input type="text" name="zip" class="textbox">
						</td>
					</tr>
					<tr>
					
						<td colspan="2">
							<table cellpadding="0" border="0" cellspacing="0" width="100%">
							
<?php 				
					echo $shipping_output 

?>							</table>
						</td>
					</tr>
					

					</tr>
					
					<tr>
						<td colspan="2" align="center">
							<input type="submit" name="submit" value="Submit" class="button">
							<input type="button" name="close" value="Close" class="button" onClick="Javascript:window.close();">
						</td>
					</tr>
				</table>
			
		</td>
	</tr>
</table>


		</div>	
	
	
		<div align="left" class="float_clear1"></div>
					
					
		 <!--items display area end -->
		  </div>
		
			</form>
		<!--center content area end-->	


		<div align="left" class="clear"></div>		
		<!--mainbody area end -->
        </div>
		<div align="left" class="clear"></div>


<?php

	include "includes/".$active_template[2];	/* Bottom File Name */

?>
		


