///Original Code Created Posted In The Ecwid Forum By Eugene

//Adapted by Andrew Leonard of EcwidMadeEasy.com to show the terms and conditions checkbox in the SHopping Cart

// On line 48 below YOU MUST REPLACE ************** with the url that contains your terms and conditions.


// Bind a change handler to the window location.
$j( window.location ).bind(
  "change",
  function(objEvent, objData){
    var page = {};
    hash = objData.currentHash.slice(6);  //trim the ecwid and the COLON leading.
    page.mode = getPageType(hash);
    page.category = getCategory(hash);
    page.product = getProduct(hash);
    runMods(page);  //If the window's changed, run the mods.
  }
);


function runMods(page) {
  //Call anything you want on every page here.
  functionComplete = false;  //They all retrun true if successful. false if not.
  if (window.t1 !== undefined) { clearInterval(t1); }

  t1 = setInterval (function() {
    switch(page.mode) {
//     case "checkoutPO":                                //Original code for it to work after the checkout button is selected (eg if phone ordering has been selected)
      case "cart":                                      // Change made by EcwidMadeEasy.com for it to work in Shopping Cart - decides which page it is displayed on
        functionComplete = process_placeorder_button();
        break;
      default:
        //Nothing to do!
        functionComplete = true;
    }
    if (functionComplete) { clearInterval(t1); }
  }, 100);
}


function process_placeorder_button() {
  //var button = $j('div.ecwid-Checkout-placeOrderButton');          //Original code for it to work after the checkout button is selected (eg if phone ordering has been selected)
  var button = $j('div.ecwid-productBrowser-cart-checkoutButton');  // Change made by EcwidMadeEasy.com for it to work in Shopping Cart
  var terms_checkbox = $j('input#ecwid_terms_checkbox');

  if (button.length) {
    if (!terms_checkbox.length) {
      button.closest('table').after(content_for_terms_checkbox());
      return false;
    }
    if ($j('#ecwid_terms_checkbox').attr('checked')) {
      button.closest('table').show();
      return true;
    } else {
      button.closest('table').hide();
      return true;
    }
  } else {
    return false;
  }
  return false;
}


function ecwid_checkbox() {
  //var button = $j('div.ecwid-Checkout-placeOrderButton');          //Original code for it to work after the checkout button is selected (eg phone ordering has been selected)
  var button = $j('div.ecwid-productBrowser-cart-checkoutButton');  // Change made by EcwidMadeEasy.com for it to work in Shopping Cart
  if (button.length) {
    if ($j('#ecwid_terms_checkbox').attr('checked')) {
      show_terms_page();
      button.closest('table').show();
    } else {
      button.closest('table').hide();
    }
  } else {
    return false;
  }
  return true;
}


function show_terms_page() {
  window.open(
    'http://www.leadingresources.com/license-agreement',
    'license_agreement',
    'width=666,height=666,scrollbars=yes,status=no,toolbar=no,location=no'
  ).focus();
}


// Overwrite this function for a custom checkbox/label/message
function content_for_terms_checkbox() {
  return '<p style="width:168px;"><input type="checkbox" id="ecwid_terms_checkbox" onclick="ecwid_checkbox();"/> <span>I agree to the terms &amp; conditions of the <a href="javascript:show_terms_page()">End User License Agreement</a>.</span></p><p style="font-size:80%;font-style:italic">You may checkout once you have agreed to these terms.</p>'
}


$j(document).ready(function() {
  t2 = setInterval (function() {
    process_placeorder_button();
  }, 50);
});

