var xmlHttp

function showData(str,str2)
{ 
    

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="getdata2.php"
url=url+"?q="+str+"&a="+str2
url=url+"&sid="+Math.random()
//alert(url);
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
//alert("xxx");
//alert(xmlHttp.responseText);
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
 //document.getElementById("txtHint").innerHTML=xmlHttp.responseText.replace(/^\s*|\s*$/g,"");
 } 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

function utf8_encode ( string ) {
    // Encodes an ISO-8859-1 string to UTF-8
    //
    // version: 812.316
    // discuss at: http://phpjs.org/functions/utf8_encode
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman
    // +   improved by: Yves Sucaet
    // +   bugfixed by: Onno Marsman
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
    string = (string+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");

    var utftext = "";
    var start, end;
    var stringl = 0;

    start = end = 0;
    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;

        if (c1 < 128) {
            end++;
        } else if((c1 > 127) && (c1 < 2048)) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {
            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc != null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }

    if (end > start) {
        utftext += string.substring(start, string.length);
    }

    return utftext;
}


function base64_encode( data ) {

    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, enc="", tmp_arr = [];
//    alert('suhut');
    data = utf8_encode(data);

    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);

        bits = o1<<16 | o2<<8 | o3;

        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;

        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);

    enc = tmp_arr.join('');

    switch( data.length % 3 ){
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }

    return enc;
}



/**********************************************************
*	Fungsi     : Pencarian Form AJAX
*	Programmer : Airlangga Eka W. (procom44@yahoo.com)
*	Tanggal    : 13/02/2009
**********************************************************/
function showList(field, filename, mode, page, search, order_field, order_type)
{
	period = document.getElementById("period").value;
    if (field == "period")
	{
		
		value = document.getElementById("cboPeriod").value;
		if (value == "Seluruh") {
//			document.getElementById(period).checked = false;
//			document.getElementById("div"+period).style.display = "none";
//			document.getElementById("cbo"+period).selectedIndex = 0;
                         for (var i=0; i < document.forms[0].elements.length; i++) {
                                    var element = document.forms[0].elements[i];
                                    if (element.type == "checkbox" && element.checked) {
                                            element.checked = false;
                                            document.getElementById("div"+element.name).style.display = "none";
                                    }
                            }

		} else {
			var data = new Array();

			data = value.split('-');
			date1 = format_date(data[0]);
			date2 = format_date(data[1]);

			document.getElementById(period).checked = true;
			document.getElementById("div"+period).style.display = "block";
			document.getElementById("cbo"+period).selectedIndex = 5;
			document.getElementById("dtp"+period+"1").value = data[0];
			document.getElementById("dtp"+period+"2").value = data[1];
		}              
		showList(document.getElementById('btnCari') , filename, 'view', 0, '', '', '');
		return
	}
	else if(search=='All') {
		 try {
			 document.getElementById("cboPeriod").selectedIndex = 0;
		  } catch(ex) {
			   
		   } 
		
	   for (var i=0; i < document.forms[0].elements.length; i++) {
			var element = document.forms[0].elements[i];
			if (element.type == "checkbox" && element.checked) {
				element.checked = false;
				document.getElementById("div"+element.name).style.display = "none";
			}
		}
		
	}
	else if(field.value=='Semua') {
	  try {
			 document.getElementById("cboPeriod").selectedIndex = 0;
		  } catch(ex) {
			   
		   } 
	  for (var i=0; i < document.forms[0].elements.length; i++) {
			var element = document.forms[0].elements[i];
			if (element.type == "checkbox" && element.checked) {
				element.checked = false;
				document.getElementById("div"+element.name).style.display = "none";
			}
		}
	}
        
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}

	document.getElementById("divDetail").innerHTML = '<img src=images/loading.gif>';

        // Mode Period Date
	


	if (search == null || search == "")
	{
		var str = "";
		for (var i=0; i < document.forms[0].elements.length; i++) {
			var element = document.forms[0].elements[i];
			if (element.type == "checkbox" && element.checked) {
				var element2 = document.forms[0].elements[i+1];
				var element3 = document.forms[0].elements[i+2];

				if (element2.value == "sama dengan") {
					str = str + " and "+ element.name + " = '" + element3.value + "' ";
				} else if (element2.value == "seperti") {
					str = str  + " and "+  element.name + " like '%" + element3.value + "%' ";
				} else if (element2.value == "antara") {
					var element4 =document.getElementById("dtp"+element.name+"2");
					str = " and "+  element.name + " >= '" + format_date(element3.value) + "' and " + element.name + " <= '" + format_date(element4.value) + "' "+str ;
				} else {
					var prefix = element3.name.substring(0, 3);
					if (prefix == "dtp") {
						value = format_date(element3.value);
					} else if (prefix == "cur") {
						value = parseFloat(ElementCurrencyByNameGetValue(element3.name));
					} else {
						value = element3.value;
					}
					str = str  + " and "+  element.name + " " + element2.value + " '" + value + "' ";
				}
			}
		}
//		str = escape(str.substring(0, str.length-1));
		if (str.length > 0) {
            str = "WHERE 1=1 " + str;
        }
        else {
            str=" WHERE 1=1";
        }
	} else {
		keyword = document.getElementById("keyword").value;
		if (search == "All") {
			document.getElementById(keyword).checked = false;
			document.getElementById("div"+keyword).style.display = "none";
			document.getElementById("cbo"+keyword).selectedIndex = 0;
			//document.getElementById("txt"+keyword).value = "";
			str = " WHERE 1=1";
		} else if (search == "0") {
			str = "WHERE "+keyword+" LIKE '0%' OR "+keyword+" LIKE '1%' OR "+keyword+" LIKE '2%' OR "+keyword+" LIKE '3%' OR "+keyword+" LIKE '4%' OR "+keyword+" LIKE '5%' OR "+keyword+" LIKE '6%' OR "+keyword+" LIKE '7%' OR "+keyword+" LIKE '8%' OR "+keyword+" LIKE '9%'";
		} else {
			document.getElementById(keyword).checked = true;
			document.getElementById("div"+keyword).style.display = "block";
			document.getElementById("cbo"+keyword).selectedIndex = 1;
			//document.getElementById("txt"+keyword).value = search;
			str = "WHERE "+keyword+" LIKE '" + search + "%'";
		}
	}
    //alert(str);
	
    str=base64_encode(str);

	var url=filename
	url=url+"&q="+str+"&mode="+mode+"&page="+page+"&order_field="+order_field+"&order_type="+order_type
	//url=url+"&mode=" + modex + "&q=" + str + "&id=" + idx + "&page=" + pagex
	url=url+"&sid="+Math.random()
	// alert(url);
	xmlHttp.onreadystatechange=stateListed
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}
function stateListed()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//alert(xmlHttp.responseText);
		var result = xmlHttp.responseText;
		var data = new Array();
		data = result.split('|');

		document.getElementById("divDetail").innerHTML=data[0];
		document.getElementById("divPage").innerHTML=data[1];
	}
}


/**********************************************************
*	Fungsi     : Pencarian Form AJAX
*	Programmer : Airlangga Eka W. (procom44@yahoo.com)
*	Tanggal    : 13/02/2009
**********************************************************/
function format_date(tgl) {
	var hari = tgl.substring(0, 2);
	var bulan = tgl.substring(3, 5);
	var tahun = tgl.substring(6, 10);
	return (tahun + "/" + bulan + "/" + hari);
}


function showReport(field)
{
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}

	document.getElementById("content").innerHTML = '<img src=images/loading.gif>';
	str1 = document.getElementById("name").value;
	str2 = document.getElementById("query").value;

	var fl = "";
	var fv = "";
	for (var i=0; i < document.forms[0].elements.length; i++) {
		var element = document.forms[0].elements[i];

		var prefix = element.name.substring(0, 2);
		if (prefix == "fd")	{
			value = format_date(element.value);
		} else {
			value = element.value;
		}
		fl = fl + element.name + "|";
		fv = fv + value + "|";
	}
	fl = escape(fl.substring(0, fl.length-1));
	fv = escape(fv.substring(0, fv.length-1));

	var url="report.php"
	url=url+"?n="+str1+"&q="+str2+"&fl="+fl+"&fv="+fv
	url=url+"&sid="+Math.random()
	//alert(url);
	xmlHttp.onreadystatechange=stateReported
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}


function stateReported()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		//alert(xmlHttp.responseText);
		var result = xmlHttp.responseText;
		document.getElementById("content").innerHTML=result;
	}
}
