﻿/* --------------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
/* ------------------------------------------ COMPONENTE --------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
function JSObj_user_signon()
{
	this.posId = 1;
	this.compId = 1;
	this.lngId = 'es';
	this.contentTypeId = 0;
	this.contentId;
	this.contentVersionId;
	this.varname = ''
	this.user_type = null;
	this.user_role = null;
	
	this.controlUpload = false;
	this.controlUploadLength = 81920; //sromero tamaño máximo de control de subida de archivo (por defecto 80Mb)
	
	this.goPageNewIfOk = '';
	this.goPageNewIfFail = '';	
	this.goPageModifyIfOk = '';
	this.goPageModifyIfFail = '';

	this.do_login = false;
	// sbadal 25.10.2007
	this.FormValidator = new WBEFormValidator();
	this.useSaveEvent = "cms_user_signon_save";
	this.useModifyEvent = "cms_user_modify";
	this.customLoginErrorMsg = null;	// Mensaje de error para personalizar en proyectos.
	
	this.setParams = function (oAjax) {
		var oIdsElem, sId, tempElem;
		
		if (this.user_type==null || this.user_type=='')
			oAjax.addPostParameter('user_type', this.getValue2('user_type', false));
		else
			oAjax.addPostParameter('user_type', this.user_type);

		oAjax.addPostParameter('user_role', this.user_role);
		oAjax.addPostParameter('user_login', this.getValue2('user_login', false));
		oAjax.addPostParameter('user_password', this.getValue2('user_password', false));
		oAjax.addPostParameter('user_start_date', this.getValue2('user_start_date', false));
		oAjax.addPostParameter('user_end_date', this.getValue2('user_end_date', false));
		oAjax.addPostParameter('user_email', this.getValue2('user_email', false));
		oAjax.addPostParameter('user_first_name', this.getValue2('user_first_name', false));
		oAjax.addPostParameter('user_last_name', this.getValue2('user_last_name', false));
		oAjax.addPostParameter('req_user_id', this.reqUserId);

		oIdsElem = eval('document.forms[0].att_ids_' + this.posId);
		if (oIdsElem.length) {
			for (var i = 0; i < oIdsElem.length; i++) {
				sId = oIdsElem[i].value;
				oAjax.addPostParameter('att_input_' + sId , this.getValue(sId));
			}
		} else {
			if(oIdsElem.value != '')
			{
				var aIdsElem = oIdsElem.value.split(',');
				
				for (var i = 0; i < aIdsElem.length; i++) {
					sId = aIdsElem[i];
					oAjax.addPostParameter('att_input_' + sId, this.getValue(sId));
				}
			}
			//sId = oIdsElem.value;
			//oAjax.addPostParameter('att_input_' + sId, this.getValue(sId));
		}

		oAjax.addPostParameter('att_ids', oIdsElem.value);
		oAjax.addPostParameter('content_ct_id', this.contentTypeId);
		oAjax.addPostParameter('content_id', this.contentId);
		oAjax.addPostParameter('lng_id', this.lngId);
		oAjax.addPostParameter('pos_id', this.posId);
		oAjax.addPostParameter('comp_id', this.compId);

		oAjax.addPostParameter('cms_contentV_id', this.contentVersionId);
		oAjax.addPostParameter('category_ids', this.getHtmlSelectFieldValue('category_ids_' + this.posId));
		
		// Categorías, si las hay las coge del constructor de formularios
		var oFormBuilder = new InmediaFormBuilder(this.posId);
		var sValues;
		sValues = '';
		sValues = oFormBuilder.getFieldMultipleValues("category_" + this.posId);
		oAjax.addPostParameter("category_ids", sValues);

		// Contenidos Padre, si los hay
		this.setRelParams(oAjax);
		
		// El valor del CAPTCHA, si está
		if (!(this.contentVersionId) || (this.contentVersionId == 0)) {
		//alert('1');
			var oElemCaptcha = eval('document.InmediaFrm.txt_captcha_' + this.posId);
			if (oElemCaptcha) oAjax.addPostParameter('captcha', oElemCaptcha.value);
		}
	};

	this.setRelParams = function (oAjax) {
		var oFormBuilder = new InmediaFormBuilder(this.posId);
		var sValue = this.getValue2('cnt_parent_relship', false);
		if (sValue == ''){
			sValues = '';
			sValues = oFormBuilder.getFieldMultipleValues("content_parent_" + this.posId);
			oAjax.addPostParameter('cnt_parent_relship', sValues);
		}else{
			oAjax.addPostParameter('cnt_parent_relship', sValue);
		}
	};
	
	// 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 opción
			oDedCheck.checked = false;
			return true;
		}
		return false;
	}

	this.getValue2 = function (sElemName, bFailIfNotExists, sElemId) {
		var oElem = eval('document.InmediaFrm.' + sElemName + '_' + this.posId);
		var sValue = '';

		if ((bFailIfNotExists != undefined) && (bFailIfNotExists == false) && !(oElem)) return '';

		// Si es un fichero y su valor está marcado para borrar.. devuelve vacío
		if (sElemId!=null && this.isChekedForDelete(sElemId)) return sValue;
		
		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;
	};
	
	this.getValue = function (sElemId) {
		return this.getValue2('att_input_' + sElemId, true, sElemId);
	};

	this.getHtmlSelectFieldValue = function (sHtmlField) {
		var sResult = '';
		var elem = eval('document.forms[0].' + sHtmlField);
		if (elem) {
			if (elem.length) {
				for (var i = 0; i < elem.length; i++) {
					//alert('elem ' + elem[i]);
					if (elem[i].type=='hidden' || elem[i].type=='text') {
						if (sResult != '') sResult += ',';
						sResult += elem[i].value;
					} else if (elem[i].type == 'select-multiple') { // Estamos recuperando multiples tipos de contenido relacionados
						for (var j = 0; j < elem[i].length; j++) {
							var oOption = elem[i][j];
							if (oOption.selected) {
								if (sResult != '') sResult += ',';
								sResult += oOption.value;
							}
						}
					} else {
						//alert('elem[i].type ' + elem[i].type);
						if (elem[i].checked || elem[i].selected) {
							if (sResult != '') sResult += ',';
							sResult += elem[i].value;
						}
					}
				}
			} else if (elem.checked) {
				sResult += elem.value;
			} else if (elem.value) {
				sResult += elem.value;
			}
		}
		return sResult;
	};

	/*this.save = function () {
		// sbadal 25.10.2007
		this.saveAndGo(null);
	}*/

	this.save = function () {		
		var oAjax = new WBE_AjaxClass();
		

		// sbadal 25.10.2007
		if (!this.FormValidator.validateForm(document.forms[0], this.posId) || !this.checkCaptcha()) return;

		this.hideOkMsg();
		this.hideNOkMsg();
		this.showSavingMsg();
		this.hideForm();
		window.scroll(0, 0);

		oAjax.clear();
		this.setParams(oAjax);

		xmlObj = (this.contentVersionId) 
			? oAjax.throwEventXML(this.useModifyEvent)
			: oAjax.throwEventXML(this.useSaveEvent);
		
		this.postProcess(xmlObj);
		
		this.hideSavingMsg();
		
	};

	/* Post Proceso del save */
	this.postProcess = function (xmlObj) {
		var bOK = false;
		var oAjax = new WBE_AjaxClass();
		if (xmlObj){
			this.offeringId = oAjax.getXMLNodeValue(xmlObj, 'id');
			bOK = (this.offeringId!=undefined);
		}
		
		if (bOK) {
			this.showOkMsg();
			if (!this.contentVersionId && this.do_login) this.doLogin(oAjax);
			else {
				if (!this.contentVersionId && this.goPageNewIfOk != ''){
					window.location.href = this.goPageNewIfOk;
					return;
				}
				if (this.contentVersionId && this.goPageModifyIfOk != ''){
					window.location.href = this.goPageModifyIfOk;
					return;
				}
			}
		} else {
			if (!this.contentVersionId && this.goPageNewIfFail != ''){
				window.location.href = this.goPageNewIfFail;
				return;
			}
			if (this.contentVersionId && this.goPageModifyIfFail != ''){
				window.location.href = this.goPageModifyIfFail;
				return;
			}
			this.showNOkMsg();
		}
	}
	
	/* Comprueba si existe otro usuario con ese login */
	this.checkDuplicateLogin = function(oField)
	 {
		if (oField.value=='') return true;
		
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		oAjax.addPostParameter("txt_login", oField.value);
		oXmlDoc = oAjax.throwEventXML("user_check_login");
		if (!oXmlDoc) {	alert('Error al intentar entrar.'); return;	}
		var sStatus = oAjax.getXMLNodeValue(oXmlDoc, 's');
		if (sStatus!=0) {	// No es correcto.
			var sErrorMessage = (this.customLoginErrorMsg!=null && this.customLoginErrorMsg!='') 
				? this.customLoginErrorMsg
				: oAjax.getXMLNodeValue(oXmlDoc, 'm');
			alert(sErrorMessage);
			oField.select();
			oField.focus();
			return false;
		} else return true;
	};
	
	this.doLogin = function(oAjax) {
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		oAjax.addPostParameter("txt_login", this.getValue2('user_login', false));
		oAjax.addPostParameter("txt_password", this.getValue2('user_password', false));
		oXmlDoc = oAjax.throwEventXML("user_login");
		if (!oXmlDoc) {	alert('Error al intentar entrar.'); return;	}
		var sStatus = oAjax.getXMLNodeValue(oXmlDoc, 's');
		if (sStatus!=0) {
			var sErrorMessage = oAjax.getXMLNodeValue(oXmlDoc, 'm');
			alert(sErrorMessage);
			return;
		} else {			
			window.location.reload();
		}
	};
	
	this.showForm = function () {
		document.getElementById('signon_form_' + this.posId).style.display = '';
	};
	
	this.hideForm = function () {
		document.getElementById('signon_form_' + this.posId).style.display = 'none';
	};
	
	this.showOkMsg = function () {
		document.getElementById('message_ok_' + this.posId).style.display = '';
	};
	
	this.hideOkMsg = function () {
		document.getElementById('message_ok_' + this.posId).style.display = 'none';
	};
	
	this.showNOkMsg = function () {
		document.getElementById('message_nok_' + this.posId).style.display = '';
	};
	
	this.hideNOkMsg = function () {
		document.getElementById('message_nok_' + this.posId).style.display = 'none';
	};

	this.showSavingMsg = function () {
		document.getElementById('message_saving_' + this.posId).style.display = '';
	};
	
	this.hideSavingMsg = function () {
		document.getElementById('message_saving_' + this.posId).style.display = 'none';
	};
	
	this.showFileUpload = function (sAttId) {
		document.getElementById('file_upload_' + sAttId + '_' + this.posId).style.display = '';
		document.getElementById('file_upload_' + sAttId + '_' + this.posId).height=30;
	};
	
	this.controlFileUpload = function (sAttId) {	
		this.heightFrame = document.getElementById('file_upload_' + sAttId + '_' + this.posId).height;
		if (document.getElementById('upload_loading_' + sAttId + '_' + this.posId)) {
			document.getElementById('file_upload_' + sAttId + '_' + this.posId).height='0';
			document.getElementById('upload_loading_' + sAttId + '_' + this.posId).style.display = '';
		}
		if (this.controlUpload) {
			window.open('ADMIN_PAGE.wbe?cmp_code=CMS_FILE_UPLOAD_CONTROL&guid='+sAttId+'&pObj='+this.varName,'','toolbar=0,scrollbars=0,location=0,status=0,menubar=0,resizable=0,height=170,width=320,top=70,left=100');
		}
	};
	
	this.hideFileUpload = function (sAttId, sImgName) {
		//document.getElementById('file_upload_' + sAttId + '_' + this.posId).style.display = 'none';
		document.getElementById('file_upload_' + sAttId + '_' + this.posId).height=0;
		if (document.getElementById('upload_link_' + sAttId + '_' + this.posId)) {
			document.getElementById('upload_link_' + sAttId + '_' + this.posId).style.display = 'none';
		}
		if (document.getElementById('file_upload_msg_' + sAttId + '_' + this.posId)) {
			document.getElementById('file_upload_msg_' + sAttId + '_' + this.posId).style.display = 'none';
        }
        if (document.getElementById('upload_loading_' + sAttId + '_' + this.posId)) {
            document.getElementById('upload_loading_' + sAttId + '_' + this.posId).style.display = 'none';
        }
		document.getElementById('file_show_' + sAttId + '_' + this.posId).style.display = '';
		document.getElementById('img_' + sAttId + '_' + this.posId).src = 'resources/image/' + sImgName;
		eval('document.forms[0].att_input_' + sAttId + '_' + this.posId).value = sImgName;
	};

	this.hideFileTypeUpload = function (sAttId, sImgName) {
		//document.getElementById('file_upload_' + sAttId + '_' + this.posId).style.display = 'none';
		document.getElementById('file_upload_' + sAttId + '_' + this.posId).height=0;
		if (document.getElementById('upload_link_' + sAttId + '_' + this.posId)) {
			document.getElementById('upload_link_' + sAttId + '_' + this.posId).style.display = 'none';
		}
		if (document.getElementById('file_upload_msg_' + sAttId + '_' + this.posId)) {
			document.getElementById('file_upload_msg_' + sAttId + '_' + this.posId).style.display = 'none';
        }
        if (document.getElementById('upload_loading_' + sAttId + '_' + this.posId)) {
            document.getElementById('upload_loading_' + sAttId + '_' + this.posId).style.display = 'none';
        }
		document.getElementById('file_show_' + sAttId + '_' + this.posId).style.display = '';
		document.getElementById('file_' + sAttId + '_' + this.posId).href = 'resources/image/' + sImgName;
		eval('document.forms[0].att_input_' + sAttId + '_' + this.posId).value = sImgName;
	};

	this.hideImgUpload = function (sAttId, sImgName) {
		//document.getElementById('file_upload_' + sAttId + '_' + this.posId).style.display = 'none';
		document.getElementById('file_upload_' + sAttId + '_' + this.posId).height=0;
		if (document.getElementById('upload_link_' + sAttId + '_' + this.posId)) {
			document.getElementById('upload_link_' + sAttId + '_' + this.posId).style.display = 'none';
		}
		if (document.getElementById('file_upload_msg_' + sAttId + '_' + this.posId)) {
			document.getElementById('file_upload_msg_' + sAttId + '_' + this.posId).style.display = 'none';
        }
        if (document.getElementById('upload_loading_' + sAttId + '_' + this.posId)) {
            document.getElementById('upload_loading_' + sAttId + '_' + this.posId).style.display = 'none';
        }
		document.getElementById('file_show_' + sAttId + '_' + this.posId).style.display = '';
		document.getElementById('img_' + sAttId + '_' + this.posId).src = 'resources/image/s_' + sImgName;
		eval('document.forms[0].att_input_' + sAttId + '_' + this.posId).value = 's_' + sImgName + '||' + sImgName + '|';
	};
	
	this.toggleWindowDates = function (sId) {
		var elem = eval('document.InmediaFrm.trv_shop_window_pub_' + sId + '_' + this.posId);
		if (elem.options[elem.selectedIndex].value == '1') {
			document.getElementById('show_window_dates_' + sId + '_' + this.posId).style.display = '';
		} else {
			document.getElementById('show_window_dates_' + sId + '_' + this.posId).style.display = 'none';
		}
	};
	
	this.increaseFieldSize = function (sId, mode) {
		if (mode=="html") {
			var oFrame = document.getElementById(sId + "___Frame");
			oFrame.height = parseInt(oFrame.height) + 50; 
		} else {
			var elem = eval('document.forms[0].' + sId);
			elem.rows += 5;
		}
	};

	this.decreaseFieldSize = function (sId, mode) {
		if (mode=="html") {
			var oFrame = document.getElementById(sId + "___Frame");
			var i = parseInt(oFrame.height);
			if (i>100) oFrame.height = i - 50; 
		} else {
			var elem = eval('document.forms[0].' + sId);
			if (elem.rows>5) elem.rows -= 5;
		}
	};

	// Elemento del formulario modificado, hace comprobaciones del valor.
	this.itemModified = function (elem, att_id, editorType, elemInitValue) {
		//this.HasModifications = true;
		var bReturn = true;
		if (elem==null || att_id==null || editorType==null) return;
		if (editorType == 'TEXT_UNIQUE_VALUES') {
			bReturn = this.checkFieldValue(elem, att_id, elemInitValue);
		}
		return bReturn;
	};
	
	// Comprueba el valor Ãºnico de un atributo.
	this.checkFieldValue = function (elem, att_id, elemInitValue) {
		var oAjax = new WBE_AjaxClass();
		oAjax.clear();
		oAjax.addPostParameter("ct_id", this.contentTypeId);
		oAjax.addPostParameter("cv_id", this.contentVersionId);
		oAjax.addPostParameter("att_id", att_id);
		oAjax.addPostParameter("value", elem.value);
		var xmlObj = oAjax.throwEventXML("count_version_by_att_value");
		if (xmlObj){
			var iCount = oAjax.getXMLNodeValue(xmlObj, 'c');
			if (iCount!=0) {				
				this.processElem(elem, elemInitValue);				
				return false;
			}
		}
		return true;
	}
	
	this.processElem = function (elem, elemInitValue){
		alert('Existe un contenido con el valor: '+ elemInitValue +'.\nElija otro por favor.');
		elem.value = elemInitValue;
		elem.select();
		elem.focus();
	}
	
	this.checkCaptcha = function() {
		if ((this.contentVersionId) && (this.contentVersionId != 0))  return true;

		var oElemCaptcha = eval('document.InmediaFrm.txt_captcha_' + this.posId);
		if (oElemCaptcha) {
			var oAjax = new WBE_AjaxClass();
			oAjax.clear();
			oAjax.addPostParameter('captcha', oElemCaptcha.value);
			xmlObj = oAjax.throwEventXML("check_captcha");
			if (xmlObj) return true;
			else {
				alert('Error comprobando campo de seguridad');
				oElemCaptcha.focus();
				return false;
			}

		} else {
			return true;
		}
	}
};

/* --------------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
/* ------------------------------------------ EDICIÓN DEL COMPONENTE --------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
function JSObj_user_signon_edit()
{
	this.posId = 1;
	this.compId = 1;
	this.parentPosId = 1;
	this.varName = '';
	this.lngId = 'es';
	this.config_xml_process = 'xml_process';

	// 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
	};
	
	// Muestra todos los campos de formulario SELECT por el problema que tiene el IE. (como en el back azul)
	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
	};
	
	// 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;
	};
	
	this.save = function () {
		window.parent.objLoading.showWindow();
		this.hideSelectBoxes();
		setTimeout(this.varName + '.save2();', 0);
	}

	this.save2 = function () {
		var objConfig = new WBEConfigMgr();
		objConfig.posId = this.posId;
		objConfig.compId = this.compId;
		//objConfig.cache_delete_prefix = '|travel_offerings/front_offering_data.' + this.posId + '|';
		
		objConfig.saveConfig();
	
		var objReloader = new WbePositionReloader();
		objReloader.posId = this.parentPosId;
		objReloader.reload();

		window.parent.___wbeADMIN.hidePopUp();
		window.parent.objLoading.hideWindow();
		this.displaySelectBoxes();
	};

};


/* --------------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
/* ------------------------------------------ AUXILIARES --------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------------- */
function FCKeditor_OnComplete( editorInstance ) {editorInstance.Events.AttachEvent( 'OnBlur' , __FCKeditor_OnBlur ) ; editorInstance.Events.AttachEvent( 'OnFocus', __FCKeditor_OnFocus );	}
/* Al salir del editor */
function __FCKeditor_OnBlur( editorInstance ) {
	var oFrame = document.getElementById(editorInstance.Name + "___Frame");
	editorInstance.ToolbarSet.Collapse(); 
	oFrame.height = parseInt(oFrame.height) - 40; 
	// Copia el valor en el textarea para enviarlo (Importante para pasarlo por ajax)
	eval("document.forms[0]." + editorInstance.Name).value = editorInstance.GetXHTML( true );
};
/* al entrar en el editor */
function __FCKeditor_OnFocus( editorInstance ){ 
	var oFrame = document.getElementById(editorInstance.Name + "___Frame");
	editorInstance.ToolbarSet.Expand() ;  
	oFrame.height = parseInt(oFrame.height) + 40; 
};

