// JavaScript validation
function resetForm(what) {
	var f = $F(what).split(';');
	var method = f.shift();
	var isValid = true;
	if (method == 'inline') {
		for (var i = 0; i < f.length; i++) {
			var ff = f[i].split(':');
			if (ff.length < 3) ff[2] = ff[0];
			if ($(ff[2])) $(ff[2]).hide();
		}
	}
	return true;
}

function validateForm(what) {
	var f = $F(what).split(';');
	var method = f.shift();
	var isValid = true;
	if (method == 'inline') {
		for (var i = 0; i < f.length; i++) {
			var error = '';
			var ff = f[i].split(':');
			var ff0 = ff[0];
			ff0 = ff[0].split(',')[0];
			if (ff.length < 3) ff[2] = ff[0];
			if ($(ff[2])) $(ff[2]).hide();
			if ($(ff0)) error = eval(ff[1] + '("' + ff[0] + '", "' + ff[2] + '")');
			if ($(error)) { 
				$(error).show();
				isValid = false;
			}
		}
		return isValid;
	}
	var errorText = '';
	for (var i = 0; i < f.length; i++) {
		var ff = f[i].split(':');
		var ff0 = ff[0];
		ff0 = ff[0].split(',')[0];
		if (ff.length < 3) ff[2] = ff[0];
		if ($(ff0)) errorText += eval(ff[1] + '("' + ff[0] + '", "' + ff[2] + '")');
	}
	if (!errorText) return true;
	alert (errorText);
	return false;
}

function blank(ff0, ff2) {
	if ($(ff0).present()) return '';
	return ff2;	
}

function blankgroup(ff0, ff2) {
	var group = ff0.split(',');
	for (var i = 0; i < group.length; i++) {
		if ($(group[i]).present()) return '';
	}
	return ff2;	
}

function number(ff0, ff2) {
	if (!($F(group[i]).match(/^\d+$/) == null)) return '';
	return ff2;
}

function numbergroup(ff0, ff2) {
	var group = ff0.split(',');
	for (var i = 0; i < group.length; i++) {
		if (!($F(group[i]).match(/^\d+$/) == null)) return '';
	}
	return ff2;	
}

function match(ff0, ff2) {
	var group = ff0.split(',');
	for (var i = 1; i < group.length; i++) {
		if ($F(group[0]) != $F(group[i])) return ff2;
	}
	return '';	
}

function email(ff0, ff2) {
	val = $F(ff0);
	var error_atsign = true;
	var error_period = true;
	for (var i=0; i < val.length; i++) {
		var vchar = val.charAt(i);
		if (vchar == "@") error_atsign = false;
		if (vchar == ".") error_period = false;
	}
	if ((error_atsign) || (error_period) || (val.length < 6)) return ff2;
	return '';
}

// Create Tagger Prototype Object
var formTracker = Class.create();

formTracker.prototype = {

	initialize: function(formID, selector, selectorR, codeField) {

		this.formID = formID;
		this.codeField = codeField;
		this.codeFieldAmt = codeField + '-amt';

		this.xhq = this.getNumber('xh-qty');
		this.xhc = $F('xh-cost');
		this.xhs = this.getRadio('xh-shipping');
		
		this.xgq = this.getNumber('xg-qty');
		this.xgc = $F('xg-cost');
		this.xgs = this.getRadio('xg-shipping');

		this.cdrq = this.getNumber('xg-cdr-qty');
		this.cdrc = $F('xg-cdr-cost');

		this.xqCalc = new Array();
		this.xqCalc[0] = 1;
		this.xqCalc[1] = 1.5;
		this.xqCalc[2] = .25;

		this.track(selector);		
		this.trackR(selectorR);		
		this.formCalc();
	},
	
	getRadio: function(field) {
		return Form.getInputs(this.formID, 'radio', field).find(function(radio) { return radio.checked; }).value;
	},
	
	getRadioID: function(field) {
		return Form.getInputs(this.formID, 'radio', field).find(function(radio) { return radio.checked; }).readAttribute('id');
	},

	getNumber: function(id) {
		if ($F(id).match(/^\d+$/) == null) return 0;
	 	return $F(id);
	},

	track: function(selector) {
		$$(selector).each(function(value, index) {
			v = $(value).readAttribute('id')
			Event.observe(value, 'blur', this.formCalc.bindAsEventListener(this)); 
		}.bind(this));
	},

	trackR: function(selectorR) {
		$$(selectorR).each(function(value, index) {
			Event.observe(value, 'click', this.formCalc.bindAsEventListener(this)); 
			Event.observe(value, 'click', this.formShip.bindAsEventListener(this)); 
		}.bind(this));
	},

	formShip: function(e) {
		var oID;
		var r = Event.element(e);
		var xhsID = this.getRadioID('xh-shipping');
		var xgsID = this.getRadioID('xg-shipping');
		if (r.readAttribute('id').match(/xg/)) {
			$(xhsID).removeAttribute('checked');
			oID = r.readAttribute('id').replace(/xg/,'xh');
		} else {
			$(xgsID).removeAttribute('checked');
			oID = r.readAttribute('id').replace(/xh/,'xg');
		}
		$(oID).checked = 'checked';
	},

	formCalc: function() {
		var xhq = this.getNumber('xh-qty');
		var xhs = this.getRadio('xh-shipping');
		var	xhsID = this.getRadioID('xh-shipping');
		var xgq = this.getNumber('xg-qty');
		var xgs = this.getRadio('xg-shipping');
		var	xgsID = this.getRadioID('xg-shipping');
		var	discount = 1;
		if (xhq > 0) {
			if ((xhsID == 'xh-shipping-1' || xhsID == 'xh-shipping-3') && xhq > 10) {
				shipping = 0;
			} else {
				if (xhq > 2) { 
					shipping = (this.xqCalc[2] * (xhq-2) + this.xqCalc[1]) * xhs;
				} else { 
					shipping = this.xqCalc[xhq-1] * xhs;
				}
			}
			$('xh-total').value = this.xhc * xhq;
			$('xh-shipping-total').value = shipping;
		} else {
			$('xh-total').value = '';
			$('xh-shipping-total').value = '';
		}
		if (xgq > 0) {
			if ((xgsID == 'xg-shipping-2' && xhsID == 'xh-shipping-2') || (xgsID == 'xg-shipping-4' && xhsID == 'xh-shipping-4')) {
				shipping = 0;
			} else {
				if (xgq > 2) { 
					shipping = (this.xqCalc[2] * (xgq-2) + this.xqCalc[1]) * xgs;
				} else { 
					shipping = this.xqCalc[xgq-1] * xgs;
				}
			}
			$('xg-total').value = this.xgc * xgq;
			$('xg-shipping-total').value = shipping;
		} else {
			$('xg-total').value = '';
			$('xg-shipping-total').value = '';
		}
		this.cdrq = this.getNumber('xg-cdr-qty');
		if (this.cdrq > 0) {
			$('xg-cdr-total').value = this.cdrq * this.cdrc;
		} else {
			$('xg-cdr-total').value = '';
		}
// Calculate Totals
		var xht = $F('xh-total')*1;
		var xgt = $F('xg-total')*1;
		var xct = $F('xg-cdr-total')*1;
		var xhst = $F('xh-shipping-total')*1;
		var xgst = $F('xg-shipping-total')*1;
		var dt = this.getDiscount();
		var ds = this.getDiscountShipping();
		var amountTotal = (xht + xgt + xct)*dt;
		var shippingTotal = (xhst + xgst)*ds;
		var total = amountTotal + shippingTotal;
		if (total == 0) total = '';
		$('iph-total').value = total;	
		$('amount').value = amountTotal;	
		$('shipping').value = shippingTotal;	
	},
	
	getDiscount: function() {
		var code = $F(this.codeField).toLowerCase();
		switch (code) {
			case 'cz7hbpp':
			return .9;
			break;
			case 'y7gstpk':
			return .75;
			break;
			case 'kh3cz54':
			return .60;
			break;
			case 'cf7cn59':
			return .50;
			break;
		}
		return 1;
	},
	
	getDiscountShipping: function() {
		var code = $F(this.codeField).toLowerCase();
		switch (code) {
			case 'hx9dt6f':
			return 0;
			break;
		}
		return 1;
	}
}
