function clearField(sText){
  sText = sText.replace(/,/, "");
  sText = sText.replace(/\$/, "");
  sText = sText.replace(/\%/, "");
  return sText;
}
function IsNumeric(sText){
  sText = clearField(sText);
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
  //check for numeric characters
  return objRegExp.test(sText);
}
function clone(o) {
    var c = new Object();

    for (var e in o) {
      c[e] = o[e];
    }
    return c;
  }

var Calc = {
	errors: "",
	fields: {
		"name" 		: null,
		"balance" 	: null,
		"rate"		: null,
		"payment"	: null
	},
	filled_fields: {},
	plan: {},
	total_pay: 0,
	init: function(){
		this.calc_form = $("calc_form");
		if(!this.calc_form){
			return false;
		}
		this.calc_form.onsubmit = this._submit.bind(this);

		Calc.content = $("content");
	},
	_submit: function(){
		this.errors = "";
		var plan = false;
		this.total_pay = 0;
		$$(".radiobutton").each(function(el){
			el = $(el);
			if(el.checked){
				plan = el.getValue();
			}
		}.bind(this));

		if(!plan){
			this.setErrors("<div>You have not chosen a plan.</div>");
			return false;
		}
		var submit = true;
		this.is_cart = false;
		$$(".f1").each(function(el){
			this.addToChecker(el);
		}.bind(this));
		if(this.checkOnErrors()){
			submit = false;
		}else{
			Calc.filled_fields["f1"] = clone(this.fields);
		}
		$$(".f2").each(function(el){
			this.addToChecker(el);
		}.bind(this));
		if(this.checkOnErrors()){
			submit = false;
		}else{
			Calc.filled_fields["f2"] = clone(this.fields);
		}
		$$(".f3").each(function(el){
			this.addToChecker(el);
		}.bind(this));
		if(this.checkOnErrors()){
			submit = false;
		}else{
			Calc.filled_fields["f3"] = clone(this.fields);
		}
		$$(".f4").each(function(el){
			this.addToChecker(el);
		}.bind(this));
		if(this.checkOnErrors()){
			submit = false;
		}else{
			Calc.filled_fields["f4"] = clone(this.fields);
		}
		$$(".f5").each(function(el){
			this.addToChecker(el);
		}.bind(this));
		if(this.checkOnErrors()){
			submit = false;
		}else{
			Calc.filled_fields["f5"] = clone(this.fields);
		}
		if(this.errors.length == 0 && !this.is_cart){
			this.setErrors("<div>No Entered Cards</div>");
		}
		//alert(plan);
		if(plan == "fixed"){
			var fixed_payment = $("fixed_payment").getValue();
			//alert(fixed_payment);
			//alert(this.total_pay);
			if(fixed_payment < this.total_pay){
				this.setErrors("<div>The amount you plan to spend each month to pay off your credit card debts is less than the sum of your minimum monthly payments.</div>");
				submit = false;
			}
		}else if(plan == "time"){
			var monthes_payments 	= $("monthes_payments").getValue();
			var years_payments		= $("years_payments").getValue();
			if(monthes_payments.length > 0 && !IsNumeric(monthes_payments)){
				this.errors += '<div>Invalid entry for the number of months in which you want to pay off your credit card debts. Please enter a number.</div>';
				submit = false;
			}
			if(years_payments.length > 0 && !IsNumeric(years_payments)){
				this.errors += '<div>Invalid entry for the number of years in which you want to pay off your credit card debts. Please enter a number.</div>';
				submit = false;
			}
			if(years_payments.length == 0 && monthes_payments.length == 0){
				this.errors += '<div>Please specify when you want to be free of credit card debts.</div>';
				submit = false;
			}
			if(!submit){
				this.setErrors(this.errors);
			}
		}
		if(submit && this.is_cart){
			//Calc.old_content = Calc.content.innerHTML;
			var requestData = new Ajax.Request(this.calc_form["action"],{
				method: this.calc_form["method"],
				parameters: this.calc_form.serialize(true),
				onComplete: function(transport) {
					if(transport.responseText.length == 0){
						return false;
					}
					$("results").show();
					$("results").update(transport.responseText);
					Calc.content.hide();
			}
			});
		}

		return false;
	},
	addToChecker: function(el){
		el = $(el);
		if(el.name.search(/name/) != -1){
			this.fields["name"] = el.getValue();
		}
		if(el.name.search(/balance/) != -1){
			this.fields["balance"] = el.getValue();
		}
		if(el.name.search(/rate/) != -1){
			this.fields["rate"] = el.getValue();
		}
		if(el.name.search(/payment/) != -1){
			this.fields["payment"] = el.getValue();
		}
	},
	checkOnErrors: function(){
		if(this.fields["name"].length == 0){
			return false;
		}
		var needValidate = false;
		for(var i in this.fields){
			if(this.fields[i].length > 0){
				needValidate = true;
			}
		}
		if(needValidate){
			if(this.fields["balance"].length == 0){
				this.errors += "<div>You didn't specify the amount owed on your "+this.fields["name"]+" card.</div>";
			}else if(!IsNumeric(this.fields["balance"])){
				this.errors += "<div>Invalid entry for the amount owed on your "+this.fields["name"]+" card. Please enter a number.</div>";
			}
			if(this.fields["rate"].length == 0){
				this.errors += "<div>You didn't specify the interest rate on your "+this.fields["name"]+" card. </div>";
			}else if(!IsNumeric(this.fields["rate"])){
				this.errors += "<div>Invalid entry for the interest rate on your "+this.fields["name"]+" card. Please enter a number.</div>";
			}
			if(this.fields["payment"].length == 0){
				this.errors += "<div>Invalid entry for the minimum monthly payment on your "+this.fields["name"]+" card. Please enter a number. </div>";
			}else if(!IsNumeric(this.fields["payment"])){
				this.errors += "<div>Invalid entry for the minimum monthly payment on your "+this.fields["name"]+" card. Please enter a number. </div>";
			}
			if(this.errors.length > 0){
				this.setErrors(this.errors);
				return true;
			}else{
				this.setErrors("");
			}
			this.total_pay += parseFloat(clearField(this.fields["payment"]));
			this.is_cart = true;
			return false;
		}
	},
	setErrors: function(errors){
		$("errors").update("");
		$("errors").update(errors);
	},
	change: function(){
		$("results").hide();
		Calc.content.show();
		return false;
		Calc.content.update(Calc.old_content);

		for(var i in Calc.filled_fields){
			if(Calc.filled_fields[i]["name"].length > 0){
				$$("."+i).each(function(el){
					el = $(el);
					if(el.name.search(/name/) != -1){
						el.setValue(Calc.filled_fields[i]["name"]);
					}
					if(el.name.search(/balance/) != -1){
						el.setValue(Calc.filled_fields[i]["balance"]);
					}
					if(el.name.search(/rate/) != -1){
						el.setValue(Calc.filled_fields[i]["rate"]);
					}
					if(el.name.search(/payment/) != -1){
						el.setValue(Calc.filled_fields[i]["payment"]);
					}
				}.bind(this));
			}
		}
	}
};