if (!JS_OXIMO) { var JS_OXIMO = new Object(); }

JS_OXIMO.pages = {

	init: function(){
	
		// when calculate link is clicked
		$('#calcsubmit').click(function(e) {
			e.preventDefault();
			JS_OXIMO.pages.calculateCost();
		});		
		// when enter button is pressed
		$('#costinput').keyup(function(e) {
			if(e.which == 13) JS_OXIMO.pages.calculateCost();
		});			
		// cancel submit 
		$('#calculateForm').submit(function () { return false; });
		
		// submit contact form when contact link is clicked
		$('#contactSubmit').click(function(e) {
			e.preventDefault();
			$('#contactForm').submit();
		});
		// submit contact form when enter is pressed
		$('#name').keyup(function(e) {
			if(e.which == 13) $('#contactForm').submit();
		});
		$('#contact_string').keyup(function(e) {
			if(e.which == 13) $('#contactForm').submit();
		});
		$('#email').keyup(function(e) {
			if(e.which == 13) $('#contactForm').submit();
		});
		$('#telephone').keyup(function(e) {
			if(e.which == 13) $('#contactForm').submit();
		});
	},
	
	/**
	 *	Calculate cost
	 */
	calculateCost: function(){
		// get value from input
		var price = $("#costinput").val().replace(',', '').replace('.', '').replace(' ', '');
		
		// re-add so users see the changes
		$("#costinput").val(price);
		
		var allOk = "true";
		
		// validate
		if(parseInt(price)<=0 || price=='' || isNaN(price)){
			var allOk="false";
			$("#price_wrong").css({'display' : 'block'});
			$("#estimated").css({'display' : 'none'});
		}
		
		// calculate
		if(allOk=="true"){
			$("#estimated_reduction").html(""+JS_OXIMO.pages.beautifyCurrency(Math.round(price*1.5)/100)+",- euro*");
			$("#estimated").css({'display' : 'block'});
			$("#price_wrong").css({'display' : 'none'});
		}
	},
	
	
	/**
	 * 	Make numbers pretty!
	 */
	 beautifyCurrency: function (vValue){
		   aDigits = vValue.toFixed(2).split(".");
		   aDigits[0] = aDigits[0].split("").reverse().join("").replace(/(\d{3})(?=\d)/g, "$1 ").split("").reverse().join("");
		   if(aDigits[1] == 0) return aDigits[0];
		   else return aDigits.join(",");
	},
	

	/**
	 * end of the object
	 */
	_eoo: true
}

jQuery(function($){
	JS_OXIMO.pages.init();
});
