$(document).ready(function(){
	
	//$('input.inputBox').inputBox();
	//$('input.inputBoxNeeded').inputBoxNeeded();
	
	$("#succes").hide();
	
	/*
	 * INPUTS AUTOFORMAT
	 */
	
	$("input").each(function(){
		$(this).css({ 
			'color':'#444'
		});
	});
	
	
	/*
	 * SOLO RUT
	 */
	$(".rut").keypress(function(evt){
		var key = evt.keyCode ? evt.keyCode : evt.which ;
		return (key <= 31 || (key >= 48 && key <= 57) || key == 75 || key == 107);
	});
	
	$("#id").blur(function(){
		formatearRut("id");
	});
	
	
	
	/*
	 * IN CASE OF PRESS "SEND FORM"
	 */
	$("a#send_form").click(function(){
		
		if ($("#name").attr("value") == "Nombre"){
			$("#name").val("");
			$('#name').focus(function(){ 
				if ($(this).attr("value") == "Nombre"){
					$(this).val("");
					$(this).css({ 
						'font-style':'normal',
						'color':'#000'
					});
				}
			});	
			$('#name').blur(function(){ 
				if ($(this).attr("value") == ""){
					$(this).val("Nombre");
					$(this).css({ 
						'font-style':'italic',
						'color':'#444'
					});
				}
			});
			
		}
		else if ($("#lastname").attr("value") == "Apellidos"){
			$("#lastname").val("");
			$('#lastname').focus(function(){ 
				if ($(this).attr("value") == "Apellidos"){
					$(this).val("");
					$(this).css({ 
						'font-style':'normal',
						'color':'#000'
					});
				}
			});	
			$('#lastname').blur(function(){ 
				if ($(this).attr("value") == ""){
					$(this).val("Apellidos"); 
					$(this).css({ 
						'font-style':'italic',
						'color':'#444'
					});
				}
			});
			
		}
		else if ($("#id").attr("value") == "R.U.T."){
			$("#id").val("");
			/*RUT INPUT*/
			$('#id').focus(function(){ 
				if ($(this).attr("value") == "R.U.T."){
					$(this).val("");
					$(this).css({ 
						'font-style':'normal',
						'color':'#000'
					});
				}
			});	
			$('#id').blur(function(){ 
				if ($(this).attr("value") == ""){
					$(this).val("R.U.T."); 
					$(this).css({ 
						'font-style':'italic',
						'color':'#444'
					});
				}
			});
			
		}
		else if ($("#email").attr("value") == "Email"){
			$("#email").val("");
			$('#email').focus(function(){ 
				if ($(this).attr("value") == "Email"){
					$(this).val(""); 
					$(this).css({ 
						'font-style':'normal',
						'color':'#000'
					});
				}
			});	
			$('#email').blur(function(){ 
				if ($(this).attr("value") == ""){
					$(this).val("Email");
					$(this).css({ 
						'font-style':'italic',
						'color':'#444'
					});
				}
			});
	
		}
		else if ($("#phonenumber").attr("value") == "Tel\u00E9fono de contacto"){
			$("#phonenumber").val("");
			$('#phonenumber').focus(function(){ 
				if ($(this).attr("value") == "Tel\u00E9fono de contacto"){
					$(this).val("");
					$(this).css({ 
						'font-style':'normal',
						'color':'#000'
					});
				}
			});	
			$('#phonenumber').blur(function(){ 
				if ($(this).attr("value") == ""){
					$(this).val("Tel\u00E9fono de contacto");
					$(this).css({ 
						'font-style':'italic',
						'color':'#444'
					});
				}
			});
					
		}
		
		if($("form#bbva").valid()) {
			/* Send data */
			var dataString = $('#bbva').serialize();
			$.ajax({
				type: 'POST',
				url: 'send.php',
				data: dataString,
				success: function(html) {
					$("#form").hide();
					$("#succes").fadeIn(200);
					$("#form_message").html(html);
					$('#uniform-cod').hide();
				},
				error: function(html) {
					$("#form").hide();
					$("#succes").fadeIn(200);
					$("#form_message").html('En este momento tenemos problemas, intente mas tarde...');
					//console.log(html);
					$('#uniform-cod').hide();
				}
			  });
		}
		return false;
	});
	
});

function formatearRut(casilla){
	function formatearMillones(nNmb){
		var sRes = "";
		for (var j, i = nNmb.length - 1, j = 0; i >= 0; i--, j++)
		 sRes = nNmb.charAt(i) + ((j > 0) && (j % 3 == 0)? ".": "") + sRes;
		return sRes;
	}
	
	var casillaRut=document.getElementById(casilla);
	
	var rut=casillaRut.value;
	var ultimoDigito=rut.substr(rut.length-1,1);
	var terminaEnK = (ultimoDigito.toLowerCase()=="k");
	rutSinFormato=rut.replace(/\W/g,"");
	rut=rut.replace(/\D/g,"");
	var dv=rut.substr(rut.length-1,1);
	if(!terminaEnK){ rut=rut.substr(0,rut.length-1); }
	else{ dv="K"; }
	if(rut && dv) {
		casillaRut.value=formatearMillones(rut)+"-"+dv;
		//document.getElementById('buic_rutdv').value=rutSinFormato;
	}
}

