// scripts/buy_now_constants.js MUST also be included with this file for it to function correctly

var licencePrice = new Array(2);
var supportPrice = new Array(10);
var themePrice = const_themePrice;
var trainingPrice = new Array(3);
var seminarTrackerPrice = const_seminarTrackerPrice1Year;

//Standard Author licences
licencePrice[0] = new Array(10);
licencePrice[0][0] = const_standardAuthor1;
licencePrice[0][1] = const_standardAuthor2;
licencePrice[0][2] = const_standardAuthor3;
licencePrice[0][3] = const_standardAuthor4;
licencePrice[0][4] = const_standardAuthor5;
licencePrice[0][5] = const_standardAuthor6;
licencePrice[0][6] = const_standardAuthor7;
licencePrice[0][7] = const_standardAuthor8;
licencePrice[0][8] = const_standardAuthor9;
licencePrice[0][9] = const_standardAuthor10;

//Commercial Author licences (inc. Standard support)
licencePrice[1] = new Array(10);
licencePrice[1][0] = const_commercialAuthor1;
licencePrice[1][1] = const_commercialAuthor2;
licencePrice[1][2] = const_commercialAuthor3;
licencePrice[1][3] = const_commercialAuthor4;
licencePrice[1][4] = const_commercialAuthor5;
licencePrice[1][5] = const_commercialAuthor6;
licencePrice[1][6] = const_commercialAuthor7;
licencePrice[1][7] = const_commercialAuthor8;
licencePrice[1][8] = const_commercialAuthor9;
licencePrice[1][9] = const_commercialAuthor10;

//Support prices
supportPrice[0] = const_standardSupport1;
supportPrice[1] = const_standardSupport2;
supportPrice[2] = const_standardSupport3;
supportPrice[3] = const_standardSupport4;
supportPrice[4] = const_standardSupport5;
supportPrice[5] = const_standardSupport6;
supportPrice[6] = const_standardSupport7;
supportPrice[7] = const_standardSupport8;
supportPrice[8] = const_standardSupport9;
supportPrice[9] = const_standardSupport10;

//Training Prices
trainingPrice[0] = const_trainingOnSite;
trainingPrice[1] = const_trainingOffSiteFull;
trainingPrice[2] = const_trainingOffSiteHalf;


function updatePrices() {
	var oLicenceType = document.forms[0].licence_type;
	var oLicenceNum = document.forms[0].licence_num;
	var oSupport = document.forms[0].support;
	var oTheme = document.forms[0].theme;
	var oTraining = document.forms[0].training;
	var oSeminarTracker = document.forms[0].seminartracker;

	var lLicence = document.getElementById('priceLicence');
	var lSupport = document.getElementById('priceSupport');
	var lTheme = document.getElementById('priceTheme');
	var lTraining = document.getElementById('priceTraining');
	var lSeminarTracker = document.getElementById('priceSeminarTracker');
	var lTotal = document.getElementById('priceTotal');
	var lTotal2 = document.getElementById('priceTotal2');

	if ( oLicenceType && oLicenceNum && lLicence && oSupport && lSupport && oTheme && lTheme && oTraining && lTraining && oSeminarTracker && lSeminarTracker && lTotal )
	{
		var iLicenceType = 0;
		if ( getRadioValue(oLicenceType) == 'Commercial' ) iLicenceType = 1;

		//Calculate licence price
		var pLicence = licencePrice[iLicenceType][oLicenceNum.value - 1];
		lLicence.value = formatNumber(pLicence);

		if (iLicenceType == 1) {
		    lLicence.value = formatNumber(pLicence) + " p.a."; 
		}

		//Calculate support price
		var pSupport = 0;
		if (iLicenceType == 1) {
		    oSupport[0].disabled = true;
		    oSupport[1].disabled = true;
		    oSupport[0].checked = true;
		    lSupport.value = "0 (included)";
		    
		}
		else {
			oSupport[0].disabled = false;
			oSupport[1].disabled = false;

			if ( getRadioValue(oSupport) == "Yes" ) {
				pSupport = supportPrice[oLicenceNum.value - 1];
				lSupport.value = formatNumber(pSupport) + " p.a.";
			}
			else {
				lSupport.value = "0";
			}        }
		
		//Calculate theme price
		var pTheme = 0;
		if ( getRadioValue(oTheme) == "Yes" ) {
			pTheme = themePrice;
        }
        lTheme.value = formatNumber(pTheme);

		//Calculate training price
		var pTraining = 0;

		switch(getRadioValue(oTraining)) {
			case 'On site':
		  		pTraining = trainingPrice[0];
		  		break;
			case 'At Seminar offices':
				pTraining = trainingPrice[1];
		  		break;
			case 'At Seminar offices half day':
				pTraining = trainingPrice[2];
		  		break;
			default:
				pTraining = 0;
		}
		lTraining.value = formatNumber(pTraining);

		//Calculate MySeminar Tracker price
		var pSeminarTracker = 0;
		if ( getRadioValue(oSeminarTracker) == "Yes" ) {
		    pSeminarTracker = seminarTrackerPrice;
		    lSeminarTracker.value = formatNumber(pSeminarTracker) + " p.a.";
		}
		else {
		    lSeminarTracker.value = "0";
		}
		
		//Calculate total price
		var pTotal = pLicence + pSupport + pTheme + pTraining + pSeminarTracker;
		lTotal.value = formatNumber(pTotal);
		lTotal2.value = formatNumber(pTotal);
    }
}


function getRadioValue(oRadio)
{
	for (var i=0; i<oRadio.length; i++)
	{
		if(oRadio[i].checked) return oRadio[i].value;
	}
}


function formatNumber(num) {

var decpoint = '.';
var sep = ',';

  // check for missing parameters and use defaults if so
  if (arguments.length == 2) {
    sep = ",";
  }
  if (arguments.length == 1) {
    sep = ",";
    decpoint = ".";
  }
  // need a string for operations
  num = num.toString();
  // separate the whole number and the fraction if possible
  a = num.split(decpoint);
  x = a[0]; // decimal
  y = a[1]; // fraction
  z = "";


  if (typeof(x) != "undefined") {
    // reverse the digits. regexp works from left to right.
    for (i=x.length-1;i>=0;i--)
      z += x.charAt(i);
    // add seperators. but undo the trailing one, if there
    z = z.replace(/(\d{3})/g, "$1" + sep);
    if (z.slice(-sep.length) == sep)
      z = z.slice(0, -sep.length);
    x = "";
    // reverse again to get back the number
    for (i=z.length-1;i>=0;i--)
      x += z.charAt(i);
    // add the fraction back in, if it was there
    if (typeof(y) != "undefined" && y.length > 0)
      x += decpoint + y;
  }
  return x;
}

function launchHelp(section)
{
	var url="buy_now_help.aspx?section=" + escape(section);
	var winHelp = window.open (url,"myhelpwindow","menubar=0,resizable=1,status=0,location=0,toolbar=0,directories=0,scrollbars=1,width=380,height=300");
	if ( winHelp )
	{
		winHelp.focus();
	}
}

function showOptionsForm()
{
    var oOptions = document.getElementById('optionsForm');
    var oDetails = document.getElementById('detailsForm');
    if (oOptions && oDetails)
    {
        oDetails.style.display = 'none';
        oOptions.style.display = 'block';
    }
}
function showDetailsForm()
{
    var oOptions = document.getElementById('optionsForm');
    var oDetails = document.getElementById('detailsForm');
    if (oOptions && oDetails)
    {
        oOptions.style.display = 'none';
        oDetails.style.display = 'block';
    }
}  