AIM = {
	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;
	},
	form : function(f, name) {
		f.setAttribute('target', name);
		f.submit();
	},
	submit : function(f, c) {
		AIM.form(f, AIM.frame(c));
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},
	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);
		}
	}
}

function startCallback()
{
	$('loader').style.display = "block";
	$('submit-info').disabled = true;
	$('oForm').setOpacity(0.5);
	return true;
}

function completeCallback(response)
{
	if (response == "success")
	{
		alert('Your information succesfully uploaded. Thank You.');
	}
	else
	{
		alert(response);
	}

	$('loader').style.display = "none";
	$('submit-info').disabled = false;
	$('oForm').setOpacity(1);
}

window.addEvent('domready', function()
{
	$('submit-info').addEvent('click', function(e)
	{
		new Event(e).stop();		
		var fname=$('fname').getValue();
		var lname=$('lname').getValue();
		var email=$('email').getValue();		
		var file=$('file').value;
		if (fname.length == 0 || lname.length == 0 || email.length == 0 || file.length == 0)
		{
			alert('Please fill "First Name", "Last Name", "Email" fields and don\'t forget upload your picture?')
		}
		else
		{		
			AIM.submit($('oForm'), {'onStart' : startCallback, 'onComplete' : completeCallback})
		}
		return false;
	});
	
}); 