/**
 * OmegaFlash - Web-Based Flash Cards
 * Copyright (C) 2007 Omega Vortex Corporation
 */

var t = null;

/**
 * Goes out to the server and checks to see if
 * the user's key matches on we have on record.
 * Redirects if there's a successful match.
 */
function checkKey(element, e)
{
	if ( element.value == "" || element.value == "Enter Flash Card Key" ) {
		return false;
	}
	
	var check = null;
	
	check = function() {
		document.getElementById('checkingText').innerHTML = "Searching for your Key ...";
		var xmlHttp = getXHRObject();
		
		if ( xmlHttp == false ) {
			document.getElementById('checkingText').innerHTML = "<strong><span class='color:#F00;'>Your browser doesn't seem to support OmegaFlash's JavaScript. Please ensure you're using Internet Explorer 6+, Firefox 1.5+, or Opera 9+.</span></strong>";
		}
		
		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4) {
				result = xmlHttp.responseText;
				
				if ( result == "TRUE" ) {
					document.getElementById('checkingText').innerHTML = "We found your key '" + element.value + "' ... Redirecting.";
					setTimeout(function() { window.location = "collections.php?key=" + element.value; }, 3000);
				}
				else {
					document.getElementById('checkingText').innerHTML = "The key you entered '" + element.value + "' could not be found. Please try again.";
				}
			}
		};
		
		var url = "checkKey.php?key=" + element.value;
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	};
	
	if ( e != null ) {
		if ( window.event ) {
			keynum = e.keyCode;
		}
		else if ( e.which ) {
			keynum = e.which;
		}
		
		if ( keynum == 13 ) {
			clearTimeout(t);
			check();
		}
	}
	
	if ( t != null ) {
		clearTimeout(t);
	}
	
	t = setTimeout(check, 2000);
}

/**
 * Cross-browser function for fetching the
 * XMLHTTPRequest Object.
 */
function getXHRObject()
{
	var xmlHttp = false;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
    catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				return false;
			}
		}
    }
    
    return xmlHttp;
}

/**
 * Function to flip the cards over.
 */
function flipCard()
{
	obj1 = document.getElementById('cardBody1');
	obj2 = document.getElementById('cardBody2');
	if (obj1.style.display == 'block')
	    obj1.style.display = 'none';
	else
	    obj1.style.display = 'block';

        if (obj2.style.display == 'block')
            obj2.style.display = 'none';
        else
            obj2.style.display = 'block';

}
