var primChild = false;
var altChild = false;
var subItems = Array(false, false, false, false, false, false, false);

changeList = function(obj, num, sk)
{
	for(var i = 0; i < obj.parentNode.childNodes.length; i++)
	{
		obj.parentNode.childNodes[i].className = 'titleListButton';
	}
	obj.className = 'titleListButtonActive';
	
	for(var j = 1; j <= sk; j++)
	{
		var node = document.getElementById('titleListContentBlock'+j);
		if (j == num)
		{
			node.style.display = 'block';
		} else
		{
			node.style.display = 'none';
		}
	}
}

blockShow = function(id)
{
	document.getElementById(id).style.display = 'block';
}
blockHide = function(id)
{
	document.getElementById(id).style.display = 'none';
}

checkOnBlur = function(obj, value)
{
	if(obj.value == '')
	{
		obj.value = value;
	}
}
checkOnFocus = function(obj, value)
{
	if(obj.value == value)
	{
		obj.value = '';
	}
}

changeState = function(obj, st)
{
	if(st == 1)
	{
		obj.parentNode.className = 'menuItemLevel1Active';
	}
	if (st == 0)
	{
		obj.parentNode.className = 'menuItemLevel1';
	}
}

changeTitle = function(id1, id2)
{
	document.getElementById('newsTitle'+id1).className = 'newsBlockTitleActive';
	document.getElementById('newsTitle'+id2).className = 'newsBlockTitle';
	
	document.getElementById('newsBlock'+id1).style.display = 'block';
	document.getElementById('newsBlock'+id2).style.display = 'none';
}

submenuShow = function(obj, id, num)
{
	var block = document.getElementById(id);
	if(block.style.display == 'block')
	{
		for(i = 1; i < subItems.length; i++)
		{
			subItems[i] = false;
		}
		
		var test = obj.id;
		obj.id = test.replace(/-act/,'');
		block.style.display = 'none';
	} else
	{
		for(i = 1; i < subItems.length; i++)
		{
			subItems[i] = false;
		}
		subItems[num] = true;
		obj.id = obj.id + '-act';
		block.style.display = 'block';
	}
}

checkActiveOrNot = function(obj, id, num)
{
	if(subItems[num])
	{
		var block = document.getElementById(id);
		if(block.style.display == 'none')
		{
			block.style.display = 'block';
		}
	}
}

childrenShow = function (obj, id)
{
	obj = obj.parentNode.parentNode;
	var block = document.getElementById(id);
	if(block.style.display == 'block')
	{
		var test = obj.className;
		
		obj.className = test.replace(/Active/,'');
		
		block.style.display = 'none';
	} else
	{
		var test = obj.className;
		obj.className = test.replace(/-act/,'');
		obj.className = obj.className + 'Active';
		block.style.display = 'block';
	}
	//alert(obj.className);
}

childrenHide = function(id)
{
	altChild = true;
	setTimeout("actionChildrenHidde("+id+")", 100);
}
actionChildrenHidde = function(id)
{
	if(altChild)
	{
		document.getElementById('topMenuItemChild'+id).style.display='none';
	}
}
childrenBlockHide = function(id)
{
	subItems[id] = false;
	setTimeout("childrenBlockHide2("+id+")", 1000);
}

childrenBlockHide2 = function(id)
{
	if(!subItems[id])
		document.getElementById('submenuContent'+id).style.display='none';
}

subscribeNewsletter = function(id)
{
	var check1 = document.getElementById('subscribeGroup1');
	var check2 = document.getElementById('subscribeGroup2');
	var check3 = document.getElementById('subscribeGroup3');
	
	if(check1.checked == false && check2.checked == false && check3.checked == false)
	{
		alert('Pasirinkite bent vieną naujienų grupę.');
		return;
	}
	
	try{
		if(!checkEmail(document.getElementById('subscribeEmail').value))
		{
			alert('Netinkamas el. pašto formatas.');
			return;
		}
	} catch(e)
	{
		alert(e);
		return;
	}
	
	$.post(
		'subscribe.php',
		$('#'+id).serialize(),
		function(data){
			alert(data);
		}
	);
}

subscribeNewsletter2 = function(id)
{
	var check1 = document.getElementById('subscribeGroup21');
	var check2 = document.getElementById('subscribeGroup22');
	var check3 = document.getElementById('subscribeGroup23');
	var check4 = document.getElementById('subscribeGroup24');
	
	if(check1.checked == false && check2.checked == false && check3.checked == false && check4.checked == false)
	{
		alert('Pasirinkite bent vieną naujienų grupę.');
		return;
	}
	
	try{
		if(!checkEmail(document.getElementById('subs_emailz').value))
		{
			alert('Netinkamas el. pašto formatas.');
			return;
		}
	} catch(e)
	{
		alert(e);
		return;
	}
	
	$.post(
		'subscribe.php',
		$('#'+id).serialize(),
		function(data){
			alert(data);
		}
	);
}

togglesubz = function(obj)
{
	if(obj.checked == true)
	{
		document.getElementById('subaiz').style.display = 'block';
	} else{
		document.getElementById('subaiz').style.display = 'none';
	}
}

	checkEmail = function(str)
	{
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1)
		{
		   //alert("Įveskite teisingą el pašto adresą");
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		   //alert("Įveskite teisingą el pašto adresą");
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		    //alert("Įveskite teisingą el pašto adresą");
		    return false;
		}

		if (str.indexOf(at,(lat+1))!=-1)
		{
		    //alert("Įveskite teisingą el pašto adresą");
		    return false;
		}

		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		{
		    //alert("Įveskite teisingą el pašto adresą");
		    return false;
		}

		if (str.indexOf(dot,(lat+2))==-1)
		{
		    //alert("Įveskite teisingą el pašto adresą");
		    return false;
		}
		
		if (str.indexOf(" ")!=-1)
		{
		    //alert("Įveskite teisingą el pašto adresą");
		    return false;
		}

 		 return true;				
	}
	
function submitForm(obj)
{
	var form = obj.parentNode.parentNode;
	form.submit();
}