//ajax engine
function smiley(form,field,emoticon)
{
   	theString = document.getElementById(field);

	theString.value = theString.value + emoticon;
	return;
}
function tilfoejSmiley (id,string)
{
   	theString = document.getElementById(id);

	theString.value = theString.value + string;
}
function createXMLHTTPRequest() {
try {
request = new XMLHttpRequest();
} catch(fejl1) {
  try {
  request = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (fejl2) {
    try {
    request = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (fejl3) {
      request = false;
    }
  }
}
return request;
}
function alterall(f)
{
for(i=0;f.elements[i];i++)
{
e = f.elements[i];
if(e.type=="checkbox")
{
e.checked = (e.checked)?false:true;
}
}
}
function bbcodeLink (){
	var url=prompt("Hjemmesideadresse","http://www.");
	var titel=prompt("Link beskrivelse","");
	if ( url != null && titel != null )
	{
		document.personelig.profiltekst.value=document.personelig.profiltekst.value + '' + "[url="+url+"]"+titel+"[/url]";
	}
}

function bbcodeBillede(){
	var img=prompt("Billede sti","http://www.");
	if ( img != null )
	{
		document.personelig.profiltekst.value=document.personelig.profiltekst.value + "[img]"+img+"[/img]";
	}
}

function bbcodeMail ()
{
	var mail=prompt("Email adresse","");
	var titel=prompt("Email beskrivelse","");
	if ( mail != null && titel != null )
	{
		document.personelig.profiltekst.value=document.personelig.profiltekst.value + "[mail="+mail+"]"+titel+"[/mail]";
	}
}

function bbcode (tag)
{
		document.personelig.profiltekst.value=document.personelig.profiltekst.value + '' + tag + '';
}

function $(id) { return document.getElementById(id); }

function isIE() {
	return navigator.appName == 'Microsoft Internet Explorer';
}

function show(id) {
	$(id).style.display = '';
}

function hide(id) {
	$(id).style.display = 'none';
}

function lukGbAnswer() {
	setTimeout("lukGbAnswerClose();", 2000);
}

function lukGbAnswerClose() {
	window.close();
}

var gbMessages = createXMLHTTPRequest();

function gbMessage()
{
  	var MessageUrl = 'gbMessages.php';
  	gbMessages.open("GET", MessageUrl, true);
  	gbMessages.onreadystatechange = responsGbMessage;
  	gbMessages.send(null);

}

function responsGbMessage()
{

	if (gbMessages.readyState == 4)
	{
    		if(gbMessages.status == 200)
		{
			var theMessage = gbMessages.responseText;
			if( theMessage != 0 )
			{
				alert("Du har modtaget nye beskeder i din gæstebog!");
			}
      			setTimeout("gbMessage();", 1000);
    		}
	}
}

function fadeIn(id, speed) {
	if(!speed) speed = 80;
	show(id);
	for(var i=1; 5>=i; i++) {
		var opacity = i*0.2;
		if(i==1) setOpacity(id, opacity);
		else setTimeout("setOpacity('"+id+"', '"+(opacity)+"')", (i-1)*speed);
	}
}

function fadeOut(id, speed) {
	if(isIE()) {
		hide(id);
	}else{
		if($(id).style.display != 'none') {
			if(!speed) speed = 80;
			for(var i=1; 5>=i; i++) {
				setTimeout("setOpacity('"+id+"', '"+(1-i*0.2)+"')", i*speed);
			}
			setTimeout("hide('"+id+"')", 5*speed);
		}
	}
}

function fade(id) {
	if($(id).style.display == 'none') {
		fadeIn(id);
	}else {
		fadeOut(id);
	}
}

function tilfoejSmiley (smiley)
{
	besked = opener.document.getElementById("besked");
	besked.value = besked.value + smiley;
}

function smileyPopup()  {
	window.open('http://vux.dk/smiley.php','Smiley','height=125,width=135');
}

function gbAnswer(modtager)  {
	window.open('http://vux.dk/gbanswer.php?modtager=' + modtager,'Gæstebog','height=155,width=565');
}

function profilBillede(billede,height,width)  {
	window.open(billede,'Profilbillede','height='+height+',width='+width+'');
}

function galleriBillede(billede,height,width)  {
	window.open(billede,'Profilbillede','height='+height+',width='+width+'');
}


function setOpacity(id, newOpacity) {
	if(isIE()) {
		$(id).style.filter="alpha(opacity="+newOpacity+")";
	} else {
		if(newOpacity == 1) newOpacity = null;
		$(id).style.opacity = newOpacity;
	}
	if(newOpacity == 0) hide(id);
	else {
		if($(id).style.display == 'none') show(id);
	}
}

function openId(id) {
	for(var i=1; 5>=i; i++) {
		var newHeight = i*20;
		setTimeout("setWH('"+id+"', '"+newHeight+"px', 'height')", i*80);
	}
}
function closeId(id) {
	for(var i=1; 5>=i; i++) {
		var newHeight = 100-i*20;
		setTimeout("setWH('"+id+"', '"+newHeight+"px', 'height')", i*80);
	}
	setTimeout("hide('"+id+"')", 400)
	setTimeout("newMessage();", 5000)
}
function setWH(id, wh, typeWh) {
	if(typeWh == "height") {
		if($(id).style.height == "0px") show(id);
		if($(id).style.display == "none") show(id);
		$(id).style.height = wh;
	}
	if(typeWh == "width") {
		if($(id).style.width == "0px") show(id);
		if($(id).style.display == "none") show(id);
		$(id).style.width = wh;
	}
}

function helpBox() {
	if($('helpBox').style.display == 'none') {
		fadeIn('helpBox');
	}else{
		fadeOut('helpBox');
	}
}

function mOver(elm){
	elm.style.backgroundColor = "#F4F4F4";
}

function mOut(elm){
	elm.style.backgroundColor = "#FFFFFF";
}
function ajaxRequest() {
	try {
		request = new XMLHttpRequest();
	} catch(fejl1) {
		try {
	  		request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (fejl2) {
		    try {
		    	request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (fejl3) {
				request = false;
			}
		}
	}
	return request;
}

var http = ajaxRequest();

function newMessage() {
	date = new Date().getTime();
	url = '/js/ajaxEngine/newMessages.php?date='+date;
	http.open("GET", url, true);
	http.onreadystatechange = newMessageResponse;
	http.send(null);
}

function newMessageResponse() {
	if (http.readyState == 4) {
    	if(http.status == 200) {
      		var newMessage = http.responseText;
      		if(newMessage != 0) {
				openId("newMessage")
			}else{
      			setTimeout("newMessage()", 10000);
      		}
    	}
	}
}
window.name="mymain";
