function sendNl() {
	var errorMsg = "";
	errorMsg += validateNlForm();
	if (errorMsg != "") {
		alert("Before you can proceed, you must correct the following errors:\n\n" + errorMsg);
	} else if (!calculate_mPayment($("#eec").val()))	{
		return false;
	} else {
		var queryString = $('#nlForm').formSerialize();
		$.ajax({
		   type: "POST",
		   url: "/a_nlSend.php",
		   data: queryString,
		   success: function(msg){
		   		$('#nlForm').html(msg); 
				window.setTimeout(hideNlSignup,2000);
		  }
		 });	
		showCalc();
	}
	return false;
}

function validateNlForm() {
	reqFields = new Array();
	// List required fields in this array as "[ Display Name , Form Field Name ]
	// NOTE - this function only works for text fields.  All others need to
	//        be hand coded in the submitMe() function.
	reqFields[0] = ["First Name", "first_name"];
	reqFields[1] = ["Last Name", "last_name"];
	reqFields[2] = ["Company", "company"];
	reqFields[3] = ["Title", "title"];
	reqFields[4] = ["Email", "email"];
	//reqFields[5] = ["Estimated Equipment Cost", "eec"];
	var returnVal = "";
	//Checking first_name
	if(($("input#first_name").val()==undefined) || ($("input#first_name").val()==""))	{
		
		returnVal += " - First Name field is required. \n";	
	}
	
	if(($("input#last_name").val()==undefined) || ($("input#last_name").val()==""))	{
		
		returnVal += " - Last Name field is required. \n";	
	}
	
	if(($("input#company").val()==undefined) || ($("input#company").val()==""))	{
		
		returnVal += " - Company field is required. \n";	
	}
	
	if(($("select#title").val()== "none") || ($("input#title").val()==""))	{
		
		returnVal += " - Title field is required. \n";	
	}
	
	if(($("input#email").val()==undefined) || ($("input#email").val()==""))	{
		
		returnVal += " - Email field is required. \n";	
	}
	
	/*for (i=0; i < reqFields.length; i++) {
		//alert($("input#company").val());
		//var fieldType = (eval("document.nlForm." + reqFields[i][1] + ".type") == "select-one") ? "select" : "input";
		var formField = $("input#"+reqFields[i][1]).val();
		
		if (formField[0] == "" || formField[0] == undefined) {
			alert("debug"+formField[0]); 
			returnVal += " - " + reqFields[i][0] + " field is required. \n";
		}
	}*/
	// Test the rest of the validation:
	// var email_regex = new RegExp("/^(\w+[\.|\-])*\w+\@(\w+[\.|\-])*\w+\.\w+$/");
	// var emailVal = $('#nlForm input[@name=email]').fieldValue();
	// alert(emailVal[0]);
	// if(!email_regex.test(emailVal[0])) {
	// 	returnVal += " - Email address is invalid. \n";
	// }
	return returnVal;
}

function showCalc() {
	
	$("#eec2").val($("#eec").val());
	$("#calc-signup").fadeOut("500");
	$("#calc-results").fadeIn("500");
}

function calculate_mPayment(principal_orig){
	
	var principal = principal_orig.replace(/\,/g,''); 
	if (principal<1 || principal>3000000 || isNaN(principal)){
		alert("Please use an Estimated Equipment Cost between 1 and 3,000,000.00.");
		return false;
	}
	var monthly_i_rate = 0.00908333333; //10.9% / 12						
	var months = 36;
	var monthly_payment = 0;
	//var tmp = pow(2,3);=(monthly_i_rate+(monthly_i_rate/((Math.pow((1+monthly_i_rate),months))-1)))*principal
	monthly_payment = currency_erize((monthly_i_rate+(monthly_i_rate/((Math.pow((1+monthly_i_rate),months))-1)))*principal);
	
	$("#result").html(monthly_payment);
	return monthly_payment;
}

function currency_erize(val)
{
	var nStr = val.toFixed(2);
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}
