<?php
include("../session.php");
include("../connect.php");

$user=$_SESSION['company'];
$no = "on";				
?>
<html>
<head>
<title>User Page(Company profile)</title>
<link href="header.css" rel="stylesheet" type="text/css"/>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<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.ui.position.min.js"></script>
    <script type="text/javascript" src="javascript/jquery.ui.timepicker.js?v=0.2.9"></script>
	<!--<script type="text/javascript" src="javascript/include/jquery-1.5.1.min.js"></script>-->
	
	
	<!--<link type="text/css" href="./css/docs.css" rel="stylesheet" media="all" />-->
	<link type="text/css" href="./css/jquery.ibutton.css" rel="stylesheet" media="all" />
	<script type="text/javascript" src="jquery.min.js"></script>
	<script type="text/javascript" src="./lib/jquery.ibutton.js"></script>
	<script type="text/javascript" src="./lib/jquery.easing.1.3.js"></script>
	<script type="text/javascript" src="./lib/jquery.metadata.js"></script>
	<script type="text/javascript">
$(document).ready(function (){
  $(":checkbox").iButton();
});
</script>
<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>
<script type="text/javascript">
	function debug(){
		if( window.console && window.console.log ) console.log(arguments);
	}
	$(document).ready(function (){
		$(".group").iButton({
			  init: function (){
				debug("init", arguments);
			}
			, change: function (){
				debug("change", arguments);
			}
			, click: function (){
				debug("click", arguments);
			}
			, disable: function (){
				debug("disable", arguments);
			}
			, destroy: function (){
				debug("destroy", arguments);
			}
		});
		$("#radio_allowRadioUncheck :radio").iButton({allowRadioUncheck: true});
		}
		
</script>

<script type="text/javascript">
function checkempty(getvalue){
var keywordA = getvalue;
if (keywordA.length < 1){
alert("Please fill in something at search column");
return false;
}else{
return true;
}
}
function vali2(count,a,b,c,e){
	var x = count;
	var startdate = a;
	var enddate = b;
	var startdate1 = a;
	var enddate1 = b;
	var pPrice = c;
	//var pcondition = d;
	var pID = e;
	var test1;
	var errmsg1="";
	var test2;
	var errmsg2="";
	
	if (document.getElementById("ex03"+x).checked==true){	
	////////////////////////check price////////////////////////////////////////////////////////
		matches = pPrice.match(/\$?[0-9]{1,9}\.[0-9]{2}?/g);
		if ((matches==pPrice)&&(pPrice!="")){
			test1 = "ok";
		}else{		
			test1 = "error";
			errmsg1 = "*Please fill in money format(example - 9.99) at promotion price."+'\n\n';
		}
	///////////////////////check date///////////////////////////////////////////////////////////
		var d1 = startdate1.split("-");
		var d2 = enddate1.split("-");
		if ((new Date(d1[2],d1[1],d1[0]) <= new Date(d2[2],d2[1],d2[0]))&&(startdate!="")&&(enddate!="")){
			test2 = "ok";
		}
		else{
			test2 = "error";
			errmsg2 = "*Please check your promotion date,End date must later than start day or same with start day.";
		}
	///////////////////////go action ///////////////////////////////////////////////////////////	
		if ((test2=="ok")&&(test1=="ok")){	
			vali(x,startdate,enddate,pPrice,pID);
			return true;
			
		}else{
			//document.getElementById("ex03"+x).checked = false;
			alert("ERROR MESSAGE"+'\n\n'+""+errmsg1+""+errmsg2);
			return false;
		}
		
	}
	else{
		 vali(x,startdate,enddate,pPrice,pID);
		return true;
	}

}
function vali(count,a,b,c,e){
	var x = count;
	var pID = e;
	if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else{// code for IE6, IE5
		xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
	}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	if (document.getElementById("ex03"+x).checked==true){
	var startdate = a;
	var enddate = b;
	var pPrice = c;
	var pcondition = "";
	//on anction
	document.getElementById("a"+x).innerHTML = "<input type='hidden' size='22' name='startdate"+x+"' id='startdate"+x+"'>"+startdate;
	document.getElementById("b"+x).innerHTML = "<input type='hidden' size='22' name='enddate"+x+"' id='enddate"+x+"'>"+enddate;
	document.getElementById("c"+x).innerHTML = "<input type='hidden' name='pPrice"+x+"' id='pPrice"+x+"'>"+pPrice;
	
	//document.getElementById("d"+x).innerHTML = "<textarea name='pcondition"+x+"' id='pcondition"+x+"' readonly='readonly' rows='2' cols='37' style='resize:none;background-color:#F5A9BC'>"+pcondition+"</textarea>";
	
	xmlhttp.open('GET','on.php?a='+startdate+'&b='+enddate+'&c='+pPrice+'&d='+pcondition+'&e='+pID+'',true);
	xmlhttp.send();
	//alert(startdate+","+enddate+","+pPrice+","+pcondition+","+pID);
	}
	else{
	var startdate = "";
	var enddate = "";
	var pPrice = "";
	//var pcondition = "";
	//off action
	document.getElementById("a"+x).innerHTML = "<input type='text' style='cursor:pointer' name='startdate"+x+"' size='22' id='startdate"+x+"' onClick='updateFields(this.id)' readonly='readonly'/>";
	document.getElementById("b"+x).innerHTML = "<input type='text' style='cursor:pointer' name='enddate"+x+"' size='22' id='enddate"+x+"' onClick='updateFields2(this.id)' readonly='readonly'>";
	document.getElementById("c"+x).innerHTML = "<input type='text' name='pPrice"+x+"' id='pPrice"+x+"' size='22'>";
	//document.getElementById("d"+x).innerHTML = "<textarea rows='2' cols='37'  style='resize:none;background-color:#F2F5A9'' name='pcondition"+x+"' id='pcondition"+x+"' maxlength='500'></textarea>";
	
	xmlhttp.open('GET','off.php?a='+startdate+'&b='+enddate+'&c='+pPrice+'&d='+pcondition+'&e='+pID+'',true);
	xmlhttp.send();
	
	//alert(startdate+","+enddate+","+pPrice+","+pcondition+","+pID);
	} 	
}
</script>
<style>
	a#pag {color:black;font-family: Century Gothic, Segoe UI; text-decoration:none; font-size:13px}
	
</style>
</head>
<body>
<div id="content2">
<!-----------------Outer table--------------->
<table width=1000 border=0 cellspacing=10 cellpadding=3 >
<tr>
	<td colspan=2 >
	<?php include("head.php");?>
	</td>
	</tr>
<tr>
	<td colspan=2>
	<div class=main>&nbsp;&nbsp;>>
			<a href="<?php echo"company.php";?>">&nbsp;Home&nbsp;</a>>>
			&nbsp;Promotion Product&nbsp;
			</div></td>
	</tr>
<tr>
	
	<td valign=top colspan=2>
	<!--------------------Inner table 2----------------------->
<div id="inner">
<form name="searchForm" method="post">
<table border="0" cellspacing="5" cellpadding="5" align="right">
<tr>
	<td align="right">
		<input type="hidden" name="searchBy" value="promotion.pname"/>
		<input type="text" id="keyword" name="keywords">&nbsp;&nbsp;
		<input type="submit" name="search" value="Search Name" style = "background-color: #9191FF; border: 0px; color: #FBFDFB; width:100; height:23; font-family: Segoe UI" onclick="return checkempty(keyword.value)">
		<input type="submit" name="showAll" value="ShowAll" style = "background-color: #9191FF; border: 0px; color: #FBFDFB; width:70; height:23; font-family: Segoe UI">
	</td>
</tr>
</table>
</form>
<br/>
<?php
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE){ 
		echo "<br/><br/><br/>";
} 
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== FALSE){
		echo "<br/><br/><br/>";
}
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== FALSE){ 
		echo "<br/>";
}			
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== FALSE){ 
}			
else{ 
}			
?>
<?php

if($_POST["search"] == "Search Name"){
	$searchBy = $_POST["searchBy"];
	$keywords = $_POST["keywords"];
	$result = mysql_query("SELECT product.status,promotion.pID,promotion.pname,promotion.startdate,promotion.enddate,promotion.normalprice,promotion.promotionprice,promotion.pcondition,promotion.status FROM product INNER JOIN promotion ON product.id=promotion.pID WHERE ".$searchBy." LIKE '%".$keywords."%' AND product.companyID='$user' AND product.status='available' ORDER BY promotion.pID");
}
else if($_POST["showAll"] == "ShowAll"){
	$limit = 5; //max displayed per page
    $start = $_GET['start']; //get start variable
    if(isset($_POST["showAll"]))
	{	$start = 0;}
 	else
 	{	if (!$start) {$start = 0;}}
	
	$sql = $result = mysql_query("SELECT product.status,promotion.pID,promotion.pname,promotion.startdate,promotion.enddate,promotion.normalprice,promotion.promotionprice,promotion.pcondition,promotion.status FROM product INNER JOIN promotion ON product.id=promotion.pID WHERE product.companyID='$user' AND product.status='available' ORDER BY promotion.pID");
	$record_count = mysql_num_rows($sql);
	$max_pages = $record_count / $limit;
	
	$result = mysql_query("SELECT product.status,promotion.pID,promotion.pname,promotion.startdate,promotion.enddate,promotion.normalprice,promotion.promotionprice,promotion.pcondition,promotion.status FROM product INNER JOIN promotion ON product.id=promotion.pID WHERE product.companyID='$user' AND product.status='available' ORDER BY promotion.pID LIMIT $start,$limit");
}
else{
	$limit = 5; //max displayed per page
    $start = $_GET['start']; //get start variable
    if(isset($_POST["showAll"]))
	{	$start = 0;}
 	else
 	{	if (!$start) {$start = 0;}}
	
	$sql = $result = mysql_query("SELECT product.status,promotion.pID,promotion.pname,promotion.startdate,promotion.enddate,promotion.normalprice,promotion.promotionprice,promotion.pcondition,promotion.status FROM product INNER JOIN promotion ON product.id=promotion.pID WHERE product.companyID='$user' AND product.status='available' ORDER BY promotion.pID");
	$record_count = mysql_num_rows($sql);
	$max_pages = $record_count / $limit;
	
	$result = mysql_query("SELECT product.status,promotion.pID,promotion.pname,promotion.startdate,promotion.enddate,promotion.normalprice,promotion.promotionprice,promotion.pcondition,promotion.status,promotion.dollar FROM product INNER JOIN promotion ON product.id=promotion.pID WHERE product.companyID='$user' AND product.status='available' ORDER BY promotion.pID LIMIT $start,$limit");
}

//echo "<table align = 'center' border = '1' cellpadding = '5' cellspacing = '2' width = '1250' style = 'border:solid;border-color:white'>";
//echo "<tr style='background-color:#76888f'>";
//copy add

//echo "<th id = 'table_data'> Product ID</th>";
//echo "<th id = 'table_data'> Product Name</th>";
//echo "<th id = 'table_data'> Promotion Start Date</th>";
//echo "<th id = 'table_data'> Promotion End Date</th>";
//echo "<th id = 'table_data'> Normal Price </th>";
//echo "<th id = 'table_data'> Promotion Price </th>";
//echo "<th id = 'table_data'> Currency Code </th>";
//echo "<th id = 'table_data'> Condition</th>";
//echo "<th id = 'table_data'> Action</th>";
//until here
//echo "</tr>";
$aa = 1;

while($row = mysql_fetch_array($result)){

	if($row['status'] == 'on'){
		//echo"<tr style='background-color:#BDBDBD'>";
		echo "<table border=0 align=center width=700 class=on >";
		echo "<tr class=on><td><b>ID : </b><input type='hidden' name='id".$aa."' id='id".$aa."' value='".$row["pID"]."'>" . $row['pID']."</td></tr>";
		echo "<tr class=on><td><b>Name : </b><input type='hidden' name='pname' id='pname' value='".$row["pname"]."'>" . $row['pname']."</td></tr>";
		echo "<tr><td>";
		echo "<table border=0 width=699 cellpadding=3 class=onInner>";
		echo "<tr>
				<td width=136><b>Start Date :</b></td>
				<td width=170><div id='a".$aa."'><input type='hidden' name='startdate".$aa."' id='startdate".$aa."' size='12' value='".$row["startdate"]."'>" .$row['startdate']."</div></td>";
		echo "	<td width=160><b>End Date :</b></td>
				<td width=233><div id='b".$aa."'><input type='hidden' name='enddate".$aa."' id='enddate".$aa."' size='12' value='".$row["enddate"]."'>" . $row['enddate']."</div></td></tr>";
		echo "<tr>
				<td><b>Current Price :</b></td>
				<td>
				<input type='hidden' name='nPrice' id='nPrice' value='".$row["normalprice"]."'>";printf("%10.2f",$row['normalprice']);echo"<input type='hidden' name='code' id='code' value='".$row["dollar"]."'>" ." ". $row['dollar']."</td>";
		echo "	<td><b>Promotion Price :</b></td>
				<td><div id='c".$aa."'><input type='hidden' name='pPrice".$aa."' id='pPrice".$aa."' value='".$row["promotionprice"]."'>";printf("%10.2f",$row['promotionprice']);echo"</div></td></tr>";
		//echo "<tr>
				//<td><b>Condition :</b></td>
				//<td colspan=3><div id='d".$aa."'><textarea rows='2' cols='37' style='resize:none;background-color:#F5A9BC' name='pcondition".$aa."' id='pcondition".$aa."'>" . $row['pcondition']."</textarea></div></td></tr>";
		echo "<tr ><td align=right  colspan=4>
					<div id='metadata' class='group'>
					<div class='row' id='check".$aa."'>
					<input type='checkbox' style='cursor:pointer' id='ex03".$aa."' name='status' class='{labelOn: 'Yes', labelOff: 'No'}' onchange='return vali2($aa,startdate".$aa.".value,enddate".$aa.".value,pPrice".$aa.".value,id".$aa.".value)' checked='checked'/>
					</div>
					</div>
			</td>";
		echo "</tr></table></td></tr></table><br/>";
	}
	else{
		echo "<table border=0 align=center width=700 class=off>";
		//echo"<tr style='background-color:#BDBDBD'>";
		echo "<tr class=off><td>&nbsp;<b>ID : </b><input type='hidden' name='id".$aa."' id='id".$aa."' value='".$row["pID"]."'>" . $row['pID']."</td></tr>";
		echo "<tr class=off><td>&nbsp;<b>Name : </b><input type='hidden' name='pname' id='pname' value='".$row["pname"]."'>" . $row['pname']."</td></tr>";
		echo "<tr><td>";
		echo "<table border=0 width=699 cellpadding=3 class=offInner>";
		echo "<tr >
				<td width=136><b>Start date :</b></td>
				<td width=170><div id='a".$aa."'><input type='text' style='cursor:pointer' name='startdate".$aa."' size='22' id='startdate".$aa."' onClick='updateFields(this.id)' readonly='readonly'/></div></td>";
		echo "<td width=160><b>End date :</b></td><td width=233><div id='b".$aa."'><input type='text' style='cursor:pointer' name='enddate".$aa."' size='22' id='enddate".$aa."' onClick='updateFields2(this.id)' readonly='readonly'></div></td></tr>";
		echo "<tr >
				<td width='130'><b>Current Price :</b></td>
				<td>
				<input type='hidden' name='nPrice' id='nPrice' value='".$row["normalprice"]."'>";printf("%10.2f",$row['normalprice']);echo"<input type='hidden' name='code' id='code' value='".$row["dollar"]."'>" ." ". $row['dollar']."</td>";
		echo "<td width='140'><b>Promotion Price :</b></td>";
		echo "<td width='130'><div id='c".$aa."'><input type='text' name='pPrice".$aa."' size='22' id='pPrice".$aa."'></div></td></tr>";
		//echo "<tr>
				//<td><b>Condition :</b></td>
				//<td colspan=3><div id='d".$aa."'><textarea rows='2' cols='37' style='resize:none;background-color:#F2F5A9'' name='pcondition".$aa."' id='pcondition".$aa."' maxlength='500'></textarea></div></td></tr>";
		echo "<tr><td align=right  colspan=4>
					<div id='metadata' class='group'>
					<div class='row' id='check".$aa."'>
					<input type='checkbox' style='cursor:pointer' id='ex03".$aa."' name='status' class='{labelOn: 'Yes', labelOff: 'No'}' onchange='return vali2($aa,startdate".$aa.".value,enddate".$aa.".value,pPrice".$aa.".value,id".$aa.".value)'/>
					</div>
					</div>
			</td></tr></table>";
		echo "</td></tr></table><br/>";	
		
	}$aa++;
}
	
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



echo "<div id='pageNavi'>";
//setup prev and next variables
$prev = $start - $limit;
$next = $start + $limit;

//show prev button
echo "<p align='center'>";
if (!($start<=0))
       {echo "<a href='viewproduct.php?start=$prev' id='pag'>Previous</a> ";}

//show page numbers and set variable for first page
$p=1;
for ($x=0;$x<$record_count;$x=$x+$limit)
{
 if ($start!=$x)
    {echo "<a href='viewproduct.php?start=$x' id='pag'>$p</a> ";}
 else
    {echo "<a href='viewproduct.php?start=$x' style='color: #ff2626; font-family: Segoe UI Light' id='pag'><b>Page $p </b></a> ";}
 $p++;
}

//show next button
if (!($start>=$record_count-$limit))
       {echo " <a href='viewproduct.php?start=$next' id='pag'>Next</a>";}
echo "</p>";

echo "</div>";

//$result->free();

?>
<table width="100%">
<tr>
	<td align="center">
		<a href="company.php">
			<font size="4cm" color="purple">Back</font>
		</a>
	</td>
</tr>
</table>
	</div>
	<!----------------------End inner table 2-------------------->
	
	</td>
	</tr>
</table>
<!-----------------------End outer table-------------------------->
</div>

<?php 
include("../closeconnection.php");
?>

</body>
</html>