<?php
if(isset($_GET["i"])){
	$item=explode(',',$_GET["i"]);
	$list='';
	$ct=count($item);
	if($ct % 2 == 1)
		die('Error: Quantity to item mismatch (not all items have a quantity or not all quantities have a item)');
	for($i=0;$i<$ct;$i=$i+2){
		$list.=$item[$i].'|'.$item[$i+1].',';
	}
	header('Location: http://secure.newegg.com/Shopping/AddToCart.aspx?Submit=ADD&ItemList='.$list.'&Note=links_on_the_community.newegg.com_forum_do_not_like_pipes');
	die();
}
else if(isset($_GET['source'])){
	die('<pre>'.htmlspecialchars(file_get_contents(basename($_SERVER['PHP_SELF']))).'<pre>');
}
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8"/>
		<title></title>
		<script type="application/javascript">
			function getURL(findMe,txt){
				if(!txt)
					txt=location.search;
				if(txt){
					txt=txt.slice(1);
					txt=txt.slice(txt.indexOf(findMe+'='));
					var l=txt.indexOf('&');
					if(l!=-1){
						return txt.slice(findMe.length+1,l);
					}
					else{
						return txt.slice(findMe.length+1);
					}
				}
				return null;
			}
			function convert(url){
				var file="<?php echo basename($_SERVER['PHP_SELF']); ?>",
				list=getURL('ItemList',url).replace(/\|/g,',');
				document.getElementById('output').value=location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '')+'/'+file+'?i='+list;
			}
		</script>
	</head>
	<body>
		<p>This page exist as a workaround for a stupid forum bug that prevents links with pipes (|) from working</p>
		<p>Insert newegg add to cart link here: <input onchange="convert(this.value)" onkeypress="if(event.which==13){convert(this.value)}"/>, the new URL will be printed below.<br/>
		<input id="output" readonly="readonly" style="width:100%"><br>
		If you would like the source code for this page <a href="./<?php echo basename($_SERVER['PHP_SELF']); ?>?source">here it is</a></p>
	</body>
</html>