/************************************************************
  Some of the codes is suggested/provided in the book "Ajax 
  Design Patterns" written by Michael Mahemof. The rest of 
  the code is written by William Au Yeung.
  
  If you find any codes useful, feel free to copy them away
  but keep the information above as reference.
************************************************************/

var xhReq = null;
var debug = false;
var ffFilterWarning = false;

function $(id) { return document.getElementById(id); }

function createXMLHttpRequest()
{
	try { return new XMLHttpRequest(); } catch (e) {}
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTPP"); } catch (e) {}
	alert("XMLHTTPRequest not supported");
	return null;
}

function setupMainFrameFilter()
{
	if (! ffFilterWarning )
	{
		try
		{
			$('mainFrame').style.filter = "revealTrans(Duration=1.0,Transition=2)"; //  "blendTrans(duration=0.7)";
		} catch (e)
		{
			ffFilterWarning = true;
			window.alert('This website supports IE 6 , 7, or 8 only. Please use IE and try again');
		}
	}
}

function setupGreyOut(makeGrey)
{
	if ( ! ffFilterWarning )
	{
		if ( makeGrey )
		{
			$('theBody').style.filter ='progid:DXImageTransform.Microsoft.BasicImage(grayscale=1)';
		}
		else
		{
			$('theBody').style.filter = '';
		}
    }
}

function setupShowHiddenDiv(theDiv)
{
	var theDivPanel = $(theDiv);
	if ( theDivPanel == null )
	{
		window.alert("Please wait until the site is finished loading");
	}
	else
	{
		theDivPanel.style.visibility = 'visible';
		setupGreyOut(true);
	}
}

function setupHideHiddenDiv(theDiv)
{
	var theDivPanel = $(theDiv);
	theDivPanel.style.visibility = 'hidden';
	setupGreyOut(false);
}

function setupItemMouseOver()
{
	var dirList = $('directoryListingGroup');
	if ( dirList == null ) 
	{
		return;
	}
	var len = dirList.childNodes.length;
	for ( var i = 0 ; i < len ; ++i )
	{
		var theDir = dirList.childNodes[i];
		theDir.onmouseover = function() { this.style.backgroundColor='#FFCC66'; }
		theDir.onmouseout = function () { this.style.backgroundColor='#DDDDDD'; }
	}
	var fileList = $('fileListingGroup');
	len = fileList.childNodes.length;
	for ( var i = 0 ; i < len ; ++i )
	{
		var theFile = fileList.childNodes[i];
		theFile.onmouseover= function() { this.style.backgroundColor='#FFCC66'; }
		theFile.onmouseout= function() { this.style.backgroundColor='white'; }
	}
}

function prepareAjax()
{
	if ( xhReq == null )
	{
		xhReq = createXMLHttpRequest();
	}
	else
	{
		xhReq.abort();
	}
}

function deactivateLoginForm(isDisabled)
{
	var theForm = $('loginForm');
	var len = theForm.elements.length;
	for ( var i = 0 ; i < len ; ++i )
	{
		theForm.elements[i].disabled = isDisabled;
	}

}

function loginSubmit()
{
	prepareAjax();
	deactivateLoginForm(true);
	var queryString = "submitted_uname=" + document.loginForm.name.value + "&submitted_upasswd=" + document.loginForm.password.value;
	if ( debug ) 
	{
		alert(queryString);
	}
	xhReq.open("POST", "login.php", true);
	xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhReq.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");
	xhReq.onreadystatechange = handleLoginSubmit;
	xhReq.send(queryString);
}

function getPageContent(page)
{
	setupShowHiddenDiv('loadingPanel');
	prepareAjax();
	xhReq.open("GET", page, true);
	xhReq.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");
	xhReq.onreadystatechange = handlePageContent;
	xhReq.send(null);
}

function openDir(path)
{
	getPageContent("openPage.php?a=j&d=" + path);
}

function openLogin()
{
	getPageContent("openPage.php?a=l");
}

function openLogout()
{
    setupGreyOut(true);
    var logoutConfirm = window.confirm("Are you sure you want to log out?");
    if ( logoutConfirm )
    {
        prepareAjax();
        setupShowHiddenDiv('loadingPanel');
        xhReq.open("GET", "logout.php", false);
        xhReq.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");
        xhReq.send(null);
        var result = xhReq.responseText;
        if ( result == 'logged out' )
        {
            setupHideHiddenDiv('loadingPanel');
            setupGreyOut(false);
            window.location = "http://www.darkmars.net/";
        }
    }
    else
    {
        setupHideHiddenDiv('loadingPanel');
        setupGreyOut(false);
    }
}

function handlePageContent()
{
	if ( xhReq != null )
	{
		if ( xhReq.readyState != 4 )
		{
			// handle state update msg to LoadingPanel
			return;
		}
		if ( xhReq.status != 200 )
		{
			alert(xhReq.statusText);
			return;
		}
		setupHideHiddenDiv('loadingPanel');
		$('mainFrame').filters.revealTrans.apply();
		$('mainFrame').innerHTML = xhReq.responseText;
		$('mainFrame').filters.revealTrans.play();
		setupItemMouseOver();
	}
}

function handleLoginSubmit()
{
	if ( xhReq != null )
	{
		if ( xhReq.readyState != 4 )
		{
			// handle state update msg to LoadingPanel
			return;
		}
		if ( xhReq.status != 200 )
		{
			// handle error
			deactivateLoginForm(false);
			var errorDiv = $('errorMsg');
			if ( errorDiv != null )
			{
				errorDiv.innerText = "" + xhReq.status + " " + xhReq.statusText;
			}
			return;
		}
		
		var result = xhReq.responseText;
		if ( result == 'success' )
		{
			openDir('');
		}
		else
		{
			if ( debug ) { alert('result = ' + result); }
			deactivateLoginForm(false);
			var errorDiv = $('errorMsg');
			if ( errorDiv != null )
			{
				errorDiv.innerText = result;
			}
			return;
		}
	}

}

function cancelLoading()
{
	if ( xhReq != null )
	{
		xhReq.abort();
	}
	setupHideHiddenDiv('loadingPanel');
}

function handleForgetPassword(theDiv)
{
	var theDivPanel = $(theDiv);
	theDivPanel.style.visibility = 'hidden';

    prepareAjax();
    setupShowHiddenDiv('loadingPanel');
    var queryString = "submitted_email=" + document.requestPasswdForm.requestEmail.value;
    //alert("query = " + queryString);
    xhReq.open("POST", "forgetPassword.php", false);
    xhReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhReq.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2005 00:00:00 GMT");
    xhReq.send(queryString);
        
    var result = xhReq.responseText;
    if ( result == 'sent' )
    {
        alert("An email containing your login information has been sent to your mailbox!");
    }
    else
    {
        alert("Request fails!\r\n" + result);
    }
    setupHideHiddenDiv('loadingPanel');
}

function getFieldValue(fieldName)
{
	var theField = $(fieldName);
	if ( theField == null ) 
	{
		return "";
	}
	else
	{
		return theField.nodeValue;
	}
}

