<?php
    session_start();          
    if(empty($_SESSION['p0B_c0ff33'])) header('location:login.php?goto=index');
?>

<?php include('templates/header.php'); ?>
<?php include('templates/menu.php'); ?>
  	<script>	
		$(document).ready(function() {
			// SUCCESS AJAX CALL, replace "success: false," by:     success : function() { callSuccessFunction() },

			$("#formID").validationEngine()						// CLOSE ALL OPEN PROMPTS
		});
	</script>
<?php

include "../db_function.php";
$table="tbl_clothes";
if(!empty($_GET['sid'])){
    $sid = $_GET['sid'];
}
$show_title = mysql_fetch_array(mysql_query("Select * From tbl_shop WHERE id='".mysql_real_escape_string($sid)."'"));
$imagelocation = "../Site/images/clothes/";
$bigimagelocation = "../Site/images/clothes/b/";

$linkUpload='&table=tbl_moreimages';
if(isset($_POST['submit'])){
    $cate = isset($_POST['cate']) ? $_POST['cate'] : '';
    $subcate = isset($_POST['subcategory']) ? $_POST['subcategory'] : '';
    $code = isset($_POST['code']) ? trim($_POST['code']) : '';
    $price = isset($_POST['price']) ? floatval($_POST['price']) : 0;
    $check = isset($_POST['check']) ? $_POST['check'] : 'on';
    $desc = isset($_POST['detail']) ? $_POST['detail'] : '';
    $c = (!empty($subcate)) ? $subcate : $cate;
    $sid_val = !empty($sid) ? $sid : '1';
    
    $image = '';
    $errors = 0;
    $change = '';
    
    if(!file_exists($imagelocation)) { @mkdir($imagelocation, 0777, true); }
    if(!file_exists($bigimagelocation)) { @mkdir($bigimagelocation, 0777, true); }
    
    if (isset($_FILES['image']) && !empty($_FILES['image']['name'])) {
        $raw_image = $_FILES['image']['name'];
        $uploadedfile = $_FILES['image']['tmp_name'];
        $extension = strtolower(pathinfo($raw_image, PATHINFO_EXTENSION));
        
        if (!in_array($extension, array('jpg', 'jpeg', 'png', 'gif'))) {
            $change = '<div class="msgdiv">Unknown Image extension</div>';
            $errors = 1;
        } else {
            $clean_name = pathinfo($raw_image, PATHINFO_FILENAME);
            $image = $clean_name . '.' . $extension;
            $counter = 1;
            while (file_exists($imagelocation . $image) || file_exists($bigimagelocation . $image)) {
                $image = $clean_name . '_' . $counter . '.' . $extension;
                $counter++;
            }
            
            if (!@move_uploaded_file($uploadedfile, $bigimagelocation . $image)) {
                @copy($uploadedfile, $bigimagelocation . $image);
            }
            @copy($bigimagelocation . $image, $imagelocation . $image);
        }
    }
    
    if (!$errors) {
        $sid_esc = mysql_real_escape_string($sid_val);
        $mcid_esc = mysql_real_escape_string($cate);
        $cid_esc = mysql_real_escape_string($c);
        $image_esc = mysql_real_escape_string($image);
        $code_esc = mysql_real_escape_string($code);
        $price_esc = mysql_real_escape_string($price);
        $status_esc = mysql_real_escape_string($check);
        $desc_esc = mysql_real_escape_string($desc);
        
        $query = "INSERT INTO $table (s_id, mcid, cid, image, code_id, price, status, detail) VALUES ('$sid_esc', '$mcid_esc', '$cid_esc', '$image_esc', '$code_esc', '$price_esc', '$status_esc', '$desc_esc')";
        $suc = mysql_query($query);
        if($suc){
            $masg = 1;
            $id = mysql_insert_id();
        } else {
            $masg = 0;
        }
    } else {
        $masg = 2;
    }
}
	
?>
<script>	
	$(document).ready(function() {
		// SUCCESS AJAX CALL, replace "success: false," by:     success : function() { callSuccessFunction() }, 
		$("#formID").validationEngine()						// CLOSE ALL OPEN PROMPTS
	});
</script>
 
<!-- start content-outer -->
<div id="content-outer">
<!-- start content -->
<div id="content">

<div id="page-heading">
  <div style="font-size: 20px; border: 0px solid #ff0000; text-align: center;"><?php echo $show_title['name']; ?></div>
  <h1>Add Clothes</h1></div>


<table border="0" width="100%" cellpadding="0" cellspacing="0" id="content-table">
<tr>
	<th rowspan="3" class="sized"><img src="images/shared/side_shadowleft.jpg" width="20" height="300" alt="" /></th>
	<th class="topleft"></th>
	<td id="tbl-border-top">&nbsp;</td>
	<th class="topright"></th>
	<th rowspan="3" class="sized"><img src="images/shared/side_shadowright.jpg" width="20" height="300" alt="" /></th>
</tr>
<tr>
	<td id="tbl-border-left"></td>
	<td>
		<div id="content-table-inner">
	<!--  start content-table-inner -->
	<?php
	if(isset($masg) && $masg==1 ){
	echo'
	<div id="message-green">
		<table border="0" width="100%" cellpadding="0" cellspacing="0">
		<tr>
			<td class="green-left">
				You added product in room: <b>'.$image.'</b> success!
				<a href="edit.php?table='.$table.'&row=detail&fildID=id&id='.$id.'" rel="facebox" >
					Please add description !
				</a>
				&nbsp; OR &nbsp;
				<a href="addmoreimage/viewmoreimage.php?aid='.$id.$linkUpload.'" rel="facebox" style="color:#168a10;"> 
					Add more images
				</a>				
			</td>
			<td class="green-right"><a class="close-green"><img src="images/table/icon_close_green.gif" alt="" /></a></td>
		</tr>
		</table>
	</div>';
	}
	else if(isset($masg) && $masg==0 ){
	echo'
	<div id="message-red">
		<table border="0" width="100%" cellpadding="0" cellspacing="0">
			<tr>
					<td class="red-left">Fail. Please try again.</td>
				<td class="red-right"><a class="close-red"><img src="images/table/icon_close_red.gif"   alt="" /></a></td>
			</tr>
		</table>
	</div>';
	}else if(isset($masg) && $masg==2){
	echo'
	<div id="message-yellow">
		<table border="0" width="100%" cellpadding="0" cellspacing="0">
			<tr>
					<td class="yellow-left">'.$change.'</td>
				<td class="yellow-right"><a class="close-yellow"><img src="images/table/icon_close_yellow.gif"   alt="" /></a></td>
			</tr>
		</table>
	</div>';
	}
	?>

    <script type="text/javascript">
	$(document).ready(function()
	{
		$(".category").change(function()
		{		
			var id=$(this).val();
			var sid ='<?php echo $sid; ?>';
			var dataString = 'id='+id+'&sid='+sid;				
			$(".subcategory").addClass('subcategory'+id);
			$.ajax
			({
				type: "POST",
				url: "select_sub_cate.php",
				data: dataString,
				cache: false,
				success: function(html)
				{
					$(".subcategory").html(html);
				}						
			});			
		});			
	});
	</script>
	
	<table border="0" width="100%" cellpadding="0" cellspacing="0">
	<tr valign="top">
	<td>

		<!-- start id-form -->
	 <form name="form" id="formID" method="POST" action="" enctype="multipart/form-data">
		<table border="0" cellpadding="0" cellspacing="0"  id="id-form">

		<!--<tr>
			<th valign="top"><font class="note">* </font> Name :</th>
			<td><input type="text" class="validate[required] inp-form" id="name" name="name" /></td>
			<td></td>
		</tr> -->
		<tr>
			<th valign="top"><font class="note">* </font> Code Number :</th>
			<td><input type="text" class="validate[required] inp-form" id="code" name="code" /></td>
			<td></td>
		</tr> 
		<tr>
			<th valign="top"><font class="note">* </font> Price :</th>
			<td><input type="text" class="inp-form" id="price" name="price" /><font size='5' class="note">$</font></td>
			<td></td>
		</tr> 
		<tr>
			<th valign="top"><font class="note"></font> New Product:</th>
			<td>
            	<input type="checkbox" name="check" id="check" />
            </td>
			<td></td>
		</tr>   		
        <tr>
			<th valign="top"><font class="note">* </font> Category :</th>
			<td>
            	<select name="cate" id="cate" class="styledselect-year validate[required] category" style="width:200px;">
                <option value="">Select Category</option>
            	<?php
				$cate = mysql_query("SELECT * FROM tbl_cate_clothes WHERE s_id='$sid' AND par_id='0'");
				while($rowcate=mysql_fetch_array($cate))
				{
					echo '<option value="'.$rowcate['id'].'">'.$rowcate['name'].'</option>';
				}
				?>
                </select>
            </td>
			<td></td>
		</tr>
		<tr>
			<th valign="top"><font class="note">* </font> Sub Category :</th>
			<td>
            	<select name="subcategory" id="type" class="styledselect-year subcategory" style="width:200px;">
                	<option value="">Select Category First</option>            		
                </select>
            </td>
			<td></td>
		</tr>

		<tr>
            <th valign="top"><font class="note">* </font>Image :</th>
            <td>
            	<input name="image" class="imagefial" size="30" type="file" /> <div class="bubble-left"></div><div class="bubble-inner">JPG, PNG, GIF Size (600 x 774)px</div><div class="bubble-right"></div>
            </td>
			<td></td>
		</tr>
		<tr>
			<th>&nbsp;</th>
			<td valign="top"><br /><br />
                <input type="submit" value="" name="submit" class="form-submit" />
                <input type="reset" value="" class="form-reset"  />
			</td>
			<td></td>
		</tr>
        
		<tr height="20">
			<th valign="top"></th>
			<td></td>
			<td></td>
		</tr>
		
		</table>
	<!-- end id-form  -->
	</form>
	</td>
	<td>

	<!--  start related-activities -->
	<div id="related-activities">
		
		<!--  start related-act-top -->
		<div id="related-act-top">
		<img src="images/forms/header_related_act.gif" width="271" height="43" alt="" />
		</div>
		<!-- end related-act-top -->
		
		<!--  start related-act-bottom -->
		<div id="related-act-bottom">
		
			<!--  start related-act-inner -->
			<div id="related-act-inner">
			
				<div class="left"><a href=""><img src="images/forms/icon_plus.gif" width="21" height="21" alt="" /></a></div>
				<div class="right">
					<h5><font color="red">Note</font></h5>
					 <font size="2">You must fill inthe fields marked with a (<font color="red"> * </font>)</font>
				</div>
				
				<div class="clear"></div>
				<div class="lines-dotted-short"></div>
				
				<div class="left"><a href=""><img src="images/forms/icon_minus.gif" width="21" height="21" alt="" /></a></div>
				<div class="right">
					<font size="2"><h5>More problems</h5>
					If you have any problems please contact us :<br /><br />
					<font color="#4e4e4e"><b>- Designer : +855 81 6000 61<br />- Hotline &nbsp;&nbsp;&nbsp;: +855 23 994 119<br />
                    	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+855 23 5000 470<br />
                        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;+855 23 63 64 678
                    </b></font>
					</font>
				</div>
				
				<div class="clear"></div>
				
			
				
			</div>
			<!-- end related-act-inner -->
			<div class="clear"></div>
		
		</div>
		<!-- end related-act-bottom -->
	
	</div>
	<!-- end related-activities -->

</td>
</tr>
<tr>
<td><img src="images/shared/blank.gif" width="695" height="1" alt="blank" /></td>
<td></td>
</tr>
</table>
 
<div class="clear"></div>
 

</div>
<!--  end content-table-inner  -->
</td>
<td id="tbl-border-right"></td>
</tr>
<tr>
	<th class="sized bottomleft"></th>
	<td id="tbl-border-bottom">&nbsp;</td>
	<th class="sized bottomright"></th>
</tr>
</table>


<div class="clear">&nbsp;</div>

</div>
<!--  end content -->
<div class="clear">&nbsp;</div>
</div>
<!--  end content-outer -->

 

<div class="clear">&nbsp;</div>