/**
 * Miscelaneous
 *
 * Miscelaneous methods for convenience use on the website.
 *
 * @version 0.10 Apr.11.2004 First Approach.
 * @version 0.15 Apr.17.2008 Trivia Functions working correctly.
 * @version 0.20 Apr.23.2008 Added handleEmail() function, to 
 * properly handle input from email-field at index.html
 * @version 0.25 Apr.24.2008 Added loadContent() function.
 *
 * @author Luis Fernando González Fernández
 * 	   Rafael Alejandro Huerta Pérez
 */
/*
window.addEvent('domready', function(){
	var history = new HistoryManager();
	history.addEvent('onHistoryChange', handleHistoryChange);
	firstState = history.getCurrentLocation();
	if(firstState == null || firstState == "") /*Load Content Divs loadContent('content-sidebar-right', 'contenido', null, true);
	else history.fireEvent('onHistoryChange', firstState);
});
*/
/**
 * Asynchronously loads the specified script result into the
 * specified block-level element.
 *
 * @param divId The id to identify the block-level element in which
 * response from the specified script should be loaded.
 * @param script The name of the script which will be queried.
 * @param params A Hash-Table contaning parameters which will be pased
 * by URL to script.
 */
function loadContent(divId, script, params, dontAttach){
	var url = 'contenido/' + script + '.php';
	new Ajax(url, {
		method: 'get',
		data: params,
		evalScripts: true,
		update: $(divId)
	}).request();
	/*
	if(typeof dontAttach == 'undefined'){
		var history = new HistoryManager();
		var windowLocationHash = divId + '#' + script;
		if(params != undefined && !(params === null)) windowLocationHash += '#';
		var hasParams = false;
		for(var param in params){
			windowLocationHash += param + '=' + params[param] + '&';
			hasParams = true;
		}
		if(hasParams){
			windowLocationHash = windowLocationHash.substr(0, windowLocationHash.length - 1);
		}
		history.addState(encodeURIComponent(windowLocationHash));
	}
	*/
}

/**
 * Validates the radio buttons on Trivia Area, then submits the answer.
 *
 * @return true if an option was selected. false if no option was selected.
 */
function submitTrivia(){
	var id_question = document.getElementById('id_pregunta').value;
	var answers = document.encuesta.respuesta;
	var i;
	for(i = 0 ; i < answers.length; i++){
		if(answers[i].checked) break;
	}
	if(i >= answers.length){
		alert('Selecciona una respuesta.');
		return false;
	}
	var selected_answer = answers[i];
	loadContent('encuesta', 'encuesta', {'submitted':true, 'id_pregunta':id_question, 'id_respuesta':selected_answer.value}, true);
	return true;
}

/**
 * Shows current Trivia voting data on index.html Trivia area.
 */
function viewTriviaResults(){
	var id_question = document.getElementById('id_pregunta').value;
	loadContent('encuesta', 'resultados_encuesta', {'id_pregunta':id_question}, true);
}

/**
 * Splits email-field value into username and domainName.
 * According to domainName, selects the appropiate mail server,
 * and directs mail to it.
 */
function handleEmail(){
	var email = document.getElementById('email-field').value;
	
	var splitIndex = email.lastIndexOf("@");

	if(splitIndex != -1){
		username = email.substring(0, splitIndex);
		domainName = email.substring(splitIndex + 1);
	}
	//alert("Username: " + username + ", DomainName: " + domainName);
	var mailServer = '';
	var validDomain = false;

	switch(domainName){
		case "cencar.udg.mx":
			mailServer = 'webmail.udg.mx:80/iwc';
			validDomain = true;
			break;
		case "mail.udg.mx":
			mailServer = 'webmail.udg.mx:80/iwc';
			validDomain = true;
			break;
		case "cualtos.udg.mx":
			mailServer = 'webmail.udg.mx:80';
			validDomain = true;
			break;
		case "cucba.udg.mx":
			mailServer = 'maiz.cucba.udg.mx:81';
			validDomain = true;
			break;
		case "lagos.udg.mx":
			mailServer = 'afrodita.lagos.udg.mx:80';
			validDomain = true;
			break;
		case "red.cucei.udg.mx":
			mailServer = 'quantum.red.cucei.udg.mx:80';
			validDomain = true;
			break;
		case "cuci.udg.mx":
			mailServer = 'correo.cuci.udg.mx:80';
			validDomain = true;
			break;
		case "cucea.udg.mx":
			mailServer = 'wendy.cucea.udg.mx:80';
			validDomain = true;
			break;
		case "red.radio.udg.mx":
			mailServer = 'red.radio.udg.mx:81';
			validDomain = true;
			break;
		default:
			mailServer='webmail.udg.mx:80';
			break;
	}

	if (validDomain){
		var targetAction = "http://" + mailServer + "/amserver/UI/Login?goto=http://" + mailServer + "/iwc/&gotoOnFail=http://" + mailServer + "/iwc/?err=1&module=LDAP" + "&org=" + domainName;
		document.emailForm.action = targetAction;
		document.getElementById('email-field').value = username;
		return true;
	}
	else{
		return false;
	}
}

function redirectServer(server){
	if(server != -1){
		window.location = server;
	}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
	eval(targ + ".location='" + selObj.options[selObj.selectedIndex].value + "'");
	if (restore) selObj.selectedIndex = 0;
}

function displayContent(content, display){
	document.getElementById(content).style.display = display;
}

function changeClassName(element, newClass){
	document.getElementById(element).className = newClass;
}

function contactPopup(to){
	var url = "contenido/formulario_correo.php";
	if(typeof to != 'undefined'){
		url += "?to=" + to;
	}
	window.open(url, "Contacto", "status = 1, height = 490, width = 590, resizable = 0");
}

function contactPopup(to){
	var url = "contenido/formulario_correo.php";
	if(typeof to != 'undefined'){
		url += "?to=" + to;
	}
	window.open(url, "Contacto", "status = 1, height = 490, width = 590, resizable = 0");
}

function sendToFriendPopup(reference_url){
	var url = "contenido/formulario_enviar_amigo.php";
	if(typeof reference_url != 'undefined'){
		url += "?reference_url=" + reference_url;
	}
	window.open(url, "Enviar a un Amigo", "status = 1, height = 390, width = 590, resizable = 0");
}

function printPage(){
	if(window.print) window.print();
	else alert('Hubo un error interno al intentar llamar el diálogo de impresión.');
}

/**
 * Movable Popup
**/

function popupRelocate(){
	var scrolledX, scrolledY;
	if(self.pageYOffset){
   		scrolledX = self.pageXOffset;
   		scrolledY = self.pageYOffset;
 	}
	else if(document.documentElement && document.documentElement.scrollTop){
   		scrolledX = document.documentElement.scrollLeft;
   		scrolledY = document.documentElement.scrollTop;
 	}
	else if(document.body){
   		scrolledX = document.body.scrollLeft;
   		scrolledY = document.body.scrollTop;
 	}

	var centerX, centerY;
 	if(self.innerHeight){
   		centerX = self.innerWidth;
   		centerY = self.innerHeight;
 	}
	else if(document.documentElement && document.documentElement.clientHeight){
   		centerX = document.documentElement.clientWidth;
   		centerY = document.documentElement.clientHeight;
 	}
	else if(document.body){
   		centerX = document.body.clientWidth;
   		centerY = document.body.clientHeight;
 	}

 	var leftOffset = scrolledX + (centerX - 380) / 2;
 	var topOffset = scrolledY + (centerY - 300) / 2;

 	document.getElementById("popup2").style.top = topOffset + "px";
 	document.getElementById("popup2").style.left = leftOffset + "px";
}

function popupClose(){
 	document.getElementById("popup").style.display = "none";
}

/* COMENTADO LA ACCION QUE MUESTRA EL POPUP*/
/*window.addEvent('load', function(){
	
	popupRelocate();
 	document.getElementById("popup2").style.display = "block";
	document.getElementById("avisos2").style.top = "-400px";
 	document.body.onscroll = popupRelocate;
 	window.onscroll = popupRelocate;
});*/

/*
function handleHistoryChange(){
	//parse URL
	var history = new HistoryManager();
	var urlParts = decodeURIComponent(history.getCurrentLocation()).split('#');
	
	if(urlParts.length >= 2){
		var evalContent = "loadContent('" + urlParts[0] + "', '" + urlParts[1] + "'";

		// Parse Parameters
		if(urlParts.length > 2){
			var params = urlParts[2].split('&');
			evalContent += ', {';
			for(var i = 0; i < params.length; i++){
				var paramName = params[i].split('=')[0];
				var paramValue = params[i].split('=')[1];
				evalContent += "'" + paramName + "': " + paramValue + ', ';
			}
			evalContent = evalContent.substring(0, evalContent.length - 2);
			evalContent += '}';
		}
		evalContent += ");";
		//alert('Evaluating :' + evalContent);
		eval(evalContent);
	}
	else loadContent('content-sidebar-right', 'contenido', null, true);
}
*/
/******************/
//omarb@redudg.udg.mx
//javascript:alert(handleEmail());
function handleEmail2(){
	var email = document.getElementById('email-field').value;

	var splitIndex = email.lastIndexOf("@");

	if(splitIndex != -1){
		username = email.substring(0, splitIndex);
		domainName = email.substring(splitIndex + 1);
	}
	//alert("Username: " + username + ", DomainName: " + domainName);
	var mailServer		= '';
	var destination		= '';
//	var username		= 'username';
//	var password		= 'password';
	var value_password	= document.getElementById('password').value;
	var value_username	= username;
	
	document.getElementById('email-field').value	= username;
	document.getElementById('username').value		= username;
	document.getElementById('password').value		= document.getElementById('password-field').value;
	document.getElementById('destination').value	= "https://correo.udg.mx/exchange";

	var https		= false;
	var validDomain	= false;

	switch(domainName){
		case "cencar.udg.mx":
			mailServer = 'webmail.udg.mx:80';
			validDomain = true;
			break;
		case "mail.udg.mx":
			mailServer = 'webmail.udg.mx:80';
			validDomain = true;
			break;
		case "cualtos.udg.mx":
			mailServer = 'webmail.udg.mx:80';
			validDomain = true;
			break;
		case "cucba.udg.mx":
			mailServer = 'maiz.cucba.udg.mx:81';
			validDomain = true;
			break;
		case "lagos.udg.mx":
			mailServer = 'afrodita.lagos.udg.mx:80';
			validDomain = true;
			break;
		case "red.cucei.udg.mx":
			mailServer = 'quantum.red.cucei.udg.mx:80';
			validDomain = true;
			break;
		case "cuci.udg.mx":
			mailServer = 'correo.cuci.udg.mx:80';
			validDomain = true;
			break;
		case "cucea.udg.mx":
			mailServer = 'wendy.cucea.udg.mx:80';
			validDomain = true;
			break;
		case "red.radio.udg.mx":
			mailServer = 'red.radio.udg.mx:81';
			validDomain = true;
			break;
		case "cucei.udg.mx":/*nose*/
			mailServer	= "https://"+ value_username + ":" + value_password +"cucei.udg.edu.mx/exchange";
			validDomain	= true;
			https		= true;
			break;
		case "csh.udg.mx":/*nose*/
			mailServer	= "https://"+ value_username + ":" + value_password +"cucsh.udg.edu.mx/exchange";
			validDomain	= true;
			https		= true;
			break;
		case "cucsur.udg.mx":
			mailServer	= "https://"+ value_username + ":" + value_password +"cucsur.udg.edu.mx/exchange";
			validDomain	= true;
			https		= true;
			break;
		case "cusur.udg.mx":
			mailServer	= "https://"+ value_username + ":" + value_password +"cusur.udg.edu.mx/exchange";
			validDomain	= true;
			https		= true;
			break;
		case "cuvalles.udg.mx":
			mailServer	= "https://"+ value_username + ":" + value_password +"cuvalles.udg.edu.mx/exchange";
			validDomain	= true;
			https		= true;
			break;
		case "cepe.udg.mx":
			mailServer	= "https://"+ value_username + ":" + value_password +"cepe.udg.edu.mx/exchange";
			validDomain	= true;
			https		= true;
			break;
		case "proulex.udg.mx":
			mailServer	= "https://"+ value_username + ":" + value_password +"proulex.udg.edu.mx/exchange";
			validDomain	= true;
			https		= true;
			break;
		case "sems.udg.mx":
			mailServer	= "https://"+ value_username + ":" + value_password +"sems.udg.edu.mx/exchange";
			validDomain	= true;
			https		= true;
			break;
			
		case "redudg.udg.mx":
			mailServer	= 'https://correo.udg.mx/exchweb/bin/auth/owaauth.dll';
			validDomain	= true;
			https		= true;
			break;
		case "cucs.udg.mx":
			mailServer	= 'https://correo.udg.mx/exchweb/bin/auth/owaauth.dll';
//			mailServer	= 'https://correo.udg.mx/owa/';
			validDomain	= true;
			https		= true;
			break;
		case "correo.udg.mx":
			mailServer	= 'https://correo.udg.mx/exchweb/bin/auth/owaauth.dll';
//			mailServer	= 'https://correo.udg.mx/exchange';
			validDomain	= true;
			https		= true;
			break;
				
			
		default:
			mailServer='webmail.udg.mx:80';
			break;
	}

	if (validDomain)
	{
		if(https)
		{
			document.emailForm.action	= mailServer;
			return true;
		}
		else
		{
			var targetAction = "http://" + mailServer + "/amserver/UI/Login?goto=http://" + mailServer + "/iwc/&gotoOnFail=http://" + mailServer + "/iwc/?err=1&module=LDAP" + "&org=" + domainName;
			document.emailForm.action = targetAction;
			document.getElementById('email-field').value = username;
			return true;
		}
	}
	else{
		return false;
	}
}