/*
 *	JavaScript para manejar las funciones del módulo de pauta (Definicion del Anuncio)
 *	Derechos Reservados, Grupo Nación GN S.A.
 */

//Cambiar el medio de publicación
function selectMedia(name) {
	medio = getObject("IMPRESO");
	if ("IMPRESO" == name){
		rbutton = getObject("parm_Medio",0);
		if (rbutton) {
			rbutton.checked = true;
			changeStyle(medio,"rowEnable");
			showObject("TIPOANUNCIO");
		};
	} else {
		changeStyle(medio,"rowDisable");
	};
	medio = getObject("DIGITALEIMPRESO");
	if ("DIGITALEIMPRESO" == name){
		rbutton = getObject("parm_Medio",2);
		if (rbutton) {
			rbutton.checked = true;
			changeStyle(medio,"rowEnable");
			showObject("TIPOANUNCIO");
		};
	} else {
		changeStyle(medio,"rowDisable");
	};
}


var	img_caracteristicas_tipo = "/imgs/ayuda/ejemploSencillo";

//Cambiar la imagen de ejemplo 
function changePreviewImg(Parm) {
	img = getObject('EJEMPLOCARACTERISTICAS');
	img.src = img_caracteristicas_tipo+Parm+'.gif';
};

//Cambiar el tipo de anuncio
function selectType(name,caracteristica) {
	if ("ES" == name){
	//	Cambiar el combo de características del anuncio y el preview
		img_caracteristicas_tipo = "/imgs/ayuda/ejemploSencillo";
		combo = getObject("parm_Caracteristica")
		combo.options.length = 0;
		combo.options[combo.options.length] = new Option("Normal", "Normal", false, false);
		combo.options[combo.options.length] = new Option("Amarillo", "AM", false, false);
		changePreviewImg(caracteristica)
		for (i=0; i < combo.options.length; i++){
			combo.options[i].selected = (combo.options[i].value == caracteristica);
		};
	};
	if ("ME" == name){
	//	Cambiar el combo de características del anuncio y el preview
		img_caracteristicas_tipo = "/imgs/ayuda/ejemploModulado";
		combo = getObject("parm_Caracteristica")
		combo.options.length = 0;
		combo.options[combo.options.length] = new Option("Normal", "Normal", false, false);
		combo.options[combo.options.length] = new Option("Amarillo", "AM", false, false);
		combo.options[combo.options.length] = new Option("Gris", "GR", false, false);
		combo.options[combo.options.length] = new Option("Invertido", "IV", false, false);
		changePreviewImg(caracteristica)
		for (i=0; i < combo.options.length; i++){
			combo.options[i].selected = (combo.options[i].value == caracteristica);
		};
	};
}

//Manejar la lista de categorías
function nC(data, parent, name) {
	this.name = name;
	this.parent = parent;
	this.data = data;
};

var	C = new Array();
C[0]=new nC(-2,-1,' << CATEGORÍA >>');

var	categoryList = new Array();
categoryList = C;


function compareText (option1, option2) {
  return option1.text < option2.text ? -1 :
    option1.text > option2.text ? 1 : 0;
}
function compareValue (option1, option2) {
  return option1.value < option2.value ? -1 :
    option1.value > option2.value ? 1 : 0;
}
function compareTextAsFloat (option1, option2) {
  var value1 = parseFloat(option1.text);
  var value2 = parseFloat(option2.text);
  return value1 < value2 ? -1 :
    value1 > value2 ? 1 : 0;
}
function compareValueAsFloat (option1, option2) {
  var value1 = parseFloat(option1.value);
  var value2 = parseFloat(option2.value);
  return value1 < value2 ? -1 :
    value1 > value2 ? 1 : 0;
}
function sortSelect (select, compareFunction) {
  if (!compareFunction)
    compareFunction = compareText;
  var options = new Array (select.options.length);
  for (var i = 0; i < options.length; i++)
    options[i] = 
      new Option (
        select.options[i].text,
        select.options[i].value,
        select.options[i].defaultSelected,
        select.options[i].selected
      );
  options.sort(compareFunction);
  select.options.length = 0;
  for (var i = 0; i < options.length; i++)
    select.options[i] = options[i];
}

function createDropDown(categoria,padre){
    categoryObject = getObject(categoria);
	categoryObject.options.length = 0;
	for (i=0; i < categoryList.length; i++){
		if (categoryList[i] != null) {
			if (categoryList[i].parent == padre) {
				categoryObject.options[categoryObject.options.length] = new Option(categoryList[i].name, categoryList[i].data, false, false);;
			};
		};
	};
	if (categoryObject.options.length > 0){
		sortSelect(categoryObject);
		categoryObject.selectedIndex = 0;
	};
}

function selectDropDown(categoria, seleccion) {
	categoryObject = getObject(categoria);
	for (i=0; i < categoryObject.options.length; i++){
		categoryObject.options[i].selected = (categoryObject.options[i].value == seleccion);
	};
}

function haveContent(seleccion) {
	contenido = false;
	for (i=0; i < categoryList.length && !contenido; i++){
		if (categoryList[i] != null) {
			contenido = (categoryList[i].parent == seleccion);
		};
	};
	return contenido;
}

function validarCategoria() {
	categoriaValida = false;
	categoryObject = getObject('categoryLevel2');
	if (categoryObject != null) {
		if (categoryObject.selectedIndex >= 0){
			if (haveContent(categoryObject.options[categoryObject.selectedIndex].value)) {
				categoryObject2 = getObject('categoryLevel3');
				if (categoryObject2 != null) {
					if (categoryObject2.selectedIndex >= 0){
						categoriaValida = true;
					};
				};
			} else {
				categoriaValida = true;
			};
		}
	}
	return categoriaValida;
};

function traerCategoriaSeleccionada() {
	categoriaValida = "-1";
	categoryObject = getObject('categoryLevel2');
	if (categoryObject != null) {
		if (categoryObject.selectedIndex >= 0){
			if (haveContent(categoryObject.options[categoryObject.selectedIndex].value)) {
				categoryObject2 = getObject('categoryLevel3');
				if (categoryObject2 != null) {
					if (categoryObject2.selectedIndex >= 0){
						categoriaValida = categoryObject2.options[categoryObject2.selectedIndex].value;
					};
				};
			} else {
				categoriaValida = categoryObject.options[categoryObject.selectedIndex].value;
			};
		}
	}
	return categoriaValida;
};

function traerCategoriaPadre(categoria) {
	encontrado = false;
	padre = "-1";
	for (i=0; i < categoryList.length && !encontrado; i++) {
		if (categoryList[i] != null) {
			encontrado = (categoryList[i].data == categoria);
			if (encontrado)	{
				padre = categoryList[i].parent;
			};
		};
	};
	return padre;
};

function seleccionarCategoria(categoria) {
	padre1 = traerCategoriaPadre(categoria);
	if ( padre1 >= 0) {
		padre2 = traerCategoriaPadre(padre1);
		if (padre2 >= 0) {
			selectDropDown('categoryLevel1', padre2);
			showObject('categoryLevel1');
			createDropDown('categoryLevel2', padre2);
			selectDropDown('categoryLevel2', padre1);
			showObject('categoryLevel2');
			createDropDown('categoryLevel3', padre1);
			selectDropDown('categoryLevel3', categoria);
			showObject('categoryLevel3');
		} else {
			selectDropDown('categoryLevel1', padre1);
			showObject('categoryLevel1');
			createDropDown('categoryLevel2', padre1);
			selectDropDown('categoryLevel2', categoria);
			showObject('categoryLevel2');
			hideObject('categoryLevel3');
		}
	} else {
		selectDropDown('categoryLevel1', categoria);
		showObject('categoryLevel1');
		hideObject('categoryLevel2');
		hideObject('categoryLevel3');
	};
};


