/*===============*/
/* warranties.js */
/*-------------------------------------------------------------------------------------------*/
/* JavaScript functions for handling adding warranties - along with products - to the basket */
/*===========================================================================================*/

function QueryString() {
  var oQuery = new Object();
  var sSearch = document.location.search.substring(1);
  if (sSearch.length > 0) {
    var asKeyValues = sSearch.split('&');
    var asKeyValue = '';
    for (var i = 0; i < asKeyValues.length; i++) {
      asKeyValue = asKeyValues[i].split('=');
      oQuery[asKeyValue[0]] = asKeyValue[1];
    }
 } return oQuery;
}

var myQueryStuff = new QueryString();


/*===================================*/
/* AddWarrantyToHrefToSubmitToBasket */
/*---------------------------------------------------------------------------------------------*/
/* Change the 'Buy' button LINK for a product to include/not a warranty along with the product */
/* This is run from the selected guarantee check-box when it's clicked.                        */
/*=============================================================================================*/
function AddWarrantyToHrefToSubmitToBasket(GuaranteeId, AlwaysChecked, theObject, thePage, thePageWithWarranty)
{
  /*------------------------*/
  /* Set default parameters */
  /*------------------------*/
  if (theObject!=null) objtheObject = theObject;
  else objtheObject = document.getElementById('cmdaddtobasket');
  if (objtheObject==null) objtheObject = cmdaddtobasket;
  if (objtheObject==null) objtheObject = document.links['cmdaddtobasket'];

  if (thePage!=null) strthePage = thePage;
  else strthePage = 'addtobasket';

  if (thePageWithWarranty!=null) strthePageWithWarranty = thePageWithWarranty;
  else strthePageWithWarranty = 'addtobasketwithwarranty';

  /*------------------------------------------------------------------------*/
  /* Ensure that a click on a checkbox always checks, and never unchecks it */
  /*------------------------------------------------------------------------*/
  if (AlwaysChecked==true) { document.frmGuarantees[GuaranteeId].checked=true; }

  /*----------------------------------------------*/
  /* Uncheck all the other checkboxes in the form */
  /*----------------------------------------------*/
  for (i=0;i<document.frmGuarantees.length;i++)
  {
    if (document.frmGuarantees[i].type=="checkbox"&&document.frmGuarantees[i].name!=GuaranteeId)
    { document.frmGuarantees[i].checked=false; }
  }

  /*--------------------------------------------------------*/
  /* Change the Buy button to include the selected warranty */
  /*--------------------------------------------------------*/
  if (GuaranteeId!='stdman'&&GuaranteeId!=''&&document.frmGuarantees[GuaranteeId].checked==true) {
    objtheObject.href=strthePageWithWarranty+"/"+myQueryStuff['pid']+"/"+GuaranteeId;
    }
  else {
    objtheObject.href=strthePage+"/"+myQueryStuff['pid'];
    }
}


/*=========================================*/
/* AddWarrantyToFormActionToSubmitToBasket */
/*-------------------------------------------------------------*/
/* Add the warranty to the basket via a FORM instead of a link */
/*=============================================================*/
function AddWarrantyToFormActionToSubmitToBasket(GuaranteeId, AlwaysChecked, theObject, thePage, thePageWithWarranty)
{
  /*------------------------*/
  /* Set default parameters */
  /*------------------------*/
  if (theObject!=null) objtheObject = theObject;
  else objtheObject = document.getElementById('cmdaddtobasket');
  if (objtheObject==null) objtheObject = cmdaddtobasket;
  if (objtheObject==null) objtheObject = document.forms['cmdaddtobasket'];

  if (thePage!=null) strthePage = thePage;
  else strthePage = 'basket.php?cda=addtobasket';

  if (thePageWithWarranty!=null) strthePageWithWarranty = thePageWithWarranty;
  else strthePageWithWarranty = 'basket.php?cda=addtobasket';

  /*-----------------------------------------------------*/
  /* Force the checkbox to be always checked if required */
  /*-----------------------------------------------------*/
  if (AlwaysChecked==true) document.frmGuarantees[GuaranteeId].checked=true;

  /*----------------------------------------------*/
  /* Uncheck all the other checkboxes in the form */
  /*----------------------------------------------*/
  for (i=0;i<document.frmGuarantees.length;i++) {
    if (document.frmGuarantees[i].type=="checkbox"&&document.frmGuarantees[i].name!=GuaranteeId)
      { document.frmGuarantees[i].checked=false; }
    }

  /*--------------------------------------------------------*/
  /* Change the Buy button to include the selected warranty */
  /*--------------------------------------------------------*/
  if (GuaranteeId!='stdman'&&GuaranteeId!=''&&document.frmGuarantees[GuaranteeId].checked==true) {
    objtheObject.action=strthePageWithWarranty+"&pid="+myQueryStuff['pid']+"&warrantyid="+GuaranteeId;
    }
  else {
    objtheObject.action=strthePage+"&pid="+myQueryStuff['pid'];
    }
}
