function number_format(number, decimals, dec_point, thousands_sep) {
    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}
function obtiene_comunas(id_region){
	new Request({
		url:'lib/comunas.php',
		onSuccess: function(responseText){
			$('comunas').set('html', responseText);
		}
	}).send('region=' + id_region);
}

function agregaCarro(id){
	if(isNaN($('cantidad').value.trim())){
		$('cantidad').value = 1;
	}
	$('frmAgregaCarro_' + id).submit();
}
function actualizarCantidad(id){
	if(isNaN($('cantidad_' + id).value.trim())){
		$('cantidad_' + id).value = 1;
	}
	new Request({
		url:'lib/actualiza_carro.php',
		data:'id_producto=' + id + '&cantidad=' + $('cantidad_' + id).value,
		onSuccess: function(responseText){
			window.location = "carro.php";
		}
	}).send();
}
function eliminarProducto(id){
	new Request({
		url:'lib/elimina_carro.php',
		onSuccess: function(responseText){
			window.location = "carro.php";
		}
	}).send('id_producto=' + id);
}
function costo_envio(id, metodo){
	$('metodo_envio').set('value', metodo);
	new Request({
		url:'lib/costo_envio.php',
		data:'id=' + id + '&metodo=' + metodo,
		onSuccess: function(responseText){
			var v = responseText.split("|");
			$('valor_envio').set('html', v[0]);
			var des = 0;
			var d1 = 0;
			var v_ds = v[1].toFloat();
			var total = $('valor_iva').get('html').replace(/\./, '').toInt() + $('valor_neto').get('html').replace(/\./, '').toInt();
			$('valor_total').set('html', number_format(total, 0, '.', '.'));
			if(v_ds > 0){
				var total = $('valor_total').get('html').replace(/\./, '').toInt();
				var d1 = (total * (v_ds/100));
				$('valor_descto').set('html', number_format(d1, 0, '.', '.'));
			}
			var subto = total - d1;
			$('valor_subtotal').set('html', number_format(subto, 0, '.', '.'));
			$('valor_total1').set('html', number_format(total - d1 + $('valor_envio').get('html').replace(/\./, '').toInt(), 0, '.', '.'));
		}
	}).send();
}
function vaciarCarro(){
	if(confirm("Desea vaciar su carro de compras?")){
		window.location = "lib/vaciar_carro.php";
	}
}

function finalizar(){
	$('frmFinalizar').submit();
}
function seguir(id_cat){
	if(id_cat > 0) location.href='/categoria.php?idCat=' + id_cat;
	else location.href='/index.php';
}
function verificador(dv, campo_dv, campo_rut){
	var dig = dv;
	var rut = $(campo_rut).get('value').trim().replace(/[^0-9]/g, "");
	var dv = null;
	if (rut.test(/\d{7,8}/)){
		var largo = rut.length;
		if(largo > 2 && largo <=10){
			var i=0;
			var mult=2;
			var suma=0;
			largo--;
			while(largo>=0){
				suma=suma+(rut.charAt(largo)*mult);
				if(mult>6) mult=2;
				else mult++;
				largo--;
			}
			var resto = suma%11;
			var digito = 11-resto
			if(digito==10) digito="k" ;
			else if(digito==11) digito=0;
			
			if(dig.toLowerCase() != digito){
				$(campo_dv).set('value', '');	
			}
		}else $(campo_dv).set('value', '');
    }else $(campo_dv).set('value', '');
}
function comprobar_premium(region, metodo){
	if(region == 13){
		$('metodo2').removeProperty('disabled');
		$('label_metodo2').setStyle('visibility', 'visible');
	}else{
		$('metodo_envio').set('value', 1);
		$('metodo2').setProperty('disabled', 'disabled');
		$('label_metodo2').setStyle('visibility', 'hidden');
		$('metodo1').checked = true;
	}
	costo_envio(region, metodo);
}