/**
 * 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';
			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;
		default:
			mailServer='webmail.udg.mx:80';
			break;
	}

	if (validDomain){
		var targetAction = "http://" + mailServer + "/amserver/UI/Login?goto=http://" + mailServer + "/uwc/&gotoOnFail=http://" + mailServer + "/uwc/?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 = 390, 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 = 390, 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.');
}
/*
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);
}
*/
