﻿/************************************** Steve Doggett - Paypal ********************
 * Created By:		Steve Doggett
 * Creation Date:	7th July 1007
 * Edited ----------------------------------------------------------------------------
 *		By:				On:
 * Description -----------------------------------------------------------------------
 *		This file contains a number of functions for the shopping cart functionality
 *
 * Functions:
 *    addBook()
 *    changePostage(sVal)
 *    setBookQty()
 *
 **************************************************************************************/

var booksOnSale = 10;
var bookPrice = 4.99;
var ukPost = 1.5;
var overseasPost = 2.5;

var currPostage = 'uk';
var totalQty = 0;
var postage = 0;
var subTotal = 0;
var totalPrice = 0;
var discount = 0;
var discountValue = 0;

/****
 * Updates the book totals and adds the form options to the form
 ****/
function addBook(obj)
{
  setBookQty();
  changePostage(currPostage);
  
//  if( obj.id == 'book5' || obj.id == 'book6' || obj.id == 'book7')
//      subTotal = totalQty * 5.99;
//  else
//      subTotal = totalQty * bookPrice;

  if( discount != 0 ){
  	discountValue = subTotal * discount;
  	document.getElementById('Discount').innerHTML = "-" + formatCurrency(discountValue);
  	document.getElementById('DiscountTitle').innerHTML = "Discount";
  }
  
  totalPrice =  parseFloat((subTotal - discountValue) + postage);
  document.getElementById('TotalSubPrice').innerHTML = formatCurrency(subTotal)+'&nbsp;';
  document.getElementById('TotalPrice').innerHTML = formatCurrency(totalPrice)+'&nbsp;';
}

/****
 * Updates the postage value
 ****/
function changePostage(sVal)
{
  var total = 0;

  if(sVal == 'uk')
  {
    currPostage = 'uk';
    if( parseInt(totalQty) == 1 )
    {
      document.getElementById('PostageCost').innerHTML = formatCurrency(ukPost)+'&nbsp;';
      postage = ukPost;
    } else
    {
      document.getElementById('PostageCost').innerHTML = 'Free&nbsp;';  
      postage = 0;  
    }
  } else
  {
    currPostage = 'overseas';
    total = parseFloat(totalQty * overseasPost);
    document.getElementById('PostageCost').innerHTML = formatCurrency(total)+'&nbsp;'; 
    postage = total;     
  }

  totalPrice = parseFloat( (subTotal-discountValue) + postage);
  document.getElementById('TotalPrice').innerHTML = formatCurrency(totalPrice)+'&nbsp;';
}

/****
 * Finds out how many books in total have been selected
 ****/
function setBookQty()
{
  totalQty = 0;
  subTotal = 0;
  for( var i=1; i<=booksOnSale; i++)
  {
    var newQuantity = parseInt(document.getElementById('book'+i).value);
    if( newQuantity > 0)
    {
      totalQty = parseInt(totalQty) + newQuantity;
      if( i == 5 )
          subTotal = subTotal + (newQuantity * 5.99);
      else if( i > 5 )
          subTotal = subTotal + (newQuantity * 6);
      else
          subTotal = subTotal + (newQuantity * bookPrice);
    }
  }
}

/****
 * update the Paypal form
 ****/
function updatePayPalForm( )
{
  var node = null;
  var n = null;
  var pst = 0;
  var bolStatus = false;
  var itmNum = 1;
  
  bolStatus = confirm('You are about to be redirected through to PayPal to pay for your order.\n\nPlease confirm that your order details are correct.');
  if( bolStatus )
  {
    // Add in the currency code
//    n = document.createElement('input');
//    n.setAttribute('type', 'hidden');
//    n.setAttribute('name', 'currency_code');
//    n.setAttribute('value', 'GBP');
//    document.getElementById('buyform').appendChild(n);
    
    // Now add in the details for the chosen books
    for( i=1; i<=booksOnSale; i++)
    {
      node = document.getElementById('book'+i);
      if( parseInt( node.value ) > 0 )
      {
        if( parseInt(totalQty) == 1 && currPostage == 'uk' )
          pst = ukPost
        else if( currPostage != 'uk' )
          pst = parseInt( node.value ) * overseasPost;

        switch( node.id )
        {
          case 'book1':
            createFormElements(itmNum, 'The Energy Crisis', node.value, pst);
            break;
          case 'book2':
            createFormElements(itmNum, 'Global Warming', node.value, pst);
            break;
          case 'book3':
            createFormElements(itmNum, 'Middle East Conflict', node.value, pst);
            break;
          case 'book4':
            createFormElements(itmNum, 'Food', node.value, pst);
            break;
          case 'book5':
            createFormElements(itmNum, 'Pandemics', node.value, pst);
            break;
          case 'book6':
            createFormElements(itmNum, 'The Credit Crunch', node.value, pst);
            break;
          case 'book7':
            createFormElements(itmNum, 'Fat', node.value, pst);
            break;
          case 'book8':
            createFormElements(itmNum, 'Al Qaeda', node.value, pst);
            break;
          case 'book9':
            createFormElements(itmNum, 'Big Brother', node.value, pst);
            break;
          case 'book10':
            createFormElements(itmNum, 'Drink & Drugs', node.value, pst);
            break;
        }
        itmNum = parseInt(itmNum)+1;
      }
    }
  }  
}

function createFormElements(itemNum, bookTitle, qty, myPost)
{
//alert("itemNum: "+itemNum+"\nbookTitle: "+bookTitle+"\nqty: "+qty)
  var n = document.createElement('input');
  n.type = 'hidden';
  n.name = 'item_name_'+itemNum;
  n.value = bookTitle;
  document.getElementById('buyform').appendChild(n);
  n = null;
  
  n = document.createElement('input');
  n.type = 'hidden';
  n.name = 'amount_'+itemNum;
/*  if (bookTitle == 'Pandemics') {
  	if (discount)
  		n.value = 4.79;
  	else
  		n.value = 5.99;
  } else if (bookTitle == 'Money' || bookTitle == 'The Credit Crunch' || bookTitle == 'Al Qaeda' || bookTitle == 'Fat' || bookTitle == 'Big Brother' || bookTitle == 'Drink & Drugs') {
  	if (discount)
  		n.value = 4.80;
  	else
  		n.value = 6;
  } else {
  	if (discount)
  		n.value = 3.99
  	else
  		n.value = 4.99;
  }
  */
	var price = 0;
	if (bookTitle == 'Pandemics') {
		price = 5.99
	} else if (bookTitle == 'Money' || bookTitle == 'The Credit Crunch' || bookTitle == 'Al Qaeda' || bookTitle == 'Fat' || bookTitle == 'Big Brother' || bookTitle == 'Drink & Drugs') {
		price = 6;
	} else {
		price = 4.99;
	}
	if(discount != 0){
		price = price-(price*discount);
	}
	n.value = roundVal(price, 2);
	document.getElementById('buyform').appendChild(n);
  n = null;

  n = document.createElement('input');
  n.type = 'hidden';
  n.name = 'quantity_'+itemNum;
  n.value = qty;
  document.getElementById('buyform').appendChild(n);
  n = null;

  n = document.createElement('input');
  n.type = 'hidden';
  n.name = 'shipping_'+itemNum;
  n.value = myPost;
  document.getElementById('buyform').appendChild(n);
  n = null;
  
 }

 function applyDiscount(code) {
// 	if (code.toLowerCase() == 'soil' || code.toLowerCase() == 'prosp08' || code.toLowerCase() == 'merry08')
// 		discount = true;
    switch(code.toLowerCase()){
        case "soil":
            discount= 0.2;
            break;
        case "crunch09":
            discount = 0.25;
            break;
        default:
            discount= 0;
            break;
    }
 	addBook(null);
 }