function SetRadioCheck (id, title, filename, contenttype)
{
    evitaGetElementById ("ViewerlinkFileName").value = filename;
    evitaGetElementById ("ViewerlinkTitle").value = title;
    evitaGetElementById ("ViewerlinkId").value = id;
    evitaGetElementById ("ViewerlinkType").value = contenttype;

}

function check(file, title, id)
{
    var editorname 	= 'p_description';   
    if (editorname.length !=0)
        document.getElementById("linkTarget").value = document.getElementById("targetframe").value;
    document.getElementById("linkFilename").value = file;
    document.getElementById("linkTitle").value = title;
    document.getElementById("linkID").value = id;
}

function doInsertIntoEditor(strEditorName, iKnowBasePath, siteid, domainid) 
{	
	var linkFileName = evitaGetElementById ("ViewerlinkFileName").value;
	var linkTitle 	 = evitaGetElementById ("ViewerlinkTitle").value;
	var linkId 	 = evitaGetElementById ("ViewerlinkId").value;
	var linkType 	 = evitaGetElementById ("ViewerlinkType").value;
	var finaltarget;

	if (linkType == "")
		alert('Velg et dokument i listen');
	else
	{
		if (!top.opener.closed)
		{
           		//insert the link.  The insertMediaFile object is documented.

			if (evitaGetElementById ("targetframe")) 
				var target = evitaGetElementById ("targetframe").value;
			else
				var target = "_new";

			var strmedia_title = linkTitle;

			if (target == "redirect") 
				finaltarget = " onclick=\"return call_ikb_homeplace(" + linkId +",'TITLE_REDIRECT'," + siteid + "," + domainid + ");\"";	
			else
				finaltarget = " target=\"" + target + "\"";
			
			if (linkType == "FILE")
			{
	          		var strmedia_path = iKnowBasePath;
	           		var strfile_name = linkId + "/" + linkFileName;

				var htmlTMLTag = "<a href='" + strmedia_path + strfile_name + "'" + finaltarget + "'>" + strmedia_title;
			}
			else
				if ((linkFileName.indexOf("http://") == -1) && (linkFileName.indexOf("https://") == -1))					
					var htmlTMLTag = "<a href='http://" + linkFileName + "'" + finaltarget + "'>" + strmedia_title; 
				else		
					var htmlTMLTag = "<a href='" + linkFileName + "'" + finaltarget + "'>" + strmedia_title;


            		htmlTMLTag = htmlTMLTag + "<" + "/a" + ">";

            		if (strEditorName == "p_text" || strEditorName == "p_description") 
			{ 
				top.opener.eWebEditPro[strEditorName].pasteHTML(htmlTMLTag);
			  	top.close(); //close the library			
			}  
			else 
			{
				// Sjekker om det er TinyMCE istedet

				parent.tinyMCE.execCommand('mceInsertContent', false, htmlTMLTag);
				parent.tinyMCEPopup.close();

			}
		}
		else 
		{
			alert('Lenken du valgte kan ikke legges inn i dokumentet fordi editeringsvinduet ble lukket.');
		}
	}
}

function listCallback ()
{
    var list = evitaGetElementById('selectlist');

	var list_ids = new Array();
	var list_labels = new Array();
	var list_subattribs = new Array();

	// TODO: This mechanism of passing subAttribute-info MUST change!
	// We pass an array with one element for each set of subattributes
	// Each element is a TEXTUAL description of the subattributes for a single item
	// Thus, three items {a,b,c} where a and c have subattributes, will give a 
	// subattr-list with two items, containing subattrs for a and c respectiveliy

	var itemCount = 0;
	var subCount = 0;

	for (i=0; i<list.length; i++)
	{
		if (list[i].value.length == 0)
			continue;

		list_ids[itemCount] = list[i].value;
		list_labels[itemCount] = list[i].text;
		++itemCount;
		/*
		if (subAttributes[list[i].value])
		{
			list_subattribs[subCount] = subAttributes[list[i].value];
			++subCount;
		}*/
	}
	
	var cbFunction 	= 'storeValues';
	var cbClosure 	= '';
	
	var runThis = "parent.opener." + cbFunction+"(parent, cbClosure, list_ids, list_labels, list_subattribs);";
	eval(runThis);
}

function AddLink()
{
    var editorname 	= 'p_description';   
    if (editorname.length !=0)
        doInsertIntoEditor();
    else
        AddLinkToList();
}

function AddLinkToList()
{
    var file = evitaGetElementById("linkFilename").value;
    var title = evitaGetElementById("linkTitle").value;
    var id = evitaGetElementById("linkID").value;
    var target = evitaGetElementById("linkTarget").value;
    
    var maxCount = '1'; 
    var numInList = evitaGetElementById ("selectlist").length;
    if (numInList < maxCount)
    {
        var check = false;
        for (var i=0; i<evitaGetElementById ("selectlist").length; i++)
        {
            if (evitaGetElementById ("selectlist").options[i].value == id)
                check = true;
        }
        if (check == false)
        {
            listAddElement(evitaGetElementById ("selectlist"), title, id);
        }
        else
        {
            str = "Verdien er allerede i listen!";
            alert(str);
        }
    }
    else
    {
        str = "Maks antall verdier for dette attributtet er" + " " + maxCount + ".";
        alert(str); 
    }
    
}


function ReturnFromPickList(cbfunction, cbcloseure)
{
 var cbs = evitaGetElementById ("p_picklist_id");
 var txt = evitaGetElementById ("p_picklist_title");

  v1 = new Array();
  v2 = new Array();

  if (evitaGetElementById ("ViewerlinkId").value != "")
  {
		v2[0] = evitaGetElementById ("ViewerlinkTitle").value;
		v1[0] = evitaGetElementById ("ViewerlinkId").value;
  }

  if (parent.window.opener == null)
  {
	alert("Vinduet som du skal returnere til er lukket.");
	return false;
  }
  else
  {
	var runThis = "parent.opener." + cbfunction + "(parent, cbcloseure,[v1], [v2]);";
	eval(runThis);

      parent.opener.focus();

      self.close();
  }
}
