/* readCookie() - this function is used to read a cookie stored with the browser when the page was sent by the server
 *
 * Arguments    : name - string representing the name of the cookie we want to find the value for
 *
 * Return       : value - the value of the cookie referenced by name argument
 *
 * Dependancies : this function depends on readCookie('cookiename') method
 */ 
function readCookie(name) 
{
	if (name == null)
		return null;
		
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function isChildWindow()
{
	return (window.opener != null);
}

function hasClientLoggedIn()
{
	// if the accountid cookie is present then the client is logged in on this browser
	return (readCookie('accountid') != null);
}

function hasViewCredits()
{
	var paid = readCookie('paid_tracker');
	var promo = readCookie('promo_tracker');
	var gift = readCookie('gift_tracker');
	
	var tcredits = 0;
	if (paid != null) { tcredits += paid; }
	if (promo != null)	{ tcredits += promo; }
	if (gift != null) { tcredits += gift; }
		
	return (tcredits > 0)
}

function parseKeyValuePairsInQueryString(qString)
{
	if (qString == null)
		return null;
		
	if(qString.length <= 1)
		return null;

	var params  = qString.substring(1, qString.length);
	var keyValuePairs = new Array();
	
	for(var i=0; i < params.split("&").length; i++) 
	{
		keyValuePairs[i] = params.split("&")[i];
	}
	
	return keyValuePairs;
}

function getKey(keyValPair)
{
	if (keyValPair == null)
		return null;
		
	return keyValPair.split("=")[0];
}

function getValue(keyValPair)
{
	if (keyValPair == null)
		return null;
		
	return keyValPair.split("=")[1];
}

function hasBeenPurchasedForViewing()
{
	var keyValueArray = parseKeyValuePairsInQueryString(unescape(window.location.search));
	if (keyValueArray == null)
		return false;
		
	var movieid = null;
	for (var i = 0; i < keyValueArray.length; i++)
	{
		if (getKey(keyValueArray[i]) == 'movielib_id')
		{
			movieid = getValue(keyValueArray[i]);
			i = keyValueArray.length;
		}
	}
	
	return (readCookie(movieid) != null);
}

function checkPurchaseWindowAuthentication()
{
	// client has not logged in on this browser
	if ( ! hasClientLoggedIn() ) 
	{
		window.location='../php/notloggedin.php';
	}
	
//	if ( ! hasViewCredits() )
//	{
//		window.location='../php/noviewcredits.php';
//	}
	
	if ( ! isChildWindow() )
	{
		window.location='../php/notchildwindow.php';
	}
}

function checkUserAuthentication()
{
	// client has not logged in on this browser
	if ( ! hasClientLoggedIn() ) 
	{
		window.location='../lib/php/notloggedin.php';
	}
}

function checkMovieAuthentication()
{
	// if the client does not have any movie credits left
//	if ( ! hasViewCredits() )
//	{
//		window.location='../lib/php/noviewcredits.php';
//	}
	
	// if the movie credit view time has expired then we need to use another credit
	if ( ! hasBeenPurchasedForViewing() ) 
	{
		window.location='../lib/php/expiredmoviecredit.php';
	}
}

function authenticateMovieViewPurchaseForm(form)
{
	if ( ! hasViewCredits() )
	{
		setTimeout("window.location='../lib/php/noviewcredits.php'", 0);
		return;
	}

	var movieurl = form.movielib_url.value;
	var movieid = form.movielib_id.value;
	var actionurl = form.action_url.value;
	var url = actionurl + "?movielib_id=" + movieid + "&" + "movielib_url=" + movieurl;
	var nw = window.open(url, 'newwin', 'height=300, width=300');
	nw.moveTo(100,100);
}

/* initAuthentication() - this function is used as a driver to perform all page authentication. 
 *
 * Arguments    : authfuncname - name for specific type of authentication to do
 *
 * Return       : none
 *
 * Dependancies : this function depends on readCookie('cookiename') method
 */
function initAuthentication(authfunction)
{
	if (authfunction == 'userauth')
	{
		checkUserAuthentication();
	}
	
	if (authfunction == 'movieauth')
	{
		checkMovieAuthentication();
	}
	
	if (authfunction == 'purchasewindowauth')
	{
		checkPurchaseWindowAuthentication();
	}
}
