﻿function JSObj_shopping_cart()
{
	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.goPageNewIfOk = '';
	this.goPageNewIfFail = '';	
	this.goPageModifyIfOk = '';
	this.goPageModifyIfFail = '';

	this.do_login = false;

	this.FormValidator = new WBEFormValidator();
	this.customLoginErrorMsg = null;

	this.spainId = '50';
	
	// Acciones antes de llamar al evento
	this.savePre = function() {}
	
	// Acciones a realizar después de guardar
	this.savePost = function(xmlObj) { 
		if (xmlObj) {
			var sUrl = oAjax.getXMLNodeValue(xmlObj, 'url');
			if (sUrl && (sUrl != ''))
				document.location.href = sUrl;
			else
				window.location.reload();
		} else {
			alert('Se ha producido un error guardando el pedido');
		}
	}
	
	// Acciones a realizar después de guardar
	this.savePostAddress = function(xmlObj) { 
	}
	
	this.SaveShoppingCart = function(){
		var oAjax = new WBE_AjaxClass();
		this.savePre();
		xmlObj = oAjax.throwEventXML('com_save_shopping_cart');
		this.savePost(xmlObj);
    };
	
	this.SaveShoppingCartPaypal = function(){
		var oAjax = new WBE_AjaxClass();
		this.savePre();
		xmlObj = oAjax.throwEventXML('com_save_shopping_cart_paypal');
		this.savePost(xmlObj);
    };

    this.SaveRoleShoppingCart = function() {
        var oAjax = new WBE_AjaxClass();
        this.savePre();
        xmlObj = oAjax.throwEventXML('com_buy_role_save_order');
        this.savePost(xmlObj);
    };

	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(xmlObj);		
	};		

	this.RemoveFromShoppingCart = function(sId){
	
		var oAjax = new WBE_AjaxClass();
		oAjax.addPostParameter('line', sId);
		this.savePre();
		xmlObj = oAjax.throwEventXML('com_remove_from_shopping_cart');
		this.savePost(xmlObj);		
	};
	
	this.UpdateQuantityOnShoppingCartLine = function(sId, sNewQuant){
		
		var oAjax = new WBE_AjaxClass();
		oAjax.addPostParameter('line', sId);
		oAjax.addPostParameter('new_quantity', sNewQuant);
		this.savePre();
		xmlObj = oAjax.throwEventXML('com_update_quantity_on_shopping_cart_line');
		this.savePost(xmlObj);
	}
	
	this.getValue2 = function (sElemName, bFailIfNotExists) {
		var oElem = eval('document.WBForm.' + sElemName + '_' + this.posId);
		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.setParams = function (oAjax, index) {
		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('email_' + index, this.getValue2('user_login', false));
		
		oAjax.addPostParameter('nombre_' + index, this.getValue2('nombre_' + index, false));
		oAjax.addPostParameter('apellidos_' + index, this.getValue2('apellidos_' + index, false));
		oAjax.addPostParameter('nif_' + index, this.getValue2('nif_' + index, false));
		oAjax.addPostParameter('tipo_via_' + index, this.getValue2('tipo_via_' + index, false));
		oAjax.addPostParameter('via_' + index, this.getValue2('via_' + index, false));
		oAjax.addPostParameter('via_number_' + index, this.getValue2('via_number_' + index, false));
		oAjax.addPostParameter('direccion_' + index, this.getValue2('direccion_' + index, false));
		oAjax.addPostParameter('localidad_' + index, this.getValue2('localidad_' + index, false));
		oAjax.addPostParameter('codigo_postal_' + index, this.getValue2('codigo_postal_' + index, false));
		oAjax.addPostParameter('provincia_' + index, this.getValue2('provincia_' + index, false));
		oAjax.addPostParameter('provincia_nombre_' + index, this.getValue2('provincia_nombre_' + index, false));
		oAjax.addPostParameter('telefono_' + index, this.getValue2('telefono_' + index, false));
		oAjax.addPostParameter('fax_' + index, this.getValue2('fax_' + index, false));
		oAjax.addPostParameter('fecha_nacimiento_' + index, this.getValue2('fecha_nacimiento_' + index, false));
		oAjax.addPostParameter('observaciones_' + index, this.getValue2('observaciones_' + index, false));

		oAjax.addPostParameter('tipo_direccion_' + index, this.getValue2('tipo_direccion_' + index, false));
		oAjax.addPostParameter('pais_' + index, this.getValue2('pais_' + index, false));
		oAjax.addPostParameter('referencia_' + index, this.getValue2('referencia_' + index, false));
		oAjax.addPostParameter('user_fk', this.getValue2('user_fk_' + index, 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('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.getValue = function (sElemId) {
		return this.getValue2('att_input_' + sElemId, true);
	};
	
	this.save=function(index){
	
		if (!this.FormValidator.validateForm(document.forms[0], this.posId)) return false;
			
		var oAjax = new WBE_AjaxClass();
		
		this.savePre();
		
		oAjax.clear();
		this.setParams(oAjax, index);
		
		
		xmlObj = oAjax.throwEventXML('com_save_shopping_address');
		
		this.savePostAddress(xmlObj);
		
		return (xmlObj != null);
	};
	
	
	this.cbo_change_province = function (index) {
		var oInput = document.getElementById('txt_prov_' + index);
		var oSelect = document.getElementById('provincia_' + index + '_' + this.posId);
		
		if (oInput && oSelect) {
			var txt = oSelect.options[oSelect.selectedIndex].text;
			oInput.value = txt;
		}
	};
	
	
	this.changeCountry = function(i) {
		if (!i) i = 1;
		var oElemCtry = eval('document.WBForm.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 oElemCP = eval('document.WBForm.codigo_postal_' + i + '_' + this.posId); 
				var oElemCIF = eval('document.WBForm.nif_' + 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 (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 (oElemProvSel) oElemProvSel.setAttribute('required', '');
					if (oElemProvName) oElemProvName.setAttribute('required', '1');
				}

				if (oElemCtryName) oElemCtryName.value = oElemCtry.options[oElemCtry.selectedIndex].text;
			}
		}
	};

	// Acciones antes de llamar al evento
	this.addCouponPre = function() { }

	// Acciones a realizar después de guardar
	this.addCouponPost = function(xmlObj, oElem) {
		if (xmlObj) {
			var oAjax = new WBE_AjaxClass();
			var sResult = oAjax.getXMLNodeValue(xmlObj, 'result');
			if (sResult == 'OK')
				window.location.reload();
			else {
				var sMsg = 'Se ha producido un error añadiendo el cupón';
				if (sResult == 'NOT_EXISTS') sMsg = 'El código de cupón introducido no existe';
				else if (sResult == 'EMPTY_STR') sMsg = 'Debes introducir un ´código de cupón';
				else if (sResult == 'ALREADY_USED') sMsg = 'Ya has utilizado este cupón';
				else if (sResult == 'NOT_ACTIVE') sMsg = 'Este cupón no está activo';
				else if (sResult == 'OUT_OF_START_DATE') sMsg = 'Este cupón todavía no es válido';
				else if (sResult == 'OUT_OF_END_DATE') sMsg = 'Este cupón ya no es válido';
				alert(sMsg);
				oElem.value = '';
			}
		} else {
			alert('Se ha producido un error añadiendo el cupón');
			oElem.value = '';
		}
	}

	this.addCoupon = function(oElem) {
		var oAjax = new WBE_AjaxClass();
		this.addCouponPre();
		oAjax.addPostParameter('code', oElem.value);
		xmlObj = oAjax.throwEventXML('com_add_promotion_coupon');
		this.addCouponPost(xmlObj, oElem);
	};

	this.addComment = function(oElem) {
		var oAjax = new WBE_AjaxClass();
		this.addCouponPre();
		oAjax.addPostParameter('comment', oElem.value);
		oAjax.throwEventXML('com_add_comment');
	};
}
