var xmlHttpCipher,xmlHttpKey,xmlHttpLog,xmlHttpReg,xmlHttpSave,xmlHttpOut,xmlHttpLoad,xmlHttpCheck,xmlHttpCaptcha,xmlHttpFbCaptcha,xmlHttpForgot,xmlHttpChange,xmlHttpSubmitFeedback;

function loadAndDraw()
{
	if(document.getElementById('saveValidator'))
		draw();
	loadKeys();
	checkLoggedIn();
}

function checkLoggedIn()
{
  xmlHttpCheck=GetXmlHttpObject();
  if (xmlHttpCheck==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  } 
  var url="check.php";
  xmlHttpCheck.onreadystatechange=checkStateChanged;
  xmlHttpCheck.open("GET",url,true);
  xmlHttpCheck.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttpCheck.send(null);
}

function draw(key)
{
  var keyName=(key==null)?"":key;
  if(keyName=="")
  {
	  document.getElementById('saveValidator').innerHTML="";
  }
  xmlHttpCipher=GetXmlHttpObject();
  if (xmlHttpCipher==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  } 
  var url="drawCipher.php?key="+keyName;
  xmlHttpCipher.onreadystatechange=cipherStateChanged;
  xmlHttpCipher.open("GET",url,true);
  xmlHttpCipher.send(null);

  xmlHttpKey=GetXmlHttpObject();
  if (xmlHttpKey==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  } 
  var url="drawKey.php?key="+keyName;
  xmlHttpKey.onreadystatechange=keyStateChanged;
  xmlHttpKey.open("GET",url,true);
  xmlHttpKey.send(null);
}

function login()
{ 
	if(document.getElementById('loginButton').value=='Change')
	{
		changePassword();
	}
	else
	{
		xmlHttpLog=GetXmlHttpObject();
		if (xmlHttpLog==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		} 
		var url="login.php";
		xmlHttpLog.onreadystatechange=loginStateChanged;
		xmlHttpLog.open("POST",url,true);
		xmlHttpLog.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		var user = document.getElementById("username").value;
		var pass = document.getElementById("password").value;
		document.getElementById("password").value='';
		xmlHttpLog.send('username='+user+'&password='+pass);
	}
}

function changePassword()
{ 
	if (document.getElementById("pwdchg1").value!=document.getElementById("pwdchg2").value)
	{
		document.getElementById("loginValidation").innerHTML="Passwords Do Not Match";
	}
	else if(document.getElementById("pwdchg1").value.length<8)
	{
		document.getElementById("loginValidation").innerHTML="Passwords must be at least 8 characters";
	}
	else
	{
		xmlHttpChange=GetXmlHttpObject();
		if (xmlHttpChange==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		} 
		var url="changepassword.php";
		xmlHttpChange.onreadystatechange=changeStateChanged;
		xmlHttpChange.open("POST",url,true);
		xmlHttpChange.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		var user = document.getElementById("username").value;
		var oldpass = document.getElementById("password").value;
		var newpass = document.getElementById("pwdchg1").value;
		xmlHttpChange.send('username='+user+'&oldpassword='+oldpass+'&newpassword='+newpass);
	}
}

function forgotPassword()
{ 
  xmlHttpForgot=GetXmlHttpObject();
  if (xmlHttpForgot==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  } 
  var url="resetpassword.php";
  xmlHttpForgot.onreadystatechange=forgotStateChanged;
  xmlHttpForgot.open("POST",url,true);
  xmlHttpForgot.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  var user = document.getElementById("username").value;
  xmlHttpForgot.send('username='+user);
}

function register()
{ 
	xmlHttpReg=GetXmlHttpObject();
	if (xmlHttpReg==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="register.php";
	xmlHttpReg.onreadystatechange=registerStateChanged;
	xmlHttpReg.open("POST",url,true);
	xmlHttpReg.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var user = document.getElementById("username1").value;
	var pass = document.getElementById("regPassword").value;
	xmlHttpReg.send('username='+user+'&password='+pass);
}

function save()
{
  if(document.getElementById('saveTextBox').value=='')
  {
    document.getElementById('saveValidator').innerHTML='Please enter a name';
  }
  else if (document.getElementById('saveTextBox').value.length>30)
  {
    document.getElementById('saveValidator').innerHTML='Please enter 30 characters or less';
  }
  else
  {
    document.getElementById('saveValidator').innerHTML='';
    xmlHttpSave=GetXmlHttpObject();
    if (xmlHttpSave==null)
    {
      alert ("Your browser does not support AJAX!");
      return;
    } 
    var url="save.php";
    var plaintext;
    var key='<?xml version="1.0"?>\n<key cipher="zodiac340" name="'+document.getElementById('saveTextBox').value+'">';
    var x=document.getElementsByTagName('span');
    var i;
    for (i in x)
    {
      if (x[i].className=='key')
      {
        plaintext = x[i].childNodes.length>0?x[i].childNodes[0].innerHTML:"";
        key += '\n  <character ciphertext="'+x[i].title+'" plaintext="'+plaintext+'" />';
      }
    }
    key += '\n</key>';
    xmlHttpSave.onreadystatechange=saveStateChanged;
    xmlHttpSave.open("POST",url,true);
    xmlHttpSave.setRequestHeader("content-type", "text/xml");
    xmlHttpSave.send(key);
  }
}

function loadKeys()
{
  xmlHttpLoad=GetXmlHttpObject();
  if (xmlHttpLoad==null)
  {
    alert ("Your browser does not support AJAX!");
    return;
  } 
  var url="loadKeys.php";
  xmlHttpLoad.onreadystatechange=loadStateChanged;
  xmlHttpLoad.open("GET",url,true);
  xmlHttpLoad.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlHttpLoad.send(null);
}

function signout()
{ 
	if (document.getElementById('signout').innerHTML=='Log in/Register')
	{
		showLogin();
	}
	else
	{
		xmlHttpOut=GetXmlHttpObject();
		if (xmlHttpOut==null)
		{
			alert ("Your browser does not support AJAX!");
			return;
		} 
		var url="signout.php";
		xmlHttpOut.onreadystatechange=signoutStateChanged;
		xmlHttpOut.open("GET",url,true);
		xmlHttpOut.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttpOut.send(null);
	}
}

function GetXmlHttpObject()
{
  var xmlHttp=null;
  try
  {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

function cipherStateChanged() 
{ 
  if (xmlHttpCipher.readyState==4)
  { 
    document.getElementById("cipher").innerHTML=xmlHttpCipher.responseText;
  }
}

function loginStateChanged() 
{ 
	if (xmlHttpLog.readyState==4)
	{ 
		document.getElementById("loginValidation").innerHTML=xmlHttpLog.responseText;
		if(xmlHttpLog.responseText=='Success')
		{
			closeLogin();
			document.getElementById("signout").innerHTML='Sign out';
			loadKeys();
		}
	}
}

function changeStateChanged() 
{ 
	if (xmlHttpChange.readyState==4)
	{ 
		document.getElementById("loginValidation").innerHTML=xmlHttpChange.responseText;
		if(xmlHttpChange.responseText=='Password Changed')
		{
			//do stuff
		}
	}
}

function forgotStateChanged() 
{ 
	if (xmlHttpForgot.readyState==4)
	{ 
		document.getElementById("loginValidation").innerHTML=xmlHttpForgot.responseText;
	}
}

function registerStateChanged() 
{ 
  if (xmlHttpReg.readyState==4)
  { 
    var resp=xmlHttpReg.responseText;
    document.getElementById("registerValidation").innerHTML=resp;
    if (resp=='Success')
    {
		document.getElementById('regPassword').value='';
		document.getElementById('regPassword2').value='';
    }
  }
}

function keyStateChanged() 
{ 
  if (xmlHttpKey.readyState==4)
  { 
    document.getElementById("keyBlock").innerHTML=xmlHttpKey.responseText;
  }
}

function saveStateChanged() 
{ 
  if (xmlHttpSave.readyState==4)
  { 
    if(xmlHttpSave.responseText=='Success')
    {
    	document.getElementById("saveValidator").innerHTML="Saved";
      loadKeys();
    }
    else if (xmlHttpSave.responseText=='Overwrite')
    {
      document.getElementById("saveValidator").innerHTML="Name already exists";
    }
    else
    {
      document.getElementById("saveValidator").innerHTML="Please log in to save";
    }
  }
}

function signoutStateChanged() 
{ 
	if (xmlHttpOut.readyState==4)
	{ 
		document.getElementById("signout").innerHTML=xmlHttpOut.responseText;
		if (xmlHttpOut.responseText=='Log in/Register')
		{
			document.getElementById("savedKeys").innerHTML="";
			draw();
		}
	}
}

function loadStateChanged() 
{ 
  if (xmlHttpLoad.readyState==4)
  { 
    document.getElementById("savedKeys").innerHTML=xmlHttpLoad.responseText;
  }
}

function checkStateChanged()
{
  if (xmlHttpCheck.readyState==4)
  { 
    document.getElementById("signout").innerHTML=xmlHttpCheck.responseText;
  }
}

function submitOnEnter(e)
{
	var keycode;
	if (window.event)
		keycode = window.event.keyCode;
	else if (e) 
		keycode = e.which;
	if (keycode == 13)
	{
		login();
	}
}

function submitRegOnEnter(e)
{
	var keycode;
	if (window.event)
		keycode = window.event.keyCode;
	else if (e) 
		keycode = e.which;
	if (keycode == 13)
	{
		register();
	}
}

function saveOnEnter(e)
{
	var keycode;
	if (window.event)
		keycode = window.event.keyCode;
	else if (e) 
		keycode = e.which;
	if (keycode == 13)
	{
		save();
	}
}

function checkCaptcha()
{
	if(document.getElementById("username1").value!=document.getElementById("username2").value)
	{
		document.getElementById("registerValidation").innerHTML='E-mail Addresses Do Not Match';
	}
	else if(document.getElementById("regPassword").value!=document.getElementById("regPassword2").value)
	{
		document.getElementById("registerValidation").innerHTML='Passwords Do Not Match';
	}
	else if (document.getElementById("regPassword").value.length<8)
	{
		document.getElementById("registerValidation").innerHTML='Password must be at least 8 characters';
	}
	else
	{
		xmlHttpCaptcha=GetXmlHttpObject();
	    if (xmlHttpCaptcha==null)
	    {
	      alert ("Your browser does not support AJAX!");
	      return;
	    } 
	    var url="validateCaptcha.php";
	    var ch=window.frames["captchaWidget"].document.getElementsByName('recaptcha_challenge_field')[0].value;
	    var re=window.frames["captchaWidget"].document.getElementsByName('recaptcha_response_field')[0].value;
	    xmlHttpCaptcha.onreadystatechange=captchaStateChanged;
	    xmlHttpCaptcha.open("POST",url,true);
	    xmlHttpCaptcha.setRequestHeader("content-type", "application/x-www-form-urlencoded");
	    xmlHttpCaptcha.send('recaptcha_challenge_field='+ch+'&recaptcha_response_field='+re);
	}
} 

function captchaStateChanged() 
{ 
	if (xmlHttpCaptcha.readyState==4 || xmlHttpCaptcha.readyState=="complete")
	{ 
		if(xmlHttpCaptcha.responseText=='Successful')
		{
			register();
			var f = document.getElementById('captchaWidget');
			f.src = f.src;
		}
		else
		{
			document.getElementById('registerValidation').innerHTML=xmlHttpCaptcha.responseText;
			var f = document.getElementById('captchaWidget');
			f.src = f.src;
		}
	} 
}

function checkFbCaptcha()
{
	if(document.getElementById("fbDescription").value.length<1 || document.getElementById("fbDescription").value.length>1000)
	{
		document.getElementById("fbValidation").innerHTML='Please enter a description between 1 and 1000 characters';
	}
	else
	{
		xmlHttpFbCaptcha=GetXmlHttpObject();
	    if (xmlHttpFbCaptcha==null)
	    {
	      alert ("Your browser does not support AJAX!");
	      return;
	    } 
	    var url="validateCaptcha.php";
	    var ch=window.frames["fbCaptchaWidget"].document.getElementsByName('recaptcha_challenge_field')[0].value;
	    var re=window.frames["fbCaptchaWidget"].document.getElementsByName('recaptcha_response_field')[0].value;
	    xmlHttpFbCaptcha.onreadystatechange=fbCaptchaStateChanged;
	    xmlHttpFbCaptcha.open("POST",url,true);
	    xmlHttpFbCaptcha.setRequestHeader("content-type", "application/x-www-form-urlencoded");
	    xmlHttpFbCaptcha.send('recaptcha_challenge_field='+ch+'&recaptcha_response_field='+re);
	}
} 

function fbCaptchaStateChanged() 
{ 
	if (xmlHttpFbCaptcha.readyState==4 || xmlHttpFbCaptcha.readyState=="complete")
	{ 
		if(xmlHttpFbCaptcha.responseText=='Successful')
		{
			submitFeedback();
			var f = document.getElementById('fbCaptchaWidget');
			f.src = f.src;
		}
		else
		{
			document.getElementById('fbValidation').innerHTML=xmlHttpFbCaptcha.responseText;
			var f = document.getElementById('fbCaptchaWidget');
			f.src = f.src;
		}
	} 
}

function submitFeedback()
{ 
	xmlHttpSubmitFeedback=GetXmlHttpObject();
	if (xmlHttpSubmitFeedback==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="submitfeedback.php";
	xmlHttpSubmitFeedback.onreadystatechange=submitFeedbackStateChanged;
	xmlHttpSubmitFeedback.open("POST",url,true);
	xmlHttpSubmitFeedback.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	var fbtype = document.getElementById("fbtype").value;
	var fbEmail = document.getElementById("fbEmail").value;
	var fbDescription = document.getElementById("fbDescription").value;
	xmlHttpSubmitFeedback.send('fbType='+fbtype+'&fbEmail='+fbEmail+'&fbDescription='+fbDescription);
}

function submitFeedbackStateChanged() 
{ 
  if (xmlHttpSubmitFeedback.readyState==4)
  { 
    var resp=xmlHttpSubmitFeedback.responseText;
    document.getElementById("fbValidation").innerHTML=resp;
    if (resp=='Success')
    {
    	document.getElementById("fbValidation").innerHTML='Feedback Submitted';
    	document.getElementById("fbDescription").value='';
    }
  }
}
