// This set of functions will allow a license product 
// to be added to the cart as a FOC item for downloads
// or as a cost item if a media kit is requested. 
// The license item will hold the details of the customer within it.

var sMEDIA_PACK = 'LIC/PK';
var sDOWNLOAD_LICENSE = 'LIC/DL';
var sAGENT_PREFIX = "s";

function addLicense(bContainsDigital)
{
  showSiteBox(bContainsDigital);
}
  
function hideSiteBox() { 
  hideElement('licenseDetails');
  hideElement('siteBoxLayer');
}

function showSiteBox(bContainsDigital) { 
  if (document.getElementById('googlecart-widget-body'))
  {
    googlecartWidget.close();	  
  }
  if (bContainsDigital)
  {
    showElement('licenseDetails');
  }
  else
  {
    hideElement('licenseDetails');
  }
  showElement('siteBoxLayer');
  window.scrollTo(50,50);
  window.location.hash="licenseBoxTop"; 
}

function showElement(elementName)
{
  if (document.getElementById) { // DOM3 = IE5, NS6 
    document.getElementById(elementName).style.visibility = 'visible'; 
    document.getElementById(elementName).style.display = 'inline'; 
  } 
  else { 
    if (document.layers) { // Netscape 4 
      document.hideShow.visibility = elementName; 
    } 
    else { // IE 4 
      document.all.hideShow.style.visibility = elementName; 
    } 
  } 
}

function hideElement(elementName)
{
  if (document.getElementById) { // DOM3 = IE5, NS6 
    document.getElementById(elementName).style.visibility = 'hidden'; 
    document.getElementById(elementName).style.display = 'none'; 
  } 
  else { 
    if (document.layers) { // Netscape 4 
      document.hideShow.visibility = elementName; 
    } 
    else { // IE 4 
      document.all.hideShow.style.visibility = elementName; 
    } 
  } 
	
}

function googlecartOnCheckoutClick(items,form) 
{
  if ((items.length ==0))
  {
    window.alert('Your cart is empty!'); 	  
  }
  else
  {
  var bLicenseFound = false;
  var bContainsDigital = false;
  var bRemovedExtras = false;
  var sProductCode = '';
  var bBundledProduct = false;
  var bConfirmation = false;
  
  if (window.document.getElementById("ljConfirmation")) 
  {
    bConfirmation=(window.document.getElementById("ljConfirmation").value=='true');
  }
  if (items.length >0)
  {
    for (var i=0; i<items.length; i++)
    {
      if (items[i].isMarkedForRemoval()==false)
	  {
        sProductCode = items[i].getCustomAttribute('product-attr-code');
        items[i].setCustomAttribute('product-attr-agent', sAGENT_PREFIX+iLJAgentID);
		
		if ((sProductCode==sMEDIA_PACK) || (sProductCode==sDOWNLOAD_LICENSE))
		{
		  bLicenseFound = true;
		}

        if (items[i].isDigital()) 
		{
		  bContainsDigital = true;
		  if (items[i].getQuantity()>1)
		  {
			 items[i].setQuantity(1);
			 bRemovedExtras=true;
		  }
		}
		
        if (bIsBundledProduct(sProductCode))
		{
		  bBundledProduct = true;
		  
		  //Do not allow for more than one copy of downloads.
		  if (items[i].getQuantity()>1)
		  {
			 items[i].setQuantity(1);
			 bRemovedExtras=true;
		  }
		}

      }
	}
  }
  if (bRemovedExtras)
  {
    googlecart.saveCartAndRefreshWidget();
    window.alert(sMultipleOrder); 	  
  }
  
  // If there is no agreement, and the cart contains 
  // at least one digital download, then redirect the checkout to 
  // display the license entry box.
	  
  //if ((bLicenseFound==true) || (bBundledProduct==false))
  if ((bLicenseFound==true) || (bConfirmation==true))
  {
    return true;
  }
  else
  {
    addLicense(bBundledProduct);
  }
  }
}

function checkOffers()
{
  // SPECIAL OFFER SECTION - CUSTOMISE FOR NEW OFFERS...
  var sExciteOffer = 'Excite and Engage: 10% Discount Offer';
  var sTWitterOffer = 'Twitter: 25% Discount Offer';
  var sSSATOffer = 'SSAT: 10% Discount Offer';
  var sSSATALOffer = 'SSAT Applied Learning 2009: 10% Discount Offer';
  var sASEOffer = 'ASE East of England Area Conference: 25% Discount Offer';
  var sSIWROffer = 'Science Interactive Whiteboard Resources: 25% Discount Offer';
  var sLJUserOffer = 'LJ Website Users: 25% Discount Offer';
  var sRIAMTECOffer = 'RIAMTEC: 5% Discount Offer';
  var sBETTOffer = 'BETT 2010: 20% Discount Offer';
  var sOfferCode = ""+window.document.getElementById("ljOffer").value.toUpperCase();
  
  // Remove any existing offers from the cart.
  var oAllItems = googlecart.getItems();
  
  if (oAllItems.length >0)
  {
	for (var i=0; i<oAllItems.length; i++)
	{
		if (oAllItems[i].getCustomAttribute('type') == 'offer')
		{
		  oAllItems[i].markForRemoval();
		}
	}
	googlecart.expungeMarkedForRemoval();
  }
 
  var nTotalCost = googlecart.getSubtotal();

if ((sOfferCode=="WEB22") || (sOfferCode=="TES22") || (sOfferCode=="TWEET22"))
  {
	  if (nTotalCost > 199.99)
	  {
		var iOfferPrice = (nTotalCost * -0.1)
		var oOfferItem = googlecart.makeItem({'title':sExciteOffer,'price': iOfferPrice},
											 {'type':'offer','code':sOfferCode}, 1);
		googlecart.addItem(oOfferItem);
	  }
	  else
	  {
		  window.alert("The offer code '"+sOfferCode+"' is only valid if you send over "+sDisplayedCurrency+"200.00");
		  return false;
	  }
  }
else if ((sOfferCode=="SSATSC09") || (sOfferCode=="SSATSCO9"))
  {
	var iOfferPrice = (nTotalCost * -0.1)
	var oOfferItem = googlecart.makeItem({'title':sSSATOffer,'price': iOfferPrice},
											 {'type':'offer','code':sOfferCode}, 1);
	googlecart.addItem(oOfferItem);
  }
else if ((sOfferCode=="ASENOR") || (sOfferCode=="ASEN0R"))
  {
	var iOfferPrice = (nTotalCost * -0.25)
	var oOfferItem = googlecart.makeItem({'title':sASEOffer,'price': iOfferPrice},
											 {'type':'offer','code':sOfferCode}, 1);
	googlecart.addItem(oOfferItem);
  }
else if (sOfferCode=="TWIT231")
  {
	var iOfferPrice = (nTotalCost * -0.25)
	var oOfferItem = googlecart.makeItem({'title':sTWitterOffer,'price': iOfferPrice},
											 {'type':'offer','code':sOfferCode}, 1);
	googlecart.addItem(oOfferItem);
  }
else if ((sOfferCode=="DTB10") || (sOfferCode=="DTB1O"))
  {
	var iOfferPrice = (nTotalCost * -0.1)
	var oOfferItem = googlecart.makeItem({'title':sSSATALOffer,'price': iOfferPrice},
											 {'type':'offer','code':sOfferCode}, 1);
	googlecart.addItem(oOfferItem);
  }
else if (sOfferCode.length >0)
  {
	  window.alert("The offer code '"+sOfferCode+"' is not valid!");
	  return false;
  }
  return true;
}

function bIsBundledProduct(sProductCode)
{
	var sShortCode = (sProductCode.slice(0,4).toUpperCase());
	return ((sShortCode=="WA20") || (sShortCode=="WA80") || (sShortCode=="WP20") || (sShortCode=="WP80"))

}

function sendCart(button)
{
  if (checkOffers())
  {
      window.document.getElementById("ljConfirmation").value="true";
	
	  googlecart.expungeMarkedForRemoval();
	  var oNewItem = googlecart.makeItemFromNode(window.document.getElementById("license"));
	  var sNewSiteName = ""+window.document.getElementById("siteName").value;
	  var sNewSiteAddress = ""+window.document.getElementById("siteAddress").value;
	  var sNewTax = ""+window.document.getElementById("ljTax").value;
	  
	  
	  document.getElementById('googlecart-checkout-config').innerHTML += document.getElementById('applyTaxRules').innerHTML;
	  //If a US tax exemption number has not been applied, then add the tax rules to the submitted form.
	  if (window.document.getElementById('chkTaxExempt'))
	  {
		  if (window.document.getElementById('chkTaxExempt').checked==false)
		  {
			  document.getElementById('googlecart-checkout-config').innerHTML += document.getElementById('USTaxRules').innerHTML;
		  }
	  }
	  oNewItem.setTitle(sLJLicenseProduct+' '+sLJMediaPack);
	  oNewItem.setCustomAttribute('product-attr-code', sMEDIA_PACK);
	  oNewItem.setCustomAttribute('product-attr-agent', sAGENT_PREFIX+iLJAgentID);
	  oNewItem.setDigital(false);
	  oNewItem.setPricePerUnit(rLJMediaPrice);
	  oNewItem.setCustomAttribute('product-attr-sitename', sNewSiteName);
	  oNewItem.setCustomAttribute('product-attr-siteAddress', sNewSiteAddress);
	  oNewItem.setCustomAttribute('product-attr-tax', sNewTax);
	  oNewItem.setQuantity(1);
	
	  googlecart.addItem(oNewItem);	
	  googlecart.saveCartAndRefreshWidget();
	  hideSiteBox();
	  googlecart.checkout();
  }

}

function googlecartWidgetLoaded() 
{
  var items = googlecart.getItems();
  var nTotalCost = googlecart.getSubtotal();
  
  if (items.length >0)
  {
	for (var i=0; i<items.length; i++)
	{
        sProductCode = items[i].getCustomAttribute('product-attr-code');
        items[i].setCustomAttribute('product-attr-agent', sAGENT_PREFIX+iLJAgentID);
		
		if ((sProductCode==sMEDIA_PACK) || (sProductCode==sDOWNLOAD_LICENSE))
		{
		  window.document.getElementById("siteName").value = 
		          items[i].getCustomAttribute('product-attr-sitename');
		  window.document.getElementById("siteAddress").value =	
		          items[i].getCustomAttribute('product-attr-siteAddress');
		  window.document.getElementById("ljTax").value =	
		          items[i].getCustomAttribute('product-attr-tax');
		  items[i].markForRemoval();
		  
		  if (window.document.getElementById('chkTaxExempt'))
	      {
			  if (window.document.getElementById('ljTax').value=='US Tax Exempt')
			  {
				window.document.getElementById('chkTaxExempt').checked=true;
			  }
			  else
			  {
				window.document.getElementById('chkTaxExempt').checked=false;
			  }
		  }
		}
		else if (items[i].getCustomAttribute('type') == 'offer')
		{
		  window.document.getElementById("ljOffer").value =	items[i].getCustomAttribute('code');
		}
		
	}
	googlecart.expungeMarkedForRemoval();
  }
  window.document.getElementById("ljConfirmation").value=='';
}

function googlecartAfterAdd(item, index)
{
  //if ((item.getQuantity()>0) || (item.isDigital()))
  //{
  //  item.setQuantity(0);
  //  googlecart.saveCartAndRefreshWidget();
	//window.alert(sMultipleOrder); 	  
	//return false
  //}
}

