//Javascript functions used by the FamilyWare controls
var bIE;
var bBrowserChecked;
function IE() {
	if ( !bBrowserChecked ) {
		bIE = navigator.userAgent.toLowerCase().indexOf('msie') >= 0;
		bBrowserChecked = true;
	}
	return bIE;
}

function ValidateDate() {
	var keyCode = window.event.keyCode;
	var typedChar = String.fromCharCode(keyCode);
	var allowedChars = '-1234567890';	
	return (keyCode <= 31) || (allowedChars.indexOf(typedChar) >= 0);
}

function ValidateTime() {
	var keyCode = window.event.keyCode;
	var typedChar = String.fromCharCode(keyCode);
	var allowedChars = ':1234567890';	
	return (keyCode <= 31) || (allowedChars.indexOf(typedChar) >= 0);
}

function ValidateNumber(scale, bNonNegative) {
	var keyCode = window.event.keyCode;
	var typedChar = String.fromCharCode(keyCode);
	var allowedChars = '.1234567890';
	if (scale > 0) allowedChars += ',';
	if (!bNonNegative) allowedChars += '-';
	var bValid = (keyCode <= 31) || (allowedChars.indexOf(typedChar) >= 0);
	return bValid;
}

function $(id) {
	return document.getElementById(id);
}

function FindElement(id) {
	return document.getElementById(id);
}

function GetElementValue(id) {
	var el = FindElement(id);
	var value;	
	if ( el != null ) value = el.value;
	return value;
}

function SetElementValue(id, value) {
	var el = FindElement(id);
	if ( el != null ) el.value = value;
}

function GetObjectElementText(el) {
	if (el != null) {
		if ( IE() ) {
			return el.innerText;
		}
		else {
			if (el.textContent) {
				return el.textContent;
			}
			else {
				return el.innerText;
			}		
		}
	}
}

function GetElementText(id) {
	return GetObjectElementText(FindElement(id));
}

function SetObjectElementText(el, value) {
	if (el != null) {
		if ( IE() ) {
			el.innerText = value;
		}
		else {
			if (el.tagName == 'INPUT') el.value = value;
			else {
				if (el.textContent) {
					el.textContent = value;
				}
				else {
					el.innerText = value;
				}
			}
		}
	}
}

function SetElementText(id, value) {
	SetObjectElementText(FindElement(id), value);
}

function ShowElement(id, show) {
	var el = FindElement(id);
	if ( el != null ) el.style.display = show ? "block" : "none";
}

function ShowElementInline(id, show) {
	var el = FindElement(id);
	if ( el != null ) el.style.display = show ? "inline" : "none";
}


function SubmitForm() {
	document.forms[0].submit();
}

function ValidateForm() {
	SetElementValue('DCValidation', 1);
	SubmitForm();
}

var msgConfirmDelete;
function SubmitForDelete() {
	if (msgConfirmDelete == null || msgConfirmDelete.length == 0)
		msgConfirmDelete = 'Weet u zeker dat u deze gegevens wilt verwijderen?';
	if (confirm(msgConfirmDelete)) {
		SetElementValue('DCAction', 2);
		SubmitForm();
	}
}

function BrowseData(id, returnFunction, returnFunctionParam, width, height) {
	return BrowseDataParam(id, null, null, returnFunction, returnFunctionParam, width, height);
}

function BrowseDataParam(id, clientParam, browseParam, returnFunction, returnFunctionParam, width, height) {
	var options;
	if (width == null) width = 700;
	options = 'dialogWidth:' + width.toString() + 'px';
	if (height == null) height = 400;
	options += ';dialogHeight:' + height.toString() + 'px';
	var url = 'DataBrowser.aspx?ID=' + escape(id)
	if (clientParam != null && clientParam.length > 0 && browseParam != null && browseParam.length > 0) {
		var i = clientParam.indexOf(',');
		var j = browseParam.indexOf(',');
		if (i >= 0 && j >= 0) {
			url += '&ParamName=' + browseParam.substr(0, j) + '&ParamValue=' + 
				escape(GetElementValue(clientParam.substr(0, i)));			
			url += '&ParamName=' + browseParam.substr(j + 1, browseParam.length - j - 1) + '&ParamValue=' + 
				escape(GetElementValue(clientParam.substr(i + 1, clientParam.length - i - 1)));							
		}
		else {
			url += '&ParamName=' + browseParam + '&ParamValue=' + escape(GetElementValue(clientParam));
		}
	}	
	var value = showModalDialog(url, null, options);
	if (typeof(value) != 'undefined') {
		if ( returnFunction != null ) {
			if ( returnFunctionParam != null ) {
				returnFunction(returnFunctionParam, value);
			}
			else {
				returnFunction(value);
			}
		}
	}
	return value;
}

function BrowseDataOnMouseOver() {
	 var e = event.srcElement;
    if (e != null && e.tagName == "TD") {
		var p = e.parentNode;
		if (p != null && p.tagName == "TR") {
			e.parentNode.className = "Selected";
		}
	}
}

function BrowseDataOnMouseOut() {
	var e = event.srcElement;
	if (e != null && e.tagName == "TD") {
		var p = e.parentNode;
		if (p != null && p.tagName == "TR") {
			p.className = ((p.rowIndex % 2) != 0) ? "Uneven" : "Even";
		}
	}
}

function BrowseDataOnClick() {
	var e = event.srcElement;
	if (e.tagName == "TD") {
		var p = e.parentNode;
		if (p != null  && p.tagName == "TR") {
			returnValue = new Array(p.id, p.title);
			close();
		}
	}
}

function BrowseDataOnKeyPress() {
}

function SetBrowseResult(id, value) {
	SetElementValue(id, value[0]);
	SetElementValue(id + '_RefValue', value[1]);
}

function AddContactList(id, value) {
	var separator = id.substr(0, 1);
	id = id.substr(1, id.length - 1);
	var newValue = GetElementValue(id);
	if (newValue != null && newValue.length > 0) newValue += '; ';
	newValue += value[1];
	SetElementValue(id, newValue);
}

function WriteFlash(width, height, file, link) {
	if ( IE() ) {
		document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="');
		document.write(width);
		document.write('" height="');
		document.write(height);
		document.writeln('" viewastext>');
		document.write('<param name="movie" value="');
		document.write(file);
		document.writeln('">');
		if (link != null && link.length > 0) {
			document.write('<param name="base" value="');
			document.write(link);
			document.writeln('">');
		}
		document.writeln('</object>');
	}
	else {
		document.write('<embed src="');
		document.write(file);
		document.write('" base="');
		document.write(link);
		document.write('" width="');
		document.write(width)
		document.write('" height="');
		document.write(height);
		document.writeln('"	type="application/x-shockwave-flash"></embed>');	
	}
}

function WriteFlashWithoutResize(file, link) {
	if ( IE() ) {
		document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ');
		document.writeln(' viewastext>');
		document.write('<param name="movie" value="');
		document.write(file);
		document.writeln('">');
		if (link != null && link.length > 0) {
			document.write('<param name="base" value="');
			document.write(link);
			document.writeln('">');
		}
		document.writeln('</object>');
	}
	else {
		document.write('<embed src="');
		document.write(file);
		document.write('" base="');
		document.write(link);
		document.writeln('"	type="application/x-shockwave-flash"></embed>');	
	}
}
function GetXmlHttpRequestObject() {
	var xmlhttp = null;
	if (IE()) {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	else	{
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function DoAsyncXmlRequest(xmlhttp, target, callbackFunction) {
	xmlhttp.open("GET", target, true);
	if (callbackFunction != null) xmlhttp.onreadystatechange = callbackFunction;
	xmlhttp.send(null);
}

function LogClientError(msg) {
	var xmlhttp = GetXmlHttpRequestObject();
	DoAsyncXmlRequest(xmlhttp, 'ClientLog.aspx?Error=' + encodeURIComponent(msg), null);
}

function GetAbsoluteElementPosition(el) {
	for (var lx=0, ly=0; el != null; el = el.offsetParent) {
		lx += el.offsetLeft;
		ly += el.offsetTop;
		if ( IE() ) {
			lx += el.clientLeft;
			ly += el.clientTop;
		}
	}
	return {x:lx, y:ly};
}

function GetWindowSize()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' )
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return {width:myWidth, height:myHeight};
}


var xmlhttpNameSearch;
var nameSearchTarget;
var divNameSearch, frameNameSearch;
function HandleNameSearchResult() {
	if (nameSearchTarget == null || xmlhttpNameSearch == null || xmlhttpNameSearch.readyState != 4) return;
	var response = xmlhttpNameSearch.responseText;	
	if (response == null || response.length == 0) return;
	
	var style;
	if (divNameSearch == null) {
		divNameSearch = document.createElement("div")
		document.body.appendChild(divNameSearch);
		style = divNameSearch.style;
		style.position = 'absolute';	
		style.height = "100px";	
		style.borderColor = 'black';
		style.borderStyle = 'solid';
		style.borderWidth = '1px';
		style.overflow = 'auto';
		divNameSearch.onkeydown = 'alert("abc")';
	}
	else {
		style = divNameSearch.style;
	}
	divNameSearch.innerHTML = response;	
	var pos = GetAbsoluteElementPosition(nameSearchTarget);
	style.left = pos.x - 3;
	style.top = pos.y + nameSearchTarget.offsetHeight - 2;
	style.width = nameSearchTarget.offsetWidth;
	style.visibility = 'visible';
	style.display = 'inline';
	if (frameNameSearch == null) {
		frameNameSearch = document.createElement("iframe")
		frameNameSearch.src = "blank.htm";			
		document.body.appendChild(frameNameSearch);
	}
	var fstyle = frameNameSearch.style;
	fstyle.visibility = 'visible';
	fstyle.position = 'absolute';
	fstyle.left = style.left;
	fstyle.top = style.top;
	fstyle.height = style.height;
	fstyle.width = style.width;
	fstyle.display = 'inline';	
	style.zIndex = 1;
	fstyle.zindex = -1;			
}

var bDontHideNameSearch = false;
function HideNameSearch() {
	if (bDontHideNameSearch) return;
	if (divNameSearch != null ) {
		divNameSearch.style.visibility = 'hidden';
	}
	if (frameNameSearch != null ) {
		frameNameSearch.style.visibility = 'hidden';
	}	
}

function DelayedHideNameSearch() {
	setTimeout('HideNameSearch()', 200);
}

function DoNameSearch(e, id, target) {
	var key = e.keyCode;
	if (key < 48) return;
	nameSearchTarget = FindElement(id);
	
	var value;
	if (nameSearchTarget != null) value = nameSearchTarget.value;
	if (value == null || value.length == 0) return;
	if (xmlhttpNameSearch == null) {
		xmlhttpNameSearch = GetXmlHttpRequestObject();
	}
	else {
		xmlhttpNameSearch.abort();
	}
	target = target + ((target.indexOf('?') < 0) ? '?' : '&');
	target = target + 'NameSearch=' + encodeURI(value);
	DoAsyncXmlRequest(xmlhttpNameSearch, target, HandleNameSearchResult)
}

function NameSearchDown(event) {
	if (divNameSearch != null && divNameSearch.style.visibility == 'visible') {
		var firstChild = divNameSearch.children[0];
		if (firstChild != null) {
			bDontHideNameSearch = true;
			firstChild.focus();
		}
		event.returnValue = false;
	}
}

function NameSearchKeyDown(e) {
	if (divNameSearch == null || divNameSearch.style.visibility != 'visible') return;
	var key = e.keyCode;
	if (key == 40 || key == 38) {
		e.returnValue = false;	 
		var current = document.activeElement;
		if (current != null) {
			var el;
			if (key == 40) el = current.nextSibling;
			else el = current.previousSibling;
			if (el != null && el.tagName == 'A') {
				el.focus();
			}
		}
	}
	else if (key == 27) {
		bDontHideNameSearch = false;
		HideNameSearch();
		nameSearchTarget.focus();
	}
}

function SelectNameSearchElement(el) {
	if (el == null || nameSearchTarget == null) return;
	SetObjectElementText(nameSearchTarget, GetObjectElementText(el));
	bDontHideNameSearch = false;
	HideNameSearch();
	nameSearchTarget.focus();
	
	var l = nameSearchTarget.value.length;
	if (l > 0) {
		var tr = nameSearchTarget.createTextRange();
		tr.moveStart('character', l);
		tr.select();
		tr.collapse(false);
	}
}

function SSL() {
	if (document.URL.indexOf('https:') == 0) {
		return true;
	} else {
		return false;
	}
}

function HTTPPrefix() {
	return 'http' + ( SSL() ? 's' : '') + '://';
}

function ftbInsertImage(ftb, domain) {
	var value = BrowseData('FTBInsertImage.aspx', null, null, 750, 400);
	if (typeof(value) != 'undefined') {
		var tag = '<img alt="' + value[2] + '" src="';
		if (value[0] == 1) {
			if (domain != null && domain.length > 0)
				tag += HTTPPrefix() + domain + '.familyware.nl/docs/'
			tag += 'Picture.aspx?Type=Documents&ID=' + value[1];
			if (value[3] != null && value[3].length > 0) {
				tag += '&MaxHeight=' + value[3];
			}
			if (value[4] != null && value[4].length > 0) {
				tag += '&MaxWidth=' + value[4];
			}
			tag += '">';
		} else {
			tag += value[1] + '"';
			if (value[3] != null && value[3].length > 0) {
				tag += ' height="' + value[3] + '"'
			}
			if (value[4] != null && value[4].length > 0) {
				tag += ' width="' + value[4] + '"'
			}
			tag += '>';
		}
		ftb.InsertHtml(tag);
	}
}

function ftbInsertLink(ftb, domain) {
	var value = BrowseData('FTBInsertLink.aspx', null, null, 750, 400);
	if (typeof(value) != 'undefined') {
		var tag = '<a href="';
		var url = '';
		if (value[0] == 1) {
			if (domain != null && domain.length > 0)
				url = HTTPPrefix() + domain + '.familyware.nl/docs/'
			url += 'DocumentFile.aspx?SingleDoc=0&ID=' + value[1] + '&Download=' + value[3];
			if (value[3] == '1') {
				tag += url + '">';
			} else {
				tag += url + '" target="_blank">';
			}
		} else {
			url = value[1];
			tag += url + '" target="_blank">';
		}
		if (value[2] != null && value[2].length > 0) {
			tag += value[2];
		} else {
			tag += url;
		}
		tag += '</a>'
		ftb.InsertHtml(tag);
	}
}

function ToggleDisplay(id) {
	var e = FindElement(id);
	if (e != null) {
		if (e.style.display == 'none') {
			e.style.display = 'inline';
		}
		else {
			e.style.display = 'none';
		}	
	}
}

function SetLabelListMode(mode) {
	SetElementValue('LabelListMode', mode);
	var lm;
	for (var i = 0; i <= 2; i++) {
		lm = FindElement('LabelListMode_' + i);
		if (lm != null) lm.className = i == mode ? 'Selected' : '';
	}
	var row;
	var parentid;
	var img;
	var show;
	var ll = FindElement('LabelList');
	for (var i = 1; i < ll.rows.length; i++) {
		row = ll.rows(i);
		show = mode == row.mode;
		if (show) {
			parentid = row.getAttribute('parentid');
			if (parentid != null && parentid.length > 0) {
				img = FindElement('img' + parentid);
				show = img.src.substr(img.src.lastIndexOf('/') + 1) == 'fw1050-10.gif';
			}
		}
		row.style.display = (show ? 'block' : 'none');
	}
}

var fnExpandLabelList = null;

function ShowLabelList(mode) {
	var pos = GetAbsoluteElementPosition($('LabelListTabs'));
	var ll = $('divLabelList');
	if (ll == null) {
		alert('divLabelList niet gevonden');
		return;
	}
	if (ll.style.display == 'none') {
		ll.style.top = pos.y;
		ll.style.width = 185;
		ll.style.left = pos.x - 185;
		ll.style.display = 'block';
		if (fnExpandLabelList != null) {
			fnExpandLabelList(true);
		}
		SetLabelListMode(mode);
		FindElement('LabelListTab' + mode).className = 'LabelListTabActive';
	}
	else {
		var llm = $('LabelListMode');
		if (llm == null || mode == -1) {
			ll.style.display = 'none';
			if (fnExpandLabelList != null) fnExpandLabelList(false);
			for (i = 0; i <= 2; i++) {
				if(FindElement('LabelListTab' + i) != null)
				{
					FindElement('LabelListTab' + i).className = 'LabelListTab';
				}
			}
		}
		else {
			var curMode = GetElementValue('LabelListMode');
			if (curMode == mode)	{
				ll.style.display = 'none';
				if (fnExpandLabelList != null) fnExpandLabelList(false);
				FindElement('LabelListTab' + mode).className = 'LabelListTab';
			}
			else {
				SetLabelListMode(mode);
				var i, tab;
				for (i = 0; i <= 3; i++) {
					tab = FindElement('LabelListTab' + i);
					if (tab != null) {
						if (i == mode)	tab.className = 'LabelListTabActive';
						else tab.className = 'LabelListTab';
					}
				}			
			}
		}
	}
}

function ToggleSection(imgname, secname) {
		var sec = FindElement(secname); 
		if (sec != null) {
			if ( sec.style.display == 'none' ) {
				sec.style.display = 'inline';
				FindElement(imgname).src = 'images/collapse-16.gif';
			}
			else {
				sec.style.display = 'none';
				FindElement(imgname).src = 'images/expand-16.gif';
			}
		}
	}
	
function FWInsertAtCaret(ctl, text) {
	if (ctl.createTextRange && ctl.caretPos){
		var caretPos = ctl.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == '' ? text + ' ' : text;
	}
	else {
		ctl.focus() ;
		if (ctl.value.length > 0) { ctl.value = ctl.value + " " + text ; } else {ctl.value = text ;}
	}
}

function FWStoreCaret (ctl) {
	if (ctl.createTextRange) { ctl.caretPos = document.selection.createRange().duplicate();}
}

function FWPad2 (i) {
	var res = i.toString();
	if (i < 10) res = '0' + res;
	return res;
}

function WriteMediaPlayer(id, url, width, height, uiMode, playCount) {
	document.write(MediaPlayerString(id, url, width, height, uiMode, playCount));
}

function MediaPlayerString(id, url, width, height, uiMode, playCount) {
	var player;
	if (IE()) {
		player = '<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"';
		if (width > 0) {
			player += ' width="' + width + '"';
		}
		if (height > 0) {
			player += ' height="' + height + '"';
		}
		if (id != null && id.length > 0) {
			player += ' id="' + id + '"';
		}
		player += ' viewastext><param name="URL" value="' + url + '">';
		if (uiMode != null && uiMode.length > 0) {
			player += '<param name="uiMode" value="' + uiMode + '">';
		}
		if (playCount != null && playCount > 0) {
			player += '<param name="playCount" value="' + playCount + '">';
		}
		player += '</object>';
	} else {
		player += '<embed type="application/x-mplayer2" ';
		player += 'pluginspace="http://www.microsoft.com/Windows/MediaPlayer/" ';
		player += 'src="' + url + '" width="' + width + '" height="' + height + '"';
		if (id != null && id.length > 0) {
			player += ' id="' + id + '"';
		}
		if (uiMode != null && uiMode.length > 0) {
			player += ' uimode="' + uiMode + '"';
		}
		if (playCount != null && playCount.length > 0) {
			player += ' playcount="' + playCount + '"';
		}
		player += '></embed>';
	}
	return player;
}