require("config.php");
ConnectDB();
if($cart_id=="") {
  $cart_id=$_COOKIE['cart_id'];
}
if (!isset($cart_id)) {
   $cookie_expire=8800;
   $time=time();
   if ($remote_address=="") {
     $remote_address=strtoupper(uniqid($cookie_name));
   }
   if (preg_match("/MSIE/", $_SERVER["HTTP_USER_AGENT"])) {
	 $ck_time=time()+8800;
     setcookie("cart_id","$remote_address-$time-$aff_id",$ck_time+$cookie_expire,"/",".$cookie_domain",0);
     // Original code //setcookie("user","$val",$date,"/",".$cookie_domain",0);
   }
   else {
	 setcookie("cart_id","$remote_address-$time-$aff_id",time()+8800,"/",".$cookie_domain",0);
   }
} else {
   $temp=explode("-",$cart_id);
   $ip=$temp[0];
   $time=$temp[1];
   $aff_id=$temp[2];
}
if ($cart_id!="") {
   if($cart_id!=hackerDefense($cart_id)) {$cart_id="";}
   if($cart_id!=str_ireplace("'","",$cart_id)) {$cart_id="";}
}
$id=$cart_id;
$cart_id=hackerDefense($cart_id);
$cart_id=str_ireplace("'","",$cart_id);
$command=hackerDefense($command);
$category=hackerDefense($category);
$category=str_ireplace("'","",$category);
if(!is_numeric($category)) {$category="";}
$subcategory=hackerDefense($subcategory);
if(!is_numeric($subcategory)) {$subcategory="";}
$quantity=hackerDefense($quantity);
if ($quantity!="") {
   if(!is_numeric($quantity)) {$quantity="";}
}
$sku_id=hackerDefense($sku_id);
$sku_id=str_ireplace("'","",$sku_id);
if ($sku_id!="") {
   if(!is_numeric($sku_id)) {$sku_id="";echo"";}
}
$id=hackerDefense($id);
if ($id!="") {
   if(!is_numeric($id)) {$id="";}
}
$aff_id=hackerDefense($aff_id);
if (!isset($cart_id)) {
   $cookie_expire=8800;
   $time=time();
   if ($remote_address=="") {
     $remote_address=strtoupper(uniqid("WOM1"));
   }
   if (ereg("MSIE", getenv("HTTP_USER_AGENT"))) {
	 $ck_time=time()+8800;
     setcookie("cart_id","$remote_address-$time-$aff_id",$ck_time+$cookie_expire,"/",".compasservicesinc.com",0);
     // Original code //setcookie("user","$val",$date,"/",".worldofmobility.co.uk",0);
   }
   else {
	 setcookie("cart_id","$remote_address-$time-$aff_id",time()+8800,"/",".compasservicesinc.com",0);
   }
} else {
   $temp=split("-",$cart_id);
   $ip=$temp[0];
   $time=$temp[1];
   $aff_id=$temp[2];
}
$id=$cart_id;
mysql_connect("$DBHost","$DBUser","$DBPass");
mysql_select_db("$DB");
$result=mysql_query("select * from $items where id='$sku_id'");
$row  =  mysql_fetch_row($result);
$item_sku=$row[1];
$item_name=$row[4];
$category=$row[2];
$subcategory=$row[3];
$extra_title=$row[19];
$keywords=$row[20];
$style="font-size: 9pt; height: auto; width: auto;";
if ($sku_id!="") {
   echo "\n";
   echo "\n";
   echo "\n
\n";
   echo "\n";
//echo "\n\n\n\n";
if ($command=="calc" || $add2cart=="OrderNow") {
  mysql_connect("$DBHost","$DBUser","$DBPass");
  mysql_select_db("$DB");
  // Find Multi Price
  echo "\n\n";
  if ($price_id!="") {
     $result=mysql_query("select * from multi_price where id='$price_id'");
     $row  =  mysql_fetch_row($result);
	 if ($row[4]!=1) {
        $base_price=$row[3]*$quantity;
	 } else {
	    $orig_price_id=$price_id;
	    $result2=mysql_query("select * from multi_price where item_id='$sku_id' and top_qty>'$quantity' order by top_qty asc");
		$temp_count=mysql_num_rows($result2);
		if ($temp_count>0) {
           $row2  =  mysql_fetch_row($result2);
  	       $price_id=$row2[0];
		   $base_price=$row2[3]*$quantity;
		   if ($orig_price_id!=$price_id) {
		      echo "
Adjusted Quantity Pricing: $$row2[3] x $quantity\n";
		   }
		   
		} else {
		   $result2=mysql_query("select * from multi_price where item_id='$sku_id' order by top_qty desc");
           $row2  =  mysql_fetch_row($result2);
 	       $price_id=$row2[0];
		   $base_price=$row2[3]*$quantity;
		   if ($orig_price_id!=$price_id) {
		     echo "
Adjusted Quantity Pricing: $$row2[3] x $quantity\n";
		   }
		}
	 }
  }
  
  // Main Price is set, so check options.
  echo "\n\n\n";
  echo "\n\n\n";
  if ($option_count>0) {
     echo "\n\n\n";
     for ($i=0;$i<$option_count;$i++) {
	    echo "\n\n\n";
	    $temp_option=explode("|",$option_value[$i]);
		echo "\n\n\n";
		if ($temp_option[2]!="") {
		   $item_price=$item_price+$temp_option[2];
		}
		$cart_option_name[$i]=$temp_option[0];
		$cart_option_id[$i]=$temp_option[1];
		$cart_option_value[$i]=$temp_option[3];
     }
	 //$item_price=$item_price+$base_price;
	 //$price_shown=$item_price;
  }
  $item_price=$item_price+$base_price;
  $price_shown=$item_price;
   if ($add2cart=='OrderNow') {
      if ($quantity=="" || $quantity==0) {$quantity=1;}
	  if ($price_shown=="0.00" || $price_shown=="" || $price_shown=="0") {
	     $add_cart_error=1;
	  }
	  echo "\n\n";
	  $date_now=time();
	  if ($add_cart_error==0) {
         mysql("$DB", "INSERT INTO cart (id,cust_id,sku_id,quantity,from_multi,final_price,aff_id,date_in) 
	            values ('','$cart_id','$sku_id','$quantity','$price_id','$price_shown','$aff_id','$date_now')");
	     $result=mysql_query("select * from cart where cust_id='$cart_id' and sku_id='$sku_id' and date_in='$date_now'");
	     $row  =  mysql_fetch_row($result);
	     $cart_item_id=$row[0];
	 
	     // Section off information for options side of cart.
	     if ($option_count>0) {
	        for ($i=0;$i<$option_count;$i++) {
		       if ($option_value[$i]!="") {
	              mysql("$DB", "INSERT INTO cart_options (id,cust_id,sku_id,item_option_id,item_option_value,cart_id,date_in) 
			              values ('','$cart_id','$sku_id','$option_id[$i]','$cart_option_id[$i]','$cart_item_id','$date_now')");
		       }
	        }
         }
         echo "
Item Added To Cart\n";
         $item_counter=0;
         $result=mysql_query("select quantity from cart where cust_id='$cart_id'");
         while  ($row  =  mysql_fetch_row($result))  {
            $item_counter++;
         }
         echo "
\n";
         if ($item_counter==1) {
            echo "You have $item_counter item in your cart. View Cart";
         } elseif ($item_counter>1) {
            echo "
You have $item_counter items in your cart. View Cart";
         }
         echo "\n";
      } else {
	     echo "
Item NOT Added To Cart\n";
		 echo "
$add_cart_error_name[$add_cart_error]\n";
	  }
   }
}
	  echo "\n\n";
	  echo "
\n";
      echo " ";
	  echo "Your are here: Home :: ";
	  $result2=mysql_query("select * from categories where cat_id='$category'");
      $row2  =  mysql_fetch_row($result2);
	  echo "$row2[1]";
	  if ($subcategory!="0") {
	     $result3=mysql_query("select * from subcategory where id='$subcategory'");
         $row3  =  mysql_fetch_row($result3);
	     echo " :: $row3[1]";
	  }
	  echo "";
      echo "  :: $item_name";
	  echo "";
	  echo "";
	  echo "  | 
\n";
      echo " \n";
	  echo " | 
\n";
	  echo "| \n";
      echo " | 
\n";
      echo "

\n";
// Pull Cart Item Information
$result=mysql_query("select * from $items where id='$sku_id'");
while  ($row  =  mysql_fetch_row($result))  {
   echo "