/** Handler for the open button on input toolbar. */

var openDlg = false;
var convertDlg = false;

function FileOpener(animateDialogs, loadCancelButtonText, loadImportButtonText, convertCancelButtonText, convertOKButtonText) {
	this.convertRequest = false;
	this.uploadedDataRequest = false;
	this.animateDialogs = animateDialogs;
	this.loadCancelButtonText = loadCancelButtonText;
	this.loadImportButtonText = loadImportButtonText;
	this.convertCancelButtonText = convertCancelButtonText;
	this.convertOKButtonText = convertOKButtonText;
}

FileOpener.prototype.openFile = function() {
	if (!openDlg) {
		openDlg = new YAHOO.ext.BasicDialog("open-dlg", {
			modal:true,
			autoTabs:true,
			resizable:false,
			width:370,
			height:230,
			shadow:true,
			animateTarget: (this.animateDialogs ? 'inputImportButton' : null)
		});
		openDlg.addKeyListener(27, openDlg.hide, openDlg);
		openDlg.addButton(this.loadCancelButtonText, openDlg.hide, openDlg);
		openDlg.addButton(this.loadImportButtonText, this.doImport, this);
	}
	openDlg.show();
}

FileOpener.prototype.doImport = function() {
	var fileField = document.getElementById('uploadFile');
	doFileUpload(fileField, this.convertFile, this); // dcforms/file_upload.js
	if (openDlg) openDlg.hide();
}

FileOpener.prototype.convertFile = function(toForm, toVersion, fromVersion) {
	if (!versionHandler.isFormAVersion(toForm, toVersion)) return false;
	if (!convertDlg) {
		convertDlg = new YAHOO.ext.BasicDialog("convert-dlg", {
			modal:true,
			autoTabs:true,
			resizable:false,
			width:350,
			height:220,
			shadow:true,
			animateTarget: (this.animateDialogs ? 'inputImportButton' : null)
		});
		convertDlg.addKeyListener(27, convertDlg.hide, convertDlg);
		convertDlg.addButton(this.convertCancelButtonText, convertDlg.hide, convertDlg);
		convertDlg.addButton(this.convertOKButtonText, this.doConvertOrOpenOther, this);
	}
	convertDlg.toForm = toForm;
	convertDlg.toVersion = toVersion;
	if (versionHandler.isSameVersion(toForm, toVersion, fromVersion)) {
		this.doConvertFile();
	} else {
		convertDlg.show();
	}
	return true;
}

FileOpener.prototype.doConvertOrOpenOther = function() {
	if (document.getElementById('convert').checked) {
		this.doConvertFile();
	} else if (document.getElementById('openother').checked) {
		this.doOpenOther();
	}
}

FileOpener.prototype.doConvertFile = function() {
	this.convertRequest = createXMLHttpRequest();
	this.convertRequest.open('GET', 'doconversion?bean=' + bean, false);
	this.convertRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	this.convertRequest.send(null);
	this.doneConvert();
}

FileOpener.prototype.doneConvert = function() {
	if (this.convertRequest.readyState == 4) {
		if (this.convertRequest.status == 200) {
			this.closeConvertDialog();
			if (checkForTimeout(this.convertRequest.responseXML)) return;
			showForm(currentForm, -2);
		}
	}	
}

FileOpener.prototype.doOpenOther = function() {
	this.uploadedDataRequest = createXMLHttpRequest();
	this.uploadedDataRequest.open('GET', 'getuploadeddata', false);
	this.uploadedDataRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	this.uploadedDataRequest.send(null); // Blocking
	this.doOpenOtherWithData();
}

FileOpener.prototype.doOpenOtherWithData = function() {
	if (this.uploadedDataRequest.readyState == 4) {
		if (this.uploadedDataRequest.status == 200) {
			var xmldoc = this.uploadedDataRequest.responseXML;
			var uploadeddata = xmldoc.getElementsByTagName('uploadeddata');
			if (uploadeddata) {
				var data = uploadeddata.item(0).getAttribute('text');
				if (data && data != '') {
					versionHandler.openVersion(convertDlg.toForm, convertDlg.toVersion, data);
				} else {
					showMessage('error.openother.nodata');
				}
			} else {
				showMessage('error.openother.noresponse');
			}
			this.closeConvertDialog();
		}
	}
}

FileOpener.prototype.closeConvertDialog = function() {
	convertDlg.toForm = null;
	convertDlg.toVersion = null;
	if (convertDlg.hide) {
		convertDlg.hide();
	}
}

