/*
	Tecknosfera DBNET S.L.
	Element: BECARA CATALOGO library
	Programed by: francisco_javier_martinez AT hotmail DOT com
	To : Tecknosfera DBNET S.L.
*/


// "Clase BECARA CATALOGO"
function BECARA_CATALOGO_object ()
{
	var ambientsPreview = Array();
	var imageToPreview = null;
	//
	this.show_ambient_preview = function (objReferer, theStatus)
	{
		var WIDTH_IMAGE_PREVIEW = 119;
		var HEIGHT_IMAGE_PREVIEW = 153;
	
		var urlPreview = null;
		var ambientID = null;
		if (objReferer && objReferer.nodeName.toLowerCase() == "a")
		{
			if (objReferer.firstChild.nodeName.toLowerCase() == "img")
			{
				imageToPreview = objReferer.firstChild;
				ambientsPreview[ambientsPreview.length] = imageToPreview;
			}
		}
		
		if (imageToPreview)
		{
			var actualURL = imageToPreview.src.split("/")[imageToPreview.src.split("/").length - 1];
			actualURL = imageToPreview.src.split(actualURL)[0];
			
			var actualImageID = imageToPreview.src.split("/")[imageToPreview.src.split("/").length - 1].split("_")[0];
			ambientID = actualImageID;
			
			var actualImageExtension = imageToPreview.src.split(".")[imageToPreview.src.split(".").length - 1];
			
			var actualTDambient = document.getElementById("td_ambient_" + ambientID);
			
			switch (theStatus)
			{
				case "over":
					desactiveAll();
					
					urlPreview = actualURL + "/" + actualImageID + "_medium." + actualImageExtension;
					
					var divPreview = document.createElement("div");
					divPreview.style.position = "absolute";
					divPreview.style.top = LIB_navigator.findTopObj(actualTDambient) + "px";
					divPreview.style.left = LIB_navigator.findLeftObj(actualTDambient) + "px";
					divPreview.style.width = WIDTH_IMAGE_PREVIEW + "px";
					divPreview.style.height = HEIGHT_IMAGE_PREVIEW + "px";
					divPreview.style.cursor = "pointer";
					divPreview.style.zIndex = 10;
					divPreview.id = "divPreview_" + ambientID;
					
					var imageAhref = document.createElement("a");
					
					var categoryCriteria = "";
					if (objReferer.href.indexOf("&category=") != -1)
					{
						categoryCriteria = "&category=" + objReferer.href.split("&category=")[1].split("')")[0];
					}
					imageAhref.href = "javascript:LIB_PROTOS.go_to(\"ambient_view.php?id=" + ambientID + categoryCriteria + "\");";
/*					LIB_events.addDynamicEvent (imageAhref, "onclick", function ()
																			{
																				LIB_PROTOS.go_to("ambient_view.php?id=" + ambientID, imageAhref);
																			});
*/
					var imagePreview = document.createElement("img");
					imagePreview.style.width = WIDTH_IMAGE_PREVIEW;
					imagePreview.style.height = HEIGHT_IMAGE_PREVIEW;
					imagePreview.border = "0";
					imagePreview.src = urlPreview;
					imagePreview.onload = function ()
					{
						LIB_events.addDynamicEvent (imagePreview, "mouseout", function ()
																				{
																					BECARA_CATALOGO.show_ambient_preview(objReferer, "out")
																				});
						imageAhref.appendChild(imagePreview);
						divPreview.appendChild(imageAhref);
					}
					
					actualTDambient.appendChild(divPreview);
				break;
				
				case "out":
					desactiveAll();
					var divPreview = document.getElementById("divPreview_" + ambientID);
					if (divPreview)
					{
						document.getElementById("td_ambient_" + ambientID).removeChild (divPreview);
					}
				break;
			}
		}
	}
	
	function desactiveAll ()
	{
		// Desactivamos todos los elementos del array
		for (contAmbients=0;contAmbients<ambientsPreview.length;contAmbients++)
		{
			var previewedAmbientId = ambientsPreview[contAmbients].src.split("/")[ambientsPreview[contAmbients].src.split("/").length - 1].split("_")[0];
			// Si existe al imagen de previsualización... la eliminamos
			var actualDivPreview = document.getElementById("divPreview_" + previewedAmbientId);
			if (actualDivPreview)
			{
				document.getElementById("td_ambient_" + previewedAmbientId).removeChild (actualDivPreview);
			}
		}
	}
	
	this.ambientProducts_rollover = function (pointNumber, theStatus, objReferer)
	{
		// Hacemos el Rollover de CIRCULO del Punto
		if (arguments[3] == 0)
		{
			rolloverPoint(pointNumber, theStatus, arguments[3]);
		}
		else
		{
			rolloverPoint(pointNumber, theStatus);
		}
		
		// Hacemos el Rollover de los elementos de información
		var actualTRInfo = document.getElementById("tr_point_info_" + pointNumber);
		if (actualTRInfo)
		{
			var pointStatus = "on";
			var characteristics_product_display = "inline";
			var allAhref = actualTRInfo.getElementsByTagName("a");
			for(contAhref=0;contAhref<allAhref.length;contAhref++)
			{
				switch (theStatus)
				{
					case "over":
						allAhref[contAhref].style.color = "#000000";
						pointStatus = "on";
						characteristics_product_display = "inline";
					break;
					case "out":
						allAhref[contAhref].style.color = "#FFFFFF";
						pointStatus = "off";
						characteristics_product_display = "none";
					break;
				}
			}
			var addImage = actualTRInfo.getElementsByTagName("img")[0];
			if (addImage)
			{
				var tmpSrc = addImage.src;
				addImage.src = tmpSrc.split("_")[0] + "_" + pointStatus + "." + tmpSrc.split(".")[tmpSrc.split(".").length - 1];
			}
			var characteristics_product = document.getElementById("characteristics_product_" + pointNumber);
			if (characteristics_product)
			{
				characteristics_product.style.display = characteristics_product_display;
			}
		}
	}
	
	function rolloverPoint (pointNumber, theStatus)
	{
		var actualPoint = document.getElementById("punto_" + pointNumber);
		var centerElement = actualPoint.getElementsByTagName("div")[0];
		if (actualPoint)
		{
			var pointStatus = "on";
			switch (theStatus)
			{
				case "over":
					pointStatus = "on";
					
					if (centerElement)
					{
						if (arguments[2] != 0)
						{
							centerElement.style.top = "-2px";
							centerElement.style.left = "4px";
							centerElement.style.width = "15px";
							centerElement.style.height = "15px";
							centerElement.innerHTML = "+";
							centerElement.style.fontFace = "Arial"
							centerElement.style.fontSize = "20px";
						}
						centerElement.style.color = "#FFFFFF";
					}
				break;
				
				case "out":
					pointStatus = "off";
					
					if (centerElement)
					{
						centerElement.innerHTML = pointNumber;
						centerElement.style.top = "3px";
						if (parseInt(pointNumber) < 10)
						{
							centerElement.style.left = "8px";
						}
						else
						{
							centerElement.style.left = "4px";
						}
						centerElement.style.width = "15px";
						centerElement.style.height = "15px";
						centerElement.style.fontFace = "Arial"
						centerElement.style.fontSize = "11px";
						centerElement.style.color = "#000000";
					}
				break;
			}
			actualPoint.style.backgroundImage = "url(../../img/productSelector_" + pointStatus + ".gif)";
		}
	}
	
	var inputElements = Array();
	this.rollover_input = function (objReferer, theStatus)
	{
		inputElements[objReferer.id] = objReferer;
		switch (theStatus)
		{
			case "focus":
				inputElements[objReferer.id].elementIsFocused = true;
			case "over":
				// si no tiene el foco...
				if (!inputElements[objReferer.id].elementIsFocused)
				{
					objReferer.style.backgroundColor = "#FFFFFF";
					if (objReferer.parentNode.nodeName.toLowerCase() == "td")
					{
						objReferer.parentNode.style.backgroundColor = "#FFFFFF";
					}
				}
			break;
			case "blur":
				inputElements[objReferer.id].elementIsFocused = false;
			case "out":
				// si no tiene el foco...
				if (!inputElements[objReferer.id].elementIsFocused)
				{
					objReferer.style.backgroundColor = "#E0DED8";
					if (objReferer.parentNode.nodeName.toLowerCase() == "td")
					{
						objReferer.parentNode.style.backgroundColor = "#E0DED8";
					}
				}
			break;
		}
	}
	
	/***********************************************************************************************************/
	/*************** SHOP CART FUNCTIONS ***********************************************************************/
	/***********************************************************************************************************/
	var actualUnits = 1;
	this.SHOPCART_saveUnits = function (objReferer)
	{
		actualUnits = objReferer.value;
	}
	this.SHOPCART_changeUnits = function (objReferer, finishedId, tempOrderActualPrice, actualUserID, userPriceGroup)
	{
		if (objReferer.value == "" || objReferer.value.indexOf(" ") != -1 || objReferer.value == 0)
		{
			alert ("Debe indicar unidades para este producto o eliminarlo de Su Pedido");
			objReferer.value = actualUnits;
			objReferer.focus();
			return(false);
		}
		BECARA_CATALOGO.SHOPCART_initLoader();
		// Llamamos por medio de AJAX a la actualización de datos en la Base de Datos
		var url = "shop_cart_ajax_controller.php?flagControl=updateUnits";
		var pars = "finishedId=" + finishedId + "&newUnits=" + objReferer.value + "&acualUserID=" + actualUserID + "&userPriceGroup=" + userPriceGroup + "&actualPrice=" + tempOrderActualPrice;
		var ajax = new Ajax.Request( url,{
											parameters: pars, 
											method:"post", 
											onComplete: function ()
														{
															BECARA_CATALOGO.SHOPCART_responseRefreshShopCart (actualUserID, userPriceGroup)
														}
										}
									);
	}
	
	this.SHOPCART_initLoader = function ()
	{
		// Cargamos le Loader
		var url = "shop_cart_ajax_controller.php?flagControl=initLoader";
		var myAjax = new Ajax.Updater("my_order_header", url, {
																method:"get"
															}
										);
	}
	this.SHOPCART_endLoader = function ()
	{
		// Cargamos le Loader
		var url = "shop_cart_ajax_controller.php?flagControl=endLoader";
		var myAjax = new Ajax.Updater("my_order_header", url, {
																method:"get"
															}
										);
	}
	
	this.SHOPCART_addElement = function (productId, finishedId, finishedPrice, userPriceGroup, actualUserID, elementType)
	{
		BECARA_CATALOGO.SHOPCART_initLoader();
		// Llamamos por medio de AJAX a la actualización de datos en la Base de Datos
		var url = "shop_cart_ajax_controller.php?flagControl=addTempOrder&typeElement=" + elementType;
		var pars = "productId=" + productId + "&finishedId=" + finishedId + "&finishedPrice=" + finishedPrice + "&actualUserID=" + actualUserID;
		var ajax = new Ajax.Request( url,{
											parameters: pars, 
											method:"post", 
											onComplete: function ()
														{
															BECARA_CATALOGO.SHOPCART_responseRefreshShopCart (actualUserID, userPriceGroup);
														}
										}
									);
	}
	
	this.SHOPCART_deleteElement = function (tempOrderID, actualUserId, userPriceGroup, typeElement, finishedId)
	{
		BECARA_CATALOGO.SHOPCART_initLoader();
		// Llamamos por medio de AJAX a la actualización de datos en la Base de Datos
		var url = "shop_cart_ajax_controller.php?flagControl=deleteTempOrder";
		var pars = "tempOrderId=" + tempOrderID + "&typeElement=" + typeElement + "&finishId=" + finishedId;
		var ajax = new Ajax.Request( url,{
											parameters: pars, 
											method:"post", 
											onComplete: function ()
														{
															BECARA_CATALOGO.SHOPCART_responseRefreshShopCart(actualUserId, userPriceGroup);
														}
										}
									);
		
	}
	
	this.SHOPCART_getTotalItemsShopCart = function (actualUserId)
	{
		// Llamamos a Ajax para que actualice el texto de la cabecara de "No hay ningún producto en su pedido"
		var url = "shop_cart_ajax_controller.php?flagControl=getTotalItemsShopCart";
		var pars = "actualURL=" + document.location.href + "&actualUserID=" + actualUserId;
		var myAjax = new Ajax.Updater("countShopCartUnits", url, {
																parameters: pars, 
																method:"get", 
																onComplete: function()
																			{
																				BECARA_CATALOGO.SHOPCART_endLoader();
																			}
															}
										);
	}
	
	this.SHOPCART_updateFinished = function (tempOrderID, productId, finishedId, finishedPrice, actualUserID, userPriceGroup)
	{
		BECARA_CATALOGO.SHOPCART_initLoader();
		// Llamamos por medio de AJAX a la actualización de datos en la Base de Datos
		var url = "shop_cart_ajax_controller.php?flagControl=updateFinishedTempOrder";
		var pars = "tempOrderId=" + tempOrderID + "&userId=" + actualUserID + "&productId=" + productId + "&newFinished=" + finishedId + "&finishedPrice=" + finishedPrice;
		var ajax = new Ajax.Request( url,{
											parameters: pars, 
											method:"post", 
											onComplete: function ()
														{
															BECARA_CATALOGO.SHOPCART_responseRefreshShopCart (actualUserID, userPriceGroup);
														}
										}
									);
	}
	
	this.SHOPCART_responseRefreshShopCart = function (actualUserId, userPriceGroup)
	{
		// Llamamos a AJAX para que actualice el contenido del carrito
		var url = "shop_cart_ajax_controller.php?flagControl=getActualShopCart";
		var pars = "actualURL=" + document.location.href + "&actualUserID=" + actualUserId + "&userPriceGroup=" + userPriceGroup;
		if (document.getElementById("observations"))
		{
			var observations = document.getElementById("observations").value;
			pars+= "&observations=" + observations;
		}
		var myAjax = new Ajax.Updater("shopCartContent", url, {
																parameters: pars, 
																method:"get", 
																onComplete: function ()
																			{
																				BECARA_CATALOGO.SHOPCART_getTotalItemsShopCart(actualUserId);
																			}
															}
									);
	}
	
	/***********************************************************************************************************/
	/*************** PROTOS FUNCTIONS **************************************************************************/
	/***********************************************************************************************************/
	this.actualPoint = null;
	this.totalPoints = null;
	this.colocateDivPoints = function ()
	{
		var actualImage = document.getElementById("ambient_image");
		var divPoints = document.getElementById("container_points");
		if (divPoints)
		{
			divPoints.style.width = actualImage.width;
			divPoints.style.height = actualImage.height;
			divPoints.style.top = LIB_navigator.findTopObj(actualImage) + "px";
			divPoints.style.left = LIB_navigator.findLeftObj(actualImage) + "px";
		}
		
		// Recorremos todos los PUNTOS y los hacemos visibles
		var allDivs = document.getElementsByTagName("div");
		for(contDivs = 0;contDivs<allDivs.length;contDivs++)
		{
			if (allDivs[contDivs].id.indexOf("punto_") != -1)
			{
				allDivs[contDivs].style.display = "inline";
			}
		}
		
		// Si existe la capa de Numeración de ambiente (Solo es en la Web, en el PROTOS no) la mostramos
		var divAmbientNumber = document.getElementById("ambientNumber");
		if (divAmbientNumber)
		{
			divAmbientNumber.style.display = "inline";
		}
		// Si existe el botón de navegación ANTERIOR...
		var divBeforeButton = document.getElementById("beforeButton");
		if (divBeforeButton)
		{
			divBeforeButton.style.display = "inline";
		}
		// Si existe el botón de navegación SIGUIENTE...
		var divAfterButton = document.getElementById("afterButton");
		if (divAfterButton)
		{
			divAfterButton.style.display = "inline";
		}
		// Si existe la capa de separación entre ambos botones
		var divAfterButton = document.getElementById("paginatorSeparator");
		if (divAfterButton)
		{
			divAfterButton.style.display = "inline";
		}
		
		// Si existe el ICONO de la LUPA para ampliar la imagen, lo mostramos
		var divEnlaregeImage = document.getElementById("enlaregeImage");
		if (divEnlaregeImage)
		{
			var ambient_image = document.getElementById("ambient_image");
			if (ambient_image)
			{
				var ambient_image_left = LIB_navigator.findLeftObj(ambient_image)
				var ambient_image_top = LIB_navigator.findTopObj(ambient_image);
				
				divEnlaregeImage.style.left = (ambient_image_left + parseInt(ambient_image.width) - parseInt(divEnlaregeImage.style.width) - 15) + "px";
				divEnlaregeImage.style.top = (ambient_image_top + parseInt(ambient_image.height) - parseInt(divEnlaregeImage.style.height) - 15) + "px";
				
				divEnlaregeImage.style.visibility = "visible";
			}
		}
	}
	
	this.PROTOS_ambientsAddNewProduct = function (evt)
	{
		BECARA_CATALOGO.totalPoints = getTotalPoints();
		// Creamos y posicionamos un PUNTO
		createNewProductPoin ();
		
		// Creamos las cajas de información de este punto creado
		createNewPointInfo();
	}
	
	this.PROTOS_ambientsStartToMoveNewProductPoint = function (evt, objPoint)
	{
		BECARA_CATALOGO.actualPoint = objPoint;
		BECARA_CATALOGO.actualPoint.style.backgroundImage = "url(../img/productSelector_on.gif)";
		var centerElement = BECARA_CATALOGO.actualPoint.getElementsByTagName("div")[0];
		centerElement.style.top = "-2px";
		centerElement.style.left = "4px";
		centerElement.style.width = "15px";
		centerElement.style.height = "15px";
		centerElement.innerHTML = "+";
		centerElement.style.fontFace = "Arial"
		centerElement.style.fontSize = "20px";
		centerElement.style.color = "#FFFFFF";
		
		var divPoints = document.getElementById("container_points");
		LIB_events.addDynamicEvent(divPoints, "mousemove", function(evt)
										   {
											   	BECARA_CATALOGO.PROTOS_ambientsMoveNewProductPoint(evt);
										   });
	}
	this.PROTOS_ambientsStopToMoveNewProductPoint = function (evt)
	{
		var ambientImage = document.getElementById("ambient_image");
		BECARA_CATALOGO.actualPoint.style.backgroundImage = "url(../img/productSelector_off.gif)"

		var centerElement = BECARA_CATALOGO.actualPoint.getElementsByTagName("div")[0];
		if (centerElement)
		{
			centerElement.innerHTML = BECARA_CATALOGO.actualPoint.id.split("_")[1];
			centerElement.style.top = "3px";
			if (parseInt(BECARA_CATALOGO.actualPoint.id.split("_")[1]) < 10)
			{
				centerElement.style.left = "8px";
			}
			else
			{
				centerElement.style.left = "4px";
			}
			centerElement.style.width = "15px";
			centerElement.style.height = "15px";
			centerElement.style.fontFace = "Arial"
			centerElement.style.fontSize = "11px";
			centerElement.style.color = "#000000";
		}
		
		// Vamos a grabar las coordenadas del punto, al soltarlo. las guardamos en el INPUT asociado a ese Punto
		var mouse_X = LIB_navigator.getMouse_X(evt);
		var mouse_Y = LIB_navigator.getMouse_Y(evt);
		
		var ambientImage = document.getElementById("ambient_image");
		
		var divPoints = document.getElementById("container_points");
		var divPoints_X = LIB_navigator.findLeftObj(divPoints);
		var divPoints_Y = LIB_navigator.findTopObj(divPoints);
		
		var relativeMouse_X = mouse_X - parseInt(divPoints_X);
		var relativeMouse_Y = mouse_Y - parseInt(divPoints_Y);
		
		var actualPointNumber = BECARA_CATALOGO.actualPoint.id.split("_")[1];
		setActualCoords (relativeMouse_X, relativeMouse_Y, actualPointNumber);

		BECARA_CATALOGO.actualPoint = null;
	}
	this.PROTOS_ambientsMoveNewProductPoint = function (evt)
	{
		if (BECARA_CATALOGO.actualPoint)
		{
			var mouse_X = LIB_navigator.getMouse_X(evt);
			var mouse_Y = LIB_navigator.getMouse_Y(evt);
			
			var ambientImage = document.getElementById("ambient_image");
			
			var divPoints = document.getElementById("container_points");
			var divPoints_X = LIB_navigator.findLeftObj(divPoints);
			var divPoints_Y = LIB_navigator.findTopObj(divPoints);
			
			var relativeMouse_X = mouse_X - parseInt(divPoints_X);
			var relativeMouse_Y = mouse_Y - parseInt(divPoints_Y);
	
			//  Control para no salirnos del tamaño de la imagen
			// Que no se salga por la derecha
			var move = false;
			if (parseInt(relativeMouse_X) <= (parseInt(ambientImage.width) - 10))
			{
				// Que no se salga por la izquierda
				if (parseInt(relativeMouse_X) >= 10)
				{
					// Que no se salga por arriba
					if (parseInt(relativeMouse_Y) >= 10)
					{
						if (parseInt(relativeMouse_Y) <= (parseInt(ambientImage.height) - 10))
						{
							move = true;
						}
					}
				}
			}
			if (move)
			{
				BECARA_CATALOGO.actualPoint.style.top = mouse_Y - divPoints_Y - 10 + "px";
				BECARA_CATALOGO.actualPoint.style.left = mouse_X - divPoints_X - 10 + "px";
			}
		}
	}
	
	// Private Functions
	function createNewProductPoin()
	{
		var ambientImage = document.getElementById("ambient_image");
		var ambientImage_X = LIB_navigator.findLeftObj(ambientImage);
		var ambientImage_Y = LIB_navigator.findTopObj(ambientImage);
		var ambientImage_width = ambientImage.width;
		var ambientImage_height = ambientImage.height;

		var newProductSelectorPoint = document.createElement("div");
		
		newProductSelectorPoint.id = "punto_" + BECARA_CATALOGO.totalPoints;
		newProductSelectorPoint.name = "point";
		newProductSelectorPoint.style.position = "absolute";
		newProductSelectorPoint.style.top = (parseInt(ambientImage_height / 2) - 10) + "px";
		newProductSelectorPoint.style.left = (parseInt(ambientImage_width / 2) - 10) + "px";;
		newProductSelectorPoint.style.width = "20px";
		newProductSelectorPoint.style.height = "20px";
		newProductSelectorPoint.style.cursor = "pointer";
		newProductSelectorPoint.style.backgroundImage = "url(../img/productSelector_on.gif)";
		newProductSelectorPoint.style.backgroundRepeat = "no-repeat";
		
		LIB_events.addDynamicEvent(newProductSelectorPoint, "mousedown", function(evt)
										   {
											   	BECARA_CATALOGO.PROTOS_ambientsStartToMoveNewProductPoint(evt, newProductSelectorPoint);
										   });
		LIB_events.addDynamicEvent(newProductSelectorPoint, "mouseup", function(evt)
										   {
											   	BECARA_CATALOGO.PROTOS_ambientsStopToMoveNewProductPoint(evt);
										   });
		
		var centerElement = document.createElement("div");
		centerElement.style.position = "relative";
		centerElement.style.top = "-2px";
		centerElement.style.left = "4px";
		centerElement.style.width = "15px";
		centerElement.style.height = "15px";
		centerElement.innerHTML = "+";
		centerElement.style.fontFace = "Arial"
		centerElement.style.fontSize = "20px";
		centerElement.style.color = "#FFFFFF";
		newProductSelectorPoint.appendChild (centerElement);
		
		document.getElementById("container_points").appendChild(newProductSelectorPoint);
		return (newProductSelectorPoint);
	}
	
	function createNewPointInfo ()
	{
		var ambientTablePointsInfo = document.getElementById("ambient_content_points_info");

		var separator1Div = document.createElement("div");
		separator1Div.id = "separator1_" + BECARA_CATALOGO.totalPoints;
		separator1Div.style.width = "100%";
		separator1Div.style.height = "1px";
		separator1Div.style.fontFace = "Arial"
		separator1Div.style.fontSize = "1px";
		separator1Div.style.lineHeight = "1px";
		separator1Div.style.margin = "0px";
		separator1Div.style.marginTop = "17px";
		separator1Div.style.padding = "0px";
		ambientTablePointsInfo.appendChild(separator1Div);
		
		// SPAN con el nombre del punto
		var newTextInfo = document.createElement("span");
		newTextInfo.id = "textInfo_" + BECARA_CATALOGO.totalPoints;
		newTextInfo.innerHTML = "Punto " + BECARA_CATALOGO.totalPoints + ": ";
		newTextInfo.style.display = "inline";
		newTextInfo.style.float = "left";
		newTextInfo.style.fontFace = "Arial"
		newTextInfo.style.fontWeight = "bold"
		newTextInfo.style.fontColor = "#000000"
		newTextInfo.style.fontSize = "11px";
		newTextInfo.style.paddingRight = "12px";
		ambientTablePointsInfo.appendChild(newTextInfo);

		// Enlace para abrir el popup de los productos
		var newLinkInfo = document.createElement("a");
		newLinkInfo.id = "productsLink_" + BECARA_CATALOGO.totalPoints;
		newLinkInfo.style.display = "inline";
		newLinkInfo.style.float = "left";
		newLinkInfo.style.textDecoration = "underline";
		newLinkInfo.style.cursos = "pointer";
		newLinkInfo.href = "javascript:BECARA_CATALOGO.PROTOS_openPopupProducts(" + BECARA_CATALOGO.totalPoints + ")";
		newLinkInfo.innerHTML = "Añadir Producto";
		if (LIB_navigator.getName() == "Microsoft Internet Explorer") {
			attributes = new Array ({attributeName:"className", attributeValue:"a11_000000_bold"});
		} else {
			attributes = new Array ({attributeName:"class", attributeValue:"a11_000000_bold"});
		}
		LIB_DHTML.setDynamicAttributes(newLinkInfo, attributes);
		ambientTablePointsInfo.appendChild(newLinkInfo);
		
		// Enlace para eliminar este punto
		var newEraseInfo = document.createElement("a");
		newEraseInfo.id = "erase_" + BECARA_CATALOGO.totalPoints;
		newEraseInfo.style.display = "inline";
		newEraseInfo.style.float = "right";
		newEraseInfo.style.textDecoration = "underline";
		newEraseInfo.style.cursos = "pointer";
		newEraseInfo.style.paddingLeft = "12px";
		newEraseInfo.href = "javascript:BECARA_CATALOGO.PROTOS_deletePoint(" + BECARA_CATALOGO.totalPoints + ")";
		newEraseInfo.innerHTML = "Eliminar Producto";
		if (LIB_navigator.getName() == "Microsoft Internet Explorer") {
			attributes = new Array ({attributeName:"className", attributeValue:"a11_000000_bold"});
		} else {
			attributes = new Array ({attributeName:"class", attributeValue:"a11_000000_bold"});
		}
		LIB_DHTML.setDynamicAttributes(newEraseInfo, attributes);
		ambientTablePointsInfo.appendChild(newEraseInfo);
		
		// Input para las coordenadas del producto
		var newInputInfo = document.createElement("input");
		newInputInfo.type = "hidden";
		newInputInfo.id = "point_input_" + BECARA_CATALOGO.totalPoints;
		newInputInfo.name = "products[]";
		newInputInfo.style.display = "inline";
		newInputInfo.style.float = "left";
		newInputInfo.style.width = "60px"
		newInputInfo.style.height = "18px"
		newInputInfo.style.fontFace = "Arial"
		newInputInfo.style.fontWeight = "bold"
		newInputInfo.style.fontColor = "#000000"
		newInputInfo.style.fontSize = "10px";
		ambientTablePointsInfo.appendChild(newInputInfo);
		
		var separator2Div = document.createElement("div");
		separator2Div.id = "separator2_" + BECARA_CATALOGO.totalPoints;
		separator2Div.style.width = "100%";
		separator2Div.style.height = "4px";
		separator2Div.style.fontFace = "Arial"
		separator2Div.style.fontSize = "1px";
		separator2Div.style.lineHeight = "1px";
		separator2Div.style.margin = "0px";
		separator2Div.style.padding = "0px";
		ambientTablePointsInfo.appendChild(separator2Div);
		
		// SPAN que contendrá la referencia del producto señalado
		var newReferenceInfo = document.createElement("span");
		newReferenceInfo.id = "reference_info_" + BECARA_CATALOGO.totalPoints;
		newReferenceInfo.innerHTML = "";
		newReferenceInfo.style.display = "inline";
		newReferenceInfo.style.float = "left";
		newReferenceInfo.style.fontFace = "Arial"
		newReferenceInfo.style.fontColor = "#000000"
		newReferenceInfo.style.fontSize = "11px";
		newReferenceInfo.style.paddingLeft = "12px";
		ambientTablePointsInfo.appendChild(newReferenceInfo);
		
		var separator3Div = document.createElement("div");
		separator3Div.id = "separator3_" + BECARA_CATALOGO.totalPoints;
		separator3Div.style.width = "100%";
		separator3Div.style.height = "4px";
		separator3Div.style.fontFace = "Arial"
		separator3Div.style.fontSize = "1px";
		separator3Div.style.lineHeight = "1px";
		separator3Div.style.margin = "0px";
		separator3Div.style.padding = "0px";
		ambientTablePointsInfo.appendChild(separator3Div);
		
		// SPAN que contendrá el nombre del producto señalado
		var newNameInfo = document.createElement("span");
		newNameInfo.id = "name_info_" + BECARA_CATALOGO.totalPoints;
		newNameInfo.innerHTML = "";
		newNameInfo.style.display = "inline";
		newNameInfo.style.float = "left";
		newNameInfo.style.fontFace = "Arial"
		newNameInfo.style.fontColor = "#000000"
		newNameInfo.style.fontSize = "11px";
		newNameInfo.style.paddingLeft = "12px";
		ambientTablePointsInfo.appendChild(newNameInfo);
		
	}
	
	function getTotalPoints ()
	{
		var totalPoints = 0;
		var allDivs = document.getElementsByTagName("div");
		for(contDivs=0;contDivs<allDivs.length;contDivs++)
		{
			if (allDivs[contDivs].id.indexOf("punto_") == 0)
			{
				totalPoints = totalPoints + 1;
			}
		}
		return(totalPoints + 1);
	}
	
	this.PROTOS_openPopupProducts = function (pointNumber)
	{
		// Preparamos el listado de PUNTOS (referencias) que ya tenemos seleccionados
		var pointsInfoContainer = document.getElementById("ambient_content_points_info");
		var pointsInfoContainerItems = pointsInfoContainer.getElementsByTagName("input");
		var actualReferences = "";
		for (contInputs=0;contInputs<pointsInfoContainerItems.length;contInputs++)
		{
			if (pointsInfoContainerItems[contInputs].id && pointsInfoContainerItems[contInputs].id.indexOf("point_input_") != -1)
			{
				var tempArrayReferences = pointsInfoContainerItems[contInputs].value.split("|");
				if (tempArrayReferences[0] && tempArrayReferences[0].indexOf(",") == -1)
				{
					actualReferences+= tempArrayReferences[0];
					// Si tenemos coordenadas de FINISHED_ID ...
					if (tempArrayReferences.length > 1 && tempArrayReferences[1].indexOf(",") == -1)
					{
						actualReferences+= "_" + tempArrayReferences[1];
					}
					actualReferences+= "|";
				}
			}
		}
		var theURL = "products_select_product.php?point=" + pointNumber;
		if (actualReferences != "")
		{
			theURL+= "&references=" + actualReferences;
		}
		LIB_navigator.openPopup (theURL, "products_popup", 830, 730, "onlyscroll");
	}
	
	this.PROTOS_openPopupFinisheds = function (thePoint, productId, productReference, productName)
	{
		var theURL = "products_select_product_finisheds.php?productId=" + productId + "&productReference=" + productReference + "&point=" + thePoint + "&productName=" + productName;
		// Recogemos los ID's de los acabados que se están usando
		
		var actualReferences = document.getElementById("point_input_" + thePoint).value.split("|")[1];
		
		if (actualReferences != "")
		{
			theURL+= "&references=" + actualReferences;
		}
		LIB_navigator.openPopup (theURL, "products_popup", 830, 730, "onlyscroll");
	}
	
	this.PROTOS_selecProductAndClose = function (thePoint, idProduct, referenceProduct, productName, tempFinisheds)
	{
		var actualPoint = opener.document.getElementById("point_input_" + thePoint);
		var actualReferenceInfo = opener.document.getElementById("reference_info_" + thePoint);
		var actualNameInfo = opener.document.getElementById("name_info_" + thePoint);
		if (actualPoint)
		{
			var finishedsIds = "";
			for (contFinisheds=0;contFinisheds<tempFinisheds.length;contFinisheds++)
			{
				
				if (finishedsIds == "")
				{
					finishedsIds = tempFinisheds[contFinisheds]["FINISHED_ID"];
				}
				else
				{
					finishedsIds = finishedsIds + "_" + tempFinisheds[contFinisheds]["FINISHED_ID"];
				}
			}
			if (actualPoint.value == "")
			{
				actualPoint.value = idProduct + "|" + finishedsIds;
			}
			else
			{
				// Miramos a ver si tiene COORDENADAS
				if (actualPoint.value.indexOf(",") != -1)
				{
					if (actualPoint.value.indexOf("|") != -1)
					{
						var actualCoords = actualPoint.value.split("|")[actualPoint.value.split("|").length - 1];
						actualPoint.value = idProduct + "|" + finishedsIds + "|" + actualCoords;
					}
					else
					{
						var actualCoords = actualPoint.value;
						actualPoint.value = idProduct + "|" + finishedsIds + "|" + actualCoords;
					}
				}
				else
				{
					actualPoint.value = idProduct + "|" + finishedsIds;
				}
			}
			actualReferenceInfo.visibility = "visible";
			actualReferenceInfo.innerHTML = "Ref. " + referenceProduct;
			actualNameInfo.visibility = "visible";
			actualNameInfo.innerHTML = "Producto: " + productName.replace(/_/g, " ");
			
			// Generamos el listado de Acabados elegidos para este Producto
			
			
			window.close();
		}
	}
	
	this.PROTOS_deletePoint = function (pointToDelete)
	{
		if (confirm("¿Desea eliminar este punto, y su producto asignado?"))
		{
			var pointsContainer = document.getElementById("container_points");
			
			// *******************************************
			// Gestión de PUNTOS
			var actualPointToDelete = document.getElementById("punto_" + pointToDelete);
			pointsContainer.removeChild (actualPointToDelete);
			
			var allPoints = pointsContainer.getElementsByTagName("div");
			for(contPoints=0;contPoints<allPoints.length;contPoints++)
			{
				// Si el elemento actual es un punto...
				if (allPoints[contPoints].id && allPoints[contPoints].id.indexOf("punto_") != -1)
				{
					var actualPoint = parseInt(allPoints[contPoints].id.split("punto_")[1]);
					if (actualPoint >= pointToDelete)
					{
						allPoints[contPoints].id = "punto_" + (parseInt(actualPoint) - 1);
						var centerElement = allPoints[contPoints].getElementsByTagName("div");
						if (centerElement)
						{
							centerElement = centerElement[0];
							// Si el punto tiene visible su texto...
							if (centerElement.innerHTML != "+")
							{
								centerElement.innerHTML = (parseInt(actualPoint) - 1);
							}
						}
					}
				}
			}
			
			// *******************************************
			// Gestión de ELEMENTOS INFORMATIVOS
			var pointsInfoContainer = document.getElementById("ambient_content_points_info");
			
			var point_ambientContainer = document.getElementById("finisheds_" + pointToDelete + "_container");
			if (point_ambientContainer)
			{
				pointsInfoContainer.removeChild(point_ambientContainer);
			}
			
			var actualSeparatorToDelete = document.getElementById("separator1_" + pointToDelete);
			pointsInfoContainer.removeChild (actualSeparatorToDelete);
			var actualSeparatorToDelete = document.getElementById("separator2_" + pointToDelete);
			pointsInfoContainer.removeChild (actualSeparatorToDelete);
			var actualSeparatorToDelete = document.getElementById("separator3_" + pointToDelete);
			pointsInfoContainer.removeChild (actualSeparatorToDelete);
			
			var actualTextInfoToDelete = document.getElementById("textInfo_" + pointToDelete);
			pointsInfoContainer.removeChild (actualTextInfoToDelete);
			
			var actualProductsLinkToDelete = document.getElementById("productsLink_" + pointToDelete);
			pointsInfoContainer.removeChild (actualProductsLinkToDelete);
			
			var actualReferenceToDelete = document.getElementById("reference_info_" + pointToDelete);
			pointsInfoContainer.removeChild (actualReferenceToDelete);
			
			var actualNameToDelete = document.getElementById("name_info_" + pointToDelete);
			pointsInfoContainer.removeChild (actualNameToDelete);
			
			var actualEraseToDelete = document.getElementById("erase_" + pointToDelete);
			pointsInfoContainer.removeChild (actualEraseToDelete);
			
			var actualPointInputToDelete = document.getElementById("point_input_" + pointToDelete);
			pointsInfoContainer.removeChild (actualPointInputToDelete);
			
			var typesToModified = new Array("div", "span", "a", "input");
			for (contTypes=0;contTypes<typesToModified.length;contTypes++)
			{
				var pointsInfoContainerItems = pointsInfoContainer.getElementsByTagName(typesToModified[contTypes]);
				for(cont=0;cont<pointsInfoContainerItems.length;cont++)
				{
					var idToSearch = null;
					// Si el elemento actual es un punto...
					switch (typesToModified[contTypes])
					{
						case "div":
							if (pointsInfoContainerItems[cont].id && pointsInfoContainerItems[cont].id.indexOf("separator") != -1)
							{
								var actualPoint = pointsInfoContainerItems[cont].id.split("separator")[1];
								var actualSeparatorPosition = parseInt(actualPoint.split("_")[0]);
								actualPoint = parseInt(actualPoint.split("_")[1]);
								if (actualPoint >= pointToDelete)
								{
									pointsInfoContainerItems[cont].id = "separator" + actualSeparatorPosition + "_" + (parseInt(actualPoint) - 1);
								}
							}
						break;
						case "span":
							if (pointsInfoContainerItems[cont].id && pointsInfoContainerItems[cont].id.indexOf("textInfo_") != -1)
							{
								var actualPoint = parseInt(pointsInfoContainerItems[cont].id.split("textInfo_")[1]);
								if (actualPoint >= pointToDelete)
								{
									pointsInfoContainerItems[cont].id = "textInfo_" + (parseInt(actualPoint) - 1);
									pointsInfoContainerItems[cont].innerHTML = "Punto " + (parseInt(actualPoint) - 1) + ": ";
								}
							}
							if (pointsInfoContainerItems[cont].id && pointsInfoContainerItems[cont].id.indexOf("reference_info_") != -1)
							{
								var actualPoint = parseInt(pointsInfoContainerItems[cont].id.split("reference_info_")[1]);
								if (actualPoint >= pointToDelete)
								{
									pointsInfoContainerItems[cont].id = "reference_info_" + (parseInt(actualPoint) - 1);
								}
							}
							if (pointsInfoContainerItems[cont].id && pointsInfoContainerItems[cont].id.indexOf("name_info_") != -1)
							{
								var actualPoint = parseInt(pointsInfoContainerItems[cont].id.split("name_info_")[1]);
								if (actualPoint >= pointToDelete)
								{
									pointsInfoContainerItems[cont].id = "name_info_" + (parseInt(actualPoint) - 1);
								}
							}
						break;
						case "a":
							if (pointsInfoContainerItems[cont].id && pointsInfoContainerItems[cont].id.indexOf("productsLink_") != -1)
							{
								var actualPoint = parseInt(pointsInfoContainerItems[cont].id.split("productsLink_")[1]);
								if (actualPoint >= pointToDelete)
								{
									pointsInfoContainerItems[cont].id = "productsLink_" + (parseInt(actualPoint) - 1);
									pointsInfoContainerItems[cont].href = "javascript:BECARA_CATALOGO.PROTOS_openPopupProducts(" + (parseInt(actualPoint) - 1) + ")";
								}
							}
							if (pointsInfoContainerItems[cont].id && pointsInfoContainerItems[cont].id.indexOf("erase_") != -1)
							{
								var actualPoint = parseInt(pointsInfoContainerItems[cont].id.split("erase_")[1]);
								if (actualPoint >= pointToDelete)
								{
									pointsInfoContainerItems[cont].id = "erase_" + (parseInt(actualPoint) - 1);
									pointsInfoContainerItems[cont].href = "javascript:BECARA_CATALOGO.PROTOS_deletePoint(" + (parseInt(actualPoint) - 1) + ")";
								}
							}
						break;
						case "input":
							if (pointsInfoContainerItems[cont].id && pointsInfoContainerItems[cont].id.indexOf("point_input_") != -1)
							{
								var actualPoint = parseInt(pointsInfoContainerItems[cont].id.split("point_input_")[1]);
								if (actualPoint >= pointToDelete)
								{
									pointsInfoContainerItems[cont].id = "point_input_" + (parseInt(actualPoint) - 1);
								}
							}
						break;
					}
				}
			}
		}
	}
	
	function setActualCoords (relative_X, relative_Y, pontNumber)
	{
		var actualInputInfo = document.getElementById("point_input_" + pontNumber);
		if (actualInputInfo.value == "")
		{
			actualInputInfo.value = (parseInt(relative_X) - 10) + "," + (parseInt(relative_Y) - 10);
		}
		else
		{
			// Miramos a ver si tiene ID DE PRODUCTO
			if (actualInputInfo.value.indexOf("|") != -1)
			{
				// si hay 3 elementos en la caja (PRODUCT_ID|FINISHED_ID|COORDS)...
				var tempArray = actualInputInfo.value.split("|");
				if (tempArray.length == 3)
				{
					var productId = tempArray[0];
					var finishedId = tempArray[1];
					var actualCords = tempArray[2];
					actualInputInfo.value = productId + "|" + finishedId + "|" + (parseInt(relative_X) - 10) + "," + (parseInt(relative_Y) - 10);
				}
				else
				{
					// Si hay coordenadas...
					if (tempArray[1].indexOf(",") != -1)
					{
						var productId = tempArray[0];
						var actualCords = tempArray[1];
						actualInputInfo.value = productId + "|" + (parseInt(relative_X) - 10) + "," + (parseInt(relative_Y) - 10);
					}
					else
					{
						var productId = tempArray[0];
						var finishedId = tempArray[1];
						actualInputInfo.value = productId + "|" + finishedId + "|" + (parseInt(relative_X) - 10) + "," + (parseInt(relative_Y) - 10);
					}
				}
			}
			else
			{
				actualInputInfo.value = (parseInt(relative_X) - 10) + "," + (parseInt(relative_Y) - 10);
			}
		}
	}
}

// Creamos un objeto de la clase LIB_navigator
var BECARA_CATALOGO = new BECARA_CATALOGO_object ();
if (document.location.href.toLowerCase().indexOf("protos/ambients_view.php") != -1 || document.location.href.toLowerCase().indexOf("ambient_view.php") != -1)
{
	LIB_events.addDynamicEvent (window, "load", BECARA_CATALOGO.colocateDivPoints);
}
