window.addEvent('load', function() {

	// checking inputs
	var matchyFun = new InputMatcher('waydQuant', /^\d*$/);
	var matchyFun2 = new InputMatcher('wyutdQuant', /^\d*$/);
	var matchyFun3 = new InputMatcher('stQuant', /^\d*$/);		

	// our onchange
	$$('.onChange').each(function(el) {
		el.addEvent('keyup', calculateIt);
		el.addEvent('click', calculateIt);
	});

	// the cuntry selecter
	$$('#selectDestItems a').each(function(el) {
		el.addEvent('click', function(){
			$$('#selectDestItems a').removeClass('selecta');
			this.addClass('selecta');
			calculateIt();
		});
	});

	// the auto kills for the inputs
	$('selectquant').getElements('input').each(function(el) {
		el.addEvent('change', function(){
			if (el.value == '') {
				el.value='0';
			}
			
		});
	});

	// the update link
	$('updateRow').addEvent('click', function(){
		$('checkOrder').getElement('ul').highlight('#330000', '#1c1c1c');
	});
	
	// the pay button
	$('payCoverUp').fade('hide');
	// click
	$('payButton').addEvent('click', function(){
		$('payCoverUp').fade('in');
	});


}); // end onload

// this calculates:
function calculateIt(){

//	alert('vag');

	// first we get the values:
	var waydQty = $('waydQuant').get('value').toInt();
	var waydCost = (waydQty * 25);
	
	var wyutdQty = $('wyutdQuant').get('value').toInt();
	var wyutdCost = (wyutdQty * 10);
	
	var stQty = $('stQuant').get('value').toInt();
	var stCost = (stQty * 10);
	
	var shipping;
	
	var totalCost = (
		waydCost +
		wyutdCost +
		stCost +
		5
	);
	
	if ($('destAust').get('class').contains('selecta')){
		shipping = 'Within Australia';
	} else{
		shipping = 'Outside Australia';
	}
	
	// now set our final string:
	
	if (waydQty > 0) { var waydString = " What Are You Doing (" + waydQty + " copies)." ;} else {waydString = "";}
	if (wyutdQty > 0) { var wyutdString = " Win You Up The Dick (" + wyutdQty + " copies)." ;} else {wyutdString = "";}
	if (stQty > 0) { var stString = " Self Titled (" + stQty + " copies)." ;} else {stString = "";}

	var payPalString =
		"Innocent Cabbage CD Order:"
		+ waydString
		+ wyutdString
		+ stString
	;

	// set it for paypal
	$('paypal_item_name').set('value', payPalString);
	$('paypal_amount').set('value', totalCost);
	
	
	
	// now set the thing at the end
	$('chkWAYDqty').set('html', waydQty);
	$('chkWAYDcost').set('html', '$' + waydCost);

	$('chkWYUTDqty').set('html', wyutdQty);
	$('chkWYUTDcost').set('html', '$' + wyutdCost);
	
	$('chkSTqty').set('html', stQty);
	$('chkSTcost').set('html', '$' + stCost);	

	$('chkSHIP').set('html', shipping);	

	$('chkTotal').set('html', '$' + totalCost);		


} // done it


var InputMatcher = new Class({

    initialize: function(input, matcher){
        this.input = $(input);
        this.value = this.input.get('value');
        this.matcher = RegExp.type(matcher) ? function(string, old) {
            return string.match(matcher) ? false : old;
        } : matcher;
        this.action.periodical(100, this);
    },
    
    action: function() {
        var value = this.input.get('value');
        if (this.value == value) return;
        var filtered = this.matcher(value, this.value);
        if (filtered !== false) {
            this.input.set('value', filtered);
            this.value = filtered;
        } else this.value = value;
    }
    
});

// usage new InputMatcher(element, /^[a-z]*$/);