String.prototype.replaceAll = function(strTarget, strSubString) 
{
	var strText = this;
	var intIndexOfMatch = strText.indexOf( strTarget );
	while (intIndexOfMatch != -1) {
		strText = strText.replace( strTarget, strSubString )
		intIndexOfMatch = strText.indexOf( strTarget );
	}
	return( strText );
}

function fav_send_favorites(obj_to_open_name, proj_id, fav_type)
{
	var openedBlock = document.getElementById(obj_to_open_name);
	
	if(openedBlock.style.display == 'inline')
	{
		openedBlock.style.display = 'none';
	}
	else
	{
		openedBlock.style.top = (315+f_scrollTop()) + 'px';
		openedBlock.style.left = '455px';
		openedBlock.style.display = 'inline';
		if(proj_id != 0)
		{
			document.getElementById('fav_title_type').innerHTML = 'projekt';
			document.getElementById('send_projId').value = 'proj_'+proj_id;
		}
		document.getElementById('send_type1').value = fav_type;
	}
	return false;
}

var x_move = 0;

function fav_send_favorites2(opener_obj, obj_to_open_name, prodImg, favId, fav_type)
{
	var openedBlock = document.getElementById(obj_to_open_name);
	
	if(openedBlock.style.display == 'inline')
	{
		openedBlock.style.display = 'none';
	}
	else
	{
		var pos = findPos(opener_obj);
		openedBlock.style.left = (pos[0]+opener_obj.offsetWidth-190-x_move) + 'px';
		openedBlock.style.top = (pos[1]+15) + 'px';
		openedBlock.style.display = 'inline';
		document.getElementById('send_prodImg').innerHTML = prodImg.replaceAll('|', '"');
		document.getElementById('send_favId').value = favId;
		document.getElementById('send_type2').value = fav_type;
		document.getElementById('send2_comm').value = document.getElementById('comm['+favId+']').value;
		
		if(fav_type == 'vids')
		{
			document.getElementById('send_prod_type').innerHTML = 'video';
		}
		else if(fav_type == 'articles')
		{
			document.getElementById('send_prod_type').innerHTML = 'artikkel';
		}
		else if(fav_type == 'igal')
		{
			document.getElementById('send_prod_type').innerHTML = 'pilt';
		}
		else
		{
			document.getElementById('send_prod_type').innerHTML = 'toode';
		}
	}
	return;
}

function fav_return_nothing(output)
{
	return;
}

function fav_save_comm(id)
{
	var comm = document.getElementById('comm[' + id + ']').value;
	document.getElementById('status[' + id + ']').innerHTML = 'salvestan...';
	x_fav_save_comm(id, comm, fav_return_nothing);
	setTimeout("fav_setEmptyHTML('status[" + id + "]')", 1000);
	return;
}

function fav_setEmptyHTML(element_id)
{
	document.getElementById(element_id).innerHTML = '';
}

function fav_send_email(opener_id, type, fav_id)
{
	var name = document.getElementById('send' + type + '_name').value;
	var email = document.getElementById('send' + type + '_email').value;
	var emailto = document.getElementById('send' + type + '_emailto').value;
	var comm = document.getElementById('send' + type + '_comm').value;
	var fav_type = document.getElementById('send_type' + type).value;
	
	if(emailto == "" || emailto.indexOf('@', 0) == -1)
	{
		alert("Sisestatud saaja e-mail ei ole korrektne!");
		return;
	}
	if(email == "" || email.indexOf('@', 0) == -1)
	{
		alert("Sisestatud sinu e-mail ei ole korrektne!");
		return;
	}

	var str_chk = fav_id.split("_");
	if(str_chk[0] == 'proj')
	{
		x_saada_lemmikud(name, email, str_chk[1], comm, emailto, 0, fav_return_nothing);
	}
	else
	{
		x_fav_send_email(emailto, name, email, fav_id, comm, fav_type, fav_return_nothing);
	}
	document.getElementById(opener_id).style.display = 'none';

	fav_msg('Toode edukalt edastatud.');
	return;
}

function fav_add(id, type)
{
	x_fav_add(id, type, fav_msg);
}

function fav_add_to_proj(item_id, img_path, openItem)
{
	var openedBlock = document.getElementById('fav_add');
	var opener_obj = document.getElementById('0_link_to_add['+item_id+']');
	
	if(openItem == 1)
	{
		if(openedBlock.style.display == 'inline' && document.getElementById('item_id').value != item_id) 
		{
			document.getElementById('1_link_to_add['+document.getElementById('item_id').value+']').style.display = 'inline';
			document.getElementById('0_link_to_add['+document.getElementById('item_id').value+']').style.display = 'none';
		}
		
		document.getElementById('1_link_to_add['+item_id+']').style.display = 'none';
		opener_obj.style.display = 'inline';
		
		var pos = findPos(opener_obj);
		openedBlock.style.left = pos[0] + 'px';
		openedBlock.style.top = (pos[1]+27) + 'px';
		
		openedBlock.style.display = 'inline';
		document.getElementById('item_id').value = item_id;
	}
	else
	{
		document.getElementById('1_link_to_add['+item_id+']').style.display = 'inline';
		opener_obj.style.display = 'none';
		openedBlock.style.display = 'none';
	}
	return;
}

function fav_add_to_proj2(proj_id, room_id)
{
	var item_id = document.getElementById('item_id').value;
	
	document.getElementById('0_link_to_add['+item_id+']').style.display = 'none';
	document.getElementById('1_link_to_add['+item_id+']').style.display = 'none';	
	document.getElementById('fav_add').style.display = 'none';
	document.getElementById('link_to_add['+item_id+']').style.display = 'inline';
	
	x_fav_set_to_proj(proj_id, room_id, item_id, fav_return_nothing);
	
	return;
}

function fav_msg(output)
{
	/*
	var favWin = window.open("", "favorites", "scrollbars=no,menubar=no,height=55,width=258,resizable=yes,toolbar=no,location=no,status=no,left=380,top=450'");
	favWin.document.write('<html><head><title>Sisustus.ee</title></head><body onclick="window.close();" marginheight="0" marginwidth="0" topmargin="0" bottommargin="0" rightmargin="0" leftmargin="0">');
	favWin.document.write('<table align="center" cellspacing="0" cellpadding="0" border="0" width="100%" height="100%" bgcolor="#ffffff" style="cursor: pointer;">');
	favWin.document.write('<tr><td colspan="3" height="1" bgcolor="#B09C45"></td></tr>');
	favWin.document.write('<tr>');
	favWin.document.write('<td width="1" bgcolor="#B09C45"></td>');
	favWin.document.write('<td width="254" height="26" background="http://static.sisustus.ee/images/new1/fav/bg_added.png"></td>');
	favWin.document.write('<td width="1" bgcolor="#B09C45"></td>');
	favWin.document.write('</tr><tr>');
	favWin.document.write('<td width="1" bgcolor="#B09C45"></td>');
	favWin.document.write('<td width="254" height="70" align="center" valign="middle" style="font-family: Verdana; font-weight: bold; font-size: 10px;">' + output + '</td>');
	favWin.document.write('<td width="1" bgcolor="#B09C45"></td>');
	favWin.document.write('</tr>');
	favWin.document.write('<tr><td colspan="3" height="1" bgcolor="#B09C45"></td></tr>');
	favWin.document.write('</table>');
	favWin.document.write('</body></html>');
	favWin.focus();
	*/
	var obj = document.getElementById('added_to_favorite_msg');
	obj.style.top = (380+f_scrollTop()) + 'px';
	obj.style.left = '465px';
	obj.style.display = 'inline';
	document.getElementById('html_value').innerHTML = output;
	return;
}

function fav_set_proj(opener_obj, pid, cur_room, type)
{
	var openedBlock = document.getElementById('lisa_toode_projekti');
	
	if(openedBlock.style.display == 'inline')
	{
		openedBlock.style.display = 'none';
	}
	else
	{
		//var pos = findPos(opener_obj);
		//openedBlock.style.left = (pos[0]+opener_obj.offsetWidth-5) + 'px';
		//openedBlock.style.top = pos[1] + 'px';
		
		openedBlock.style.top = (190+f_scrollTop()) + 'px';
		openedBlock.style.left = '465px';
		
		document.getElementById('fav_pid2').value = pid;
		document.getElementById('fav_proj_add_title').innerHTML = (type == 0) ? 'Lisa toode projekti' : 'Liiguta toode teise ruumi';
		if(document.getElementById('room_'+cur_room))
		{
			document.getElementById('room_'+cur_room).checked = 1;
		}
		openedBlock.style.display = 'inline';
	}
	return;
}

function fav_change_room(opener_obj, pid)
{
	var openedBlock = document.getElementById('fav_change_room');
	
	if(openedBlock.style.display == 'inline')
	{
		openedBlock.style.display = 'none';
	}
	else
	{
		var pos = findPos(opener_obj);
		openedBlock.style.left = (pos[0]+opener_obj.offsetWidth-5) + 'px';
		openedBlock.style.top = pos[1] + 'px';
		openedBlock.style.display = 'inline';
		document.getElementById('fav_pid').value = pid;
	}
	return;
}

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


