
var url = 'http://www.gamis.co.il/';


function pollImage(URL) {
	pollwin = window.open(url + 'images/' + URL,'imageWindow','height=500,width=750,scrollbars=yes,resizable=yes');
}

function writeComment(module, id) {
	pollwin = window.open(url + 'index.php?design=standalone;module=' + module + ';submodule=comments;task=write;id=' + id,'commentWindow','height=325,width=375,scrollbars=no,resizable=no');
}

var xmlHttp;
var tablePosition;
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) {
	if ($("comment_" + commentId).style.display == "") {
		$("comment_" + commentId).hide();
		$("comment_header_" + commentId).hide();
		$("comment_footer_" + commentId).hide();
		$("comment_inside_" + commentId).style.backgroundColor = "";
		$("comment_inside_" + commentId).style.marginTop = "10px";
	}
	else {
		$("comment_" + commentId).show();
		$("comment_header_" + commentId).show();
		$("comment_footer_" + commentId).show();
		$("comment_inside_" + commentId).style.backgroundColor = "#313131";
		$("comment_inside_" + commentId).style.marginTop = "0px";
	}
}


// Comments - request comments from server
function startCommentsRequest(module, id) {
	var ajaxComments = new Ajax.Updater("comments", url + "index.php",
	{
		method: "get",
		parameters: "design=empty&module=" + module + "&submodule=comments&id=" + id
	}
	);

}

// Scores - request Scores from server
function startScoresRequest(id, page, position) {


	tablePosition = position;
	var myAjax = new Ajax.Request(
	url + "index.php",
	{
		method: 'get',
		parameters: "design=empty&module=games&task=scores&page=" + page + "&id=" + id,
		onComplete: showResponse
	});
}

function showResponse(originalRequest)
	{
		//put returned XML in the textarea
		parent.$('winnersTable').innerHTML = originalRequest.responseText;
		colorLine(tablePosition);
	}

// Scores - update html
function doScoresUpdate(position) {
	if (xmlHttp.readyState == 4) {
		var commentsDiv = parent.document.getElementById("winnersTable")
		commentsDiv.innerHTML = xmlHttp.responseText;
	}

	colorLine(position);
}

function colorLine(lineNumber) {
	var elements = parent.document.getElementsByClassName(lineNumber, 'td');
	for(var i=0; i<elements.length; i++){
		elements[i].className = "selected";
	}
}


// vote
function ajaxVote(gameId, rating) {
	var voteAjax = new Ajax.Updater("votes", url + "index.php",
	{
		method: "get",
		parameters: "design=empty&module=games&task=rate&id=" + gameId + "&rating=" + rating
	}
	)
}


// Hide Alarm
function hidePage() {
	document.body.style.backgroundColor = "#FFFFFF";
	document.body.style.color = "#000000";
	document.body.innerHTML = '<body bgcolor=#ffffff text=#000000 link=#0000cc vlink=#551a8b alink=#ff0000><div align="center"><img src="http://www.google.co.il/intl/he_il/images/logo.gif"><table border=0 cellspacing=0 cellpadding=4 dir=rtl><tr><td nowrap><font size=-1><b>אתרים באינטרנט</b>&nbsp;&nbsp;&nbsp;&nbsp;<a class=q href="http://images.google.co.il/imghp?ie=UTF-8&oe=UTF-8&hl=iw&tab=wi" onclick="return qs(this)">תמונות</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class=q href="http://groups.google.co.il/grphp?ie=UTF-8&oe=UTF-8&hl=iw&tab=wg" onclick="return qs(this)">קבוצות</a>&nbsp;&nbsp;&nbsp;&nbsp;<a class=q href="http://news.google.co.il/nwshp?ie=UTF-8&oe=UTF-8&hl=iw&tab=wn" onclick="return qs(this)">חדשות</a>&nbsp;&nbsp;&nbsp;&nbsp;</font></td></tr></table><table cellpadding=0 cellspacing=0><tr valign=top><td width=25%>&nbsp;</td><td align=center nowrap><input name=hl type=hidden value=iw><input maxlength=2048 name=q size=55 title="חיפוש ב-Google" value=""><br><input name=btnG type=submit value="חיפוש ב-Google"><input name=btnI type=submit value="יותר מזל משכל"></td><td nowrap width=25%><font size=-2>&nbsp;&nbsp;<a href=/advanced_search?hl=iw>חיפוש מתקדם</a><br>&nbsp;&nbsp;<a href=/preferences?hl=iw>העדפות</a><br>&nbsp;&nbsp;<a href=/language_tools?hl=iw>כלי  שפה</a></font></td></tr><tr><td align=center colspan=3><font size=-1>חפש: <input id=all type=radio name=meta value="" checked><label for=all> דפים באינטרנט </label><input id=lgr type=radio name=meta value="lr=lang_iw" ><label for=lgr> דפים בעברית </label><input id=cty type=radio name=meta value="cr=countryIL" ><label for=cty> דפים מישראל </label></font></td></tr></table></form><br><br><font size=-1><a href="/intl/iw/ads/">תכנית פרסום</a> - &rlm;<a href=/intl/iw/about.html>הכל על Google</a>&rlm; - <a href=/intl/iw/jobs/index.html)">דרושים</a> - <a href=http://www.google.com/ncr>Google.com in English</a></font><p><font size=-2>&copy;2007 Google</font></p></center></div></body>';
}


/* 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();
		}
	}
}

// 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", url + "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);

}


