
/* Onload events Loader */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}


// Screenshots - Move to last screenshot
function getPreviousScreenshot() {
	if (currentScreenshot>1) {
		currentScreenshot--;
		document.getElementById("imagebox").src= images[currentScreenshot][0];
	}
	if (currentScreenshot>1)
	document.getElementById("previousImageButton").style.display="inline";
	else
	document.getElementById("previousImageButton").style.display="none";
	if (currentScreenshot<screenshotsCounter)
	document.getElementById("nextImageButton").style.display="inline";
	else
	document.getElementById("nextImageButton").style.display="none";
}

// Screenshots - Move to next screenshot
function getNextScreenshot() {
	if (currentScreenshot<screenshotsCounter) {
		currentScreenshot++;
		document.getElementById("imagebox").src= images[currentScreenshot][0];
	}
	if (currentScreenshot>1)
	document.getElementById("previousImageButton").style.display="inline";
	else
	document.getElementById("previousImageButton").style.display="none";
	if (currentScreenshot<screenshotsCounter)
	document.getElementById("nextImageButton").style.display="inline";
	else
	document.getElementById("nextImageButton").style.display="none";
}

var xmlHttp;
var cache = new Array();

// AJAX creator
function createXMLHttpRequestObject() {
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
		"MSXML2.XMLHTTP.5.0",
		"MSXML2.XMLHTTP.4.0",
		"MSXML2.XMLHTTP.3.0",
		"MSXML2.XMLHTTP",
		"Microsoft.XMLHTTP");

		for (var i = 0; i<xmlHttpVersions.length && !xmlHttp; i++) {
			try {
				xmlHttp = new ActiveXObject(xmlHttpVersions[i]);
			} catch (e) { }
		}
	}

	if (!xmlHttp) {
		displayError("Error creating XMLHttpRequest Object");
	} else {
		return xmlHttp;
	}

}

// Display Error to Browser
function displayError(message) {
	alert("AJAX ERROR: " + message);
}

// Open Comment Box with styles
function openComment(commentId) {
	var commentDiv = document.getElementById("comment_" + commentId);
	var commentHeader = document.getElementById("comment_header_" + commentId);
	var commentFooter = document.getElementById("comment_footer_" + commentId);
	var commentInside = document.getElementById("comment_inside_" + commentId);
	if (commentDiv.style.display == "") {
		commentDiv.style.display = "none";
		commentHeader.style.display = "none";
		commentFooter.style.display = "none";
		commentInside.style.backgroundColor = "";
		commentInside.style.marginTop = "10px";
	}
	else {
		commentDiv.style.display = "";
		commentHeader.style.display = "";
		commentFooter.style.display = "";
		commentInside.style.backgroundColor = "#404040";
		commentInside.style.marginTop = "0px";
	}
}


// Comments - request comments from server
function startCommentsRequest(module, id) {
	createXMLHttpRequestObject();

	xmlHttp.onreadystatechange = doCommentsUpdate;
	xmlHttp.open("GET", "http://"+document.domain+"/index.php?design=empty;module=" + module + ";submodule=comments;id=" + id, true);
	xmlHttp.send("");
}

// Comments - update html
function doCommentsUpdate() {
	if (xmlHttp.readyState == 4) {
		var commentsDiv = document.getElementById("comments")
		commentsDiv.innerHTML = xmlHttp.responseText;
	}
}


// Members - Vaildate form input
function validate(inputValue, fieldId) {

	if (!xmlHttp) {
		xmlHttp = createXMLHttpRequestObject();
	}

	// Make sure xml object exists
	if (xmlHttp) {
		// add field to check array
		if (fieldId) {
			// encode values
			inputValue = encodeURIComponent(inputValue);
			fieldId = encodeURIComponent(fieldId);

			// add to cache array
			cache.push("value=" + inputValue + "&id=" + fieldId);
		}
		try {
			if ((xmlHttp.readyState == 4 || xmlHttp.readyState==0) && cache.length>0) {
				var cacheEntry = cache.shift();
				xmlHttp.open("POST", "http://"+document.domain+"/index.php", true);
				xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlHttp.onreadystatechange = handleValidateStateChange;
				xmlHttp.send("design=xml&module=members&task=check&" + cacheEntry);
			}
		} catch (e) {
			displayError(e.toString());
		}
	}
}

// function will enter the real function, when ajax object is done
function handleValidateStateChange() {
	if (xmlHttp.readyState==4) {
		if (xmlHttp.status==200) {
			try {
				readValidateResponse();
			} catch (e) {
				displayError(e.toString());
			}
		}
	}
}

function readValidateResponse() {
	var response = xmlHttp.responseText;
	if (response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0) {
		throw (response.length == 0 ? "Server error." : response);	
	}
	
	// get response in XML format (assume the response is valid XML)
	responseXml = xmlHttp.responseXML;
	xmlDoc = responseXml.documentElement;
	message = xmlDoc.getElementsByTagName("message")[0].firstChild.data;
	fieldId = xmlDoc.getElementsByTagName("field")[0].firstChild.data;
	
	var errorDiv = document.getElementById(fieldId + "Error");
	// these is a error
	if (message.length>1) {
		errorDiv.innerHTML = message + "<br />";
		errorDiv.style.display = "";
	} else {
		// no error or error was corrected
		errorDiv.style.display = "none";
	}
	
	
	
	setTimeout("validate();", 100);
	
}