var UPLOADPROCESS;

function uploadProcess(vendorid, email, lang, successURL, width, height) {
	this.vendorid   = vendorid;
	this.email      = email;
	this.lang       = lang;
	this.successURL = successURL;

	this.tan = '';
	this.orderid = '';

	this.width  = width;
	this.height = height;
		
	this.uploadElementsHeight = '';
	this.lastTimeoutVar = '';

	this.top  = screen.height / 2 - this.height / 2;
	this.left = screen.width  / 2 - this.width  / 2;

	this.dlgwin = false;
	this.name = 'process';
	this.file = 'process.php';
	this.lfdnr  = getStartLFDNR();
	this.fileid = -1;
	
	this.infoFrameName = 'infoFrame';
	this.processFrameName = 'processFrame';

	this.show = function show() {
		var file = this.file + '?vendorid=' + encodeURIComponent(this.vendorid) + 
				'&email=' + encodeURIComponent(this.email) + 
				'&successURL=' + encodeURI(this.successURL) +
				'&lang=' + encodeURI(this.lang) + 
				'&uniqueid=' + encodeURI(getInfo('uniqueid'));

		if(getInfo('tan') != '' && getInfo('orderid') != '') {
			file += '&tan=' + encodeURIComponent(getInfo('tan')) +
				'&orderid=' + encodeURIComponent(getInfo('orderid'));
		}

		window.myGetElementById("uploadFrame").style.display = "block";

		if(
			navigator.userAgent.toLowerCase().indexOf('safari') < 0 &&
			!(
				/rv\:[0-1]\.[0-3]\.[0-9]/g.test(navigator.userAgent) &&
				navigator.userAgent.toLowerCase().indexOf('mozilla') >= 0
			)
		) {
			window.myGetElementById("uploadElements").style.display = "none";
		} else {
			//If the user uses Safari:
			//hide as most as possible from the div
			window.myGetElementById("uploadElements").style.overflow = "hidden";
			this.uploadElementsHeight = window.myGetElementById("uploadElements").style.height;
			window.myGetElementById("uploadElements").style.height = "1px";
		}

		window.myGetElementById("uploadButton").style.display = "none";
		window.myGetElementById("uploadElementCount").style.display = "none";
		window.myGetElementById("uploadEmail").style.display = "none";
		window.myGetElementById("uploadLanguage").style.display = "none";
		
		this.start(file);
	}

	this.stopProcess = function stopProcess() {
		clearTimeout(this.lastTimeoutVar);
		if(frames["uploadFrame"]) {
			this.dlgwin = frames["uploadFrame"];
			this.dlgwin.location.href = 'blank.html';
		}

		window.myGetElementById("uploadFrame").style.display = "none";

		window.myGetElementById("uploadElements").style.display = "block";
		window.myGetElementById("uploadButton").style.display = "block";
		window.myGetElementById("uploadElementCount").style.display = "block";
		window.myGetElementById("uploadEmail").style.display = "block";
		window.myGetElementById("uploadLanguage").style.display = "block";

		if(navigator.userAgent.toLowerCase().indexOf('safari') >= 0) {
			//If the user uses Safari:
			//show the div again
			window.myGetElementById("uploadElements").style.overflow = "auto";
			window.myGetElementById("uploadElements").style.height = this.uploadElementsHeight;
		}
	}

	this.start = function start(file) {
		if(frames["uploadFrame"]) {
			this.dlgwin = frames["uploadFrame"];

			// this.window.width = width;
			// this.window.height = height;

			this.dlgwin.location.href = file;
		} else {
			UPLOADPROCESS = this;
			this.lastTimeoutVar = setTimeout('UPLOADPROCESS.start("' + file + '")', 100);
		}
	}
	
	this.getInfoFrame = function getInfoFrame() {
		return this.dlgwin.frames['infoFrame'];
	}
	
	this.getProcessFrame = function getProcessFrame() {
		return this.dlgwin.frames['processFrame'];
	}

	this.getCompleteForm = function getCompleteForm() {
		return window.document.forms['completeForm'];
	}


	this.getCancelForm = function getCancelForm() {
		return window.document.forms['cancelForm'];
	}

	this.getNextLfdnr = function getNextLfdnr() {
		this.lfdnr++;
		return this.lfdnr;
	}

	this.getNextUploadFile = function getNextUploadFile() {
		this.fileid++;

		if(this.fileid == elements.length) {
			return -1;
		} else if(elements[this.fileid].isset()) {
			return this.fileid;
		} else {
			return this.getNextUploadFile();
		}
	}

	this.complete = function complete() {
		var XML = '';
		XML += addLine('<?xml version="1.0"?>');
		XML += addLine('<DATA>');
		XML += addLine('	<VENDORID>' + this.vendorid + '</VENDORID>');
		XML += addLine('	<ORDER>');
		XML += addLine('		<TAN>' + this.tan + '</TAN>');
		XML += addLine('		<ORDERID>' + this.orderid + '</ORDERID>');
		XML += addLine('		<COMPLETE>0</COMPLETE>');
		XML += addLine('	</ORDER>');
		XML += addLine('</DATA>');

		this.getCompleteForm().elements['XML'].value = XML;
		if(this.getCompleteForm().elements['goto'].value.indexOf('?') >= 0) {
			this.getCompleteForm().elements['goto'].value += '&tan=' + this.tan + '&orderid=' + this.orderid + '&uniqueid=' + getInfo('uniqueid');
		} else {
			this.getCompleteForm().elements['goto'].value += '?tan=' + this.tan + '&orderid=' + this.orderid + '&uniqueid=' + getInfo('uniqueid');
		}

		if(document.infoForm.freetextvoucher) {
			this.getCompleteForm().elements['goto'].value += '&freetextvoucher=' + getInfo('freetextvoucher');
		}

		this.getCompleteForm().submit();
	}

	this.cancel = function cancel(msg) {
		msgid = 4755;

		var XML = '';
		XML += addLine('<?xml version="1.0"?>');
		XML += addLine('<DATA>');
		XML += addLine('        <VENDORID>' + this.vendorid + '</VENDORID>');
		XML += addLine('        <ORDER>');
		XML += addLine('		<TAN>' + this.tan + '</TAN>');
		XML += addLine('		<ORDERID>' + this.orderid + '</ORDERID>');
		XML += addLine('		<REASONID>' + msgid + '</REASONID>');
		XML += addLine('		<REASON>' + msg + '</REASON>');
		XML += addLine('        </ORDER>');
		XML += addLine('</DATA>');

		this.getCancelForm().elements['XML'].value = XML;
		this.getCancelForm().submit();
	}

	this.adjustProgress = function adjustProgress(item, msg) {
		var all = getUploadFileCount() + 1;

		var progressText = this.getInfoFrame().document.getElementById('progressText');
		var progressBar  = this.getInfoFrame().document.getElementById('progressBar');

		var width = (100 / all) * item;
		width = Math.floor(width);

		progressText.innerHTML = msg + ' ... ';

		if(progressBar && width > 0) {
			progressBar.width = width + "%";
		}
	}
}

