// Extract from:
//
// BASIC DATA VALIDATION FUNCTIONS:
// 18 Feb 97 created Eric Krock
// (c) 1997 Netscape Communications Corporation

// Check whether string s is empty.
function isEmpty(s)
{
	return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace(s)
{
	var i;
	// whitespace characters
	var whitespace = " \t\n\r";

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function isEmail(s)
{   
	var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++;
    }

    if ((i >= sLength) || (s.charAt(i) != "@"))
	 return false;
    
	else
	 i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++;
    }

// there must be at least one character after the .
    if((i >= sLength - 1) || (s.charAt(i) != "."))
	 return false;
    
	else
	 return true;
}

function esComentarioVacio()
{
	s = document.getElementById('edit-comment');

	if(isWhitespace(s.value))
	{
		alert('Por favor escriba su opinion en el espacio indicado.');
		s.value = '';
		s.focus();
		return false;
	}

	return true;
}

function esIngresoUsuario()
{
	n = document.getElementById('edit-name-ingreso');
	c = document.getElementById('edit-pass-ingreso');
	

	if(isWhitespace(n.value))
	{
		alert('Por favor ingrese su nombre de usuario.');
		n.value = '';
		n.focus();
		return false;
	}

	if(isWhitespace(c.value))
	{
		alert('Por favor ingrese su contrase&ntilde;a.');
		c.value = '';
		c.focus();
		return false;
	}

	return true;
}

function esOlvidoContrasena()
{
	n = document.getElementById('edit-name-olvido');

	if(isWhitespace(n.value))
	{
		alert('Por favor ingrese su nombre de usuario.');
		n.value = '';
		n.focus();
		return false;
	}

	return true;
}

function esUsuarioNuevo()
{
	n = document.getElementById('edit-name-nuevo');
	m = document.getElementById('edit-mail-nuevo');

	if(isWhitespace(n.value))
	{
		alert('Por favor ingrese un nombre de usuario.');
		n.value = '';
		n.focus();
		return false;
	}

	if(isWhitespace(m.value))
	{
		alert('Por favor ingrese una dirección de correo.');
		m.value = '';
		m.focus();
		return false;
	}

	if(!isEmail(m.value))
	{
		alert('Por favor ingrese una dirección de correo válida.');
		m.value = '';
		m.focus();
		return false;
	}

	return true;
}

var BrowserDetect = {
	init: function () { 
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [		{string: navigator.userAgent,subString: "OmniWeb",versionSearch: "OmniWeb/",identity: "OmniWeb"},		{string: navigator.vendor,subString: "Apple",identity: "Safari"},		{prop: window.opera,identity: "Opera"},		{string: navigator.vendor,subString: "iCab",identity: "iCab"},		{string: navigator.vendor,subString: "KDE",identity: "Konqueror"},		{string: navigator.userAgent,subString: "Firefox",identity: "Firefox"},		{string: navigator.vendor,subString: "Camino",identity: "Camino"},		{string: navigator.userAgent,subString: "Netscape",	identity: "Netscape"},		{string: navigator.userAgent,subString: "MSIE",identity: "Explorer",versionSearch: "MSIE"},		{string: navigator.userAgent,subString: "Gecko",identity: "Mozilla",versionSearch: "rv"},		{string: navigator.userAgent,subString: "Mozilla",identity: "Netscape",versionSearch: "Mozilla"}	],

	HacerPaginaInicio: function(obj){ 
		if(this.browser == 'Firefox'){
			window.sidebar.addPanel('Noticias | ELESPECTADOR.COM', 'http://www.elespectador.com','');
		}else if(this.browser == 'Explorer'){
		  obj.style.behavior='url(#default#homepage)';  obj.setHomePage('http://www.elespectador.com/');
		}else{
			alert('Por favor presione CTRL + D para agregar a ELESPECTADOR.COM a sus enlaces favoritos');
		}	
	}
};

function desplegarMenuEdicion(obj,thisEvent){ 
	BrowserDetect.init();
	if(BrowserDetect.browser=='Explorer'){
		if(document.readyState == 'complete')  dropdownmenu(obj, thisEvent, menu1, '175px'); 
	}
	else{ if(BrowserDetect.browser=='Firefox') 
			dropdownmenu(obj, thisEvent, menu1, '175px');
		else
			dropdownmenu(obj, thisEvent, menu1, '175px');
	}

}

function limitText(obj){ 
	var count = "800"; 
	if(obj.value.length > count){
		obj.value = obj.value.substring(0,count);
		return false;
	}
	document.getElementById("caracter").value = 800 - parseInt(obj.value.length);
	return true;
}

function limitCTRV(obj, evento){ 
	var text; 
	text = obj.value;
	var ctrl = typeof evento.modifiers == 'undefined' ?   evento.ctrlKey : evento.modifiers & evento.CONTROL_MASK;
	var v = typeof evento.which == 'undefined' ?   evento.keyCode == 86 : evento.which == 86;
	if ( ctrl && v ) { 		
		alert('Lo sentimos, no se puede pegar un texto externo');
		obj.value=text;
		return false;
	}
	return true;
}

function limitClickRigth(e) {
if (navigator.appName == 'Netscape' && (e.which == 3 || e.which == 2)){
	alert('Lo sentimos, no se puede pegar un texto externo')
	return false;
}else  if (navigator.appName == 'Microsoft Internet Explorer' && (event.button == 2)){
			alert('Lo sentimos, no se puede pegar un texto externo')
		}	
}

function abrirVentana(menu)
{
  if(menu.selectedIndex != 0)
  {
	window.open(menu[menu.selectedIndex].value,"Revista","width=1024,height=768,scrollbars=NO") 
  }
}

function cambiarPagina(menu)
{
  if(menu.selectedIndex != 0)
  {
      window.document.location = menu[menu.selectedIndex].value;
  }
}

function bookmarksite(title, url)
{
	if (document.all)
	{
		window.external.AddFavorite(url, title);	
	}

	else if (window.sidebar)
	{
		window.sidebar.addPanel(title, url, "");
	}
}
