<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.

Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/



	// ========================================================================================
	// main guestbook script
	// ========================================================================================
	
	
	$query = "select * from `".$Settings['DBPrefix']."gb-Settings`";
	$result = mysql_query($query) or die (mysql_error());
	while ($row = mysql_fetch_array($result)) {
		$gbSettings[$row['Name']] = $row['Value'];
	}
	
	
	if ($gbSettings['EntriesPerPage'] < 1) {
		$gbSettings['EntriesPerPage'] = 10;
	}
	
	
		
	if ($Arguments2 != 'sign') {
		
		if ($gbSettings['PageTitle'] != '') {
			//$PageTitle = '<div align="'.$AlignmentCodes[$gbSettings['TitleAlignment']].'">'.$gbSettings['PageTitle'].'</div>';
			$BrowserTitle = $gbSettings['PageTitle'];
			$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > '.$gbSettings['PageTitle'];
		}
		else {		
			//$PageTitle = '<div align="'.$AlignmentCodes[$gbSettings['TitleAlignment']].'">'.$MsgText[6][4].'</div>';
			$gbSettings['PageTitle'] = $MsgText[6][4];
			$BrowserTitle = $MsgText[6][4];
			$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > '.$MsgText[6][4];
		}
		
		
				
		if ($gbSettings['TitleImage'] != '') {
			$PageTitle = '<div align="'.$AlignmentCodes[$gbSettings['TitleAlignment']].'"><img src="'.$BaseURL.$gbSettings['TitleImage'].'" alt="'.$gbSettings['PageTitle'].'" border="0"></div>';	
		}
		else {
			$PageTitle = '<div align="'.$AlignmentCodes[$gbSettings['TitleAlignment']].'">'.$gbSettings['PageTitle'].'</div>';
		}
			
		
		if ($gbSettings['TopText'] != '') {
			$PageContents .= '<div align="'.$AlignmentCodes[$gbSettings['TopTextAlignment']].'">'.$gbSettings['TopText'].'</div><br>';
		}
		
		
		$PageContents .= '<br>'.$MsgText[6][5].'<br><br>';
		
		
		// figure out how many messages there are
		$query = "select count(RecordNumber) as MessagesCount from `".$Settings['DBPrefix']."gb-Messages` where Approved=1";
		$result = mysql_query($query) or die (mysql_error());
		$row = mysql_fetch_array($result);
		$GBMessagesCount = $row['MessagesCount'];
		
		$LastPage = ceil($GBMessagesCount/$gbSettings['EntriesPerPage']);
		
		$Arguments2 = intval($Arguments2);
		if ($Arguments2 == '' || $Arguments2 == 0) {
			$GBPage = 1;
		}
		else if ($Arguments2 > $LastPage) {
			$GBPage = $LastPage;
		}
		else {
			$GBPage = $Arguments2;
		}
		
		$GBMessagesStart = (($GBPage-1)*$gbSettings['EntriesPerPage']);
		
		
		if ($GBMessagesCount > $gbSettings['EntriesPerPage']) {
			if ($GBPage > 1) {
				$PreviousPageLink = '<a href="'.$MainURL.'/guestbook/'.($GBPage-1).'.html">&lt; '.$MsgText[6][18].'</a>';
			}
			else {
				$PreviousPageLink = '&nbsp;';
			}
			
			if ($GBPage < $LastPage) {
				$NextPageLink = '<a href="'.$MainURL.'/guestbook/'.($GBPage+1).'.html">'.$MsgText[6][17].' &gt;</a>';
			}
			else {
				$NextPageLink = '&nbsp;';
			}
			
			$PageLinks = '<div align="center"><table width="400" border="0" cellpadding="0" cellspacing="0"><tr>
				<td width="45%" align="left"><font face="'.$FontFace.'" size="'.$FontSize.'"><b>'.$PreviousPageLink.'</b></font></td>
				<td width="10%" align="center"><font face="'.$FontFace.'" size="'.$FontSize.'"><b>'.$GBPage.'</b></font></td>
				<td width="45%" align="right"><font face="'.$FontFace.'" size="'.$FontSize.'"><b>'.$NextPageLink.'</b></font></td>
				</tr></table></div><br><br>';
				
		}
		
		
		if ($gbSettings['SortDirection'] == 'desc' || $gbSettings['SortDirection'] == '') {
			$SortDirection = 'desc';
		}
		else {
			$SortDirection = 'asc';
		}
		
		$query = "select * from `".$Settings['DBPrefix']."gb-Messages` where Approved=1 order by DateCode ".$SortDirection." limit ".$GBMessagesStart.",".$gbSettings['EntriesPerPage'];
		$result = mysql_query($query) or die (mysql_error());
		while ($row = mysql_fetch_array($result)) {
		
			$row['Message'] = str_replace('<','&lt;',$row['Message']);
			$row['Message'] = str_replace("\n",'<br>',$row['Message']);
			
			$PageContents .= '<br><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td bgcolor="'.$Colors['Text'].'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" alt="" border="0"></td></tr></table><br>'.$row['Message'].'<br><b><font size="'.($FontSize-1).'">'.$MsgText[6][6].' '.$row['Name'].' '.$MsgText[6][7].' '.date('F j, Y @ g:ia',$row['DateCode']).'</font></b><br>';		
		
		}
		
		$A6 = intval(GetTimestamp()/60/60/24);
		$A7 = substr($Settings['lks'],10,5);
		
		$PageContents .= '<br><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td bgcolor="'.$Colors['Text'].'"><img src="'.$ImagesURL.'/admin/spacer.gif" width="1" height="1" alt="" border="0"></td></tr></table>';
		
		$JSCode[1] = 'document.forms["SubmitForm"].elements["A4"].value = (parseInt(document.forms["SubmitForm"].elements["A2"].value)*parseInt(document.forms["SubmitForm"].elements["A3"].value)); document.forms["SubmitForm"].elements["A5"].value = (parseInt(document.forms["SubmitForm"].elements["A2"].value)+parseInt(document.forms["SubmitForm"].elements["A3"].value));';
		$JSCode[2] = 'document.forms["SubmitForm"].elements["A4"].value = (parseInt(document.forms["SubmitForm"].elements["A2"].value)-parseInt(document.forms["SubmitForm"].elements["A3"].value)); document.forms["SubmitForm"].elements["A5"].value = (parseInt(document.forms["SubmitForm"].elements["A2"].value)+(parseInt(document.forms["SubmitForm"].elements["A3"].value)*parseInt(document.forms["SubmitForm"].elements["A2"].value)));';
		$JSCode[3] = 'document.forms["SubmitForm"].elements["A4"].value = (parseInt(document.forms["SubmitForm"].elements["A2"].value)*parseInt(document.forms["SubmitForm"].elements["A2"].value)*parseInt(document.forms["SubmitForm"].elements["A3"].value)); document.forms["SubmitForm"].elements["A5"].value = (parseInt(document.forms["SubmitForm"].elements["A3"].value)*parseInt(document.forms["SubmitForm"].elements["A3"].value));';
		$JSCode[4] = 'document.forms["SubmitForm"].elements["A4"].value = (Math.pow(parseInt(document.forms["SubmitForm"].elements["A2"].value),2)); document.forms["SubmitForm"].elements["A5"].value = (Math.floor(Math.PI*parseInt(document.forms["SubmitForm"].elements["A2"].value)));';
		$JSCode[5] = 'document.forms["SubmitForm"].elements["A4"].value = ((parseInt(document.forms["SubmitForm"].elements["A2"].value)*parseInt(document.forms["SubmitForm"].elements["A3"].value))-parseInt(document.forms["SubmitForm"].elements["A2"].value)); document.forms["SubmitForm"].elements["A5"].value = ((parseInt(document.forms["SubmitForm"].elements["A2"].value)*47)+parseInt(document.forms["SubmitForm"].elements["A3"].value));';
		
		$A1 = rand(1,5); $A2 = rand(10,99);	$A3 = rand(100,999);
		
		$PageContents .= '<br><br>'.$PageLinks.'<br><br><a name="sign"></a><b>'.$MsgText[6][8].':</b><br><form name="SubmitForm" action="'.$MainURL.'/guestbook/sign" method="post"><input type="hidden" name="A1" value="'.$A1.'"><input type="hidden" name="A2" value="'.$A2.'"><input type="hidden" name="A3" value="'.$A3.'"><input type="hidden" name="A4" value=""><input type="hidden" name="A5" value=""><input type="hidden" name="A6" value="'.dechex($A6).'"><input type="hidden" name="A7" value="'.$A7.'"><script>'.$JSCode[$A1].'</script><noscript><br><br><b>'.$MsgText[0][633].'</b><br><br></noscript>
			
'.$MsgText[6][9].':<br><input type="text" name="Name" size="25" maxlength="255" style="'.$FormStyle.'">
<br><br>
'.$MsgText[6][10].':<br><textarea name="Message" cols="45" rows="4" wrap="virtual" style="'.$FormStyle.'"></textarea>
<br><br><br>
<input type="submit" value="'.$MsgText[6][11].'" style="'.$FormStyle.'">
</form>';
				
	
	}
	else {
		
		$PageTitle = '<div align="'.$AlignmentCodes[$gbSettings['TitleAlignment']].'">'.$MsgText[6][11].'</div>';
		$BrowserTitle = $MsgText[6][11];
		$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > '.$MsgText[6][11];
		
		
		ReadFormArguments();
		Global $FormArguments;
		
		
		$FormValidated1 = false;
		if ($FormArguments['A1'] == 1) {
			if (($FormArguments['A4'] == ($FormArguments['A2']*$FormArguments['A3'])) && ($FormArguments['A5'] == ($FormArguments['A2']+$FormArguments['A3']))) {
				$FormValidated1 = true;
			}
		}
		else if ($FormArguments['A1'] == 2) {
			if (($FormArguments['A4'] == ($FormArguments['A2']-$FormArguments['A3'])) && ($FormArguments['A5'] == ($FormArguments['A2']+($FormArguments['A3']*$FormArguments['A2'])))) {
				$FormValidated1 = true;
			}
		}
		else if ($FormArguments['A1'] == 3) {
			if (($FormArguments['A4'] == ($FormArguments['A2']*$FormArguments['A2']*$FormArguments['A3'])) && ($FormArguments['A5'] == ($FormArguments['A3']*$FormArguments['A3']))) {
				$FormValidated1 = true;
			}
		}
		else if ($FormArguments['A1'] == 4) {
			if (($FormArguments['A4'] == ($FormArguments['A2']*$FormArguments['A2'])) && ($FormArguments['A5'] == floor(M_PI*$FormArguments['A2']))) {
				$FormValidated1 = true;
			}
		}
		else if ($FormArguments['A1'] == 5) {
			if (($FormArguments['A4'] == ($FormArguments['A2']*$FormArguments['A3'])-$FormArguments['A2']) && ($FormArguments['A5'] == ($FormArguments['A2']*47)+$FormArguments['A3'])) {
				$FormValidated1 = true;
			}
		}
		
		$FormValidated2 = false;
		if (hexdec($FormArguments['A6']) <= (intval(GetTimestamp()/60/60/24)+2) && $FormArguments['A7'] == substr($Settings['lks'],10,5)) {
			$FormValidated2 = true;
		}
	
		
		if (count($FormArguments) > 0 && $FormArguments['Message'] != '' && $FormValidated1 == true && $FormValidated2 == true) {
		
			$FormArguments['Name'] = $FormArguments['Name'];
			$FormArguments['Message'] = $FormArguments['Message'];
			
			
			if ($FormArguments['Name'] == '') {
				$FormArguments['Name'] = 'Anonymous';
			}
		
			
			if ($gbSettings['Notify'] == 'y') {
			
				if ($gbSettings['NotifyEmail'] != '') {
					$NotifyEmail = $gbSettings['NotifyEmail'];
				}
				else {
					$NotifyEmail = $Settings['ContactEmail'];
				}
				
				
				if ($gbSettings['RequireApproval'] == 'y') {
					$ApprovalPart = '  '.$MsgText[6][33];
				}
				
				
				$Message = "\n".$MsgText[6][34].$ApprovalPart."\n\n\n".$MsgText[0][142].','."\n".$Settings['SiteName']."\n".$IndexURL;
				mail($NotifyEmail, $MsgText[6][35], $Message, 'From: "'.$Settings['SiteName'].'" <'.$Settings['ContactEmail'].'>','-f'.$Settings['ContactEmail']);
				
			
			}
			
			
			if ($gbSettings['RequireApproval'] != 'y') {
			
				$query = "insert into `".$Settings['DBPrefix']."gb-Messages` (Name,Message,DateCode,Approved) values ('".$FormArguments['Name']."','".$FormArguments['Message']."','".GetTimestamp()."',1)";
				$result = mysql_query($query) or die (mysql_error());
			
			
				header('Location: '.$MainURL.'/guestbook.html');
				
			}
			else {
			
				$query = "insert into `".$Settings['DBPrefix']."gb-Messages` (Name,Message,DateCode,Approved) values ('".$FormArguments['Name']."','".$FormArguments['Message']."','".GetTimestamp()."',0)";
				$result = mysql_query($query) or die (mysql_error());
				
				
				$PageContents = '<br><br>'.$MsgText[6][36].'<br><br><br><a href="'.$MainURL.'/guestbook.html">'.$MsgText[6][37].'</a>';
			
			}
				
		
		}
		else {
			$PageContents = $MsgText[0][98];
		}
	
	}
				
	
	// ========================================================================================




?>
