
//********************************** 	SUMAR Y RESTAR Y CONTROLAR CANTIDAD DE PRODUCTOS	*****************************************//
function controlKeyPress(e){
	if (  e.keyCode < 48 || e.keyCode > 57 ) 
		if ( e.keyCode != 44 && e.keyCode != 46 )
			e.returnValue = false;
}
function stringToInt(valor){
	var v = parseFloat(valor.replace(",","."))
	v = ( isNaN(v) ) ? 1:v;
	return v;
}
function compruebaValor(obj,incremento,minValor, avis, atencio){
	if (typeof(obj) != "object") alert("parametro obj tiene que ser el elemento sobre el que se actua [function compruebaValor]");
	var valor = stringToInt(obj.value);
	valorIni = valor;
	var inc = stringToInt(incremento);
	var minV = stringToInt(minValor);
	valor = ( valor < minV) ? minV:valor;
	var resto = valor%inc
	var result = ((valor-resto)/inc)
	result = ((result+1)*inc);
	valor = ( resto != 0 ) ?  result :valor;
	obj.value = valor;

	if(valorIni!=valor) { alertFacil(atencio, avis); }
}

function sumar(id,incremento,minValor,avis, atencio){
	var v = _getValor(id);
	valorIni = v;
	var inc = stringToInt(incremento);
	var minV = stringToInt(minValor);
	_setValor(id,v+inc)
}

function restar(id,incremento,minValor,avis, atencio){
	var v = _getValor(id);
	var inc = stringToInt(incremento);
	var minV = stringToInt(minValor);
	incremento = stringToInt(incremento);
	v = ( v-inc < minV) ? minV:v-inc;
	_setValor(id, v)
	if(v==minValor) { alertFacil(atencio, avis); }
}

function _getValor(id){
	return parseFloat($("input[id='quantitat"+ id+ "']").val().replace(",","."));
}
function _setValor(id,v){
	$("input[id='quantitat"+ id+ "']").val(v.toString())
}

//*************************************************************************************************************//
//*************************************************************************************************************//

 
function _showLoading(id){
	$('#'+id).prepend("<div class='loading' id='load_'"+id+"><img border='0' src='aspimagina/comu/images/ajax-loader.gif' alt='' /></div>")

}
function _removeLoading(id){
	$("#load_"+id).remove();
}
function _printResult(idDiv,rObj){
	if (rObj.idError != 0 && rObj.idError < 100 )
			alert("err["+rObj.idError+"]: " + _errTags[parseInt(rObj.idError)]);
	else if (rObj.idError > 100)
			alert("err["+rObj.idError+"]: " + _statusReturn[rObj.idError]);
	else
		$('#'+idDiv).empty().html(rObj.resultat);
}
function _getParamsCompra(id){
		var sendObjetc = new carritoJsonSendParams()
		sendObjetc.idGrupProducte = id
		var tempAttr = new Array();
		var attr = $("input[name='atributsProducteId']")
		attr.each( function(){
					var value = $(this).val();
					if( value.length > 0 ) tempAttr.push(value);
					}
			)

		sendObjetc.idsAtributs = "";
		if ( tempAttr.length > 0) sendObjetc.idsAtributs = escape(tempAttr);
		sendObjetc.unitat = $("input[id='unitat"+ sendObjetc.idGrupProducte + "']").val();
		sendObjetc.quantitat = $("input[id='quantitat"+ sendObjetc.idGrupProducte + "']").val();		
		//debug("quantitat: " + sendObjetc.quantitat,true)
		
		return sendObjetc;
}





/* FUNCIONES CLIENTE PARA RECOGER LOS DATOS A PASAR
*
*
*
*/
function recojeIds(){
	var arrTemp = new Array
	var radios = $('input[type=radio]:checked');

	radios.each(function(){
		arrTemp.push($(this).attr("id"))				
		 })

	return arrTemp.join(',')

}
	
	
function recojeValors(){

	var arrTemp = new Array
	var radios = $('input[type=radio]:checked');

	radios.each(function(){
		arrTemp.push($(this).val())				
		})

	return arrTemp.join(',')
	}
	
	
	
function calculaPrecioTotal(){
	if(document.getElementById("precioBase")) return document.getElementById("precioBase").value;
}


function actualitzaPreu(accio, preu, moneda){

	if(!document.getElementById("precioInicial")) return false;
	
	actual = document.getElementById("precioInicial").value;

	switch(accio)
	{
		case "+":

			document.getElementById("precioBase").value = parseFloat(actual) + parseFloat(preu);
			document.getElementById("preuBase").innerHTML = document.getElementById("precioBase").value 
			
		break;
		case "=":
			document.getElementById("precioBase").value = parseFloat(preu);
			document.getElementById("preuBase").innerHTML = document.getElementById("precioBase").value
		break;
		
	}
}


function miraObligatoris(textAlert){
	
	retornem = true;
	var camps;
	var alerta = "";
	var contador = 0;
	radios = $('input[obligatori=1]')
	radios.each( function (){
		if(this.value=='')
		 {
			 camps = this.id
			 camps = camps.substr(2, camps.length - 40)
			 if(textAlert!=''){
				if(contador==0)
					alerta = textAlert + '' + camps;
				else
					alerta = alerta + ' y ' + camps;
				}
			 retornem = false; 
		 }
		 contador++;
	})	

   if(!retornem){
	idElem = "idOblig" + Math.round(Math.random()*10);
	obj = mostraConfirm('!', alerta, idElem);
	obj.dialog('option', 'buttons',{ "ok": function() { $(this).dialog("close");$(this).dialog('destroy'); $(this).empty();} });
	obj.dialog( 'open' );
	}

	return retornem;
}
/* FIN FUNCIONES CLIENTE PARA RECOGER LOS DATOS A PASAR
*
*
*
*/

