var specCookieName = 'FVIPSPECCOOKIE';
var categoryCookieName = 'FVIPCATEGCOOKIE';
var orderByCookieName = 'FVIPORDERBYCOOKIE';
var shopCartCookieName = 'FVIPSHOPSID';

function addProduct(productId) {
	var content = document.getElementById('content');
	myForm = document.createElement('form');
	myForm.id = 'shopCartForm';
	myForm.name = 'shopCartForm';
	myForm.method = 'post';
	myForm.action = 'carro.php';
	content.appendChild(myForm);

	myForm = document.getElementById('shopCartForm');
	
	var tmpInput = document.createElement('input');
	tmpInput.type = 'hidden';
	tmpInput.name = 'act';
	tmpInput.id = 'act';
	tmpInput.value = 'add';
	myForm.appendChild(tmpInput);
	
	var tmpInput = document.createElement('input');
	tmpInput.type = 'hidden';
	tmpInput.name = 'productId';
	tmpInput.id = 'productId';
	tmpInput.value = productId;
	myForm.appendChild(tmpInput);
	
	myForm.submit();
	return false;
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name)
{
	createCookie(name,"",-1);
}

function checkSearchField() {
	var field = document.getElementById('searchTerm');
	if (!field.value)
		field.value = "Buscar";
		
}

function checkSearchform() {
	var field = document.getElementById('searchTerm');
	
	if (!field.value || field.value == 'Buscar') {
		alert('Por favor, ingrese un término para la búsqueda.');
		field.focus();
		return false;
	}
	
	return true;
}

function addToCart(id) {
	var shopForm = document.forms.buyProductForm;
	
	var qty = shopForm.qty.value;
	if (isNaN(Number(qty))) {
		alert('El número de productos a comprar es inválido.');
		return false;
	}
	else {
		if (qty < 1) {
			alert('Debe ingresar al menos 1 para la cantidad.');
			return false;
		}
	}
	
	//var warranty = shopForm.warrantyType.value;
	var warranty = 1;
	var inputs = shopForm.getElementsByTagName('input');
	for (var n=0; n < inputs.length; n++) {
		if (inputs.item(n).getAttribute('name') == 'warrantyType' &&
			inputs.item(n).checked) {
			warranty = inputs.item(n).value;
			break;
		}
	}
	
	if (!scriptor_load('httpRequest', 'default', 'es')) {
		alert('No se puede agregar el producto al carro de compras.\nInténtelo nuevamente más tarde.');	
	}
	else {
		var http_request = new httpRequest('xml/xml_addProductToCart.php', 'POST', checkProductAdded, null, null);
		http_request.send('productId=' + id + '&qty=' + qty + '&warrantyId=' + warranty);
	}
}

function addToCartRelated(id) {
	var qty = 1;
	var warranty = 1;
	
	if (!scriptor_load('httpRequest', 'default', 'es')) {
		alert('No se puede agregar el producto al carro de compras.\nInténtelo nuevamente más tarde.');	
	}
	else {
		var http_request = new httpRequest('xml/xml_addProductToCart.php', 'POST', checkProductAdded, null, null);
		http_request.send('productId=' + id + '&qty=' + qty + '&warrantyId=' + warranty);
	}
}

function checkProductAdded(xmlData) {
	var root = xmlData.getElementsByTagName('root').item(0);
	
	if (root.getAttribute('success') == '1') {
		//console.log('session=' + root.getAttribute('sessionid'));
		if (root.getAttribute('sessionid'))
			createCookie(shopCartCookieName, root.getAttribute('sessionid'));
		window.location.reload();
	}
	else {
		alert(root.getAttribute('error'));
	}
}

//invalidate and set popup windows
function makeTransparent( obj, ndx ) { 
	if (obj.style) {		
		if (obj.style.filter !== undefined) 
			obj.style.filter = 'alpha(opacity=' + ndx + ');';
		else {	
			if (obj.style.MozOpacity !== undefined) {			
				obj.style.MozOpacity = '0.' + ndx;
			}
			else {	
				if (obj.style.opacity !== undefined)
					obj.style.opacity = '0.' + ndx;
			}
		}
	}
}

var stylePanel = false;
var stylePanelVisible = false;

function showEventsPanel(e) {
	if (!e)
		e = window.event;
	
	var elem = (e.target) ? e.target : e.srcElement;
	var x, y;
	
	if (elem == document.getElementById('eventsPanel')) {
		if (e.offsetX) {
			x = e.offsetX;
			y = e.offsetY
		}
		else {
			x = e.pageX - document.getBoxObjectFor(elem).x;
			y = e.pageY - document.getBoxObjectFor(elem).y;
		}

		if (e.pageX) {
			x = e.pageX - x;
			y = e.pageY - y - 21;
		}
		else {
			if (e.x) {
				x = e.x + document.documentElement.scrollLeft - x;
				y = e.y + document.documentElement.scrollTop - y - 21;
			}
			else {
				x = 0;
				y = 0;
			}
		}
	}
	
	
	if (e.button == 0)
		setTimeout('_showEventsPanel('+x+','+y+')', 300);
	else
		_showEventsPanel(x,y);
	
}

function _showEventsPanel(x, y) {
	if (!stylePanelVisible) {
		stylePanel.style.left = x + 'px';
		stylePanel.style.top = y + 'px';
		stylePanel.style.display = 'block';
		document.onmousemove = hideStylesPanel;
		stylePanelVisible = true;
	}
}

function hideStylesPanel(e) {
	if (!e)
		e = window.event;
		
	var x, y;
	
	if (e.pageX) {
		x = e.pageX;
		y = e.pageY;
	}
	else {
		if (e.x) {
			x = e.x + document.documentElement.scrollLeft;
			y = e.y + document.documentElement.scrollTop;
		}
		else {
			x = 0;
			y = 0;
		}
	}

	setTimeout('_hideStylesPanel('+x+','+y+')', 300);
}

function _hideStylesPanel(x, y) {
	var elemX1 = Number(stylePanel.style.left.substr(0, stylePanel.style.left.length -2));
	var elemX2 = elemX1 + 360;
	var elemY1 = Number(stylePanel.style.top.substr(0, stylePanel.style.top.length -2));
	var elemY2 = elemY1 + 20
	
	var toolTipX1 = elemX1;
	var toolTipY1 = elemY1 + 21;
	var toolTipX2 = elemX1 + 85;
	var toolTipY2 = elemY1 + 21 + 35;
	
	if (!(x >= elemX1 && x <= elemX2) || !(y >= elemY1 && y <= elemY2)) {
		if (!(x >= toolTipX1 && x <= toolTipX2) || !(y >= toolTipY1 && y <= toolTipY2)) {
			if (stylePanelVisible) {
				stylePanel.style.display = 'none';
				document.onmousemove = null;
				stylePanelVisible = false;
			}
		}
	}
}

function showOtherDesc() {
	if (document.getElementById('prodLeftHidden').style.display == 'none') {
		document.getElementById('prodLeft').style.display = 'none';
		document.getElementById('prodLeftHidden').style.display = 'block';
	}
	else {
		document.getElementById('prodLeft').style.display = 'block';
		document.getElementById('prodLeftHidden').style.display = 'none';
	}
	
	return false;
}