﻿/*
** Wildsow Simple Javascript Libary
**
** @desciption This is a libary which contains few goodies
** @author	Yves Bannwart-Landert
** @info	<info@wildsow.ch>
** @version	1.0
*/


/* check selected checkbox */
function checkCheckbox(cb, cbValue)
{
	if (cbValue != '' && cb)
	{
		document.getElementById(cb).checked = true;
	}
}

/* check selected radio button */
function checkRadio(rb, rbValue)
{
	var radioBtn = document.getElementById(rb);
	if (radioBtn.value == rbValue)
	{
		radioBtn.checked = true;
	}
}

/* check selected dropdown menu */
function checkSelect(sl, selection)
{
	if (selection != '') {
		selector = document.getElementById(sl);
		for (var i=0; i<selector.length; ++i)
		{
			if (selector.options[i].value == selection)
			{
				selector.options[i].selected = true;
			}
		}
	}
}


function changeVoteing(tipp, id, fld)
{
	if (id)
	{
		$('id_' + fld + '_' + id).className = 'input_field';
		
		if (tipp >= 0)
		{
			if (tipp <= 16)
			{
				sendAjaxRequest(tipp, id, fld);
			} else {
				$('id_' + fld + '_' + id).className = 'error_field';
				$('id_' + fld + '_' + id).value = 16;
				sendAjaxRequest(16, id, fld);
			}
		} else {
			$('id_' + fld + '_' + id).className = 'error_field';
			$('id_' + fld + '_' + id).value = 0;
			sendAjaxRequest(0, id, fld);
		}
	}
}

function sendAjaxRequest(tipp, matchid, restype)
{
	$('loader_' + matchid).style.display = 'block';
	
	var url = 'index.php';
	var params = { page: 'update', vote: tipp, type:restype, id: matchid };
			
	var myAjax = new Ajax.Request(url,   
								 {
									method:'post',
									parameters: params,
									encoding: 'iso-8859-1',
									onSuccess: function() { ajaxSuccess(matchid) },
									onFailure: function() { alert('Error while sending ajax request!') },
									asynchronous: true
								 }
								);
}

function ajaxSuccess(id)
{
	if (id != '') $('loader_' + id).style.display = 'none';	
}

function showLayer(lName)
{
	if (lName) $(lName).style.display = 'block';	
}

function hideLayer(lName)
{
	if (lName) $(lName).style.display = 'none';	
}

