//BOUNCE SWITCH
//Remember to set bounceSwitch = 1 on uploaded copy after code changes and testing
var bounceSwitch = 1; // 1 = bounce out of invalid entry pages; 0 = do not bounce for test usage only
var thisHomePage = 0;

//BANNED USER ARRAY
$bannedUsername = new Array();
$bannedStatus = new Array();

//LIMITED STATUS INDICATIONS ($bannedStatus[#]=
// 0 = listed as reminder of future banning but not yet banned
// 1  = username banned
// 2 = username  banned with review required message
// 3 = username allowed but download banned on this temporary trial version

$bannedUsername[0] = "ian.kampel";
$bannedStatus[0] = 1;
$bannedUsername[1] = "beta-test1";
$bannedStatus[1] = 2;
$bannedUsername[2] = "stuart-macbride"; //Celebrity author review till about 31aug07
$bannedStatus[2] = 2;
$bannedUsername[3] = "available"; //Synergise & book uthor review till about 31aug07
$bannedStatus[3] = 0;
$bannedUsername[4] = "maria-nolan"; //Synergise & book uthor review never responded so excluded 10OCT07
$bannedStatus[4] = 2;
$bannedUsername[5] = "available"; //Synergise & book uthor review till about 31aug07
$bannedStatus[5] = 0;
$bannedUsername[6] = "wendy-harris"; //Synergise & book uthor review till about 31aug07
$bannedStatus[6] = 3;
$bannedUsername[7] = "scott-mariani"; //Synergise & book uthor review till about 31aug07
$bannedStatus[7] = 2;

//PASSWORD VARIABLES
var encryptor=" abcdefghijklmnopqrstuvwxyz0123456789.-_";
var vowels="aeiou";//5 available
var available_consonants="bcdfghklmnprstvwy";//17 available

//LOGON STATUS
var $logonStatus = "LOGGED OFF";

//TEST FUNCTION
function testNow()
{
alert("Hi test now");
}

//GET USERNAME & KEY
var $username = "";
var $Key = "";
var $keyValid = 0;

//ON ANY PAGE ENTRY - other than home page - THIS GETS $username & $Key FROM QUERY STRING
//the home page script is the same but with a change at the end, and is called getPublicPageKey()
//the change needed to avoid bouncing out of page when home page!
function getUsernameKey()
{
//alert("Hi from getUsernameKey");
$nowBannedFlag = 0;

var handyString = window.location.search;
//alert("handyString=" + handyString);

var startOfSource = handyString.indexOf('user=');

//if it is there find the end of it
if (startOfSource != -1) 
	{
		//Determine username
		var endOfSource = handyString.indexOf("&key", startOfSource);
		$username = handyString.substring(startOfSource+5, endOfSource);
		//Determine password
//		alert("$username = " + $username);
		//Determine key
		startOfSource = handyString.indexOf('key=');
		endOfSource = handyString.length;
		$Key =handyString.substring(startOfSource+4, endOfSource);
//		alert("$Key = " + $Key);
		//VALIDATE KEY FROM HOME PAGE
		$calcPassword = username2password($username);
		$calcKey = username2password($calcPassword);
		if ($Key == $calcKey)
			{
			$keyValid = 1;
			//alert("Key valid");
			//CHECK IF USERNAME BANNED
			$bannedLgthMax = $bannedUsername.length;
			for (i =0; i < $bannedLgthMax; i++)
			{
				if ( ($bannedUsername[i] == $username) && ($bannedStatus[i] == 1) )
				{
				$bannedMessage = "Sorry, " + $username + ", you are not longer entitled to enter this site!"
				$nowBannedFlag = 1;
				}
				if ( ($bannedUsername[i] == $username) && ($bannedStatus[i] == 2) )
				{
				$bannedMessage = "Sorry, " + $username + ", but you need to let Synergise know when your agreed review of P4 will be ready in order to reinstate your access to P4."
				$nowBannedFlag = 1;
				}
			if ($nowBannedFlag == 1)
				{				
					alert($bannedMessage);
					$logonStatus = "LOGGED OFF";
					window.location = "index.htm?user=&key=";
					break; 
					}
				else
					{
					$logonStatus = "LOGGED ON";
					
					}
			}		
			}
		else
		{
		// INVALID KEY so bounce to home page if not home page already - local file index pages refresh to P4 home page
		//so directory level for this does not matter.
		$keyValid = 0;
//		alert("thisHomePage= " + thisHomePage);
			if ((bounceSwitch == 1) && (thisHomePage != 1))
			{
			alert("You are being logged off because of invalid user data in page URL.");
			window.location = "bounce2home.htm?user=&key=";
			}
		}		
	}
else
{
//alert("Hi from no QS loop");
window.location = "index.htm?user=&key=";
}
}

//ON HOME PAGE ENTRY THIS GETS $username & $Key FROM QUERY STRING
//the ordinary page script is the same but without final 'else' at the end, and is called getUsernameKey()
//the change needed to avoid bouncing out of page when home page!
function getPublicPageKey()
{
thisHomePage = 1;
getUsernameKey();
}

//Add blank user and key to entered public pages without them
//or calls normal getPublicPageKey() if present
//allowing this to be a gateway page without the query string present!
function addBlankUserKey($target)
{
var handyString = window.location.search;
var startOfSource = handyString.indexOf('user=');
if (startOfSource == -1) //ie not there
	{$destination = $target + "?user=&key=";
	//alert("$destination=" + $destination);
	window.location = $destination;
	}
else
	{
	getPublicPageKey();
	}	
}


//WRITE LOGON STATUS ON HOME PAGE
function showLogonStatus()
{
document.write($logonStatus);
}

//JAVASCRIPT OPEN PAGE FUNCTION FOR GENERAL USE
//adding query string
function OpenPage($target)
{
$destination = $target + "?user=" + $username + "&key=" + $Key;
//alert("$destination=" + $destination);
//window.location = $destination;
window.open($destination, "_parent");

}

//JAVASCRIPT OPEN NEW WINDOW PAGE FUNCTION FOR GENERAL USE
//adding query string
function OpenNewWindowPage($target)
{
$destination = $target + "?user=" + $username + "&key=" + $Key;
//alert("$destination=" + $destination);
window.open($destination, "tutorial-window");
}

//JAVASCRIPT OPEN IN DISPLAY IFRAME
//adding query string
function OpenDisplay($target)
{
$destination = $target + "?user=" + $username + "&key=" + $Key;
//alert(" Display destination=" + $destination);
window.open($destination, "display");
}

//REGISTER FUNCTION - called from payment/thanks4payment.htm
function register()
{
flag = 0;
$AccessCode = form1.AccessCode.value;
//alert($AccessCode);
if ($AccessCode == "apples") //code for a single user
	{
	window.location = "../p4/register-3grttrh76JK6ydfB4456855465tgg8gk5ign5nfnfdkgmrenvbdwgw00Hjfdgsfdg.htm";
	flag = flag+1;
	}
if ($AccessCode == "multiple") //code for multiple user application
	{
	window.location = "../p4/multiple-response.htm";
	flag = flag+1;
	}	
//ELSE show the following alert if not already sent to another page!
if (flag == 0)
	{alert("Incorrect Registration Code!");}
}

//GET PASSWORD FUNCTION called from register.htm
function getPassword()
{
$entries = 0;
//$username = document.getElementById("username").value;
$firstname = document.getElementById("firstname").value;
if ($firstname != "") {$entries++} //$entries=1 if present
$lastname = document.getElementById("lastname").value;
if ($lastname != "") {$entries++} //$entries=1 if present
$username = $firstname + "-" + $lastname;
$username =$username.toLowerCase();
$password = username2password($username);
$emailAddress = document.getElementById("emailAddress").value;
if ($emailAddress != "") {$entries++} //$entries=2 if present
$transId = document.getElementById("transId").value;
if ($transId != "") {$entries++} //$entries=3 if present
$PurchasersName = document.getElementById("PurchasersName").value;
if ($PurchasersName != "") {$entries++} //$entries=4 if present
$PayersName = document.getElementById("PayersName").value;
if ($PayersName != "") {$entries++} //$entries=5 if present
$FoundP4By = document.getElementById("FoundP4By").value;
if ($FoundP4By != "Select one...") {$entries++} //$entries=6 if present
//alert("FoundP4By=" + $FoundP4By);	
//alert("$entries=" + $entries);	
if ($entries == 7)
	{
	$message = "IMPORTANT: Write down your username '" + $username + "' and password of '" + $password + "' NOW for use with the product - or your payment will have been wasted! After clicking 'OK' you will see an email open covering your registration details. Make sure you SEND it to yourself - AND US!";
	alert($message);
	//Send details in an email

	$buildloc = "mailto:" + $emailAddress + "?cc=info40@synergise.com&subject="  + $username + ": Synergise%20P4%20Registration%20Details&body=Purchaser\'s%20Name=%20" + $PurchasersName + "%0A%0APayer\'s%20Name=%20" + $PayersName + "%0A%0AUsername%20=%20" + $username + "%0A%0APassword%20=%20" + $password + "%0A%0APayPal%20Transaction%20ID%20=%20" + $transId + "%0A%0AAfter%20sending%20this%20email%20to%20yourself%20you%20have%20a%20permanent%20record%20of%20these%20important%20details%20you%20need%20to%20Logon%20from%20the%20P4%20Home%20Page%20(http://www.synergise.com/p4).%20A%20copy%20of%20this%20email%20is%20also%20being%20sent%20to%20Synergise%20to%20register%20your%20product.%0A%0AMarket%20Research%20Info%20for%20Synergise:%20I%20found%20P4%20via%20" + $FoundP4By + ". If%20further%20details%20can%20clarify%20this%20then%20please%20add%20them%20here:";
	window.location = $buildloc;
	window.location = "index.htm";
	}
	else
	{
	alert("Not all the entry details are present!");
	}
}

// USED above by function getUserEntry()
function username2password(entryUserID)
{
var key=1; //changing this changes all the passwords generated by affecting offset
var offset=entryUserID.length //used in encryption
//convert User Name entry to lower-case
entryUserID=entryUserID.toLowerCase();
//letter1 consonant
var posn = (offset+key)%17;
//alert("posn= "+posn);
latest=available_consonants.charAt(posn);
output=latest;
//letter 2 vowel
inputChar = entryUserID.charAt(0);//input letter  
inputVal = encryptor.indexOf(inputChar);//val for input letter
posn = (offset+key+inputVal)%5;//vowel modulus
latest=vowels.charAt(posn);//vowel for input letter
output=output+latest;
//alert("output2= " + output);
//letter 3 consonant
inputChar = entryUserID.charAt(1);//input letter
inputVal = encryptor.indexOf(inputChar);//val for input letter
posn = (offset+key+inputVal+1)%17;
latest=available_consonants.charAt(posn);
output=output+latest;
//alert("output3= " + output);
//letter 4 consonant
posn=(entryUserID.length)-2;
inputChar = entryUserID.charAt(posn);//input letter
inputVal = encryptor.indexOf(inputChar);//val for input letter
posn = (offset+key+inputVal+2)%17;
latest=available_consonants.charAt(posn);
output=output+latest;
//alert("output4= " + output);
//letter 5 vowel
posn=(entryUserID.length)-1;
inputChar = entryUserID.charAt(posn);//input letter
inputVal = encryptor.indexOf(inputChar);//val for input letter
posn = (offset+key+inputVal+3)%5;
latest=vowels.charAt(posn)
output=output+latest;
//alert("output5= " + output);
//letter 6 consonant
posn=entryUserID.length;
inputChar = entryUserID.charAt(posn);//input letter
inputVal = encryptor.indexOf(inputChar);//val for input letter
posn = (offset+key+inputVal+4)%17;
latest=available_consonants.charAt(posn);
output=output+latest;
//alert("output=" + entryUserID);
return output;
//END calculate password
}

//VALIDATE PASSWORD - called from logon.htm
function validatePassword()
{
$username = document.getElementById("username").value;
//alert("$username=" + $username);
$calcPassword = username2password($username);
$UserPassword = document.getElementById("UserPassword").value;
//alert("$UserPassword=" + $UserPassword);
if ($UserPassword == $calcPassword)
	{
	//alert("Password correct");
	$key = username2password($calcPassword);
	//alert("$key=" + $key);
	window.location = "welcome.htm?user=" + $username + "&key=" + $key;
	}
	else
	{
	if ($UserPassword == "apples")
		{
		alert("You entered the Registration Code instead of your password. Please try again!");
		}
	else
		{
		alert("Username/Password wrong!");
		}
	}
}

//HOME PAGE LOGON/ENTER DETERMINATION
//CALLED FROM EnterSite button
//If query string values for user and key present then enter to welcome.php otherwise open logon.htm
//get query string values if present
function logonOrEnter()
{
if ($keyValid == 1) 
	{
	window.location = "welcome.htm?user=" + $username + "&key=" + $Key;
	}
	else
	{
	//Open Logon page since no query string values
	OpenPage('logon.htm');
	}
}

//OPEN RELEVANT BUY PAGE called from home page
function buyProduct($targetPage)
{
	window.location = $targetPage;
//window.open($targetPage, 'buy', 'scrollbars=1,status=1,location=0');
}

// TEST FUNCTION ONLY to open thanks page
function openRegister()
{
window.open('register.htm','buy', 'scrollbars=1,status=1,location=0');
}

//WRITE COPYRIGHT STATEMENT AT FOOT OF HOME PAGE PROVIDED bounceSwitch = 1
//as visual check bounce is switched on!
function writeCopyright()
{
$copyright = '<p align="center"><font color="#993366" size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>&copy;  2007-2008 Synergise, all rights reserved for all information on this website</strong></font></p>';
if (bounceSwitch == 1)
	{document.write($copyright)}
}

//WRITE TUTORIAL MENU
//called by all tutorial pages
function TutorialMenu()
{
$P4menu = '<a href="lesson1.htm"><strong>Lesson 1</strong></a> | <strong> <font color="#993366">Lesson 2:</font></strong> <a href="lesson2-novelist.htm"><strong>Novelist</strong></a> / <a href="lesson2-personal.htm"><strong>Personal</strong></a> / <a href="lesson2-business.htm"> <strong>Business</strong></a> | <a href="lesson3.htm"> <strong>Lesson 3</strong></a>  | <a href="lesson4.htm"> <strong>Lesson 4</strong></a> | <a href="lesson4answers.htm"> <strong>Lesson 4A</strong></a> | <a href="lesson5.htm"> <strong>Lesson 5</strong></a> | <a href="lesson6.htm"> <strong>Lesson 6</strong></a>  | <a href="lesson7.htm"> <strong>Lesson 7</strong></a>  | <a href="lesson7answers.htm"> <strong>Lesson 7A</strong></a> | <a href="lesson8.htm"> <strong>Lesson 8</strong> </a> | <a href="lesson8answers.htm"> <strong>Lesson 8A</strong> </a> | <a href="lesson9.htm"> <strong>Lesson 9</strong></a>  | <strong><font color="#993366">Lesson 10:</font></strong> <a href="lesson10-novelist.htm"><strong>Novelist</strong></a> / <a href="lesson10-personal.htm"><strong>Personal</strong></a> / <a href="lesson10-business.htm"> <strong>Business</strong></a> | <a href="lesson11.htm"> <strong>Lesson 11</strong></a>';
document.write($P4menu);
}

//CHECK IF DOWNLOAD BANNED FOR USERNAME
function checkDownloadStatus()
{
		$bannedLgthMax = $bannedUsername.length;
		//default message if all ok will be:
		$downloadMessage = '<a href="../onlinedownload/download-update.htm" target="_blank"><strong>Download Update page</strong></a>';
		//but now check to see if user banned to download
		for (i =0; i < $bannedLgthMax; i++)
			{
				if ($bannedUsername[i] == $username)
				{//user found on list, so
					if ($bannedStatus[i] >0)
						{//then change message because banned
						$downloadMessage = 'Download Update Page<br><font color="#FF0000"><strong>not available in this trial version<strong></font>';
						}	
//				break; //do not look further in list since username already found
				}
			}
//	alert($downloadMessage);
	document.write($downloadMessage);
	
}

