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').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, tipo){

	new Request({

		url:'lib/costo_envio.php',

		data:'codigo=' + id + '&tipo=' + tipo,

		onSuccess: function(responseText){

			var envio = responseText.toInt();

			var subtotal = $('subtotal').get('html').replace('.', '').toInt();

			$('costo_envio').set('html', number_format(envio, 0, '.', '.'));

			var total = envio + subtotal;

			$('total').set('html', number_format(total, 0, '.', '.'));

		}

	}).send();

}

function vaciarCarro(){

	if(confirm("Desea vaciar su Cotizador?")){

		window.location = "lib/vaciar_carro.php";

	}

}



function finalizar(){

	$('frmFinalizar').submit();

}

function seguir(ir){
		//location.href="index.php";
		alert("index");
}

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 costo_region(region){

	new Request

}

function minimo(minimo){

	alert("Lo sentimos, pero el minimo de compra es de $" + number_format(minimo, 0, '.', '.'));



}
