function externalLinks() 
{
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) 
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
			anchor.getAttribute("rel") == "external")
		anchor.target = "_blank";
	}
}

function validaPresupuesto()
{
	var msg = '';
	
	if (esVacio('nombre'))
		msg = cadMsg(msg, 'Es necesario que nos digas tu nombre o razón social');
		
	if (esVacio('email') && esVacio('telefono'))
		msg = cadMsg(msg, 'Necesitamos un teléfono o una dirección de email para poder ponernos en contacto contigo');
		
	var sok = parseInt(document.getElementById('n1').innerHTML, 10) + parseInt(document.getElementById('n2').innerHTML, 10);
	
	if (document.getElementById('suma').value != sok)
		msg = cadMsg(msg, 'Verifica la suma de los números, por favor.');

	if (document.getElementById('condiciones').checked == false)
		msg = cadMsg(msg, 'Es necesario que aceptes las condiciones de uso del sitio web');
		
	if (msg == '')
		return true;
	else
	{
		alert(msg);
		return false;
	}
}

function alguncheckseleccionado(obj)
{
	var inputs = document.getElementsByTagName('input');
	var nCol = inputs.length;
	var input;
	var haychecked = false;
	
	for (var i = 0; i < nCol; i++)
	{
		if (inputs[i].type == 'checkbox' && inputs[i].id.substring(0, obj.length) == obj)
		{
			if (inputs[i].checked)
				haychecked = true;
		}
	}

	return haychecked;
}

function checkvars(msg, msgW)
{
	var ok;
		
	if (msg != '')
	{
		alert(msg);
		ok = false;
	}
	else
		if (msgW != '')
			ok = confirm(msgW + "\r\nQuieres continuar de todas formas?");

	return ok;
}

function esOculto(obj)
{
	if (trim(document.getElementById(obj).style.display) == 'none')
		return true;
	else
		return false;
}

function esVisible(obj)
{
	return !esOculto(obj);
}

function getValor(obj)
{
	return document.getElementById(obj).value;
}

// -------------------------------------------

function Longitud(obj)
{
	return document.getElementById(obj).value.length;
}

function Valor(obj)
{
	return document.getElementById(obj).value;
}

function esVacio(obj)
{
	if (trim(document.getElementById(obj).value) == '')
		return true;
	else
		return false;
}

function esNumero(obj)
{
	if (isnumeric(document.getElementById(obj).value))
		return true;
	else
		return false;
}

function cadMsg(textoantiguo, texto, campo)
{
	if (campo != undefined)
		texto = texto + ' [se encontró "' + document.getElementById(campo).value + '"]';

	if (textoantiguo == '')
		return texto;
	else
		return textoantiguo + "\r\n" + texto;
}

function Comparar(obj, valor)
{
	return (document.getElementById(obj).value == valor);
}

function esEmail(obj)
{
	var val = document.getElementById(obj).value;
	if (val.indexOf('@') == -1)
		return false;
	if (val.indexOf('.') == -1)
		return false;
		
	return  true;
}

function esRGB(obj)
{
	var val = document.getElementById(obj).value;
	var i=0;
	var ok=false;
	
	if (val.length != 7)
		return false;
	if (val.substring(0,1) != '#')
		return false;
	
	for (i=1;i<6;i++)
	{
		ok=false;
		if (val.charCodeAt(i) >= 48 && val.charCodeAt(i) <= 57)
			ok = true;
		if (val.charCodeAt(i) >= 65 && val.charCodeAt(i) <= 70)
			ok = true;
		if (val.charCodeAt(i) >= 97 && val.charCodeAt(i) <= 102)
			ok = true;
			
		if (ok == false)
			return false;
	}
	
	return true;
}

// --------------------------------------------------


function trim(cadea)
{
	senBrancos = cadea;
	while (senBrancos.indexOf(" ")!=-1)
	{
		senBrancos = senBrancos.replace(" ","");
	}
	
	return senBrancos;
}

function isnumeric(sText) 
{
	var ValidChars = "0123456789";
	var IsNumber = true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}

function isDate(dateStr) 
{
	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?
	
	if (matchArray == null) 
	{
		return false;
	}
	
	day = matchArray[1]; // parse date into variables
	month = matchArray[3];
	year = matchArray[5];
	
	if (month < 1 || month > 12) 
	{ // check month range
		return false;
	}
	
	if (day < 1 || day > 31) 
	{
		return false;
	}
	
	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		return false;
	}
	
	if (month == 2) 
	{ // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) 
		{
			return false;
		}
	}
	return true; // date is valid
}

function esLink(obj)
{
	var l = document.getElementById(obj).value;

	if (l.indexOf('www') == -1)
	{
		l = 'http://www.' + l;
	}
	
	if (l.substring(0,4) != 'http')
	{
		l = 'http://' + l;
	}
	
	if (l.indexOf('.com') == -1 && l.indexOf('.es') == -1 && l.indexOf('.org') == -1 && l.indexOf('.biz') == -1 && l.indexOf('.net') == -1)
	{
		return false;
	}
	
	return l;
}

function esChecked(obj)
{
	return document.getElementById(obj).checked;
		
}
