var standardprice;


function checkPromo() {
		promofield = document.getElementById('promo').value;
		if (promofield == promocodes['promocode']) {
			GiveDiscount();
			document.getElementById('promostatus').innerHTML = "<span class='greentext'>Your promocode has been successfully validated</span>";
		}
		else {
			document.getElementById('promostatus').innerHTML = "<span class='redtext'>Your promocode is invalid. Please try again</span>";
		}
}

function giveDiscount() {
	numtickets = document.getElementById('tickets').value;
	currentprice = document.getElementById('Amt').value;
	standardprice = currentprice;
	
	if (numtickets <= 2) {
		discountprice = currentprice - (promocodes['discount'] * numtickets);
		document.getElementById('Amt').value = discountprice;
		document.getElementById('card_total').value = discountprice;
		document.getElementById('cheque_total').value = discountprice;
		document.bookings.discountsize.value = (promocodes['discount'] * numtickets);
	}
	else {
		discountprice = currentprice - (promocodes['discount'] * 2);
		document.getElementById('Amt').value = discountprice;
		document.getElementById('card_total').value = discountprice;
		document.getElementById('cheque_total').value = discountprice;
		document.bookings.discountsize.value = (promocodes['discount'] * 2);
	}
}

function checkCode(code, id)
{
  $('promostatus').innerHTML = '<span class="greentext">Checking Promocode...Please wait</span>'
  
  // send off ajax request
  new Ajax.Request('promocheck.check.'+id, {
    parameters: {'promocode': code},
    onComplete: function(xhr) { checkCodeCallback(xhr.responseText); }
  });
}

function recalculate() {
	if ($('promostatus').innerHTML == '<span class="greentext"> Promocode has been recognised</span>') {
		giveDiscount();
	}
}

function unDiscount() {
		/*
		document.getElementById('Amt').value = standardprice;
		document.getElementById('card_total').value = standardprice;
		document.getElementById('cheque_total').value = standardprice;
		document.bookings.discountsize.value = 0;
		*/
}

function checkCodeCallback(response)
{
  // check for error
  var codeIsValid = (response == 'valid');
  
  if (codeIsValid) {
    $('promostatus').innerHTML = '<span class="greentext"> Promocode has been recognised</span>';
	document.bookings.isdiscount.value = "Yes";
	
    giveDiscount();
  }
  else {
    $('promostatus').innerHTML = '<span class="redtext"> Your code is invalid. Please try again</span>';
    document.bookings.isdiscount.value = "No";
	
    unDiscount();
  }
}
