<SCRIPT language=Javascript>
    $(document).ready(function() {
        $.mask.definitions['A'] = "[A-Za-z0-9]";

        $('select').select2();
        $.each($(".select2-container"), function(i, n) {
            $(n).next().show().fadeTo(0, 0).height("0px").css("left", "auto"); // make the original select visible for validation engine and hidden for us
            $(n).prepend($(n).next());
            $(n).delay(500).queue(function() {
                $(this).removeClass("validate[required]"); //remove the class name from select2 container(div), so that validation engine dose not validate it
                $(this).dequeue();
            });
        });
        $("input.mask_account").mask('AAAA-AAA-AA');
        jQuery('.datepicker2').datepicker({
            dateFormat: 'dd-mm-yy',
            changeMonth: true,
            changeYear: true
        });
		
		getAcctGroupCode();
    });


    function addDash() {

    }

    function getRelatedChartOfAcct() {
        $.ajax({
            type: "POST",
            data: {
                grpID: $("#acctGroupID").val()
            },
            url: "<?php echo base_url() ?>glsetting/getRelatedChartOfAcct",
            success: function(data) {
                $('#chartOfAcct').empty();
                var resul = eval("(" + data + ")");
                var n = resul.recordSet.length;
                var i = 0;
                var opt = '' +
                        '<option></option>';
                for (i = 0; i < n; i++)
                    opt = opt + '<option value="' + resul.recordSet[i].ID + '">' + resul.recordSet[i].acctCode + '</option>';

                $('#chartOfAcct').append(opt);

                //  $('#chartOfAcct').empty();
                //        $('#chartOfAcct').append(opt);

            }
        });
    }

    function getAcctGroupCode() {
        var c = $("#chartOfAcct option:selected").text();
        //  $("#acctCode").val(c.substring(0,4));
        if (!c)
            $("input.mask_account").mask('AAAA-AAA-AA');
        else {
            if (c.substring(5, 8) == '000')
                $("input.mask_account").mask(c.substring(0, 4) + '-AAA-AA');
            else
                $("input.mask_account").mask(c.substring(0, 4) + '-' + c.substring(5, 8) + '-AA');
        }
        //  $("#acctCode").val(c.substring(0,4));
    }

    function checkIfAcctExist() {
        $.ajax({
            type: "POST",
            data: {
                acctCode: $("#acctCode").val()
            },
            url: "<?php echo base_url() ?>glsetting/checkIfAcctExist",
            success: function(data) {
                if ($.trim(data) !== 'notFound') {
                    $("#acctCode").val("");
                    $("#acctCode").attr("placeholder", "Dupplicate not allowed");
                }
            }
        });
    }

	function deleteChartOfAcct(obj){
		var urls = "<?php echo base_url();?>glsetting/chartDelete/<?php echo $acctID;?>";
			window.location = urls;
	} 
</script>
<div id="content"> 
	<div class="wrap">
        <div class="head">
            <div class="info">
                <!--<h1><?php echo element('compName', $this->session->userdata('logged_in')) ?> [ 
					<?php echo element('compNo', $this->session->userdata('logged_in')) ?> ]</h1>-->
                <ul class="breadcrumb">
                    <li><a href='<?php echo base_url() . "home" ?>'><?php echo $this->lang->line('dashboard') ?></a> <span class="divider">-</span></li>
                    <li><a href='<?php echo base_url() . "GeneralLedger" ?>'> <?php echo $this->lang->line('module') ?></a> <span class="divider">-</span></li>
                    <li class="active"><?php echo $this->lang->line('title_coa') ?></li>
                </ul>
            </div>
            <div class="search">
                <form action="<?php echo base_url() ?>admin/search" method="post">
                    <input name="search_text" type="text" placeholder="search..."/>                                
                    <button type="submit"><span class="i-magnifier"></span></button>
                </form>
            </div>
        </div><!-- head --> 
        <div class="content" id="grid_content_1">
					<div class="block">
						<div class="head">
							<h2><strong><?php echo $this->lang->line('title_coa') ?></strong></h2>
						</div><!-- head -->
						<div class="content np">
                                                    <div class="row-fluid scRow">    
                                                         <?php
                                    $hidden = array('order_time' => date('Y-m-d H-i-s'));
                                    $attrib = array('id' => 'validate');
                                    echo form_open('', $attrib, $hidden);
                                    ?>
                                    <!-- form id="validate" method="POST" action="javascript:alert('Form #validate submited');"-->
                                    <div class="block">
                                        <?php
                                        $data = array(
                                            'name' => 'acctCodeID',
                                            'id' => 'acctCodeID',
                                            'type' => 'hidden',);
                                        if ($acmaster) {
                                            echo form_input($data, $acmaster->ID);
                                        } else {
                                            echo form_input($data);
                                        }
                                        ?>
                                        <div class="content np">
                                            <div class="controls-row">
                                                <div class="span2"><?php echo $this->lang->line('acctGroup') ?></div>

                                                <div class="span10">
                                                    <?php
                                                    $preselcat = ($acmaster) ? $acmaster->acctGroupID : '';

                                                    //$options[] = '';//$preselcat;
                                                    //$options[] = "";
													$options[4] = $this->lang->line('CurrentLiabilities');
                                                    foreach ($accGroup as $row) {
                                                        $options[$row->ID] = $this->lang->line($row->acctGroupName); //.' | '.$row->acctGroupName;
                                                    }
                                                    $attr = 'class="span5" id="acctGroupID" onchange="getRelatedChartOfAcct()"';
                                                    echo form_dropdown('acctGroupID', $options, $preselcat, $attr);
                                                    ?>       
                                                </div>                

                                            </div>
                                            <div class="controls-row">
                                                <div class="span2"><?php echo $this->lang->line('chartOfAcct') ?></div>

                                                <div class="span10">
                                                    <?php
                                                    //$preselcat = ($acmaster)?$acmaster->acctGroupID:'';
                                                    //$options[] = '';//$preselcat;
                                                    //$options1[] = "";
													$options1[31296] = "3000-000-00 | Trade Payable";
                                                    if ($chargeAccount)
                                                        foreach ($chargeAccount as $row1) {
                                                            $options1[$row1->ID] = substr($row1->acctCode, 0, 4) . '-' . substr($row1->acctCode, 4, 3) . '-' . substr($row1->acctCode, 7) . " | " . str_replace(array("\n", "\r", "'"), " ", $row1->acctName, $count); //.' | '.$row->acctGroupName;
                                                        }
                                                    $attr = 'class="span5" id="chartOfAcct" onchange="getAcctGroupCode()"';
                                                    echo form_dropdown('chartOfAcct', $options1, '', $attr);
                                                    ?>       
                                                </div>                

                                            </div>

                                            <div class="controls-row">
                                                <div class="span2"><?php echo $this->lang->line('acctCode') ?></div>
                                                <div class="span10">
                                                    <?php
                                                    $data = array(
                                                        'name' => 'acctCode',
                                                        'id' => 'acctCode',
                                                        'class' => 'span5 mask_account validate[required,maxSize[11]]',);
                                                    if ($acmaster) {
                                                        echo form_input($data, $acmaster->acctCode);
                                                    } else {
                                                        echo form_input($data, set_value('acctCode'), "onblur='checkIfAcctExist()'");
                                                    }
                                                    ?>
                                                </div>
                                            </div>
                                            <div class="controls-row">
                                                <div class="span2"><?php echo $this->lang->line('acctName') ?></div>
                                                <div class="span10">
													<?php
                                                    $data = array(
                                                        'name' => 'acctName',
                                                        'id' => 'acctName',
                                                        'class' => 'span5 validate[required, maxSize[32]]',
                                                        'value' => ($acmaster) ? $acmaster->acctName : "");
                                                    echo form_input($data, set_value('acctName'));
                                                    ?>                                                                                        
                                                </div>
                                            </div>                                        
                                        </div>
                                        <div class="footer">
                                            <div class="side fr">
                                                <div class="btn-group">
														<?php
														if(!empty($checkUpdateable) && ($checkUpdateable[0]->updateable) <> 1){
                                                        	$data = array(
                                                           		'name' => 'remove',
                                                            	'id' => 'remove',
                                                            	'class' => 'btn-form btn-danger-form remove',
                                                            	'onClick' => "deleteChartOfAcct()");
																echo form_button($data, 'Delete', 'disabled');
														}
														else{
															$data = array(
                                                           		'name' => 'remove',
                                                            	'id' => 'remove',
                                                            	'class' => 'btn-form btn-danger-form remove',
                                                            	'onClick' => "deleteChartOfAcct()");
																echo form_button($data, 'Delete');
														}
                                                        $data = array(
                                                            'name' => 'validat',
                                                            'class' => 'btn-form btn-warning-form',
                                                            'onClick' => "$('#validate').validationEngine('hide');");
                                                        echo form_button($data, $this->lang->line('hideprompt'));
                                                        $data = array(
                                                            'name' => 'submit',
                                                            'id' => 'submit',
                                                            'class' => 'btn-form btn-success-form');
                                                        echo form_submit($data, $this->lang->line('submit'));
                                                        ?>
                                                </div>
                                            </div>
                                        </div>                                    
                                    </div>
                                    </form>
                                                    </div>
                                                    </div>
                                                </div>
                </div>
        </div>
    </div>
                                                


