var imgs=2;
var xmlHttp;
function login()
{
	if ((document.getElementById('gaiatoolsusername').value.length>0) && (document.getElementById('gaiatoolspassword').value.length>0))
	{
		document.getElementById('use').value=document.getElementById('gaiatoolsusername').value;
		document.getElementById('pas').value=document.getElementById('gaiatoolspassword').value;
		xmlHttp=GetXmlHttpObject(stateChanged);
		xmlHttp.open('GET','login.php?usr='+document.getElementById('gaiatoolsusername').value+'&pwd='+document.getElementById('gaiatoolspassword').value,true);
		xmlHttp.send(null);
	}
	else
	{
		alert('Please fill in the fields !');
	}
}
function stateChanged()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		if(xmlHttp.responseText.substr(0,2)=='ls')
		{
			document.getElementById('mes').innerHTML='Login Successful ! Welcome, '+document.getElementById('use').value+' !';
			document.getElementById('stuf').style.display='block';
			document.getElementById('tables').innerHTML='';
			imgs=xmlHttp.responseText.substring(2,xmlHttp.responseText.indexOf(','));
			document.getElementById('randomsigimgsdiv').innerHTML=xmlHttp.responseText.substr(xmlHttp.responseText.indexOf(',')+1);
		}
		else
		{
			alert('Impossible to log in.\nMake sure your password and your username are correct.');
		}
	}
}
function GetXmlHttpObject(handler)
{
// Thanks nate for this snippet.
	var http=false;
	try
	{
		http=(window.XMLHttpRequest) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			var MSxmlHttpRequest=new Array('Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP');
			var success=false;
			for (var i=0;i<MSxmlHttpRequest.length && !success;i++)
			{
				try
				{
					http=new ActiveXObject(MSxmlHttpRequest[i]);
					success=true;
				}
				catch(e)
				{
					/* empty */
				}
			}
		}
		catch(e)
		{
			http=false;
		} 
	}
	http.onload=handler;
	http.onreadystatechange=handler;
	http.onerror=handler;
	return http;
}
function addimg()
{
	imgs++;
	var tmpar=new Array();
	for(var i=1;i<imgs;i++)
	{
		tmpar[i]=document.getElementById('txt'+i.toString()).value;
	}
	document.getElementById('randomsigimgsdiv').innerHTML+='<div id="rando'+imgs+'"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="20%" height="48"><div align="center" class="pageName">'+imgs+'</div></td><td height="48"><input name="txt'+imgs+'" type="text" id="txt'+imgs+'" size="60" maxlength="255" onkeyup="refimg('+imgs+');" onchange="refimg('+imgs+');" onblur="refimg('+imgs+');" onfocus="refimg(1);" /></td><td height="48" width="20%"><img id="img'+imgs+'" width="48" height="48" /></td></tr></table></div>';
	for(i=1;i<imgs;i++)
	{
		document.getElementById('txt'+i).value=tmpar[i];
		refimg(i);
	}
}
function removeimg()
{
	if(imgs>2)
	{
		imgs--;
		var tmpar=new Array();
		for(var i=1;i<=imgs;i++)
		{
			tmpar[i]=document.getElementById('txt'+i).value;
		}
		document.getElementById('randomsigimgsdiv').innerHTML='';
		for(i=1;i<=imgs;i++)
		{
			document.getElementById('randomsigimgsdiv').innerHTML+='<div id="rando'+i+'"><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="20%" height="48"><div align="center" class="pageName">'+i+'</div></td><td height="48"><input name="txt'+i+'" type="text" id="txt'+i+'" size="60" maxlength="255" onkeyup="refimg('+i+');" onchange="refimg('+i+');" onblur="refimg('+i+');" onfocus="refimg(1);" /></td><td height="48" width="20%"><img id="img'+i+'" width="48" height="48" /></td></tr></table></div>';
		}
		for(i=1;i<=imgs;i++)
		{
			document.getElementById('txt'+i).value=tmpar[i];
			refimg(i);
		}
	}
	else
	{
		alert('What\'s the point of creating a random signature with less than 2 images ? XD');
	}
}
function refimg(im)
{
	document.getElementById('img'+im).src=document.getElementById('txt'+im).value;
}
function batchadd()
{
	var b=prompt('How many images do you want to add ?',2);
	if(b!=null)
	{
		for(var a=0;a<b;a++)
		{
			addimg();
		}
	}
}
function batchremove()
{
	var b=prompt('How many images do you want to remove ?',2);
	if(b!=null)
	{
		var c=0;
		for(var a=0;a<b;a++)
		{
			if(imgs>2)
			{
				removeimg();
			}
			else
			{
				if(c==0)
				{
					c=1;
					alert('Impossible to remove any more images.');
				}
			}
		}
	}
}
function don()
{
	var e=0;
	for(var d=1;d<=imgs;d++)
	{
		if(document.getElementById('txt'+d).value=='')
		{
			e++;
		}
	}
	if(e>0)
	{
		alert('Error : '+e+' fields have not been filled.\nTo remove unwanted ones, click the minus button :)');
	}
	else
	{
		document.getElementById('pics').value=imgs;
		document.frm.submit();
	}
}