function isInteger(val)
{
    if(val=='')
    {
        return true;
    }
    if (val.length==0)
    {
        return false;
    }
    for (var i = 0; i < val.length; i++) 
    {
        var ch = val.charAt(i)
        if (i == 0 && ch == "-")
        {
            continue
        }
        if (ch < '0' || ch > '9')
        {
            return false
        }
    }
    return true
}

function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '&pound;' + dblValue + '.' + strCents);
}


function update(val) {
	if (val=='') {
		document.getElementById('price1').innerHTML = '';
		document.getElementById('postage1').innerHTML = '';
		document.getElementById('price2').innerHTML = '';
		document.getElementById('postage2').innerHTML = '';
		document.getElementById('total').innerHTML = '';
	} else {
	if (val>10) { price = '6.5'; postage = '0.9'; }
	else if (val<11&&val>4) { price = '7.5'; postage = '1'; }
	else { price = '8.5'; postage = '1.5'; }
	document.getElementById('price1').innerHTML = val+' at '+formatCurrency(price);
	document.getElementById('price2').innerHTML = formatCurrency((price*val));
	document.getElementById('postage1').innerHTML = val+' at '+formatCurrency(postage);
	document.getElementById('postage2').innerHTML = formatCurrency((postage*val));
	document.getElementById('total').innerHTML = formatCurrency((postage*val)+(price*val));
	document.getElementById('ppamount').value = price;
	document.getElementById('ppquantity').value = val;
	document.getElementById('ppshipping').value = postage*val;
	}
}

window.onload = function() {
	if (document.getElementById('update')) {
		document.getElementById('update').style.display = 'none';
	}
	document.getElementById('copies').onkeyup = function() {
		if (isInteger(this.value)) update(this.value);
		else {
			alert('The value you have entered is not a number. Please try again.');
			this.value = '';
		}
	}
}
