$(document).ready(function(){

	$(":input").change(checkForm);
	$(":input").keyup(checkForm);

	//for backpages with dark backgrounds
	if($('body').css('color') == 'rgb(255, 255, 255)' || '#ffffff'){
		$('.inputSpan').css({'color':'#000000'});
		$('.rateSpan').css({'color':'#000000'});
	}

	function checkForm(eventObject){
		currentTarg = eventObject.target;
		if(currentTarg.type == 'button')return;
		//figure out which form is being validated
		if(currentTarg.id.indexOf('_pf')>=0){
			validateForm(currentTarg,1);
		}else{
			validateForm(currentTarg,0);
		}
	}

	function validateForm(input,pf){
		var msg = '';
		$(':input', input.form).each(function() {
			if(this.value == null || this.value.length == 0){
				//this.value = 0;
			}else if(this.value < 0){
				msg += 'The ' + this.title + ' field must be a positive integer.<br/>';
				//$(this).css({'background-color' : '#F0B2B2'});
			}else if(!this.value.match(/^\d+(?:\.\d{0,3})?$/)){
				msg += 'The ' + this.title + ' field must only contain numbers.<br/>';
				//$(this).css({'background-color' : '#F0B2B2'});
			}else{
				//$(this).css({'background-color' : '#fff'});
			}
		});
		if(msg != ''){
			$(input.form).find(".error-msg").addClass('error');
			$(input.form).find(".error-msg").html(msg);
		}else{
			$(input.form).find(".error-msg").removeClass('error');
			computeForm(input.form,pf);
		}
	}

	function computeForm(form,pf) {
		var term = 'term';
		var rate = 'rate';
		var price = 'price';
		var monthly = 'monthly';
		var cashDown = 'cashDown';
		if(pf){
			term = term + '_pf';
			rate = rate + '_pf';
			price = price + '_pf';
			monthly = monthly + '_pf';
			cashDown = cashDown + '_pf';
		}

		var i = form[rate].value;
		if (i >= 1.0) {
			i = i / 100.0;
		}
		i /= 12;
		var pow = 1;
		for (var j = 0; j < form[term].value; j++)
		pow = pow * (1 + i);
		if(pf){
			if(i == 0){
				money = "" + (form[price].value - form[cashDown].value)/form[term].value;
			}else{
				money = "" + .01* Math.round(100*((form[price].value - form[cashDown].value) * pow * i) / (pow - 1));
			}
			dec = money.indexOf(".");
			if(dec == -1){
				dollars = money;
			}else{
				dollars = money.substring(0,dec);
			}
			cents = money.substring(dec+1,dec+3);
			cents = (cents.length < 2) ? cents + "0" : cents;
			money = dollars + "." + cents;

			if(money>0){
				$('#monthly_pf').text('$' + money);
			}else{
				$('#monthly_pf').text('$' + 0);
			}

		}else{
			//reverse
			yrs = eval (form[term].value);
			rte = eval (i);
			pmt = eval (form[monthly].value);
			if(rte == 0){
				amt = ( pmt * yrs ) + eval(form[cashDown].value + 0);
				amt=Math.round(amt);
			}else{
				amt = pmt/(rte+(rte/(Math.pow((1+rte),(yrs))-1)));
				amt = amt*100;
				amt=Math.round(amt);
				amt=amt/100;
				amt=amt + eval(form[cashDown].value) + 0;
			}
			if(amt>0 && amt != 'Infinity'){
				$('#price').text('$' + amt.toFixed(2));
			}else{
				$('#price').text('$' + 0);
			}
		}
	}

	searchInventory = function(it,pp,pf){
		var price = '';
		if(pf){
			price = $('#price_pf').val();
		}else{
			price = $('#price').text().replace('$','');
		}
		var str = baseURL;
		if(price == 0){
			str += it + '-inventory';
		}else{
			str += it + '-inventory?range.' + pp + '=1|' + price;
		}
		top.location.href = str;
	}
});
