var browsername = navigator.appName;
var browserversion = navigator.appVersion;
var browsermajor = Number(browserversion.substring(0,browserversion.indexOf('.')+1));

// returns an array that contains the name/value pairs of the url parameters
// array[even_number] = name
// array[odd_number] = value
function parseURL()
{

	var urlThere = ((document.URL.indexOf('?') != -1) ? true : false);
	var urlarr = new Array();
	if (urlThere) {
		var urlstr = document.URL.substring(document.URL.indexOf('?')+1,document.URL.length);
		var urlidx = 0;
		var urlid1 = '=';
		var urlid2 = '&';
		var aindx = -1;
		var tcntr = 1;  // a test control so while loop does not go to excursion

		// this is the loop 
		while (urlstr.length > 0)
		{
			var urllen = urlstr.length;
			var morethanone = urlstr.indexOf(urlid2);

			var tsub = urlstr.substring(urlidx,urlstr.indexOf(urlid1));
			var tval = urlstr.substring(urlstr.indexOf(urlid1)+1, (morethanone == -1 ? urllen : urlstr.indexOf(urlid2)) );

			if (morethanone > 0)
				urlidx = urlstr.indexOf(urlid2)+1;
			else
				urlidx = urllen;

			//urlarr["eval(tsub)"] = tval;
			//alert(urlarr["eval(tsub)"]);
			urlarr[++aindx] = tsub;
			urlarr[++aindx] = tval

			urlstr = urlstr.substring(urlidx,urllen);
		
			++tcntr;
			urlidx = 0;
		}
	}
	return urlarr;
}

// This function test if a text input has data in it
// returns true if data, false otherwise.
// Accept input types of text, file and textarea
function isInputText(input_text)
{
	var rtnflag = false;
	var input_type = input_text.type.toLowerCase();
	var s = allTrim(input_text.value);
	if (( input_type == 'text' || input_type == 'file' || input_type == 'textarea' || input_type == 'password' || input_type == 'hidden') && s.length > 0)
		rtnflag = true;
	return rtnflag;
}

// This function test if all text input is a number
// returns true if all digits, false otherwise.
function isInputInteger(input_text)
{
	var rtnflag = true;
	if (input_text.type.toLowerCase() == 'text' && input_text.value.length > 0)
	{
		var s = allTrim(input_text.value);
		//input_text.value = s;
		var slen = s.length
		for (var i=0; i<slen; i++)
		{
			if (!(s.charAt(i) >= '0' && s.charAt(i) <= '9'))
			{
				rtnflag = false;
				break;
			}
		}
	}
	return rtnflag;
}


// This function removes leading blanks.
function removeLeftBlanks(s)
{
	var nEnd = s.length;
	for (var i=0; i<s.length; i++) 
		if (s.charAt(i) != " " && s.charAt(i) != "\n" && s.charAt(i) != "\r")
			break
	return s.substring(i,nEnd)
}

// Remove leading and trailing blanks.
function allTrim(s)
{
	var rtnstr = removeLeftBlanks(s);
	var lastblank = rtnstr.lastIndexOf(' ');
	if (lastblank > -1)
		rtnstr = rtnstr.substring(0,lastblank);
	return rtnstr;
}

/* This function checks of the text input conforms to email address
true, if the string contains a valid e-mail address which is a string
plus an '@' character followed by another string containing at least 
one '.' and ending in an alpha (non-punctuation) character
false, otherwise.
*/
function isEmail(email_fld)
{
	var rtnflag = false;
	if (email_fld.value == "")
	{
		rtnflag = true;
	}
	else
	{
		// is there a @ and not first, is the .  there and not the first character, 
		// is the combo @. present, last character alphabet
		// dot dot pair in domain || (email_fld.value.indexOf('..') > -1 && email_fld.value.indexOf('..') > email_fld.value.indexOf('@')
		// the underscore in domain || (email_fld.value.indexOf('_') > -1 && email_fld.value.indexOf('_') > email_fld.value.indexOf('@')
		// domain rules (a-z,A-Z,0-9,.,-) at least 3 alphanumeric, cannot start with - or end in -, no spaces
		var email_fld_len = email_fld.value.length;
		var last_char = email_fld.value.substring(email_fld_len -1,email_fld_len).toUpperCase();
		var last_not_AZ = ((last_char >= 'A' && last_char <= 'Z') ? false : true);
		if ( (email_fld.value.indexOf('@') < 1) || (email_fld.value.indexOf('.') < 1) || (email_fld.value.indexOf('@.') != -1) || last_not_AZ)
		{
			// not a valid email format
			email_fld.focus();
		}
		else
		{
			rtnflag = true;
		}
	}
	return rtnflag;
}

// This function counts the number of items (options) selected
// in a select type input (can handle single or multiple)
// returns the number selected.
function cntSelectOption(selectoption)
{
	var rtnint = 0;
	if (selectoption.type.toLowerCase().indexOf('select') > -1)
	{
		if (selectoption.type.toLowerCase().indexOf('select-m') > -1)
		{
			// multi select
			var len = selectoption.length;
			var cnt = 0;
			for (var i=0; i<len; i++)
			{
				if (selectoption.options[i].selected)
					++cnt;
			}
			rtnint = cnt;
		}
		else
		{
			// single select
			rtnint = (selectoption.selectedIndex > -1 ? 1 : 0);
		}
	}
	return rtnint;
}

// This function deselects all options in a single select box.
// returns nothing
function unSelectAll(selectoption)
{
	if (selectoption.type.toLowerCase().indexOf('select') > -1)
	{
			var len = selectoption.length;
			var cnt = 0;
			for (var i=0; i<len; i++)
			{
				selectoption.options[i].selected = false;
			}	
	}
	return null;
}

// This function turns on the select options
function selectAll(selectoption)
{
	if (selectoption.type.toLowerCase().indexOf('select') > -1)
	{
			var len = selectoption.length;
			var cnt = 0;
			for (var i=0; i<len; i++)
			{
				selectoption.options[i].selected = true;
			}	
	}
	return null;
}

// This function puts a text input into a select box and resorts the select option object.
// required input:
// input_data - the text of the new option (or text object)
// selectoption - the select option object
// ------------------------------------------------------
// optional input:
// init_value - the value for the new option value attribute
// select_value - the value (true/false) of the new option selected attribute
function addSelectOption(input_data,selectoption,init_value,select_value)
{
	var input_data_type = typeof(input_data);
	var input_data_len = (input_data_type == "object" ? input_data.value.length : input_data.length);
	if (selectoption.type.toLowerCase().indexOf('select') > -1 &&  input_data_len > 0)
	{
		var optval = (init_value == null ? 0 : init_value);
		var optsel = (select_value == null ? true : select_value);
		var aTmpval = new Array();
		var aTmptxt = new Array();
		var aTmpsel = new Array();
		var aTmpdsl = new Array();
		var aTmpsrt = new Array();
		var iTmpidx = -1;
		for (var i=0; i<selectoption.length; i++)
		{
			aTmpval[++iTmpidx] = selectoption.options[i].value;
			aTmptxt[iTmpidx] = selectoption.options[i].text;
			aTmpsel[iTmpidx] = selectoption.options[i].selected;
			aTmpdsl[iTmpidx] = selectoption.options[i].defaultSelected;
			aTmpsrt[iTmpidx] = selectoption.options[i].text + '\t' + i;
		}
		aTmpval[++iTmpidx] = optval;
		aTmptxt[iTmpidx] = (input_data_type == "object" ? input_data.value : input_data);
		aTmpsel[iTmpidx] = optsel;
		aTmpsrt[iTmpidx] = (input_data_type == "object" ? input_data.value : input_data) + '\t' + selectoption.length;
		++selectoption.length;
		aTmpsrt.sort();
		for (var i=0; i<selectoption.length; i++)
		{
			var idxptr = aTmpsrt[i].lastIndexOf('\t');
			var idx = Number(aTmpsrt[i].substring(idxptr+1));
			selectoption.options[i].value = aTmpval[idx];
			selectoption.options[i].text = aTmptxt[idx];
			selectoption.options[i].selected = aTmpsel[idx];
			selectoption.options[i].defaultSelected = aTmpdsl[idx];
		}
		// this will resize the select box
		//history.go(0);
	}
	return null;
}

// This function will remove all selected items from a select box or
// move selected items of the source select option object to the target
// select option object.
// required input:
// the source select option object
// -----------------------------
// optional input:
// the target select option object (used for a move)
function moveSelectOptions(selectoption_source,selectoption_target)
{
	if (selectoption_source.type.toLowerCase().indexOf('select') > -1)
	{
		var tArrval = new Array();
		var tArrtxt = new Array();
		var tArrsel = new Array();
		var tindx = -1;
		var trgtthere = (selectoption_target == null ? false : true);
		for (var i=0; i<selectoption_source.length; i++)
		{
			var tval = selectoption_source.options[i].value;
			var ttxt = selectoption_source.options[i].text;
			var tsel = selectoption_source.options[i].selected;
			// keep this item in the source select option object
			if (!selectoption_source.options[i].selected)
			{
				tArrval[++tindx] = tval;
				tArrtxt[tindx] = ttxt;
				tArrsel[tindx] = tsel;
			}
			else
				// move the selected into the target select option object
				if (trgtthere)
				{
					addSelectOption(ttxt,selectoption_target,tval,tsel);
				}
		}
		selectoption_source.length = 0;
		for (var i=0; i<tArrval.length; i++)
		{
			var optitem = new Option(tArrtxt[i],tArrval[i]);
			selectoption_source.options[i] = optitem;
		}
		selectoption_source.length = tArrval.length;
	}
	return null;
}


// test if java enabled in browser
function isJavaOn()
{
	var flag = false;
	if (navigator.javaEnabled())
		flag = true;
	return flag;
}

// This is the generic popup window 
function newwin(file_to_open,winname)
{
	var appver = navigator.appVersion;
	var majorver = appver.substring(0,appver.indexOf('.'));
	var h = 600;
	var w = 730;
	var lwinname = (winname == null ? 'newwindow' : winname);
	var toolbarstring = '"toolbar=no,height='+h+',width='+w+',scrollbars=yes,status=yes,menu=no,location=no,resizable"';
	//var owindow = window.open("../administration/index.cfm","adminwin",eval(toolbarstring));
	var owindow = window.open(file_to_open,lwinname,eval(toolbarstring));
	//owindow.onblur = owindow.close;
}

function newwinDesc(file_to_open,winname)
{
	var appver = navigator.appVersion;
	var majorver = appver.substring(0,appver.indexOf('.'));
	var h = 400;
	var w = 400;
	var lwinname = (winname == null ? 'newwindow' : winname);
	var toolbarstring = '"toolbar=no,height='+h+',width='+w+',scrollbars=yes,status=yes,menu=no,location=no,resizable=0"';
	//var owindow = window.open("../administration/index.cfm","adminwin",eval(toolbarstring));
	var owindow = window.open(file_to_open,lwinname,eval(toolbarstring));
	//owindow.onblur = owindow.close;
}
// This is the generic popup window 
function password(file_to_open,winname)
{
	var appver = navigator.appVersion;
	var majorver = appver.substring(0,appver.indexOf('.'));
	var h = 100;
	var w = 280;
	var lwinname = (winname == null ? 'newwindow' : winname);
	var toolbarstring = '"toolbar=no,height='+h+',width='+w+',scrollbars=yes,status=yes,menu=no,location=no,resizable"';
	//var owindow = window.open("../administration/index.cfm","adminwin",eval(toolbarstring));
	var owindow = window.open(file_to_open,lwinname,eval(toolbarstring));
	//owindow.onblur = owindow.close;
}

// This is the popup window used specifically for MyPage Edit
function editmpwin(file_to_open,winname)
{
	var appver = navigator.appVersion;
	var majorver = appver.substring(0,appver.indexOf('.'));
	var h = 300;
	var w = 730;
	var lwinname = (winname == null ? 'newwindow' : winname);
	var toolbarstring = '"toolbar=no,height='+h+',width='+w+',scrollbars=yes,status=yes,location=no,resizable,menu=yes"';
	//var owindow = window.open("../administration/index.cfm","adminwin",eval(toolbarstring));
	var owindow = window.open(file_to_open,lwinname,eval(toolbarstring));
	//owindow.onblur = owindow.close;
}

// This is the popup window for contact directory (others?)
function newwin3(file_to_open,winname)
{
	var appver = navigator.appVersion;
	var majorver = appver.substring(0,appver.indexOf('.'));
	var h = 600;
	var w = 650;
	if (majorver >= 4)
	{
		h =  screen.height - Math.round(screen.height / 3);
		w =  screen.width - Math.round(screen.width / 3);
	}
	w = 700;
	h = 450;
	var lwinname = (winname == null ? 'newwindow' : winname);
	var toolbarstring = '"toolbar=no,height='+h+',width='+w+',scrollbars=yes,status=yes,location=no,resizable,menu=yes,location=no"';
	//var owindow = window.open("../administration/index.cfm","adminwin",eval(toolbarstring));
	var owindow = window.open(file_to_open,lwinname,eval(toolbarstring));
	//owindow.onblur = owindow.close;
}

// This is the popup window used specifically for WorkSpace
function newwin_wkspc(file_to_open,winname)
{
	var appver = navigator.appVersion;
	var majorver = appver.substring(0,appver.indexOf('.'));
	var h = 750;
	var w = 420;
	if (majorver >= 4)
	{
		h =  Math.round(screen.height / 1.33);
		w =  Math.round(screen.width / 1.33);
	}
	var lwinname = (winname == null ? 'newwindow' : winname);
	var toolbarstring = '"toolbar=no,height='+h+',width='+w+',scrollbars=yes,status=yes,location=no,resizable,location=no"';
	//var owindow = window.open("../administration/index.cfm","adminwin",eval(toolbarstring));
	var owindow = window.open(file_to_open,lwinname,eval(toolbarstring));
	//owindow.onblur = owindow.close;
}

function printwin(file_to_open,winname,ph,pw)
{
	var appver = navigator.appVersion;
	var majorver = appver.substring(0,appver.indexOf('.'));
	var sh = 25;
	var sw = 25;
	var dh = 500;
	var dw = 750;
	var nh = (ph != null ? ph : dh);
	var nw = (pw != null ? pw : dw);
	if (majorver >= 4)
	{
		sh =  Math.round((screen.height - nh) / 4);
		sw =  Math.round((screen.width - nw) / 2);
	}
	var lwinname = (winname == null ? 'Print_Window' : winname);
	var toolbarstring = '"toolbar=yes,height='+nh+',width='+nw+',top='+sh+',left='+sw+',scrollbars=yes,status=yes,location=no,resizable"';
	var owindow = window.open(file_to_open,lwinname,eval(toolbarstring));

}


function newwin_vivakos(file_to_open,winname)
{
	var appver = navigator.appVersion;
	var majorver = appver.substring(0,appver.indexOf('.'));
	var h = 670;
	var w = 400;
	if (majorver >= 4)
	{
		h =  screen.height - Math.round(screen.height / 3);
		w =  screen.width - Math.round(screen.width / 3);
	}
	w = 670;
	h = 400;
	var lwinname = (winname == null ? 'newwindow' : winname);
	var toolbarstring = '"toolbar=no,height='+h+',width='+w+',scrollbars=yes,status=no,location=no,resizable=no,location=no"';
	//var owindow = window.open("../administration/index.cfm,"adminwin",eval(toolbarstring));
	var owindow = window.open(file_to_open,lwinname,eval(toolbarstring));
	//owindow.onblur = owindow.close;
}

function newwin_full(file_to_open,winname) 
{
	var owindow = window.open(file_to_open);
}

function openLink(url,window_title){
	window_title = (window_title == null ? 'External_Link' : window_title);
	window.open(url,window_title);
}

function ViewContentItem(content_item_id,folder_ka_id){
	var token = '<cfoutput>#URLTOKEN#</cfoutput>';
	var appver = navigator.appVersion;
	var majorver = appver.substring(0,appver.indexOf('.'));		
	var h = 450;
	var w = 620;
	if (majorver >= 4)
	{
		h =  Math.round(screen.height / 1.2);
		w =  Math.round(screen.width / 1.2);
	}	
	where = "/content/view_content.cfm?content_item_id=" + content_item_id + "&folder_id=" + folder_ka_id + "&in_workspace=True";
	window.open(where,"View_Content_Item","scrollbars,resizable,width="+w+",height="+h+",left=20,top=20")
}

function EditContentItem(content_item_id,folder_ka_id){
	where = "/kad/confirm_and_close.cfm?content_item_id=" + content_item_id + "&folder_ka_id=" + folder_ka_id + "&in_workspace=True";
	window.open(where,"Edit_Content_Item","resizable,scrollbars,width=700,height=592,left=200")
}

function CreateContentItem(folder_ka_id){
	where = "/kad/ka-d1.cfm?content_item_id=0&folder_ka_id=" + folder_ka_id;

	window.open(where,"New_Content_item","resizable,scrollbars,width=700,height=592,left=200")
}

/*
//HTML AREA INCLUSION
// load htmlarea
_editor_url = "";                     // URL to htmlarea files
var win_ie_ver = parseFloat(navigator.appVersion.split("MSIE")[1]);
if (navigator.userAgent.indexOf('Mac')        >= 0) { win_ie_ver = 0; }
if (navigator.userAgent.indexOf('Windows CE') >= 0) { win_ie_ver = 0; }
if (navigator.userAgent.indexOf('Opera')      >= 0) { win_ie_ver = 0; }
if (win_ie_ver >= 5.5) {
 document.write('<scr' + 'ipt src="' +_editor_url+ 'editor.js"');
 document.write(' language="Javascript1.2"></scr' + 'ipt>');  
} else { document.write('<scr'+'ipt>function editor_generate() { return false; }</scr'+'ipt>'); }
*/

/*
 * resizes the icon image size to the specified 
 * maxWidth and maxHeight (relative ratio)
 * Must use ImagePreloader object so as to ensure image 
 * is loaded before width and height are used.
 *
 * @author: Yujin Kim
 */
function resizeImage(imageURL, maxWidth, maxHeight) {

	var image = new Image();
	var id = imageURL.substring(imageURL.lastIndexOf("/")+1, imageURL.indexOf("."));
	document.write("<div id=\""+id+"\"></div>");
	image.src = imageURL;
	image.onload = function() { resizeSub(image, id, maxWidth, maxHeight); };
}

function resizeSub(image, id, maxWidth, maxHeight){
	var width = image.width;
	var height = image.height;
	if ( width > maxWidth ) {
		height = ( height * maxWidth ) / width;
   		width  = maxWidth;
	}
   	if ( height > maxHeight ) {
		width = ( width * maxHeight ) / height;
	    height = maxHeight;
	}
	
	height = parseInt(height);
	width = parseInt(width);
  
	image.height = height;
	image.width = width;

	var imgObj = document.getElementById(id);
	imgObj.innerHTML = "<img src=\""+image.src+"\" width=\""+width+"\" height=\""+height+"\" border=\"0\">";
}
