function calculate_fees()
{
	var bid = parseInt($('max_bid').value);
	var type = 'preliminary';//$('type').value;
	var transaction_fee = 200;
	var postal_fee = 25;
	var buyer_fee = 0;
	var gate = 30;
	
	if(isNaN(bid))
		bid = 0;
	
	$('r_transaction_fee').update(transaction_fee);
//	$('r_postal_fee').update(postal_fee);
//	$('r_gate').update(gate);

	if($('homeowners').checked===true) {
		buyer_fee = bid * 0.2;
	} else if (bid < 100) {
		buyer_fee = 20;
	} else if (bid < 200) {
		buyer_fee = 45;
	} else if (bid < 400) {
		buyer_fee = 75;
	} else if (bid < 600) {
		buyer_fee = 95;
	} else if (bid < 800) {
		buyer_fee = 125;
	} else if (bid < 1000) {
		buyer_fee = 150;
	} else if (bid < 1200) {
		buyer_fee = 175;
	} else if (bid < 1400) {
		buyer_fee = 200;
	} else if (bid < 1700) {
		buyer_fee = 225;
	} else if (bid < 2000) {
		buyer_fee = 250;
	} else if (bid < 2500) {
		buyer_fee = 275;
	} else if (bid < 3000) {
		buyer_fee = 300;
	} else if (bid < 4000) {
		buyer_fee = 325;
	} else if (bid < 17500) {
		buyer_fee = 350;
	} else if (bid > 17500) {
		buyer_fee = bid * 0.02;
	}
//	$('r_buyer_fee').update(buyer_fee);
	
	if (bid < 1000) 
		internet_bid_fee = 35;
	else if (bid < 2000) 
		internet_bid_fee = 50;
	else
		internet_bid_fee = 75;
//	$('r_internet_bid_fee').update(internet_bid_fee);

	$('r_copart_fee').update(buyer_fee + internet_bid_fee + gate);
	$('r_total').update(bid + buyer_fee + internet_bid_fee + gate + transaction_fee);// + postal_fee
}
function calculate_bid_allowance()
{
	var min_deposit = 400;
	var per_deposit = 10;
	var deposit = parseInt($('deposit').value, 10);
	if (deposit < min_deposit) 
	{
		deposit = min_deposit;
		$('deposit').value = min_deposit;
	}
	var bid_allowance = parseInt(deposit * 100/per_deposit, 10);
	$('bid-allowance').update(bid_allowance);
}
function bidIncDo(act)
{
	if (validateBid(true))
	{
		var cur_bid = parseInt($('max_bid').value, 10);
		var cur_bid_st = cur_bid;
		var bid_inc = get_bid_inc(cur_bid);
	
		var new_bid = 0;
		if(act==1)
		{
			new_bid = cur_bid + bid_inc;
			$('max_bid').value = cur_bid = new_bid;
		} else if (act==2) {
			new_bid = cur_bid - bid_inc;
			if(new_bid < 0)
				new_bid = 0;
			$('max_bid').value = cur_bid = new_bid;
		}
		calculate_fees();
	}
}
function validateBid(fromForm) {
	var isBidValid = validateBidAmt($('max_bid').value, $('max_bid'));
	
	if(fromForm) {
		return isBidValid;
	} else 
		return false;
}
function validateBidAmt(v, elm) {
	var iCurrentBid = parseInt($('cur_bid').value, 10);
	var iBidAmt = get_bid_inc(iCurrentBid);
	var iMinBidAmt = 20;
	
	if(v.indexOf('.') >= 0)
	{
		var howManyDecimals = v.split('.');
		if(howManyDecimals.length > 2) { //If we have more than 1 decimal
			$('advice-common-before').innerHTML = message.invalidBidAmt;
			$('max_bid').focus();
			return false;
		} 
		else { // 1 decimal found
			// check the number after the decimal
			if(howManyDecimals[1] !== null && howManyDecimals[1] !== '') {
				// If its .0 or .00 then trim it off and return true, for no error
				if(howManyDecimals[1] == "0" || howManyDecimals[1] == "00") {
					$(elm).value = howManyDecimals[0];
					if((($(elm).value - iCurrentBid) % iBidAmt) > 0) {
						$('max_bid').focus();
						return false;
					}
					return true;
				}
				else {
					// User tried to enter cents, return an error.
					$('max_bid').focus();
				}
			}
			else {
				// User entered a 100., which is valid, ignore the decimal and fix value
				$(elm).value = howManyDecimals[0];
				if((($(elm).value - iCurrentBid) % iBidAmt) > 0) {
					$('max_bid').focus();
					return false;
				}
				return true;
			}
		}
	}
	else if(v.indexOf(',') >= 0) {
		$('max_bid').value = iBidAmt;
		$('max_bid').focus();
		return false;
	}
	else if(v < 0 || isNaN(v))	{
		$('max_bid').value = iBidAmt;
		$('max_bid').focus();
		return false;
	}
	$('advice-common-before').innerHTML = "";
	
	return true;
}
function get_bid_inc(bid)
{
	var inc = 0;
	 
	if(bid < 5)
		inc = 1;
	else if(bid < 40)
		inc = 5;
	else if(bid < 100)
		inc = 10;
	else if(bid < 1000)
		inc = 25;
	else if(bid < 5000)
		inc = 50;
	else if(bid < 25000)
		inc = 100;
	else if(bid < 50000)
		inc = 250;
	else if(bid < 100000)
		inc = 500;
	else 
		inc = 1000;
	
	return inc;
}

Event.observe(window, 'load', function() {
	$('max_bid').value ='4000';
	//$('type').value='preliminary';
	calculate_fees();
	//calculate_bid_allowance();
});