function carritoCantidad( cant ) {
	var nCant = document.getElementById( "celdaCnt" );
	if ( nCant != null ) {
		nCant.innerHTML = cant;
	}
}

function carritoImporte( imp ) {
	var nImp = document.getElementById( "celdaImp" );
	if ( nImp != null ) {
		nImp.innerHTML = imp;
	}
}

function resultadoABM( req ) {
	eval( req.xhRequest.responseText );
	carritoCantidad( cantTotal );
	carritoImporte( req.userData.simbolo + ' ' + impTotal );
	if ( req.userData.msgOk != '' ) {
		alert( req.userData.msgOk );
	}
	if ( req.userData.reload ) {
		window.location.reload();
	}
}

function errorServidor( req ) {
	alert( 'Error' );
}

function carrito_agrega( elemento, valor, estruct ) {
	var col, ldescrip, campo;
	var jsonValores = '{"ROWCOUNT":1,"COLUMNS":["' + this.columns.join('","') + '"],"DATA":{';

	for ( var i in this.columns ) {
		col = this.columns[ i ];
		ldescrip = this.descrip[ i ];
		campo = this.campos[ i ];
		switch ( col ) {
			case 'cont':
				jsonValores += '"cont":[' + elemento + ']';
				break;
			case 'cant':
				jsonValores += ',"cant":[1]';
				break;
			case 'imp':
				jsonValores += ',"imp":[' + valor + ']';
				break;
			case 'struct':
				jsonValores += ',"struct":[' + estruct + ']';
				break;
			default:
				var oInput = document.Carrito[ campo + elemento ];
				if ( typeof oInput != 'undefined' ) {
					if ( ldescrip ) {
						var nSel = oInput.selectedIndex
						jsonValores += ',"' + col + '":["' + oInput.options[ nSel ].text + '"]';
					}
					else {
						jsonValores += ',"' + col + '":["' + oInput.value + '"]';
					}
				}
				else {
					jsonValores += ',"' + col + '":[""]';
				}
				break;
		}
	}
	jsonValores += '}}';

	var oMensajes = new Object;
	oMensajes.msgOk = this.txt_agregado;
	oMensajes.simbolo = this.simbolo;
	oMensajes.reload = false;

	var req = Spry.Utils.loadURL("POST", "/a2/objetos/carrito/index.cfm?fuseaction=AGREGA", true, resultadoABM, { postData: "aplicacion=" + this.nombre + "&codaplicacion=" + this.codigo + "&elemento=" + jsonValores, headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" }, errorCallback: errorServidor, userData: oMensajes });
}

function carrito_elimina( oElemento ) {
	if ( ! confirm( this.txt_elimina ) ) {
		return false;
	}
	var col;
	var jsonValores = '{"ROWCOUNT":1,"COLUMNS":["' + this.columns.join('","') + '"],"DATA":{';

	for ( var i in this.columns ) {
		col = this.columns[ i ];
		//campo = this.campos[ i ];
		switch ( col ) {
			case 'cont':
				jsonValores += '"cont":[' + oElemento.cont + ']';
				break;
			case 'cant':
				jsonValores += ',"cant":[-' + oElemento.cant + ']';
				break;
			case 'imp':
				jsonValores += ',"imp":[' + oElemento.imp + ']';
				break;
			default:
				if ( oElemento[ col ] != null ) {
					jsonValores += ',"' + col + '":["' + oElemento[ col ] + '"]';
				}
				else {
					jsonValores += ',"' + col + '":[""]';
				}
				break;
		}
	}
	jsonValores += '}}';
	var oMensajes = new Object;
	oMensajes.msgOk = this.txt_eliminado;
	oMensajes.simbolo = this.simbolo;
	oMensajes.reload = true;

	var req = Spry.Utils.loadURL("POST", "/a2/objetos/carrito/index.cfm?fuseaction=AGREGA", true, resultadoABM, { postData: "aplicacion=" + this.nombre + "&codaplicacion=" + this.codigo + "&elemento=" + jsonValores, headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" }, errorCallback: errorServidor, userData: oMensajes });
}

function carrito_cambiaCant( oElemento, cant ) {
	var col, campo;
	var jsonValores = '{"ROWCOUNT":1,"COLUMNS":["' + this.columns.join('","') + '"],"DATA":{';

	for ( var i in this.columns ) {
		col = this.columns[ i ];
		campo = this.campos[ i ];
		switch ( col ) {
			case 'cont':
				jsonValores += '"cont":[' + oElemento.cont + ']';
				break;
			case 'cant':
				jsonValores += ',"cant":[' + ( cant - oElemento.cant ) + ']';
				break;
			case 'imp':
				jsonValores += ',"imp":[' + oElemento.imp + ']';
				break;
			default:
				if ( oElemento[ col ] != null ) {
					jsonValores += ',"' + col + '":["' + oElemento[ col ] + '"]';
				}
				else {
					jsonValores += ',"' + col + '":[""]';
				}
				break;
		}
	}
	jsonValores += '}}';

	var oMensajes = new Object;
	oMensajes.msgOk = '';
	oMensajes.simbolo = this.simbolo;
	oMensajes.reload = false;

	var req = Spry.Utils.loadURL("POST", "/a2/objetos/carrito/index.cfm?fuseaction=AGREGA", true, resultadoABM, { postData: "aplicacion=" + this.nombre + "&codaplicacion=" + this.codigo + "&elemento=" + jsonValores, headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8" }, errorCallback: errorServidor, userData: oMensajes });
}

function carrito_confirmaCompra( cUrl ) {
	if ( this.cantidad == 0 ) {
		alert( this.txt_vacio );
	}
	else {
		window.location = cUrl;
	}
}

function buscaArray( array, elemento ) {
	var i;
	for ( i in array ) {
		if ( elemento == array[ i ] ) {
			return i;
		}
	}
	return -1;
}

function carrito_setCol( cCol, lDescrip ) {
	var nitem = buscaArray( [ this.campos ], [ cCol ] );

	if ( nitem == -1 ) {
		this.columns = this.columns.concat( cCol );
		this.descrip = this.descrip.concat( false );
		this.campos = this.campos.concat( cCol );
		if ( lDescrip ) {
			this.columns = this.columns.concat( cCol + 'descr' );
			this.descrip = this.descrip.concat( true );
			this.campos = this.campos.concat( cCol );
		}
	}
}

function carrito( nombre, codigo, simbolo ) {
	this.nombre = nombre;
	this.codigo = codigo;
	this.cambiaCant = carrito_cambiaCant;
	this.elimina = carrito_elimina;
	this.agrega = carrito_agrega;
	this.confirmaCompra = carrito_confirmaCompra;
	this.setCol = carrito_setCol;
	this.simbolo = simbolo;
	this.columns = ['cont','cant','imp','struct'];
	this.descrip = [false,false,false,false];
	this.campos = ['','','',''];
	this.txt_agregado = 'Se ha agregado 1 elemento al carrito.';
	this.txt_elimina = 'Elimina este elemento?';
	this.txt_eliminado = 'Se ha quitado el elemento del carrito.';
	this.txt_vacio = 'No hay elementos seleccionados.';
}
