<?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 language="javascript" type="text/javascript">

  $(document).ready(function()   
  {
     //Clearing the Check box while Page Refreshing...
        <?php   if(!empty($cfields)) {?>	
         $("input[name='customfields']").each(function(i, j){
             if($(j).attr('checked')){
                 $(j).attr('checked', false);
             }
         });
	<?php } ?>
  });


function validateCat(id)
{
        $.ajax({
		type: "post",  // Request method: post, get
		url: "checkcategory/"+id, // URL to request		
		success: function(response)
		 {	
		   var res=$.trim( response);
		   if(res=='yes')
		   {		   
			 alert("Products exists under this category.\nPlease select a different category.\n\n"); 
		     $('#CategoryParentId').val(0);
			 return false;
		   }	 
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		alert(textStatus);
		}
		});		
}


function validateFields()
{	
    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();}
		  if (errorstr != '')
		   {
			msgstr = msgstr + errorstr;
			alert(msgstr);
			return false;
		  }
		  else
		  {
		       /* 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;
		  }
	} 
} 


function getAttributes(id)
{
   $('#loader').show();
   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)
		 {	
		    $('#loader').hide();
		    $('.showattrib').after(response);		   
		},
		error:function (XMLHttpRequest, textStatus, errorThrown) {
		alert(textStatus);
		}
		});		
    }
    else
	{
       $('#loader').hide();
	   $('.'+rowid+'').remove();
	}   
	return true;
}


</script>



<div align="center" class="subheading">
              <div align="left">Category</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('Category', array('name' =>'Category'));?>
                  <div id="admin_heading">Add 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>
								
						
						      <?php
                                 if(empty($cfields)==false) {?>
								
								
								<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">
								
								<div style="padding:5px 0px 0px 100px;float:right;display:none;" id="loader"><?php echo $html->image('/img/load.gif');?></div>
									<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;?>" onClick="javascript:return getAttributes(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 } ?>
								
								
								
								
								<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"  id="submit_btn" 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('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 /> 