
// sends a html site's content to an email
function SendOrderReceipt(url, email, order_head_id) {
	document.location = "order_edit_send_receipt.asp?url=" + escape(url) + "&email=" + email + "&next=" + escape("order_edit.asp?order_head_id=" + order_head_id);
}

function remove_field(sType, src, hidden){
	var scrList=src;
	var l = scrList.length;
	var i = 0;
	for (i = l-1; i > -1; i--) {
		if(scrList.options[i].selected | sType=='a'){
			//scrList.options.remove(i);
			scrList.options[i]=null;
		}
	}

	update_multiple_select_fields(src, hidden);
}

function add_field(src, dest, hidden){
	if (src.selectedIndex!=-1) {
		var scrList=src;
		var TabText = src.options[src.selectedIndex].text;
		var TabValue = src.options[src.selectedIndex].value;
		var l = scrList.length;
		var i = 0;
		for (i = l-1; i > -1; i--) {
			if(scrList.options[i].selected){
			sValue = scrList.options[i].value;
			sValue=name_only(sValue);
			sText = scrList.options[i].text;
			n = dest.length; 
			dest.options[n] = new Option(sText,sValue,false,false); 
			}
		}

		update_multiple_select_fields(dest, hidden);
	}

}

function update_multiple_select_fields(src, hidden){
	var l = src.length;
	var i = 0;
	var temp = '0';
	for (i = l-1; i > -1; i--) {
		if (temp.indexOf(src.options[i].value)==-1) {
			temp = temp + ',' + src.options[i].value;
		}
	}

	document.getElementById(hidden).value=temp;
}

function insertAttachment(obj){
	var arr = null;
	loc = "object_frame.asp?EDITOR=J"
	arr = window.showModalDialog(loc , "", "resizable:yes;help:no;status:no;dialogHeight:40em;dialogWidth:62em;height:650px;width:1000px")
	if (arr != null) {
		document.getElementById(obj).value = arr;
	}
}

// Function that executes function inside input string when enter is pressed.
function doKey(sFunc) {
	if (document.all) {
		whichASC = event.keyCode;
		if(whichASC==13){
			eval(sFunc);
		}       
	}
}

/*	
	NAME: setClass
	FUNC: Will change the class of provided object. Nice for onMouseOver effects.
*/
function setClass(thisobject, className){
	thisobject.className = className;
}

/* 
	Use getElementsBySelector function to select all classes of a certain name
	and then assign functions to those classes
*/
function assignFunctions(){
	assignthese(document.getElementsBySelector('.main_bigbutton'), 'main_bigbutton_over', 'main_bigbutton');
	assignthese(document.getElementsBySelector('.menu_tab_deselected'), 'menu_tab_over', 'menu_tab_deselected');
	assignthese(document.getElementsBySelector('tr td.row_ascending'), 'row_ascending_over', 'row_ascending');
	assignthese(document.getElementsBySelector('tr td.row_descending'), 'row_descending_over', 'row_descending');
	assignthese(document.getElementsBySelector('tr td.row_ASC'), 'row_ascending_over', 'row_ascending');
	assignthese(document.getElementsBySelector('tr td.row_DESC'), 'row_descending_over', 'row_descending');
	assignthese(document.getElementsBySelector('tr td.row_deselected'), 'row_descending_over', 'row_deselected');
	assignthese(document.getElementsBySelector('div.bt'), 'bt_over', 'bt');
	assignthese(document.getElementsBySelector('div.thumbnail'), 'thumbnail_over', 'thumbnail');
}

/*
	Helper function for assignFunctions - this one does the actual assigning of 
	functions.
*/
function assignthese(a, onMouseOver, onMouseOut) {
	for (i = 0; i != a.length; i++) {
		a[i].onmouseover = function() {this.className = onMouseOver}
		a[i].onmouseout = function() {this.className = onMouseOut}
	}
}

/* new on mouse over function */
function over (obj){
	obj.className=obj.className+'_over';
}

/* new on mouse out function */
function out (obj){
	var str=obj.className;
	var pos=str.indexOf("_over")
	obj.className=str.substr(0,pos);
}

/* 
	This onclick event gets triggered when a folder is clicked.
	It is supposed to display that folder contents on the frame named 'hoved'
	numID = id of the clicked folder.
*/
function dispMenu(numID,sMode){
	document.HIDDEN_FIELDS.HIDDEN_MENU_ID.value = numID;
	//document.HIDDEN_FIELDS.JSMENU_OBJECT_ID.value = eval('tree'+document.HIDDEN_FIELDS.LANG.value).getSelected().id;

	/* Only redirect parent.hoved if sMode != '1' */
	if ((sMode == '') || (sMode == '2')){
		//parent.hoved.location='browse.asp?id='+numID+'&mode='+sMode+'&lang='+document.HIDDEN_FIELDS.LANGUAGE_ID.value;
		parent.hoved.location='browse.asp?id='+numID+'&mode='+sMode+"&command="+document.HIDDEN_FIELDS.COMMAND.value+"&objid="+document.HIDDEN_FIELDS.OBJID.value;
		//getSubMenu(numID);
	}
}

function dispLang(tree, lang, mode) {
	tree.getSelected().expand();

	if (mode == '') {
		document.HIDDEN_FIELDS.HIDDEN_MENU_ID.value = '';
		AJAXset_lang(lang);
		parent.hoved.location='start_frame.asp?view=start&amp;'
	} else {
		document.HIDDEN_FIELDS.LANG.value = lang;
		document.HIDDEN_FIELDS.LANGUAGE_ID.value = lang;
	}
}
/* 
	Where to go when the root of the folder menu is clicked 
*/
function goHome(sMode){
	/* If sMode contains anything, select root folder for folder return */
	if (sMode == ''){
		parent.hoved.location='start_frame.asp?view=start';
	}
	document.HIDDEN_FIELDS.HIDDEN_MENU_ID.value='';

}

/*
	When clicking a folder inside the file archive, what do then? 
*/
function dispArch(numID,sMode){
	document.HIDDEN_FIELDS.HIDDEN_MENU_ID.value = numID;
	/* Only redirect parent.hoved if sMode = '' */
	if (sMode == ''){
		parent.hoved.location='object_frame.asp?OBJECT_GROUP_ID='+numID+'&EDITOR='+document.HIDDEN_FIELDS.EDITOR.value+'&FIX='+document.HIDDEN_FIELDS.EDITOR.value;
	}
}

function clientDispArch(numID,sMode){
	document.HIDDEN_FIELDS.HIDDEN_MENU_ID.value = numID;
	/* Only redirect parent.hoved if sMode = '' */
	if (sMode == ''){
		//parent.hoved.location='object_frame.asp?OBJECT_GROUP_ID='+numID+'&EDITOR='+document.HIDDEN_FIELDS.EDITOR.value+'&FIX='+document.HIDDEN_FIELDS.EDITOR.value;
		document.location='client_filearchive.asp?folder=' + numID;
	}
}
/* 
	Where to go when the root of the filearchive is clicked 
*/
function goArchHome(sMode){
	if (sMode == ''){
		document.HIDDEN_FIELDS.HIDDEN_MENU_ID.value = '';
		parent.hoved.location='start_frame.asp?view=start_filearchive&EDITOR='+document.HIDDEN_FIELDS.EDITOR.value+"&OBJECT_GROUP_ID=-1";
	}
}

/* 
	Action for admin menu
*/
function goAdmin(sFile){
	parent.hoved.location=sFile;
}
function goCalculator(sFile){
	 loc = sFile;
	 myWin1 = window.open(loc, "helpWin","width=240,height=410,resizable=yes,status=no,toolbar=no,menubar=no,scrollbars=no");
	 myWin1.focus();
}

/* search ignitor */
function search(){
	document.FORM1.action='find_advanced.asp';
	document.FORM1.submit();
}

/* Display a help record */
function help_rec(menu,id){
	 loc = "help/help_frame.asp?menu=" + menu + "&id=" + id;
	 loc = "http://login.avento.no/dev/help/help_frame.asp?menu=" + menu + "&id=" + id;
	 myWin1 = window.open(loc, "helpWin","width=750,height=500,resizable=yes,status=no,toolbar=no,menubar=no,scrollbars=no");
	 myWin1.focus();
	
	//loc = "help/help_frame.asp?menu=" + menu + "&id=" + id;
	//vReturnValue = window.showModelessDialog(loc , null, "dialogHeight:40;dialogWidth:43")
}

function help_menu(menuid){
	parent.hoved.location='default.asp?menu='+menuid;
}

/* Add to favorites function */
function add_favorite(s,icon){
	loc = "add_favorite_frame.asp?suggestedname="+s+'&icon='+icon;
	myWin1 = window.open(loc, "addFavWin","width=400,height=300,resizable=no,status=yes,toolbar=no,menubar=no,scrollbars=no");
	myWin1.focus();	
}

/* Load a favorite */
function goFavorite(strMENY_URL,strHOVED_URL){
	window.location=strMENY_URL
	window.location=strHOVED_URL;
}



/* Functions for navigating between main tabs. */

	function content(){
		document.location='menu.asp';
		parent.hoved.location='start_frame.asp?view=start';
	}
	function filearchive(){
		document.location='menu_filearchive.asp';
		parent.hoved.location='start_frame.asp?view=start_filearchive';
	}
	function webshop(){
		document.location='menu_webshop.asp';
		parent.hoved.location='start_frame.asp?view=start_webshop';
	}
	function admin(){
		document.location='menu_admin.asp';
		parent.hoved.location='start_frame.asp?view=start_admin';
	}

/* some helper functions for string manipulation */
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

/* two functions for sending sms and email. requires FORM1.SQL.value containing
   SQL string with fields EMAIL and/or MOBILE, FNAME, LNAME.
   SQL must be server.URLEncode */
function open_sms(main_id,category,sub_id_field){
	loc = 'send_sms.asp?SQL='+document.FORM1.SQL.value+'&MAIN_ID='+main_id+'&CATEGORY='+category+'&SUB_ID_FIELD='+sub_id_field;
	myWin = window.open(loc, "displayWindow2","width=600,height=400,resizeable=auto,status=no,toolbar=no,menubar=no,scrollbars=1");
	myWin.focus();
}
function open_email(main_id,category,sub_id_field){
	loc = 'send_mail.asp?SQL='+document.FORM1.SQL.value+'&MAIN_ID='+main_id+'&CATEGORY='+category+'&SUB_ID_FIELD='+sub_id_field;
	myWin = window.open(loc, "displayWindow2","width=600,height=400,resizeable=auto,status=no,toolbar=no,menubar=no,scrollbars=1");
	myWin.focus();
}

function ajax_parse_url(arr){
	var url = 'ajax_app_form_data_edit_doc.asp?url=' + arr;
	PassAjaxResponseToFunction(url, 'update_app_form_data_edit_doc');
}

function update_app_form_data_edit_doc(response) {
	var data = response.split(';');
	document.getElementById(document.FORM1.TEMP_FIELD.value).value = data[0];
	document.getElementById(document.FORM1.TEMP_FIELD.value + '_label').value = data[1];
}

/* AJAX functionality ------------------------------------------------------- */

/**
SAL - Simple Ajax Lib. 23-Sep-2005
by Nigel Liefrink
Email: leafrink@hotmail.com
*/

var debug = false;
/**
Browser Compatability function.
Returns the correct XMLHttpRequest depending on the current browser.
*/
function GetXmlHttp() {	
	var xmlhttp = false;
	if (window.XMLHttpRequest)
	{
		xmlhttp = new XMLHttpRequest();
  }
	else if (window.ActiveXObject)// code for IE
	{
		try 
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp=false;
			}
		}
	}
	return xmlhttp;
}


/**
<summary>
Gets the response stream from the passed url, and then calls the callbackFuntion passing the response and the div_ids.
</summary>
<param name="url">The url to make the request to get the response data.</param>
<param name="callbackFunction">The function to call after the response has been recieved. the response <b>must</b> always be the first argument to the function.</param>
<param name="params"> (optional) Any other parameters you want to pass to the functions. (Note: only constants/strings/globals can be passed as params, most variables will be out of scope.) </param>
</summary>
<example>
	<code>
PassAjaxResponseToFunction('?getsomehtml=1', 'FunctionToHandleTheResponse', "\'div1\',\'div2\',\'div3\'');

function FunctionToHandleTheResponse(response, d1, d2, d3){
	var data = response.split(';');
	document.getElementById(d1).innerHTML = data[0];
	document.getElementById(d2).innerHTML = data[1];
	document.getElementById(d3).innerHTML = data[2];
}
	</code>
</example>
*/
function PassAjaxResponseToFunction(url, callbackFunction, params)
{		
  var xmlhttp = new GetXmlHttp();
  //now we got the XmlHttpRequest object, send the request.
  if (xmlhttp)
  {
    xmlhttp.onreadystatechange = function () 
		{
			if (xmlhttp && xmlhttp.readyState==4)
			{//we got something back..
				if (xmlhttp.status==200)
				{
					var response = xmlhttp.responseText;
					var functionToCall = callbackFunction+'(response,'+params+')';
					if(debug){
						alert(response);
						alert (functionToCall);
					}
					eval(functionToCall);
				} else if(debug){
					document.write(xmlhttp.responseText);
				}
			}
		}
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}


/**
///<summary>
///Sets the innerHTML property of obj_id with the response from the passed url./
///</summary>
///<param name="url">The url to make the request to get the response data.</param>
///<param name="obj_id">The object or the id of the object to set the innerHTML for.</param>
*/
function SetInnerHTMLFromAjaxResponse(url, obj_id)
{		
  var xmlhttp = new GetXmlHttp();
  //now we got the XmlHttpRequest object, send the request.
  if (xmlhttp)
  {
    xmlhttp.onreadystatechange = function () 
                                {
	                                if (xmlhttp && xmlhttp.readyState==4)
	                                {//we got something back..
		                                if (xmlhttp.status==200)
		                                {
			                                if(debug){
				                                alert(xmlhttp.responseText);
			                                }
			                                if(typeof obj_id == 'object'){
				                                obj_id.innerHTML = xmlhttp.responseText;
			                                } else {
				                                document.getElementById(obj_id).innerHTML = xmlhttp.responseText;
			                                }
		                                } else if(debug){
			                                document.Write(xmlhttp.responseText);
		                                }
	                                }
                                }
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
  }
}


/* End if AJAX functionality ----------------------------------------------- */

//--------------------------------------------- CUSTOM FIELDS JS -----------------------------------------------------------

/* Get object group id */
	function get_ogid(objid){
		//document.FORM1.OBJID.value=objid;
		loc = "filearchive_frame.asp?MODE=1"
		var brow=navigator.appName;
		if (brow=="Microsoft Internet Explorer"){
			var arr = null;
			arr = window.showModalDialog(loc , "", "resizable:yes;help:no;status:no;dialogWidth:300px; dialogHeight:400px")
			if (arr != null) {				
				// set text
				from = arr.indexOf('[title]')
				to = arr.indexOf('[/title]')
				document.getElementById(objid+'_PATH').innerHTML = arr.substring(from+7,to)

				// set menu id
				from = arr.indexOf('[folderid]')
				to = arr.indexOf('[/folderid]')
				document.getElementById(objid).value = arr.substring(from+10,to);
				
			}
		} else if(brow=="Netscape"){
			window.open(loc, "", "fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=300,height=400,left=50,top=100");
		}
	}

/* Remove OGID and text from field */
	// old
	//function delete_ogid(objid){
	//	document.getElementById(objid+'_PATH').innerHTML = '';
	//	document.getElementById(objid).value = '\\';
	//}

	// new
	function delete_ogid(objid){
		document.getElementById(objid+'_PATH').innerHTML = '\\\\';
		document.getElementById(objid).value = '';
	}


/* Get menu id */
	function GetCustomFieldMenuId(objid) {
		loc = "folder_frame.asp?MODE=1&COMMAND=custom_field&OBJID=" + objid
		var brow=navigator.appName;
		if (brow=="Microsoft Internet Explorer"){
			var arr = null;
			arr = window.showModalDialog(loc , "", "resizable:yes;help:no;status:no;dialogWidth:400px; dialogHeight:800px")
			if (arr != null) {
				parseMenuId(arr)
			}
		} else if(brow=="Netscape"){
			window.open(loc, "", "fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=400,height=800,left=50,top=100");
		}
	}

	/* Parse return value from menu ID */
	function parseMenuId(arr)
	{
		// nothing?
		if (arr == null) return;

		// parse CmsJsXml
		var title = GetCmsJsXml('[title]',arr);
		var menuid = GetCmsJsXml('[menuid]',arr);
		var languageid = GetCmsJsXml('[languageid]',arr);
		var command = GetCmsJsXml('[command]',arr);
		var objid = GetCmsJsXml('[objid]',arr);

		// what to do with data?
		switch (command)
		{
			case "custom_field" : 
				// set path as text
				document.getElementById(objid+'_PATH').innerHTML = title;

				// set menu id
				document.getElementById(objid).value = menuid;

				break;
		
		}

	}

	/* remove menu id */
	function ClearCustomFieldMenuId(objid)
	{
		document.getElementById(objid+'_PATH').innerHTML = '';
		document.getElementById(objid).value = '';
	}

/* Get element */
	function GetCustomFieldElement(objid) {
		loc = "object_select_frame.asp?MODE=2&COMMAND=custom_field&OBJID=" + objid
		var brow=navigator.appName;
		if (brow=="Microsoft Internet Explorer"){
			var arr = null;
			arr = window.showModalDialog(loc , "", "resizable:yes;help:no;status:no;dialogWidth:800px; dialogHeight:600px")
			if (arr != null) {
				ParseCustomFieldElement(arr)
			}
		} else if(brow=="Netscape"){
			window.open(loc, "", "fullscreen=no,toolbar=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no,width=800,height=600,left=50,top=100");
		}
	}

	/* Parse return value (URL) from object_select_frame.asp */
	function ParseCustomFieldElement(arr)
	{
		// nothing?
		if (arr == null) return;

		// parse CmsJsXml
		var title = GetCmsJsXml('[title]',arr);
		var url = GetCmsJsXml('[url]',arr);
		var command = GetCmsJsXml('[command]',arr);
		var objid = GetCmsJsXml('[objid]',arr);
		//var basehref = GetCmsJsXml('[basehref]',arr);

		// what to do with data?
		switch (command)
		{
			case "custom_field" : 
				// set path as text
				document.getElementById(objid+'_PATH').innerHTML = title;

				// set menu id
				document.getElementById(objid).value = url;

				break;
		
		}

	}

	/* remove custom field element data */
	function ClearCustomFieldElement(objid)
	{
		document.getElementById(objid+'_PATH').innerHTML = '';
		document.getElementById(objid).value = '';
	}


/* parse cms jsXML format */
/* returns 1 from [foo]1[/foo] */
function GetCmsJsXml(tag,xml)
{
	var startTag = tag;
	var endTag = "[/" + tag.substring(1,tag.length);
	var from = xml.indexOf(startTag);
	var to = xml.indexOf(endTag);
	var len = String(startTag).length;
	return xml.substring(from+len,to)
}

/* These functions are used for moving data between two listboxes */

	// remove selected fields
	function cf_remove_field(sType,srcList){
		var l = srcList.length;
		var i = 0;
		for (i = l-1; i > -1; i--) {
			if(srcList.options[i].selected | sType=='a'){
				//srcList.options.remove(i);
				srcList.options[i]=null;
			}
		}
	}
	
	// Add selected fields to destList
	function cf_add_field(srcList, destList){
		//Check if record already exists in destination
		for (x=0;x<destList.options.length;x++) {
			if(srcList.options[srcList.selectedIndex].value == destList.options[x].value) {
				//alert("§Bruker allerede knyttet til rolle!#User already exists§");
				return;
			}
		}

		if (srcList.options.value != ""){
			var TabText = srcList.options[srcList.selectedIndex].text;
			var TabValue = srcList.options[srcList.selectedIndex].value;
			var l = srcList.length;
			var i = 0;
			for (i = l-1; i > -1; i--) {
				if(srcList.options[i].selected){
				sValue = srcList.options[i].value;
				sValue=cf_name_only(sValue);
				sText = srcList.options[i].text;
				n = destList.options.length; 
				destList.options[n] = new Option(sText,sValue,false,false); 
				}
			}	
		}	
	}

	/* update the destination field with selected values */
	function cf_keyword_update(destListId, fieldId)
	{
		var destList = document.getElementById(destListId);
		var szURL = '';
		l = destList.length;
		for (i = 0; i < l; i++) {
			if (szURL!=''){
				szURL = szURL + ',';
			}	
			szURL = szURL + destList.options[i].value;
		}
		document.getElementById(fieldId).value=szURL;
	}
	
	/* Helper */
	function cf_name_only(szFullName){
		var pos=szFullName.indexOf('#');
		if (pos==-1){
			return szFullName;
		}
		else{
			return szFullName.substring(0,pos);
		}	
	}	

	/* won't let you select keywords that area already added */
	function cf_checkForDisabled(srcList)
	{
		var len = srcList.length;
		var i = 0;
		for(i = 0; i < len; i++)
		{
			if(srcList.options[i].disabled)
			{
				srcList.options[i].selected = false;
			}
		}
	}

	/* disable/enable options */
	function cf_disable_enable_options(srcListId,destListId)
	{
		var destList = document.getElementById(destListId);
		var srcList  = document.getElementById(srcListId);
		for (i = 0; i < srcList.length ; i++ )
		{
			srcList.options[i].disabled = false;
		}

		for (var i = 0; i < destList.length ; i++)
		{
			for (var x = 0; x < srcList.length ; x++ )
			{
				//alert(destList.options[i].text + " ? " + srcList.options[x].text);

				if (destList.options[i].value == srcList.options[x].value)
				{
					srcList.options[x].disabled = true;
					break;
				}
			}
		}
	}



