<?php
if ($_SESSION['FirstName'] == "") {
	echo 'You are not authorized to view this page. <br><br>You may <a href="index.php">log in here.</a>';
	exit;
	}


//require_once ('mysql_connect.php');
$action          = $_POST["a"];

	//echo 'Action = ' . $action; (for testing)
//require_once ('config.inc');

if ($action == "add") {
			
			$query = "Insert into SEO (metaDescr) Values ('" . mysql_real_escape_string($_POST['metaDescr']) . "' )";

			if (!mysql_query($query)){
					//echo $query;
  					die('Error: ' . mysql_error());
 			 } else {
				echo "<div align=center><font color=red>SEO record added successfully. Please fill in the remaining fields to optimize your search engine settings.</font></div>";
				
			}
		}

if ($action == "update") {
$checkbox = $_POST['chkdelete'];
$admin = $_POST['chkadmin'];
$active = $_POST['chkactive'];


	
	
		
		$query = "UPDATE SEO SET metaDescr= '" . mysql_real_escape_string($_POST['metaDescr']) . "',  metaKeywords = '" . mysql_real_escape_string($_POST['metaKeywords']) . "', city= '" . mysql_real_escape_string($_POST['city']) ."', state= '". mysql_real_escape_string($_POST['state']) . "'";

		
		
		$query = $query . " WHERE recordKey = '" . $_POST['key'] ."'" ;
		//echo $query;
		mysql_query($query) or die(mysql_error());   //execute query

		//confirmation message that update worked...
		echo "<font color=red>Data updated successfully.</font>";

		
	}	//end of update query
	
	
			$query = "SELECT * FROM SEO";

            
	

		$rt=mysql_query($query);          // query executed 
		echo mysql_error();                    // if any error is there that will be printed to the screen



echo '<div align="center"><table border=0 cellpadding=0 cellspacing=8 width="600">';

while($nt=mysql_fetch_array($rt)){
	echo '<tr><td><form action="' . $_SERVER['PHP_SELF'] . '" method="post"><input type="hidden" name="a" value="update" /><input type="hidden" name="key" value="'.$nt[recordKey].'" />';
    
    echo '<tr><td valign="top"><div align=left>Description:<td valign="top"><div align=left><textarea cols=80, rows=3 name="metaDescr" id="metaDescr">'. $nt[metaDescr] . '</textarea><br>Characters: <span class="charcount" id="countDescr">0</span><br><i><font color="red"><div align="justify">Enter a short paragraph about your business. This is a brief description that search engines may provide to visitors to give them information about your website.</i></font>';
    
    echo '<tr><td valign="top"><div align=left>Keywords:<td valign=top><div align=left><textarea cols=80, rows=3 name="metaKeywords" id="metaKeywords">' . $nt[metaKeywords] . '</textarea><br><span>Characters: <span class="charcount" id="countKeyChars">0</span></span>&nbsp;&nbsp;&nbsp;&nbsp;<span>Keyword count: <span class="keywordcount" id="countKeywords">0</span></span><br><i><font color="red"><div align="justify">Enter up to 100 words, separated by commas. These are words that visitors will search for on the internet to find your website.</i></font>';

	echo '<tr><td><div align=left>City:<td valign=top><div align=left><input type="text" id="city" name="city" style="width:100%;max-width:100%;" value="' . $nt[city] . '"><br><span>Characters: <span class="charcount" id="countCity">0</span>';  

	echo '<tr><td><div align=left>State:<td valign=top><div align=left><input type="text" id="state" name="state" style="width:100%;max-width:100%;" value="' . $nt[state] . '"><br><span>Characters: <span class="charcount" id="countState">0</span><br><i><font color="red"><div align="justify">The city and state entered here will show up in the title of your web pages to improve search results in your area.</i></font>';

    function getWebsiteName() { $query = "Select title from storePrefs"; $rt=mysql_query($query); while($nt=mysql_fetch_array($rt)) { $seotitle = $nt[title]; return $seotitle; } }

    // echo '<tr><td><div valign=left>Dynamic SEO:<td valign=top><div align=left><input type="text" id="dynamicSEO" name="dynamicSEO" size="75" value="' . getWebsiteName($seotitle) . " - " . $nt[city] . ", " . $nt[state] . '" readonly><br><span>Characters: <span id="countDynSEO" name="countDynSEO">0</span></span>';
    echo '<tr><td><div valign=left>Page Title:<td valign=top><div align=left>';
    echo '<input type=hidden id=sitename value="'.getWebsiteName().'" />';
    echo '<label id="dynamicSEO"></label><br><span>Characters: <span id="countDynSEO" class="charcount">0</span></span>';

    ?>
        <tr>
            <td>Preview: </td>
            <td>
                <div class="seo-preview">
                    <h3><a class="page-title" href="#"><?php echo getWebsiteName(); ?></a></h3>
                    <div class="url"><?php echo "http://$_SERVER[HTTP_HOST]"; ?>/</div>
                    <div class="description"><?php echo $nt['metaDescr']; ?></div>
                </div>
            </td>
        </tr>
    <?php

	echo '<tr><td><td valign=top><div align=left><input type="submit" id="save-button" name="save" value="Save"></form><tr><td colspan=2>';
		
}

echo '</table><br><br>';


	
if (mysql_num_rows($rt) == 0) {		//only show the ADD button if no record exists because only one record is used....

	echo '    <form action="' . $_SERVER['PHP_SELF'] . '" method="post"><input type="hidden" name="a" value="add" />Add new meta: (descr) <br><input type="text" name="metaDescr" size=50>

		<input type="submit" id="save-button" name="save" value="Save">
		</form>
    <br>';
}
echo '
		</div></table></div>';
	

	

?>