function UpdateSelectListOne (attribute, newDimensionID, newDimensionLabel, p_form, p_field)
{
	var attribute_field = document.forms[p_form].elements[p_field + "_" + attribute];
	if (typeof(attribute_field) != "undefined")
		attribute_field.value = newDimensionID;

	return true;
}
function UpdateSelectList (attribute, max_values, newDimensionID, newDimensionLabel, errorMsg, p_form, p_field, p_dummy)
{
	var attribute_field = document.forms[p_form].elements[p_field + "_" + attribute];
	var dummy 	= document.forms[p_form].elements[p_dummy + "_" + attribute];

	if (typeof(attribute_field) == "undefined")
		return true;

	if (newDimensionID != "")
	{
		var dimIDs = attribute_field.value;
		var dimPrompts = dummy.value;
		
		var dimIDarray = dimIDs.split(";");
		var l_sep = "";

		if (dimIDs == "")
			l_sep = "";
		else
			l_sep = ";";

		if (max_values > dimIDarray.length)
		{
			l_found = "no";
			for (var i=0;i<dimIDarray.length;i++)
			{
				if (dimIDarray[i] == newDimensionID)
				{
					l_found = "yes";
				 }
			}
			if (l_found == "no")
			{	
				dummy.value = dimPrompts + l_sep + newDimensionLabel;
				attribute_field.value = dimIDs + l_sep + newDimensionID;

				oldInnerHtml = document.getElementById("divdummy_" + attribute).innerHTML;
				if (oldInnerHtml == "")					
				{
document.getElementById("divdummy_" + attribute).innerHTML = newDimensionLabel +  " <a href=\"#\" onClick=\"javascript:DeleteSelectedDocIDs(" + newDimensionID + "," +  attribute + ",'" + p_form + "','" + p_field + "','" + p_dummy + "'); return false;\"><img src='/ressurs/evita/iKBDesktop/images/common/slett_ikon.gif' border='0'></a>";
				}
				else
				{
					document.getElementById("divdummy_" + attribute).innerHTML = document.getElementById("divdummy_" + attribute).innerHTML + "<br>" +  newDimensionLabel + " <a href=\"#\" onClick=\"javascript:DeleteSelectedDocIDs(" + newDimensionID + "," + attribute + ",'" + p_form + "','" + p_field + "','" + p_dummy +  "'); return false;\"><img src='/ressurs/evita/iKBDesktop/images/common/slett_ikon.gif' border='0'></a>";
				}
				return true;
			}
		}
		else
			alert(errorMsg + " " + max_values + ".");
	}
}

function DeleteSelectedDocIDs (pDocID, attribute, p_form, p_field, p_dummy)
{
	var attribute_field = document.forms[p_form].elements[p_field + "_" + attribute];
	var dummy 	= document.forms[p_form].elements[p_dummy + "_" + attribute];

	if (typeof(attribute_field) == "undefined")
		return false;

	var l_Ids =  attribute_field.value.split(";");
	var l_Names =  dummy.value.split(";");

	var teller = 0;
	if (l_Ids.length == 1)
	{
		attribute_field.value = "";
		dummy.value = "";
		document.getElementById("divdummy_" + attribute).innerHTML = "";		
	}
	else
	{
		var returnIDs = new Array();
		var returnNames = new Array();
		for (var i=0;i<l_Ids.length;i++)
		{
			if (l_Ids[i] != pDocID)
			{			
				returnIDs[teller] = l_Ids[i];
				returnNames[teller] = l_Names[i];
				teller++;
			}
		}
		rebuild_sel_value_list(returnIDs,returnNames, attribute, p_form, p_field, p_dummy)
	}	
	return false;
}

function rebuild_sel_value_list(v1, v2, attribute, p_form, p_field, p_dummy)
{
	var favorite_list = document.forms[p_form].elements["p_favorite_list_" + attribute];
	var attribute_field = document.forms[p_form].elements[p_field + "_" + attribute];
	var dummy = document.forms[p_form].elements[p_dummy + "_" + attribute];
	var divdummy = document.getElementById("divdummy_" + attribute)

	var l_v3 = "";
	var l_v2 = "";
	var l_v1 = "";
	var hasFavorite = 0;
	var favCounter = 0;

   	for (i=0 ; i< v1.length; i++)
	{
		if (l_v1 == "")
			l_v1= v1[i];
		else
			l_v1 += ";" + v1[i];
	}
   	for (i=0 ; i< v2.length; i++)
	{
		if (l_v2 == "")
		{
			l_v2= v2[i];
			l_v3= v2[i] + " <a href='#' onClick=javascript:DeleteSelectedDocIDs(" + v1[i] + ",'" + attribute + "','" + p_form + "','" + p_field + "','" +  p_dummy + "'); return false;><img src='/ressurs/evita/iKBDesktop/images/common/slett_ikon.gif' border='0'></a>";
		}
		else
		{
			l_v2 += ";" + v2[i];
			l_v3 += "<br>" + v2[i] + " <a href='#' onClick=javascript:DeleteSelectedDocIDs(" + v1[i] + "," + attribute + ",'" + p_form + "','" + p_field + "','"  + p_dummy + "'); return false;><img src='/ressurs/evita/iKBDesktop/images/common/slett_ikon.gif' border='0'></a>";
		}
	}

	if (typeof(favorite_list) != "undefined")
	{ 
		if  (favorite_list.type != "hidden")
		{
				hasFavorite = 1;
				var antall = favorite_list.options.length;
				for (x=0 ; x< v1.length; x++)
				{						
					l_found = 0;
					for (i = 0; i < antall; i++)					
					{
						if (favorite_list.options[i].value == v1[x])
						{	
							l_found = 1;
							favorite_list.options[i].selected = true;
						}
					}
					if (l_found == 0)
					{
						favorite_list.options[favorite_list.options.length] = new Option(v2[x],v1[x]);
						favorite_list.options[i].selected = true;
					}
				}
			}
	}
	dummy.value = l_v2;
	attribute_field.value = l_v1;

	if (typeof(divdummy) != "undefined")
	{
		if (document.getElementById("p_max_value_" + attribute).innerHTML != "1" || hasFavorite == 0)
  			{ divdummy.innerHTML = l_v3; }
	}
	return false;
}

function call_navigators (p_TopDimensionList, p_TargetReciver, p_MaxCount, p_PreSelectedDimValues, p_PreSelectedDimIds, p_cbclosure, p_attributeID, p_page, p_target,  p_cbfunction)
{
	var l_toppdim = "";
	var dimIDarray = p_PreSelectedDimValues.split(";");
	var dimPromptarray = p_PreSelectedDimIds.split(";");

	if (p_TopDimensionList.length > 0)
	   	for (i=0 ; i<p_TopDimensionList.length; i++)
		{
			if (l_toppdim == "")
				l_toppdim = "topDimensionIDs=" + p_TopDimensionList[i];
			else
				l_toppdim += "&" + "topDimensionIDs=" + p_TopDimensionList[i];
		}

	else
		if (typeof(p_attributeID) != "undefined")
			l_toppdim = "attributeID=" + p_attributeID;

	navURL = p_page + "?" + l_toppdim + "&mode=";

	if (p_MaxCount == 1)
		navURL = navURL + "single";
	else
		navURL = navURL + "multiple";

	if (p_MaxCount == -1 || p_MaxCount == "")
		p_MaxCount = 99;

	if (p_PreSelectedDimValues != "")
		for (var i=0;i<dimIDarray.length;i++)
		{
			navURL = navURL + "&preSelDim=" + escape(dimPromptarray[i]);
		}

	navURL = navURL + "&maxCount=" + p_MaxCount + "&cbFunction=" + p_cbfunction + "&cbClosure=" + p_cbclosure;

	if (p_target == "")
		window.open(navURL, "DimensionNavigator","width=850, height=600, scrollbars, resizable=yes");
	else
		call_ikb_popup(navURL, p_target);
}

function StoreDocValues(parentWindow, attribute, v1, v2, subAttribs)
{
	if (parentWindow != null)
		parentWindow.close();
	rebuild_sel_value_list(v1,v2,attribute, "DocumentForm","p_attrib_value", "p_dummy");
}
function Ce_CalendarSetup (pInput, pButton, pFormat)
{
	Calendar.setup(
	 { 
	 inputField : pInput, // ID of the input field
	 ifFormat : pFormat, // the date format 
	 button : pButton // ID of the button
	 }
	);
}
function changeMasterFile() 
{
	var form = document.DocumentForm;
	var filename = form["p_file"].value;
	var path = filename.split("\\"); // Microsoft-specific, but we don't care.
	if (document.DocumentForm.p_title.value == "")
		document.DocumentForm.p_title.value = path[path.length-1];
}

function f_setfocus( aForm )
{
	if( aForm.elements[0]!=null) {
		var i;
		var max = aForm.length;
		for( i = 0; i < max; i++ ) {
			if( aForm.elements[ i ].type != "hidden" &&
				aForm.elements[ i ].type != "" &&
				!aForm.elements[ i ].disabled &&
				!aForm.elements[ i ].readOnly ) {				
				aForm.elements[ i ].focus();
				break;
			}
		}
	}
}

function decode(str){
	for (i = 0; i < str.length-4; i++)
	{
		if (str.substring(i,i+5) == "&amp;")
			str = str.substring(0,i)+ "&" + str.substring(i+5, str.length);
	}

	for (i = 0; i < str.length-3; i++)
	{
		if (str.substring(i,i+4) == "&lt;")
			str = str.substring(0,i)+ "<" + str.substring(i+4, str.length);
	}
	for (i = 0; i < str.length-3; i++)
	{
		if (str.substring(i,i+4) == "&gt;")
			str = str.substring(0,i)+ ">" + str.substring(i+4, str.length);
	}
	for (i = 0; i < str.length-3; i++)
	{
		if (str.substring(i,i+4) == "&#39"){
			str = str.substring(0,i)+ "'" + str.substring(i+4, str.length);
		}
	}
	for (i = 0; i < str.length-5; i++)
	{
		if (str.substring(i,i+6) == "&quot;"){
			str = str.substring(0,i)+ '"' + str.substring(i+6, str.length);
		}
	}
	return str;
}
function form_validate_number (p_this, p_from, p_to, p_err) 
{
	if ( p_this.value < p_from || p_this.value > p_to || isNaN( Math.abs(p_this.value) ) ) 
	{ 
		alert( p_err); p_this.focus; p_this.select; return false;
	}   
	return true;
}

function refreshmainwindow()
{
 	if (window.opener && !window.opener.closed) 
	{	
		window.focus();
		self.close();
	}
	else
       		history.back(-1);
}
function clear_parent ()
{
	document.getElementById("parent_id_text").innerHTML  = "";
	document.DocumentForm.p_document_id_ref.value = "";
}
function set_parent_id (closure, v1, v2)
{
	var l_v2 = "";
	var l_v1 = "";

   	for (i=0 ; i< v1.length; i++)
	{
		if (l_v1 == "")
			l_v1= v1[i];
		else
			l_v1 += ";" + v1[i];
	}
   	for (i=0 ; i< v2.length; i++)
	{
		if (l_v2 == "")
			l_v2= v2[i];
		else
			l_v2 += ";" + v2[i];
	}

	document.getElementById("parent_id_text").innerHTML = l_v2 + ' <a href="" onClick="javascript:clear_parent(); return false;"><img  src=/ressurs/evita/iKBDesktop/images/common/slett_ikon.gif border=0></a>';
	document.DocumentForm.p_document_id_ref.value = l_v1;
}

function openDynamicACLs(p_url)
{
	var navURL=p_url;
	var l_acl_id = document.DocumentForm["p_acl_id"].value;
	if (l_acl_id == "")
		l_acl_id = -1;

	navURL = navURL + "?cbFunction=" + "RefreshACLList" + "&aclID=" + l_acl_id; // NB: m�tte hardkode cbFunction
	vindu = window.open(navURL, "DynamicACLs", 'width=850, height=600, scrollbars, resizable=yes');
	vindu.focus();
}

function RefreshACLList(id, verdi)
{
// M� finne ut om verdien ligger i listen allerede */
        var iLength = document.DocumentForm('p_acl_id').length - 1;
	  var found = 0;
        for(i = iLength; i >= 0; i--)
        {
            if(document.DocumentForm('p_acl_id').options[i].value == id)
            {
            	document.DocumentForm('p_acl_id').options[i].selected = true;
			found = 1;
            }
         }
	if (found == 0)
	{
	document.DocumentForm.p_acl_id.options[document.DocumentForm.p_acl_id.options.length] = new Option(verdi, id);
	document.DocumentForm.p_acl_id.options[document.DocumentForm.p_acl_id.options.length-1].selected = true;
	}
}
function split_text_and_description(theForm, p_text_item, p_text_array)
{
 //	var form = theForm;
 	inputData = document.forms[theForm].elements[p_text_item].value;
	document.forms[theForm].elements[p_text_item].disabled = "true";
 	totalLength = inputData.length;
	if (totalLength > 30000)
	{
		data = "";
		charLeft = totalLength;
		numIteration = 0;

		while (charLeft > 0)
		{
			if (numIteration > 0)
				data = data + '<input type="hidden" name="' + p_text_array + '" value="">';
			numIteration++;
			charLeft -=30000;
		}
				// alert("Data: " + data + " Num " + numIteration);
		charLeft = totalLength;


		if (document.layers) 
		{
			document.layers.dynamic_text.document.write(data);
			document.layers.dynamic_text.document.close();
		}
		else 
			if (document.all) 
				dynamic_text.innerHTML = data;

		teller = 0;
		for (i=1; i <= numIteration; i++)
		{

			if (i == 1)
			{
				startpos = 0;
				charLeft -= 30000;
				numChar = 30000;
			}
			else
			{
				if (i < numIteration)
				{
					startpos += 30000;
					charLeft -= 30000;
					numChar = 30000;
				}
				else
				{
					startpos += 30000;
					numChar = charLeft;
				}
			}

			thisChunk = inputData.substr(startpos,numChar);
            		document.forms[theForm].elements[p_text_array][teller].value = thisChunk;

			teller += 1;
		}
	}
	else
            	document.forms[theForm].elements[p_text_array].value = inputData;
}
	
function toggle_block_none (Field, Value)
{
	if (document.getElementById(Field))
      		document.getElementById(Field).style.display = Value;
}

function toggle_formats(obj) {
//   var type = obj[obj.selectedIndex].value;
   var type = obj;
   if (type == 'file') {
      document.getElementById('p_document_format').value = "F";
      toggle_block_none("file","block");
      toggle_block_none("text","none");
      toggle_block_none("url","none");

   }
   if (type == 'url') {
      document.getElementById('p_document_format').value = "U";
      toggle_block_none("file","none");
      toggle_block_none("text","none");
      toggle_block_none("url","block");
   }
   if (type == 'text') {
      document.getElementById('p_document_format').value = "T";
      toggle_block_none("file","none");
      toggle_block_none("text","block");
      toggle_block_none("url","none");

   }

}
