currPostUrl = "/webserv/bloClientServ.php";

function DoCalc()
{
	if( ! ValidateCalc() )
	{
		return(false);
	}
	
	DoPostAndGetTotals();
	return(false);
}

function ValidateCalc()
{
	if( $('calc_name').value == "" )
	{
		alert( "Please let us know your name.");
		$('calc_name').focus();
		return(false);
	}
	
	var EmailRegex = "^[a-zA-Z0-9\-\_\.]+\@[a-zA-Z0-9\-\_\.]+\.[a-zA-Z0-9]+$";
    if($('calc_email').value.search(EmailRegex) == -1 )
    {
    	alert( "Please enter in a valid email address.");
    	$('calc_email').focus();
    	return(false);
    }
	
	if( $('calc_totalvcm').value.search( /^[0-9\,]+\.?[0-9]?/ ) == -1 )
	{
		alert( "Please type in your estimated total monthly credit card transactions.");
		$('calc_totalvcm').focus();
		return(false);
	}
	
	if( $('calc_totalgross').value.search( /^[0-9\,]+\.?[0-9]?/ ) == -1 )
	{
		alert( "Please type in your estimated monthly gross income.");
		$('calc_totalgross').focus();
		return(false);
	}
	
	tempGross = $('calc_totalgross').value.replace( ",", "" );
	tempVcm = $('calc_totalvcm').value.replace( ",", "" );
	if( parseFloat(tempGross) < parseFloat(tempVcm))
	{
		alert( "Your total Visa/MC sales cannot be less than your total gross." );
		$('calc_totalgross').focus();
		return(false);
	}
	return(true);
}

function DoPostAndGetTotals()
{
	var postTo = currPostUrl;
	postTo += "?function=calc"
	postTo += "&totalvcm=" + $('calc_totalvcm').value;
	postTo += "&totalgross=" + $('calc_totalgross').value;
	postTo += "&name=" + $('calc_name').value;
	postTo += "&email=" + $('calc_email').value;
	
	var adj1 = cObj.getCookie("adj1");
	var adj2 = cObj.getCookie("adj2");
	adj2 = adj2.replace( " ", "" );
	postTo += "&adj1=" + adj1 + "&adj2=" + adj2;
	
	new Ajax.Request(postTo, {
			method: 'get',
			onSuccess: 
				function (msg) {
					if(msg != "" )
					{
						var lcMsg = msg.responseText.toLowerCase();
						if( lcMsg.indexOf("error") != -1 )
						{
							alert( msg.responseText );
						}
						else
						{
							var returnData = msg.responseText.evalJSON();
							DoUpdateCalcDisplay(returnData);
						}
					}
				}
		}
	);
}

function DoUpdateCalcDisplay(returnData)
{
	$('calcResults').innerHTML = "<div style=\"font-size:18px; text-align: center;\">May Qualify For:<div style=\"text-align: center; font-size: 28px; font-weight: bold; margin-top: 8px;\">$" + returnData.advance + "</div></div>";
	
	if(returnData.max == "1" )
	{
		$('calcResults').innerHTML += "<div style=\"margin-top: 14px; font-size: 8px; margin-left: 20px;\">Gross revenue limited total, ask your sales person.</div>";
	}
	var adj1 = cObj.getCookie("adj1");
	if( adj1 == "" ) {
		cObj.setCookie("adj1", returnData.adj1);
		cObj.setCookie("adj2", returnData.adj2);
	}
}
