// functions.js
function runajax(objID, serverPage)
{
	//Create a boolean variable to check for a valid Internet Explorer instance.
	var xmlhttp = false;
	//Check if we are using IE.
	try {
	//If the JavaScript version is greater than 5.
	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//If not, then use the older ActiveX object.
		try {
		//If we are using Internet Explorer.
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
		//Else we must be using a non-IE browser.
		xmlhttp = false;
		}
	}

	// If we are not using IE, create a JavaScript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
	xmlhttp = new XMLHttpRequest();
	}
	var obj = document.getElementById(objID);
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	obj.innerHTML = xmlhttp.responseText;
	}
	}
	xmlhttp.send(null);
}


// Delay in milliseconds before refreshing gallery.
var refreshrate = 1000;
var refreshrate2 = 2000;
//Function to show a loading message.
function updateStatus()
{
	document.getElementById("errordiv").innerHTML = "";
	document.getElementById("imagecontainerCenter").innerHTML = "<b>Mohon tunggu, sedang proses...</b><br /><img src='./images/loading1.gif' alt='loading..'>";
}

function refreshView(angk)
{
	// Reload the full-size image.
	var url1 = "midpic.php?angk=" + angk;
	var url2 = "picnav.php?angk=" + angk;

	setTimeout('runajax("middiv", "'+url1+'")', refreshrate);
	// Reload the navigation.
	setTimeout('runajax("picdiv", "'+url2+'")', refreshrate);
	document.getElementById("errcomm").innerHTML = "";
	document.getElementById("errordiv").innerHTML = "";
}

function uploadimg(theform, angk)
{
	// Update user status message.
	document.getElementById("errcomm").innerHTML = "";
	document.getElementById("errcomm").innerHTML = "<b>Mohon tunggu, sedang proses...</b><br /><img src='./images/loading.gif' alt='loading..'>";
	// Now submit the form.
	theform.submit();
	theform.reset();
	// And finally update the display.

	var url1 = "midpic.php?angk=" + angk;
	var url2 = "picnav.php?angk=" + angk;

	setTimeout('runajax("middiv", "'+url1+'")', refreshrate);
	// Reload the navigation.
	setTimeout('runajax("picdiv", "'+url2+'")', refreshrate2);

	refreshView(angk);
}

function removeimg(theimg, angk)
{
	if(	confirm("Yakin mau MENGHAPUS foto -- " + theimg + " -- ??")){
		runajax("errordiv", "delpic.php?pic=" + theimg + "&angk="+angk);
		refreshView(angk);
		runajax('commentdiv', 'formcomment.php?imgID=&angk='+angk);
	}
}

function removeComment(commentID, comment, theImg, angk)
{
	if(	confirm("Yakin mau MENGHAPUS Komentar -- " + comment + " -- ??")){
		runajax("errordiv", "delcomment.php?cid=" + commentID);
		runajax("middiv", "midpic.php?angk=" + angk + "&curimage="+theImg);
		runajax("commentdiv", "formcomment.php?curimage=" + theImg + "&angk=" + angk);
	}
}


function imageClick(imgID, img, angk)
{
	updateStatus();
	runajax('middiv', 'midpic.php?curimage='+img+'&angk='+angk);
	runajax('picdiv', 'picnav.php?curimage=' + img +'&angk='+angk);
	runajax('commentdiv', 'formcomment.php?imgID=' + imgID+'&angk='+angk);
}

function saveComment(theform, angk, imgID)
{
	var url = "formcomment.php?imgID="+imgID+"&angk="+angk;

		document.getElementById("errcomm").innerHTML = "";
		document.getElementById("errcomm").innerHTML = "<img src='./images/loading.gif' alt='loading..'><b>Mohon tunggu, sedang proses...</b>";
		
		theform.submit();
		theform.reset();
		// And finally update the display.
		setTimeout('runajax("commentdiv", "'+url+'")', refreshrate);
}

function updateCurimage(img){
	runajax('formcommentdiv', 'galery.php?img=' + img);
}
