function setCookie(name, value, days)
	{
	var expire = new Date();
	expire.setTime(expire.getTime() + Math.round(86400000*days));
	document.cookie = name + "=" + escape(value)
	+ ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
	}

function getCookie(Name)
	{
	var search = Name + "="
	if(document.cookie.length > 0)
		{ 
		offset = document.cookie.indexOf(search) 
		if(offset != -1)
			{ 
			offset += search.length 
			end = document.cookie.indexOf(";", offset) 
			if (end == -1) 
			end = document.cookie.length
			return unescape(document.cookie.substring(offset, end))
			} 
		}
	}

function random(maxValue)
	{
	var randscript = -1
	while (randscript < 1 || randscript > maxValue || isNaN(randscript))
		{
		randscript = parseInt(Math.random()*(maxValue+1))
		}
	return randscript
	}

function Currency(number)
	{
	var num = new String(number);
	if(num.indexOf(".") == -1)
		{
		intLen = num.length;
		toEnd = intLen;
		var strLeft = new String(num.substring(0, toEnd));
		var strRight = new String("00");
		}
	else
		{
		pos = eval(num.indexOf("."));
		var strLeft = new String(num.substring(0, pos));
		intToEnd = num.length;
		intThing = pos + 1;
		var strRight = new String(num.substring(intThing, intToEnd));
            
		if(strRight.length > 2)
			{
			nextInt = strRight.charAt(2);
			if(nextInt >= 5)
				{
				strRight = new String(strRight.substring(0, 2));
				strRight = new String(eval((strRight * 1) + 1));
				if((strRight * 1) >= 100)
					{
					strRight = "00";
					strLeft = new String(eval((strLeft * 1) + 1));
					}               
				if(strRight.length <= 1)
					{
					strRight = new String("0" + strRight);
					}
				}
			else
				{
				strRight = new String(strRight.substring(0, 2));
				}
			}
		else
			{
			if(strRight.length != 2)
				{
				strRight = strRight + "0";
				}
			}
		}
	if(strLeft.length > 3)
		{
		var curPos = (strLeft.length - 3);
		while(curPos > 0)
			{
			var remainingLeft = new String(strLeft.substring(0, curPos));
			var strLeftLeft = new String(strLeft.substring(0, curPos));
			var strLeftRight = new String(strLeft.substring(curPos, strLeft.length));
			strLeft = new String(strLeftLeft + "," + strLeftRight);
			curPos = (remainingLeft.length - 3);
			}
		}
	strWhole = strLeft //+ "." + strRight; since i dont need the Cents this is out. - Pip
	finalValue = "$" + strWhole;
	return(finalValue);
	}

function calcmax(p)
	{
	var mp 
	var max = sAvail - sLeft;
	for(x = 0; x <= max; x++)
		{
		mp = p * x;
		if ((mp >= cash) || (x == max))
			{
			return x;  
			}  
		}
	}

function randomevent()
	{
	var y = random(3);
	if(y == 3)
		{
		var xx = random(15); 
		if(xx == 4)
			{
			alert("The market is flooded with cheep homemade Acid!");
			price[2] = 100 + random(35); 
			}
		if(xx == 2)
			{
			alert("Addicts are buying speed at high prices!");
			price[8] = 1000 + random(500);
			}
		if(xx == 3)
			{
			alert("Weed prices have bottomed out!");
			price[0] = 10 + random(100);
			}
		if(xx == 5)
			{
			var lost = random(2000);
			if((cash - lost) < 0 ) lost = cash;
			alert("You were mugged in the subway, you lost " + Currency(lost));   
			cash = cash - lost; 
			if (cash < 0 ) cash = 0; 
			}
		if(xx == 1)
			{
			alert("Addicts are buying coke at very high prices");
			price[7] = 25000 + random (12000);
			}
		if(xx == 6)
			{
			alert("Major weed bust, prices are high!");
			price[0] = 1000 + random(1000);
			}
		if(xx == 7)
			{ 
			var poo = random(5);
			x = random(12) -1;
			alert("Your crackhead friend unloaded some " + drugs[x] + " on you!");
			yourdrugs[x] = drugs[x]; 
			yourprice[x] = 0;
			quants[x] = Number(quants[x]) + Number(poo);
			sLeft = Number(sLeft) + Number(poo);
			showyours();
			}
		if(xx == 8)
			{
			x = random(12) - 1
			alert(drugs[x] + " prices are low!");
			price[x] = price[x] / 3;
			}
		if(xx == 9)
			{
			alert("A bunch of hippies are in town. Weed, Shrooms, and Acid prices are high!");
			price[0] = 1000 + random(420);
			price[1] = 2000 + random(2000);
			price[2] = 4000 + random(2000);
			}
		if(xx == 10)
			{
			alert("Huge Opium bust, prices are out of control!");
			price[3] = 10000 + random(10000);
			}
		if(xx == 11)
			{
			x = random(12) - 1;
			alert("Big bust on "+ drugs[x] + ". Prices have doubled!");
			price[x] = price[x] * 2;   
			}
		if(xx == 12)
			{
			x = random(12) - 1;
			alert("There is shitty " + drugs[x] + " going around!");
			price[x] = price[x] / 4
			}
		if(xx == 13)
			{
			alert("You got BUSTED!!! Game over!!");
			daysleft = 0;
			}
		if(xx == 14)
			{
			alert("Big Pharmacy Raid! Ecstacy prices are LOW!!");
			price[11] = 1 + random(10);
			}
		if(xx == 15)
			{
			x = 500 + random(100);
			var x2 = confirm("Do you want to buy a Trench Coat with 50 more pockets for " + Currency(x) + "?");
			if((x2 == true) && (cash >= x))
				{
				cash = cash - Number(x);
				sAvail = Number(sAvail) + 50;
				}
			else if((x2 == true) && (cash < x))
				{
				alert("You don't have enough money!!");
				}
			}
		}
	}

function showyours()
	{
	for(x = 0; x < 12; x++)
		{
		document.drugform.DrugsYouHave.options[x].text = quants[x] + " - " + yourdrugs[x];
		document.drugform.DrugPrice.options[x].text = Currency(yourprice[x]);
		}
	}

function sellit()
	{
	var sel = document.drugform.DrugsYouHave.selectedIndex; 
	if(sel == -1)
		{ 
		alert("You need to select one.");
		return;
		} 
	quant = prompt("How much " + drugs[sel] + " do you want to sell?", quants[sel]); 
	if(quant == null) return;
	if(quant < 0)
		{
		alert("No negative numbers, mofo!");
		return; 
		}
	if ((quants[sel] - quant) >= 0)
		{
		quants[sel] = Number(quants[sel]) - Number(quant); 
		cash = cash + price[sel] * Number(quant);
		document.drugform.money.value = Currency(cash);
		sLeft = Number(sLeft) - Number(quant);
		document.drugform.spaceAvail.value = sAvail - 1;
		document.drugform.spaceLeft.value = sLeft; 
		if(Number(quants[sel]) == 0)
			{
			yourdrugs[sel] = "";
			yourprice[sel] = "";
			quants[sel] = "";
			}
		showyours();
		document.drugform.DrugsForSale.selectedIndex = -1; 
		document.drugform.DrugPrices.selectedIndex = -1; 
		document.drugform.DrugsYouHave.selectedIndex = -1; 
		document.drugform.DrugPrice.selectedIndex = -1; 
		}
	}

function buyit()
	{
	var sel = document.drugform.DrugsForSale.selectedIndex; 
	if(sel == -1)
		{ 
		alert("You need to select one.");
		return;
		} 
	 quant = prompt("How much " + drugs[sel] + " do you want?", calcmax(price[sel]) - 1); 
 	if(quant == null) return;
 	if(quant < 0)
		{
		alert("No negative numbers, mofo!");
		return;
		}
	if(quant == 0) return;
	if(((price[sel] * Number(quant)) <= cash) && ((Number(sLeft) + Number(quant)) <= Number(sAvail) - 1))
		{
		quants[sel] = Number(quants[sel]) + Number(quant); 
		yourdrugs[sel] = drugs[sel];
		yourprice[sel] = price[sel];
		cash = cash - price[sel] * Number(quant);
		sLeft = Number(sLeft) + Number(quant);
		document.drugform.spaceAvail.value = sAvail - 1;
		document.drugform.spaceLeft.value = sLeft; 
		showyours();
		document.drugform.money.value = Currency(cash); 
		document.drugform.DrugsForSale.selectedIndex = -1; 
		document.drugform.DrugPrices.selectedIndex = -1; 
		document.drugform.DrugsYouHave.selectedIndex = -1; 
		document.drugform.DrugPrice.selectedIndex = -1; 
		}
	else if ((quant > 0) && (cash < price[sel] * quant))
		{
		alert("You don't have enough money!!");
		return;
		}
	else
		{
		alert("Not Enough Space!!!");
		return;
		}
	}

function RefreshSale()
	{
	for(x = 0; x < 12; x++)
		{
		if((x == 0) || (x == 1) || (x == 8) || (x == 9))
			{
			price[x] = setprices[x] + random(420)
			}
		else if((x == 10) || (x == 11))
			{
			price[x] = setprices[x] + random(100)
			}
		else
			{
			price[x] = setprices[x] + random(5000)
			}
		}
	randomevent();
	daysleft = daysleft-1; 
	document.drugform.left.value = daysleft; 
	document.drugform.spaceAvail.value = Number(sAvail -1);
	document.drugform.spaceLeft.value = Number(sLeft); 
	if(daysleft == 1) alert("This is your last day!");
	if(daysleft <= 0)
		{
		alert("You have made " + Currency(cash) + " selling drugs, don't you feel proud?");
		daysleft = 31; 
		for(x = 0; x < 12; x++)
			{
			yourdrugs[x] = "";
			yourprice[x] = "";
			quants[x] = "";
			}
		if(cash > oldScore)
			{
			alert("CONGRATULATIONS: You have beaten you old record!");
			setCookie("AbleDrugDelater",cash,356);
			oldScore = cash;
			document.drugform.score.value = Currency(cash);
			if(username != "")
				{
				RecordScore(cash);
				}
			}

		document.drugform.left.value = daysleft; 
		cash = 2500;
		document.drugform.money.value = Currency(cash);
		sLeft = 0; 
		sAvail = 201;
		document.drugform.spaceAvail.value = sAvail -1;
		document.drugform.spaceLeft.value = sLeft; 
		showyours();
		}
	for(x = 0; x < 12; x++)
		{
		document.drugform.DrugsForSale.options[x].text = drugs[x];
		document.drugform.DrugPrices.options[x].text = Currency(price[x]);
		document.drugform.DrugsForSale.selectedIndex = -1; 
		document.drugform.DrugPrices.selectedIndex = -1; 
		document.drugform.DrugsYouHave.selectedIndex = -1; 
		document.drugform.DrugPrice.selectedIndex = -1;
		document.drugform.money.value = Currency(cash); 
		} 
	}

function handleHttpResponseScore()
	{
	results = http.responseText
	document.getElementById('dd_top').innerHTML = results;
	}

var isWorking = false;

function RecordScore(cash) {
	if(!isWorking && http)
		{
		var url = host + "/index.php?a=drug_dealer&b=record&c=" + cash;
		http.open("GET", url);
		http.onreadystatechange = handleHttpResponseScore;
		isWorking = true;
		http.send(null);
		}
	}

var http = getHTTPObject();