//-----------------------------------------
function bidderChosen() {
	// SET A COOKIE WHEN THE USER CHOOSES HIS/HER BIDDER NAME, SO WE CAN PRESELECT IT IN THE FUTURE
	$bidder = document.form.bidder.options[document.form.bidder.selectedIndex].value;
	if ($bidder=='not listed') { window.location='bidderapp.html';}
	else {
		document.cookie = "GamblingAds=" + escape($bidder) + "; path=/; domain=GamblingAds.com; expires=" + "Fri, 1 Jan 2038 00:00:00";
	}
}

//------------------------------------------
function selectBidder() {
	$advertiser = readCookie();
	$advertiser = unescape($advertiser);
	if ($advertiser != null && document.form.bidder!==null) {
		for (counter=0; counter< document.form.bidder.length-1; counter++) {
			if ($advertiser == document.form.bidder[counter].value) {
				document.form.bidder.selectedIndex = counter;
				break;
			}
		}

		if (window.location.href.indexOf('auction')>-1) document.form.bid.focus(); // Don't run this if on the Account page
//		window.status=$advertiser;
	}
}
//------------------------------------------
function readCookie() {
	if (document.cookie == '') { return;} // Prevents scripting error if no cookies have been set.
	cookieArray = document.cookie.split(';')

	for ($counter=0; $counter<=cookieArray.length; $counter++) {
		if (cookieArray[$counter].indexOf('GamblingAds=')>-1) { // That's a different cookie.
			valueArray = cookieArray[$counter].split('=');
			return valueArray[1];
			break;
		}
	}
}

//------------------------------------------
function calculate() {

	// REJECT EMPTY INPUT

	$bid = document.form.bid.value;
	if ($bid=='') { alert("Please type in the bid you're thinking about so we can calculate effective PPC/CPM rates for you.");
		document.form.bid.focus();
		return false;
	}

	// REMOVE TRAILING .00

	$bid = $bid.replace(/\.00/, "");
	$bid = parseInt($bid);
	document.form.bid.value = $bid;

	// NUMBERS ONLY

	if (parseInt($bid) +0 != $bid) {
		alert("Bid must be a number. Please don't use punctuation.");
		document.form.bid.value="";
		document.form.bid.focus();
		return false;
	}


	// SHOW PPC/CPM EQUIVALENTS

	$clicks 		= parseInt(document.utility.clicks.value);
	$impressions	= parseInt(document.utility.impressions.value);

	bidderOutput ='('; // Have to prime it, or get an error if there are no clicks.
	if ($clicks >0)	{ bidderOutput += '$'+ (parseInt(($bid/$clicks)*100))/100 + ' PPC / '; }
	bidderOutput += '$'+ (parseInt(($bid/$impressions)*100))/100 +' CPM)';
	document.getElementById('bidderMetrics').innerHTML = bidderOutput;
}
//----------------------------------------
function validate() {

	// REMOVE TRAILING .00

	$bid = document.form.bid.value;
	$bid = $bid.replace(/\.00/, "");
	$bid = parseInt($bid);
	document.form.bid.value = $bid;

	// NUMBERS ONLY

	if (parseInt($bid) +0 != $bid) {
		alert("Bid must be a number. Please don't use punctuation.");
		document.form.bid.value="";
		document.form.bid.focus();
		return false;
	}



	// PROPER INCREMENT

	$increment = parseInt(document.utility.increment.value);
	if ($bid % $increment != 0) {
		alert ("Bid must be a multiple of $" +$increment+ ".");
		document.form.bid.value="";
		document.form.bid.focus();
		return false;
	}
	


	// EXCEEDS MINIMUM

	$minimum = parseInt(document.form.highbid.value) + $increment;
	if ($bid < $minimum) {
		alert("Bid must be at least $" +$minimum+ ".");
		document.form.bid.value=$minimum;
		document.form.bid.focus();
		return false;
	}


	// VALID BIDDER

	description = document.form.bidder.value;
	if (description=="" || description.indexOf("not listed")!=-1) {
		alert("It looks like you need to choose your name from the list.");
		return false;
	}


	// ALL DONE

	document.form.submitbutton.value="Please wait...";
	return true;

}
//------------------------------------------
function passwordReminder() {
	$advertiser = readCookie();
	document.write('<BR><A href="/password-reminder.cgi?' +document.utility.whichAuction.value+ '+' +$advertiser+ '">(Email my password to me.)</A>');
}
