﻿function JSObj_subscriber_box()
{
	this.posId = 1;
	this.compId = 1;
	this.lngId = 'es';
	
	this.url_confirm = '';
	this.url_delete = '';


	this.setParams = function (oAjax) {
		this.setCommonParams(oAjax);
		this.setTypesParams(oAjax);
		this.setAttParams(oAjax);
	};

	this.setCommonParams = function(oAjax) {
		oAjax.addPostParameter('lng_id', this.lngId);
		oAjax.addPostParameter('pos_id', this.posId);
		oAjax.addPostParameter('comp_id', this.compId);

		oAjax.addPostParameter('url_confirm', this.url_confirm);
		oAjax.addPostParameter('url_delete', this.url_delete);

		oAjax.addPostParameter('user_id', this.user_id);
		oAjax.addPostParameter('user_cv_id', this.user_cv_id);
	}
	
	this.setTypesParams = function (oAjax) {
		var oIdsElem, sId, sIds;

		oIdsElem = eval('document.forms[0].type_' + this.posId);

		if (oIdsElem) {
			if (oIdsElem.length) {

				var is_first = true;

				for (var i = 0; i < oIdsElem.length; i++) {
					sId = oIdsElem[i].value;
					if (is_first){
						if (oIdsElem[i].checked) {
							sIds = sId + '';
							is_first = false;
						}
					}else{
						if (oIdsElem[i].checked) sIds += ',' + sId;
					}
				}
			} else {
				sId = oIdsElem.value;
				sIds = sId + '';
			}
			
			oAjax.addPostParameter('subs_types', sIds);
		}
	};

	this.setAttParams = function (oAjax) {
		var oIdsElem, sId, tempElem, oLngElem;

		oIdsElem = eval('document.forms[0].att_ids_' + this.posId);
		
		if (oIdsElem) {
			if (oIdsElem.length) {
				for (var i = 0; i < oIdsElem.length; i++) {
					sId = oIdsElem[i].value;
					oAjax.addPostParameter('att_input_' + sId , this.getValue(sId));
				}
			} else {
				sId = oIdsElem.value;
				oAjax.addPostParameter('att_input_' + sId, this.getValue(sId));
			}
		}
	};
	
	// Comprueba, en el caso que sea un fichero si debe eliminar el valor.
	this.isChekedForDelete = function (sElemId) {
		var oDedCheck = eval('document.forms[0].file_del_' + sElemId + '_' + this.posId);
		if (oDedCheck!=null && oDedCheck.checked) {
			// Muestra subir fichero y oculta cambiar
			document.getElementById("file_show_" + sElemId + "_" + this.posId).style.display = "none";
			document.getElementById("upload_link_" + sElemId + "_" + this.posId).style.display = "";
			// desmarca opcin
			oDedCheck.checked = false;
			return true;
		}
		return false;
	}

	this.getValue = function (sElemId) {

		var oElem = eval('document.forms[0].att_input_' + sElemId + '_' + this.posId);
		var sValue = '';

		// Si es un fichero y su valor estÃ¡ marcado para borrar.. devuelve vacÃ­o
		if (this.isChekedForDelete(sElemId)) return sValue;
		try {
			// En caso contrario continÃºa normal
			if (oElem.length) {
				for (var i = 0; i < oElem.length; i++) {
					if (oElem[i].checked) {
						if (sValue != '') sValue += ',';
						sValue += oElem[i].value;
					} else {
						if (oElem[i].selected) {
							if (sValue != '') sValue += ',';
							sValue += oElem[i].value;
						}
					}
				}
			} else {
				if (oElem.type == "checkbox" || oElem.type == "radio") sValue = (oElem.checked) ? oElem.value : "";
					else sValue = oElem.value;
			}
			return sValue;
		} catch (e) {
			return "";
		}
	};

	this.save = function () {
		var oXml;
		var oFormValidator = new WBEFormValidator();
		var oAjax = new WBE_AjaxClass();
		if (oFormValidator.validateForm(document.forms[0], this.posId)) {

			oAjax.clear();
			this.setParams(oAjax);
			oAjax.addPostParameter("email", eval('document.forms[0].email_' + this.posId).value);
			
			oXml = oAjax.throwEventXML("wbe_subscriber_save");
	
			this.parseResult(oAjax, oXml);
		}
	};
	
	this.confirm = function () {
		var oXml;
		var oFormValidator = new WBEFormValidator();
		var oAjax = new WBE_AjaxClass();
		if (oFormValidator.validateForm(document.forms[0], this.posId)) {

			oAjax.clear();
			//this.setParams(oAjax);
			oAjax.addPostParameter("email", eval('document.forms[0].email_' + this.posId).value);
			
			oXml = oAjax.throwEventXML("wbe_subscriber_confirm");
	
			this.parseResult(oAjax, oXml);
		}
	};
	
	this.deleteSubscription = function () {
		var oXml;
		var oFormValidator = new WBEFormValidator();
		var oAjax = new WBE_AjaxClass();
		if (oFormValidator.validateForm(document.forms[0], this.posId)) {

			oAjax.clear();
			//this.setParams(oAjax);
			oAjax.addPostParameter("email", eval('document.forms[0].email_' + this.posId).value);
			
			oXml = oAjax.throwEventXML("wbe_subscriber_delete");
	
			this.parseResult(oAjax, oXml);
		}
	};
	
	this.parseResult = function (oAjax, oXml) {
		if (oXml) {
			var OkMsg = oAjax.getXMLNodeValue(oXml, 'ok');

			if (OkMsg) {
				this.hideForm();
				this.showOkMsg();
				
				document.getElementById('suscribir_txt_ok').innerHTML = '<h1>' + OkMsg + '</h1>';
			} else {
				this.hideForm();
				this.showNoOkMsg();
				
				var ErrMsg = oAjax.getXMLNodeValue(oXml, 'error');
				if (ErrMsg) {
					document.getElementById('suscribir_txt_fail').innerHTML = '<h1>' + ErrMsg + '</h1>';
				}
			}
		} else {
			this.hideForm();
			this.showNoOkMsg();
		}
	}
	
	this.showOkMsg = function () {
		document.getElementById('suscription_box_ok_' + this.posId).style.display = '';
	};

	this.showNoOkMsg = function () {
		document.getElementById('suscription_box_mail_exists_' + this.posId).style.display = '';
	};

	this.hideForm = function () {
		document.getElementById('suscription_box_' + this.posId).style.display = 'none';
	};
	
	this.showForm = function () {
		eval('document.InmediaFrm.email_' + this.posId).value = '';
		document.getElementById('suscription_box_' + this.posId).style.display = '';
		document.getElementById('suscription_box_ok_' + this.posId).style.display = 'none';
		document.getElementById('suscription_box_mail_exists_' + this.posId).style.display = 'none';
	};
	
};



/* ******************************************************** */
/* ******************************************************** */
//							EDITOR
/* ******************************************************** */
/* ******************************************************** */

function JSObj_subscriber_box_edit()
{
	this.posId = 1;		// Posición en la que se ha cargado el componente
	this.currentTab = 1;
	this.parentPosId = 1;
	this.compId = 1;
	this.lngId = 'es';
	this.config_xml_process = 'xml_process';

	this.LINKTYPE = "6";
	this.EMAILTYPE = "16";
	
	this.save = function () {

		window.parent.objLoading.showWindow();

		var oTxtMgr = new WBETextsMgr();
		oTxtMgr.posId = this.posId;
		oTxtMgr.compId = this.compId;
		oTxtMgr.saveTexts();

		var objConfig = new WBEConfigMgr();
		objConfig.posId = this.posId;
		objConfig.compId = this.compId;
		objConfig.cache_delete_prefix = "|wbe/subscriber_box." + this.posId + "|";
		objConfig.saveConfig();

		window.parent.___wbeADMIN.hidePopUp();
		window.parent.objLoading.hideWindow();
		
		var objReloader = new WbePositionReloader();
		objReloader.posId = this.parentPosId;
		objReloader.reload();
	};

	// Mustra la ruta de la xsl al seleccionar algún proceso
	this.changeProcess = function () {
		var dest_div = document.getElementById('xsl_path_' + this.posId);
		var elem_sel = eval('document.InmediaFrm.config_' + this.config_xml_process + '_' + this.posId);
		var from_div = document.getElementById('xsl_path_' + this.posId + '_' + elem_sel.selectedIndex);
		dest_div.innerHTML = from_div.innerHTML;
	};
	
	// Oculta todos los campos de formulario SELECT por el problema que tiene el IE. (como en el back azul)
	this.hideSelectBoxes = function () {
		for(var i = 0; i < document.forms.length; i++) {
			for(var e = 0; e < document.forms[i].length; e++){
				if(document.forms[i].elements[e].tagName == "SELECT")
					document.forms[i].elements[e].style.visibility="hidden";
			} // for e
		} // for i
	};
	
	this.displaySelectBoxes = function () {
		for(var i = 0; i < document.forms.length; i++) {
			for(var e = 0; e < document.forms[i].length; e++){
				if(document.forms[i].elements[e].tagName == "SELECT")
					document.forms[i].elements[e].style.visibility="visible";
			} // for e
		} // for i
	};
	
}



