/**
 * Enlaces Externos Accesibles
 * (c) 2009 Asesoramiento Pymesweb, S.L.
 * Por Alejandro Arco <http://www.alejandroarco.es>
 */
setExternalLinks=function() {
	if(!document.getElementsByTagName) { return null; }
	
	var anchors = document.getElementsByTagName('a');
	for(var i=0;i<anchors.length;i++) {
		var anchor = anchors[i];
		if(anchor.getAttribute('href') && ((anchor.getAttribute('href').substr(0,7)=='http://' && anchor.getAttribute('href').lastIndexOf(document.domain)==-1) || anchor.className.substr(0,8)=='external')) { anchor.setAttribute('target','_blank'); }
	}
}

if(document.all&&window.attachEvent) { window.attachEvent("onload",setExternalLinks); } // IE-Win
else if(window.addEventListener) { window.addEventListener("load",setExternalLinks,false); } // Otros

/**
 * Validar Buscador
 * (c) 2009 Asesoramiento Pymesweb, S.L.
 * Por Alejandro Arco <http://www.alejandroarco.es>
 */
validateSearch=function() {
	if(!document.frm_src) { return null; }
	
	var q = new Array(document.getElementById('q'), document.frm_src['q'].value);
	var enviar = document.getElementById('sa');
	
	/* Eventos */
	q[0].onfocus = onFocus; q[0].onblur = onBlur;
	enviar.onclick = validateForm;
	
	/* Funciones */
	function onFocus() { if(document.frm_src[this.name].value==eval(this.name)[1]) document.frm_src[this.name].value='' }	
	
	function onBlur() {
		var value = document.frm_src[this.name].value;
		value = value.replace(/^\s*/, ''); value = value.replace(/\s*$/, '');
		if(!value) document.frm_src[this.name].value = eval(this.name)[1]
	}
	
	function validateForm() {
		if(document.frm_src['q'].value==q[1]) { viewError(document.frm_src['q']); return false; }
		return true;
	}
	
	function viewError(el) {
		el.focus();
		alert('Introduzca una cadena de busqueda.');
	}	
}

if(document.all&&window.attachEvent) { window.attachEvent("onload",validateSearch); } // IE-Win
else if(window.addEventListener) { window.addEventListener("load",validateSearch,false); } // Otros

/**
 * Validar Contacto
 * (c) 2009 Asesoramiento Pymesweb, S.L.
 * Por Alejandro Arco <http://www.alejandroarco.es>
 */
validateContact=function() {
	if(!document.frm_con) { return null; }
	var nombre = new Array(document.getElementById('nombre'), document.frm_con['nombre'].value);
	var email = new Array(document.getElementById('email'), document.frm_con['email'].value);
	var telefono = new Array(document.getElementById('telefono'), document.frm_con['telefono'].value);
	var ciudad = new Array(document.getElementById('ciudad'), document.frm_con['ciudad'].value);
	var consulta = new Array(document.getElementById('consulta'), document.frm_con['consulta'].value);
	var privacidad = new Array(document.getElementById('privacidad'), document.frm_con['privacidad'].value);	
	var enviar = document.getElementById('enviar');
	
	/* Eventos */
	nombre[0].onfocus = onFocus; nombre[0].onblur = onBlur;
	email[0].onfocus = onFocus; email[0].onblur = onBlur;
	telefono[0].onfocus = onFocus; telefono[0].onblur = onBlur;
	ciudad[0].onfocus = onFocus; ciudad[0].onblur = onBlur;
	if(/consulta/.test(consulta[1])) { consulta[0].onfocus = onFocus; } consulta[0].onblur = onBlur;
	enviar.onclick = validateForm;
	
	/* Funciones */
	function onFocus() { if(document.frm_con[this.name].value==eval(this.name)[1]) document.frm_con[this.name].value='' }	
	
	function onBlur() {
		var value = document.frm_con[this.name].value;
		value = value.replace(/^\s*/, ''); value = value.replace(/\s*$/, '');
		if(!value) document.frm_con[this.name].value = eval(this.name)[1]
	}
	
	function validateForm() {
		if(document.frm_con['nombre'].value==nombre[1]) { viewError(document.frm_con['nombre']); return false; }
		else if(document.frm_con['email'].value==email[1]) { viewError(document.frm_con['email']); return false; }
		else if(!document.frm_con['privacidad'].checked) { viewError(document.frm_con['privacidad']); return false; }
		return true;
	}
	
	function viewError(el) {
		el.focus();
		alert('El campo "'+ el.name +'" es obligatorio.');
	}
}

if(document.all&&window.attachEvent) { window.attachEvent("onload",validateContact); } // IE-Win
else if(window.addEventListener) { window.addEventListener("load",validateContact,false); } // Otros

/**
 * Abrir Popup
 * (c) 2009 Asesoramiento Pymesweb, S.L.
 * Por Alejandro Arco <http://www.alejandroarco.es>
 */
openPopUp=function() {
	var anchors = document.getElementsByTagName('img');
	
	/* Eventos */
	for(var i=0;i<anchors.length;i++) {
		var anchor = anchors[i];
		if(anchor.parentNode.className.substr(0,5)=='popup') {		
			anchor.parentNode.onclick = onClick;
		}
	}	
	
	/* Funciones */
	function onClick() {
		var img = this.href;		
		var res = this.className.replace('popup_','').split('x');
		var w = res[0];
		var h = res[1];
		var x = (screen.width/2)-(w/2);
		var y = (screen.height/2)-(h/2);
		var features='width='+ w +',height='+ h +',left='+ x +',top='+ y +',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0';
		var popup = window.open('','popup',features);
		/* Popup */		
		if(img.lastIndexOf('youtube')==-1) popup.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" dir="ltr" lang="es"><head><title>' + this.getAttribute('title') + '</title><style type="text/css">* { margin:0; padding:0; border=0; }</style></head><body><img src="'+ this.getAttribute('href') +'" width="100%" height="100%" alt="' + anchor.getAttribute('title') + '" title="' + anchor.getAttribute('title') + '" /></body></html>');
		else {
			var vid = img.split('v=')[1].split('&')[0];					
			popup.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" dir="ltr" lang="es"><head><title>' + this.getAttribute('title') + '</title><style type="text/css">* { margin:0; padding:0; border=0; }</style></head><body><object width="'+ w +'" height="'+ h +'"><param name="movie" value="http://www.youtube.com/v/'+ vid +'&amp;hl=es_ES&amp;fs=1&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;fmt=18"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+ vid +'&amp;hl=es_ES&amp;fs=1&amp;color1=0x2b405b&amp;color2=0x6b8ab6&amp;fmt=18" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'+ w +'" height="'+ h +'"></embed></object></body></html>');
		}
		return false;
	}
}

if(document.all&&window.attachEvent) { window.attachEvent("onload",openPopUp); } // IE-Win
else if(window.addEventListener) { window.addEventListener("load",openPopUp,false); } // Otros

/**
 * Reemplazar HTML
 */
function replaceHTML(el, html) {
	var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
	/*@cc_on
	oldEl.innerHTML = html;
	return oldEl;
	@*/
	var newEl = oldEl.cloneNode(false);
	newEl.innerHTML = html;
	oldEl.parentNode.replaceChild(newEl, oldEl);
	return newEl;
};