function stopRKey(evt) { 
  var evt = (evt) ? evt : ((event) ? event : null); 
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
}

function optional(inputID) {
	var custAccount = $('#'+inputID).val();
	if (custAccount == '') { $("#"+inputID).addClass('optional'); }
	else { $("#"+inputID).removeClass('optional'); }	
} 

$(document).ready(function() {
    $("#step2").hide();
    $("#step3").hide();
    $("#step4").hide();
    $("#loading1").hide();
    $("#loading2").hide();    
    document.onkeypress = stopRKey; 
});	

function resetForm()
	{
	$("#step3").fadeOut(function() {
   		$("#step2").fadeOut(function() {
   			 $("#step1").fadeIn();
			});
   		});	
	}

function step3()
	{
	$("#step2").fadeOut(function() {
   			 $("#step3").fadeIn();
		});	
	}
function step4()
	{
	$("#step3").fadeOut(function() {
   			 $("#step4").fadeIn();
		});	
	}	
		
function getRates(minGalRange,maxGalRange)
	{	
	var retailerID = $('#retailerID').val();
	var annualUsage = $('#annualUsage').val();
	var month = $('#month').val();
	var bill = $('#bill').val();
	var fuelTaxRate = $('#fuelTaxRate').val();	
	// strip away non numeric & decimal characters
	var annualUsage = annualUsage.replace(/[^\d\.-]/g,'');
	var bill = bill.replace(/[^\d\.-]/g,'');
	var fuelTaxRate = fuelTaxRate.replace(/[^\d\.-]/g,'');
	// validate
	var error = 0;
	var message = '';
	
	if (isNaN(retailerID)) { var message = message+'Invalid Retailer ID\n'; var error = 1;  }
	if (isNaN(annualUsage) || annualUsage == 0) { var message = message+'Please enter a valid Annual Usage.\n'; var error = 1; }
	if (isNaN(bill) || bill == 0) { var message = message+'Please enter a valid Bill Amount. '; var error = 1; }	
	if (isNaN(fuelTaxRate)) { var message = message+'Please enter a valid fuelTaxRate or leave blank if not applicable. \n'; var error = 1;  }
		
	if (annualUsage < minGalRange) { var message = message+'You entered an Annual Usage number that is below the minimum allowed for this calculator.  Please give us a call for more information.\n'; var error = 1; }
	if (annualUsage > maxGalRange) { var message = message+'You entered an Annual Usage number that is above the maximum allowed for this calculator.  Please give us a call for more information.\n'; var error = 1; }
	
	if (error == 0)
		{
		$("#step1").fadeOut(function() {
   			$("#loading1").fadeIn();
			$.ajax({
			   type: "POST",
			   cache: false,
			   url: "../ajax/getRates.php",
			   data: "retailerID="+retailerID+"&annualUsage="+annualUsage+"&bill="+bill+"&month="+month+"&fuelTaxRate="+fuelTaxRate,
			   success: function(html){
			     var returnArray = html.split(":"); 
			     $("#custRateDisplay").html('$'+returnArray[0]);
			     $("#custRate").val(returnArray[0]);
			     $("#widgetUseID").val(returnArray[2]);
				 $("#resultParagraph").html('<p>You could pay the above Stable Propane Pricing rate every month, even in the winter! Never worry about a high propane bill again!</p><a onclick="step3();" class="button forward orange">Sign Up Today!</a><a class="button back" onclick="resetForm()">Back</a>');
			            
			     if(returnArray[1] > '0') 
			     	{
				    $("#savings").html('<p>Last '+$('#month').val()+' you could have saved: <span id="savingsTotal">$'+returnArray[1]+'</span></p>');
				    $("#custSavings").val(returnArray[1]);
			     	}
			   
			   	$("#loading1").fadeOut(function() {
				$("#step2").fadeIn('slow');
				});
			     
			   }
			 });
			});
		}
	else { alert(message); }
	}
	
function signUp()
	{
	var retailerID = $('#retailerID').val();
	var custName = $('#custName').val();
	var custPhone = $('#custPhone').val();
	var custEmail = $('#custEmail').val();
	var custAccount = $('#custAccount').val();
	var widgetUseID = $('#widgetUseID').val();
	
	// validate
	var error = 0;
	var message = '';
	if (custName == '') { var message = message+'Please enter your name.\n'; var error = 1; }
	if (custPhone == '') { var message = message+'Please enter your phone number.\n'; var error = 1; }
	if (isNaN(widgetUseID)) { var message = 'Error: Usage ID not set. Please restart.\n'; var error = 1; }
		
	if (error == 0)
		{
		$("#step3").fadeOut(function() {
   			$("#loading2").fadeIn();	
			$.ajax({
			   type: "POST",
			   cache: false,
			   url: "../ajax/signUp.php",
			   data: "retailerID="+retailerID+"&widgetUseID="+widgetUseID+"&custName="+custName+"&custPhone="+custPhone+"&custEmail="+custEmail+"&custAccount="+custAccount,
			   success: function(html){	   
			     	$("#loading2").fadeOut(function() {
					$("#step4").fadeIn('slow');
					});
			   		}
				});	
				
			});
		}
	else { alert(message); }
	}
