/*
Author:  	Ryan Chesney 			ryanchesney @ yahoo . com
Note:		This is not a hardened solution and is actually easy to bypass.  It is simply
			intended to deter average visitors that have javascript enabled from accessing this page.
!!!!!!!!!!!!Please leave name and email for credit.
*/

var sPassword = '2US9VA';		//Change this to whatever you want your password to be.
var iAttemptsMade = 0;			//Number of attempts made by the visitor.
var sURL = 'http://www.secondcavalry.org' //URL to send the visitor to if the password is not correct
var aPromptText = new Array(1) 	//Password prompt array
aPromptText[0] = 'Please enter a valid password and click "Ok" to access this page.';
aPromptText[1] = 'The password you provided was not correct. Please try again.';
var cookieName = 'access';		//Name of the cookie
var cookieValue = 'true';		//Value of the cookie
var cookieExpires 				//Defaults to end of current session.
var cookiePath					//Defaults to path of calling document
var cookieDomain				//Defaults to domain of calling document
var cookieSecure				//Whether the cookie requires secure transmission


//Check for the cookie.  If it's not found, determine the action
if (fcnGetCookie(cookieName) != 'true')
	fcnDetermineAction()

function fcnPasswordPrompt(){
	//this function prompts the user to enter the site password.
	var sPassValue = prompt(aPromptText[0]);
	
	if(!fcnComparePassword(sPassValue)){
		//increment number of attempts and prompt 
		iAttemptsMade++;
		fcnDetermineAction()
	} else {
		//Set a cookie so users do not need to enter a password every
		//time they try to view a password protected page.
		fcnSetCookie()
	}	
}

function fcnComparePassword(aPassword){
	//this function compares the value the user submitted to the designated password.
	if (aPassword == sPassword)
		return true;
	else
		return false;
}
	
function fcnDetermineAction(){
	//this function determines what action to take
	if (iAttemptsMade == 0){
		fcnPasswordPrompt()	//prompt for password
	}else if(iAttemptsMade == 1){
		alert(aPromptText[iAttemptsMade]);  //alert user they made 1 attempt
		fcnPasswordPrompt()	//prompt for password again
	}else if (iAttemptsMade > 1){
		alert('You made ' + iAttemptsMade + ' attempts to enter the valid password. \n You will now be transferred to another page.');
		location.href = sURL; //send visitor to some other page
	}
}

function fcnSetCookie(){
	//this functioon sets the cookie	
	var cooky = cookieName + "=" + escape(cookieValue) +
    ((cookieExpires) ? "; expires=" + cookieExpires.toGMTString() : "") +
    ((cookiePath) ? "; path=" + cookiePath : "") +
    ((cookieDomain) ? "; domain=" + cookieDomain : "") +
    ((cookieSecure) ? "; secure" : "");
	document.cookie = cooky	//write cookie
}

function fcnGetCookie(name) {
	//this function gets the cookie
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function fcnLogOut() {
  if (fcnGetCookie(cookieName)) {
    document.cookie = cookieName + "=" +
    ((cookiePath) ? "; path=" + cookiePath : "") +
    ((cookieDomain) ? "; domain=" + cookieDomain : "") +
    "; expires=Thu, 01-Jan-76 00:00:01 GMT";
	
	location.href = sURL; //send visitor to some other page
  }
}

