/**
*
*  AJAX IFRAME METHOD (AIM)
*  http://www.webtoolkit.info/
*
**/

AIM = {

	frame : function(c) {
		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe src="" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
		//style="visibility:hidden"
		alert("adding iframe");
		document.body.appendChild(d);

		var i = document.getElementById(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}

		return n;
	},

	form : function(f, name) {
		f.setAttribute('target', name);
	},

	submit : function(f, c) {
		AIM.form(f, AIM.frame(c));
		if (!validateInput()){
			return false;
		}
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},

	loaded : function(id) {
		alert("loaded " + id);
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		alert(d);
		if (d.location.href == "about:blank") {
			return;
		}

		if (typeof(i.onComplete) == 'function') {
		
			i.onComplete(d.body.innerHTML);
		}
	}

}


function startUpload() {
	// make something useful before submit (onStart)
	if (document.getElementById('fileHelp') == undefined){
		return false;
	} else{
		document.getElementById('fileHelp').innerHTML = "";
	}
	return true;
}

function completeUpload(response) {
	// make something useful after (onComplete)
	//document.getElementById('nr').innerHTML = parseInt(document.getElementById('nr').innerHTML) + 1;
	var resp = response.split("+++++");
	if (resp.length > 1){
		Dasmi.loadDataFromFile(resp[0], function(id){
			if (id == undefined){
				document.getElementById('fileHelp').innerHTML = "Unable to parse and load the file. Please make sure it is a valid PSI MI 2.5 file.";
			}else{
				document.getElementById('fileHelp').innerHTML = id;
				//document.getElementById('fileId').value = id;
				//alert("about to add " + dump(id));
				addSource(id);
			}
		});

		//document.getElementById('fileHelp').innerHTML = resp[0];
	}else{
		document.getElementById('fileHelp').innerHTML = response;
	}
}



