﻿function JSObj_free_form()
{
	this.posId = 1;
	this.compId = 1;
	this.lngId = 'es';
	this.contentTypeId = 0;
	this.varName = '';
	this.url = '';
	this.url_error = '';
	this.uploadingFilesIds = new Array();
	this.uploadingFileDone = new Array();

	this.setParams = function (oAjax) {
		var oIdsElem, sId, tempElem;
		
		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 {
			sId = oIdsElem.value;
			oAjax.addPostParameter('att_input_' + sId, this.getValue(sId));
		}
		oAjax.addPostParameter('lng_id', this.lngId);
		oAjax.addPostParameter('pos_id', this.posId);
		oAjax.addPostParameter('comp_id', this.compId);
	};
		
	this.getValue = function (sElemId) {
		var oElem = eval('document.InmediaFrm.' + sElemId);
		var sValue = '';
		if(oElem == null) 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.save = function () {
		window.scroll(0, 0);

		this.uploadFiles();

		setTimeout(this.varName + '.save2();', 0);		
	};
	
	this.save2 = function() {
	
		if (!this.isAllUploaded()) {
			setTimeout(this.varName + '.save2();', 500);	
		} else {
	
			var oAjax = new WBE_AjaxClass();
			oAjax.clear();
			this.setParams(oAjax);

			var oValidator = new WBEFormValidator();
			if(!oValidator.validateForm(document.InmediaFrm))return false;

			oAjax.throwEvent2("cms_send_free_form")//;, true, "XML_DOCUMENT.wbe");//NO_OUTPUT.wbe"); // Si hay OUTPUT es html_output !!!!!!!

			if (oAjax.responseText == ''){
				document.location.href = this.url;
			}
			else {
				document.getElementById('form_error_' + this.posId).style.display = 'block';
				document.location.href = this.url_error;
			}
		}
	};
	
	this.isAllUploaded = function () {
		var bOK = true;
		//alert('isAllUploaded: ' + this.uploadingFileDone.length);
		for (var i = 0; i < this.uploadingFileDone.length; i++) {
			//alert(i + ' - ' + bOK + ' - ' + this.uploadingFileDone[i]);
			bOK = bOK && this.uploadingFileDone[i]; 
		}
		
		return bOK;
	}
	
	this.uploadFiles = function () {
		var elem = eval('document.InmediaFrm.file_atts_' + this.posId);
		if (elem) {
			if (elem.value) {
				this.uploadFile(elem.value);
			} else {
				for (var i = 0; i < elem.length; i++) {
					this.uploadFile(elem[i].value);
				}
			}
		}
	};
	
	this.uploadFile = function (att_id) {
		var iframe_doc = this.getIFrameDocument('iframe_' + att_id + '_' + this.posId);
		this.uploadingFilesIds[this.uploadingFilesIds.length] = att_id;
		this.uploadingFileDone[this.uploadingFileDone.length] = false;
		//var elem = this.getFileElem(iframe_doc.InmediaFrm);
		//alert(elem.value);
		iframe_doc.InmediaFrm.submit();
	};

	this.getFileElem = function (oForm) {
		for (var i = 0; i < oForm.elements.length; i++) {
			if (oForm.elements[i].type == 'file') {
				return oForm.elements[i].type;
			}
		}
	};

	this.getIFrameDocument = function (aID) {
	  // if contentDocument exists, W3C compliant (Mozilla)
	  if (document.getElementById(aID).contentDocument){
			return document.getElementById(aID).contentDocument;
	  } else {
			// IE
			return document.frames[aID].document;
	  }
	};
	
	this.fileUploaded = function (att_id, file_name) {
		eval('document.InmediaFrm.att_input_' + att_id + '_' + this.posId).value = file_name;
		this.uploadingFileDone[this.getArrayAttIndex(att_id)] = true;
	};
	
	this.getArrayAttIndex = function (att_id) {
		for (var i = 0; i < this.uploadingFilesIds.length; i++) {
			if (this.uploadingFilesIds[i] == att_id) {
				return i;
			}
		}
	};
};