/******************************************************************************
Epiware: Project and Document Management
http://www.epiware.com
Copyright (C) 2006 James Kern

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Epiware, Inc. and Praxis Inc, hereby disclaims all copyright
interest in the program 'Epiware' written
by James Kern, Edward Kline, Ryan Gilfether, Ray Gorospe, Patrick Waddingham

06 September 2006
James Kern, President of Epiware
Jack Chapman, President of Praxis
*****************************************************************************/
function myWindow(popupURL,x,y)
{
        if(typeof popup == "undefined" || popup.closed)
        {
        	popup = window.open("","",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+x+',height='+y);
        }

        popup.location = popupURL;
        popup.focus();
}


function setfocus()
{
	var login = $('username');
	var password = $('password');
	if(login.value == '')
	{
		Field.focus(login);
	}
	else
	{
		Field.activate(password);
	}
	return;
}

function disableLogin()
{
	var loginBtn = $('loginButton');
	if(loginBtn)
	{
		loginBtn.hide();
	}
}

function init()
{
	var jsWarning = $('jsWarning');
	if(jsWarning)
	{
		jsWarning.hide();
	}
	var loginBtn = $('loginButton');
	if(loginBtn)
	{
		loginBtn.show();
	}
	if(typeof CookieHandler != 'undefined')
	{
		var Cookie = new CookieHandler({warningDiv:'jsWarning',onCookieFail:disableLogin});
		Cookie.check('ajax_cookie_check.php');
		var cookie_login = Cookie.get('epi_login_name');

		var login = $('username');

		if(login && cookie_login)
		{
			login.value = cookie_login;
		}

	}

	setfocus();
	var logindiv = $('loginDiv');
	if(logindiv)
	{
		Event.observe(logindiv,'keydown',captureEnter,false);
	}
}

Event.observe(window,'load',init,false);

function captureEnter(e)
{
	var key = e.keyCode || e.which;
	if(key == Event.KEY_RETURN)
	{
		var loginBtn = $('loginButton');
		if(loginBtn)
		{
			if(loginBtn && loginBtn.style.display != 'none')
			{
				doSubmit();
			}
		}
	}
}

function doSubmit()
{
	var form = $('loginForm');
	if(form)
	{
		var ediv = $('loginError');
		if(ediv)
		{
			ediv.hide();
		}
		var div = $('loginProcessing');
		if(div)
		{
			div.show();
		}

		var postStr = Form.serialize(form);
		var options = {method:'post',postBody:postStr,onFailure:onfail,onSuccess:onLoginSuccess};
		var t = new Ajax.Request('./epi_ajax_login.php',options);
	}
	return false;
}

function onfail(req)
{
	var msg = req.responseText || req.statusText || req.status;
	if(msg && msg != '')
	{
		alert(msg);
	}
	var div = $('loginProcessing');
	if(div)
	{
		div.hide();
	}
	setfocus();
}
var fail_attempt = 0;
function onLoginSuccess(req)
{

	var result = (req.responseXML && req.responseXML.documentElement && req.responseXML.documentElement.nodeName && req.responseXML.documentElement.nodeName != 'parsererror')?req.responseXML.documentElement:null;

	if(result)
	{
		var div = $('loginProcessing');
		if(div)
		{
			div.hide();
		}



		clearError();
		var msgs = $A(result.getElementsByTagName('message'));
		if(msgs.length > 0)
		{
			fail_attempt++;
			if(typeof shakeObject == 'function')
			{
				shakeObject('shakeMe');
			}
			if(fail_attempt > 3)
			{
				window.location.href='./forgot_password.php';
				return;
			}
			msgs.each(showMessage);
			setfocus();
		}


		var redir = $A(result.getElementsByTagName('redirect'));

		redir.each(doRedirect);
	}
	else
	{
		onfail(req);
	}
}

function doRedirect(node)
{
	var url = getFieldValue(node);
	if(url != '')
	{
		document.location.href = url;
	}
}

function clearError()
{
	var div = $('loginError');
	if(div)
	{
		div.innerHTML = '';
	}
}

function showMessage(msg)
{
	var div = $('loginError');
	if(div)
	{
		div.innerHTML = getFieldValue(msg);
		div.show();
	}
}

function getFieldValue(field)
{
	var value = field.getAttribute('value');
	var cdata = getcdata(field);
	if(cdata && cdata!='')
		return cdata;

	return value;
}
function getcdata (collectionObj)
{
	var fieldvalue = '';
	for(var i=0;i<collectionObj.childNodes.length;i++)
	{
		if(collectionObj.childNodes[i].nodeType==4 && collectionObj.childNodes[i].nodeValue!='')
		{
			fieldvalue += collectionObj.childNodes[i].nodeValue;
		}
	}
	return fieldvalue;
}