// JavaScript Document

var f1Login_http_request = false;
function f1LogIn() 
{
    var uid = encodeURIComponent(document.getElementById("uid").value)
    var pwd = encodeURIComponent(document.getElementById("pwd").value);

	var tmpFields = "uid=" + uid + "&pwd=" + pwd;

	//alert(tmpFields);
	makeRequest(tmpFields);
}


function makeRequest(fields)
{
    if (f1Login_http_request)
    {
        f1Login_http_request.abort();
        f1Login_http_request = false;
	}

	var fullurl = "/includes/f1GetToken.aspx?" + fields;
	if (window.XMLHttpRequest) { // Mozilla [11], Safari, IE 7
	    f1Login_http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE 6-
	    f1Login_http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	f1Login_http_request.onreadystatechange = alertContents;
	f1Login_http_request.open("GET", fullurl, true);
	f1Login_http_request.send(null);

}

function alertContents() 
{
	var xml;
	var txt;
	var tmpArray;
	   
	if (f1Login_http_request.readyState == 4)
	{
	 //alert(f1Login_http_request.responseText);
	    if (f1Login_http_request.status == 200)
		{		  
			// reassign data
		    xml = f1Login_http_request.responseXML;
		    txt = f1Login_http_request.responseText;
			tmpArray = txt.split("~");
			
			switch(tmpArray[0]) 
			{
			    case "success":
			        document.getElementById("f1set").value = "true";
			        var type = document.getElementById("f1type").value;
			        var code = document.getElementById("f1code").value;

			        openF1(type, code);
			        document.location = document.location;
			        break;
				
			case "log in failed":
				alert("We could not find the user name and password you provided.");
				break;

			default:
				alert("ERROR CHECKING ACCOUNT DATABASE. Please try again");
				break;
			}
		} 
		else 
		{
			alert("There was a problem with the request. Please try again.");
		}
		
	}
}


function openF1(type, code)
{
    var churchCode = "TO4GFFEQvGIu62FRCzgy/w==";
    var title = "";

    var url = null;
    switch (type)
    {
        case "create":
            {
                title = "Create Account"
                url = "https://integration.fellowshipone.com/integration/conversion/create.aspx?cCode=" + encodeURI(churchCode);
                break;
            }
        case "update":
            {
                title = "My Account"
                url = "https://integration.fellowshipone.com/integration/profileEditor.aspx?cCode=" + encodeURI(churchCode);
                break;
            }
        case "giving":
            {
                title = "Give Online"
                url = "https://integration.fellowshipone.com/integration/contribution/onlinecontribution.aspx?cCode=" + encodeURI(churchCode);
                break;
            }
        case "eventRegistration":
            {
                title = "Register"
                url = "https://integration.fellowshipone.com/integration/registration/eventregistration.aspx?cCode=" + encodeURI(churchCode) + "&fCode=" + encodeURI(code);
                break;
            }
        case "customRegister":
            {
                title = "Register"
                url = "https://integration.fellowshipone.com/integration/FormBuilder/FormBuilder.aspx?cCode=" + encodeURI(churchCode) + "&fCode=" + encodeURI(code);
                break;
            }
        case "loginHelp":
            {
                title = "My Account"
                url = "https://integration.fellowshipone.com/integration/loginhelp.aspx?cCode=" + encodeURI(churchCode);
                break;
            }
        case "resetPassword":
            {
                title = "My Account"
                url = "https://integration.fellowshipone.com/integration/resetpassword.aspx?cCode=" + encodeURI(churchCode);
                break;
            }
        case "groupFinder":
            {
                title = "Group Finder"
                url = "https://integration.fellowshipone.com/integration/activityfinder/activityfinder.aspx?cCode=" + encodeURI(churchCode) + "&mCode=J0JzfLVEKnSgY1UpRSDCnQ==" + code;
                break;
            }
        case "groupManager":
            {
                title = "Group Manager"
                //I odn't know what the acode is at the end of this
                url = "https://integration.fellowshipone.com/integration/smallgroup/sgmembers.aspx?cCode=" + encodeURI(churchCode) + "&aCode=" + code;
                break;
            }
        case "f1Contact":
            {
                title = "Contact"
                url = "https://integration.fellowshipone.com/integration/contact/onlinecontact.aspx?cCode=" + encodeURI(churchCode) + "&ctCode=" + code;
                break;
            }
        case "volunteer":
            {
                title = "Volunteer"
                url = "https://integration.fellowshipone.com/integration/volunteer/volunteerapplication.aspx?cCode=" + encodeURI(churchCode) + "&appCode=" + code;
                break;
            }
    }


    var ScreenWidth = window.screen.width;
    var ScreenHeight = window.screen.height;
    var movefromedge = 0;
    placementx = (ScreenWidth / 2) - ((620) / 2);
    placementy = (ScreenHeight / 2) - ((630 + 50) / 2);

    //document.location = document.location;
    window.open(url, "f1window", "height=650,width=760,resizeable=1,scrollbars=1,menubar=0,toolbar=0,status=0,location=0,left=" + placementx + ",top=" + placementy + ",screenX=" + placementx + ",screenY=" + placementy);

}


function showModal()
{
    $("#contact-container").modal({ onOpen: function(dialog)
    {
        dialog.overlay.fadeIn('fast', function()
        {
            dialog.container.fadeIn('fast', function()
            {
                dialog.data.fadeIn('fast');
            });
        });
    }
    });
}

function hideModal()
{
    $("#contact-container").modal({ onClose: function(dialog)
    {
        dialog.data.slideUp('slow', function()
        {
            dialog.container.slideUp('slow', function()
            {
                dialog.overlay.fadeOut('slow', function()
                {
                    $.modal.close(); // must call this!
                });
            });
        });
    }
    });

}


/*
function f1LogOut()
{
	if(http_request) 
	{
		//http_request.onreadystatechange = '';
		http_request.abort();
		http_request = false;
	}
	document.getElementById("uid").value = "";
	document.getElementById("pwd").value = "";
	document.getElementById("sessid").value = "";
	document.getElementById("targetcode").value = "";
	document.getElementById("targetfunction").value = "";

	
	var fullurl = "/includes/f1Logout.asp"

	if (window.XMLHttpRequest) { // Mozilla [11], Safari, IE 7
		http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE 6-
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	http_request.onreadystatechange = refreshPage;
	
	http_request.open("GET", fullurl, true);
	http_request.send(null);

}

function refreshPage()
{
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
			http_request = false;
			document.location = document.location
		}
	}
}


function KeyCheck(e)
{
	var KeyID = (window.event) ? event.keyCode : e.keyCode;
	if (KeyID == 13 && (currFocus == "uid" || currFocus == "pwd"))
	{
		//alert("Please wait while we access your account.");
		//Set cursor to hourglass

		f1LogIn();
	}
}
	
//document.onkeyup = KeyCheck;

*/
