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");
      }
  }
  return xmlhttp;
}

var http = getHTTPObject(); // We create the HTTP Object


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 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 = "<img src='images/loading6.gif' border=0>";
        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 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;
}

function hide(id) {
    var div=document.getElementById(id);
    div.style.visibility='hidden';
}

function logreg(){
    var ajaxDisplay = document.getElementById('maindiv');
    ajaxDisplay.style.visibility='visible';
    http.open("GET",'logreg',true);
    http.onreadystatechange=function(){
        document.getElementById('maindiv').innerHTML = "<img src='images/loading6.gif' border=0>";
        if(http.readyState==4){
            if(http.responseText) ajaxDisplay.innerHTML=http.responseText;
        }
    }
    http.send(null);
}

function forgot_password(theLink) {
    var email=document.getElementById("email").value;
    var checkValidation=emptyValidation('email');
    if(checkValidation==true) {
        requestURL(theLink,'&email='+email,'maindiv');
    }
}

function sign_up1(theLink) {
    var username=document.getElementById("username").value;
    var pass1=document.getElementById("pass1").value;
    var pass2=document.getElementById("pass2").value;
    var checkValidation=emptyValidation('username~pass1~pass2');
    if(checkValidation==true){
        if(username.length<5) alert("Username harus lebih dari 5 karakter!");
        else if(pass1!=pass2) alert("Password tidak sama!");
        else requestURL(theLink,'&username='+username+'&pass1='+pass1+'&pass2='+pass2,'maindiv');
    }
}

function sign_up2(theLink) {
    var user=document.getElementById("user").value;
    var pass1=document.getElementById("pass1").value;
    var pass_ses=document.getElementById("pass_ses").value;
    var randx=document.getElementById("randx").value;
    var nama=document.getElementById("nama").value;
    var profile=document.getElementById("profile").value;
    var alamat=document.getElementById("alamat").value;
    var aliran=document.getElementById("aliran").value;
    var telp=document.getElementById("telp").value;
    var email=document.getElementById("email").value;
    var term=document.getElementById("term");

    var checkValidation=emptyValidation('user~pass1~pass_ses~randx~nama~profile~aliran~alamat~telp~email');
    if(term.checked){
        if(checkValidation==true) requestURL(theLink,'&user='+user+'&pass1='+pass1+'&pass_ses='+pass_ses+'&randx='+randx+'&nama='+nama+'&profile='+profile+'&alamat='+alamat+'&aliran='+aliran+'&telp='+telp+'&email='+email,'maindiv');
    }else{
        var submit=document.getElementById("submit").disabled=true;
        document.getElementById("submit").style.color="#ccc";
        alert('Anda belum menyetujui Term of Service CO/Band!');
    }
}

function sign_riwee(theLink) {
    var nama=document.getElementById("nama").value;
    var profile=document.getElementById("profile").value;
    var aliran=document.getElementById("aliran").value;
    var manager=document.getElementById("manager").value;
    var alamat=document.getElementById("alamat").value;
    var telp=document.getElementById("telp").value;
    var web=document.getElementById("web").value;
    var email=document.getElementById("email").value;
    var capt=document.getElementById("capt").value;

    var checkValidation=emptyValidation('nama~profile~aliran~manager~alamat~telp~email~capt');
    if(checkValidation==true) requestURL(theLink,'&nama='+nama+'&profile='+profile+'&aliran='+aliran+'&manager='+manager+'&alamat='+alamat+'&telp='+telp+'&web='+web+'&email='+email+'&capt='+capt,'maindiv');
}

function comment(theLink) {
    var testimonial=document.getElementById("testimonial").value;
    var capt=document.getElementById("capt").value;
    var checkValidation=emptyValidation('testimonial~capt');
    if(checkValidation==true) requestURL(theLink,'&testimonial='+testimonial+'&capt='+capt+'&homecomment=true','comment');
}

function capt(theLink) {
    var capt=document.getElementById("capt").value;
    var checkValidation=emptyValidation('capt');
    if(checkValidation==true) requestURL(theLink,'&capt='+capt,'captarea');
}

function optSearch(theLink,out){
    var search=document.getElementById("txtsearch").value;
    var checkValidation=emptyValidation('txtsearch');
    if(checkValidation==true) {
        requestURL2(theLink,'&search='+search,'viewsearch',out);
    }
}

function FilterLogin(theLink,search,div,out) {
    var divout = "<div>"+out+"</div>";
    var submit=document.getElementById("submit").disabled=true;
    document.getElementById("submit").style.color="#ccc";
}

function FilterLogin2(theLink,search,div,out) {
    var divout = "<div>"+out+"</div>";
    var username=document.getElementById("user").value;
    var password=document.getElementById("pass").value;

    if(username!=""){
        var submit=document.getElementById("submit").disabled=false;
        document.getElementById("submit").style.color="#000";
    }
}

function FilterReg1(theLink,search,div,out) {
    var divout = "<div>"+out+"</div>";
    var submit=document.getElementById("submit").disabled=true;
    document.getElementById("submit").style.color="#ccc";
}

function FilterReg1a(theLink,search,div,out) {
    var divout = "<div>"+out+"</div>";
    var usr=document.getElementById("usr").value;

    if(usr!=""){
        var submit=document.getElementById("submit").disabled=false;
        document.getElementById("submit").style.color="#000";
    }
}

function FilterReg2(theLink,search,div,out) {
    var divout = "<div>"+out+"</div>";
    var submit=document.getElementById("submit").disabled=true;
    document.getElementById("submit").style.color="#ccc";
}

function FilterReg2a(theLink,search,div,out) {
    var divout = "<div>"+out+"</div>";
    var eml=document.getElementById("eml").value;
    var term=document.getElementById("term"); if(term.checked){ var xa='1'; }

    if(eml!="" && xa!=""){
        var submit=document.getElementById("submit").disabled=false;
        document.getElementById("submit").style.color="#000";
    }
}

