//$Id: facebook.js 4294 2010-04-22 19:23:18Z ddrake $

function facebookConnectFinish() {
	var facebookContainer = document.getElementById('facebookconnectcontainer');
	document.getElementById('facebookconnectbutton').style.display = 'none';
	document.getElementById('facebookconnectchecking').style.display = '';

	var facebookSignInContainer = document.getElementById('facebookconnectcontainersignin');
	var facebookHeaderContainer = document.getElementById('facebookconnectcontainerheader');
	
	if (facebookHeaderContainer != null) {
		document.getElementById('facebookconnectbuttonheader').style.display = 'none';
		document.getElementById('facebookconnectcheckingheader').style.display = '';
	}	
	
	if (facebookSignInContainer != null) {
		document.getElementById('facebookconnectbuttonsignin').style.display = 'none';
		document.getElementById('facebookconnectcheckingsignin').style.display = '';
	}	
	

	var postURI = '/ajaxserver/handlefacebooklogin.php5';
	var postString = '';
	loadXMLDocPost(postURI, postString);
}

function redirectToMystuff() {
	window.location = '/';
}

function redirectToLinkSuccess() {
	window.location = '/facebook/facebook_link_success.php5';
}

function redirectToProfileLink() {
	window.location = '/facebook/facebook_link_success.php5?new';
}

function redirectToConfirmRegistration() {
	window.location = '/confirm_registration.php5';
}

function redirectToLocation(input, results, success) {
	var redirectLocation = results[0].getElementsByTagName('location')[0].firstChild.data;
	window.location = redirectLocation;
}

function closeFacebookConnectWindow() {
	toggleme('opacitize', '0');
	toggleme('fullimagebox');
	
	document.getElementById('facebookconnectbutton').style.display = '';
	document.getElementById('facebookconnectchecking').style.display = 'none';


	var facebookSignInContainer = document.getElementById('facebookconnectcontainersignin');

	if (facebookSignInContainer != null) {
		document.getElementById('facebookconnectbuttonsignin').style.display = '';
		document.getElementById('facebookconnectcheckingsignin').style.display = 'none';
	}
}

function toggleFacebookSigninBox(input, results, success) {
	// If we don't have the correct elements on the page, then we need to take 'em to the signin page where they can finish
	if (document.getElementById('linkaccountserror') == null) {
		window.location = '/signin.php5?finishfacebooklogin';
	}

	var facebookName = results[0].getElementsByTagName('facebookname')[0].firstChild.data;

	//Functions that grab screen dimensions
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	//ID's for affected html elements	
	fullboxID = 'fullimagebox';

	fullbox = document.getElementById(fullboxID);
	fullbox.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 500) / 2) + 'px');
	fullbox.style.left = (((arrayPageSize[0] - 600) / 2) + 'px');

	toggleme('opacitize', '0');

	//Wierd IE7 Bug: If height of opacitize div is over 4096, it won't accept an alpha transparency effect. Workaround:
	if (arrayPageSize[1] > 4096 && document.all) { 
	document.getElementById('opacitize').style.height = '4096px'; 
	}
	else //do the intended thing
	{
	document.getElementById('opacitize').style.height = (arrayPageSize[1] + 'px');     
	}

	// Bug in IE6 where dropdown boxes show through
	var dropDowns = document.getElementsByTagName('select');
	for (var ii = 0; ii < dropDowns.length; ii++) {
		if (dropDowns[ii].style.display == 'none') {
			dropDowns[ii].style.display = '';
		} else {
			dropDowns[ii].style.display = 'none';
		}
	}

	//If IE, force alpha transparency using the "filter" property      
	if (document.all) { document.getElementById('opacitize').style.filter = 'alpha(opacity=50)';}
	
	var facebookNameArea = document.getElementById('facebooknamearea');
	if (facebookNameArea != null && facebookName != null && facebookName != '') {
		if (facebookNameArea.hasChildNodes()) {
			while (facebookNameArea.childNodes.length >= 1) {
				facebookNameArea.removeChild(facebookNameArea.firstChild);
			}
		}
		facebookNameArea.appendChild(document.createTextNode(facebookName));
	}

	toggleme(fullboxID);
}

function checkLogin() {
	// Grab our inputs
	var userName = document.getElementById('loginusername').value;
	var password = document.getElementById('loginpassword').value;

	// Make sure they aren't null
	if (userName == null || userName == '') {
		alert("You must provide a non-empty username.");
	} else if (password == null || password == '') {
		alert("You must provide a non-empty pasword.");
	} else {
		// Let's give it a shot. First we'll clear the button
		document.getElementById('linkaccountsbutton').style.display = 'none';

		// Also, we'll clear out any errors that may be there
		document.getElementById('linkaccountserror').style.display = 'none';

		// Now we'll show them a loading image to let 'em know we're doing something
		document.getElementById('linkaccountloading').style.display = '';
		var postURI = '/ajaxserver/checklogin.php5';
		var postString = 'username='+userName+'&password='+password;
		
		loadXMLDocPost(postURI, postString);
	}
}

function facebookLinkFail() {
	// A fatal error occurred while trying to sign in
	var linkAccountsError = document.getElementById('linkaccountserror');
	
	if (linkAccountsError.hasChildNodes()) {
		while (linkAccountsError.childNodes.length >= 1) {
			linkAccountsError.removeChild(linkAccountsError.firstChild);
		}
	}

	linkAccountsError.appendChild(document.createTextNode("There was an error that occurred while trying to check your username and password. Please contact the administrative team."));
	linkAccountsError.appendChild(document.createElement('br'));
	linkAccountsError.appendChild(document.createElement('br'));
	linkAccountsError.style.display = '';
}

function facebookCreateFail() {
	// A fatal error occurred while trying to sign in
	var newAccountsError = document.getElementById('newaccountserror');
	
	if (newAccountsError.hasChildNodes()) {
		while (newAccountsError.childNodes.length >= 1) {
			newAccountsError.removeChild(newAccountsError.firstChild);
		}
	}

	newAccountsError.appendChild(document.createTextNode("There was an error that occurred while trying to check the availability of your username and email address. Please contact the administrative team."));
	newAccountsError.appendChild(document.createElement('br'));
	newAccountsError.appendChild(document.createElement('br'));
	newAccountsError.style.display = '';
}

function facebookLinkAuthFail() {
	// A fatal error occurred while trying to sign in
	var linkAccountsError = document.getElementById('linkaccountserror');
	
	if (linkAccountsError.hasChildNodes()) {
		while (linkAccountsError.childNodes.length >= 1) {
			linkAccountsError.removeChild(linkAccountsError.firstChild);
		}
	}

	linkAccountsError.appendChild(document.createTextNode("You provided an incorrect username or password. Please try again."));
	linkAccountsError.appendChild(document.createElement('br'));
	linkAccountsError.appendChild(document.createElement('br'));
	linkAccountsError.style.display = '';
	
	// Give them their button back
	document.getElementById('linkaccountloading').style.display = 'none';
	document.getElementById('linkaccountsbutton').style.display = '';
}

function facebookCreateDeny(input, results, success) {
	// A fatal error occurred while trying to sign in
	var newAccountsError = document.getElementById('newaccountserror');
	var message = results[0].getElementsByTagName('denyreason')[0].firstChild.data; 
	
	if (newAccountsError.hasChildNodes()) {
		while (newAccountsError.childNodes.length >= 1) {
			newAccountsError.removeChild(newAccountsError.firstChild);
		}
	}

	newAccountsError.appendChild(document.createTextNode(message));
	newAccountsError.appendChild(document.createElement('br'));
	newAccountsError.appendChild(document.createElement('br'));
	newAccountsError.style.display = '';
	
	// Give them their button back
	document.getElementById('newaccountloading').style.display = 'none';
	document.getElementById('newaccountsbutton').style.display = '';
}

function checkAccountAvailibility() {
	// Grab our variables
	var emailAddress = document.getElementById('newemail').value;

	// Confirm they aren't empty and verify the email address is legit and that the passwords match
	if (emailAddress == null || emailAddress == '') {
		alert("You must provide a non-empty email address.");
	} else if (emailAddress.match(/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/) == false) {
		alert("You must provide a valid email address.");
	} else {
		// Let's give it a shot. First we'll clear the button
		document.getElementById('newaccountsbutton').style.display = 'none';

		// Remove any errors that may be there
		document.getElementById('newaccountserror').style.display = 'none';

		// Show them the loading image to let 'em know we're checking
		document.getElementById('newaccountloading').style.display = '';

		var postURI = '/ajaxserver/checkregistration.php5';
		var postString = 'email='+emailAddress;
		
		loadXMLDocPost(postURI, postString);
	}
}
