<?php
set_time_limit(300);
class Ups{
	function getPrice($weight){
		global $siteConf,$db;
		$userId = $_SESSION['userId'];
		$sql = "select * from qk_user_addr where userId='$userId' and `primary`=1";
		$addr = $db->getRow($sql);
		if(!$addr){
		$sql = "select * from qk_user_addr where userId='$userId'";
		$addr = $db->getRow($sql);
		}
		$selResidential = "01";
		$selPackaging = "00";
		$selService = "GND";
		$selRate = "Regular Daily Pickup";
		$txtPackWeight = $weight;
		$txtPackLength = "";
		$txtPackWidth = "";
		$txtPackHeight = "";
		$txtFromCountry = $siteConf['country'];
		$txtToCity = "";
		$txtFromZip = $siteConf['zipCode'];
		$txtToCity = "";
		$txtToCountry = $addr['country']?$addr['country']:"US";
		$txtToZip = $addr['zipCode'];
		$arr = array("http://www.ups.com/using/services/rave/qcostcgi.cgi?accept_UPS_license_agreement=yes",
	   "10_action=3",
	   "13_product=".$selService,
	   "14_origCountry=".$txtFromCountry,
	   "15_origPostal=".$txtFromZip,
	   "origCity=".$txtFromCity,
	   "19_destPostal=".$txtToZip,
	   "20_destCity=".$txtToCity,
	   "22_destCountry=".$txtToCountry,
	   "23_weight=".$txtPackWeight,
	   "47_rateChart=".$selRate,
	   "48_container=".$selPackaging,
	   "49_residential=".$selResidential,
	   "25_length=".$txtPackLength,
	   "26_width=".$txtPackWidth,
	   "27_height=".$txtPackHeight);
		$Url = join("&", $arr);
		//echo $Url;
		//$Url="http://www.";
		//echo file_get_contents($Url);
		//print_r($arr);
		$http = new http($Url);
		$http->createCurl();
		$Resp = $http->responseText();
	   //$Resp = @fopen($Url, "r");
	   if(!$Resp){return false;}
	   //while(!feof($Resp))
	   //{   
		  //$Result = fgets($Resp, 500);
		  $Result = $Resp;
		  $Result = explode("%", $Result);
		  $Err = substr($Result[0], -1);
		  switch($Err)
		  {
			 case 3:
			 $ResCode = $Result[8];
			 break;
			 case 4:
			 $ResCode = $Result[8];
			 break;
			 case 5:
			 $ResCode = $Result[1];
			 break;
			 case 6:
			 $ResCode = $Result[1];
			 break;
		  }
	   //}
	   //fclose($Resp);
	   //print_r($Result);
	   if(!$ResCode)
	   {
		  //$ResCode = "An error occured.";
		  return false;
	   }
	   return $ResCode;

	}
}
?>