var outBox = $('total');
var startPrice;
var cratingCharge = 300;
var total;
var fromIL;
var amountToTax = parseInt(0);
var taxTotal = parseInt(0);
var taxTotalFmt = parseInt(0);
var addons = parseInt(0);

window.addEvent('domready', function() {
	if($('theStartPrice')){
	
	if($('contactInfo')){
		$('contactInfo').setStyle('display', 'none');
	 }
	if($('totalPrice')){
		$('totalPrice').setStyle('display', 'block');
	}
	if($('nextStep')){
		$('nextStep').setStyle('display', 'block');
	}
	startPrice = parseInt($('theStartPrice').value);
	total = startPrice;
	processTax();
	processTotal();
	$$('tr.in input').addEvent('click', processAddons);
	$('addTax').addEvent('click', processTax);
	$('submitQuote').addEvent('click', revealContactForm);
	$('printQuote').addEvent('click', printQuote);
	//$('sendQuote').addEvent('click', validateQuote);
	$('submitBtn').addEvent('submit', validateQuote);
	
	}
});

function processAddons() {
	if (this.checked) {
		addons += parseInt(this.value);
	}
	else {
		addons -= parseInt(this.value);
	}
	processTax();
	processTotal();
}

function processTax() {
	amountToTax = parseInt(startPrice + addons + cratingCharge);
	taxTotal = parseInt(amountToTax*0.08);
	taxTotalFmt = formatCurrency(taxTotal);
	$('taxTotal').innerHTML = "[Add " + taxTotalFmt.toString() + "]";
}

function processTotal() {
	fromIL = $('addTax').checked;
	if( fromIL ) {
	  total = startPrice + addons + cratingCharge + taxTotal;
	}
	else {
	  total = startPrice + addons + cratingCharge;
	}
	total = formatCurrency(total);
	$('total').innerHTML = "Total: " + total.toString();
}

function printQuote(e) {
	window.print();
	new Event(e).stop();
}

function revealContactForm(e) {
	$('submitQuote').setStyle('display', 'none');
	$('or').setStyle('display', 'none');
	$('contactInfo').setStyle('display', 'block');
	$('name').focus();
	new Event(e).stop();
}

function validateQuote(e) {
	if (validate_required($('name'), "Please enter your name") == false) {
		style_text_error($('name'));
		//return false;
	}
	if (validate_email($('email'),"Please enter a valid e-mail address!") == false) {
		style_text_error($('email'));
		//return false;
	}
	return false;
}

function validate_required(field,alerttxt) {
	with (field) {
		if (value==null||value=="") {
			create_error_message(alerttxt);
			return false;
		}
		else {
			style_text_norm($('name'));
			remove_error_message();
			return true;
		}
	}
}

function validate_email(field,alerttxt) {
	with (field) {
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) {
			create_error_message(alerttxt);
			return false;
		}
		else {
			style_text_norm($('email'));
			remove_error_message();
			sendQuote();
			return true;
		}
	}
}

function sendQuote(e) {
	$('basePriceInp').value = formatCurrency(startPrice);
	$('optionsInp').value = formatCurrency(addons);
	$('cratingChargeInp').value = formatCurrency(cratingCharge);
	$('taxInp').value = taxTotal;
	$('totalInp').value = total;
	document.form.submit();
    
}

function create_error_message(msg) {
	if(!$('error_msg_div')) {
		var error_msg_div  = new Element('div', {id: 'error_msg_div'});
		error_msg_div.inject($('contactForm'), 'before');  
		error_msg_div.setStyles({ 'color': '#cc0000', 'margin-bottom': '10px' });
		$('error_msg_div').innerHTML = msg;
	}
}

function remove_error_message() {
	if($('error_msg_div')) {
		$('error_msg_div').remove();
	}
}

function style_text_error(o) {
	o.focus();
	o.setStyles({ 'border': '1px solid #f00', 'background': '#ffb9af', 'padding': '8px 8px 5px' });
}

function style_text_norm(o) {
	o.setStyles({ 'border': '1px solid #000', 'background': '#fff', 'padding': '8px 8px 5px' });
}


function formatCurrency(num) {
	num = isNaN(num) ? 0 : num;
	var rounded = round(num, 2).toFixed(2);
  
	// split to three parts
	var parts = rounded.match(/^(-?)([0-9]+)(.[0-9]+)$/);
	var sign = parts[1];
	var integer = parts[2];
	var fraction = parts[3];
  
	// split integer part into thousands
	var thousands = [];
	while (integer.length > 3) {
		thousands.unshift( integer.substr(integer.length - 3, 3) );
		integer = integer.substr(0, integer.length - 3);
	}
	thousands.unshift(integer);
  
	// separate thousands with comma and put it all together
	return sign + "$" + thousands.join(",") + fraction;
}

// Better rounding than with Math.round()
// Allows to specify the number of digits after decimal point
// round(5.1256, 2) --> 5.13
function round(nr, digits) {
	digits = digits || 0;
	var multiplier = Math.pow(10, digits);
	return Math.round(nr * multiplier) / multiplier;
};


