var xmlhttp;
var  myobj = "myspan"
var please_wait = "<div align='center'><img src='http://pustand.depdag.go.id/img/progressbar.gif'></div>";

if(window.ActiveXObject){
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			xmlhttp = false;
		}
	}
}else{
	try{
		xmlhttp = new XMLHttpRequest();
	}
	catch(e){
		xmlhttp = false;
	}
}

function open_url(url, targetId) {
  	if(!xmlhttp)return false;
    	var e=document.getElementById(targetId);if(!e)return false;
    	if(please_wait)e.innerHTML = please_wait;
    		xmlhttp.open("GET", url, true);
    		xmlhttp.onreadystatechange = function() { response(url, e); }
    	try{
      		xmlhttp.send(null);
    	}catch(l){
    		while(e.firstChild)e.removeChild(e.firstChild);//e.innerHTML="" the standard way
    		e.appendChild(document.createTextNode("request failed"));
  }
}

function response(url, e) {
  	if(xmlhttp.readyState != 4)return;
    	var tmp= (xmlhttp.status == 200 || xmlhttp.status == 0) ? xmlhttp.responseText : "Ooops!! A broken link! Please contact the webmaster of this website ASAP and give him the following error code: " + xmlhttp.status+" "+xmlhttp.statusText;
    	var d=document.createElement("div");
    	d.innerHTML=tmp;
    	setTimeout(function(){
      		while(e.firstChild)e.removeChild(e.firstChild);//e.innerHTML="" the standard way
      		e.appendChild(d);
    	},10)
}

function makeRequest(url, parameters) {
      	
      	if (!xmlhttp) {
         		alert('Cannot create XMLHTTP instance');
         		return false;
      	}
      	      	      	
      	if(please_wait) document.getElementById(myobj).innerHTML = please_wait; 	
		xmlhttp.open('GET', url + parameters, true);
      	xmlhttp.onreadystatechange = alertContents;      	
      	xmlhttp.send(null);
}

function alertContents() {
      	if (xmlhttp.readyState == 4) {      		
         		if (xmlhttp.status == 200) {            	
            		result = xmlhttp.responseText;
            		document.getElementById(myobj).innerHTML = result;            
         		} else {
            		alert('There was a problem with the request.');            		
         		}
      	}
}

function get(obj,url,span) {
      	var getstr = "/";
      	for (i=0; i<obj.childNodes.length; i++) {
         		if (obj.childNodes[i].tagName == "INPUT") {
            		if (obj.childNodes[i].type == "text") {						               			
							getstr += obj.childNodes[i].name + "/" +  replaceAll(obj.childNodes[i].value,'/','-') + "/";
            		}
            		if (obj.childNodes[i].type == "checkbox") {
               			if (obj.childNodes[i].checked) {
                  				getstr += obj.childNodes[i].name + "/" + obj.childNodes[i].value + "/";
               			} else {
                  				getstr += obj.childNodes[i].name + "/0/";
               			}
            		}
            		if (obj.childNodes[i].type == "radio") {
               			if (obj.childNodes[i].checked) {
                  				getstr += obj.childNodes[i].name + "/" + obj.childNodes[i].value + "/";
               			}
            		}
            		if (obj.childNodes[i].type == "hidden") {
               			getstr += obj.childNodes[i].name + "/" + obj.childNodes[i].value + "/";
            		}
            		if (obj.childNodes[i].type == "password") {
               			getstr += obj.childNodes[i].name + "/" + obj.childNodes[i].value + "/";
            		}
            		if (obj.childNodes[i].type == "file") {
               			getstr += obj.childNodes[i].name + "/" + obj.childNodes[i].value + "/";
            		}
         		}
         		
         		if (obj.childNodes[i].tagName == "TEXTAREA") {
					getstr += obj.childNodes[i].name + "/" +  replaceAll(obj.childNodes[i].value,'/','-') + "/";
         		}
         		
         		if (obj.childNodes[i].tagName == "SELECT") {
            		var sel = obj.childNodes[i];    		
            		getstr += sel.name + "/" + sel.options[sel.selectedIndex].value + "/";
         		}
      }	
	  if(span)
	  		myobj = span;
      makeRequest(url, getstr);
}

// Replaces all instances of the given substring.
function replaceAll(strText,strTarget,strSubString){
		var intIndexOfMatch = strText.indexOf(strTarget);		  
		while (intIndexOfMatch != -1){	
			strText = strText.replace(strTarget, strSubString );	
			intIndexOfMatch = strText.indexOf( strTarget );
		}
    	return( strText );
}

//fungsi untuk tambah produk
function tambahproduk(url,span){	
	xmlhttp.open('GET', url, true);
	xmlhttp.onreadystatechange = function() { aksitambahproduk(span); };    
    xmlhttp.send(null);
}
function aksitambahproduk(span){	
	if (xmlhttp.readyState == 4) {      		
         		if (xmlhttp.status == 200) {            		
            		result = xmlhttp.responseText;
            		document.getElementById(span).innerHTML = result; 
         		} else {
            		alert('There was a problem with the request.');           		
         		}
     }
}