<?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" src="jquery.js"></script>
<script type="text/javascript">
function vali(str,str2,getCount)
{
var r = getCount;
//alert(str);
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=="name"+r){
			document.getElementById("pname"+r).innerHTML=xmlhttp.responseText;
			if(xmlhttp.responseText != "OK"){
				document.getElementById("pname"+r).style.background ="";
			}else{
				document.getElementById("pname"+r).style.background ="";
			}
		}		
		if(str=="price"+r){
			document.getElementById("pprice"+r).innerHTML=xmlhttp.responseText;	
			if(xmlhttp.responseText != "OK"){
			document.getElementById("pprice"+r).style.background ="";
			}else{
				document.getElementById("pprice"+r).style.background ="";
			}	
		}
		if(str=="picture"+r){
			document.getElementById("pimage"+r).innerHTML=xmlhttp.responseText;	
			if(xmlhttp.responseText != "OK"){
			document.getElementById("pimage"+r).style.background ="";
			}else{
				document.getElementById("pimage"+r).style.background ="";
			}	
		}
		if(str=="search"+r){
			document.getElementById("psearch"+r).innerHTML=xmlhttp.responseText;	
			if(xmlhttp.responseText != "OK"){
			document.getElementById("psearch"+r).style.background ="";
			}else{
				document.getElementById("psearch"+r).style.background ="";
			}	
		}
		if(str=="type2"+r){
			document.getElementById("ptype2"+r).innerHTML=xmlhttp.responseText;	
			if(xmlhttp.responseText != "OK"){
			document.getElementById("ptype2"+r).style.background ="";
			}else{
				document.getElementById("ptype2"+r).style.background ="";
			}	
		}
		
		
  }
} 
xmlhttp.open('GET','validate4.php?q='+str+'&q2='+str2+'&q3='+r+'',true);
xmlhttp.send();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function finalcheck(getNumChar1)
{
var ff = getNumChar1;
var f1 = ff.split("-");
var getCheck;
for(p=1;p<=5;p++){
	if(f1[p]==0 || f1[p]==1 || f1[p]==2 || f1[p]==3 || f1[p]==4){
			var div1 = document.getElementById("pname"+f1[p]);
			var div2 = document.getElementById("pprice"+f1[p]);
			var div3 = document.getElementById("pimage"+f1[p]);
			var div4 = document.getElementById("ptype2"+f1[p]);
			var div5 = document.getElementById("psearch"+f1[p]);

			if(div1.innerHTML=='*Please fill in (Maxlength 1-100)' || div2.innerHTML=='*Please fill in money format(example - 9.99)' || div3.innerHTML == '*Invalid file type' || div4.innerHTML=='*Please fill in (Maxlength 1-100)' || div5.innerHTML=='*Please fill in (Maxlength 1-100)'){
				
				getCheck = false;
			}
			else {
				
			}
			
	}
	
}
if(getCheck != false){
		getCheck = true;
}else{
		alert('Please make sure all fields are properly completed.');
	}
//alert(getCheck);
return getCheck;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	function resetimg(getNumChar){	
		var dd = getNumChar;
		var d1 = dd.split("-");
		for(t=1;t<=5;t++){
			if(d1[t]==0 || d1[t]==1 || d1[t]==2 || d1[t]==3 || d1[t]==4){
				document.getElementById("imgDiv"+d1[t]).innerHTML = "";
				var imgDiv = document.getElementById("imgDiv"+d1[t]);
				imgDiv.style.filter = '';
				if(navigator.appName == "Microsoft Internet Explorer"){
					imgDiv.style.width = 0; // image width
					imgDiv.style.height = 0; // image height
				}
				else{
				}
				document.getElementById("pimage"+d1[t]).innerHTML = "";
				document.getElementById("pprice"+d1[t]).innerHTML = "";
				document.getElementById("pname"+d1[t]).innerHTML = "";
				document.getElementById("psearch"+d1[t]).innerHTML = "";
				document.getElementById("ptype2"+d1[t]).innerHTML = "";
				document.getElementById("pimage"+d1[t]).style.background ="";
				document.getElementById("pprice"+d1[t]).style.background ="";
				document.getElementById("pname"+d1[t]).style.background ="";
				document.getElementById("psearch"+d1[t]).style.background ="";
				document.getElementById("ptype2"+d1[t]).style.background ="";
			}
		}
		//alert(d1);
	}

	////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	function choosetype(getNumC){
	var getE = getNumC
	var select1 = document.getElementById("typeP"+getE).value;
	//alert(select1);
	if (select1 == 'Other'){
		document.getElementById("type2"+getE).style.background ="#FFFFFF";
		document.getElementById("type2"+getE).readOnly = false;
		document.getElementById('ptype2'+getE).innerHTML="*Please fill in (Maxlength 1-100)";
		document.getElementById('ptype2'+getE).style.background ="";
	}
	else{
		document.getElementById("type2"+getE).style.background = "#D8D8D8";
		document.getElementById("type2"+getE).readOnly = true;
		document.getElementById("type2"+getE).value = "";
		document.getElementById('ptype2'+getE).innerHTML="";
		document.getElementById('ptype2'+getE).style.background ="#FFFFFF";
	}
	
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkpicture(yy){
var re_text = /\.jpg|\.jpeg|\.gif|\png|\.JPG|\.JPEG|\.GIF|\.PNG/i;
var filename = yy;
if(filename.search(re_text) == -1){
var ddd = "wrong";
return ddd;
//alert("wrong");
}else{
var ddd = "right";
return ddd;
//alert("right");
}
}	
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    function changeImg(obj,count,y) {
	var hhh1 = checkpicture(y);
	//alert(hhh1);
	if (hhh1 == "right"){
	var x = count;
        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"+x).innerHTML = "<img src="+this.src+" width="+this.width+" height="+this.height+">";
					
                }
            }
        } else { // msie...
		document.getElementById("imgDiv"+x).innerHTML = "";
            obj.select();
            var path = document.selection.createRange().text;
            if (path) {
                var imgDiv = document.getElementById("imgDiv"+x);
                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
            }
        }
	}else{}
    }
    function checkSize(width, height) {
        var maxWidth = 200;
        var maxHeight = 200;
        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 [ 200, 200 ];
    }
</script>
<style type="text/css">
	div.comment{
		color:#FE2E64;
		font-size:0.32cm;
	}
</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>>>
			<a href="<?php echo"editproduct.php";?>">&nbsp;Edit Product&nbsp;</a>
			</div></td>
	</tr>
<tr>
	
	<td valign=top colspan=2>
	<!--------------------Inner table 2----------------------->
	<div id="inner">
	<br/>
	<br/>
	
	<?php
$array_num = count($_POST['name']);

if(isset($_POST['submit'])){
	
	//echo "<tr style='background-color:#76888f'>";
//echo "<th id = 'table_data' width='50'></th>";
//echo "<th id = 'table_data' width='100'> ID</th>";
//echo "<th id = 'table_data' > Product Name</th>";
//echo "<th id = 'table_data' > Searches Tags</th>";
//echo "<th id = 'table_data' > Type</th>";
//echo "<th id = 'table_data' width='150'> Price</th>";
//echo "<th id = 'table_data' > URL</th>";
//echo "<th id = 'table_data' > Detail</th>";
//echo "<th id = 'table_data' width='120'> Picture</th>";
	//echo "</tr>";

	for ($i=0; $i <$array_num; $i++){
	
		foreach(array($i) as $num){
			$id = $_POST['id'][$num];
			$checkpic = $id.".";
			$name = $_POST['name'][$num];
			$search = $_POST['search'][$num];
			$type = $_POST['type'][$num];
			$price = $_POST['price'][$num];
			$dollar = $_POST['dollar'][$num];
			$url = $_POST['url'][$num];
			$detail = $_POST['detail'][$num];
			$productPic = $_POST['productPic'][$num];
	
			$query=mysql_query("select * from product where id = '$id'")or die(mysql_error());
			$countType = mysql_query("select * from itemtype ORDER BY ptype");
			echo "<table align = 'center' border = '1' cellpadding = '1' cellspacing = '1' width = '800' style = 'border:solid;border-color:white'>";
			
			if(isset($_POST['check'][$i])){
			$getNumChar =$getNumChar."-".$num;			
				echo "<form enctype='multipart/form-data' action='editproduct3.php' method='POST'>";
				echo "<tr>";
				echo "<td align='center' valign='top' rowspan=6 width=50 style='background-color:#ffffff;'><input type='file' name='file[]' id='picture".$num."' accept='image/gif,image/jpeg,image/png,image/jpg' onchange='changeImg(this,$num,this.value);vali(this.id,this.value,$num)'>";
				echo"<br/>";
				if ($productPic == $checkpic){
						echo"<div id='imgDiv".$num."' align='center' style='margin: 0 0 0 0'>
								<img src='album/NoImage.jpg' width = '200' height = '200' align=center border='0' style='display:block'>
							</div>";
				}else{	
						echo"<div id='imgDiv".$num."' align='center' style='margin: 0 0 0 0'>
								<img src='album/$productPic' width = '200' height = '200' align=center border='0' style='display:block'>						
							</div>";
				}
				echo"<div id='pimage".$num."' class='comment'></div>";
				echo"</td>"; 
				echo "<td  valign='top'>
				<table border=0 width=500>
				<tr><td width=100>ID :</td><td width=400> <input type='hidden' name='id[]' id='ID' value='$id'/>".$_POST['id'][$num]."</td></tr>";
				//$namefieldname='name'.$id;
				echo "<tr><td  valign='top' style='width:140'>Product Name :</td><td width=400> <input type='text' name='name[]' id='name".$num."' maxlength='100' style='width:340' value='$name' style='width:140' onchange='vali(this.id,this.value,$num)'/>";
				echo"<div id='pname".$num."' class='comment'></div></td></tr>";
				
				echo "<tr><td valign='top' style='width:140'>Search Tag :</td><td width=400> <input type='text' name='search[]' id='search".$num."' maxlength='100' style='width:340' value='$search' style='width:140' onchange='vali(this.id,this.value,$num)'/>";
				echo"<div id='psearch".$num."' class='comment'></div></td></tr>";
				//$typefieldname='type'.$id;
				echo "<tr><td valign='top' style='width:140'>Type : </td><td width=400>
					<select name='type[]' id='typeP".$num."' value='$type' style='width:140' onchange='choosetype($num)'>";
						$iii = 0;
						while($gg = mysql_fetch_array($countType)){
							if ($gg['ptype'] == $type){
								$iii = 1;
								echo"<option value='$type' selected='selected'>$type</option>";
							}
							else{
								echo"<option value='".$gg['ptype']."'>".$gg['ptype']."</option>";
							}
						}
							if ($iii == 0){
								echo"<option value='Other' selected='selected'>Other</option>";
							}else{
								echo"<option value='Other'><b>Other</b></option>";
							}
						
				echo"</select>&nbsp;";
				if($iii == 0){
					echo"<input type='text' name='type2[]' id='type2".$num."' style='width:195;background-color:#FFFFFF' maxlength='100' value='$type' onchange='vali(this.id,this.value,$num)'/>
						<div id='ptype2".$num."' class='comment'></div>";
				}else{
					echo"<input type='text' name='type2[]' id='type2".$num."' style='width:195;background-color:#D8D8D8' maxlength='100' readonly='readonly' onchange='vali(this.id,this.value,$num)'/>
						<div id='ptype2".$num."' class='comment'></div>";
				}
				echo"</td></tr>";
				//$pricefieldname='price'.$id;
				echo "<tr><td valign='top' style='width:150'>Price : </td><td width=400>
				<select name='dollorsign[]' style='width:140'>";
					echo"<option value='$dollar' selected = 'selected'>$dollar</option>";
					include("currencycode.php");
				echo"</select>
				<input type='text' name='price[]'  id='price".$num."' style='width:195' value='$price' onchange='vali(this.id,this.value,$num)'/></br>
				<div id='pprice".$num."' class='comment'></div></td></tr>";
				//$detailfieldname='detail'.$id;
				echo "<tr><td valign='top' >Detail : </td><td width=400><textarea rows='8' cols='45' style='resize:none' name='detail[]' value='$detail'>".$detail."</textarea>
				<div id='pcomment' class='comment'></div></td></tr></table></td></tr>";
				echo "<tr><td align='right' valign='top' colspan=2 style='background-color:#E8E8E8;'>URL : <input type='text' name='url[]' id='url".$num."' value='$url' maxlength='100' style='width:140' onchange='vali(this.id,this.value,$num)'/></tr>";
				
				
				//$file='file'.$id;
				
				
				echo "</table><hr width=800>";
			}
		}
	}
	echo"<input type='hidden' name=getCount id='getCount' value='$getNumChar'>";
	echo"<tr style='background-color:#76888f'>";
	//echo $getNumChar;
	echo"<td colspan='8' align='center'><input type='submit' name='submit' value='Modify' onclick='return finalcheck(getCount.value)'><input type='reset' value='Reset' onclick='resetimg(getCount.value)'></td>";
	echo"</tr>";
	echo "</table>"; 
}
echo "";
echo "</form>";
?>
<table width="100%">
<tr>
	<td align="center">
		<a href="editproduct.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>