//IT Experts functions
//
var ITE_itemsids;
//
function ITE_initPage(path)
{
	ITE_itemsids = ITE_getItemsIDs();
	if(ITE_itemsids.length > 0)
	{
		ITE_updatePage(path, ITE_itemsids);
	}
}
//
function ITE_getItemID(id)
{
	var tmp = id.split('_');
	return tmp.pop();
}
//
function ITE_getItemsIDs()
{
	var ids = [];
	var divs = document.getElementsByTagName('div');
	for(var i=0;i<divs.length;i++)
	{
		if(divs[i].id.toLowerCase().indexOf('ite_rate_container') != -1)
		{
			//Found rate control
			var id = ITE_getItemID(divs[i].getAttribute('id'));
			ids.push(id);
			ITE_setVoters(id, 'Loading ...');
		}
	}
	return ids;
}
//
function ITE_updatePage(path, ids)
{
	ids = ids.join(',');
	ITE_connectAjax(path, ids, -1, -1);
}
//
function ITE_getSelectedRate(id)
{
	var selid = 'ite_rate_select_' + id;
	var sel = document.getElementById(selid);
	var rate = sel.options[sel.options.selectedIndex].value;
	return rate;
}
//
function ITE_setUserRate(elm, path)
{
	var id = elm.getAttribute('id');
	id = ITE_getItemID(id);
	var cook = ITE_readCookie('iterate_' + id);
	cook = (cook)? cook : -1;
	var rate = ITE_getSelectedRate(id);
	ITE_setStatus(id, 'Saving...');
	ITE_connectAjax(path, id, rate, cook);
}
//
function ITE_connectAjax(path, ids, rate, cook)
{
	AjaxRequest.post(
	{
		'url':path+'engine/dorate.php'
		,'queryString':'ids='+ids+'&rate='+rate+'&cook='+cook+'&generateUniqueUrl=true'
		,'timeout':30000
		,'onError':ITE_AjaxOnError
		,'onTimeout':ITE_AjaxOnTimeout
		,'onSuccess':ITE_AjaxOnSuccess
	}
	);
}
//
function ITE_AjaxOnError()
{
	//Error
	alert("Error connecting to script!");
}
//
function ITE_AjaxOnTimeout()
{
	//Timed Out
	alert("Script Timeout!");
}
//
function ITE_AjaxOnSuccess(req)
{
	var rep = req.responseText;
	
	var respTypes = rep.split('|');
	//
	for(var i=0;i<respTypes.length;i++)
	{
		var resp = respTypes[i].split(':::');
		//
		if(resp[0] == 'exit')
		{
			ITE_onError();
			switch(resp[1])
			{
				case 'empty':
					//
					break;
			}
		}
		else
		{
			switch(resp[0])
			{
				case 'cookie':
					ITE_setCookie(resp[1]);
					break;
				case 'oldvoter':
					ITE_oldVoter(resp[1]);
					break;
				case 'item':
					ITE_updateItem(resp[1]);
					break;
				case 'debug':
					alert(resp[1]);
					break;
			}
		}
	}
}
//
function ITE_updateItem(info)
{
	info = info.split('@@@');
	var id = info[0].split('=');
	id = id[1];
	var width = info[1].split('=');
	width = width[1];
	var voters = info[2].split('=');
	voters = voters[1];
	voters = (voters == 1)? voters + ' voter' : voters + ' voters';
	//
	ITE_setWidth(id, width);
	//
	ITE_setVoters(id, voters);
}
//
function ITE_setCookie(info)
{
	info = info.split('@@@');
	var id = info[0].split('=');
	id = id[1];
	var cook = info[1].split('=');
	cook = cook[1];
	ITE_createCookie('iterate_'+id, cook, 365);
	ITE_setStatus(id, '');
}
//
function ITE_oldVoter(info)
{
	var id = info.split('=');
	id = id[1];
	ITE_setStatus(id, 'Already voted!');
}
//
function ITE_setWidth(id, w)
{
	w = (w < 1)? 1 : w;
	var recid = 'ite_rate_rect_' + id;
	var rec = document.getElementById(recid);
	rec.style.width = w + 'px';
	
	//M.Abdelmonem
	// 18 Nov 2008
	// Bug: distorted image when changing the text size
	rec.style.height = '20px';
}
//
function ITE_setStatus(id, txt)
{
	var stid = 'ite_rate_status_' + id;
	var content = document.getElementById(stid);
	content.innerHTML = txt;
}
//
function ITE_setVoters(id, txt)
{
	var vtid = 'ite_rate_voters_' + id;
	var content = document.getElementById(vtid);
	content.innerHTML = txt;
}
//Cookies
function ITE_createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else
	{
		var expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}
//
function ITE_readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ')
		{
			c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) == 0)
		{
			return c.substring(nameEQ.length,c.length);
		}
	}
	return false;
}
//
function ITE_addReview(elm, path)
{
	var id = elm.getAttribute('id');
	id = ITE_getItemID(id);
	location.href=path+"engine/addreview.php?id=" + id;
}
//
function ITE_showReviews(elm, path)
{
	var id = elm.getAttribute('id');
	id = ITE_getItemID(id);
	location.href=path+"engine/showreviews.php?id=" + id;
}
//
