// ToolTip v1.0
// @head	= The tooltip heading
// @txt		= The tooltip text
function tooltip(head,txt,topMargin,sideMargin) {

	var toolTipObj = document.all['tooltipBox'];


	if (head != '' && txt != '') {

		// HTML code for the tooltip table
		text = 	'<table style="border: 1px solid black;" cellpadding=0 cellspacing=0>' +
				'<tr><td>' + head + '</td></tr>' +
				'<tr><td>' + txt + '</td></tr></table>';

		toolTipObj .innerHTML = text;


		// Find the max width and height
		var maxWidth 	= document.body.clientWidth - toolTipObj.clientWidth + document.body.scrollLeft;
		var maxHeight	= document.body.clientHeight - toolTipObj.clientHeight + document.body.scrollTop;
		// Place the layer where the mouse is
		document.all['tooltipBox'].style.top = topMargin + document.body.scrollTop;
		document.all['tooltipBox'].style.right = sideMargin + document.body.scrollLeft;

		// If the layer is outside the screen, then move it back on
		var layerLeft = parseInt(toolTipObj.style.left,10);
		var layerTop = parseInt(toolTipObj.style.top,10);

		if (layerTop > maxHeight) {
			toolTipObj.style.top = maxHeight-40;
		}
		if (layerLeft > maxWidth) {
			toolTipObj.style.left = maxWidth;

		}

		toolTipObj.style.visibility = 'visible';
		document.getElementById("kategori").style.visibility="hidden";
		document.getElementById("tilgang").style.visibility="hidden";

	} else {

		toolTipObj.style.visibility = 'hidden';
        document.getElementById("kategori").style.visibility="visible";
        document.getElementById("tilgang").style.visibility="visible";

	}
}

function insertAtCursor(myField, myValue) {
    //IE support
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
        //MOZILLA/NETSCAPE support
        else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
        + myValue
        + myField.value.substring(endPos, myField.value.length);
    } else {
        myField.value += myValue;
    }
}

function open_hyperlink(textAreaID){
  window.open("hyperlink.php?textAreaID="+textAreaID,"HTML","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=yes,width=500,height=150")}

function open_galimg(textAreaID){
  window.open("galimg.php?textAreaID="+textAreaID+"&what=imgTag","HTML","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width=800,height=350")}

function HTMLHelp() {
	var closeIconPath = 'bilder/close.gif';
	licHead = '<table style="background: #3DC606; border-bottom: 1px solid black;" width="100%">';
	licHead += '<tr><td align="left"><b>Retted3 - HTML Hjelp</b></td>';
	licHead += '<td align="right"><img style="cursor:hand;" onClick="tooltip(\'\',\'\')" src="' + closeIconPath + '" border="0"></td></tr></table>';

	licBody = '<table  style="background: #f0f0f0;" width="650">';
 	licBody += '<TR><TD colspan="2">Forklaringer rundt formateringsknappene i Retted Artikkel. Knappene kan brukes på merket tekst eller før du skriver tekststrengen som skal formateres.</TD></TR>' +
    '<TR><TD><b>B</b></TD><TD>Fet font. Krever både start- og slutt-tag</TD></TR>' +
 	'<TR><TD><b>I:</b></TD><TD>Kursiv font. Krever både start- og slutt-tag</TD></TR>' +
    '<TR><TD><b>U:</b></TD><TD>Understreket font. Krever både start- og slutt-tag</TD></TR>' +
    '<TR><TD><b>S:</b></TD><TD>Gjennomstreket font. Krever både start- og slutt-tag</TD></TR>' +
    '<TR><TD><b>Link:</b></TD><TD>Hyperlink som åpner seg i samme eller nytt vindu som artikkelen.</TD></TR>' +
    '<TR><TD><b>Bildegalleri:</b></TD><TD>Her har du muligheten til å legge inn et bilde fra galleriet inn i artikkelen.</TD></TR>' +
    '<TR><TD><b>Lukk Tags:</b></TD><TD>Denne knappen lukker eventuelle tag\'er som ikke er lukket (og som må lukkes)</TD></TR>' +
    '<TR><TD><b>Søk:</b></TD><TD>Denne knappen søker opp merket tekst evt gir deg muligheten til å skrive inn søketekst</TD></TR>' +
    '<TR><TD colspan="2">Ikke-lukkede tag\'er som skal lukkes kan identifiseres ved at den tilhørende knappen har en / foran seg.</TD></TR>';



	licBody += '</table>';

	tooltip(licHead,licBody,10,10);
}

function CreateHyperLink(id){
	var closeIconPath = 'bilder/close.gif';

	licHead = '<table style="background: #3DC606; border-bottom: 1px solid black;" width="100%">';
	licHead += '<tr><td align="left"><b>Hyperlinkgenerator ' + id + ':</b></td>';
	licHead += '<td align="right"><img style="cursor:hand;" onClick="tooltip(\'\',\'\')" src="' + closeIconPath + '" border="0"></td></tr></table>';

	licBody = '<table  style="background: #f0f0f0;" width="650">';
 	licBody +='<TR><TD><b>Link:</b></TD><TD><input type="text" id="URL_adr" onBlur="javascript:setHTML()" size="40"></TD></TR>' +
 	'<TR><TD><b>Linket tekst:</b></TD><TD><input type="text" id="URL_tekst" onBlur="javascript:setHTML()" size="40"></TD></TR>' +
	'<TR><TD><b>Målvindu:</b></TD><TD><select id="URL_vindu" onChange="javascript:setHTML()">' +
	'<option value="">Samme vindu</option>' +
	'<option value=" target=\'_blank\'">Nytt vindu</option></select> (skal linken åpnes i nytt vindu?)</TD></TR>' +
	'<TR><TD><b>HTML:</b></TD><TD><input type="text" id="URL_html" size="60"></TD></TR>';
	licBody += '<TR><TD><input type="button" value="Lim inn link" onClick="javascript:pasteHTML()">';
	licBody += '<input type="button" value="Lukk" onClick="tooltip(\'\',\'\')"></TD></TR>';
	licBody += '<input type="hidden" id="URL_targetID" value="'+id+'">';
	licBody += '</form></table>';

	tooltip(licHead,licBody,300,100);
}

function setHTML(){
    	adr = document.getElementById('URL_adr').value;
    	tekst = document.getElementById('URL_tekst').value;
    	vindu = document.getElementById('URL_vindu').value;

    	document.getElementById('URL_html').value = '<a href=\''+adr+'\''+vindu+'>'+tekst+'</a>';
}

function setPicHTML(){
    	file = document.getElementById('PIC_file').value;
    	size = document.getElementById('size').value;
        document.getElementById('URL_html').value = file+'&size='+size;
}

function pasteHTML(imgURL){
    	var controlID=document.getElementById('URL_targetID').value;
		
    	window.opener.document.getElementById(controlID).value = imgURL;

    	window.close();
}

function pasteHTMLatCursor(imgURL){
    	var controlID=document.getElementById('URL_targetID').value;
    	var alt=document.getElementById('alt').value;
    	
    	var URL = '<img src=\''+imgURL+'\' alt=\''+alt+'\'>';
		
    	//window.opener.document.getElementById(controlID).value = imgURL;
		insertAtParentCursor(window.opener.document.getElementById(controlID), URL);
    	window.close();
}

function insertAtParentCursor(myField, myValue) {
    //IE support
    if (window.opener.document.selection) {
        myField.focus();
        sel = window.opener.document.selection.createRange();
        sel.text = myValue;
    }
        //MOZILLA/NETSCAPE support
        else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
        + myValue
        + myField.value.substring(endPos, myField.value.length);
    } else {
        myField.value += myValue;
    }
}

function previewArticle(artID){
  window.open("previewArticle.php?id="+artID,"HTML","toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width=600,height=500")
}




