function visitProvider(providerURL, productID, merchantID)
{
  var refPage = encodeURIComponent(window.location.href);
  window.open("visitMerchant.php?purl="+providerURL+"&pid="+productID+"&mid="+merchantID+"&ref="+refPage);
}

function updateLinks(advertDiv, linkID, merchantID, affiliateID)
{
 if (!document.getElementsByTagName) return false;
 var redirectPage="";
 var newLink="";
 var refPage = window.location.href;

 var links = advertDiv.getElementsByTagName("a");
 for ( var i=0; i < links.length; i++)
 {
   newLink = encodeURIComponent(links[i].href);
   redirectPage = "visitMerchant.php?lurl="+newLink+"&lid="+linkID+"&mid="+merchantID+"&affid="+affiliateID+"&ref="+refPage;
   links[i].href = redirectPage;
 }
}

function toggleLayer(whichLayer)
{
  var layer = getElementFromID(whichLayer);
  
  if (layer)
  {
    var layerStyle = layer.style;
    layerStyle.display = (layerStyle.display) ? "" : "none";
  }
}

function getElementFromID(elementID)
{
  var element = false;
  
  if (document.getElementById)
  {
    // standards based method
    element = document.getElementById(elementID)
  }
  else if (document.all)
  {
    // old msie method
    element = document.all[elementID];
  }
  else if (document.layers)
  {                                                               
    //  nn4 method
    element = document.layers[elementID];
  }
  
  return element;
}

// current id of DIV being updated
var requestID;
var loaded = new Array();

var responseSuccess = function(o){
   tdID=getElementFromID(requestID+"_td");
   if (tdID)
   {
     tdID.innerHTML = o.responseText;
     loaded[requestID] = true;
   }
}
var responseFailure = function(o){  alert ("Failure!!") }

var callbackObject =
{
  success:responseSuccess,
  failure:responseFailure                                                                                                                	 
}
                                                                                                                                         
function displayInfo(productID, loanAmount, loanTerm)
{
  if(window.opera)
  {
   window.open('loan.php?pid='+productID+'&la='+loanAmount +'&lt='+loanTerm, 'open_window', "menubar=no,width=550,height=600,toolbar=no");
   return;
  }

  requestID = productID;
  // toggle the info layer
  toggleLayer(requestID);

  // if loaded already then done
  if (loaded[requestID] == true)
    return;

  pageURL = 'loan.php?pid='+productID+'&la='+loanAmount +'&lt='+loanTerm+'&inPage=1';
  // make the request
  var cObj = YAHOO.util.Connect.asyncRequest('GET', pageURL, callbackObject);
}

function calculateLoan(){

	//load values from form into variables

	var frm=document.loancalc;
	var p=frm.p.value;
	var apr=frm.apr.value;
	var y=frm.y.value;
	
	//initialise those variables for calculation
	
	var n=0;
	var m=0;
	var t=0;
	var c=0;
	var r=0;
	var i=0;
	
	//validate inputs
	
	var errorMessage="";
	
	if (!v_number(p)) {errorMessage+="Amount of loan,\n";}
	if (!v_number(apr)) {errorMessage+="APR,\n";}
	if (!v_number(y)) {errorMessage+="Period of loan\n";}
	
	if (errorMessage.length!=0) {alert("The following entries are incorrect:\n\n"+errorMessage+"\nPlease correct these entries and\ntry again.");return false;}
	
	if (p.substring(0,1)=="£") {p=p.substring(1);}
	
	apr=apr/100;							//decimal value for apr
	i=12 * (Math.pow(apr+1,1/12)-1);		//nominal rate of interest
	//i=i/100;								//decimal value of nominal rate of interest
	r=i/12;									//monthly nominal rate;
	n=12 * y;								//number of monthly payments
	var q=1/(1+r);							//temp variable for common blocks
	
	m=(p*(q-1)) / (q*((Math.pow(q,n))-1));	//monthly repayment
	
	t=n*m;									//total amount payable
	c=t-p;									//cost of interest
	
	//load text boxes with results
	
	// quick NAN fix by Paul and Gary
	if( apr == 0 )
	{
		alert( "You cannot calculate a loan with 0% APR" );
		frm.n.value=n;
		frm.m.value=formatCurrency(0);
		frm.t.value=formatCurrency(0);
		frm.c.value=formatCurrency(0);
	}
	else
	{
		frm.n.value=n;
		frm.m.value=formatCurrency(m);
		frm.t.value=formatCurrency(t);
		frm.c.value=formatCurrency(c);
	}
	
	
	return false;
}

alertText="";

function ValidateLoan(thisForm){

  alertText="";
  
  // validate amount and term
  validAmount = ValidateAmount(thisForm.loanAmount);
  validTerm = ValidateTerm(thisForm.loanTerm);

  if (validAmount == false ) {
    thisForm.loanAmount.focus;
    alert (alertText);
    return false;
  }
  
  if (validTerm == false) {
    thisForm.loanAmount.focus;
    alert (alertText);
    return false;
  }

  if (thisForm.submitAC)
  {
    thisForm.submitAC.src = "images/go3.gif";
  }
  return true;
}

function ValidateAmount(Amount){

 var isValid = false;
 var amountStr = Amount.value.replace(",","")
 amountStr = amountStr.replace("£","")
 var amountVal = Number(amountStr);

 minAmount = 500;
 minCurr = "£500";
 maxAmount = 250000;
 maxCurr = "£250,000";
 
 if(!isNaN(amountVal))
 {
   if (amountVal >= minAmount && amountVal <= maxAmount)
   {
     isValid = true;
   }
 }

 if (!isValid)
 {
   Amount.className="texterror";
   alertText = "Loan amount must be between " + minCurr + " and " + maxCurr;
 }
 else Amount.className="text";

	// return result
 return isValid;
}

function ValidateTerm(Term){

  var termVal = Number(Term.value);

  if (termVal < 1 || termVal > 360)
  {
    Term.className="ierror";
    alertText = alertText + "\nPlease select a loan term";
    return false;
  }
   else Term.className="select";
  return true;
}




