var api = {
	init: function(){
		api.slides.init();
		api.calc.init();
		$('#report p.show a').click(function(){
			if ($('#report form:visible').length <= 0)
			{
				$('#report form').slideDown(700);
			}
			else 
			{
				$('#report form').slideUp(500);
			}
			return false;
		});

		$('#order').
			find('#pkg,#quantity').change(api.orderPrice).end().
			find('p.option input').change(api.orderPrice).end();
		api.orderPrice();
	},
	orderPrice: function(){
		var price1 = 0, price2 = 0;
		var pkg1 = $('#pkg');
		var t = pkg1[0].options[pkg1[0].selectedIndex].text;
		var d = t.match(/\(\$([0-9]+)\)/i);
		if (d && d.length)
		{
			price1 = parseInt(d[1]);
		}
		var quantity = parseInt($('#quantity').val());
		if (quantity > 0)
			price1 *= quantity;

		$('#order p.option input:checked').each(function(){
			var p = parseInt(this.className.split('-')[1]);
			price2 += p;
		});

		$('#price').val(price1);
		$('#price_extra').val(price2);
		$('#price_total').val(price1 + price2);
	},
	calc: {
		init: function() {
			$('#calc dl.type1 input').bind('change', api.calc.update);
		},
		update: function() {
			var price = $('#price').val();
			var t = $('#traffic').val();
			var c = $('#con').val();
			$('#spm').html(c + '%');
			var cr = $('#cr').val();
			var adgr = $('#adgr').val();
			var sales = Number(t) * Number(c) * 0.01;
			var comm = sales * price * cr * 0.01;
			var profit =  (sales * price) - comm;
			$('#sales').val(Math.round(sales, 2));
			$('#comm').val(Math.round(comm, 2));
			$('#profits').val(Math.round(profit, 2));
			var days = adgr / (profit / 30);
			$('#days').val(Math.round(days, 0) + ' days');
		}
	},
	slides: {
		t: null, n: 0,
		init: function(){
			if ($('#slides').length <= 0)
				return;
			$('#slides p.next a').click(api.slides.next);
			api.slides.n = $('#slides div.slide').length;
			api.slides.t = setTimeout(api.slides.next, 10000);
		},
		next: function(){
			if (api.slides.t)
			{
				clearTimeout(api.slides.t);
				api.slides.t = null;
			}

			var l = parseInt($('#slides div.slider').css('left'));
			var w = $('#slides').width();
			l -= w;
			if (l <= - (api.slides.n * w))
			{
				l = 0;
			}
			$('#slides div.slider').animate({ left: l + "px"}, { duration: 700, easing: "swing" });

			api.slides.t = setTimeout(api.slides.next, 10000);
			return false;
		}
	}
};

$(document).ready(api.init);

function checkForm(theFrom){var why = "";if(theFrom.first_name)why += isEmpty(theFrom.first_name.value, "Name");if(theFrom.email)why += checkEmail(theFrom.email.value);if(why != ""){alert(why);return false;}theForm.submit();return false;
}

function trim(s) {return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );}

function isEmpty(b,d){var c="";if(trim(b).length==0){c="Please Enter "+d+"\n"}return c;}

function checkEmail(c){var d="";if(c.length>0){var b=/^.+@.+\..{2,4}$/;if(!(b.test(c))){d="Invalid Email Address";}else{var e=/[\(\)\<\>\,\;\:\\\"\[\]]/;if(c.match(e)){d="Invalid Email Address";}}}else{d="Invalid Email Address";}return d}
