﻿function JSObj_user_address()
{
	this.posId = 1;
	this.compId = 1;
	this.lngId = 'es';
	this.contentTypeId = 0;
	this.contentVersionId;
	this.varname = ''
	this.user_type = null;
	this.user_role = null;
	
	this.xtra_sufix = '';
	
	this.goPageNewIfOk = '';
	this.goPageNewIfFail = '';	
	this.goPageModifyIfOk = '';
	this.goPageModifyIfFail = '';

	this.do_login = false;
	// sbadal 25.10.2007
	this.FormValidator = new WBEFormValidator();
	this.useSaveEvent = "user_address_signon_save";
	this.useModifyEvent = "user_address_modify_save";
	this.customLoginErrorMsg = null;	// Mensaje de error para personalizar en proyectos.	

	// Acciones antes de llamar al evento
	this.savePre = function() {}

	this.spainId = '50';
	
	// Acciones a realizar despus de guardar
	this.savePost = function() { 
	window.location.reload();
	}	
	
	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_email', this.getValue2('user_login', false));
		
		oAjax.addPostParameter('nombre_1', this.getValue2('nombre_1', false));
		oAjax.addPostParameter('user_first_name', this.getValue2('nombre_1', false));
		oAjax.addPostParameter('apellidos_1', this.getValue2('apellidos_1', false));
		oAjax.addPostParameter('user_last_name', this.getValue2('apellidos_1', false));
		oAjax.addPostParameter('nif_1', this.getValue2('nif_1', false));
		oAjax.addPostParameter('direccion_1', this.getValue2('direccion_1', false));
		oAjax.addPostParameter('localidad_1', this.getValue2('localidad_1', false));
		oAjax.addPostParameter('codigo_postal_1', this.getValue2('codigo_postal_1', false));
		oAjax.addPostParameter('provincia_1', this.getValue2('provincia_1', false));
		oAjax.addPostParameter('provincia_nombre_1', this.getValue2('provincia_nombre_1', false));
		oAjax.addPostParameter('telefono_1', this.getValue2('telefono_1', false));
		oAjax.addPostParameter('fax_1', this.getValue2('fax_1', false));
		oAjax.addPostParameter('fecha_nacimiento_1', this.getValue2('fecha_nacimiento_1', false));
		oAjax.addPostParameter('observaciones_1', this.getValue2('observaciones_1', false));

		oAjax.addPostParameter('tipo_direccion_1', this.getValue2('tipo_direccion_1', false));
		oAjax.addPostParameter('pais_1', this.getValue2('pais_1', false));
		oAjax.addPostParameter('referencia_1', this.getValue2('referencia_1', false));
		oAjax.addPostParameter('user_fk', this.getValue2('user_fk_1', false));
		
		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', 25/*this.contentTypeId*/);
		oAjax.addPostParameter('content_ct_code', 'usuario');
		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);
	};
	
	this.getValue2 = function (sElemName, bFailIfNotExists) {
		var oElem = eval('document.InmediaFrm.' + sElemName + '_' + this.posId + this.xtra_sufix); 
		var sValue = '';
		if ((bFailIfNotExists != undefined) && (bFailIfNotExists == false) && !(oElem)) {
			return '';
		}
		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);
	};

	
	
	this.save=function(form_id){
	
		if (!this.FormValidator.validateForm(document.forms[0], form_id)) return;
			
		var oAjax = new WBE_AjaxClass();
		
		this.savePre();
		
		oAjax.clear();
		this.setParams(oAjax);
		
		//xmlObj = oAjax.throwEventXML('user_address_signon_save');
		
		xmlObj = (this.contentVersionId) 
			? oAjax.throwEventXML(this.useModifyEvent)
			: oAjax.throwEventXML(this.useSaveEvent);
			
		//if (!xmlObj) {	alert('Error al realizar el registro.'); return;	}
		if (xmlObj) 
		{	
			var sStatus = oAjax.getXMLNodeValue(xmlObj, 's');
			if (sStatus!=0) {
				var sErrorMessage = oAjax.getXMLNodeValue(xmlObj, 'e');
				alert(sErrorMessage);
				return;
			}			
		}
		
		if(!this.contentVersionId)
			this.doLogin();
		
		this.savePost();
	};
	
	this.doLogin = function() {
		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.AddToShoppingCart=function(sVerId){
	
		var oAjax = new WBE_AjaxClass();
		oAjax.addPostParameter('l_type_code', 'PRODUCT_LINE');
		oAjax.addPostParameter('l_content_version_id', sVerId);
		this.savePre();
		xmlObj = oAjax.throwEventXML('com_add_to_shopping_cart');
		this.savePost();		
	};*/		
	
	this.changeCountry = function(i) {
		if (!i) i = 1;
		var oElemCtry = eval('document.InmediaFrm.pais_' + i + '_' + this.posId);
		if (oElemCtry) {
			if (oElemCtry.options) {
				var sId = oElemCtry.options[oElemCtry.selectedIndex].value;
				var oElemProv1 = document.getElementById('fs_prov_spain_' + i + '_' + this.posId); 
				var oElemProv2 = document.getElementById('fs_prov_nospain_' + i + '_' + this.posId); 
				var oElemCIF = eval('document.InmediaFrm.nif_' + i + '_' + this.posId); 
				var oElemCP = eval('document.InmediaFrm.codigo_postal_' + i + '_' + this.posId); 
				var oElemTlf = eval('document.InmediaFrm.telefono_' + i + '_' + this.posId); 
				var oElemProvSel = document.getElementById('provincia_' + i + '_' + this.posId); 
				var oElemProvName = document.getElementById('provincia_nombre_' + i + '_' + this.posId); 
				var oElemCtryName = document.getElementById('txt_pais_' + i + '_' + this.posId); 

				if (sId == this.spainId) {
					if (oElemProv1) oElemProv1.style.display = '';
					if (oElemProv2) oElemProv2.style.display = 'none';
					if (oElemCIF) oElemCIF.setAttribute('required', '1');
					if (oElemCIF) oElemCIF.setAttribute('expresionRegular', 'NIF_CIF');
					if (oElemCP) oElemCP.setAttribute('required', '1');
					if (oElemCP) oElemCP.setAttribute('expresionRegular', 'POSTCODE');
					if (oElemTlf) oElemTlf.setAttribute('required', '1');
					if (oElemTlf) oElemTlf.setAttribute('expresionRegular', 'TELEFONO');
					if (oElemProvSel) oElemProvSel.setAttribute('required', '1');
					if (oElemProvName) oElemProvName.setAttribute('required', '');
				} else {
					if (oElemProv1) oElemProv1.style.display = 'none';
					if (oElemProv2) oElemProv2.style.display = '';
					if (oElemCIF) oElemCIF.setAttribute('required', '');
					if (oElemCIF) oElemCIF.setAttribute('expresionRegular', '');
					if (oElemCP) oElemCP.setAttribute('required', '');
					if (oElemCP) oElemCP.setAttribute('expresionRegular', '');
					if (oElemTlf) oElemTlf.setAttribute('required', '');
					if (oElemTlf) oElemTlf.setAttribute('expresionRegular', '');
					if (oElemProvSel) oElemProvSel.setAttribute('required', '');
					if (oElemProvName) oElemProvName.setAttribute('required', '1');
				}

				if (oElemCtryName) oElemCtryName.value = oElemCtry.options[oElemCtry.selectedIndex].text;
			}
		}
	};
}

