/* DOM */
window.onload = initPage;

function initPage(){
	var c1;
	if($id('noticias')) $id('noticias').onclick = function(){ getURL('http://www.escueladavinci.net/noticias'); return false; }
	if($id('intranet')) $id('intranet').onclick = function(){ getURL('http://intranet.escueladavinci.net/'); return false; }
	if($id('banner_footer')) $id('banner_footer').onclick = function(){ getURL('http://karma-magazine.com/convocatoria03.html'); return false; }
	/* cursos de formacion profesional */
	if($id('pixologic_video')) $id('pixologic_video').onclick = function(){ getURL('http://www.pixologic.com/zbrush/industry/'); return false; }
	var opciones = ['cursos','actividades','carrera','institucional'];
	for(var i in opciones){
		if( $id( opciones[i] ) != undefined ){ matchCols( $id( opciones[i] ) ); }
	}
	rollOverBtn();
}

function getURL(u){	window.open( u , 'target'); }

function matchCols(ob){
	var c1 = $id('columna_izquierda');
	var a = Math.max( parseInt(c1.offsetHeight), parseInt(ob.offsetHeight));
	if(a > 500){ c1.style.height =  ob.style.height = a+'px'; }
}

function trim(JSvalue) { 
	var JStemp = JSvalue;
	var JSobj = /^(\s*)([\W\w]*)(\b\s*$)/;
	if (JSobj.test(JStemp)) { JStemp = JStemp.replace(JSobj, '$2'); }
	var JSobj = / +/g;
	JStemp = JStemp.replace(JSobj, " ");
	if (JStemp == " ") { JStemp = ""; }
	return JStemp;
}

function isMail(e){
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return emailPattern.test(e);	
}

function check_form(e){
	var f1 = $id('f1'); var f2 = $id('f2'); var f3 = $id('f3'); var f4 = $id('f4'); 
	var v1 = trim(f1.value); var v2 = trim(f2.value); var v3 = trim(f3.value); var v4 = trim(f4.value); 
	f1.value = v1; f2.value = v2; f3.value = v3; f4.value = v4;
	return ( v1 == '' || v2 == '' || !isMail(v3) || v3 == '' || v4 == '' ) ? false : true ;
}





//////////////////////
var uA = window.navigator.userAgent;
var op
function mostrar_publicidad(){
var html = '<h4>D&iacute;a Internacional de la Animaci&oacute;n</h4>';
	html+= '<p>La <accronym title="Aociaci&oacute;n Argentina de Cine de Animaci&oacute;n">AACA</accronym> ';
	html+= 'te invita a su <strong>VI Muestra Internacional</strong> el 28 de Octubre desde las 16:00 hs en el <strong>Sal&oacute;n Auditorio de la Escuela DaVinci</strong></p>';
	html+= '<a href="http://www.escueladavinci.net/actividades/evento/evento-130.php">M&aacute;s informaci&oacute;n e inscripciones</a>';

publicidad ( html , 400 , 100 , true, false);	
}

function publicidad ( html , ancho , opacidad , closeBtn, todasLasPaginas ){
	/* 	html: contenido del div.
		ancho: ancho del div, por defecto es 250px - se centra automaticamente
		opacidad: valor de 1 a 100 que representa el porcentaje de opacidad, por defecto es 100
		closeBtn: booleano que indica si va o no el boton de cerrar
		todasLasPaginas: booleano si es true el div aparece en cada seccion. si es false solo en la home
	*/
	html = html.replace(/\|\|/g , '<');
	html = html.replace(/##/g, '>');
	if( todasLasPaginas == undefined) { todasLasPaginas = false; }
	if( !todasLasPaginas && (window.location.search == '' || window.location.search == undefined) ){
	if(opacidad == undefined || isNaN(opacidad) ){ opacidad = 100;  }
	var advice = document.createElement('div');
		advice.style.MozBorderRadius = '10px';
		advice.style.WebkitBorderRadius = '10px';
		advice.style.borderRadius = '10px';
	if( uA.indexOf('MSIE 6') != -1 ){ 
		advice.style.position = 'absolute'; 
		left = (770 - parseInt(advice.style.width) ) / 2;
		setInterval( function(){ advice.style.top = (document.documentElement.scrollTop + 140) + 'px';  } , 10 );
	}
		op = opacidad;
	var inner =  document.createElement('div');
		inner.innerHTML = html;
	if( closeBtn ){
	var btn = document.createElement('button');
		btn.onclick = function(){ fade( advice ); }
		btn.innerHTML = '<b>cerrar</b>';
		advice.appendChild(btn);
	}
		advice.appendChild(inner);
		document.getElementById('contenedor').appendChild(advice);
		advice.style.width = ( ancho != undefined && !isNaN(ancho) ) ? ancho +'px': '250px';		
		var left = (screen.width - parseInt(advice.style.width)) / 2;
		var right = parseInt((document.documentElement.offsetWidth - 395) / 2 , 10 ) - 10;
		advice.id = 'publicidad';
		/*
		advice.style.right= ( right < 10 ) ? '0px' : right+'px';
		*/
		advice.style.opacity = op/100;
		advice.style.filter = 'alpha(opacity='+op+')';
	}
}


function cerrarPublicidad(){
	var advice = $id('publicidad');
	fade( advice );
}

function fade(e){
	var fx = setInterval(remove,10);
	function remove(){
		op-= 10; 
		e.style.opacity = op/100;
		e.style.filter = 'alpha(opacity='+op+')';
		if(op <= 20){ e.parentNode.removeChild(e); clearInterval(fx);  }
	}
}



function isIex(){
	var uA = window.navigator.userAgent;
	return ( uA.indexOf('MSIE 6') != -1 ) ? true : false ;
}

function showEmpresas(){
	var div = make('div');
	var div2 = make('div');
	var div3 = make('div');
	//agregado
	var div4 = make('div');
	var a = make('a');
		a.href= '#';
		a.innerHTML = 'cerrar';
		a.id = 'cerrar';
		div4.id = 'limpiarFlotacion'
		a.onclick = function(){ remove(div); remove(div2); return false; }
		add(a , div2 );
		add(div4 , div2);
		div.id = 'modal';
		div2.id = 'innerModalEmpresas';
		div.style.width = document.documentElement.clientWidth+'px';
		div.style.height= document.documentElement.clientHeight+'px';
		if( isIex() ){ div.style.filter = 'Alpha(opacity=50);';	}else{ div.style.opacity = 0.5;	}
		toFixed( div , 0 );
		toFixed( div2, 10);
		add(div , document.body);
		add(div2 , document.body);
		add(div3 , div2);
		var ajax = nuevoAjax();
			ajax.open("POST", '/institucional/popup_empresas.php',true);
			ajax.onreadystatechange=function() { if (ajax.readyState==4){ div3.innerHTML = ajax.responseText;  } }
			ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			ajax.send(null);
		div2.style.left = (Math.round( (document.documentElement.clientWidth - 800) / 2 ) - 15)+ 'px';
		div2.style.height = ( document.documentElement.clientHeight - 75) +'px';

}

function toFixed( e , top ){
if( isIex() ){ e.style.position = 'absolute'; setInterval( function(){ e.style.top = (document.documentElement.scrollTop + top) + 'px';  } , 5 );
	}
}

function rollOverBtn(){
	if( $id( 'consultanos_img' ) ){
		addEvent( $id( 'consultanos_img' ), 'mouseover', ContactBtnOn ) ;
		addEvent( $id( 'consultanos_img' ), 'mouseout', ContactBtnOut ) ;
	}
}

function ContactBtnOn( obj ){
	$id( 'consultanos_img' ).src = '/libs/icons/btn_consultanos-hover.jpg';
}

function ContactBtnOut(){
	$id( 'consultanos_img' ).src = '/libs/icons/btn_consultanos.jpg';
}

function addEvent(obj,type,fn) {
	if (obj.addEventListener) {
		obj.addEventListener(type,fn,false);
		return true;
	} else if (obj.attachEvent) {
		obj['e'+type+fn] = fn;
		obj[type+fn] = function() { obj['e'+type+fn]( window.event );}
		var r = obj.attachEvent('on'+type, obj[type+fn]);
		return r;
	} else {
		obj['on'+type] = fn;
		return true;
	}
}

function nuevoAjax(){ 
	var xmlhttp=false; 
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; } }
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') { xmlhttp = new XMLHttpRequest(); }
	return xmlhttp;
}


/* DOM */
function $id(o){ return document.getElementById(o); }
function $tag(t){ return document.getElementsByTagName(t); }
function make(e){ return document.createElement(e); }
function add(e , p){ p.appendChild(e); }
function remove(e){ e.parentNode.removeChild(e); }

function Ajax( responseDiv , requestedPHP , sendByPost ){		if( sendByPost == undefined ){ sendByPost = true ; }
	this.datos 			= 	[] ;
	this.callbackArgs	=	[];
	this.ajax 			= 	nuevoAjax( );
	this.place 			= 	responseDiv ; // must be an HTML object
	this.php 			= 	requestedPHP ;
	this.method 		= 	( sendByPost ) ? 'POST' : 'GET' ;

	this.callback		= 	function() { return false; }
	this.setCallback	= 	function ( f ) { this.callback = f ; }
	this.agregarArg		=	function( dato ){ this.callbackArgs.push( dato ); }
	this.agregarDato 	= 	function( variable , valor ){ this.datos.push( variable + '=' + valor ) ; }
	this.ejecutar 		= 	function()
								{
								var ajax 					= 				this.ajax ;
								var place 					= 				this.place ;
								var callback				= 				this.callback ;
								var callbackArgs			= 			( 	this.callbackArgs.length == 0 	) ? null : this.callbackArgs ;
								var datos 					=			( 	this.datos.length == 0 			) ? null : this.datos.join( '&' ) ;
								this.ajax.open							( 	this.method , this.php , true 	);
								this.ajax.setRequestHeader	("Content-Type", "application/x-www-form-urlencoded");
								this.ajax.onreadystatechange	= 	function() 
																	{
																		if (ajax.readyState==4)
																		{ 
																			place.innerHTML = ajax.responseText ;  
																			callback( callbackArgs ) ; // function post
																		}
																	}
								this.ajax.send( datos );
								}
	
}



function move_to( way )
{
	if( way == 'prev' )
	{
		mes = mes - 1 ;
		if( mes == 0 ){ mes = 12 ; anio = anio - 1; }
	}else{
		mes = mes + 1 ;
		if( mes == 13 ){ mes = 1 ; anio = anio + 1; }
	}

	var ajax = new Ajax( $id('grid_container') , 'acciones/calendar_grid.php' );	
		ajax.agregarDato( 'm' , mes ) ;
		ajax.agregarDato( 'y' , anio ) ;
		ajax.ejecutar();
}
