<?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=='searchtag'){
			document.getElementById('searchtag2').innerHTML=xmlhttp.responseText;
		}
		if(str=='type2'){
			document.getElementById('ptype2').innerHTML=xmlhttp.responseText;
		}		
		if(str=='price'){
			document.getElementById('pprice').innerHTML=xmlhttp.responseText;
		}
		if(str=='file'){
			document.getElementById('pfile').innerHTML=xmlhttp.responseText;
		}
		if(str=='store'){
			document.getElementById('store2').innerHTML=xmlhttp.responseText;
		}
				
  }
} 
xmlhttp.open('GET','validate2.php?q='+str+'&q2='+str2+'',true);
xmlhttp.send();
}
function finalcheck()
{
var div = document.getElementById('pname');
var div2 = document.getElementById('searchtag2');
var div3 = document.getElementById('pprice');
var div4 = document.getElementById('pfile');
var div5 = document.getElementById('ptype2');
var div6 = document.getElementById('store2');
if(div.innerHTML!='OK'){
	alert('Please make sure all fields are properly completed.');
	return false;
}
else if(div2.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 if(div5.innerHTML == '*Please fill in (Maxlength 1-100)'){
	alert('Please make sure all fields are properly completed.');
	return false;
}
else if(div6.innerHTML == '*Please fill in number only'){
	alert('Please make sure all fields are properly completed.');
	return false;
}
else return true;

}
</script>
<script type="text/javascript">
	function choosetype(){
	var select1 = document.getElementById("type3").value;
	//alert(select1);
	if (select1 == 'Other'){
		document.getElementById("type2").style.background ="#FFFFFF";
		document.getElementById("type2").readOnly = false;
		document.getElementById('ptype2').innerHTML="*Please fill in (Maxlength 1-100)";
	}
	else{
		document.getElementById("type2").style.background = "#D8D8D8";
		document.getElementById("type2").readOnly = true;
		document.getElementById("type2").value = "";
		document.getElementById('ptype2').innerHTML="";
	}
	
	}
	function resetimg(){
		var imgDiv = document.getElementById('imgDiv');
        imgDiv.style.filter = '';
		if(navigator.appName == "Microsoft Internet Explorer"){
			imgDiv.style.width = 0; // image width
			imgDiv.style.height = 0; // image height
		}
		else{
		}
		document.getElementById('imgDiv').innerHTML = "";
		document.getElementById('pprice').innerHTML = "*Please fill in money format(example - 9.99)";
		document.getElementById('searchtag').innerHTML = "*Please fill in (Maxlength 1-100)";
		document.getElementById('pname').innerHTML = "*Please fill in (Maxlength 1-100)";
		document.getElementById('pfile').innerHTML ="*Without image your search priority will drop down(picture format : gif, jpg, jpeg, png)";
	}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	
	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,y) {
	var hhh1 = checkpicture(y);
	if (hhh1 == "right"){
        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
            }
        }
	}else{}
    }
    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:#FE2E64;
		font-size:0.37cm;
	}
</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;Insert Product&nbsp;
			</div></td>
	</tr>
<tr>
	<td valign=top colspan=2>
	<!--------------------Inner table 2----------------------->
	<div id="inner">
<?php
$count = mysql_query("select * from product where companyID='$user'");

$numrow = mysql_num_rows($count) + 1;
if($numrow < 10)
{
	$numrow = "00000".$numrow;
	$number = "P".$user."-".$numrow;
}
elseif($numrow < 100)
{
	$numrow = "0000".$numrow;
	$number = "P".$user."-".$numrow;
}
elseif($numrow < 1000)
{
	$numrow = "000".$numrow;
	$number = "P".$user."-".$numrow;
}
elseif($numrow < 10000)
{
	$numrow = "00".$numrow;
	$number = "P".$user."-".$numrow;
}
elseif($numrow < 100000)
{
	$numrow = "0".$numrow;
	$number = "P".$user."-".$numrow;
}
elseif($numrow < 1000000)
{
	$numrow = "".$numrow;
	$number = "P".$user."-".$numrow;
}
$countType = mysql_query("select * from itemtype ORDER BY ptype");
include("productHead.php");
echo "<br/>";
echo" 
<form enctype='multipart/form-data' name='form2' action='inserting.php' onsubmit='return finalcheck()' method='post'>
<table align = 'center' border = '1' cellpadding = '5' cellspacing = '2' width = '700' style = 'border:solid;border-color:white'>
	<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='$number'>
			<input type='hidden' name='companyId' value='$user'>
			$number
		</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='text' name='productName' maxlength='100' style='width:300' onchange='vali(this.name,this.value)'/><font color='red'> *</font>
			<div id='pname' class='comment'>*Please fill in (Maxlength 1-100)</div>
		</td>
	</tr>
	<tr>
		<th align='right' style='background-color:#76888f' width='35%'><font color='white'>Searches Tags : </font></th>
		<td align='left' style='background-color:#BDBDBD'>
			<input type='text' name='searchtag' maxlength='100' style='width:300' onchange='vali(this.name,this.value)'/><font color='red'> *</font>
			<div id='searchtag2' class='comment'>*Please fill in (Maxlength 1-100)</div>
		</td>
	</tr>
	<tr>
		<th align='right' style='background-color:#76888f' width='35%'><font color='white'>Product type : </font></th>
		<td align='left' style='background-color:#BDBDBD'>
			<select name='type' id='type3' style='width:300' onchange='choosetype()'>";
				while($gg = mysql_fetch_array($countType)){
				echo"<option value='".$gg['ptype']."'>".$gg['ptype']."</option>";
				}
			echo"<option value='Other'><b>Other</b></option>";
			echo"</select><br/>
		<input type='text' name='type2' id='type2' style='width:300;background-color:#D8D8D8' readonly='readonly' onchange='vali(this.name,this.value)'><font color='red'> *</font>
		<div id='ptype2' class='comment'></div>
		</td>
	</tr>
	<tr>
		<th align='right' style='background-color:#76888f' width='35%'><font color='white'>Product price : </font></th>
		<td align='left' style='background-color:#BDBDBD'>
			<input type='text' name='price' style='width:225' onchange='vali(this.name,this.value)'>
			<select name='dollorsign' style='width:70'>";
			include("currencycode.php");
			echo"</select>
			<font color='red'> *</font>
			<div id='pprice' 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'>Store Limit : </font></th>
		<td align='left' style='background-color:#BDBDBD'>
			<input type='text' name='store' maxlength='100' style='width:300' onchange='vali(this.name,this.value)'/><font color='red'> *</font>
			<div id='store2' class='comment'>*Please fill in number only</div>
		</td>
	</tr>
	<tr>
		<th align='right' style='background-color:#76888f' width='35%'><font color='white'>Product description : </font></th>
		<td align='left' style='background-color:#BDBDBD'>
			<textarea rows='5' cols='35' style='resize:none' name='detail' maxlength='500'></textarea>
			<div id='pdetail' class='comment'>*If no comments can blank it (Maxlength 1-500)</div>
		</td>
	</tr>
	<tr>
		<th align='right' style='background-color:#76888f' width='35%'><font color='white'>URL : </font></th>
		<td align='left' style='background-color:#BDBDBD'>
			<input type='text' name='urllink' maxlength='100' style='width:300' value='http://'/>
			<div id='urllink' class='comment'>*Please ensure your url is correct before press insert button (Maxlength 1-100)</div>
		</td>
	</tr>
	<tr>
		<th align='right' style='background-color:#76888f' width='35%'><font color='white'>Product picture : </font></th>
		<td align='left' style='background-color:#BDBDBD'>
			<input type='file' name='file' accept='image/gif,image/jpeg,image/png,image/jpg'  id='file' onchange='vali(this.name,this.value);changeImg(this,this.value)'>
			<div id='pfile' class='comment'>*Without image your search priority will drop down(picture format : gif, jpg, jpeg, png)</div>
			<div id='imgDiv' align='center' style='margin: 0 0 0 0'></div>
		</td>
	</tr>
	<tr style='background-color:#76888f'>
		<td align='center' colspan='2'>
			<input type='submit' value='Insert'>
		</td>
	</tr>

</table>
</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>