var old_value = '';

function openWindow(name,url,parameter)
{
	theWindow = window.open(url,name,parameter);
	theWindow.focus();
}

function showCode(url)
{
	Element.toggle('codePanel');
	if(document.getElementById('codePanel').style.display == 'none')
	{
		document.getElementById('promotion_code').value = '';
		updatePrice(url);
	}
}

function disableAll()
{
	disableButton();
	disableVoucher();
	disablePhoneNumber();
}

function enableAll()
{
	enableButton();
	enableVoucher();
	enablePhoneNumber();
}

function disableButton()
{
	document.getElementById('submit_button').style.color='#ACA899';
	document.getElementById('submit_button').disabled = true;
}

function disableVoucher()
{
	document.getElementById('product_id').style.background='#EBEBEB';
	document.getElementById('product_id').disabled = true;
}

function disablePhoneNumber()
{
	document.getElementById('dest').style.background='#EBEBEB';
	document.getElementById('dest').disabled = true;
}

function enableButton()
{
	document.getElementById('submit_button').style.color='#E51F1F';
	document.getElementById('submit_button').disabled = false;
}

function enableVoucher()
{
	document.getElementById('product_id').style.background='#FFFFFF';
	document.getElementById('product_id').disabled = false;
}

function enablePhoneNumber()
{
	document.getElementById('dest').style.background='#FFEBBC';
	document.getElementById('dest').disabled = false;
}

function updateResponse(url)
{
	disableButton();
	ajaxUpdate(
		'response',
		url,
		Form.serialize('frm'),
		'enableButton()'
	);
}

function retryRequest(url,attempt)
{
	disableButton();
	ajaxUpdate(
		'response',
		url,
		Form.serialize('frm') + '&attempt=' + attempt,
		'enableButton()'
	);
}

function updatePrice(url)
{
	destNum = document.getElementById('dest').value;
	productId = document.getElementById('product_id').value;
	promotionCode = document.getElementById('promotion_code').value;
	
	if(productId != '' && destNum != '')
	{
		disableButton();
		ajaxUpdate(
			'price',
			url,
			'product_id=' + productId + '&code=' + promotionCode + '&voucher=' + destNum,
			'enableButton();'
		);
	}
	else
	{
		document.getElementById('price').innerHTML = '';
	}
}

function updateVoucher(url)
{
	destNum = document.getElementById('dest').value;

	if(old_value != destNum)
	{
		old_value = destNum;
		ajaxUpdate(
			'voucher',
			url,
			'style=width:320px;height:25px;font-size:18px;&voucher=' + destNum,
			'enableButton();updatePrice(\'' + url + '\');'
		);
	}
	else
	{
		enableButton();
	}
}