function getHTTPObject() {
  var xmlhttp;

  if(window.XMLHttpRequest){
      xmlhttp = new XMLHttpRequest();
  }else if (window.ActiveXObject){
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      if (!xmlhttp){
          xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
      }
  }else alert("Browser tidak mendukung ajax!");
  return xmlhttp;
}

var http = getHTTPObject(); // We create the HTTP Object

function requestInfo(url,id,redirectPage) {
    var temp=new Array();
    http.open("GET", url, true);
    http.onreadystatechange = function() {
        if (http.readyState == 4) {
            var text      = '<div align="center" style="padding:50px;"><img style="position:relative;top:3px;" src="images/loading6.gif" alt="Loading..."> Loading...</div>';
            document.getElementById(id).innerHTML = text;
            if(http.status==200) {
                var results=http.responseText;
				if(redirectPage=="" || results!="1") {
                   var temp=id.split("~"); // To display on multiple div
                   //alert(temp.length);
                   var r=results.split("~"); // To display multiple data into the div
                   //alert(temp.length);
                   if(temp.length>1) {
                       for(i=0;i<temp.length;i++) {
                           //alert(temp[i]);
                           document.getElementById(temp[i]).innerHTML=r[i];
                       }
                   } else {
                       document.getElementById(id).innerHTML = results;
                   }
    			} else {
                   //alert(results);
                   window.location.href=redirectPage;
                }
            }
        }
    };
    http.send(null);
}

function emptyValidation(fieldList) {
    var field=new Array();
    var field2=new Array();
    field=fieldList.split("~");
    var counter=0;
    for(i=0;i<field.length;i++) {
        if(document.getElementById(field[i]).value=="") {
            document.getElementById(field[i]).style.backgroundColor="#FF0000";
            field2[i]=field[i];
            counter++;
        } else {
            document.getElementById(field[i]).style.backgroundColor="#FFFFFF";
        }
    }
    
    for(i=0;i<field.length;i++) {
        if(document.getElementById(field[i]).value=="-" || field2[i]==field[i]) {
            document.getElementById(field[i]).style.backgroundColor="#FF0000";
            counter++;
        } else {
            document.getElementById(field[i]).style.backgroundColor="#FFFFFF";
        }
    }

    if(counter>0) {
        alert("Field dengan warna merah tidak boleh kosong!");
        return false;
    }  else {
        return true;
    }
}

function checkAll(string,chkid) {
    var field=new Array();
    field=chkid.split("~");
    for(i=0;i<field.length;i++) {
        if(string=="true") document.getElementById(field[i]).checked=true;
        else document.getElementById(field[i]).checked=false;
    }
}

function MultipleActions(theLink,chkid,div,divout) {
    var field=new Array();
    var nilai=new Array();
    var jml=0;
    var lnk="";
    field=chkid.split("~");
    for(i=0;i<field.length;i++) {
        if(document.getElementById(field[i]).checked==true){
            jml=jml+1;
            nilai[jml]=document.getElementById(field[i]).value;
            lnk=lnk+'&'+field[i]+'='+nilai[jml];
        }
    }
    if(jml==0) alert('Tidak ada data yang dicheklist!');
    else requestURL2(theLink,lnk+'&jml='+field.length,div,divout);
}

function requestURL(theLink,search,div){
    var url=theLink+search;
    var ajaxDisplay = document.getElementById(div);
    ajaxDisplay.style.visibility='visible';
    http.open("GET",url,true);
    http.onreadystatechange=function(){
        document.getElementById(div).innerHTML = "<div style='text-align:center'><img src='http://server2/timesheet2/images/loading6.gif' border=0> Proses...</div>";
        if(http.readyState==4){
            if(http.responseText) ajaxDisplay.innerHTML=http.responseText;
        }
    }
    http.send(null);
}

function requestURL2(theLink,search,div,out){
    var url=theLink+search;
    var divout = "<div>"+out+"</div>";
    var ajaxDisplay = document.getElementById(div);
    ajaxDisplay.style.visibility='visible';
    http.open("GET",url,true);
    http.onreadystatechange=function(){
        document.getElementById(div).innerHTML = divout;
        if(http.readyState==4){
            if(http.responseText) ajaxDisplay.innerHTML=http.responseText;
        }
    }
    http.send(null);
}

function selectChange(theLink,search,div){
    var url=theLink+search;
    http.open("GET",url,true);
    http.onreadystatechange=function(){
        document.getElementById(div).innerHTML = "<div style='text-align:center'><img src='http://server2/timesheet2/images/loading6.gif' border=0> Proses...</div>";
        if(http.readyState==4){
            var ajaxDisplay = document.getElementById(div);
            if(http.responseText) ajaxDisplay.innerHTML=http.responseText;
        }
    }
    http.send(null);
}

function show(id) {
    var div=document.getElementById(id);
    div.style.visibility='visible';
}

function hide(id) {
    var div = document.getElementById(id);
    div.style.visibility='hidden';
    div.style.innerHTML='';
}

function log(){
    var url='login';
    var ajaxDisplay = document.getElementById('maindiv');
    ajaxDisplay.style.visibility='visible';
    http.open("GET",url,true);
    http.onreadystatechange=function(){
        document.getElementById('maindiv').style.innerHTML = "<div style='text-align:center'><img src='http://localhost/manpower/images/loading6.gif' border=0> Proses...</div>";
        if(http.readyState==4){
            if(http.responseText) ajaxDisplay.innerHTML=http.responseText;
        }
    }
    http.send(null);
}

function infontot(theLink) {
    var start=document.getElementById("start").value;
    var checkValidation=emptyValidation('start');
    if(checkValidation==true) {
        var submit=document.getElementById("submit").disabled=false;
        requestURL(theLink,'&tanggal='+start,'ntot');
    }else var submit=document.getElementById("submit").disabled=true;
}

function cntot(value,theLink,div){
    var start=document.getElementById("start").value;
    var checkValidation=emptyValidation('start');
    if(checkValidation==true) {
        var submit=document.getElementById("submit").disabled=false;
        requestURL(theLink,'&tanggal='+start+'&nilai='+value,div);
    }else var submit=document.getElementById("submit").disabled=true;
}

function searchtimesheet(theLink) {
    var bln=document.getElementById("bln").value;
    var thn=document.getElementById("thn").value;
    var checkValidation=emptyValidation('bln~thn');
    if(checkValidation==true) {
        requestURL(theLink,'&bln='+bln+'&thn='+thn,'rightdiv');
    }
}

function note(theLink,div,divout) {
    var id=document.getElementById("id").value;
    var note=document.getElementById("note").value;
    var checkValidation=emptyValidation('note');
    if(checkValidation==true) {
        requestURL(theLink,'&id='+id+'&note='+note,div,divout);
    }
}

function address(theLink,div,divout) {
    var alamat=document.getElementById("alamat").value;
    var telp=document.getElementById("telp").value;
    var checkValidation=emptyValidation('alamat~telp');
    if(checkValidation==true) {
        requestURL2(theLink,'&alamat='+alamat+'&telp='+telp,div,divout);
    }
}

function pass(theLink,div,divout) {
    var pass1=document.getElementById("pass1").value;
    var pass2=document.getElementById("pass2").value;
    var pass3=document.getElementById("pass3").value;
    var checkValidation=emptyValidation('pass1~pass2~pass3');
    if(checkValidation==true) {
        if(pass2!=pass3) alert('Password baru tidak sama!');
        else requestURL2(theLink,'&pass1='+pass1+'&pass2='+pass2+'&pass3='+pass3,div,divout);
    }
}

function execute_search(theLink) {
    var search=document.getElementById("search").value;
    var filter=document.getElementById("filter").value;
    var checkValidation=emptyValidation('search~filter');
    if(checkValidation==true) {
        requestURL(theLink+'&search='+search+'&filter='+filter,'','rightdiv');
    }
}

function confirmLink(theLink){
    var is_confirmed = confirm('Are you sure to delete this record?\n\nThis will permanently delete the Record!');
    if (is_confirmed) {
        theLink.href += '';
    }

    return is_confirmed;
}
