function obtiene_comunas(id_region){
	new Request({
		url:'../lib/comunas.php',
		onSuccess: function(responseText){
			$('comunas').set('html', responseText);
		}
	}).send('region=' + id_region);
}

function agregaCarro(tam){
	if(isNaN($('cantidad').value.trim())){
		$('cantidad').value = 1;
	}
	$('tamano').value = tam;
	$('frmAgregaCarro').submit();
}
function actualizarCantidad(id){
	if(isNaN($('cantidad_' + id).value.trim())){
		$('cantidad_' + id).value = 1;
	}
	new Request({
		url:'../lib/actualiza_carro.php',
		onSuccess: function(responseText){
			window.location = "shopping-cart.php";
		}
	}).send('id_producto=' + id + '&cantidad=' + $('cantidad_' + id).value);
}
function eliminarProducto(id){
	new Request({
		url:'../lib/elimina_carro.php',
		onSuccess: function(responseText){
			window.location = "shopping-cart.php";
		}
	}).send('id_producto=' + id);
}
function vaciarCarro(){
	if(confirm("Desea vaciar su carro de compras?")){
		window.location = "../lib/vaciar_carro.php";
	}
}

function finalizar(){
	$('frmFinalizar').submit();
}
function seguir(){
	location.href='catalog.php';
}
function buscar(){
	if($('busqueda').value.trim() != "" && $('busqueda').value.trim().length >= 3){
		$('frm_busqueda').submit()
	}else{
		alert("Debe ingresar al menos 3 letras")
	}
}
function verficar_chile(id){
	if(Number($('chile').value) == Number(id)){
		$('stgo').setStyle('display', 'table-row');
	}else{
		$('stgo').setStyle('display', 'none');
		$('santiago').checked = false;
		$('ciudad').readonly = false;
	}
}
function marca_santiago(stgo){
	if(stgo){
		$('ciudad').value = "Santiago";
		$('ciudad').setProperty('readonly','true');
	}else{
		$('ciudad').value = "";
		$('ciudad').setProperty('readonly', '');
	}
}
function number_format (number, decimals, dec_point, thousands_sep) {
    var n = number, prec = decimals;
    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}

function calcular_total(costo, total){
	$('btn_pagar').setStyle('visibility', 'visible');
	$('costos_envio').set('html', number_format(costo, 2, ',', ''));
	$('total').set('html', number_format(costo+total, 2, ',', ''));
}
