// JavaScript Document

function lenguaje(lang)
{
	var wlh = window.location.href;
	if(wlh.indexOf('?')!= -1)
	{
		if(wlh.indexOf('lang=')!= -1)
		{
			var oldlang = wlh.substr(wlh.indexOf('lang=')+5,3);
			/*if(oldlang != lang)
			{*/
				var left = wlh.substr(0, wlh.indexOf('lang='));
				var right = wlh.substr(wlh.indexOf('lang=')+8);
				wlh = left + 'lang='+ lang + right;
				window.location.href = wlh;
			//}
		}
		else
			window.location.href = wlh + '&lang='+ lang;
	}
	else
		window.location.href = wlh + '?lang='+ lang;
}

function KeyCodeChk(vChar, nChar)  /* / . , -  */
{
	var eKeyCode = event.keyCode;
	var vTexto   = vChar.value;
	var nTexto   = vTexto.length;
         if ((eKeyCode < 48 || eKeyCode > 57) && (eKeyCode < 65 || eKeyCode > 90) && (eKeyCode < 96 ||  eKeyCode > 122))
		 {  
		 	if ((eKeyCode != 13) && (eKeyCode != 44)&& (eKeyCode != 45) && (eKeyCode != 46) && (eKeyCode != 47) && (eKeyCode != 8)  && (eKeyCode != 16) && (eKeyCode != 32) ) 
			{
             	if ((eKeyCode == 9) && (nTexto > nChar))
				{ 
	     	         return;
	           	} else {
               		if ((eKeyCode == 9) && (nTexto ==0))
				   {
	     	           return;
	           	   } else {
		                 if ((eKeyCode == 9) && (nTexto < nChar))
						 {
		     	             alert("Este campo no puede ser menor a "+(nChar+1)+" digitos");
			       	     }
						}
					}
			window.event.returnValue = false;
   }}
}

function validateForm(err_message1, errmessage2)
{
	var user = document.getElementById('username').value;
	if((user.length == 0) || (user == null))
	{
		alert(errmessage2);
		return false;
	}
	var email = document.getElementById('email').value;
	if((email.length == 0) || (email == null))
	{
		alert(err_message1);
		return false;
	}
	if(!isValidEmail(email))
	{
		alert(err_message1);
		return false;
	}
	return true;
}

function isValidEmail(email, required)
{
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
      	return false;
    } else if (email.indexOf("@") == (email.length-1)) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("@.") >=0) { // dot cant be after a @
		return false;
	} else if (email.indexOf("..") >=0) { // two periods in a row is not valid
		return false;
    } else if (email.indexOf(".") == (email.length-1)) {  // . must not be the last character
		return false;
    }
    return true;
}

function allValidChars(email)
{
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function ordenar(elem)
{
	var url = window.location.href;
	window.location.href = url + '&ord=' + elem.value;
}

function updURLParam(name, value) /* JISG : values must be same length */
{
	var wlh = window.location.href;
	var n = name + '=';
	var r = '';
	if(wlh.indexOf('?')!= -1)
	{
		if(wlh.indexOf(n)!= -1)
			wlh = wlh.substr(0, wlh.indexOf(n)) +n+value + wlh.substr(wlh.indexOf(n) + n.length + value.length);
		else
			r = '&'+n+value;
	}
	else
		r = '?'+n+value;
	window.location.href = wlh + r;
}

function vallog(em1, em2)
{
	var idUserName = document.getElementById('idUserName').value;
	if((idUserName.length == 0) || (idUserName == null))
	{
		alert(em1);
		return false;
	}
	var idPassword = document.getElementById('idPassword').value;
	if((idPassword.length == 0) || (idPassword == null))
	{
		alert(em2);
		return false;
	}
	return true;
}

function ocphue(chk)
{
	if(timer)
		return;
	var divHeight = document.getElementById('datos_huesped_'+chk.value).style.height;
	var Pmax = 122, Pmin = 0;
	if(divHeight == (Pmax + 'px'))
	{
		var Ptmp = Pmax; Pmax = Pmin; Pmin = Ptmp;
	}
	move('datos_huesped_'+chk.value, 'height', Pmin, Pmax,5,8);
}
var timer = null;
function move(elem,attr,ini,fin,_vel,_paso) /* JISG: triggers the animation */
{
	var vel = (_vel || 10), paso = (_paso || 2);
	timer = setInterval("move2('"+elem+"','"+attr+"',"+ini+","+fin+","+paso+");",vel);
}
function move2(elem,prop,inicial,final,paso) /* JISG: does the animation */
{
	var el = document.getElementById(elem);
	var valst = el.style[prop];
	var val = parseInt(valst.split('px')[0]);
	var sign = (final - inicial)/Math.abs(final - inicial);
	if(sign < 0)
	{
		if(val > Math.abs(final) && (val-paso) >= Math.abs(final))
		{
			val -= paso;
			el.style[prop] = val + 'px';
		}
		else
		{
			el.style[prop] = final + 'px';
			clearInterval(timer);
			timer = null;
		}
	}
	else
	{
		if(val < Math.abs(final) && (val+paso) <= Math.abs(final))
		{
			val += paso;
			el.style[prop] = val + 'px';
		}
		else
		{
			el.style[prop] = Math.abs(final) + 'px';
			clearInterval(timer);
			timer = null;
		}
	}
}





