<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: 			*/
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004-2008 ARMIA INC                                    |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts EasyCart                      |
// +----------------------------------------------------------------------+
// | Authors: Shyam<shyam.sunder@armiasystems.com>             		  |
// +----------------------------------------------------------------------+
?>
<?php echo $javascript->link('jquery.js'); ?>
<script>
 $(document).ready(function() 
   {
      var dflt_id=$('#ProductCategoryId').val();
	  if(dflt_id != 0)
	  loadCustomFields(dflt_id)
   });
</script>

<script>
var catid='';
var index=1;
var cmbn=0;
function loadCustomFields(id)
{		
	  $('.stock_div').hide(); $('.reorder_div').hide();
	 for(i=1;i<=index;i++)
	  {
	     $('.attributes'+i+'').remove();
		 $('#combn_cnt'+i+'').remove();
	  }		
	  catid=id;
	  ajaxLoadAttributes(id); 	  
	  return false;	
}

function addMore()
{		
	  for(i=index;i>0;i--)
	 {
	   $('#addmore'+i+'').remove();
	   $('#delrow'+i+'').show();
	 }  
	 ajaxLoadAttributes(catid); 			
	 return false;		
}

var imgcnt=0;
var totimgcnt=<?php echo $cnfg["product_images"];?>;
function addMoreImage()
{
  imgcnt++;
  if(imgcnt<totimgcnt)
  {
  $('#prod_img').append('<br><input type=\'file\' name=\'data[Product][pdctimg'+imgcnt+']\' size=\'10\'>'+
                        'Default<input type=\'radio\' name=\'dfltimg\' value=\''+imgcnt+'\' onclick="javascript:$(\'#ProductDefaultimg\').val(this.value)"'+'/>');
	}
  else
  alert("Sorry,You cannot upload more than "+totimgcnt+" images");
  return false;
}

function delRow(rowid)
{		
	 $('.attributes'+rowid+'').remove();
	 $('#combn_cnt'+rowid+'').remove();
	 return false;
}

function ajaxLoadAttributes(id)
{
	 $('#loader').show();
	   $.ajax({
		type: "post",  // Request method: post, get
		url: "showcustomfields/"+id+"/"+index, // URL to request		
		success: function(response)
		 {
		   if($.trim(response)!='')
		   {
		    $('.prod_row').before(response);
		    index++;
		    cmbn=1;
			 $('#loader').hide();
			}
			else
			{
			// $('#attr_div').hide();
			  $('.stock_div').show(); $('.reorder_div').show();
			  cmbn=0;
			   $('#loader').hide();
			}
		 },
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		alert(textStatus);
		}
		});		
	return false;		
}		
</script>
<script>
function validateFields()
{	
    with(document.Product)
	{ 
	 var errorstr = '';
	 var msgstr = "Sorry, we cannot complete your request.\nKindly provide us the missing or incorrect information enclosed below.\n\n";
	if($.trim($('#ProductCategoryId').val())==0) { errorstr += "- Please Select a Category.\n"; }
	if($.trim($('#ProductPname').val())=='') { errorstr += "- Please Enter Product Name.\n"; }	  
	if($.trim($('#ProductDescr').val())=='') { errorstr += "- Please Enter Product Description.\n";}
	if($.trim($('#ProductPrice').val())=='') { errorstr += "- Please Enter Product Price.\n";}
	if($.trim($('#ProductProdtype').val())=='') { errorstr += "- Please Enter Product Type.\n";}
	if($.trim($('#ProductWeight').val())=='') { errorstr += "- Please Enter Product Weight.\n";}	
	
	if(cmbn==0)
	{
      if($.trim($('#ProductStock').val())=='') { errorstr += "- Please enter the Stock value.\n"; }		 
	  if($.trim($('#ProductReorderlevel').val())=='') { errorstr += "- Please enter the Reorder level.\n";}		       
	}
	
	/*
	if(cmbn==1)
	{
   	if($.trim($('#stockattr1').val())=='') { errorstr += "- Please enter the Stock value.\n"; }		    
	}	*/  
	
		 
		  if (errorstr != '')
		   {
			msgstr = msgstr + errorstr;
			alert(msgstr);
			return false;
		  }
		  else
		  {		  
			ProductTotalimg.value=imgcnt;
			return true;
		 }
	} 
} 
</script>


<div align="center" class="subheading">
              <div align="left">Products</div>
            </div>
            <br />
			<?php if($session->check('Message.flash')) { ?><div id="okbox"><font color="#446423"><?php $session->flash(); ?></font></div><?php } ?>			
            <div align="left" id="item_details_admin">
              <!--items display area start -->
              <div align="center" id="items_display_area_admin">
                <div align="left" class="inner_content_area_admin">
				
				
                  <!--form area start -->
			<?php echo $form->create('Product', array('name' =>'Product','type' => 'file')); ?>
                  <div id="admin_heading">Add Product</div>
				  
						  <div id="grid_admin">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner">Category  <span class="required_field">*</span></p>
							</div>
							<div align="right" id="inner_rightcontent">
							<div style="padding:15px 120px 0px 0px;float:right;display:none;" id="loader"><?php echo $html->image('/img/load.gif');?></div>
								<span class="toplinks">
									<?php 
									$pcat[0]="-------------- Select ---------------";
									ksort($pcat);
									echo $form->input('category_id', array('options' => $pcat,'label' => '',"OnChange"=>"javascript:loadCustomFields(this.value)",'class' => 'box2_admin'));
									?>									
								</span>							
							</div>							
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						  </div>
						  
						  
						  <div id="grid_admin">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner">Product Name  <span class="required_field">*</span></p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input('pname', array('label' => '','class' => 'box2_admin')); ?>
								</span>							
							</div>
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						  </div>
						  
						  
						  <div id="grid_admin" class="stock_div" style="display:none;">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner">Stock  <span class="required_field">*</span></p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input('stock', array('label' => '','class' => 'box2_admin')); ?>
								</span>							
							</div>
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						  </div>
						  
						  
						  <div id="grid_admin" class="reorder_div" style="display:none;">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner">Reorder Level  <span class="required_field">*</span></p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input('reorderlevel', array('label' => '','class' => 'box2_admin')); ?>
								</span>							
							</div>
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						  </div>
						  
						  
						  <div id="grid_admin">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner">Product Image  <span class="required_field">*</span></p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<div id="prod_img" class="maintext_inner">
									 <input type="file" name="data[Product][pdctimg0]" size="10">Default
									 <input type="radio" name="dfltimg"  value="0" checked="checked" onclick="javascript:$('#ProductDefaultimg').val(this.value);">
									<a href="#" onClick="javascript:return addMoreImage();">
									<?php echo $html->image("/img/add.gif", array("border" => "0", "valign" => "top"));?>
									</a>
									</div>
									<div class="maintext_inner"><font color="#FF0000">Note: Upload Images of size greater than 400 x 400 pixels.</font></div>
									<div class="maintext_inner">&nbsp;</div>
								</span>							
							</div>
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						  </div>
						  
						  
						  <div id="grid_admin" class="prod_row">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner">Product Description  <span class="required_field">*</span></p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input('descr', array('rows' => '3','label' => '','class' => 'box2_admin')); ?>
								</span>							
							</div>
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						  </div>
						  
						  
						  <div id="grid_admin">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner">Product price  <span class="required_field">*</span></p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input('price', array('label' => '','class' => 'box2_admin')); ?>
								</span>							
							</div>
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						  </div>
						  
						  
						    <div id="grid_admin">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner">Discount Price</p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input('discount', array('label' => '','class' => 'box2_admin')); ?>
								</span>							
							</div>
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						  </div>
						  
						  
						  
						  <div id="grid_admin">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner">Product Type <span class="required_field">*</span></p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input("prodtype", 
									array("options" => array(""=>"-------------- Select ----------------","Non-Digital"=>"Non-Digital","Digital"=>"Digital"),
									"label" => "",'class' => 'box2_admin'));?>
								</span>							
							</div>
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						  </div>
						  
						  
						  
						  <div id="grid_admin">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner">Product Weight <span class="required_field">*</span></p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input('weight', array('label' => '','class' => 'box2_admin')); ?>
								</span>							
							</div>
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						  </div>
				  
				  
				        <div id="grid_admin">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner">&nbsp;</p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->radio('featured', array('No', 'Yes'), array('default' => 'No')); ?>
								</span>							
							</div>
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						  </div>
				  							
								
								<div id="admin_bottom">
								<div align="left" id="inner_leftcontent">
								  <p class="maintext_inner">&nbsp;</p>
								</div>
								<div align="right" id="inner_rightcontent">
									<span class="toplinks">		
									<input type="submit" value="Save" class="buttn_admin" onclick="return validateFields();">
									<input type="button" value="Cancel" class="buttn_admin" onClick="javascript:history.go(-1);">
									</span>
								</div>
								<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
								</div>
								
								<?php echo $form->input('totalimg', array('type' => 'hidden','value' =>0)); ?>
                                 <?php echo $form->input('defaultimg', array('type' => 'hidden','value' =>0)); ?>
								
				 <?php echo $form->end(); ?> 
                  <!--form area end -->			  
				  
                </div>
                <!--form area end -->
              </div>
              <div align="left" class="float_clear1"></div>
            </div>
            <div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
            <br /> <br /> 