/**
 * Copyright 2006 Olivier Clavel
 *
 * This file is part of Renata Shop Website software.
 *
 * Renata Shop Website is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License,
 * or any later version.
 *
 * Renata Shop Website is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MediaNetLive portal software; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @author Olivier Clavel <contact AT retiz DOT com>
 * @version $Id$
 */


/**
 * Sets the onload function for document
 */
window.onload = function () {
  var lang = document.body.className;
  var listUrl = '/' + lang + '/cartajax/cart/list';
  var ajax = new GLM.AJAX();
  ajax.callPage(listUrl, refreshCart);
}

/**
 * Safely creates a named element that will still work in IE also
 *
 * @param string tagName The tagname of the element
 * @param string name The name of the element
 */
function createNamedElement(tagName, name) {
   var element = null;
   // Try the IE way; this fails on standards-compliant browsers
   try {
      element = document.createElement('<'+tagName+' name="'+name+'">');
   } catch (e) {
   }
   if (!element || element.nodeName != tagName.toUpperCase()) {
      // Non-IE browser; use canonical method to create named element
      element = document.createElement(tagName);
      element.name = name;
   }
   return element;
}

/**
 * Creates a named hidden field with a value
 *
 * @param string name
 * @param string value
 */
function createHiddenField (name, value) {
  elem = createNamedElement('input', name);
  elem.type = 'hidden';
  elem.value = value;
  return elem;
}

/**
 * Called when user clicks on paypal buy button
 *
 */
function processPaypalCart () {
  var lang = document.body.className;
  var listUrl = '/' + lang + '/cartajax/cart/jsonlist';
  var ajax = new GLM.AJAX();
  var listUrl = '/' + lang + '/cartajax/cart/jsonlist';
  ajax.callPage(listUrl, sendPaypalCart);
}

/**
 * Ajax callback to send cart
 */
function sendPaypalCart (content) {
  // Get document language
  var lang = document.body.className;

  // Get a reference to the paypal form
  var paypalForm = document.getElementById('paypalPay');

  // This is where we configure test or production setting to send the form
  var paypalSubmitUrl = {
    'test' : 'https://www.sandbox.paypal.com/'+lang+'/cgi-bin/webscr',
    'prod' : 'https://www.paypal.com/'+lang+'/cgi-bin/webscr'
  }

  // Labels depending on language
  var labels = {"fr": {"modcol" : "Modèle / Couleur", "size" : "Taille"}, "en" : {"modcol" : "Model / Color", "size" : "Size"}};

  // Read data in from the json encoded text
  var data = eval('(' + content + ')');

  for (var i=1; i<=data.length; i++) {
    var item = data[i-1];
    paypalForm.appendChild(createHiddenField('item_name_'+i,item.clotheDescription));
    paypalForm.appendChild(createHiddenField('quantity_'+i, item.quantity));
    //paypalForm.appendChild(createHiddenField('item_number_'+i, item.color+item.clotheId+item.model+item.size));
    paypalForm.appendChild(createHiddenField('amount_'+i, item.price));
    paypalForm.appendChild(createHiddenField('on0_'+i, labels[lang].modcol));
    paypalForm.appendChild(createHiddenField('os0_'+i, item.model + ' / ' + item.color));
    paypalForm.appendChild(createHiddenField('on1_'+i, labels[lang].size));
    paypalForm.appendChild(createHiddenField('os1_'+i, item.size));
  }

  paypalForm.action = paypalSubmitUrl[paypalForm.className]
  paypalForm.submit();
}

/**
 * Enables/Disables the paypal button depending
 * terms and conditions checkbox state
 *
 */
function payButtonState () {
  var lang = document.body.className;
  var form = document.getElementById('paypalPay');
  var button = document.getElementById('payButton');
  var image = button.getElementsByTagName('img').item(0);
  var state, imageSrc;
  button.normalize();
  if (form.acceptCGV.checked && form.acceptCGV.value == 1) {
    state = false;
    imageSrc = "/images/paypalPayer_" + lang + ".gif";
  } else {
    state = true;
    imageSrc =  "/images/paypalPayerDisabled_" + lang + ".gif";
  }
  button.disabled = state;
  image.src = imageSrc;
}
