// JavaScript Document
var addrow;



//the common itration and the start // 2
$aw_i = 2;

//the tabindex to increment in dynmaic load
$tabindexnum = 15;



jQuery(document).ready(function () {

    var spryName = new Array();
    var spryPhone = new Array();


    //to start the itration with default value //

    var itr = document.getElementById("hidden_itr").value;



    spryName[0] = "yes";
    spryPhone[0] = "yes";

    var h;
    for (h = 1; h <= itr; h++)
    {


        spryName[h] = new Spry.Widget.ValidationTextField("spryName" + h);
        spryPhone[h] = new Spry.Widget.ValidationTextField("spryPhone" + h);

    }


    //add new row in bill
    //on click the add button
    /************************/
    $("#addpro").click(function () {

        $aw_i = document.getElementById("hidden_itr").value;
        $aw_i++;

        addrow($aw_i, $tabindexnum);

        // fill the iltration filed with common iltration

        spryName[$aw_i] = new Spry.Widget.ValidationTextField("spryName" + $aw_i);
        spryPhone[$aw_i] = new Spry.Widget.ValidationTextField("spryPhone" + $aw_i);


        document.getElementById("hidden_itr").value = $aw_i;

        // incrementaion of row number and tab index
        $tabindexnum++;


        return false;

    });
    /************************/




    //add new row in bill
    /**************************/
    addrow = function ($aw_if, $tabindexnumf)
    {


        $xx = '';
        $xx += '<tr>';


        $xx += '<td>'
                + '<span id="spryName' + $aw_if + '">'
                + '  <input type="text" name="representativeName' + $aw_if + '" id="representativeName' + $aw_if + '" class="form-control txt-nonraduis enteraction" tabindex="' + $tabindexnumf + '"   placeholder="اسم ال{if $Programsettingdata->delegate_seller eq 0} بائع {elseif $Programsettingdata->delegate_seller == 1}مندوب {elseif $Programsettingdata->delegate_seller == 2}طبيب {/if}" />'
                + '<span class="textfieldRequiredMsg">*</span></span>'
                + '</td>';




        $xx += '<td>'
                + '<span id="spryPhone' + $aw_if + '">'
                + '<input type="text" id="representativePhone' + $aw_if + '" name="representativePhone' + $aw_if + '" class="form-control txt-nonraduis positiveNo enteraction"  placeholder="رقم التليفون" tabindex="' + $tabindexnumf + '" />'
                + ' <span class="textfieldRequiredMsg">*</span></span>'
                + '</td>';



        $xx += '<td>'
                + '<textarea class="form-control txt-nonraduis enteraction" name="otherInfo' + $aw_if + '" id="otherInfo' + $aw_if + '" rows="1" tabindex="' + $tabindexnumf + '"></textarea>'
                + '</td>';



        $xx += '<td id="' + $aw_if + '">'
                + '	<button id="remove' + $aw_if + '" type="button" class="close flt-none removeicon removeproduct" aria-hidden="true">&times;</button>'
                + '</td>'
                + '</tr>';



        $("#containerpros").append($xx);

        $(".positiveNo").forcepositiveNum();



    }
    /**************************/





    // the delete action that delete row of products
    /************************/
    $(document).on('click', '.removeproduct', function () {

        $itemRemove = Number($(this).parent().attr("id"));


        spryName[$itemRemove].reset();
        spryName[$itemRemove].destroy();

        spryPhone[$itemRemove].reset();
        spryPhone[$itemRemove].destroy();


        $("#representativeName" + $itemRemove).val("yes");
        $("#representativePhone" + $itemRemove).val("yes");



        // delete by partents
        $(this).parent().parent().remove();



        return false;
    });
    /************************/





    //use enter action do new row

    $(document).on('keypress', ".enteraction", function (e) {


        if (e.which == 13) {

            //add the new row with itration and with all propperties
            addrow($aw_i, $tabindexnum);


            document.getElementById("hidden_itr").value = $aw_i;

            spryName[$aw_i] = new Spry.Widget.ValidationTextField("spryName" + $aw_i);
            spryPhone[$aw_i] = new Spry.Widget.ValidationTextField("spryPhone" + $aw_i);


            // incrementaion of row number and tab index
            $aw_i++
            $tabindexnum++;


            return false;
        }

    });
    /*****************************/

    //to use enter and click to submit the form
    /*******************************/
    $("#billsure").keypress(function (e) {
        if (e.which == 13) {

            $('#billform').submit();
        }
    });
    /******************************/


    // using esc to delete row
    /*****************************/
    $(document).keypress(function (e) {

        var code = (e.keyCode ? e.keyCode : e.which);
        //to stop the submit of the form
        if (e.which == 13) {

            e.preventDefault();
        }
        if (code == 114)
        {
            //print_Custom('mycustomheader');
            //this.print(false);
            //return false;
        }
        if (code == 27) {

            $trno = $("#containerpros tr").length - 1;
            //alert($trno);
            if ($trno != 1)
            {
                // delete by partents
                $('#containerpros tr:last').remove();



            }
            return false;
        }
    });
    /*******************************/


});




function deleteRepresentative()
{

    $itemRemove = Number($(this).parent().attr("id"));



    spryName[$itemRemove].reset();
    spryName[$itemRemove].destroy();

    spryPhone[$itemRemove].reset();
    spryPhone[$itemRemove].destroy();


    $("#representativeName" + $itemRemove).val("yes");
    $("#representativePhone" + $itemRemove).val("yes");

    $representativecompanyId = $("#representativecompanyId" + $itemRemove).val();

    $.ajax({
        type: 'POST',
        url: 'shippercompaniesAjaxController.php?do=deleteRepresentative&representativecompanyId=' + $representativecompanyId + '',
        success: function (data) {

            // delete by partents
            $("#representativePhone" + $itemRemove).parent().parent().parent().remove();

            return false;


        }
    });






}
