//
// Funciones para el carro de compras
//
function finalizarCompraError(msg){
	alert(msg);
	return false;
}

function finalizarCompra(formulario){
	document.forms[formulario].submit();
	return false;
}

function submitAccion(accion,parametro){
	document.forms['formAction'].CMD.value = accion;
	document.forms['formAction'].PARAMETRO.value = parametro;
	document.forms['formAction'].submit();
	return true;
}

function popupRegaloDinero(nId) {
Dialog.regalo($('regalodinero').innerHTML, {className:"alphacube", width:400, 
                                      okImage: "images/bt-validation.gif", cancelImage: "images/bt-annulation.gif",
                                      onOk:function(win){
                                        //$('login_error_msg').innerHTML='Login or password inccorect('+nId+')';
                                        //$('login_error_msg').show(); 
                                        //Windows.focusedWindow.updateHeight();
                                        //new Effect.Shake(Windows.focusedWindow.getId());
										//return false;
										if (allRequiredRegaloDinero('Remplissez tous les champs svp.')){
											//Relleno directo de parámetros
											document.forms['formAction'].NOMBRE.value = document.getElementById('nom_dinero').value;
											document.forms['formAction'].APELLIDO.value = document.getElementById('prenom_dinero').value;
											document.forms['formAction'].DIRECCION.value = document.getElementById('adresse_dinero').value;
											document.forms['formAction'].CIUDAD.value = document.getElementById('ville_dinero').value;
											document.forms['formAction'].CODPOSTAL.value = document.getElementById('codepostal_dinero').value;
											document.forms['formAction'].MENSAJE.value = document.getElementById('message_dinero').value;
											document.forms['formAction'].IMPORTE.value = document.getElementById('prix_dinero').value;       
											//
											return submitAccion('ADDREGALODINERO',nId);	
										} else {
											return false;
										}
										
										}
										});
}

//Chequea que se hayan rellenado todos los datos en el formulario de regalo de dinero.
function allRequiredRegaloDinero(msg){
	
	var esMoneda = false;
	esMoneda = isMoneda(document.getElementById('prix_dinero').value);
		
	if (document.getElementById('nom_dinero').value != "" && document.getElementById('prenom_dinero').value != "" && document.getElementById('adresse_dinero').value != "" && document.getElementById('ville_dinero').value != "" && document.getElementById('codepostal_dinero').value != "" && document.getElementById('message_dinero').value != "" && (esMoneda == true && eval(document.getElementById('prix_dinero').value) > 0)){
		return true;		
	} else {
		if (esMoneda == false || eval(document.getElementById('prix_dinero').value) <= 0){
			alert("Vous devez saisir un prix au moins égale à un.");
		} else {
			alert(msg);	
		}
		return false;
	}

}

function popupRegalo(nId) {
Dialog.regalo($('login').innerHTML, {className:"alphacube", width:400, 
                                      okImage: "images/bt-offrir.jpg", cancelImage: "images/bt-poursuirve.gif",
                                      onOk:function(win){
                                        //$('login_error_msg').innerHTML='Login or password inccorect('+nId+')';
                                        //$('login_error_msg').show(); 
                                        //Windows.focusedWindow.updateHeight();
                                        //new Effect.Shake(Windows.focusedWindow.getId());
										//return false;
										if (allRequiredRegalo('Remplissez tous les champs svp.')){
											//Relleno directo de parámetros
											document.forms['formAction'].NOMBRE.value = document.getElementById('nom').value;
											document.forms['formAction'].APELLIDO.value = document.getElementById('prenom').value;
											document.forms['formAction'].DIRECCION.value = document.getElementById('adresse').value;
											document.forms['formAction'].CIUDAD.value = document.getElementById('ville').value;
											document.forms['formAction'].CODPOSTAL.value = document.getElementById('codepostal').value;
											document.forms['formAction'].MENSAJE.value = document.getElementById('message').value;     
											//
											return submitAccion('ADDREGALO',nId);	
										} else {
											return false;
										}
										
										}
										});
}

//Chequea que se hayan rellenado todos los datos en el formulario de regalo.
function allRequiredRegalo(msg){
	
	if (document.getElementById('nom').value != "" && document.getElementById('prenom').value != "" && document.getElementById('adresse').value != "" && document.getElementById('ville').value != "" && document.getElementById('codepostal').value != "" && document.getElementById('message').value != ""){
		return true;		
	} else {
		alert(msg);
		return false;
	}

}

//Chequea que se hayan rellenado todos los datos en el formulario de login.
function allRequiredLogin(msg){
	
	if (document.getElementById('usuario').value != "" && document.getElementById('password').value != ""){
		return true;		
	} else {
		alert(msg);
		return false;
	}

}

//Chequea que se hayan rellenado todos los datos en el formulario de Code Promo.
function allRequiredPromo(msg){
	
	if (document.getElementById('codepromo').value != ""){
		return true;		
	} else {
		alert(msg);
		return false;
	}

}
function isMoneda(numero){
    num = numero.toString().replace(/\$|\,/g,'');
	return !(isNaN(num));
}
function isDecimal(numero){
    num = numero.toString().replace(/\$|\./g,'');
	return !(isNaN(num));
}
function isNumero(numero){
    return /^([0-9])*$/.test(numero);
}
function calculaProducto(indexProducto){
	var cantidad = document.getElementById('cantidad'+indexProducto).value;
	if (isNumero(cantidad)){
		if (cantidad < 1){
			cantidad = document.getElementById('hCantidad'+indexProducto).value;
			document.getElementById('cantidad'+indexProducto).value = cantidad;
			alert("Vous devez saisir une quantité au moins égale à un.");		
		}
	} else {
		cantidad = document.getElementById('hCantidad'+indexProducto).value;
		document.getElementById('cantidad'+indexProducto).value = cantidad;
		alert("Vous devez saisir une quantité au moins égale à un.");
	}
	document.getElementById('total'+indexProducto).innerHTML = formatMoneda(eval(cantidad) * formatDecimal(document.getElementById('hPrecio'+indexProducto).value))+ " €";
	document.getElementById('hCantidad'+indexProducto).value = cantidad;
}
function calculaImporte(indexProducto){
	var cantidad = document.getElementById('cantidad'+indexProducto).value;
	var importe = document.getElementById('hPrecio'+indexProducto).value;
	if (isDecimal(importe) || isMoneda(importe) ){
		if (importe <= 0){
			importe = document.getElementById('backPrecio'+indexProducto).value;
			document.getElementById('hPrecio'+indexProducto).value = importe;
			alert("Vous devez saisir un prix au moins égale à un.");		
		}
	} else {
		importe = document.getElementById('backPrecio'+indexProducto).value;
		document.getElementById('hPrecio'+indexProducto).value = importe;
		alert("Vous devez saisir un prix au moins égale à un.");
	}
	document.getElementById('total'+indexProducto).innerHTML = formatMoneda(eval(cantidad) * formatDecimal(document.getElementById('hPrecio'+indexProducto).value))+ " €";
	document.getElementById('backPrecio'+indexProducto).value = formatDecimal(importe);
	document.getElementById('hPrecio'+indexProducto).value = formatDecimal(importe);
}	
function formatMoneda(num){
	var num = new Number(eval(num));
	var sNum = "" + num.toFixed(2);
	return sNum.replace(".",","); 
}
function formatDecimal(num){
	var num = new Number(eval(num.replace(",",".")));
	var sNum = "" + num.toFixed(2);
	return sNum; 
}
function calculaTotales(){
	var cantidadProductos = document.getElementById('cantidadProductos').value;
	var totales = 0;
	for (var i=0;i<cantidadProductos;i++){
		var total = document.getElementById('total'+i).innerHTML.replace(" €", "");
		totales += eval(total.replace(",", "."));
	}
	//document.getElementById('subTotal').innerHTML = formatMoneda(totales) + " €";
	document.getElementById('total').innerHTML = formatMoneda(totales) + " €";
}
/*
function actualizaCantidad(index){
	
	cantidad = eval(document.getElementById('cantidad'+index).value.replace(",", "."));
	document.getElementsByName('quantity_'+(index+1))[0].value = cantidad;
	
	$.ajax({
	    url: 'saveCantidad.php',
	    type: 'POST',
	    data: 'nId='+index+"&nCantidad="+cantidad,
	    success: function(msg){
			//alert("OK:"+msg);
		},
		error: function(msg){
			//alert("error:"+msg);
		}
	});
}
function actualizaImporte(index){
	
	var $j = jQuery.noConflict();
	
	importe = eval(document.getElementById('hPrecio'+index).value.replace(",", "."));
	document.getElementsByName('amount_'+(index+1))[0].value = importe;
	
	$j.ajax({
	    url: 'saveImporte.php',
	    type: 'POST',
	    data: 'nId='+index+"&nImporte="+importe,
	    success: function(msg){
			//alert("OK:"+msg);
		},
		error: function(msg){
			//alert("error:"+msg);
		}
	});
	
}
*/
function AJAXCrearObjeto(){
  	var obj;
  	if (window.XMLHttpRequest) { // no es IE
   		obj = new XMLHttpRequest();
  	}
  	else { // Es IE o no tiene el objeto
	   	try {
	    	obj = new ActiveXObject("Microsoft.XMLHTTP");
	   	}
	   	catch (e) {
	    	alert('El navegador utilizado no está soportado');
	   	}
  	}
	return obj;
}
function actualizaCantidad(index){
	
	cantidad = eval(document.getElementById('cantidad'+index).value.replace(",", "."));
	document.getElementsByName('quantity_'+(index+1))[0].value = cantidad;
	
	oXML = AJAXCrearObjeto();
	oXML.open('GET','saveCantidad.php?nId='+index+"&nCantidad="+cantidad, true);
	oXML.send(null);
	// Devolvemos true
	return true;
}
function actualizaImporte(index){
	
	importe = eval(document.getElementById('hPrecio'+index).value.replace(",", "."));
	document.getElementsByName('amount_'+(index+1))[0].value = importe;
	
	oXML = AJAXCrearObjeto();
	oXML.open('GET','saveImporte.php?nId='+index+"&nImporte="+importe, true);
	oXML.send(null);
	// Devolvemos true
	return true;
}
function ocultarError(){
	return true; 
} 
window.onerror = ocultarError;  
