function FORM_Validate(){
	$("#form").validate({
		onkeyup:false,
		onfocusin:false,
		onclick:false
	});
}
function FORM_Send(){
	if($("#form").valid())
	{
		$("#form").submit();
	}
	else
	{
		$("label.error").hide();
	}
	return false;
}
function FORM_CleanInput()
{
	var value = $(this).val()
		.toLowerCase()
		.replace(/ñ/g, 'n')
		.replace(/á/g, 'a')
		.replace(/é/g, 'e')
		.replace(/í/g, 'i')
		.replace(/ó/g, 'o')
		.replace(/ú/g, 'u')
		.replace(/ /g, '-')
		.replace(/\,/g,'.')
		.replace(/([^0-9-\.])/g,'');
	$(this).val(value);
}

function FORM_Update()
{
	var value = $("#precio_compra").val();
	$("span.precio_compra").text($("#precio_compra").val());
	$("#porcentaje").text("");
	value = $("#ano option:selected").val()*value/100;
	$("span.ano").text($("#ano option:selected").text());
	value = $("#marca option:selected").val()*value/100;
	$("span.marca").text($("#marca option:selected").text());
	value = $("#condicion option:selected").val()*value/100;
	$("span.condicion").text($("#condicion option:selected").text());
	value = $("#memoria option:selected").val()*value/100;
	$("span.memoria").text($("#memoria option:selected").text());
	value = $("#disco option:selected").val()*value/100;
	$("span.disco").text($("#disco option:selected").text());
	$("#precio_usado span").text(Math.floor(value));
	if($("#precio_compra").val()=="")
	{
		$("#precio_compra").addClass("error");
	}
	else
	{
		$("#precio_compra").removeClass("error");
		$(".step1").fadeOut(function(){
			$(".step2").fadeIn();
		});
		var _t = new Array();
		$(".step2 .group").each(function(){
			if(!$(this).hasClass("border"))
			{
				_t.push($(this).text());
			}
		});
		$("#comments").text(String(_t.join("<br />").trim()));
	}
	return false;
}
$(document).ready(function(){
	$(".vslider").vslider();
	$(".vcheckbox").vcheckbox();
	$(".vradio").vradio();
	$(".vselect").vselect();
	$("#precio_compra").keyup(FORM_CleanInput);
	$("#step1").click(FORM_Update);
	$("#step2").click(FORM_Send);
	$(".step1").fadeIn();
	FORM_Validate();
});
