// JavaScript Document
$(function(){
	  // this initialises the demo scollpanes on the page.
	 $('div#contacto-form, div#lightbox-contacto, div#alert-cuadro').css( { display:"none" } );
});


var interval = Number();

function openContact(op){
	switch(op){
		case 1 : $('div#contacto-form').slideDown(1000); break;
		case 2 : $('div#contacto-form').slideUp(1000); break;
	}
		
}

function mensaje(str){
	document.getElementById('alert-cuadro').innerHTML = "<span> >> </span>" + str;
	$('div#lightbox-contacto').css( { display:"block", opacity:"0" } ).animate( { opacity:".80" }, 1000 );
	$('div#alert-cuadro').fadeIn(1000);
	
	clearTimeout(interval);
	interval = setTimeout(esconderMensaje, 2000);
}

function esconderMensaje(){
	clearTimeout(interval);
	$('div#lightbox-contacto, div#alert-cuadro').fadeOut(1000);
}

/*********************************************************************************************
  Función valida:
	función para validar formularios. Unicamente a los "id" de el fomulario hay que 
	inicializarlos con un guion bajo "_".
  Parámetros:
	form = Nombre del "id" del formulario a validar.
*********************************************************************************************/
function valida(form){
	for(x = 0; x < document.getElementById(form).elements.length; x++){
		temp=document.getElementById(form).elements[x].id;
		temp2=temp.indexOf("_");
		if( trim(document.getElementById(form).elements[x].value) == "" && document.getElementById(form).elements[x].type != "submit" && temp2 == 0){
			nombre = document.getElementById(form).elements[x].id.replace("_","");
			mensaje("El campo "+nombre+" esta vacio");
			document.getElementById(form).elements[x].focus();
			return false;
		}
	}
	
	return true;
} // Termina function valida()

/*********************************************************************************************
  Función trim:
	función para quitar los espacios iniciales y finales de una cadena
  Parámetros:
	cadena = String o Cadena a validar.
*********************************************************************************************/
function trim(cadena){
	for(i=0; i<cadena.length; i++){
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(i+1, cadena.length);
		else
			break;
	}

	for(i=cadena.length-1; i>=0; i=cadena.length-1){
		if(cadena.charAt(i)==" ")
			cadena=cadena.substring(0,i);
		else
			break;
	}
	
	return cadena;
}

/*********************************************************************************************
  Función valida_letras:
	función para validar que el texto sean solo letras
  Parámetros:
	evt = el evento se utiliza asi "onkeypress='return valida_letras(event)'"
*********************************************************************************************/
function valida_letras(evt)
{
 var charCode = (evt.which) ? evt.which : event.keyCode
 if (charCode < 48 || charCode > 57)
	return true;

 return false;

}

/*********************************************************************************************
  Función valida_numeros:
	función para validar que el texto sean solo números
  Parámetros:
	evt = el evento se utiliza asi "onkeypress='return valida_numeros(event)'"
*********************************************************************************************/
function valida_numeros(evt)
{
 var charCode = (evt.which) ? evt.which : event.keyCode
 if ( charCode != 8 && (charCode < 48 || charCode > 57) )
	return false;

 return true;

}

/*********************************************************************************************
  Función valida_float:
	función para validar que el texto sean solo números
  Parámetros:
	evt = el evento vver ejemplos arriba
	id = nombre del tag que se esta validando
*********************************************************************************************/
function valida_float(evt,id)
{
 var charCode = (evt.which) ? evt.which : event.keyCode;
 if ( charCode == 8 || (charCode >= 48 && charCode <= 57) )
	 return true;

  if((document.getElementById(id).value.indexOf('.') == -1 && charCode == 46)||(document.getElementById(id).value.indexOf('-') == -1 && charCode == 45))
    return true;

 return false;

}

function signo(id){
    if(document.getElementById(id).value.indexOf("-") > 0){
      document.getElementById(id).value = document.getElementById(id).value.replace("-","");
    }
    
    while(document.getElementById(id).value.charAt(0) == '0' && document.getElementById(id).value.charAt(1) != '.'){
        if(document.getElementById(id).value.indexOf("0") == 0)
          document.getElementById(id).value = document.getElementById(id).value.replace("0","");
    }

    while(document.getElementById(id).value.charAt(0) == '-' && document.getElementById(id).value.charAt(1) == '0' && document.getElementById(id).value.charAt(2) != '.'){
       if(document.getElementById(id).value.indexOf("0") == 1)
          document.getElementById(id).value = document.getElementById(id).value.replace("0","");
    } 
}