//========================================================================================
//classIFNbasket - (C)InforNet 1996-2004 (except where documented) - v2004021901
//========================================================================================
var _tempData = "";
var timerId = "";
function setTempData(data)	{	_tempData = data;		}
function getTempData() 	{	return "" + _tempData;	}	//Devuelve una copia para evitar problemas de sync
//-------------------------------------------------------------------------------------
			function getCookieVal (offset) {
				//Written by:  Bill Dortch, hIdaho Design &lt;bdortch@hidaho.com&gt;.This function is released to the public domain.
			 var endstr = document.cookie.indexOf (";", offset);
			  if (endstr == -1)
			    endstr = document.cookie.length;
			  return unescape(document.cookie.substring(offset, endstr));
			}
			//_________________________________________________________________________________________
			function FixCookieDate (date) {
				//Written by:  Bill Dortch, hIdaho Design &lt;bdortch@hidaho.com&gt;.This function is released to the public domain.
			  var base = new Date(0);
			  var skew = base.getTime(); 
			  if (skew > 0)  
			    date.setTime (date.getTime() - skew);
			}
			//_________________________________________________________________________________________
			function GetCookie (name) {
				//Written by:  Bill Dortch, hIdaho Design &lt;bdortch@hidaho.com&gt;.This function is released to the public domain.
			  var arg = name + "=";
			  var alen = arg.length;
			  var clen = document.cookie.length;
			  var i = 0;
			  while (i < clen) {
			    var j = i + alen;
			    if (document.cookie.substring(i, j) == arg){
			      return getCookieVal (j);
				}
			    i = document.cookie.indexOf(" ", i) + 1;
			    if (i == 0) break; 
			  }
			  return null;
			}
			//_________________________________________________________________________________________
			function SetCookie (name,value,expires,path,domain,secure) {
				//Written by:  Bill Dortch, hIdaho Design &lt;bdortch@hidaho.com&gt;.This function is released to the public domain.
			  document.cookie = name + "=" + escape (value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) 	? "; 		secure" : "");
			}
			//_________________________________________________________________________________________
			function DeleteCookie (name,path,domain) {
				//Written by:  Bill Dortch, hIdaho Design &lt;bdortch@hidaho.com&gt;.This function is released to the public domain.
			  if (GetCookie(name)) {
			    document.cookie = name + "=" + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
			  }
			}	
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
function IFNdictionary() {
	this.obj = new Object();
	this.Count = 0;
	
	function IFNdictionary_set(key, value) {
		if (this.obj[key] != null) {
			 this.obj[key] = value;		//update		
		} else {
			this.obj[key] = value;		//insert
			this.Count++;
		}
		return this.value;
	}
	function IFNdictionary_add(key, value) {
		if (this.obj[key] != null) return null;
		this.obj[key] = value;
		this.Count++;
		return this.value;
	}
	
	function IFNdictionary_exists(key) {
		return (this.obj[key] != null);
	}
	
	function IFNdictionary_item(key) {
		return this.obj[key];
	}
	
	function IFNdictionary_remove(key) {
		if (this.obj[key] == null) return null;
		this.obj[key] = null;
		this.Count--;
		return this.key;
	}
	
	this.set = IFNdictionary_set;
	this.add = IFNdictionary_add;
	this.exists = IFNdictionary_exists;
	this.item = IFNdictionary_item;
	this.remove = IFNdictionary_remove;
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
function classIFNbasket_onQtyChange (thisFormField, oldQty, newQty, key, price, basketType) {
			//alert("oldQty: [" + oldQty + "] newQty: [" + newQty + "]");

	var action = "document.getElementById('IFNbasketRow_' + '" + key + "').style.display = 'none'";
			
	if (newQty != oldQty) {	
		if (isNaN(parseInt(newQty))) {
			thisFormField.value = oldQty;
			thisFormField.focus();
		} else {
			clearTimeout(this.timers.item(action));
			
			newQty = parseInt(newQty);
			thisFormField.value = newQty;

			this.PRIVchangeCookie(thisFormField, newQty, key, price, basketType); 
			try {
				if (basketType == 'basket') this.PRIVcompute(thisFormField, newQty, key, price, basketType);	//solo en Basket
			} catch(x) {}
			
			if (newQty > 0) {
				if (document.getElementById("IcnCtl_" + key))
					document.images["IcnCtl_" + key].src = this.getIconFullBasket();
				if (document.getElementById("IFNbasketRow_" + key))
					document.getElementById("IFNbasketRow_" + key).style.backgroundColor = "";
			} else {
				if (document.getElementById("IcnCtl_" + key))
					document.images["IcnCtl_" + key].src = this.getIconEmptyBasket();
				if (document.getElementById("IFNbasketRow_" + key)) {
					document.getElementById("IFNbasketRow_" + key).style.backgroundColor = "#dddddd";
					
					this.timers.set(action, setTimeout(action, 1000));
				} else if (basketType == "basket") {
					location = window.location.pathname;	//window.location.reload(true);*/
				}						
			}
		}
	}
}
//-------------------------------------------------------------------------------------
function classIFNbasket_incrementQty(thisFormField, qty, key, price, basketType) {
	try {
		var newQty = parseInt(qty) + 1;
		this.onQtyChange(thisFormField, qty, newQty, key, price, basketType);
	} catch (x) {}
}
function classIFNbasket_decrementQty(thisFormField, qty, key, price, basketType) {
	try {
		if (qty > 0) {
			var newQty = parseInt(qty) - 1;
			this.onQtyChange(thisFormField, qty, newQty, key, price, basketType);
		} else {
			this.nullQty(thisFormField, qty, newQty, key, price, basketType);
		}
	} catch (x) {}
}
function classIFNbasket_nullQty(thisFormField, qty, key, price, basketType) {
	try {
		var newQty = 0;
		this.onQtyChange(thisFormField, qty, newQty, key, price, basketType);
	} catch (x) {}
}
function classIFNbasket_toggleQty(thisFormField, qty, key, price, basketType) {
	try {
		var newQty = (parseInt(qty) <= 0) ? 1 : 0 ;
		this.onQtyChange(thisFormField, qty, newQty, key, price, basketType);
	} catch (x) {}
}
//-------------------------------------------------------------------------------------
function classIFNbasket_isQtyToogled (oldQty, newQty) {
	try {
		if ( ((oldQty <= 0) && (newQty > 0)) || ((oldQty > 0) && (newQty <= 0)) )
			return true;
		else 
			return false;
	} catch(x) {
		return false;
	}
}
//-------------------------------------------------------------------------------------
function classIFNbasket_changeCookie2004 (qty, key, price, basketType) {
	this.PRIVchangeCookie (null, qty, key, price, basketType);
}
//-------------------------------------------------------------------------------------
function classIFNbasket_changeCookie (thisFormField, qty, key, price, basketType) {

	var basketCookie = GetCookie(this.getBasketName());
	var modifier = "1";
	
	if (qty <= 0){ 
		basketCookie = this.PRIVremoveCookieElement(basketCookie, key);
	} else{
		if (basketCookie == null || basketCookie.indexOf(this.FIELD_DELIMITER_TOKEN + key + this.FIELD_DELIMITER_TOKEN + modifier + this.REGISTER_DELIMITER_TOKEN) == -1){
			basketCookie = this.PRIVaddCookieElement(basketCookie, qty, key, modifier);
		} else{		
			basketCookie = this.PRIVmodifyCookieElement(basketCookie, qty, key, modifier);
		}
	}
	SetCookie(this.getBasketName(), basketCookie, this.getBasketExpdate(), "/");
}
//-------------------------------------------------------------------------------------
function classIFNbasket_deleteCookie () {
	DeleteCookie(this.getBasketName(), "/");
}
//-------------------------------------------------------------------------------------
function classIFNbasket_addCookieElement(basketCookie, qty, key, modifier){

	if (qty == "") qty = 1;
	if (basketCookie == null)	basketCookie = "";
	basketCookie += qty + this.FIELD_DELIMITER_TOKEN + key + this.FIELD_DELIMITER_TOKEN + modifier + this.REGISTER_DELIMITER_TOKEN;
	return basketCookie;
}
//-------------------------------------------------------------------------------------
function classIFNbasket_modifyCookieElement(basketCookie, qty, key, modifier){

	var newBasketCookie = "";
	var campo = "";
	var primerSeparador = -1;
	var segundoSeparador = -1;
	for (var i = 0; i < basketCookie.length; i++) {
		if (basketCookie.charAt(i) == this.REGISTER_DELIMITER_TOKEN){
			if (basketCookie.substring(primerSeparador + 1, segundoSeparador) == key){
				campo = qty + campo.substring(campo.indexOf(this.FIELD_DELIMITER_TOKEN));
			}	
			campo += basketCookie.charAt(i);
			newBasketCookie += campo;
			primerSeparador = -1;
			campo = "";
			continue;
		} else if (basketCookie.charAt(i) == this.FIELD_DELIMITER_TOKEN){
			if (primerSeparador == -1){
				primerSeparador = i;
			} else{
				segundoSeparador = i;
			}	
	 	}
 		campo += basketCookie.charAt(i);
	}
	return newBasketCookie;
}
//-------------------------------------------------------------------------------------
function classIFNbasket_removeCookieElement(basketCookie, key){

	var newBasketCookie = "";
	var campo = "";
	var primerSeparador = -1;
	var segundoSeparador = -1;
	for (var i = 0; i < basketCookie.length; i++) {
		if (basketCookie.charAt(i) == this.REGISTER_DELIMITER_TOKEN){
			if (basketCookie.substring(primerSeparador + 1, segundoSeparador) != key){
				campo += basketCookie.charAt(i);
				newBasketCookie += campo;
				primerSeparador = -1;
			}	
			campo = "";
			continue;
		} else if (basketCookie.charAt(i) == this.FIELD_DELIMITER_TOKEN){
			if (primerSeparador == -1){
				primerSeparador = i;
			} else{
				segundoSeparador = i;
			}
		 }
		campo += basketCookie.charAt(i);
	}
	return newBasketCookie;
}
//-------------------------------------------------------------------------------------
function classIFNbasket_compute (thisFormField, qty, key, price, basketType) {

	var total = 0;
	var thisForm = thisFormField.form;
	
	thisForm["SubtotalCompute_" + key].value = (qty * price).toFixed(2);
	thisForm["Subtotal_" + key].value = thisForm["SubtotalCompute_" + key].value;

	for (var i=0; i<thisForm.elements.length; i++) {
		if (thisForm.elements[i].name.indexOf("SubtotalCompute_") != -1)
			total += parseFloat(thisForm.elements[i].value);	
	}

	thisForm.SubtotalSumCompute.value = total.toFixed(2)

	try {
		IFNbasket_EXTERN_refreshTotals();
	} catch(x) {}
}
//_________________________________________________________________________________________
function classIFNbasket_setBasketName(value)	 {	this._basketName = value;	}
function classIFNbasket_getBasketName()		 {	return this._basketName;	}
//_________________________________________________________________________________________
function classIFNbasket_setBasketExpdate(value)	{	this._basketExpdate = value;	}
function classIFNbasket_getBasketExpdate()			{	return this._basketExpdate;	}
//_________________________________________________________________________________________
function classIFNbasket_setIconEmptyBasket(value)	{	this._iconEmptyBasket = value;	}
function classIFNbasket_getIconEmptyBasket()		{	return this._iconEmptyBasket;	}
//_________________________________________________________________________________________
function classIFNbasket_setIconFullBasket(value)	{	this._iconFullBasket = value;	}
function classIFNbasket_getIconFullBasket()		{	return this._iconFullBasket;	}
//_________________________________________________________________________________________
function classIFNbasket () {						

	this.incrementQty 	= classIFNbasket_incrementQty;
	this.decrementQty 	= classIFNbasket_decrementQty;
	this.nullQty 		= classIFNbasket_nullQty;
	this.toggleQty 		= classIFNbasket_toggleQty;
	this.onQtyChange 	= classIFNbasket_onQtyChange;

	this.PRIVcompute 				= classIFNbasket_compute;
	this.PRIVchangeCookie			= classIFNbasket_changeCookie;
	this.PRIVdeleteCookie			= classIFNbasket_deleteCookie;
	this.PRIVaddCookieElement		= classIFNbasket_addCookieElement;
	this.PRIVmodifyCookieElement	= classIFNbasket_modifyCookieElement;
	this.PRIVremoveCookieElement	= classIFNbasket_removeCookieElement;

	this.isQtyToogled 					= classIFNbasket_isQtyToogled;

	this.changeCookie2004			= classIFNbasket_changeCookie2004;


	this._basketName 	= "IFNShop";
	this.setBasketName = classIFNbasket_setBasketName;
	this.getBasketName = classIFNbasket_getBasketName;

	this._basketExpdate = new Date();
	this._basketExpdate.setTime(this._basketExpdate.getTime() + (1 * 60 * 60 * 1000));
	this.setBasketExpdate = classIFNbasket_setBasketExpdate;
	this.getBasketExpdate = classIFNbasket_getBasketExpdate;
	
	this._iconEmptyBasket 	= "emptyBasket.gif";
	this.setIconEmptyBasket = classIFNbasket_setIconEmptyBasket;
	this.getIconEmptyBasket = classIFNbasket_getIconEmptyBasket;

	this._iconFullBasket 	= "fullBasket.gif";
	this.setIconFullBasket = classIFNbasket_setIconFullBasket;
	this.getIconFullBasket = classIFNbasket_getIconFullBasket;

	this.REGISTER_DELIMITER_TOKEN = "^";
	this.FIELD_DELIMITER_TOKEN = "}";
	
	this.timers = new IFNdictionary();
	
	//to do	
	this.SRC_BUTTON_ADDED 	="images/ficha/boton_anadir_f2.gif";
	this.ALT_BUTTON_ADDED 	= "Eliminar de la cesta de la compra";
	this.SRC_BUTTON_ADD 	= "images/ficha/boton_anadir.gif";
	this.ALT_BUTTON_ADD 	= "Aņadir a la cesta de la compra";
	//to do

	return this;
}
//_________________________________________________________________________________________

	var IFNbasket = new classIFNbasket();