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

$user=$_SESSION['company'];				
?>
<html>
<head>
<title>User Page(Company profile)</title>
<link href="header.css" rel="stylesheet" type="text/css"/>
<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=='productName'){
			document.getElementById('pname').innerHTML=xmlhttp.responseText;
		}
		
		if(str=='price'){
			document.getElementById('pprice').innerHTML=xmlhttp.responseText;
		}
		if(str=='file'){
			document.getElementById('pfile').innerHTML=xmlhttp.responseText;
		}
		
  }
} 
xmlhttp.open('GET','validate2.php?q='+str+'&q2='+str2+'',true);
xmlhttp.send();
}
function finalcheck()
{
var div = document.getElementById('pname');
var div3 = document.getElementById('pprice');
var div4 = document.getElementById('pfile');

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

else if(div3.innerHTML!='OK'){
	alert('Please make sure all fields are properly completed.');
	return false;
}
else if(div4.innerHTML == '*Invalid file type'){
	alert('Please make sure all fields are properly completed.');
	return false;
}
else return true;

}
</script>
<script type="text/javascript">
    function changeImg(obj) {
        if (obj.files) { // webkit, mozilla... (jq:$.support.boxModel)
            var reader = new FileReader();
            reader.readAsDataURL(obj.files[0]);
            reader.onloadend = function(event) {
                var imgObj = new Image();
                imgObj.src = event.target.result; // image src
                imgObj.onload = function(event) {
                    var size = checkSize(this.width, this.height);
                    this.width = size[0]; // image width
                    this.height = size[1]; // image height
                    document.getElementById('imgDiv').innerHTML = '<img src="'+this.src+'" width='+this.width+' height='+this.height+'>'; // $('#imgDiv').html(this);
                }
            }
        } else { // msie...
            obj.select();
            var path = document.selection.createRange().text;
            if (path) {
                var imgDiv = document.getElementById('imgDiv');
                imgDiv.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image)';
                imgDiv.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = path;
                imgDiv.style.width = 1;
                imgDiv.style.height = 1;
                var size = checkSize(imgDiv.offsetWidth, imgDiv.offsetHeight);
                imgDiv.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)';
                imgDiv.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = path; // image src
                imgDiv.style.width = size[0]; // image width
                imgDiv.style.height = size[1]; // image height
            }
        }
    }
    function checkSize(width, height) {
        var maxWidth = 300;
        var maxHeight = 300;
        if (width && height)
            if (width / height > maxWidth / maxHeight) {
                if (width > maxWidth) {
                    height = height * maxWidth / width;
                    width = maxWidth;
                }
            } else {
                if (height > maxHeight) {
                    width = width * maxHeight / height;
                    height = maxHeight;
                }
            }
        return [ width, height ];
    }
</script>
<style type="text/css">
	div.comment{
		color:purple;
		font-size:0.4cm;
	}
</style>
<script type="text/JavaScript">
 
function confirmA(all){

var countINT = 0;
for (i = 0; i < all; i++){
	if(document.getElementById("check"+i).checked){
		countINT = countINT  + 1;
	}else{}	
}
if(countINT < 1){
	//alert(all+","+countINT);
	alert("AT least select one");
	return false;
}else{
	return true;
}
}
function checkempty(getvalue){
var keywordA = getvalue;
if (keywordA.length < 1){
alert("Please fill in something at search column");
return false;
}else{
return true;
}
}

</script>
<style type="text/css">
	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;Edit Product&nbsp;
			</div></td>
	</tr>
<tr>
	
	<td valign=top colspan=2>
	<!--------------------Inner table 2----------------------->
	<div id="inner">
<?php include("productHead.php")?>
<form name="searchForm" method="post">
<table border="0" cellspacing="5" cellpadding="5" align="right">
<tr>
	<td align="right">
		<input type="hidden" name="searchBy" value="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>
<?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 * FROM product WHERE ".$searchBy." LIKE '%".$keywords."%' AND companyID='$user' AND status='available' ORDER BY id");
}
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 = mysql_query("select * from product where companyID='$user' AND status='available' ORDER BY id ");
	$record_count = mysql_num_rows($sql);
	$max_pages = $record_count / $limit;
	
	$result = mysql_query("select * from product where companyID='$user' AND status='available' ORDER BY id 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 = mysql_query("select * from product where companyID='$user' AND status='available' ORDER BY id ");
	$record_count = mysql_num_rows($sql);
	$max_pages = $record_count / $limit;
	
	$result = mysql_query("select * from product where companyID='$user' AND status='available' ORDER BY id LIMIT $start,$limit");
}

echo "<form action='editproduct2.php' method='post'>";


$i=0;
while($row = mysql_fetch_array($result)){	
	$ID=$row['id'];
	$checkpic = $ID.".";
	echo "<table align = 'center' border = '0' cellpadding = '5' cellspacing = '2' width = '800' style = 'border:solid;border-color:white'>";
	echo"<tr style='background-color:#ffffff'>";
	echo "<td align='center'width = '10' rowspan=7><input type='checkbox' name='check[$i]' id='check".$i."' value=''/></td>";
	if ($row["productPic"] == $checkpic){
		echo "<td align=center rowspan=7 width=150>
					<input type='hidden' name='productPic[]' value='".$row['productPic']."'/>
					<img src='album/NoImage.jpg' width = '100' height = '100'/>
			</td>";
	}
	else{
		echo "<td align=center rowspan=7 width=150>
					<input type='hidden' name='productPic[]' value='".$row['productPic']."'/>
					<img src='album/$row[productPic]' width = '100' height = '100'/>
			</td>";
	}
	echo "</tr>";
	echo "<tr><td><b>ID :</b><input type='hidden' name='id[]' value='".$row["id"]."'>" . $row['id']."</td></tr>";
	echo "<tr><td><b>Product Name :</b><input type='hidden' name='name[]' value='".$row["pName"]."'>" . $row['pName']."</td></tr>";
	if($row['type']=="Other"){
		echo "<tr><td><b>Type :</b><input type='hidden' name='type[]' value='".$row["type2"]."'>" . $row['type2']."</td></tr>";
	}else{
		echo "<tr><td><b>Type :</b><input type='hidden' name='type[]' value='".$row["type"]."'>" . $row['type']."</td></tr>";
	}	
	echo "<tr><td><b>Price :</b><input type='hidden' name='dollar[]' value='".$row["dollar"]."'><input type='hidden' name='price[]' value='".$row["price"]."'>";printf("%10.2f",$row['price']);echo" ".$row["dollar"]."</td></tr>";
	echo "<tr><td><b>Detail :</b><input type='hidden' name='detail[]' value='".$row["detail"]."'>" . $row['detail']."</td></tr>";
	echo "<tr><td><b>Search Tag :</b><input type='hidden' name='search[]' value='".$row["searchtag"]."'>" . $row['searchtag']."</td></tr>";
	echo "<tr style='background-color:#E8E8E8'><td align=right colspan=3><b>URL :</b><input type='hidden' name='url[]' value='".$row["producturl"]."'><a href=''>" . $row['producturl']."</a></td></tr>";
	echo "</table>";
	echo "<hr width=800>";
	
	$i++;
}

echo "<table align=center width=800><tr align='center' style='background-color:#76888f'><td colspan='9'><input type='submit' name='submit' value='Edit Selected Products' onclick='return confirmA($i)'></td></tr></table>";	

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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='editproduct.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='editproduct.php?start=$x' id='pag'>$p</a> ";}
 else
    {echo "<a href='editproduct.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='editproduct.php?start=$next' id='pag'>Next</a>";}
echo "</p>";

echo "</div>";

//$result->free();
echo "</form>";
?>
<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>