function PopWarning()
{
   alert("You have opened an external link from Faraday Mortgage Associates Ltd. Faraday Mortgage Associates Ltd cannot be held responsible for the content of external websites.");
}


function ResizeScreen()
{

  $('.MortgageCalc').hide();
  $("#ContactResponse").hide();
  var LMC = $('#LeftMainContent').height();	
  var MC = $('#MainContentDiv').height();	
  if((LMC < 650) && (MC < 650))
  {
     $('#LeftMainContent').height(650);
     $('#MainContentDiv').height(650);	
  }
  else
  {
     if(MC > LMC)
	{$('#LeftMainContent').height(MC);}
     else
        {$('#MainContentDiv').height(LMC);}		
  }	

}

function mycarousel_initCallback(carousel){
         // Disable autoscrolling if the user clicks the prev or next button.
          carousel.buttonNext.bind('click', function() {
              carousel.startAuto(0);
          });
 
          carousel.buttonPrev.bind('click', function() {
              carousel.startAuto(0);
          });

          // Pause autoscrolling if the user moves with the cursor over the clip.
          carousel.clip.hover(function() {
             carousel.stopAuto();
          }, function() {
             carousel.startAuto();
       });
}


function addCommas(nStr) 
{
   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;
}

function checkNumber(min, max, str) 
{	
  for (var i = 0; i < str.length; i++) {
     var ch = str.substring( i, i + 1)
     if ((ch < "0" || "9" < ch) && ch != '.') 
        { return "false"; }
  }
  var num = 0 + str
  if (num < min || max < num) 
     { return "false"; }
  return str;
}

$('#CalculateMortgage').click(function() {
   var LoanTerm = checkNumber(5, 30, $("#LoanTerm").val());
   if(LoanTerm=="false")
   {
      alert("You have not entered a valid loan term.  Please try again")
      return false;	
   }
   var LoanAmount  = checkNumber(1, 999999999, $("#LoanAmount").val());
   if(LoanAmount=="false")
   {
      alert("You have not entered a valid loan amount.  Please try again")
      return false;	
   }
   var InterestRate = checkNumber(0.001, 1500, $("#InterestRate").val());
   if(InterestRate=="false")
   {
      alert("You have not entered a valid interest rate.  Please try again")
      return false;	
   }
   var F6 = InterestRate/100;
   var G6 = F6/12;
   var G7 = LoanTerm*12;
   var E1 = Math.pow ((1+G6),G7);
   var P  = ((LoanAmount* E1)*G6)/(E1 - 1);
   var T1 = P * G7;
   var T2 = T1 - LoanAmount;
   var P11 = P.toFixed(2);
   var T11 = T1.toFixed(2);
   var T21 = T2.toFixed(2);
   $("#MonthlyRepay").html(addCommas (P11));
   $("#TotalRepay").html(addCommas (T11));
   $("#InterestRepay").html(addCommas (T21));
   $('.SpacerRow').hide();
   $('.MortgageCalc').slideDown('normal');

});

$("#SubContact").click(function(){
   

   if ($("#contact_name").val().length ==0){
      alert("Please provide a contact name");
      return false;
   }
   if ($("#email").val().length ==0){
      alert("Please provide a contact e-mail address");
      return false;
   }

   if ($("#comments").val().length <2){
      alert("Please provide a short description of your query");
      return false;
   }

   $.post("ProcessContact.php", {contact_name: $("#contact_name").val(), email: $("#email").val(), 
 			         telephone: $("#telephone").val(), comments: $("#comments").val()}, function(data){
        $("#ContactResponse").html(data);
        $(".contactus").hide();
        $("#ContactResponse").show();
   });	
});

