<?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'); ?>
<?php echo $javascript->link('alphanumeric.js'); ?>
<script>
 $(document).ready(function() 
   {
      var dflt_id=$('#ProductCategoryId').val();
	  if(dflt_id != 0)
	  {
	   loadCustomFields(dflt_id)
	   }
	    var dflt_type='';
		dflt_type=$.trim($('#ProductDiscountType').val());		
		setDiscountlbl(dflt_type);
   });
   
function chkdigit(di){
//alert("chk" + di);
if(di == 'Digital'){ 	
	 $('#digit').show();
         $('#prodwght').hide();
         // $('.stock_div').hide();
         // $('.reorder_div').hide();
        
}else{
	 $('#digit').hide();
         $('#prodwght').show();
          $('.stock_div').show();
          $('.reorder_div').show();
}
return true;
}
</script>
<script>
var catid='';
var index=1;
var cmbn=0;
function loadCustomFields(id)
{		
	if($.trim($('#ProductCategoryId').val())!= 1)
	{
		 $.ajax({
			type: "post",  // Request method: post, get
			url: "checksubcat/"+id, // URL to request		
			success: function(response)
			 {	
			   var res=$.trim( response);
			   if(res=='yes')
			   {		   
				 alert("Subcategory exists under this category.\nPlease select a leaf level category.\n\n"); 
				 $('#ProductCategoryId').val(0);
				 return false;
			   }	 
			},
			error:function (XMLHttpRequest, textStatus, errorThrown) {
			alert(textStatus);
			}
			});		
	}		
	
	
	
	
	 $('.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+']\'  id=\'pimage'+imgcnt+'\' size=\'10\'>'+
                        '&nbsp;&nbsp;Set as Default Image<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+'').fadeOut();
	 $('#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)!='')
		   {
                        //alert('Hai If');
		    $('.prod_row').before(response).slideDown();
		    index++;
		    cmbn=1;
			 $('#loader').hide();
			}
			else
			{
                            //alert('Hai Else');
			// $('#attr_div').hide();
			  $('.stock_div').show(); $('.reorder_div').show();
			  cmbn=0;
			   $('#loader').hide();
			}
		 },
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		alert(textStatus);
		}
		});




                varprdtype = $('#ProductProdtype').val();
                if(varprdtype){
                    setTimeout('chkdigit(varprdtype)', 1000);

               // chkdigit(varprdtype);
                }
               
	return false;		
}


</script>
<script>
function validateFields(thisform)
{	

	
	 var i=0;
	 var j=0;
	 var fcs_fld=new Array();
	 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";  fcs_fld[i]='ProductCategoryId'; i++; }
	 if($.trim($('#ProductPname').val())=='') { errorstr += "- Please enter Product Name.\n"; fcs_fld[i]='ProductPname'; i++; }	 
	 /*
	 if(cmbn==0)
		{
		  if($.trim($('#ProductStock').val())=='') { errorstr += "- Please enter the Stock value.\n"; fcs_fld[i]='ProductStock'; i++; }		 
		  if($.trim($('#ProductReorderlevel').val())=='') { errorstr += "- Please enter the Reorder level.\n"; fcs_fld[i]='ProductReorderlevel'; i++; }		       
		}					
  	  if($.trim($('#ProductSkuno').val())=='') { errorstr += "- Please enter the product SKU No.\n"; fcs_fld[i]='ProductSkuno'; i++; }		 
	  */
	      /*Code for Image type Validation starts here */
			for(j=0;j<(imgcnt+1);j++)			  {
			   
				  if($('#pimage'+j+'').val()!='')
				  {
                                         if(!isNaN($('#pimage'+j+'').val())){
                                            var img_src=$('#pimage'+j+'').val();		

                                            var imgformats = ['.png','.gif','.jpg','.jpeg'];
                                                  if(img_src.length>0)
                                                     {
                                                            var fileType1 = checkImageType(img_src);
                                                                   if(fileType1)
                                                                            {
                                                                                  if(!inArray(imgformats,fileType1.toLowerCase()))
                                                                                          {
                                                                                                    errorstr += "- Please upload a valid File format.('png','gif','jpg','jpeg') for Product Image "+ (j+1) +".\n";
                                                                                                    img_src='';
                                                                                           }
                                                                             }
                                                     }
                                          }
					}		   
				}
			/*Code for Image type Validation starts here */					
		//if($.trim($('#ProductDescr').val())=='') { errorstr += "- Please enter Product Description.\n"; fcs_fld[i]='ProductDescr'; i++;}
		//if($.trim($('#ProductWholesalePrice').val())=='') { errorstr += "- Please enter Product Wholesale Price.\n"; fcs_fld[i]='ProductWholesalePrice'; i++; }
		if($.trim($('#ProductPrice').val())=='') { errorstr += "- Please enter Product Retail Price.\n"; fcs_fld[i]='ProductPrice'; i++; }
		
		if($.trim($('#ProductWholesalePrice').val())!='')
		{
			var wprice=$.trim($('#ProductWholesalePrice').val());
			var rprice=$.trim($('#ProductPrice').val());
			if(parseFloat(wprice) > parseFloat(rprice))
			{ errorstr += "- Wholesale Price should be less than or equal to Retail price.\n"; fcs_fld[i]='ProductPrice'; i++; }
	    }		
		
		//if($.trim($('#ProductProdtype').val())=='') { errorstr += "- Please Select Product Type.\n"; fcs_fld[i]='ProductProdtype'; i++; }
		
		if($.trim($('#ProductProdtype').val())=='Non-Digital') {
		if($.trim($('#ProductWeight').val())=='') { errorstr += "- Please enter Product Weight.\n"; fcs_fld[i]='ProductWeight'; i++; }		
		}		 
		 
	if($.trim($('#ProductDiscountType').val())=='amnt')   
        {
			   	var ret_price=$.trim($('#ProductPrice').val());
		    	var discnt=$.trim($('#ProductDiscount').val());   		
		        if(parseFloat(discnt) >= parseFloat(ret_price)) 
				{ errorstr += "- Discount amount should be smaller than retail price.\n"; fcs_fld[i]='ProductDiscount'; i++; }
        }        
        else if($.trim($('#ProductDiscountType').val())=='per')   
        {
			   	var discnt=$.trim($('#ProductDiscount').val());   		
		        if(parseFloat(discnt) >= 100) 
				{ errorstr += "- Discount percentage should be smaller than 100.\n"; fcs_fld[i]='ProductDiscount'; i++; }
        }
		
		
		if (errorstr != '')
		{
		  msgstr = msgstr + errorstr;
		  alert(msgstr);		
		  $('#'+fcs_fld[0]+'').focus();$('#'+fcs_fld[0]+'').select();
		  return false;
		}
	    else
	    {		
		  $('#ProductTotalimg').val(imgcnt);
		  thisform.submit();
		  return true;
	    }
} 

function validateRoot()
{
    if($.trim($('#ProductCategoryId').val())==1)
	{
	   alert("Please select a leaf level category.");
	   $('#ProductPname').val('');
	   return false;
	}  
}
</script>
<script>
/*for checking image extension*/
function getFileType(formobj,field) {
 var formElement = field;
 var filePath = field.value;
 var pathLength = filePath.length;
 var lastDot = filePath.lastIndexOf(".");
 var fileType = filePath.substring(lastDot,pathLength);
 return fileType;
 }
 
function inArray(array,val){
 for(var i = 0; i < array.length; i++)
 { 
  if(array[i] === val) return true; 
  } 
 return false; 
 } 
 
 
function checkImageType(path) 
{
 var filePath = path;
 var pathLength = filePath.length;
 var lastDot = filePath.lastIndexOf(".");
 var fileType = filePath.substring(lastDot,pathLength);
 return fileType;
 }		
 /*for checking image extension*/ 
 function setDiscountlbl(arg)	   
{
   var curncy='<?php echo $curlogo;?>';
   if(arg != '')
   {
     $('#disc_div').show(); 
	 if(arg=='per')
     $('#discount_lbl').html('Discount (%)');
     else if(arg=='amnt')
     $('#discount_lbl').html('Discount ('+curncy+')');
   }
   else
   $('#disc_div').hide(); 
}
</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 
									echo $form->input('category_id', array('type'=>'select', 'options'=>$tree->indentTree($categoriesList),'label' => '','class' => 'box2_admin',"OnChange"=>"javascript:loadCustomFields(this.value)"));
									?>						
								</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','onfocus' => 'javascript:validateRoot()','onchange' => 'javascript:validateRoot()')); ?>
								</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</p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input('stock', array('label' => '','class' => 'box2_admin','value' => 99999)); ?>
								</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</p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input('reorderlevel', array('label' => '','class' => 'box2_admin','value' => 5)); ?>
								</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">SKU No</p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input('skuno', 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 </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" id="pimage0">&nbsp;&nbsp;Set as Default Image
									 <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 200 x 200 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 </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">Product Wholesale price (<?php echo $curlogo; ?>) </p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input('wholesale_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">Product Retail price (<?php echo $curlogo; ?>) <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 Type </p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input("discount_type", 
									array("options" => array(""=>"-------------- Select ----------------","amnt"=>"Dollar","per"=>"Percentage"),
									"label" => "",'class' => 'box2_admin',"OnChange" => "return setDiscountlbl(this.value)"));?>
								</span>							
							</div>
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						  </div>
						  
						   
						   <div style="display:none;" id="disc_div">
						    <div id="grid_admin">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner" id="discount_lbl">Discount (<?php echo $curlogo;?>)</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>
						  
						  
						  
						  <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("Non-Digital"=>"Physical","Digital"=>"Digital"),
									"label" => "",'class' => 'box2_admin',"OnChange"=>"javascript:chkdigit(this.value);"));?>
								</span>							
							</div>
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						  </div>
						  
						  
						  <div id="digit" style="display:none;">
						   <div id="grid_admin">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner">Product File <span class="required_field">*</span></p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input('pdctfile', array('type' => 'file','label' => "",'class' => 'box2_admin')); ?>
								</span>		
								we support files with the following extentions only. <font color="#FF0000">jpg,pjpeg,jpeg,png,gif,zip,pdf,rar,psd<br>opf,azw,lit,pdb,pkg,mp3,mpeg,mp4,and avi</font>
                                                                <p>
                                                                    <b>Max. File Upload Size : <?php echo $max_upload; ?> Mb</b>
                                                                </p>
							</div>
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						  </div>
						  </div>
						  
					
						  <div id="prodwght">
						   <div id="grid_admin">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner">Product Weight(lbs)</p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php echo $form->input('weight', array('label' => '','class' => 'box2_admin','value' => 1)); ?>
								</span>		
							</div>
							<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
						     </div>
						  </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="maintext_inner">
									<?php echo $form->radio('Set as Featured Item', 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(document.Product);">
									<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 /> 
			
	<script>
	$('#ProductStock').numeric({allow:"."});
	$('#ProductReorderlevel').numeric({allow:"."});
	$('#ProductWholesalePrice').numeric({allow:"."});
	$('#ProductPrice').numeric({allow:"."});
	$('#ProductDiscount').numeric({allow:"."});
	$('#ProductWeight').numeric({allow:"."});
	</script>