var myTable;
$(document).ready(function () {
    
    var newsid = getUrlVars()["newsid"];
    var imagename = getUrlVars()["imagename"];
    if(newsid != undefined && imagename != undefined)
    {
        alert("save image data");
        var inData = {};
        inData.imageurl = "http://clients.quickloc8.co.za/"+imagename;
        inData.newsid = newsid;
        util.savenewsimage(inData);
    }

    $( "#startdate" ).datepicker(
        {
            dateFormat: 'dd-MM-yy'
        }
    );

    $( "#expirydate" ).datepicker(
        {
            dateFormat: 'dd-MM-yy'
        }
    );
      

    myTable = $('#table-news').DataTable({});
    
    var user = sessionStorage.getItem('user');
    if (user == null || user == undefined) {
        window.location = "index.html";
        // return;  
    }

    /*$("input[name=startdate]").on("keyup change", function(){            
        this.value = createMask(this.value);
    });

    $("input[name=expirydate]").on("keyup change", function(){            
        this.value = createMask(this.value);
    });*/

    util.getnews(function(thenews){
        setnews(thenews);
    });

    $("#btn_save").click(function() {
        var div = document.getElementById("errorBody");
        var headline = $("#headline").val().trim();
        var news = $("#news").val().trim();
        var startdate = $("#startdate").val().trim();
        var expirydate = $("#expirydate").val().trim();

        var isError =  false;
        var error = "Please enter : ";
        if(headline == undefined || headline == "")
        {
            isError = true;
            error = error+"<br/>Headline";
        }
        if(news == undefined || news == "")
        {
            isError = true;
            error = error+"<br/>News";
        }
        if(startdate == undefined || startdate == "")
        {
            isError = true;
            error = error+"<br/>Start Date";
        }
        else{
            if(!isValidDate(startdate))
            {
                isError = true;
                error = error+"<br/>Valid Start Date";
            }
        }
        if(expirydate == undefined || expirydate == "")
        {
            isError = true;
            error = error+"<br/>End Date";
        } 
        else{
            if(!isValidDate(expirydate))
            {
                isError = true;
                error = error+"<br/>Valid End Date";
            }
        }

        if(isError)
        {
            addAlertDiv(div, "warning", error);
            return;
        }
        var newsdata = {};
        newsdata.headline = headline;
        newsdata.news = news;
        newsdata.startdate = startdate;
        newsdata.expirydate = expirydate;
        console.log(newsdata);
        util.savenews(newsdata,function(thenews){
            alert(thenews.result);
            $("#newsid").val(thenews.newsid);
            $("#frmNews").submit();
            //document.getElementById("myForm").submit();
            //window.location = "news.html";
        });
        
    });

});

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,    
    function(m,key,value) {
      vars[key] = value;
    });
    return vars;
  }


function addAlertDiv(div, type, msg) {
    div.innerHTML = "";
    div.innerHTML +=
      '<div class="alert alert-' + type + ' role="alert">' + msg + "</div>";
  }

  function setnews(news)
  {      
    console.log(news);
    myTable.clear();
    for(i=0;i<news.length;i++)
    {
        var row = [];
        row.push(news[i].headline);
        row.push(news[i].news.substring(0,60));
        row.push(news[i].startdate);
        row.push(news[i].expirydate);
        myTable.row.add(row);
    }
    myTable.draw();  
  }

  function isValidDate(theDate)
   {
       var _date = theDate.split("/");
       //console.log(_date);
       theDate = _date[2]+"-"+_date[1]+"-"+_date[0];
       var date = new Date(theDate);
       //console.log(date);
       if(date != "Invalid Date")
       {
           return true;
       }
       else{
           return false;
       }
   }
