<?php
	include("../session.php");
	include("../connect.php");
	$user=$_GET["user"];
	$pid=$_GET["pid"];
	$pname=$_GET["pname"];
	$pprice=$_GET["pprice"];
	
	$checkquery = mysql_query("SELECT * FROM promotion where pID='$pid'"); 
?>
<!------------------------------------------------------------------>
<html>
<head>
<title>User Page(Company profile)</title>
<!------------------------------------------------------------------>
<style type="text/css">
	div.comment{
		color:purple;
		font-size:0.4cm;
	}
</style>
<!------------------------------------------------------------------>
<style type="text/css">
	div.comment{
		color:purple;
		font-size:0.4cm;
	}
</style>
<script type="text/javascript">
function vali(str,str2)
{

if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
}
else{// code for IE6, IE5
	xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange=function(){
  if (xmlhttp.readyState==4 && xmlhttp.status==200){
		if(str=='promotionprice'){
			document.getElementById('promotionprice').innerHTML=xmlhttp.responseText;
		}
		
		if(str=='startdate'){
			document.getElementById('startdate').innerHTML=xmlhttp.responseText;
		}
		if(str=='enddate'){
			document.getElementById('enddate').innerHTML=xmlhttp.responseText;
		}
		
  }
} 
xmlhttp.open('GET','validate3.php?q='+str+'&q2='+str2+'',true);
xmlhttp.send();
}
function finalcheck()
{
var div = document.getElementById('promotionprice');
var div2 = document.getElementById('startdate');
var div3 = document.getElementById('enddate');

if(div.innerHTML!='OK'){
	alert('Please make sure all fields are properly completed.');
	return false;
}

else if(div5.innerHTML!='OK'){
	alert('Please make sure all fields are properly completed.');
	return false;
}
else if(div6.innerHTML!='OK'){
	alert('Please make sure all fields are properly completed.');
	return false;
}
else return true;

}
</script>
<!------------------------------------------------------------------>
	<script src="javascript/src/js/jscal2.js"></script>
    <script src="javascript/src/js/lang/en.js"></script>
    <link rel="stylesheet" type="text/css" href="javascript/src/css/jscal2.css" />
    <link rel="stylesheet" type="text/css" href="javascript/src/css/border-radius.css" />
    <link rel="stylesheet" type="text/css" href="javascript/src/css/steel/steel.css" />
	<link rel="stylesheet" href="javascript/include/jquery-ui-1.8.14.custom.css" type="text/css" />
    <link rel="stylesheet" href="javascript/jquery.ui.timepicker.css?v=0.2.9" type="text/css" />
<!------------------------------------------------------------------------------------->
	<script type="text/javascript" src="javascript/include/jquery-1.5.1.min.js"></script>
    <script type="text/javascript" src="javascript/include/jquery.ui.position.min.js"></script>
    <script type="text/javascript" src="javascript/jquery.ui.timepicker.js?v=0.2.9"></script>

</head>

<body>

<!------------------------------------------------------------------>
<div id="header"  style="background-color:#FF2626">
	<center>
		<h1 style="margin-bottom:0">Easy shop Management</h1>
	</center>
</div>
<!------------------------------------------------------------------>

</br>
<!------------------------------------------------------------------>
<?php
if(mysql_num_rows($checkquery)==0){
echo"
<table align = 'center' border = '1' cellpadding = '5' cellspacing = '2' style = 'border:solid;border-color:white'>
	<tr>
		<td style='background-color:#76888f'><h2 align='center' style='color:white;font-size:1cm'>Add Promotion</h2></td>
	</tr>
</table>";
}
else{
echo"
<table align = 'center' border = '1' cellpadding = '5' cellspacing = '2' style = 'border:solid;border-color:white'>
	<tr>
		<td style='background-color:#76888f'><h2 align='center' style='color:white;font-size:1cm'>Update Promotion</h2></td>
	</tr>
</table>";
}

$count = mysql_query("select * from promotion");

$numrow = mysql_num_rows($count) + 1;
$number = "PRO".$user."-".$numrow;

echo" 
<form enctype='multipart/form-data' name='form2' action='insertpromotion.php?user=$user' onsubmit='return finalcheck()' method='post'>
<table align = 'center' border = '1' cellpadding = '5' cellspacing = '2' width = '500cm' style = 'border:solid;border-color:white'>
	<tr>
		<th align='right' style='background-color:#76888f' width='35%'><font color='white'>Promotion ID : </font></th>
		<th align='left' style='background-color:#BDBDBD'>
			<input type='hidden' name='promotionId' value='$number'>
			<input type='hidden' name='companyId' value='$user'>";
			if(mysql_num_rows($checkquery)==0){
				echo"$number";
			}
			else{
				$catchid = mysql_result($checkquery,0,0);
				echo"$catchid";
				
			}
		echo"</th>
	</tr>
	<tr>
		<th align='right' style='background-color:#76888f' width='35%'><font color='white'>Product ID : </font></th>
		<th align='left' style='background-color:#BDBDBD'>
			<input type='hidden' name='productId' value='$pid'>
			$pid
		</th>
	</tr>
	<tr>
		<th align='right' style='background-color:#76888f' width='35%'><font color='white'>Product Name : </font></th>
		<td align='left' style='background-color:#BDBDBD'>
			<input type='hidden' name='promotionName' value='$pname'>
			$pname
		</td>
	</tr>
	<tr>
		<th align='right' style='background-color:#76888f' width='35%'><font color='white'>Normal Price(RM): </font></th>
		<td align='left' style='background-color:#BDBDBD'>
			<input type='hidden' name='normalPrice' value='$pprice'>
			$pprice
		</td>
	</tr>
	<tr>
		<th align='right' style='background-color:#76888f' width='35%'><font color='white'>Prmotion price(RM) : </font></th>
		<td align='left' style='background-color:#BDBDBD'>
			<input type='text' name='promotionprice' size='35' onchange='vali(this.name,this.value)'>
			<div id='promotionprice' class='comment'>*Please fill in money format(example - 9.99)</div>
		</td>
	</tr>
	<tr>
		<th align='right' style='background-color:#76888f' width='35%'><font color='white'>Start Date : </font></th>
		<td align='left' style='background-color:#BDBDBD'>
			<input type='text' style='cursor:pointer' name='startdate' size='35' id='startdate' onClick='updateFields(this.id)' onchange='vali(this.name,this.value)' />
			<div id='startdate' class='comment'>*Please select date</div>
		</td>
	</tr>
	<tr>
		<th align='right' style='background-color:#76888f' width='35%'><font color='white'>End Date : </font></th>
		<td align='left' style='background-color:#BDBDBD'>
			<input type='text' style='cursor:pointer' name='enddate' size='35' id='enddate' onClick='updateFields2(this.id)' onchange='vali(this.name,this.value)' readonly='readonly'/>
			<div id='enddate' class='comment'>*Please select date</div>
		</td>
	</tr>
	<tr>
		<th align='right' style='background-color:#76888f' width='35%'><font color='white'>Condition : </font></th>
		<td align='left' style='background-color:#BDBDBD'>
			<textarea rows='5' cols='35' style='resize:none' name='pcondition' maxlength='500'></textarea>
			<div id='pcondition' class='comment'>*If no comments can blank it (Maxlength 1-500)</div>
		</td>
	</tr>
	<tr style='background-color:#76888f'>
		<td align='center' colspan='2'>
			<input type='submit' value='Confirm'>
			<input type='reset' value='Reset'>
		</td>
	</tr>

</table>
</form>
";
?>
    <script type="text/javascript">//<![CDATA[

      var cal = Calendar.setup({
          onSelect: function(cal) { cal.hide() },
		  min: Calendar.dateToInt(new Date()),
          showTime: true
		  
		  
      });
	  function updateFields(str){
      cal.manageFields(str, str, "%d-%m-%Y");
	  }
    //]]></script>
	    <script type="text/javascript">//<![CDATA[

      var cal = Calendar.setup({
          onSelect: function(cal) { cal.hide() },
		   min: Calendar.dateToInt(new Date()),
          showTime: true
		  
      });
	  function updateFields2(str){
      cal.manageFields(str, str, "%d-%m-%Y");
	  }
    //]]></script>
</body>
</html>
<?php 
include("../closeconnection.php");
?>