/**
*
*  AJAX IFRAME METHOD (AIM)
*  http://www.webtoolkit.info/
*
*  Slightly modified to support needs
*
**/


	function startCallback(suffix, pagenr) {
		//do something useful before submit (onStart)
		//alert("Debug: startCallback()");
		var descname="description_"+suffix;
		var desc=document.getElementById(descname);
		var filename="file_"+suffix;
		var file=document.getElementById(filename);

		//todo: Does this work as expected, saving the radio button options only?
		validateAndGotopage(pagenr);

		if ((!file.value)||(!desc.value)) {
			var msg="Fil og/eller beskrivelse mangler";
			if (!desc.value) msg="Du må angi en kort beskrivelse av fila";
			if (!file.value) msg="Du må velge en fil som skal lastes opp";
			var filemsgname="filemsg_"+suffix;
			var filemsg=document.getElementById(filemsgname);
			if (filemsg) {
				filemsg.className="inlineerror";
				filemsg.parentNode.className="block invalid";
				filemsg.innerHTML=msg;
// 				filemsg.setAttribute("class", "inlineerror");

			}
			else alert(msg);
			return false;
		}

		return true;
	}

	function completeCallback(response) {
		// make something useful after (onComplete)
		if (document.getElementById('pageid')){
			gotopage(document.getElementById('pageid').value);  // hack ... find better method
		}
		else document.location.reload();
	}


AIM = {

	// creates a hidden iframe
	// onload calls AIM.loaded which again calls the onComplete callback function
	frame : function(c) {

			var n = 'f' + Math.floor(Math.random() * 99999);
			var d = document.createElement('DIV');
			d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
			document.body.appendChild(d);

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

	// The form attributes, like target (eZ module and view)
	form : function(f, suffix, name) {
			f.setAttribute('target', name);
			f.setAttribute('action', '/editr/upload/');
			f.setAttribute('encType','multipart/form-data');
			f.setAttribute('method','post');
	},

	// calls "form" to set the form attributes
	//
	submit : function(fid, suffix, c) {
			var f=document.getElementById(fid);
			if (f) {
				var idc=document.getElementById('idcode');
				idc.value=suffix;

				AIM.form(f, suffix, AIM.frame(c));
				if (c && typeof(c.onStart) == 'function') {
						if (c.onStart()){
							var filename='file_'+suffix;
							var fivi=document.getElementById(filename);
//                 alert('file value: '+fivi.value+' : '+f.target);
							f.submit();
						}
				} else {
						f.submit();
				}
			}
	},

	loaded : function(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;
			}
			if (d.location.href == "about:blank") {
					return;
			}

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

}

