var cObj;

var dlgAuthWait;
var dlgErrorParse;
var dlgErrorLogin;
var dlgErrorSubscription;


var dlg;
var dlgBad = null;
var dlgBadKl;
var dlgPasswd;
var dlgPasswdKl;
var dlgRequest;
var dlgSuccess;
var dlgSubmitProgress;

var dlgError;

var responsePasswordSuccess = function(o)
{
	good_password = eval("("+o.responseText+")");
	if (good_password == 0)
	{
		dlgWait.hide();

		if (dlgBad == null)
		{
			dlgBad = new YAHOO.widget.SimpleDialog("dlgBad", { width:"20em", fixedcenter:true, modal:true, draggable:false});
			dlgBad.setHeader("Warning!");
			dlgBad.setBody("Incorrect password");
			dlgBad.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN);
			dlgBad.cfg.queueProperty("buttons", [{text:"OK",handler:handleOK,isDefault:true}]);
			dlgBadKl = new YAHOO.util.KeyListener("dlgBad", { keys:13 }, { fn:handleOK, scope:dlgBad, correctScope:true });
			dlgBad.cfg.queueProperty("keylisteners", dlgBadKl);
			dlgBad.render(document.body);
		}
		else
			dlgBad.show();

		document.getElementById("passwd").value = "";
	}
	else
		window.location="index.php";
};

var responsePasswordFailure = function(o)
{
	dlgWait.hide();
	dlgErr = new YAHOO.widget.SimpleDialog("dlgErr", { width:"20em", fixedcenter:true, modal:true, draggable:false});
	dlgErr.setHeader("Error");
	dlgErr.setBody("Communication failure to server.");
	dlgErr.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_ALARM);
	dlgErr.cfg.queueProperty("buttons", [{text:"OK",handler:handleOK,isDefault:true}]); 
	dlgErr.render(document.body);
};

var handleClose = function()
{
	this.hide();
	document.getElementById("email").focus();
	document.getElementById("email").select();
}

var callbackPassword =
{
	success:responsePasswordSuccess,
	failure:responsePasswordFailure
};

var responseSuccessLogin = function(o)
{
	try
	{
		result = eval("("+o.responseText+")");
	}
	catch(e)
	{
		dlgAuthWait.hide();

		if (!dlgErrorParse)
		{
			dlgErrorParse = new YAHOO.widget.SimpleDialog("dlgErrorParseHTML", { width:"20em", fixedcenter:true, modal:true, draggable:false, visible:false, zindex:1000});
			dlgErrorParse.setHeader("Error");
			dlgErrorParse.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_ERROR);
			dlgErrorParse.cfg.queueProperty("buttons", [{text:"Close",handler:function(){this.hide();},isDefault:true}]);
			dlgErrorParse.render(document.body);
		}
		dlgErrorParse.setBody("Failed to parse response from server:<br>"+o.responseText);
		dlgErrorParse.show();

		return;
	}

	switch(result.status)
	{
	case "0": // Logged in just fine
		window.location = "index.php";
		break;
	case "1": // Invalid login
		dlgAuthWait.hide();
		if (!dlgErrorLogin)
		{
			dlgErrorLogin = new YAHOO.widget.SimpleDialog("dlgErrorLoginHTML", { width:"20em", fixedcenter:true, modal:true, draggable:false, visible:false, zindex:1000});
			dlgErrorLogin.setHeader("Error");
			dlgErrorLogin.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_ERROR);
			dlgErrorLogin.cfg.queueProperty("buttons", [{text:"Close",handler:function(){this.hide();document.getElementById("password").value="";document.getElementById("email").focus();document.getElementById("email").select();},isDefault:true}]);
			dlgErrorLogin.render(document.body);
		}
		dlgErrorLogin.setBody(result.msg);
		dlgErrorLogin.show();
		
		break;
	case "2": // Subscription has expired
		dlgAuthWait.hide();
		if (!dlgErrorSubscription)
		{
			dlgErrorSubscription = new YAHOO.widget.SimpleDialog("dlgErrorSubscriptionHTML", { width:"20em", fixedcenter:true, modal:true, draggable:false, visible:false, zindex:1000});
			dlgErrorSubscription.setHeader("Error");
			dlgErrorSubscription.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_ERROR);
			dlgErrorSubscription.cfg.queueProperty("buttons", [{text:"Close",handler:function(){this.hide(); window.location=result.msg},isDefault:true}]);
			dlgErrorSubscription.render(document.body);
		}
		dlgErrorSubscription.setBody("Your subscription has expired. You must renew your subscription.");
		dlgErrorSubscription.show();

		break;
	default:
		alert("This ain't Toto any more Kansas!"+"\n["+result.status+"]");
	}
};

var responseFailureLogin = function(o)
{
	dlgWait.hide();
	dlgErr = new YAHOO.widget.SimpleDialog("dlgErr", { width:"20em", fixedcenter:true, modal:true, draggable:false});
	dlgErr.setHeader("Error");n
	dlgErr.setBody("Communication failure to server.");
	dlgErr.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_ALARM);
	dlgErr.cfg.queueProperty("buttons", [{text:"OK",handler:handleOK,isDefault:true}]); 
	dlgErr.render(document.body);
};

var callbackLogin =
{
	success:responseSuccessLogin,
	failure:responseFailureLogin
};

var requestSuccess = function(o)
{
	dlgSubmitProgress.hide();

	dlgSuccess = new YAHOO.widget.SimpleDialog("dlgSuccess", { width:"20em", fixedcenter:true, modal:true, draggable:false});
	dlgSuccess.setHeader("Request complete");
	dlgSuccess.setBody("Your request for a study guide has been successfully submitted.<br><br>Please allow up to 3 - 4 weeks for delivery of the Comprehensive Review Notes.");
	dlgSuccess.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_INFO);
	dlgSuccess.cfg.queueProperty("buttons", [{text:"OK",handler:handleOK,isDefault:true}]); 
	dlgSuccess.render(document.body);n
};

var requestFailure = function(o)
{
	dlgSubmitProgress.hide();

	dlgErr = new YAHOO.widget.SimpleDialog("dlgErr", { width:"20em", fixedcenter:true, modal:true, draggable:false});
	dlgErr.setHeader("Error");
	dlgErr.setBody("Communication failure to server.");
	dlgErr.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_ALARM);
	dlgErr.cfg.queueProperty("buttons", [{text:"OK",handler:handleOK,isDefault:true}]); 
	dlgErr.render(document.body);
};

var handleOK = function()
{
	this.hide();
	document.getElementById("sdpa_name").focus();
	document.getElementById("sdpa_name").select();
}

var handleRequest = function()
{
	this.hide();
	dlgRequest.show();
}

var handleEnter = function()
{
	this.hide();
	cObj = YAHOO.util.Connect.asyncRequest("POST", "processpassword.php", callbackPassword, "password="+document.getElementById("passwd").value);
	dlgWait.show();
}

var handleCancel = function()
{
	this.hide();
	dlgPasswd.show();
}

var handleSubmit = function()
{
	this.submit();
	dlgSubmitProgress.show();
}

function onSubmitLogin()
{
	dlgAuthWait.show();

	YAHOO.util.Connect.setForm(document.getElementById("loginForm"));
	cObj = YAHOO.util.Connect.asyncRequest("POST", "processlogin.php", callbackLogin);

	return false;
}

function initialise()
{
	dlgError = new YAHOO.widget.SimpleDialog("dlgErrorHTML", { width:"20em", fixedcenter:true, modal:true, draggable:false, visible:false, zindex:1000});
	dlgError.setHeader("Error");
	dlgError.setBody("");
	dlgError.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_ERROR);
	dlgError.cfg.queueProperty("buttons", [{text:"Close",handler:handleOK,isDefault:true}]);
	dlgError.render(document.body);

	dlgAuthWait = new YAHOO.widget.SimpleDialog("dlgAuthWaitHTML", { width:"20em", fixedcenter:true, modal:true, draggable:false, visible:false, zindex:1000});
	dlgAuthWait.setHeader("Communicating");
	dlgAuthWait.setBody("Please wait, authenticating...");
	dlgAuthWait.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_INFO);
	dlgAuthWait.render(document.body);

	dlg = new YAHOO.widget.SimpleDialog("dlg", { width:"30em", fixedcenter:true, modal:true, draggable:false, visible:false});
	dlg.setHeader("Warning!");
	dlg.setBody("<p style=\"text-align:center\"><strong>Unknown user name</strong></p><p style=\"text-align:center\">If you experience problems logging in, please contact ETAS at <a href=\"mailto:support@pce-corp.com?subject=SDPA%20Login%20Difficulties\">support@pce-corp.com</a> or (212) 213-5436.</p>");
	dlg.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN);
	dlg.cfg.queueProperty("buttons", [{text:"Close",handler:handleOK,isDefault:true}]); 
	dlg.render(document.body);

	dlgPasswd = new YAHOO.widget.Dialog("dlgPassHTML", { width:"50em", fixedcenter:true, modal:true, draggable:false, visible:false});
	dlgPasswd.cfg.queueProperty("buttons", [ { text:"Request Comprehensive Review Notes", handler:handleRequest, align:"left" }, { text:"Enter Website", handler:handleEnter, isDefault:true, align:"right" } ]);
	dlgPasswdKl = new YAHOO.util.KeyListener(document, { keys:13 }, { fn:handleEnter, scope:dlgPasswd, correctScope:true } );
	dlgPasswd.cfg.queueProperty("keylisteners", dlgPasswdKl);
	dlgPasswd.render();

	dlgRequest = new YAHOO.widget.Dialog("dlgRequestHTML", { width:"40em", fixedcenter:true, modal:true, draggable:false, visible:false});
	dlgRequest.cfg.queueProperty("buttons", [ { text:"Cancel", handler:handleCancel }, { text:"Submit", handler:handleSubmit, isDefault:true } ]);
	dlgRequest.cfg.setProperty("postmethod","async");
	dlgRequest.callback.success = requestSuccess;
	dlgRequest.callback.failure = requestFailure;
	dlgRequest.render();

	dlgSubmitProgress = new YAHOO.widget.SimpleDialog("dlgSubmitProgress", { width:"20em", fixedcenter:true, modal:true, draggable:false, visible:false});
	dlgSubmitProgress.setHeader("Communicating");
	dlgSubmitProgress.setBody("Please wait, submitting your request...");
	dlgSubmitProgress.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_INFO);
	dlgSubmitProgress.render(document.body);


	document.getElementById("email").focus();
}
