function init()
{
	i=0;
	IE=0;
	W=window.location.toString();
	if((W.search("Home")!=-1) || (W.search("home")!=-1) || (W=="http://amadoagra.eu/") || (W=="http://www.amadoagra.eu/"))
	{
		setTimeout('moveDown()',40);
	}
	else
	{
		document.getElementById('movingBackground').style.marginTop='-159px';
		document.getElementById('menu').style.display="block";
	}
	if(W.search("join")!=-1)
	{
		checkIfLoggedIn();
	}
	if(navigator.appVersion.search('MSIE 6.0')!=-1)
		document.getElementById('fixedTopContents').style.background="url('img/top_static_content.gif') no-repeat";
	else
		document.getElementById('fixedTopContents').style.background="url('img/top_static_content.png') no-repeat";
	theAParentTags=document.getElementsByTagName("div");
	for(j=0;theAParentTags[j];j++)
	{
		if(theAParentTags[j].className=="article_toggle")
		{
			theAParentTags[j].firstChild.onclick=function(){articleToggle(this);}
		}
		else if(theAParentTags[j].className=="article_comments")
		{
			C=0;
			for(k=0;theAParentTags[j].childNodes[k];k++)
			{
				if((theAParentTags[j].childNodes[k].tagName=="a") || (theAParentTags[j].childNodes[k].tagName=="A"))
				{
					if(theAParentTags[j].childNodes[k].className!='new_comment')
					{
						theAParentTags[j].childNodes[k].onclick=function(){showComments(this);};
					}
					else
					{theAParentTags[j].childNodes[k].onclick=function(){addComment(this);};}
				}
			}
		}
		else if(theAParentTags[j].className=="comments")
		{
			for(k=0;theAParentTags[j].childNodes[k];k++)
			{
				if((theAParentTags[j].childNodes[k].tagName=="a") || (theAParentTags[j].childNodes[k].tagName=="A"))
				{
					theAParentTags[j].childNodes[k].onclick=function(){hideComments(this);};
				}
			}
		}
		else if(theAParentTags[j].className=="article_text_short" || theAParentTags[j].className=="article_text_full")
		{
			angleBracket=theAParentTags[j].innerHTML;
			angleBracket=angleBracket.replace(/&lt;/g,'<');
			angleBracket=angleBracket.replace(/&gt;/g,'>');
			angleBracket=angleBracket.replace(/\n/g,'<br />');
			angleBracket=angleBracket.replace(/\s{2}/g,' &nbsp;');
			theAParentTags[j].innerHTML=angleBracket;
		}
	}
	lastLoginText=null;
}

function moveDown()
{
	i-=3;
	if(i>-160)
	{
		document.getElementById('movingBackground').style.marginTop=i+'px';
		setTimeout('moveDown()',40);
	}
	else
	{
		document.getElementById('movingBackground').style.marginTop='-159px';
		document.getElementById('menu').style.display="block";
	}
}

function showComments(the_a_object)
{
	articleNumber=the_a_object.parentNode.parentNode.getAttribute('id');
	articleNumber=articleNumber.substr(8);
	document.getElementById('comments_'+articleNumber).style.display='block';
}

function hideComments(the_a_object)
{
	the_a_object.parentNode.style.display='none';
}

function articleToggle(the_a_object)
{
	articleDiv=the_a_object.parentNode.parentNode.parentNode;
	if(the_a_object.parentNode.parentNode.className=='article_text_short')
	{
		hide='article_text_short';
		show='article_text_full';
	}
	else
	{
		hide='article_text_full';
		show='article_text_short';
	}
	for(i=0;articleDiv.childNodes[i];i++)
	{
		if(articleDiv.childNodes[i].className==hide)
			articleDiv.childNodes[i].style.display='none';
		if(articleDiv.childNodes[i].className==show)
			articleDiv.childNodes[i].style.display='block';
	}
}

function addComment(the_a_object)
{
	commentsDiv=the_a_object.parentNode;
	for(i=0;commentsDiv.childNodes[i];i++)
	{
		if(commentsDiv.childNodes[i].className=='add_comment')
			commentsDiv.childNodes[i].style.display='block';
	}
}

function ajaxFunction()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  return xmlHttp;
  }

function sendComment(buttonElement)
{
	formElement=buttonElement.parentNode;
	firstInput=0;
	params='?';
	aX=ajaxFunction();
	for(t=0;formElement.childNodes[t];t++)
	{
		if(formElement.childNodes[t].tagName=='input' || formElement.childNodes[t].tagName=='textarea' || formElement.childNodes[t].tagName=='INPUT' || formElement.childNodes[t].tagName=='TEXTAREA')
		{
			if(formElement.childNodes[t].type!='button')
			{
				if(firstInput)
				{
					params+='&';
				}
				params+=formElement.childNodes[t].name+"="+formElement.childNodes[t].value;
				firstInput++;
			}
		}
	}
	params+="&file=procSet";
	params=encodeURI(params);
		aX.onreadystatechange = reloadPage;
	/*
	aX.onreadystatechange = function()
		{
			if (aX.readyState == 4)
			{
				if (aX.status == 200)
				{
					alert(aX.responseText);
				}
			}
		}
	*/
	aX.open('get','ajaxIncludes.php'+params,true);
	aX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	aX.setRequestHeader("Content-length", "0");
	aX.setRequestHeader("Connection", "close");
	aX.send(null);
}

function reloadPage()
{
	if (aX.readyState == 4)
	{
		if (aX.status == 200)
		{
			window.location=window.location;
		}
	}
}

function showPicture(poza)
{
	scrolledTop=window.document.documentElement.scrollTop;
	scrolledLeft=window.document.documentElement.scrollLeft;
	document.getElementById(poza).style.display='block';
	document.getElementById('fade').style.display='block';
	document.body.style.overflow='hidden';
	window.document.documentElement.style.overflow='hidden';
	x=document.getElementById(poza);
	document.body.appendChild(x);
	containerHeight=x.scrollHeight;
	containerWidth=x.scrollWidth;
	if(window.innerWidth)
		windowWidth=window.innerWidth;
	else
		windowWidth=window.document.documentElement.offsetWidth;
	if(window.innerHeight)
		windowHeight=window.innerHeight;
	else
		windowHeight=window.document.documentElement.offsetHeight;
	if(containerHeight>windowHeight)
	{
		pictureAspectRatio=x.firstChild.scrollWidth/x.firstChild.scrollHeight;
		x.firstChild.style.height=(windowHeight*0.8)+'px';
		x.firstChild.style.width=(x.firstChild.scrollHeight*pictureAspectRatio)+'px';
		containerHeight=x.scrollHeight;
		containerWidth=x.scrollWidth;
	}
	if(containerWidth>windowWidth)
	{
		pictureAspectRatio=x.firstChild.scrollWidth/x.firstChild.scrollHeight;
		x.firstChild.style.width=(windowWidth*0.8)+'px';
		x.firstChild.style.height=(pictureAspectRatio*x.firstChild.scrollWidth)+'px';
		containerHeight=x.scrollHeight;
		containerWidth=x.scrollWidth;
	}
	window.document.documentElement.scrollTop=0;
	window.document.documentElement.scrollLeft=0;
	containerWidth=x.firstChild.width+33;
	document.getElementById(poza).style.top=((windowHeight-containerHeight)/2)+'px';
	document.getElementById(poza).style.left=((windowWidth-containerWidth)/2)+'px';
	document.getElementById(poza).style.width=containerWidth+'px';
}

function closePicture(poza)
{
	document.getElementById(poza).style.display='none';
	document.getElementById('fade').style.display='none';
	document.body.style.overflow='auto';
	window.document.documentElement.style.overflow='auto';
	document.body.style.overflowX='hidden';
	window.document.documentElement.style.overflowX='hidden';
	document.body.scrollTop=scrolledTop;
	window.document.documentElement.scrollTop=scrolledTop;
}

function register()
{
	r=ajaxFunction();
	registerForm=document.getElementById('sign_in_form');
	if((registerForm.password.value == registerForm.passconfirm.value) && (document.getElementById('sign_in_form').terms.checked))
	{
		params='?';
		for(tabl=0;registerForm.childNodes[tabl];tabl++)
			if((registerForm.childNodes[tabl].tagName=="table") || (registerForm.childNodes[tabl].tagName=="TABLE"))
				for(tbod=0;registerForm.childNodes[tabl].childNodes[tbod];tbod++)
					if((registerForm.childNodes[tabl].childNodes[tbod].tagName=="tbody") || (registerForm.childNodes[tabl].childNodes[tbod].tagName=="TBODY"))
						for(tr=0;registerForm.childNodes[tabl].childNodes[tbod].childNodes[tr];tr++)
							if(registerForm.childNodes[tabl].childNodes[tbod].childNodes[tr].tagName=="tr" || registerForm.childNodes[tabl].childNodes[tbod].childNodes[tr].tagName=="TR")
								for(td=0;registerForm.childNodes[tabl].childNodes[tbod].childNodes[tr].childNodes[td];td++)
									if(registerForm.childNodes[tabl].childNodes[tbod].childNodes[tr].childNodes[td].className=='column_2')
										for(inpu=0;registerForm.childNodes[tabl].childNodes[tbod].childNodes[tr].childNodes[td].childNodes[inpu];inpu++)
											if(registerForm.childNodes[tabl].childNodes[tbod].childNodes[tr].childNodes[td].childNodes[inpu].tagName=='input' || registerForm.childNodes[tabl].childNodes[tbod].childNodes[tr].childNodes[td].childNodes[inpu].tagName=='INPUT' || registerForm.childNodes[tabl].childNodes[tbod].childNodes[tr].childNodes[td].childNodes[inpu].tagName=='textarea' || registerForm.childNodes[tabl].childNodes[tbod].childNodes[tr].childNodes[td].childNodes[inpu].tagName=='TEXTAREA')
												if((registerForm.childNodes[tabl].childNodes[tbod].childNodes[tr].childNodes[td].childNodes[inpu].type!='button') && (registerForm.childNodes[tabl].childNodes[tbod].childNodes[tr].childNodes[td].childNodes[inpu].name!='passconfirm'))
													params+=registerForm.childNodes[tabl].childNodes[tbod].childNodes[tr].childNodes[td].childNodes[inpu].name+"="+registerForm.childNodes[tabl].childNodes[tbod].childNodes[tr].childNodes[td].childNodes[inpu].value+"&";
		params+="status=0&set=1&table=WebFurnace_users&headC=11";
		/**/
		r.onreadystatechange = function()
		{
			if (r.readyState == 4)
			{
				if (r.status == 200)
				{
					if(document.getElementById('response')){document.getElementById('response').parentNode.removeChild(document.getElementById('response'));}
					responseDiv=document.createElement('div');
					responseDiv.setAttribute('id','response');
					responseDiv.style.marginTop='15px';
					responseDiv.appendChild(document.createTextNode("Your request has been sent to the site administrator to be processed."));
					document.getElementById('left_column').appendChild(responseDiv);
					registerForm.parentNode.removeChild(registerForm);
					theForms=document.getElementsByTagName('form');
					for(f=0;theForms[f];f++)
					{
						theForms[f].reset();
					}
				}
			}
		};
		params+="&file=procSet";
		params=encodeURI(params);
		r.open('get','ajaxIncludes.php'+params,true);
		r.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		r.setRequestHeader("Content-length", "0");
		r.setRequestHeader("Connection", "close");
		r.send(null);
	}
	else if(registerForm.password.value != registerForm.passconfirm.value)
	{
		alert('The passwords you used are not identical!');
	}
	else
	{
		alert('You must agree to the terms of use!');
	}
}

function checkIfLoggedIn()
{
	cL=ajaxFunction();
	cL.onreadystatechange = function()
	{
		if(cL.readyState==4)
		{
			if(cL.status==200)
			{
				logged=cL.responseText;
				if(logged.substr(-3).search('yes')!=-1)
				{
					document.getElementById('logged_in').style.display='block';
					document.getElementById('logged_out').style.display='none';
					loggedIn='yes';
					nicEditors.allTextAreas();
				}
				else
				{
					document.getElementById('logged_in').style.display='none';
					document.getElementById('logged_out').style.display='block';
					loggedIn='no';
				}
			}
		}
	};
	cL.open('get','ajaxIncludes.php?file=logged',true);
	cL.send(null);
}

function login()
{
	u=document.getElementById('log_in_name');
	p=document.getElementById('log_in_pass');
	params="username="+u.value+"&password="+p.value;
	params+="&action=login&file=procIni";
	params=encodeURI(params);
	L=ajaxFunction();
	L.onreadystatechange = function(){if(L.readyState==4){if(L.status==200){
		SIDStart=L.responseText.search("session id: ");
		SIDEnd=L.responseText.search("!!!");
		SID=L.responseText.substr(SIDStart,SIDEnd-SIDStart);
		window.location=window.location;
	}}};
	L.open('get','ajaxIncludes.php?'+params,true);
	L.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	L.setRequestHeader("Content-length", params.length);
	L.setRequestHeader("Connection", "close");
	L.send(null);
}

function showLoggedIn()
{
	loggedInDiv=document.createElement("div");
	loggedInDiv.setAttribute('id','log_out');
	welcome=document.createTextNode('Bine ai venit dragă Vlad!');
	thisDate=new Date();
	thisDateDiv=document.createElement("div");
	theDate=document.createTextNode("Te-ai logat la "+thisDate.getHours()+":"+thisDate.getMinutes()+".");
	lastLoginDiv=document.createElement("div");
	if(lastLoginText)
	{
		lastLoginText="Ultima oară te-ai autentificat la "+lastLoginDate.getHours()+":"+lastLoginDate.getMinutes()+".";
		lastLoginDate=thisDate;
	}
	else
	{
		//lastLoginText="E prima oară când te autentifici.";
		lastLoginText="Ultima oară te-ai autentificat la 7:00";
		lastLoginDate=thisDate;
	}
	lastLoginDiv.appendChild(document.createTextNode(lastLoginText));
	logOutButton=document.createElement("input");
	logOutButton.setAttribute('type','button');
	logOutButton.setAttribute('name','log_out');
	logOutButton.setAttribute('value','Dezautentifică-mă');
	logOutButton.onclick=logout;
	thisDateDiv.appendChild(theDate);
	changePassDiv=document.createElement("div");
	changePassDiv.appendChild(document.createTextNode("> "));
	changePass=document.createElement("a");
	changePass.appendChild(document.createTextNode("Schimbare parolă"));
	changePass.setAttribute("href","#");
	changePassDiv.appendChild(changePass);
	changeCInfoDiv=document.createElement("div");
	changeCInfoDiv.appendChild(document.createTextNode("> "));
	changeCInfo=document.createElement("a");
	changeCInfo.appendChild(document.createTextNode("Schimbare date contact"));
	changeCInfo.setAttribute("href","#");
	changeCInfoDiv.appendChild(changeCInfo);

	loggedInDiv.appendChild(welcome);
	loggedInDiv.appendChild(thisDateDiv);
	loggedInDiv.appendChild(lastLoginDiv);
	loggedInDiv.appendChild(document.createElement("br"));
	loggedInDiv.appendChild(changePassDiv);
	loggedInDiv.appendChild(changeCInfoDiv);
	loggedInDiv.appendChild(document.createElement("br"));
	loggedInDiv.appendChild(logOutButton);
	document.getElementById('right_column').appendChild(loggedInDiv);
	cookiesString='lastLogin='+lastLoginDate.getMilliseconds+';expires='+(lastLoginDate.getMilliseconds()+86400);//Fri, 17 Dec 2010 10:00:00 GMT';
	document.cookies=cookiesString;
	//alert(document.cookies);
}

function logout()
{
	Lo=ajaxFunction();
	Lo.onreadystatechange = function(){if(Lo.readyState==4){if(Lo.status==200){window.location=window.location;}}};
	Lo.open('get','ajaxIncludes.php?file=procIni&action=logout',true);
	Lo.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	Lo.setRequestHeader("Content-length", "0");
	Lo.setRequestHeader("Connection", "close");
	Lo.send(null);
}
