<?php

require '../2_Projects/FileOperations.php';
$con = mysql_connect($website,$userid,$pw);
if (!$con) {
	die('Could not connect: ' . mysql_error());
}

$curDB = "../CurrentlyOpenedDBName.tmp";
if(file_exists ($curDB)){
	$f = fopen($curDB, "r");
	$dbase   = trim(fgets($f)); // linked database name
	fclose($f);
	mysql_select_db($dbase, $con);
	//echo "Connected to " . $dbase . "<br/>";
}else{
	//No database currently open
	return;
}
mysql_query("SET foreign_key_checks = 0",$con);
echo ("<head><link rel='stylesheet' type='text/css' href='../CSS/Font.css'></head>");
if($debug)echo ("<script>  document.write(document.URL); </script>");
if($_GET[source_id] != NULL)$source_id = $_GET[source_id];

if($_GET[act] == "yes"){
	if(isset($_POST[NotUpdateTable])){
	}
	if(isset($_POST[UpdateTable])){
		if($source_id == "add"){
			$res = mysql_query("SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = '{$dbase}' AND TABLE_NAME = 'Source'",$con);
			mysql_query("INSERT INTO Source(name) VALUES('{$_POST[nameField]}')",$con);
//			$res = mysql_query("SELECT LAST_INSERT_ID()",$con);
			while($row = mysql_fetch_array($res))$source_id = $row[0];
			mysql_query("INSERT INTO Static(attribute,value,source_id) VALUES('name','{$_POST[nameField]}',{$source_id})",$con);
		}else{
			mysql_query("UPDATE Source SET name='{$_POST[nameField]}' WHERE source_id={$source_id}",$con);
			$res = mysql_query("SELECT COUNT(attribute) FROM Static WHERE source_id = {$source_id} AND attribute = 'name'",$con);
			while($row = mysql_fetch_array($res))$Kount = $row[0];
			if($Kount > 0){
				mysql_query("DELETE FROM Static WHERE source_id = {$source_id} AND attribute = 'name'",$con);
			}
			mysql_query("INSERT INTO Static(attribute,value,source_id) VALUES('name','{$_POST[nameField]}',{$source_id})",$con);
		}
	}
		echo ("<a id='#Reload1' href='RightTop.php' target='TopRightFrame'></a> ");
		echo ("<a id='#Reload2' href='Source.php?source_id={$source_id}' target='SourceFrame'></a> ");
		echo ("<a id='#Reload3' href='Static.php?source_id={$source_id}' target='StaticFrame'></a> ");
		echo ("<a id='#Reload4' href='Dynamic.php?source_id={$source_id}' target='DynamicFrame'></a> ");
		echo ("<a id='#Reload5' href='../Common/BlankPage.html' target='WorkingFrame'></a> ");
		echo("<script>document.getElementById('#Reload1').click();</script>");
		echo("<script>document.getElementById('#Reload2').click();</script>");
		echo("<script>document.getElementById('#Reload3').click();</script>");
		echo("<script>document.getElementById('#Reload4').click();</script>");
		echo("<script>document.getElementById('#Reload5').click();</script>");
}

if($source_id != "add"){
	$res = mysql_query("SELECT name FROM Source WHERE source_id = {$source_id}",$con);
	while($row = mysql_fetch_array($res))$name = $row[0];
}

echo ("<form action='AddEditSource.php?act=yes&source_id={$source_id}' method='post'<br/>");
echo ("<table><br/>");
echo ("<tr>");
echo ("<td> Name </td>");
echo 	("<td><input 	type='text'
						name='nameField'
						value='{$name}'
						maxlength = '{$smaStr}'
						size = '60'
						placeholder='Enter name here'>
		</td>");
echo ("</tr>");
echo ("</table><br/>");
echo ("<button type='submit' name='UpdateTable' value='OK' >OK</button>&nbsp");
echo ("<button type='submit' name='NotUpdateTable' value='Cancel' >Cancel</button>");
echo ("<form><br/>");

?>
