/************************************************************************************************************
(C) www.dhtmlgoodies.com, March 2006

This is a script from www.dhtmlgoodies.com. You will find this and a lot of other scripts at our website.	

Terms of use:
You are free to use this script as long as the copyright message is kept intact. However, you may not
redistribute, sell or repost it without our permission.

Version:
	1.0	Released	March. 3rd 2006

Thank you!

www.dhtmlgoodies.com
Alf Magne Kalleland

************************************************************************************************************/

var flyingSpeed = 25;
var url_addProductToBasket = 'include/add_carro.php';
var url_removeProductFromBasket = 'include/del_carro.php';
var txt_totalPrice = 'Total: ';
var vaciarcarro_url = 'include/vacia_carro.php';
var ver_carro= 'include/add_carro.php';
var valida_user= 'include/validar_user.php';

var shopping_cart_div = false;
var flyingDiv = false;
var currentProductDiv = false;

var shopping_cart_x = false;
var shopping_cart_y = false;

var slide_xFactor = false;
var slide_yFactor = false;

var diffX = false;
var diffY = false;

var currentXPos = false;
var currentYPos = false;

var ajaxObjects = new Array();


function shoppingCart_getTopPos(inputObj)
{		
  var returnValue = inputObj.offsetTop;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetTop;
  }
  return returnValue;
}

function shoppingCart_getLeftPos(inputObj)
{
  var returnValue = inputObj.offsetLeft;
  while((inputObj = inputObj.offsetParent) != null){
  	if(inputObj.tagName!='HTML')returnValue += inputObj.offsetLeft;
  }
  return returnValue;
}
	

function addToBasket(productId)
{

	
	if(!shopping_cart_div)shopping_cart_div = document.getElementById('shopping_cart');
	if(!flyingDiv){
		flyingDiv = document.createElement('DIV');
		flyingDiv.style.position = 'absolute';
		document.body.appendChild(flyingDiv);
		
	}
	
	shopping_cart_x = shoppingCart_getLeftPos(shopping_cart_div);
	shopping_cart_y = shoppingCart_getTopPos(shopping_cart_div);

	currentProductDiv = document.getElementById('slidingProduct' + productId);
	
	currentXPos = shoppingCart_getLeftPos(currentProductDiv);
	currentYPos = shoppingCart_getTopPos(currentProductDiv);
	
	diffX = shopping_cart_x - currentXPos;
	diffY = shopping_cart_y - currentYPos;
	

	
	var shoppingContentCopy = currentProductDiv.cloneNode(true);
	shoppingContentCopy.id='';
	flyingDiv.innerHTML = '';
	flyingDiv.style.left = currentXPos + 'px';
	flyingDiv.style.top = currentYPos + 'px';
	flyingDiv.appendChild(shoppingContentCopy);
	flyingDiv.style.display='block';
	flyingDiv.style.width = currentProductDiv.offsetWidth + 'px';
	flyToBasket(productId);
	
}


function flyToBasket(productId)
{
	var maxDiff = Math.max(Math.abs(diffX),Math.abs(diffY));
	var moveX = (diffX / maxDiff) * flyingSpeed;;
	var moveY = (diffY / maxDiff) * flyingSpeed;	
	
	currentXPos = currentXPos + moveX;
	currentYPos = currentYPos + moveY;
	
	flyingDiv.style.left = Math.round(currentXPos) + 'px';
	flyingDiv.style.top = Math.round(currentYPos) + 'px';	
	
	
	if(moveX>0 && currentXPos > shopping_cart_x){
		flyingDiv.style.display='none';		
	}
	if(moveX<0 && currentXPos < shopping_cart_x){
		flyingDiv.style.display='none';		
	}
		
	if(flyingDiv.style.display=='block')setTimeout('flyToBasket("' + productId + '")',10); else ajaxAddProduct(productId);	
}

function showAjaxBasketContent(ajaxIndex)
{
	// Getting a reference to the shopping cart items table
	
	
	var itemBox = document.getElementById('shopping_cart_items');
	var productItems = ajaxObjects[ajaxIndex].response.split('|||');	// Breaking response from Ajax into tokens
	
	if(document.getElementById('shopping_cart_items_product' + productItems[0])){	// A product with this id is allready in the basket - just add number items
		var row = document.getElementById('shopping_cart_items_product' + productItems[0]);
		var items = row.cells[0].innerHTML /1;
		items = items + 1;
		row.cells[0].innerHTML = items;
	
	    ro=document.getElementById('total');
		var total=productItems[2].split('.');
		inner=new Number(ro.cells[1].innerHTML);
		outer=new Number(total[0]);
		
	ro.cells[1].innerHTML=inner+outer;
		 if (document.getElementById('carro_grande')){
			   ro2=document.getElementById('total_grande');
			   ro2.cells[8].innerHTML=ro.cells[1].innerHTML;
			   }
		
	}else{	// Product isn't allready in the basket - add a new row
		var tr = itemBox.insertRow(-1);
		tr.id = 'shopping_cart_items_product' + productItems[0]
		tr.style.verticalAlign = 'top';
		var td = tr.insertCell(-1);
		td.vAlign = 'top';
		td.innerHTML = '1'; 	// Number of items
		   ro=document.getElementById('total');
		   if (document.getElementById('carro_grande')){
			   
			   }
		   
		var total=productItems[2].split('.');
		inner=new Number(ro.cells[1].innerHTML);
		outer=new Number(total[0]);
		
		ro.cells[1].innerHTML=inner+outer;
		
		

		
		var td = tr.insertCell(-1);
		td.width="100";
		td.style.verticalAlign = 'top';
		//td.onmouseover=function(){carro_especial(productItems[0]);};
		td.innerHTML = "<a href='?mod=ver&id=" + productItems[0]+"' class='carrito' >"+ productItems[1]+"</a>"; 	// Description
		
			
		
		var td = tr.insertCell(-1);
		td.style.verticalAlign = 'top';
		var a = document.createElement('A');
		td.appendChild(a);
		a.href = '#';
		a.onclick = function(){ removeProductFromBasket(productItems[0]); };
		var img = document.createElement('IMG');
		img.src = 'images/remove.gif';
		img.border='0';
		a.appendChild(img);
		//td.innerHTML = '<a href="#" onclick="removeProductFromBasket("' + productItems[0] + '");return false;"><img src="images/remove.gif"></a>';	
	} 


	updateTotalPrice();
	
	ajaxObjects[ajaxIndex] = false;	

	
}



function updateTotalPrice()
{
	
	var itemBox = document.getElementById('shopping_cart_items');
	// Calculating total price and showing it below the table with basket items
	var totalPrice = 0;
	if(document.getElementById('shopping_cart_totalprice')){
		for(var no=1;no<itemBox.rows.length;no++){
		//	totalPrice = totalPrice + (itemBox.rows[no].cells[0].innerHTML.replace(/[^0-9]/g) * itemBox.rows[no].cells[2].innerHTML);
			
		}		
		//document.getElementById('shopping_cart_totalprice').innerHTML = txt_totalPrice + totalPrice.toFixed(2);
	
	
		
		
	}	
	
}

function removeProductFromBasket(productId)
{
	
	if(document.getElementById('carro_grande')){
		var productRow = document.getElementById('shopping_cart_items_productgrande' + productId);
		var numberOfItemCell = productRow.cells[5];
		var valor = productRow.cells[7].innerHTML.replace('$',"");
		var total = productRow.cells[9];
	if(numberOfItemCell.innerHTML == '1'){
		productRow.parentNode.removeChild(productRow);	
	}else{
		
		
		numberOfItemCell.innerHTML = numberOfItemCell.innerHTML/1 - 1;
		total.innerHTML="$"+numberOfItemCell.innerHTML/1 *valor;
	}
	}
	var productRow = document.getElementById('shopping_cart_items_product' + productId);

var numberOfItemCell = productRow.cells[0];
	if(numberOfItemCell.innerHTML == '1'){
		productRow.parentNode.removeChild(productRow);	
	}else{
		numberOfItemCell.innerHTML = numberOfItemCell.innerHTML/1 - 1;
	}

	
	
	
	
	
	updateTotalPrice();
	ajaxRemoveProduct(productId);

	
}


function eliminar_carro_contenido(ajaxIndex)
{
document.getElementById('shopping_cart').innerHTML="<table id='shopping_cart_items' class='letra' width='100%'></table>";
if(document.getElementById('carro_grande')){
document.getElementById('madre_carro').innerHTML="<table width='95%' border='0' align='center' cellpadding='0' cellspacing='0' id='carro_grande'> <tr>        <td bgcolor='#0D323E'><img src='../images/carro/barra_der.gif' width='5' height='27' /></td>        <td width='100' bgcolor='#0D323E' ><img src='../images/carro/tit_imagen.gif' width='62' height='27' /></td>        <td bgcolor='#0D323E' ><img src='../images/carro/barra_vert.gif' width='1' height='27' /></td>        <td width='100' bgcolor='#0D323E' ><img src='../images/carro/tit_producto.gif' width='70' height='27' /></td>        <td bgcolor='#0D323E' ><img src='../images/carro/barra_vert.gif' width='1' height='27' /></td>        <td width='100' bgcolor='#0D323E' ><img src='../images/carro/tit_cantidad.gif' width='73' height='27' /></td>        <td bgcolor='#0D323E' ><img src='../images/carro/barra_vert.gif' width='1' height='27' /></td>        <td width='100' bgcolor='#0D323E' ><img src='../images/carro/tit_precio.gif' width='73' height='27' /></td>        <td bgcolor='#0D323E' ><img src='../images/carro/barra_vert.gif' width='1' height='27' /></td>        <td width='50' bgcolor='#0D323E' ><img src='../images/carro/tit_subtotal.gif' width='73' height='27' /></td>        <td width='50' bgcolor='#0D323E' ><img src='../images/carro/barra_vert.gif' width='1' height='27' /></td>        <td width='100' bgcolor='#0D323E' ><div align='center'><img src='../images/carro/tit_quitar.gif' width='65' height='27' /></div></td>        <td bgcolor='#0D323E' ><div align='right'><img src='../images/carro/barra_izq.gif' width='4' height='27' /></div></td>      </tr>      <tr>        <td>&nbsp;</td>        <td>&nbsp;</td>        <td>&nbsp;</td>        <td>&nbsp;</td>        <td>&nbsp;</td>        <td>&nbsp;</td>        <td>&nbsp;</td>        <td>&nbsp;</td>        <td>&nbsp;</td>        <td colspan='3'>&nbsp;</td>        <td>&nbsp;</td>      </tr><tr id='total_grande'>        <td bgcolor='#0D323E'><img src='../images/carro/barra_der.gif' width='5' height='27' /></td>        <td bgcolor='#0D323E'>&nbsp;</td>        <td bgcolor='#0D323E'>&nbsp;</td>        <td bgcolor='#0D323E'>&nbsp;</td>        <td bgcolor='#0D323E'>&nbsp;</td>        <td bgcolor='#0D323E'>&nbsp;</td>        <td colspan='2' bgcolor='#0D323E'><div align='right'><img src='../images/carro/tit_total.gif' /></div></td>        <td bgcolor='#0D323E'>&nbsp;</td>        <td colspan='3' bgcolor='#0D323E' class='tit_azul'>$0</td>        <td bgcolor='#0D323E'><div align='right'><img src='../images/carro/barra_izq.gif' width='4' height='27' /></div></td>      </tr></table>";
//document.getElementById('carrito_compras').style.display="none";
 
}
ro=document.getElementById('total');
 ro.cells[1].innerHTML="0";
}





function ajaxValidateRemovedProduct(ajaxIndex)
{
	
	var productItems = ajaxObjects[ajaxIndex].response.split('|||');
    ro=document.getElementById('total');
	var total=productItems[2].split('.');
	inner=new Number(ro.cells[1].innerHTML);
	outer=new Number(total[0]);
	ro.cells[1].innerHTML=inner-outer;
	
		 if (document.getElementById('carro_grande')){
			   ro2=document.getElementById('total_grande');
			   ro2.cells[8].innerHTML='$'+ro.cells[1].innerHTML;
			   }
	
	if(ajaxObjects[ajaxIndex].response=='')alert('Error while removing product from the database');
		
}



function ajaxRemoveProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_removeProductFromBasket + '?productId=' + productId;	// Saving product in this file
	ajaxObjects[ajaxIndex].onCompletion = function(){ ajaxValidateRemovedProduct(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}

function ajaxAddProduct(productId)
{
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = url_addProductToBasket + '?productId=' + productId;	// Saving product in this file
	ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxBasketContent(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
}
function vercarro()
{
var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = ver_carro;	// Saving product in this file
	ajaxObjects[ajaxIndex].onCompletion = function(){ver_carrito(ajaxIndex); };	// Specify function that will be executed after file has been found
//		ajaxObjects[ajaxIndex].onLoading=function(){document.getElementById('cargando').style.display="block";};
	//	ajaxObjects[ajaxIndex].onLoaded=function(){document.getElementById('cargando').style.display="none";};
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		

}

function vuser(opt)
{
var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = valida_user+opt;	// Saving product in this file
	ajaxObjects[ajaxIndex].onCompletion = function(){vuser2(ajaxIndex); };	// Specify function that will be executed after file has been found
//		ajaxObjects[ajaxIndex].onLoading=function(){document.getElementById('cargando').style.display="block";};
	//	ajaxObjects[ajaxIndex].onLoaded=function(){document.getElementById('cargando').style.display="none";};
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		

}
function vuser2(ajaxIndex)
{
	
	
	if(ajaxObjects[ajaxIndex].response!="okkuj"){
document.getElementById('validar_user').innerHTML=ajaxObjects[ajaxIndex].response;
	}else{
	mostrar('main','personal');
		}

}



function vaciarcarro()
{
	
	if (confirm("Realmente desea vaciar el carro?")==true){
	
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = vaciarcarro_url;	// Saving product in this file
	ajaxObjects[ajaxIndex].onCompletion = function(){eliminar_carro_contenido(ajaxIndex); };	// Specify function that will be executed after file has been found
//		ajaxObjects[ajaxIndex].onLoading=function(){document.getElementById('cargando').style.display="block";};
	//	ajaxObjects[ajaxIndex].onLoaded=function(){document.getElementById('cargando').style.display="none";};
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
	}
}

function ch_vaciar()
{
	
	if (confirm("Realmente desea vaciar el carro?")==true){
	
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = vaciarcarro_url;	// Saving product in this file
	ajaxObjects[ajaxIndex].onCompletion = function(){eliminar_carro_contenido_ch(ajaxIndex); };	// Specify function that will be executed after file has been found
//		ajaxObjects[ajaxIndex].onLoading=function(){document.getElementById('cargando').style.display="block";};
	//	ajaxObjects[ajaxIndex].onLoaded=function(){document.getElementById('cargando').style.display="none";};
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function		
	}
}

function mostrar(target,file,op,metodo) 
{ 
if (metodo==null){
	metodo="GET";
	}else{
		metodo="POST";
		}

rand=new Date().getTime();
    document.getElementById(target).innerHTML = '<div class="load" align="center"><table width="50" border="0"><tr><td><img src="images/carro/indicator_azul.gif" /></td><td>Cargando...</td></tr></table></div>'; 
    var myConn = new XHConn(); 
        if (!myConn) alert("XMLHTTP no esta disponible. Inténtalo con un navegador más actual."); 
        var peticion = function (oXML) {  document.getElementById(target).innerHTML = oXML.responseText; }; 
        myConn.connect("include/"+file+".php", metodo, "op="+op+"&rand="+rand, peticion); 
		document.getElementById(target).style.display="block";
}

function p_mail(){
	
	var opt = "envio&email=" + encodeURI(document.getElementById('email').value);

mostrar('olvido','olvido',opt,'POST');
	}

	
	

function carro_especial(id){
	
		mostrar('desc_carro','des_carro',id);
	
	
	}
	
	
	function numeros(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

	


  var x = 10;
 var y = 1;
        function startClock(){
        x = x+y;
		
		
		
		document.getElementById('boton').value="Enviando....";
        document.getElementById('boton').disabled=true;
        setTimeout("startClock()", 1)
        if(x==100){
      listo();
         }else if (x<=100){
		document.getElementById("centro").width = x;
		 }
        }
		
		
		function listo(){
		
	/*this.document.getElementById("enviando").style.display="none";
	this.document.getElementById("listo").style.display="block";*/
	setTimeout("mostrar('olvido','olvido','ok');", 800)
			
		}


function img_cambio(numero){
	document.getElementById('txt_eval').value=numero;
	document.getElementById('img_numeros').src="../images/comentarios/"+numero+"_peces.gif"
	
	}
	
	function provincia(){
id_pais=document.getElementById('cmb_pais').value;
mostrar("estado_provincia","estado",id_pais);

	
	}
	
function mostrar_error(cod,act,cod2){
 if (act=='mostrar'){
 amar="#FFCCCC";

 document.getElementById(cod).style.background = amar;
  document.getElementById(cod).style.color = 'black';
 // document.getElementById(cod2).style.display = 'block'; 
document.getElementById('error').style.display = 'block'; 
 }else{
  document.getElementById(cod).style.background = "white";
  document.getElementById(cod).style.color = "black";
 //  document.getElementById(cod2).style.display = 'none'; 
     document.getElementById('error').style.display = 'none'; 
 }

 }

 

function validar2(){
errorMsg="";
mostrar_error('txt_review','ocultar','fname');
if (document.getElementById('txt_review').value==""){

errorMsg="error";
		mostrar_error('txt_review','mostrar','fname');	
}
	if (errorMsg != ""){

		return false;

		}else{
return true;


	}

  }
  
  
  function presiona(evt) {
	evt = (evt) ? evt : ((event) ? event : null);
	if (evt) {
		// gab code of what's typed
		var charCode = (evt.charCode) ? evt.charCode : evt.keyCode;
		
		if ( charCode == 13 ) {
			validar_user();
			return false;
		}
	}
}

function vlr(){
	
	
	
error="";
if (!(document.getElementById('txt_direccion'))){
mostrar_div('direccion_error');
error="error";
}
var combo;
if(document.getElementById('combo1').checked){
	combo=document.getElementById('combo1').value;
	}else if(document.getElementById('combo2').checked){
	combo=document.getElementById('combo2').value;
	}else if(document.getElementById('combo3').checked){
			combo=document.getElementById('combo3').value;
		
	}else{
		mostrar_div('combo_error');
		error="error";
		}

if (error==""){
	
	var opt = "?op=valida&direccion=" + encodeURI(document.getElementById('txt_direccion').value)+"&pago=" + encodeURI(combo)+"&comentario=" + encodeURI(document.getElementById('txt_comentario').value);
	mostrar('main','pedido2',opt,'POST');
	}
		
		
		

}

function mostrar_div(val){
	document.getElementById('cuadro_error').style.display="none";
	document.getElementById('combo_error').style.display="none";	
	document.getElementById('direccion_error').style.display="none";
	
	document.getElementById('cuadro_error').style.display="block";
	document.getElementById(val).style.display="block";
	}
function finalizar(){
	opt="?op=valida&direccion=" + encodeURI(document.getElementById('txt_direccion').value)+"&pago=" + encodeURI(document.getElementById('txt_pago').value)+"&comentario=" + encodeURI(document.getElementById('txt_comentario').value);
	mostrar('main','finalizado',opt,'POST');
	}
