// JavaScript Document
function initXMLHttpClient() {
var xmlhttp;
try {
    // Mozilla / Safari / IE7
    xmlhttp = new XMLHttpRequest();
    } catch (e) {
    // IE
      var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0',
                                  'MSXML2.XMLHTTP.4.0',
                                  'MSXML2.XMLHTTP.3.0',
                                  'MSXML2.XMLHTTP',
                                  'Microsoft.XMLHTTP' );
      var success = false;
      for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {
          try {
                xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
                  success = true;
              } catch (e) {}
      }
        if (!success) {
            throw new Error('Unable to create XMLHttpRequest.');
        }
    }
  return xmlhttp;
}

  function HttpClient() { }
  HttpClient.prototype = {
      // type GET,POST passed to open
      requestType:'GET',
      // when set to true, async calls are made
      isAsync:false,

      // where an XMLHttpRequest instance is stored
      xmlhttp:false,

      // what is called when a successful async call is made
      callback:false,

      // what is called when send is called on XMLHttpRequest
      // set your own function to onSend to have a custom loading
     // effect
       onSend:function() {
         document.getElementById('HttpClientStatus').style.display =
                               'block';
     },

     // what is called when readyState 4 is reached, this is
     // called before your callback
      onload:function() {
          document.getElementById('HttpClientStatus').style.display =
                              'none';
      },

     // what is called when an http error happens
     onError:function(error) {
         alert(error);
     },

     // method to initialize an xmlhttpclient
     init:function() {
       try {
           // Mozilla / Safari
            this.xmlhttp = new XMLHttpRequest();
       } catch (e) {
           // IE
           var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
           var success = false;
           for (var i=0;i < XMLHTTP_IDS.length &&
             !success; i++) {
               try {
                   this.xmlhttp = new ActiveXObject
                     (XMLHTTP_IDS[i]);
                   success = true;
               } catch (e) {}
           }
           if (!success) {
               this.onError('Unable to create XMLHttpRequest.');
           }
        }
     },

     // method to make a page request
     // @param string url  The page to make the request to
     // @param string payload  What you're sending if this is a POST
    //                        request
    makeRequest: function(url,payload) {
         if (!this.xmlhttp) {
             this.init();
         }
         this.xmlhttp.open(this.requestType,url,this.isAsync);

         // set onreadystatechange here since it will be reset after a
        //completed call in Mozilla
         var self = this;
         this.xmlhttp.onreadystatechange = function() {
        self._readyStateChangeCallback(); }

         this.xmlhttp.send(payload);

         if (!this.isAsync) {
             return this.xmlhttp.responseText;
         }
    },

     // internal method used to handle ready state changes
    _readyStateChangeCallback:function() {
         switch(this.xmlhttp.readyState) {
              case 2:
               this.onSend();
               break;
            case 4:
               this.onload();
               if (this.xmlhttp.status == 200) {
                   this.callback(this.xmlhttp.responseText);
               } else {
                   this.onError('HTTP Error Making Request: '+
                                       '['+this.xmlhttp.
                                         status+']'+this.xmlhttp.
                                         statusText);
               }
               break;
         }
     }
 }
 function getfiles( url, divloading, divto ) {
    var req = null;
    try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {
        try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {
            try { req = new XMLHttpRequest(); } catch(e) {}
        }
    }
    if (req == null) throw new Error('XMLHttpRequest not supported');
    
    req.open("GET", url, false);
    req.send(null);
    
    //return req.responseText;
    document.getElementById(divto).innerHTML = req.responseText;
}
function GetValue(obj)
{
  var Object = document.getElementById(obj).value;
  return Object;
}

function getprediction(divid) {
    if(document.getElementById(divid).style.display == 'none'){
      document.getElementById(divid).style.display = 'block';
    }else{
      document.getElementById(divid).style.display = 'none';
    }  
}

function getpregnancyinfo(url, code ){
  if(code == 'f_date_c') {
    var val = GetValue(code);
    pages = url + '?type=duedate&tgl=' + val + '&rep=' + val;    
  } else {
    var val = GetValue(code);
    pages = url + '?type=firstmens&tgl=' + val + '&rep=' + val;
  }
  window.location.href=pages;
}

function getweek(url, week, asli) {
  var val = GetValue(week);
  pages = url + '?type=duedate&w=' + val + '&rep=' + asli;
  window.location.href=pages;
}

function getpreviewpregnancy(url, code, nama, tools, toolsb ){
  var dt = GetValue(tools);
  if(dt == '') {
    var dt = GetValue(toolsb);
    var name = GetValue(nama);
    //alert(dt);
    pages = url + '?type=firstmens&tgl=' + dt + '&baby=' +name;  
  } else {
    var dt = GetValue(tools);
    var name = GetValue(nama);
    //alert(dt);
    pages = url + '?type=duedate&tgl=' + dt + '&baby=' + name;    
  }
  window.open(pages);
}

function submitpregnancy(url, code, nama, tools, toolsb ){
  var dt = GetValue(tools);
  if(dt == '') {
    var dt = GetValue(toolsb);
    var name = GetValue(nama);
    //alert(dt);
    pages = url + '?type=firstmens&tgl=' + dt + '&baby=' +name;  
  } else {
    var dt = GetValue(tools);
    var name = GetValue(nama);
    //alert(dt);
    pages = url + '?type=duedate&tgl=' + dt + '&baby=' + name;    
  }
  window.open(pages);
}

function openwindow(url) {
  window.open(url);
}
