function checkOtherAnswer(id){
	var question = document.getElementById(id);
	var index = question.selectedIndex + 1;
	var customIndex = getCustomAnswerIndex(id);
	if (customIndex == -1){
		return;	// No custom answers
	}
	else 
	{
		var custom_answer = document.getElementById("custom_" + id + "_" + customIndex);
	
		if (customIndex == index){
			custom_answer.disabled=false;
			custom_answer.style.backgroundColor="#FFFFFF";
		}
		else {
			custom_answer.disabled=true;
			custom_answer.value="";
			custom_answer.style.backgroundColor="#EEEEEE";
		}
	}
}

function getCustomAnswerIndex(id){
	var question = document.getElementById(id);
	for (var i = 1; i <= question.childNodes.length; i++){
		var custom_answer = document.getElementById("custom_" + id + "_" + i);
		if (custom_answer != null){
			return i;
		}
	}
	return -1;
}

function submitBoton(form, accion, destino){
	form.action = destino;
	form.accion.value = accion;
	form.submit();
}

function showdiv(divId, page)
{
    document.getElementById(divId).innerHTML = 'Loading...';
    new Ajax.Updater(divId, page, {asynchronous:true, evalScripts:true});
}

function clearMessages(divId)
{
	document.getElementById(divId).innerHTML = '';
}

var OkodeToolTip = {
    init: function() {
        if (
            !document.getElementById ||
            !document.createElement ||
            !document.getElementsByTagName 
        ) {
            return;
        }

		var galleryContainer = document.getElementById('gallery');
        var images = galleryContainer.getElementsByTagName('img');
        var imgLength = images.length;

        for (var j = 0; j < imgLength; j++) {
        	var src;
        	if (images[j].attributes.fullimage != null)
        		src = images[j].attributes.fullimage.value;
        	else
        		src = images[j].src
            var tip = '<div class="tool"><img src="' + src + '" class="tooltip_image"/></div>';
            new Tooltip(images[j], tip, {'html': true, 'classname': 'TooltipObject'});
        }
    }
}

function change_state_divs(value){
	switch(value){
		case '1':
			document.getElementById('type_title').innerHTML = 'Text content';
			document.getElementById('divtext').style.display = 'block';
			document.getElementById('divfile').style.display = 'none';
			document.getElementById('divyoutube').style.display = 'none';
		break;

		case '3':
			document.getElementById('type_title').innerHTML = 'HTML code';
			document.getElementById('divtext').style.display = 'block';
			document.getElementById('divfile').style.display = 'none';
			document.getElementById('divyoutube').style.display = 'none';
		break;

		case '2':
			document.getElementById('divtext').style.display = 'none';
			document.getElementById('divfile').style.display = 'block';
			document.getElementById('divyoutube').style.display = 'none';
		break;

		case '4':
			document.getElementById('divtext').style.display = 'none';
			document.getElementById('divfile').style.display = 'none';
			document.getElementById('divyoutube').style.display = 'block';
		break;
	}
}

function addToNumericElement(elementId,value){

	current = document.getElementById(elementId).value;
	nextValue = parseInt(current) + value;
	if (nextValue < 0) nextValue = 0;
	document.getElementById(elementId).value = nextValue;
}

function changeAwMatLang(id, callback){
	new Ajax.Request(
                        '../includes/actions/changematlang.php',
                        {
                                method: 'get',
                                parameters: '?newawlang='+ id,
                                asynchronous: true,
                                onComplete: callback
                        });
}

// ----------------------------------------
// Create HTTP Request
// ----------------------------------------
function httpRequest(reqType,url,asynch) {

	// Mozilla-based browsers
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		request = new ActiveXObject("Msxml2.XMLHTTP");
		if (!request) {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	// Request could still be null if neither ActiveXObject
	//   initialization succeeded
	if (request) {
		// If the reqType param is POST, then the fifth arg is the POSTed data
		if (reqType.toLowerCase() != "post") {
			initReq(reqType, url, asynch, arguments[3]);
		} else {
			// The POSTed data
			var args = arguments[4];
			var respHandle = arguments[3];
			if (args != null && args.length > 0) {
				initReq(reqType, url, asynch, respHandle, args);
			}
		}
	} else {
		alert("Your browser does not permit the use of all " +
			"of this application's features!");
	}

}

// ----------------------------------------
// Initialize a request object that is already constructed
// ----------------------------------------

function initReq(reqType, url, bool, respHandle) {
	try {
		// Specify the function that will handle the HTTP response
		request.onreadystatechange = respHandle;
		request.open(reqType, url, bool);
		// If the reqType param is POST, then the
		//   fifth argument to the function is the POSTed data
		if (reqType.toLowerCase() == "post") {
			// Set the Content-Type header for a POST request
			request.setRequestHeader("Content-Type", "application/x-ww-form-urlencoded; charset=UTF-8");
			request.send(arguments[4]);
		} else {
			request.send(null);
		}
	} catch (errv) {
		alert("The application cannot contact the server at the moment. " +
		"Please try again in a few seconds.\n" +
		"Error detail: " + errv.message);
	}
}

function changeStateFileFields(checkbox){
	var content = document.getElementsByName('content')[0];
	var mini = document.getElementsByName('mini')[0];
	
	if(checkbox.checked){
		content.disabled = false;
		mini.disabled = false;
	}
	else{
		content.disabled = true;
		mini.disabled = true;
	}
}
