<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: 			*/
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004-2010 ARMIA INC                                    |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts VistaCart                     |
// +----------------------------------------------------------------------+
// | Authors: Jipson<jipson.thomas@armiasystems.com>             		  |
?>
<?php echo $javascript->link('jquery.js'); ?>
<script>

function validateCat(id)
{
        $.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 category.\n\n"); 
		     $('#CategoryParentId').val(0);
			 return false;
		   }	 
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		alert(textStatus);
		}
		});		
}



function chk()
{	
    with(document.Category)
	{ 
		  var errorstr = '';
		  var msgstr = "Sorry, we cannot complete your request.\nKindly provide us the missing or incorrect information enclosed below.\n\n";
		  if($.trim($('#CategoryCname').val())=='') { errorstr += "- Please Enter Category Name.\n"; CategoryCname.focus();}
		  
		  var editid='';
		  editid='<?php echo $editid;?>';
		  if($.trim($('#CategoryParentId').val())== editid) { errorstr += "- Please Select a different Parent Category Name.\n"; 
		  CategoryCname.focus();
		  }
		  
		  if (errorstr != '')
		   {
			msgstr = msgstr + errorstr;
			alert(msgstr);
			return false;
		  }
		  else
		  {
				
				var sel_cnt=0;
				/*
				alert(customfields.length); 
				for(var i=0; i < customfields.length; i++)
				{				
				 
				 if(customfields[i].checked)
				 {				  
				 
				  sel_cnt++;
				 }
				}
				alert(sel_cnt);
				*/
				
				if(customfields.length === undefined)
				{
				  if(customfields.checked)
				  sel_cnt++;
				}
				else
				for(var i=0; i < customfields.length; i++)
				{							 
				 if(customfields[i].checked)
				 {						 
				    sel_cnt++;
				 }
				}  
				
				if(sel_cnt > 0)
				{
					var selectedValues = new Array();				
					if(customvalues.length === undefined)
					{
					  if(customvalues.checked)
					  selectedValues.push(customvalues.value);
					  CategoryCustomCombination.value=selectedValues;		
					}
					else
					{
						for(var i=0; i < customvalues.length; i++)
						{
						if(customvalues[i].checked)
						selectedValues.push(customvalues[i].value);
						}						
						CategoryCustomCombination.value=selectedValues;			
					}						
					return true;
				}	
				else
				CategoryCustomCombination.value='';							
		 }
	} 
} 
</script>
<script>
function ajax_validate(id)
{
 var rowid='row'+id;
 if($('#customfield'+id+'').is(':checked'))
 {
		$.ajax({
		type: "post",  // Request method: post, get
		url: "../showattributes/"+id, // URL to request		
		success: function(response)
		 {	
		   $('.showattrib').after(response);		   
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		alert(textStatus);
		}
		});
		return false;
 }
 else
  $('.'+rowid+'').remove();
}
</script>



<div align="center" class="subheading">
              <div align="left">Category</div>
            </div>
            <br />
	    <label align="center"><font color="#FF0000"><?php $session->flash(); ?></font> <br /></label>
			
            <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('Category', array('name' =>'Category'));?>
                  <div id="admin_heading">Edit Category </div>
				  
						  <div id="grid_admin">
							<div align="left" id="inner_leftcontent">
							  <p class="maintext_inner">Parent Category  <span class="required_field">*</span></p>
							</div>
							<div align="right" id="inner_rightcontent">
								<span class="toplinks">
									<?php 
									echo $form->input('parent_id', array('type'=>'select', 'options'=>$tree->indentTree($categoriesList),'label' => '','class' => 'box2_admin','onchange' => 'javascript:validateCat(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">Category Name  <span class="required_field">*</span></p>
								</div>
								<div align="right" id="inner_rightcontent">
									<span class="toplinks">								    
									<?php echo $form->input('cname', 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="showattrib">
								<div align="left" id="inner_leftcontent">
								  <p class="maintext_inner">Category Attributes</p>
								</div>
								<div align="right" id="inner_rightcontent">
									<span class="toplinks">									
										 
												<?php 
												$i=1; 
												 foreach($cfields as $cfield_id=>$cvalue)
												{
												?>
												<div style="float:left " class="maintext_inner">
												<input type="checkbox" name="customfields" value="<?php echo $cfield_id;?>"  id="customfield<?php echo $cfield_id;?>"
												<?php 
												if(isset($selected_cfields) && !empty($selected_cfields))
												{
												if(in_array($cfield_id,$selected_cfields)) 
												echo "checked"; 
												}?> 
												onClick="javascript:ajax_validate(this.value);"><?php echo $cvalue;?>
												</div>
												<?php  if(($i%3)==0) {?>
												<br />
												<?php  }$i++; } ?>
												
									</span>
								</div>
								<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>								
								</div>
								
								
								
								
								
								
						<?php
							$i=0;
							if(isset($selected_cfields) && !empty($selected_cfields))
							{
							foreach($selected_cfieldvalues as $key => $val)
								{	?>
																
							<div id="grid_admin" class="row<?php echo $key;?>">
								<div align="left" id="inner_leftcontent">
								  <p class="maintext_inner">Select <?php echo $selected_cfieldnames[$i];?></p>
								</div>
								<div align="right" id="inner_rightcontent">
									<span class="toplinks">								    
									
									 <div style="float:left " class="maintext_inner">
										<?php  foreach($val as $key1 => $val2)	 
										  { 
										  ?>
										   <input type="checkbox" name="customvalues" value="<?php echo $key1;?>"  <?php if(in_array($key1,$selected_cvalues)) echo "checked"; ?> >
										   <?php echo $val2;?>
										   <?php 
										  }  ?>
										</div> 
									</span>
								</div>
								<div align="left" class="float_clear1"><?php echo $html->image('/img/clear.gif', array('width' => 1, 'height' => 1));?></div>
								</div>
                           <?php  $i++; } }   ?>
								
								
								
								<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 chk();">
									<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('customfield_id', array('type' => 'hidden')); ?>
                                <?php echo $form->input('custom_combination', array('type' => 'hidden')); ?>
								
				 <?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 /> 