function rightText( nlength, ndecimals, ntxt ) {
	var xtxt;
	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;
	for ( i in array ) {
		if ( elemento == array[ i ] ) {
			return i;
		}
	}
	return -1;
}

function getCarrito( cName ) {
	sDatos = getCookie( cName );
	if ( sDatos == null ) {
		sDatos = '{"ROWCOUNT":0,"COLUMNS":["CONT","CANT","IMP"],"DATA":{"CONT":[],"CANT":[],"IMP":[]}}';
	}
	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;

	aDatos = getCarrito( this.name );

	aElementos = aDatos.DATA.CONT;
	aCantidades = aDatos.DATA.CANT;
	aImportes = aDatos.DATA.IMP;
	nitem = buscaArray( aElementos, elemento );
	
	if ( nitem == -1 ) {
		aElementos = aElementos.concat( elemento );
		aCantidades = aCantidades.concat( 1 );
		aImportes = aImportes.concat( importe );
	}
	else {
		aCantidades[ nitem ] ++;
	}

	sDatos = '{"ROWCOUNT":' + aElementos.length
		+ ',"COLUMNS":["CONT","CANT","IMP"],"DATA":{'
		+ '"CONT":[' + aElementos.join( ',' ) + ']'
		+ ',"CANT":[' + aCantidades.join( ',' ) + ']'
		+ ',"IMP":[' + aImportes.join( ',' ) + ']'
		+ '}}';
	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 aElementos, aCantidades, aImportes
	
	aDatos = getCarrito( this.name );
	aElementos = aDatos.DATA.CONT;
	aCantidades = aDatos.DATA.CANT;
	aImportes = aDatos.DATA.IMP;
	nitem = buscaArray( aElementos, elemento );

	if ( nitem == -1 ) {
		return;
	}
	else {
		aElementos.splice( nitem, 1 );
		aCantidades.splice( nitem, 1 )
		aImportes.splice( nitem, 1 );
	}
	
	sDatos = '{"ROWCOUNT":' + aElementos.length
		+ ',"COLUMNS":["CONT","CANT","IMP"],"DATA":{'
		+ '"CONT":[' + aElementos.join( ',' ) + ']'
		+ ',"CANT":[' + aCantidades.join( ',' ) + ']'
		+ ',"IMP":[' + aImportes.join( ',' ) + ']'
		+ '}}';
	setCookie( this.name, sDatos, 7 );
	
	alert( this.txt_eliminado );
}

function carrito_modifica( elemento, cant ) {
	var aElementos, aCantidades
	
	aDatos = getCarrito( this.name );
	aElementos = aDatos.DATA.CONT;
	aCantidades = aDatos.DATA.CANT;
	aImportes = aDatos.DATA.IMP;
	nitem = buscaArray( aElementos, elemento );

	if ( nitem == -1 ) {
		return;
	}
	else {
		aCantidades[ nitem ] = cant;
	}
	
	sDatos = '{"ROWCOUNT":' + aElementos.length
		+ ',"COLUMNS":["CONT","CANT","IMP"],"DATA":{'
		+ '"CONT":[' + aElementos.join( ',' ) + ']'
		+ ',"CANT":[' + aCantidades.join( ',' ) + ']'
		+ ',"IMP":[' + aImportes.join( ',' ) + ']'
		+ '}}';
	setCookie( this.name, sDatos, 7 );
}

function carrito_total() {
	var 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 lCantidades, 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;
}

