var Parameter = function(key, value, check) {
	this.key = key;
	this.value = value;
	if (check == undefined)
		this.check = "true";
	else
		this.check = check;
	
}
var DataCollector = function() {
};
DataCollector.prototype.validate = function(value) {
	if (value == null || value == "") {
		information($.i18n.prop('dataCollect-input'));
		return false;
	}
	return true;
}

DataCollector.prototype.checkBoxData = function(editor) {
	var name = editor.attr("id");
	var choice = editor.find("input[type='checkbox']");
	var value = choice.is(':checked');

	var required = editor.children("input").attr("required");
	var check = "true";
	if (required == "required" && value == "")
		check = "false"; 
	return new Parameter(name, value, check);
}

DataCollector.prototype.fieldData = function(editor) {
	var name = $(editor).attr("id");
	editor = $(editor.children('.field'));
	var value = editor.children("input").val();
	// if (!this.validate(value)) {
	// return false;
	// }

	var required = editor.children("input").attr("required");
	var check = "true";
	if (required == "required" && value == "")
		check = "false"; 
	return new Parameter(name, value, check);
};
DataCollector.prototype.dateData = function(editor) {
	var name = editor.attr("id");
	var value = editor.children("input").val();
	// if (!this.validate(value)) {
	// return false;
	// }
	var required = editor.children("input").attr("required");
	var check = "true";
	if (required == "required" && value == "")
		check = "false"; 
	return new Parameter(name, value, check);
};
DataCollector.prototype.multiFieldData = function(editor) {
	var editorArray = editor.children(".field");
	if (editorArray.size == 0) {
		information($.i18n.prop('dataCollect-input'));
		return;

	}
	var name = editor.attr("id")
	var value = "";
    var check = "true"
	for ( var i = 0; i < editorArray.length; i++) {
		var fieldValue = $(editorArray[i]).children("input").val();
		var tmpRequired = $(editorArray[i]).children("input").attr("required");
		if (tmpRequired =="required" && fieldValue == ""){
			check = "false";
		}
		if (fieldValue == "") {
			continue;
		}

		value += fieldValue + ";";
	}
	value = value.substring(0, value.length - 1);
	// if (!this.validate(value)) {
	// return false;
	// }
	return new Parameter(name, value, check);

};

DataCollector.prototype.tableData = function(editor) {
	var thArray = editor.find("thead").children("tr").children("th");
	var editorArray = editor.find("tbody").children("tr");
	var name = editor.attr("id");
	var value = "";
	var check = "true";
	for ( var i = 0; i < editorArray.length; i++) {
		var tdArray = $(editorArray[i]).children("td");
		var trValue = "";
		for ( var j = 0; j < tdArray.length; j++) {
			var tmpRequired = $(tdArray[j]).children("input").attr("required");
			var tmpValue = $(tdArray[j]).children("input").val();
			if (tmpRequired == "required" && tmpValue == "")
				check = "false";
			if ($(tdArray[j]).children("input").val() == undefined
					|| $(tdArray[j]).children("input").val() == "") {
				continue;
			}
			trValue += thArray[j].innerText + "=" + $(tdArray[j]).children("input").val()
					+ "|";

		}
		trValue = trValue.substring(0, trValue.length - 1);
		value += trValue + "|;";
	}
	value = value.substring(0, value.length - 2);
	// if (!this.validate(value)) {
	// return false;
	// }
	return new Parameter(name, value, check);

};
DataCollector.prototype.selectData = function(editor) {
	var name = editor.attr("id");
	var value = editor.children("select").val();
	// if (!this.validate(value)) {
	// return false;
	// }
	return new Parameter(name, value);
};

DataCollector.prototype.getUnitParams = function(paramEditors) {
	var params = new Array();
	for ( var i = 0; i < paramEditors.length; i++) {
		var editor = $(paramEditors[i]);
		var editorType = editor.attr("editorType");
		var data;
		if (editorType == "field") {
			data = this.fieldData(editor);
			if (!data) {
				return;
			}

			params.push(data);
		} else if (editorType == "date") {
			data = this.dateData(editor);
			if (!data) {
				return;
			}

			params.push(data);
		} else if (editorType == "multiField") {
			data = this.multiFieldData(editor)
			if (!data) {
				return;
			}

			params.push(data);
		} else if (editorType == "table") {
			data = this.tableData(editor);
			if (!data) {
				return;
			}
			params.push(data);
		} else if (editorType == "select") {
			data = this.selectData(editor);
			if (!data) {
				return;
			}
			params.push(data);
		} else if (editorType == "checkBox") {
			data = this.checkBoxData(editor);
			if (!data) {
				return;
			}
			params.push(data);
		} else {
			information($.i18n.prop('dataCollect-errorType'));
		}

	}
	return params;
};

