function createXMLHttpRequest(){var ua;if(window.XMLHttpRequest){try{ua=new XMLHttpRequest();}catch(e){ua=false;}}else if(window.ActiveXObject){try{ua=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){ua=false;}}
return ua;}
var req=createXMLHttpRequest();function startRequest(script,query)
{req.open('get','/script/ajax/ajax-switcher.php?script='+script+'&'+query);req.setRequestHeader('Content-Type',"text/xml");req.onreadystatechange=handleResponse;req.send(null);}
function handleResponse()
{if(req.readyState==4&&req.status==200&&req.responseXML)
{var ops=req.responseXML.getElementsByTagName('op');for(var i=0;i<ops.length;i++)
{var opi=ops.item(i);var type=opi.getElementsByTagName('type')[0].firstChild.nodeValue;if(type=="function")
{var functionToDo=opi.getElementsByTagName('name')[0].firstChild.nodeValue;var parameters=opi.getElementsByTagName('parameter');window[functionToDo](parameters);}
else if(type=="update")
{var elementToUpdate=opi.getElementsByTagName('element')[0].firstChild.nodeValue;var contents=opi.getElementsByTagName('content');document.getElementById(elementToUpdate).innerHTML="";for(var j=0;j<contents.length;j++)
{document.getElementById(elementToUpdate).innerHTML+=contents[j].firstChild.nodeValue;}}
else if(type=="style")
{var elementToUpdate=opi.getElementsByTagName('element')[0].firstChild.nodeValue;var styleToUpdate=opi.getElementsByTagName('style')[0].firstChild.nodeValue;var styleContents=opi.getElementsByTagName('styleContents')[0].firstChild.nodeValue;document.getElementById(elementToUpdate).style[styleToUpdate]=styleContents;}else if(type=="append")
{var elementToUpdate=opi.getElementsByTagName('element')[0].firstChild.nodeValue;var contents=opi.getElementsByTagName('content');for(var j=0;j<contents.length;j++)
{document.getElementById(elementToUpdate).innerHTML+=contents[j].firstChild.nodeValue;}}}}}

function disableLoadStatus()
{
	$('loadStatus').style.display = "none";
}


function tryLogin()
{
	$('loginStatus').innerHTML = "";
	$('loadStatus').style.display = "block";

	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	var currentUrl = document.location.href;

	startRequest("login", "username="+username+"&password="+password+"&currentUrl="+currentUrl);
}

function loginRedirect(url)
{
//	window.location = url[0].firstChild.nodeValue+"?login=1";
//	Modalbox.hide();
	window.location = "/myimages";
}

function recoverPassword()
{
	$('loginForm').innerHTML = "<label for='email'>E-mail</label><input type='text' name='email' id='email' /><br style='clear:both' /><br /><input type='button' value='Reset password' id='resetPasswordButton' onclick='tryRecoverPassword()' /><input type='button' value='Cancel' onclick='Modalbox.hide()' /><div id='loadStatus'><img src='/images/siteimages/ajax-loader.gif' />Please wait...</div><br style='clear:both' /><div id='resetPasswordStatus'></div>";
}

function tryRecoverPassword()
{
	$('resetPasswordStatus').innerHTML = "";


	email = $('email').value;

	if (email == "")
	{
		$('resetPasswordStatus').innerHTML = "Please enter a valid e-mail address";
		return;
	}

	$('resetPasswordButton').disabled = true;
	$('loadStatus').style.display = "block";

	startRequest("myimages", "op=resetPassword&email="+email);
}

function toggleResetPasswordButton()
{
	$('resetPasswordButton').disabled = false;
	disableLoadStatus();
}


function tryRegister()
{


	var email1 = $('email1').value;
	var email2 = $('email2').value;
	var pass1 = $('pass1').value;
	var pass2 = $('pass2').value;
	var age = $('age').value;
	var gender = $('gender').value;

	$('registerStatus').innerHTML = "";


	if (email1 == "")  $('registerStatus').innerHTML += "You must enter your email address!<br />";
	if (pass1 == "")  $('registerStatus').innerHTML += "You must enter a password address!<br />";
	if (email1 != email2) $('registerStatus').innerHTML += "The e-mail addresses you entered did not match, please check them and try again...<br />";
	if (pass1 != pass2) $('registerStatus').innerHTML += "The passwords you entered did not match, please check them and try again...<br />";
	if (pass1.length < 6)  $('registerStatus').innerHTML += "Your password must be at least 6 characters long<br />";

	email1.replace("+", "%2B");

	if ($('registerStatus').innerHTML=="")
	{
		$('loadStatus').style.display = "block";

		query = "email="+email1+"&pass="+pass1+"&age="+age+"&gender="+gender;

		//$('registerStatus').innerHTML = query;

		$('registerButton').disabled = true;
		startRequest("register", query);
	}
}

function toggleRegisterButton()
{
		$('registerButton').disabled = false;
		disableLoadStatus();
}

function registered()
{
	$('registerForm').innerHTML = "You are now registered! Click <a href='/myimages'>here</a> to go to your user page...";
}

function tryContact()
{
	$('contactButton').disabled = true;
	message = $('message').value;
	email = $('email').value;


	startRequest("contact", "email="+email+"&message="+message);

}


function countDown(secs, divId, redirect)
{
	var div = document.getElementById(divId);
	var timer = setInterval(function()
	{
		if( secs )
		{
			var tempSecs = secs;
			var timeleft = "";

			if (tempSecs/3599 > 1)
			{
				timeleft = (Math.floor(tempSecs/3600))+" hours, ";
				tempSecs = tempSecs-(Math.floor(tempSecs/3600)*3600);
			}

			if (tempSecs/59 > 1)
			{
				timeleft += (Math.floor(tempSecs/60))+" minutes, ";
				tempSecs = tempSecs-(Math.floor(tempSecs/60)*60);
			}

			--secs;
			timeleft += tempSecs+" seconds";

			div.innerHTML = timeleft;
		}
		else
		{
			clearInterval(timer);
			if (redirect==true)
			{
				document.location.reload(true);
			}
			else
			{
				var url = window.location.href;

				if (gup('key') != "")
				{
					url += "&d=1";
				}
				else
				{
					url += "?d=1";
				}

				document.getElementById("countdown").innerHTML = "<a href='"+url+"'>Click here to see your image!</a>";
			}
		}
	}, 1000);
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


function imageRedirect(idIn)
{
	imageId = idIn[0].firstChild.nodeValue;
	address = idIn[1].firstChild.nodeValue;

	if (address == "c") address = "celebrity";
	else if (address =='i') address = "image";
	else if (address == 'p') address = "pop-art";
	else if (address == 'b') address = 'baby';

	window.location = "/"+address+"/"+imageId;
}

function showUpload()
{
	$('uu_upload').style.display = "block";
}


function getMousePosition(e, element)
{
	var docPos = mousePositionRelativeToDocument(e);
	var prepImagePos = elementPositionRelativeToDocument(document.getElementById(element));

	var mouseX = docPos[0]-prepImagePos[0];
	var mouseY = docPos[1]-prepImagePos[1];

	return [mouseX, mouseY];
}


function mousePositionRelativeToDocument(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information

	return [posx, posy];
}


function elementPositionRelativeToDocument(obj) {
	var curleft = curtop = 0;

	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	return [curleft,curtop];
}
}


function queueFinished()
{
	window.location = redirUrl;
}

function queueAgain()
{
	document.getElementById("timeLeft").innerHTML = 3+" seconds";
	newCountDown(3, 'timeLeft', redirUrl);
}


function newCountDown(secs, divId, redirectUrl)
{
	var div = document.getElementById(divId);
	var timer = setInterval(function()
	{
		if( secs )
		{
			var tempSecs = secs;
			var timeleft = "";

			if (tempSecs/3599 > 1)
			{
				timeleft = (Math.floor(tempSecs/3600))+" hours, ";
				tempSecs = tempSecs-(Math.floor(tempSecs/3600)*3600);
			}

			if (tempSecs/59 > 1)
			{
				timeleft += (Math.floor(tempSecs/60))+" minutes, ";
				tempSecs = tempSecs-(Math.floor(tempSecs/60)*60);
			}

			--secs;
			timeleft += tempSecs+" seconds";

			div.innerHTML = timeleft;
		}
		else
		{
			clearInterval(timer);
			startRequest("queue", "imageId="+imageId+"&imageType="+imageType);
		}
	}, 1000);
}



