function CalcKeyCode(aChar) {
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
}
function checkNumber(val) {
  var lastchar = val.charAt((val.length) - 1);
  var cCode = CalcKeyCode(lastchar);
  if (cCode < 48 || cCode > 57 ) {
    var myNumber = val.substring(0, (val.length) - 1);
    return myNumber;
  } else {
    return val;
  }
}

function showChaps() {
  Element.show($('chapscalculator'));
  Element.hide($('internationalcalculator'));
  $('chapstab').addClassName('active');
  $('internationaltab').removeClassName('active');
}
function showIP() {
  Element.show($('internationalcalculator'));
  Element.hide($('chapscalculator'));
  $('internationaltab').addClassName('active');
  $('chapstab').removeClassName('active');
}
function lookupEuroPerMonthExisting(toLookup) {
  switch(true) {
    case toLookup<4000.01       :
                                initialrate=14;
                                break;
    case toLookup<8000.01       :
                                initialrate=17;
                                break;
    case toLookup<12000.01      :
                                initialrate=20;
                                break;
    default                     :
                                initialrate=25;
                                break;
  }
  return initialrate;
}
function lookupNonEuroPerMonthExisting(toLookup) {
  switch(true) {
    case toLookup<4000.01       :
                                initialrate=14;
                                break;
    case toLookup<8000.01       :
                                initialrate=20;
                                break;
    case toLookup<12000.01      :
                                initialrate=25;
                                break;
    default                     :
                                initialrate=30;
                                break;
  }
  return initialrate;
}
function lookupEuroPerMonthCorpExisting(toLookup) {
  switch(true) {
    case toLookup<4000.01       :
                                initialrate=11;
                                break;
    case toLookup<8000.01       :
                                initialrate=14;
                                break;
    case toLookup<12000.01      :
                                initialrate=17;
                                break;
    default                     :
                                initialrate=22;
                                break;
  }
  return initialrate;
}
function lookupNonEuroPerMonthCorpExisting(toLookup) {
  switch(true) {
    case toLookup<4000.01       :
                                initialrate=11;
                                break;
    case toLookup<8000.01       :
                                initialrate=17;
                                break;
    case toLookup<12000.01      :
                                initialrate=22;
                                break;
    default                     :
                                initialrate=27;
                                break;
  }
  return initialrate;
}
function lookupEuroPerMonthNew(toLookup) {
  switch(true) {
    case toLookup<4000.01       :
                                initialrate=11;
                                break;
    case toLookup<8000.01       :
                                initialrate=14;
                                break;
    case toLookup<12000.01      :
                                initialrate=17;
                                break;
    default                     :
                                initialrate=22;
                                break;
  }
  return initialrate;
}
function lookupNonEuroPerMonthNew(toLookup) {
  switch(true) {
    case toLookup<4000.01       :
                                initialrate=11;
                                break;
    case toLookup<8000.01       :
                                initialrate=17;
                                break;
    case toLookup<12000.01      :
                                initialrate=22;
                                break;
    default                     :
                                initialrate=27;
                                break;
  }
  return initialrate;
}
function lookupEuroPerMonthCorpNew(toLookup) {
  return 11;
}
function lookupNonEuroPerMonthCorpNew(toLookup) {
  return 11;
}
function calculate(typeOfCalculation) {
  var boschapstariff = 25;
  var corpchapstariff = 12;
  var monthlyonlinefee=25;
  resetOutputs(typeOfCalculation);
  switch(typeOfCalculation) {
    case 'existingchaps'                :
                                        var existingcost=($('existingpermonth').value*boschapstariff*12);
                                        var newcost=($('existingpermonth').value*corpchapstariff*12);
                                        if(((existingcost-newcost)-(12*monthlyonlinefee))<=0) {
                                          failsafe('chaps');
                                        } else {
                                          $('chapsoutputone').innerHTML="&pound;"+existingcost;
                                          $('chapsoutputtwo').innerHTML="&pound;"+(newcost+(12*monthlyonlinefee))+" (includes Extra Feature cost of &pound;300 per year)";
                                          $('chapsoutputthree').innerHTML="&pound;"+((existingcost-newcost)-(12*monthlyonlinefee))+" per year";
                                        }
                                        break;
    case 'newchaps'                     :
                                        var existingcost=($('newpermonth').value*$('newtariff').value*12);
                                        var newcost=($('newpermonth').value*corpchapstariff*12);
                                        if(((existingcost-newcost)-(12*monthlyonlinefee))<=0) {
                                          failsafe('chaps');
                                        } else {
                                          $('chapsoutputone').innerHTML="&pound;"+existingcost;
                                          $('chapsoutputtwo').innerHTML="&pound;"+(newcost+(12*monthlyonlinefee))+" (includes Extra Feature cost of &pound;300 per year)";
                                          $('chapsoutputthree').innerHTML="&pound;"+((existingcost-newcost)-(12*monthlyonlinefee))+" per year";
                                        }
                                        break;
    case 'existinginternational'        :
                                        var existingcost=((lookupEuroPerMonthExisting($('existingcrossaverage').value)*$('existingcrosspermonth').value)+(lookupNonEuroPerMonthExisting($('existingnoneuroaverage').value)*$('existingnoneuropermonth').value))*12;
                                        var newcost=((lookupEuroPerMonthCorpExisting($('existingcrossaverage').value)*$('existingcrosspermonth').value)+(lookupNonEuroPerMonthCorpExisting($('existingnoneuroaverage').value)*$('existingnoneuropermonth').value))*12;
                                        if((existingcost-newcost-(12*monthlyonlinefee))<=0) {
                                          failsafe('international');
                                        } else {
                                          $('internationaloutputone').innerHTML="&pound;"+existingcost;
                                          $('internationaloutputtwo').innerHTML="&pound;"+newcost+" (includes Extra Feature cost of &pound;300 per year)";
                                          $('internationaloutputthree').innerHTML="&pound;"+(existingcost-newcost-(12*monthlyonlinefee))+" per year";
                                        }
                                        break;
    case 'newinternational'             :
                                        var existingcost=((($('newcrosspermonth').value*$('newcrosstariff').value)+($('newnoneuropermonth').value*$('newnoneurotariff').value))*12)
                                        var newcost=((lookupEuroPerMonthNew($('newcrossaverage').value)*$('newcrosspermonth').value)+(lookupNonEuroPerMonthNew($('newnoneuroaverage').value))*$('newnoneuropermonth').value)*12;
                                        if(((existingcost-newcost)-(12*monthlyonlinefee))<=0) {
                                          failsafe('international');
                                        } else {
                                          $('internationaloutputone').innerHTML="&pound;"+existingcost;
                                          $('internationaloutputtwo').innerHTML="&pound;"+newcost+" (includes Extra Feature cost of &pound;300 per year)";
                                          $('internationaloutputthree').innerHTML="&pound;"+((existingcost-newcost)-(12*monthlyonlinefee))+" per year";
                                        }
                                        break;
  }
}

var intNote = "<p class=\"smalltext\">The Corporate Online cost per year for International Payments is based on a reduction of &pound;3 from our standard manual payments.</p>";

var chapsNote = "<p class=\"smalltext\">This saving is calculated by the difference in our standard manual payment charge of &pound;25 and our online charge of &pound;12 per payment less the monthly fee for the Extra Feature CHAPS service pack.<br /><br /></p>";
var newChapsNote = "<p class=\"smalltext\">This saving is calculated by using our online charge of &pound;12 per payment less the monthly fee for the Extra Feature CHAPS service pack.<br /><br /></p>";

function failsafe(type) {
  if(type == 'international') $('internationaloutputs').innerHTML="<h3><span>Your Potential Savings</span></h3><p>In this instance we are not able to save you money. However there are many other benefits in using <a href='/corporate/online-services/corporate-online.html'>Corporate Online</a>.</p>"
  if(type == 'chaps') $('chapsoutputs').innerHTML="<h3><span>Your Savings</span></h3><p>In this instance we are not able to save you money. However there are many other benefits in using <a href='/corporate/online-services/corporate-online.html'>Corporate Online</a>.</p>"
}
function resetOutputs(typeOfCalculation) {
	if(typeOfCalculation == "existinginternational" || typeOfCalculation == "newinternational") {
  $('internationaloutputs').innerHTML='<h3><span>Your Potential Savings</span></h3><p><span>Your potential Corporate Online cost saving</span><span id="internationaloutputthree" class="out"></span></p><p><span>Total existing cost of International Payments <nobr>(per year)</nobr></span><span id="internationaloutputone" class="out"></span></p><p><span>Corporate Online International Payments cost <nobr>(per year)</nobr></span><span id="internationaloutputtwo" class="out"></span></p>'+intNote;
  } else if(typeOfCalculation == "existingchaps") {
  $('chapsoutputs').innerHTML='<h3><span>Your Savings</span></h3><p><span>Your Corporate Online cost saving</span><span id="chapsoutputthree" class="out"></span></p><p><span>Existing CHAPS cost <nobr>(per year)</nobr></span><span id="chapsoutputone" class="out"></span></p><p><span>Corporate Online CHAPS Cost <nobr>(per year)</nobr></span><span id="chapsoutputtwo" class="out"></span></p>'+chapsNote;
  } else if(typeOfCalculation = "newchaps") {
  	  $('chapsoutputs').innerHTML='<h3><span>Your Savings</span></h3><p><span>Your Corporate Online cost saving</span><span id="chapsoutputthree" class="out"></span></p><p><span>Existing CHAPS cost <nobr>(per year)</nobr></span><span id="chapsoutputone" class="out"></span></p><p><span>Corporate Online CHAPS Cost <nobr>(per year)</nobr></span><span id="chapsoutputtwo" class="out"></span></p>'+newChapsNote;
  }
  if(roundBlocks) roundBlocks();
}
