// JavaScript Document
function select_open(recibo){
	obj=document.getElementById(recibo)
	if(obj.style.display=="none"){
		document.getElementById(recibo).style.display="block";
	}else{
		document.getElementById(recibo).style.display="none";
	}
}

function test_correo(elcorreo){
	correcto=false
	if(elcorreo.indexOf("@")!=-1){
		splitcorreo=elcorreo.split("@")
		if(splitcorreo.length>1 && splitcorreo[0].length>0 && splitcorreo[1].length>1){
			eldominio=splitcorreo[1]
			if(eldominio.indexOf(".")!=-1){
				splitdominio=eldominio.split(".")
				//if(splitdominio.length>1  && splitdominio[1].length>1 && splitdominio[0].length>0) correcto=true;
				if(splitdominio.length>1  && splitdominio[splitdominio.length-1].length>1 && splitdominio[0].length>0) correcto=true;
			}
		}
	}
	return correcto
}

function debugar(recibo){
	document.getElementById("debug").innerHTML=recibo;	
	document.getElementById("debug").style.display="block";
}

function know_scroll(){
	if (window.pageYOffset){
        Scrollvent = window.pageYOffset;
    }else{
        Scrollvent = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
	return Scrollvent;
}

function know_heightcontent(){
	valor=document.getElementById("main").offsetHeight
	valor=parseInt(valor);
	return valor
}


function open_coverconten(){
	obj=document.getElementById("coverconten");
	altocontenido=know_heightcontent();
	altocliente=document.documentElement.clientHeight
	if(altocontenido>=altocliente){
		obj.style.height=altocontenido+"px"
	}else{
		obj.style.height=altocliente+"px"
	}
	obj.style.display="block"
}
function close_coverconten(){
	document.getElementById("coverconten").style.display="none"
}

function close_popup(){
	document.getElementById("layerpopup").style.display="none"
	close_coverconten()
}

function open_popup(){
	open_coverconten()
	obj=document.getElementById("layerpopup")
	obj.style.display="block"
	theleft=(document.documentElement.clientWidth/2)-(obj.offsetWidth/2);
	theleft=parseInt(theleft);
	thetop=(document.documentElement.clientHeight/2)-(obj.offsetHeight/2);
	//thetop=100;
	thetop +=know_scroll()
	thetop=parseInt(thetop);
	obj.style.left=theleft+"px";
	obj.style.top=thetop+"px";
}

function open_loginpopup(){
	open_coverconten()
	$.get("pop_login.php",{}, function(data){
		document.getElementById("layerpopup").innerHTML=data;
		Cufon.replace('.eideticfont');
		open_popup()
		document.loginformpop.email.focus();
	 });	
}

function do_login(){
	objform=document.loginformpop
	if(objform.email.value.length==0 || !test_correo(objform.email.value)){
		if(idioma==1) alert("Introduzca un E-MAIL válido")	
		if(idioma==2) alert("Insert a valid E-MAIL")	
		return;
	}
	if(objform.password.value.length==0 ){
		if(idioma==1) alert("Introduzca una CONTRASEÑA")
		if(idioma==2) alert("Insert a PASSWORD")
		return;
	}
	$.get("login_do.php",{email:objform.email.value, password:objform.password.value}, function(data){
		if(data.errores!=0){
			alert(data.mensaje)
		}else{
			document.location.href="privada/";
		}
	 },"json");
}

function open_forgetpopup(){
	open_coverconten()
	$.get("pop_olvido.php",{}, function(data){
		document.getElementById("layerpopup").innerHTML=data;
		Cufon.replace('.eideticfont');
		open_popup()
	 });	
}

function do_forgetpasword(){
	objform=document.passwordforgetform
	if(objform.email.value.length==0 || !test_correo(objform.email.value)){
		if(idioma==1) alert("Introduzca un E-MAIL válido")	
		if(idioma==2) alert("Insert a valid E-MAIL")	
		return;
	}
	$.get("password_new.php",{email:objform.email.value}, function(data){
		if(data.errores!=0){
			alert(data.mensaje)
		}else{
			//document.getElementById("popup_content").innerHTML='<div style="text-align:center">'+data.mensaje;+'</div>';
			close_popup()
		}

	 },"json");
	
}

function open_cesta_popup(){
	open_coverconten()
	$.get("pop_cesta_confirm.php",{}, function(data){
		document.getElementById("layerpopup").innerHTML=data;
		Cufon.replace('.eideticfont');
		open_popup()
	 });	
}

function test_pedido(code_prod,tipo){
	$.get("JQ_test_pedido.php",{elid:code_prod, eltipo:tipo}, function(data){
		if(data==0 || data==1){
			if(data==0) open_pop_add_carrito(code_prod,tipo);
			if(data==1) add_carrito(code_prod,tipo,0);
		}else{
			alert(data);	
		}
	 });
}

function open_pop_add_carrito(code_prod,tipo){
	open_coverconten()
	$.get("pop_carrito_add.php",{elid:code_prod, eltipo:tipo}, function(data){
		document.getElementById("layerpopup").innerHTML=data;
		Cufon.replace('.eideticfont');
		open_popup()
	 });	
}

function oncesta_pop_add(recibo,tipo,es_millar){
	objform=document.formpopaddcesta
	//es_millar=0;
	//if(	objform.Tipo_Precio.value=="millar") es_millar=1;
	
	tx_cantidad_unidad_venta=objform.cantidad_univad_venta.value
	if(tx_cantidad_unidad_venta==""){
		num_cantidad_unidad_venta=1;
	}else{
		if(isNaN(tx_cantidad_unidad_venta)){
			tx_cantidad_unidad_venta=tx_cantidad_unidad_venta.replace(/\./gi,"");
			tx_cantidad_unidad_venta=tx_cantidad_unidad_venta.replace(/\,/gi,".");
		}
		if(isNaN(tx_cantidad_unidad_venta)){
			alert("Inserte un valor numérico");
			return
		}
		num_cantidad_unidad_venta=parseInt(tx_cantidad_unidad_venta)
	}
	if(num_cantidad_unidad_venta<1) num_cantidad_unidad_venta=1;
	
	tx=objform.UMV.value
	tx=tx.replace(/\./gi,"");
	tx=tx.replace(/\,/gi,".");
	unidaminima=parseFloat(tx);
	
	tx_cantidad_tipo_precio=objform.cantidad_tipo_precio.value
	if(tx_cantidad_tipo_precio==""){
		num_cantidad_tipo_precio=num_cantidad_unidad_venta*unidaminima;
	}else{
		if(isNaN(tx_cantidad_tipo_precio)){
			tx_cantidad_tipo_precio=tx_cantidad_tipo_precio.replace(/\./gi,"");
			tx_cantidad_tipo_precio=tx_cantidad_tipo_precio.replace(/\,/gi,".");
		}
		if(isNaN(tx_cantidad_tipo_precio)){
			alert("Inserte un valor numérico");
			return
		}
		num_cantidad_tipo_precio=parseFloat(tx_cantidad_tipo_precio)
	}
	
	if(num_cantidad_tipo_precio<unidaminima) num_cantidad_tipo_precio=unidaminima;
	
	if(tipo==1){
		objform.cantidad_univad_venta.value=FormatOutput(num_cantidad_unidad_venta,2)
		if(es_millar==1){
			decimales=3
		}else{
			decimales=2
		}
		objform.cantidad_tipo_precio.value=$().number_format(unidaminima*num_cantidad_unidad_venta, {numberOfDecimals:decimales,decimalSeparator: ',',thousandSeparator: '.'});
		
	}
	
	if(tipo==2){
		num_cantidad_unidad_venta=parseInt(num_cantidad_tipo_precio/unidaminima);
		objform.cantidad_univad_venta.value=FormatOutput(num_cantidad_unidad_venta,2)
		if(es_millar==1){
			decimales=3
		}else{
			decimales=2
		}
		objform.cantidad_tipo_precio.value=$().number_format(unidaminima*num_cantidad_unidad_venta, {numberOfDecimals:decimales,decimalSeparator: ',',thousandSeparator: '.'});
	}
	
	para_multiplicar=unidaminima*num_cantidad_unidad_venta;
	txpreciocompra=objform.Precio_Venta.value;
	txpreciocompra=txpreciocompra.replace(/\./gi,"");
	txpreciocompra=txpreciocompra.replace(/\,/gi,".");
	txpreciocompra=txpreciocompra.replace(/ &euro;/gi,"");
	numtxpreciocompra=parseFloat(txpreciocompra);
	subtotal=para_multiplicar*numtxpreciocompra
	//if(es_millar==1) subtotal=subtotal/1000
	
	document.getElementById("numsubtotal").innerHTML=FormatOutput(subtotal,2);
}

function pop_add_agregar(code_prod,tipo){
	objform=document.formpopaddcesta
	
	tx_cantidad_unidad_venta=objform.cantidad_univad_venta.value
	if(tx_cantidad_unidad_venta==""){
		num_cantidad_unidad_venta=1;
	}else{
		if(isNaN(tx_cantidad_unidad_venta)){
			tx_cantidad_unidad_venta=tx_cantidad_unidad_venta.replace(/\./gi,"");
			tx_cantidad_unidad_venta=tx_cantidad_unidad_venta.replace(/\,/gi,".");
		}
		if(isNaN(tx_cantidad_unidad_venta)){
			alert("Inserte un valor numérico");
			return
		}
		num_cantidad_unidad_venta=parseInt(tx_cantidad_unidad_venta)
	}
	if(num_cantidad_unidad_venta<1) num_cantidad_unidad_venta=1;
	add_carrito(code_prod,tipo,num_cantidad_unidad_venta);
	close_popup()
}
//FAVORITOS///////////////////////////////////////////////////
function test_favorito(code_prod,tipo,carpeta){
	$.get("JQ_test_favorito.php",{elid:code_prod, lacarpeta:carpeta}, function(data){
		if(data==0 || data==1){
			if(data==0) open_pop_add_favoritos(code_prod,tipo,carpeta);
			if(data==1){
				if(carpeta==0){
					if(confirm("Se eliminará el favorito de todas sus carpetas ¿CONTINUAR?")){
						add_favoritos(code_prod,tipo,carpeta,"");
					}
				}else{
					add_favoritos(code_prod,tipo,carpeta,"");
				}
			}
		}else{
			alert(data);	
		}
	 });
}

function open_pop_add_favoritos(code_prod,tipo,carpeta){
	open_coverconten()
	$.get("pop_favoritos_add.php",{elid:code_prod, eltipo:tipo,lacarpeta:carpeta}, function(data){
		document.getElementById("layerpopup").innerHTML=data;
		Cufon.replace('.eideticfont');
		open_popup()
	 });	
}

function oncheck_pop_favoritos(recibo){
	if(recibo==0 || recibo==2){
		document.getElementById("carpetafavo").selectedIndex=0	
	}
}

function onselect_pop_favoritos(){
	if(document.getElementById("carpetafavo").selectedIndex==0){
		
		document.getElementById("radiocarpeta_1").checked=true
		document.getElementById("radiocarpeta_2").checked=false
		document.getElementById("radiocarpeta_3").checked=false
	}else{
		document.getElementById("radiocarpeta_1").checked=false
		document.getElementById("radiocarpeta_2").checked=true
		document.getElementById("radiocarpeta_3").checked=false
	}
}

function send_add_favoritos(code_prod,tipo){
	if(document.getElementById("radiocarpeta_1").checked==true){
		carpeta_s=0;
		nuevacarpeta_s="";
	}
	if(document.getElementById("radiocarpeta_2").checked==true){
		if(document.getElementById("carpetafavo").selectedIndex==0){
			alert("Debe seleccionar una de sus carpetas")
			return;
		}else{
			carpeta_s=document.getElementById("carpetafavo").value;
			nuevacarpeta_s="";
		}
	}
	
	if(document.getElementById("radiocarpeta_3").checked==true){
		if(document.getElementById("nuevacarpeta").value.length==0){
			alert("Debe introducir un nombre para la nueva carpeta")
			return;
		}else{
			carpeta_s=0;
			nuevacarpeta_s=document.getElementById("nuevacarpeta").value;
		}
	}
	close_popup()
	add_favoritos(code_prod,tipo,carpeta_s,nuevacarpeta_s)
}

function open_pop_addcarpeta(){
	open_coverconten()
	$.get("pop_favoritos_add_carpeta.php",{}, function(data){
		document.getElementById("layerpopup").innerHTML=data;
		Cufon.replace('.eideticfont');
		open_popup()
	 });	
}

function FormatOutput(iValue, nDec) {
	var bIsNegative = 0;		// Flag por si valor es negativo
	var iPos = 0;				// 
	var sChar = "";				// Variable para almacenar carácter tratado
	var sTempNumber = "";
	var sNoChars = "";
	var sDollars = "";
	var sCents = "";
	var sDollarAmount = "";
	var sFormated = "";
	var x = 0;
	var IndexOfDec = -1;
	var rounded;

	if (iValue != "") {			// Si el valor es diferente a ""

		sTempNumber = iValue + "";												// Pasamos valor
		if (sTempNumber.charAt(0) == "-") {										// Si contiene el valor "-" en la primera posición
			bIsNegative = 1;													// Activamos Flag negativo
			sTempNumber = sTempNumber.substring(1, sTempNumber.length);			// Pasamos el valor sin negativo		
		}

		//sTempNumber
		IndexOfDec = sTempNumber.indexOf(".");		// Pasamos la posición en la que se encuentra el primer "." (Si lo hay, si no devuelve -1)

		if (IndexOfDec == -1) {						
			sDollars = sTempNumber;
			sCents = "00";
		}
		else if (IndexOfDec == 0) {
			sDollars = "0";
			sCents = sTempNumber.substring(IndexOfDec + 1, sTempNumber.length);
		}
		else {
			sDollars = sTempNumber.substring(0, IndexOfDec);
			if (IndexOfDec == (sTempNumber.length - 1)) {
				sCents = "00";
			}
			else {
				sCents = sTempNumber.substring(IndexOfDec + 1, sTempNumber.length)
				sCents += "0";
				sCents = sCents.charAt(0) + sCents.charAt(1);
			}
		}

		sFormated = sDollars;
		x = sDollars.length;
		iPos = 0;
		while (x > 0) {
			x--;
			sChar = sDollars.charAt(x);
			rounded = Math.round(iPos/3);
			if ( (iPos/3 == rounded ) & (iPos != 0) ) {
				sDollarAmount = "." + sDollarAmount;
			}
			sDollarAmount = sChar +  sDollarAmount;
			iPos++;
		}

		if (nDec) {
		  if (bIsNegative) {
			  sFormated = "-" + sDollarAmount + "," + sCents + "";
		  }
		  else {
			  sFormated = sDollarAmount + "," + sCents  + "";
		  }
	}
		else {
		  if (bIsNegative) {
			  sFormated = "-" + sDollarAmount + "";
		  }
		  else {
			  sFormated = sDollarAmount + "";
		  }
	}

		return (sFormated);
	}
	else {

		return("0" + "");
	}
}
