function rightText( nlength, ndecimals, ntxt ) {
	var xtxt, ntxt;
	ntxt = Math.round( ntxt * Math.pow( 10, ndecimals ) );
	xtxt = ntxt.toString();
	while ( xtxt.length < nlength ) {
		xtxt = ' ' + xtxt;
	}
	if ( ndecimals != 0 ) {
		while ( xtxt.indexOf( ' ', nlength - ndecimals - 1 ) > 0 ) {
			xtxt = xtxt.substring( 0, nlength - ndecimals - 1 )
				+ xtxt.substring( nlength - ndecimals - 1, nlength ).replace( / /, '0' );
		}
		xtxt = xtxt.substring( 1, nlength - ndecimals ) + '.' + xtxt.substring( nlength - ndecimals, nlength );
	}
	return xtxt;
}

function setCookie( name, value, expiredays ) {
	var exdate = new Date();
	exdate.setDate( exdate.getDate() + expiredays );
	var curCookie = name + "=" + escape( value )
		+ ( ( expiredays == null ) ? "" : ";expires=" + exdate );
	if ( ( name + "=" + escape( value ) ).length <= 4000 ) {
		document.cookie = curCookie;
	}
	else if ( confirm( "Cookie exceeds 4KB and will be cut!" ) ) {
		document.cookie = curCookie;
	}
}

function getCookie( name ) {
	var prefix = name + "=";
	var cookieStartIndex = document.cookie.indexOf( prefix );
	if ( cookieStartIndex == -1 ) {
		return null;
	}
	var cookieEndIndex = document.cookie.indexOf( ";", cookieStartIndex + prefix.length );
	if ( cookieEndIndex == -1 ) {
		cookieEndIndex = document.cookie.length;
	}
	if ( cookieStartIndex + prefix.length == cookieEndIndex ) {
		return null;
	}
	return unescape( document.cookie.substring( cookieStartIndex + prefix.length, cookieEndIndex ) )
}

function deleteCookie( name ) {
	if ( getCookie( name ) ) {
		document.cookie = name + "=" + 
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function buscaArray( array, elemento ) {
	var i, j, ligual, col;
	for ( i = 0; i < array[ 0 ].length; i ++  ) {
		ligual = true;
		for ( j = 0; j < elemento.length; j ++ ) {
			if ( elemento[ j ] != array[ i ][ j ] ) {
				ligual = false;
				break;
			}
		}
		if ( ligual ) {
			return i;
		}
	}
	return -1;
}

function getCarrito( cName ) {
	var aReturn;
	var sDatos = getCookie( cName );
	if ( sDatos == null ) {
		sDatos = '{"ROWCOUNT":0,"COLUMNS":["CANT","IMP","CONT"],"DATA":{"CANT":[],"IMP":[],"CONT":[]}}';
	}
	eval( 'aReturn = ' + sDatos );
	return aReturn;
}

function carrito_agrega( elemento, importe ) {
	if ( navigator.cookieEnabled == 0 ) {
  	alert( "Necesita habilitar las cookies\npara poder usar el carrito!" );
		return false;
	}

	var aElementos, aCantidades, aImportes, nitem, aDatos, sDatos;

	aDatos = getCarrito( this.name );

	aElementos = [];
	aBuscar = [];
	for ( i = 0; i < elemento.length; i ++ ) {
		aElementos[ i ] = aDatos.DATA[ elemento[ i ][ 0 ] ];
		aBuscar[ i ] = elemento[ i ][ 1 ];
	}
	nitem = buscaArray( aElementos, aBuscar );

	aCantidades = aDatos.DATA.CANT;
	aImportes = aDatos.DATA.IMP;
	aElementos = [];
	if ( nitem == -1 ) {
		aCantidades = aCantidades.concat( 1 );
		aImportes = aImportes.concat( importe );
		for ( i = 2; i < this.columnas.length; i ++ ) {
			aElementos[ i ] = aDatos.DATA[ this.columns[ i ] ].concat( elemento[ this.columns[ i ][ 1 ] ] );
		}
	}
	else {
		aCantidades[ nitem ] ++;
	}

	sDatos = '{"ROWCOUNT":' + aElementos.length
		+ ',"COLUMNS":[' + this.columns.join( ',' ) + ']'
		+ ',"DATA":{';
	for ( i = 0; i < this.columnas.length; i ++ ) {
		sDatos += ',"' + this.columnas[ i ] + '":[' + aElementos[ i ].join( ',' ) + ']';
	}
	sDatos += '}}';
	setCookie( this.name, sDatos, 7 );

	if ( window.document.carritoimp != undefined ) {
		window.document.carritoimp.importe.value = this.total();
	}
	
	if ( window.document.carritocnt != undefined ) {
		window.document.carritocnt.cantidad.value = this.cantidad();
	}
	
	alert( this.txt_agregado );
}

function carrito_baja( elemento ) {
	var aDatos, aElementos, nitem, sDatos;
	
	aDatos = getCarrito( this.name );
	aElementos = [];
	aBuscar = [];
	for ( i = 0; i < elemento.length; i ++ ) {
		aElementos[ i ] = aDatos.DATA[ elemento[ i ][ 0 ] ];
		aBuscar[ i ] = elemento[ i ][ 1 ];
	}
	nitem = buscaArray( aElementos, aBuscar );

	if ( nitem == -1 ) {
		return;
	}
	else {
		for ( i = 0; i < this.columnas.length; i ++ ) {
			aDatos.DATA[ this.columns[ i ] ].splice( nitem, 1 );
		}
	}
	
	sDatos = '{"ROWCOUNT":' + aElementos.length
		+ ',"COLUMNS":[' + this.columns.join( ',' ) + ']'
		+ ',"DATA":{';
	for ( i = 0; i < this.columnas.length; i ++ ) {
		sDatos += ',"' + this.columnas[ i ] + '":[' + aDatos.DATA[ this.columns[ i ] ].join( ',' ) + ']';
	}
	sDatos += '}}';
	setCookie( this.name, sDatos, 7 );
	
	alert( this.txt_eliminado );
}

function carrito_modifica( elemento, cant ) {
	var aDatos, aElementos, aCantidades, aImportes, nItem, sDatos, i;
	
	aDatos = getCarrito( this.name );
	aElementos = [];
	aBuscar = [];
	for ( i = 0; i < elemento.length; i ++ ) {
		aElementos[ i ] = aDatos.DATA[ elemento[ i ][ 0 ] ];
		aBuscar[ i ] = elemento[ i ][ 1 ];
	}
	aCantidades = aDatos.DATA.CANT;
	aImportes = aDatos.DATA.IMP;
	nitem = buscaArray( aElementos, aBuscar );

	if ( nitem == -1 ) {
		return;
	}
	else {
		aCantidades[ nitem ] = cant;
	}
	
	sDatos = '{"ROWCOUNT":' + aElementos.length
		+ ',"COLUMNS":[' + this.columns.join( ',' ) + ']'
		+ ',"DATA":{';
	for ( i = 0; i < this.columnas.length; i ++ ) {
		sDatos += ',"' + this.columnas[ i ] + '":[' + aDatos.DATA[ this.columns[ i ] ].join( ',' ) + ']';
	}
	sDatos += '}}';
	setCookie( this.name, sDatos, 7 );
}

function carrito_total() {
	var aDatos, aCantidades, aImportes, i;
	var nTotal = 0;

	aDatos = getCarrito( this.name );

	if ( aDatos.ROWCOUNT == 0 ) {
		return this.simbolo + ' 0.00';
	}

	aCantidades = aDatos.DATA.CANT;
	aImportes = aDatos.DATA.IMP;

	for ( i in aCantidades ) {
		nTotal += parseInt( aCantidades[ i ] ) * parseFloat( aImportes[ i ] );
	}

	return this.simbolo + ' ' + rightText( 10, 2, nTotal );
}

function carrito_cantidad() {
	var aCantidades, i;
	var nCantidad = 0;
	
	aDatos = getCarrito( this.name );

	if ( aDatos.ROWCOUNT == 0 ) {
		return 0;
	}

	aCantidades = aDatos.DATA.CANT;

	for ( i in aCantidades ) {
		nCantidad += parseInt( aCantidades[ i ] );
	}

	return nCantidad;
}

function carrito_setCol( cCol ) {
	var nitem = buscaArray( [ this.columns ], [ cCol ] );

	if ( nitem == -1 ) {
		this.columns = this.columns.concat( cCol );
	}
}

function carrito_elimina( elemento ) {
	if ( confirm( this.txt_elimina ) ) {
		this.baja( elemento );
		window.location.reload();
	}
}

function carrito_cambiaCant( elemento, cant ) {
	this.modifica( elemento, cant );

	if ( typeof document.carritocnt != 'undefined' ) {
		document.carritocnt.cantidad.value = this.cantidad();
	}

	if ( typeof document.carritoimp != 'undefined' ) {
		document.carritoimp.importe.value = this.total();
	}
}

function carrito_confirmaCompra( cUrl ) {
	if ( this.cantidad() == 0 ) {
		alert( this.txt_vacio );
	}
	else {
		window.location = cUrl;
	}
}

function carrito( name, simbolo ) {
	this.name = name;
	this.cambiaCant = carrito_cambiaCant;
	this.elimina = carrito_elimina;
	this.agrega = carrito_agrega;
	this.baja = carrito_baja;
	this.modifica = carrito_modifica;
	this.confirmaCompra = carrito_confirmaCompra;
	this.total = carrito_total;
	this.cantidad = carrito_cantidad;
	this.setCol = carrito_setCol;
	this.simbolo = simbolo;
	this.columns = ['CANT','IMP','CONT'];
	this.txt_agregado = 'Se ha agregado 1 elemento al carrito.';
	this.txt_elimina = 'Elimina este elemento?';
	this.txt_eliminado = 'Se ha quitado 1 elemento del carrito.';
	this.txt_vacio = 'No hay elementos seleccionados.';
}

