var helpContextID = -1;

function uwgBrowse_DblClick(gridName, cellId) {
    var grid = igtbl_getGridById(gridName);
    var functionString = grid.Events.AfterFilterClosed[0];

    eval(functionString);
}

function selectItemFromGrid(gridId, detailPageName, errorLabelId, showAsPopup) {
    var frm = document.forms[0];
    var act = frm.action;

    var i = act.indexOf('Popup');

    if (showAsPopup == "True") {
        var frm = document.forms[0];
        var url = frm.action;
        url = addPopupParamToUrl(url);

        frm.action = url;

        doEditBrowseForm(gridId, detailPageName);
    }

    if (i == -1) {
        selectItemFromGrid_Detail(gridId, detailPageName, errorLabelId);
    }
    else {
        selectItemFromGrid_Popup(gridId, errorLabelId);

    }

    return "function";

}

function selectItemFromGrid_Detail(gridId, detailPageName, errorLabelId) {
    var identity = getIdFromGrid(gridId, 1);
    //var row = igtbl_getActiveRow('uwgBrowse');

    if (identity != -1) {
        redirectToDetailPage(detailPageName, identity);
    }
    else {
        //var lLabel = document.getElementById(errorLabelId);
        //if (lLabel != null)
        //	lLabel.innerHTML = 'Geen rij geselecteerd';	
        window.alert("Geen geldige rij geselecteerd");
    }
}

function selectItemFromGrid_Popup(gridId, errorLabelId) {
    var identity = getIdFromGrid(gridId, 1);
    var row = igtbl_getActiveRow(gridId);
    var obj = window.dialogArguments;

    if (identity != -1) {
        obj.Identity = identity;
        obj.Values = getValuesArray(row);
        obj.KeyValues = getKeyValuesArray(row);

        window.close();
    }
    else {
        //var lLabel = document.getElementById(errorLabelId);
        //if (lLabel != null)
        //lLabel.innerHTML = 'Geen rij geselecteerd';	
        window.alert("Geen geldige rij geselecteerd");
    }
}

function getValuesArray(row) {
    var num;
    var values = new Array;
    var cells;

    //functionality dramatically changed in Infragistics 6.1 !!!
    cells = row.getCellElements();
    for (i = 0; i < cells.length; i++) {
        num = cells[i].innerText;
        values[i] = num;
    }

    //for (var i in row.cells)
    //{ 
    //	num = row.cells[i].Element.innerText;
    //	values[i]=num;
    //}

    return values;
}

function getKeyValuesArray(row) {
    var num;
    var values = new Array;

    //functionality dramatically changed in Infragistics 6.1 !!!
    cells = row.getCellElements();
    for (i = 0; i < cells.length; i++) {
        num = cells[i].innerText;
        values[i] = num;

        num = cells[i].innerText;
        num2 = row.getCell(i).Column.Key;
        values[num2] = num;
    }

    //for (var i in row.cells)
    //{ 
    //	num = row.getCell(i).Element.innerText;
    //	num2 = row.getCell(i).Column.Key;
    //	values[num2]=num;
    //}

    return values;
}



function getIdFromGrid(gridId, columnNr) {
    var row = igtbl_getActiveRow(gridId);

    if (row != null) {
        var cell = row.getCell(columnNr);
        return cell.getValue();

        // next code is due to a bug in Infragistics 2006_Vol1
        //var cell2 = row.Element.cells[columnNr];
        //return cell2.innerText;

    }
    else {
        return -1;
    }
}

function getIdFromGrid2(gridId, columnNr, rowNr) {
    var grd = igtbl_getGridById(gridId);
    var row = grd.rows[rowNr];

    if (row != null) {
        var cell = row.getCell(columnNr);
        return cell.getValue();

        // next code is due to a bug in Infragistics 2006_Vol1
        //var cell2 = row.Element.cells[columnNr];
        //return cell2.innerText;

    }
    else {
        return -1;
    }
}

function deleteItemFromGrid(gridId, errorLabelId) {

    var identity = getIdFromGrid(gridId, 1);
    var row = igtbl_getActiveRow('uwgBrowse');

    if (identity != -1) {
        if (window.confirm('Weet u zeker dat u dit item wilt verwijderen?')) {
            var browseForm = document.forms[0];
            var hiddenField2 = findHiddenControl('hiddenToolbarAction');

            hiddenField2.value = 'Delete=1&Identity=' + identity;
            browseForm.submit();
        }
    }
    else {
        var lLabel = document.getElementById(errorLabelId);
        if (lLabel != null)
            lLabel.innerHTML = 'Geen rij geselecteerd';
    }
}


function deleteSelectedRowsFromGrid(gridId) {
    var row = igtbl_getActiveRow(gridId);

    if (row != null) {
        if (window.confirm('Weet u zeker dat u de geselecteerde rij(en) wilt verwijderen?')) {
            igtbl_deleteSelRows(gridId);
            setEditState('Edit');
        }
    }
    else {
        window.alert("geen geldige rij geselecteerd");
    }
}

function getBrowseGridName() {
    var ctrl = findControlByShortID("uwgBrowse", "INPUT", "hidden");
    var name = ctrl.id;

    return name;
}

function getGridFullName(gridId) {
    var ctrl = findControlByShortID(gridId, "INPUT", "hidden");
    var name = ctrl.id;

    if (name.indexOf("_tb") != -1)
        name = name.replace("_tb", "");

    return name;
}

function editDetailFromGrid(gridId, detailPageName, width, height) {
    var lWidth = (width != null ? width : 850);
    var lHeight = (height != null ? height : 600);

    var gridName = getGridFullName(gridId);

    var identity = getIdFromGrid(gridName, 1);

    if (identity != -1) {
        openDetailsFor_Popup(identity, detailPageName, lWidth, lHeight);
    }
    else {
        window.alert("Geen geldige rij geselecteerd");
    }
}

function editDetailFromGridNotModal(gridId, detailPageName, width, height) {
    var gridName = getGridFullName(gridId);


    var identity = getIdFromGrid(gridName, 1);

    if (identity != -1) {
        var w = 800;
        var h = 600;

        if (width != null) w = width;
        if (height != null) h = height;

        var url = detailPageName + "?Identity=" + identity.toString();
        var features = "toolbar=no,status=no,scrollbars=no,width=" + w + ",height=" + h + ",resizable=yes";
        window.open(url, "", features);
    }
    else {
        window.alert("Geen geldige rij geselecteerd");
    }
}

function openDetailInWindow(url, width, height) {
    var w = 800;
    var h = 600;

    if (width != null) w = width;
    if (height != null) h = height;

    var features = "toolbar=no,status=no,scrollbars=no,width=" + w + ",height=" + h + ",resizable=yes";
    window.open(url, "", features);

}

function getGridSelectedRow(gridId, isShortId) {
    var gridName = gridId;

    if (isShortId) gridName = getGridFullName(gridId);

    var row = igtbl_getActiveRow(gridName);

    return row;
}

function addDetailRowToGrid(gridId) {
    var gridName = getGridFullName(gridId);

    var grid = igtbl_getGridById(gridName);
    var newRow = igtbl_addNew(grid.Id, 0);
    //newRow.cells[3].setValue("<< Vul hier de omschrijving in >>");

    return newRow;
}

function deleteDetailFromGrid(gridId) {
    var gridName = getGridFullName(gridId);
    var row = igtbl_getActiveRow(gridName);

    if (row != null) {
        if (window.confirm('Weet u zeker dat u de geselecteerde rij(en) wilt verwijderen?')) {
            igtbl_deleteSelRows(gridName);
            setEditState('Edit');
        }
    }
    else {
        window.alert("geen geldige rij geselecteerd");
    }
}

function addDetailToGrid(gridId, detailPageName, columnCount) {

    if (showSearchWindow(detailPageName, '') != -1) {
        var gridName = getGridFullName(gridId);
        var grid = igtbl_getGridById(gridName);
        var values = popupResult.Values;
        var ident = values[1];

        // check if Relatie already present
        if (findIdInGrid(grid, ident)) {
            window.alert("De geselecteerde relatie is reeds aanwezig!");
            return;
        }

        var newRow = igtbl_addNew(grid.Id, 0)

        var count = (columnCount != null ? columnCount : values.length);

        for (var i = 0; i < count; i++)//(var i in newRow.cells)
        {
            newRow.getCell(i).setValue(values[i]);
        }

        setEditState('Edit');
    }

}

function findIdInGrid(grid, ident) {
    for (var i = 0; i < grid.Rows.length; i++) {
        row = grid.Rows.getRow(i);
        cell = row.getCell(1);
        val = cell.getValue();

        if (val.toString() == ident.toString()) {
            return true;
        }
    }
    return false;
}

	
	function doCreateIdentityURL(strURL, identity)
	{
		var S;		
		var chr = "?";
		var action = 'Identity=' + identity;
		var pos;
		
		// remove Identity paramater if already present
		pos = strURL.indexOf(action);
		
		if (pos != -1)
		{
			return strURL;
			//var act = strURL.substring(pos-1, pos + action.length)
			//strURL = strURL.replace(act, '');
		}
		
		if (strURL.indexOf(chr) != -1)
		{
			S = strURL + '&Identity=' + identity;
		}
		else
		{
			S = strURL + '?Identity=' + identity;
		}
		
		return S;
	}
	
	function redirectToDetailPage(strURL, identity)
	{	
		var S;		
		
		S = doCreateIdentityURL(strURL, identity);
		
		var frame = top.frames["main"];
		
		if (frame == null)
		{
			frame = top.frames["frameMain"];
		}
		
		if (frame != null)
		{
			frame.location.href = S;	
		}
		else
		{
			window.navigate(S);
		}
	}
	
	function openDetailsForm(identity, formUrl)
	{
		//url = formName + '?Identity=' + identity;
		redirectToDetailPage(formUrl, identity); 
		//top.frames['main'].location.href = url;	
	}
	
	function openDetailsFor_Popup(identity, formUrl, width, height)
	{
		var openerUrl = document.forms[0].action;
		
		formUrl = increasePopupParamInUrl(formUrl, openerUrl);
		
		var url = doCreateIdentityURL(formUrl, identity);
		
		return showEditWindowModal(url, width, height);
		
	}
	
	var popupResult2  = new Object();
	function showEditWindowModal(strURL, width, height)
	{
		var lWidth = (width != null ? width : 825);
		var lHeight = (height != null ? height : 620);
		
		popupResult2.Identity = -1;
		popupResult2.success = false;
						
		var lFeatures = 'dialogWidth:' + lWidth + 'px;dialogHeight:' + lHeight + 'px;statusbar:no;center:yes;scroll:no;resizable:yes';
			
		var lValue = showEffModalForm(strURL, popupResult2, lFeatures );
		
		return popupResult2.success;
	}
	
	function showEffModalForm(strUrl, dialogArguments, features)
	{
		var url = strUrl;
		var formName;
		var redirUrl;
		var paramStr;
		
		//url = addParamToUrl(url,"Form=" + formName);
		url = addPopupParamToUrl(url);
		
		var pos = url.indexOf('?');
		if (pos != -1)
		{
			formName = url.substring(0, pos);
			paramStr = url.substring(pos + 1, url.length);
			redirUrl = "frmModalWindow.aspx?Form=" + formName + "&" + paramStr;
		}
		else
		{
			formName = url;
			redirUrl = "frmModalWindow.aspx?Form=" + formName;
		}
		
		window.showModalDialog(redirUrl, dialogArguments, features );
	}
	
	function closeEffModalForm(success)
	{
		var wnd = window.top;
		wnd.dialogArguments.success = success;
		wnd.close();
	}
	
	function addParamToUrl(strURL, param)
	{
		var url = strURL;
		
		if (url.indexOf(param) != -1)
		{
			return url;
		}
		
		if (url.indexOf('?') != -1)
			url += '&';
		else
			url += '?';
			
		return url + param;
	}
	
	function increasePopupParamInUrl(strUrl, openerUrl)
	{
		var url = strUrl;
		var waarde = parseInt(findParamInUrl(openerUrl,"Popup"));
		
		if (waarde != -1)
		{
			url = removeParamFromUrl(strUrl,"Popup");
			waarde += 1;
			url = addParamToUrl(url,"Popup=" + waarde.toString());
		}
		else
		{
			url = addParamToUrl(url,"Popup=1");
		}
		
		return url;
	}
	
	function addPopupParamToUrl(strUrl, openerUrl)
	{
		var url = strUrl;
		
		if (openerUrl != null)
			url = openerUrl;
		var waarde = parseInt(findParamInUrl(url,"Popup"));
		
		if (waarde == -1)
		{
			url = addParamToUrl(strUrl,"Popup=1");
		}
		else
		{
			if (openerUrl != null)
				url = addParamToUrl(strUrl,"Popup=" + waarde.toString());
		}
		
		return url;
	}
		
		
	function showControl(control, visible)
	{
		var ctrl = document.getElementById(control);
		if (visible)
			ctrl.style.display = 'block';
		else
			ctrl.style.display = 'none';
	}
	
	function showControlFromCheckBox(checkbox, control)
	{
		var ctrl = document.getElementById(control);
		
		if (ctrl != null)
		{
			var chk = document.getElementById(checkbox);
			if (chk.checked)
			{
				ctrl.style.display = 'block';
			}
			else
			{
				ctrl.style.display = 'none';
			}
		}
	}	
	
	function swapVisible(control)
	{
		var ctrl = document.getElementById(control);
		if (ctrl.style.display == 'none')
			ctrl.style.display = 'block';
		else
			ctrl.style.display = 'none';
	}
	
	function setControlVisible(control, show)
	{
		if (show == true)
			control.style.display = '';
		else
			control.style.display = 'none';
	
	}
	
	function swapVisiblePanelFromBalk(sender, e)
	{
		var isVisible;
		var re;
		var divControlName = sender.id;
		//var arr = divControlName.split("_");
		
		//divControlName = arr[arr.length-1];
		re = 'tbl';
		divControlName = divControlName.replace(re,'div');
		re = 'Balk';
		divControlName = divControlName.replace(re,'');
		
		var panelCtrl = document.getElementById(divControlName);
		
		if (panelCtrl == null)
			return 'panel not found';
			
		isVisible = !(panelCtrl.style.display == 'none');
		
		setControlVisible(panelCtrl, !isVisible);
		
		// try to find the image	
		var imgCtrl;
		var cell = sender.cells[2];
		imgCtrl = cell.firstChild;
		
		if (imgCtrl != null)
		{
			if (isVisible)
				imgCtrl.src = '/tph_common/Images/downarrows_white.gif';
			else
				imgCtrl.src = '/tph_common/Images/uparrows_white.gif';
				
		}
		
		return isVisible;	
	}
	
	
	var popupResult  = new Object();
	
	function showSearchWindow(frmName, ctrlName, searchColumnID, width, height)
	{
		var lWidth = (width != null ? width : 850);
		var lHeight = (height != null ? height : 600);
		
		
		popupResult.Identity = -1;
		
		var openerUrl = document.forms[0].action;
		
		lurl = addPopupParamToUrl(frmName, openerUrl);
		
		var lFeatures = 'dialogWidth:' + lWidth + 'px;dialogHeight:' + lHeight + 'px;statusbar:no;center:yes;scroll:yes;resizable:yes';
		var lValue = window.showModalDialog(lurl, popupResult, lFeatures );
		
		if (popupResult.Identity != -1)
		{
			var lControl = document.getElementById(ctrlName);
			if (lControl != null)
			{
				if (searchColumnID == null)
					lControl.innerText = popupResult.Identity;
				else
					lControl.innerText = popupResult.Values[searchColumnID];
			}	
		}		
		else
		{
			//alert('null');
		}

		return popupResult.Identity;
	}
	
	
	
	
	function showModalWindow(frmName, width, height)
	{
		var lWidth = 750;
		var lHeight = 650;
		
		lurl = addPopupParamToUrl(frmName);
		
		if (width != null)
			lWidth = width;
		if (height != null)
			lHeight = height;
			
		var lFeatures;
		lFeatures = 'dialogWidth:'+ lWidth + 'px;dialogHeight:' + lHeight + 'px;statusbar:no;center:yes;scroll:no;resizable:yes';
			
		var lValue = window.showModalDialog(lurl, popupResult, lFeatures );
		
		return popupResult;
	}
	
	
	
	
	function doCheckEditState(oEvent)
	{
			//Add code to handle your event here.
		var val = checkEditState();
		
		if (val == 'Edit')
		{
			window.alert('Niet alle wijzigingen zijn opgeslagen. Klik op OK om terug te keren naar de pagina zodat u alsnog de wijzigingen kunt opslaan.');
			oEvent.cancel = true;	
			oEvent.cancelPostBack = true;
			//oEvent.needPostBack = false;
			return false;
		}
		return true;
	}
		
	function checkEditState()
	{
		var stateControl = findEditStateControl();
		
		if (stateControl != null)
		{
			return stateControl.value;
		}
			
		return -1;
	}
	
	function setEditState(state)
	{
		var stateControl = findEditStateControl();
		
		if (stateControl != null)
			stateControl.value = state;
			
	}
	
	function findEditStateControl()
	{
		var frame = top.frames['LeftFrame'];
		
		if (frame == null)
		{
			frame = top.frames["frameLeft"];
		}
		
		if (frame != null)
		{
			var frm = frame.document.forms[0];
			if (frm != null)
			{
				var hiddenField = frame.document.getElementById('hiddenEditState');
				return hiddenField;
			}
		}	
		
		return null;
	}
	
	function doValidateEditForm()
	{
		return true;
	}
	
	function beforeSaveEditForm()
	{
	}
	
	function doSaveEditForm()
	{
		if (!doValidateEditForm())
		{
			return false;
		}
		
		var frm = document.forms[0];
		var act = frm.action;
		
		var i = act.indexOf('Popup');
		
		if (i == -1)
		{
			doSaveEditFor_Detail()
		}
		else
		{
			doSaveEditFor_Popup()
		}
		
		return true;
	}
	
	function doSaveEditFor_Detail()
	{
		var editForm = document.forms[0];
		var hiddenField2 = findHiddenControl('hiddenToolbarAction');
		
		hiddenField2.value = 'Save=1';
		editForm.submit();
		
		return true;
	}
	
	function doSaveEditFor_Popup()
	{
		if (!doSaveEditFor_Detail()) return;
		
		doWait(1500);
		
		closeEffModalForm(true);
	}
	
	function doInsertEditForm(oEvent)
	{
		doCheckEditState(oEvent);
		
		if (oEvent.cancel == true)
		{
			return;
		}
		
		var editForm = document.forms[0];
		var hiddenField2 = findHiddenControl('hiddenToolbarAction');
		
		hiddenField2.value = 'Insert=1';
		editForm.submit();
	}
	
	function doInsertBrowseForm(detailsFormName)
	{
		var frm = document.forms[0];
		var act = frm.action;
		
		var i = act.indexOf('Popup');
		
		if (i == -1)
		{
			openDetailsForm('-1', detailsFormName);
		}
		else
		{
			openDetailsFor_Popup('-1', detailsFormName);	
			
			// Submit the browseform if needed
			if (popupResult2.success)
			{
				
				frm.action = addParamToUrl(frm.action, 'RefreshAfterInsert=1');
				frm.submit();
			}
		}
	}
	
	
	function doEditBrowseForm(gridId, detailsFormName)
	{
		var identity = getIdFromGrid(gridId,1);
		//window.alert(detailsFormName + "ident=" + identity.toString());
		
		if (identity == -1)
		{
			window.alert('Geen geldige rij geselecteerd');	
			return;
		}
		
		var frm = document.forms[0];
		var act = frm.action;
		
		var i = act.indexOf('Popup');
		
		if (i == -1)
		{
			openDetailsForm(identity, detailsFormName);
		}
		else
		{
			openDetailsFor_Popup(identity, detailsFormName);	
			
			// Submit the browseform if needed
			if (popupResult2.success)
			{
				
				frm.action = addParamToUrl(frm.action, 'RefreshAfterEdit=1');
				frm.submit();
			}
		}
	}
	
	function doDeleteEditForm(oEvent)
	{
		doCheckEditState(oEvent);
		
		if (oEvent.cancel == true)
		{
			return;
		}
		else
		{
			if (window.confirm('Weet u zeker dat u dit item wilt verwijderen?'))
			{
				var editForm = document.forms[0];
				var hiddenField2 = findHiddenControl('hiddenToolbarAction');
			
				hiddenField2.value = 'Delete=1';
				editForm.submit();
			}
		}
	}
	
	function doCancelEditForm()
	{
		var frm = document.forms[0];
		var act = frm.action;
		
		var i = act.indexOf('Popup');
		
		if (i == -1)
		{
			var editForm = document.forms[0];
			var hiddenField2 = findHiddenControl('hiddenToolbarAction');
			
			hiddenField2.value = 'Cancel=1';
			editForm.submit();
		}
		else
		{
			//window.dialogArguments.success = false;
			//window.close();
			closeEffModalForm(false);
		}
		
	}
	
	function doBackToBrowseForm(oButton, oEvent)
	{
		doCheckEditState(oEvent);
		
		if (oEvent.cancel == true)
		{
			return;
		}
		else
		{
			if (oButton.Tag != null && oButton.Tag != '')
			{
				window.navigate(oButton.Tag);
			}
		
		}
		
	}
	
	function doPrintEditForm(oButton, oEvent)
	{
		doCheckEditState(oEvent);
		
		if (oEvent.cancel == true)
		{
			return;
		}
		else
		{
			if (oButton.Tag != null && oButton.Tag != '')
			{
				var url = oButton.Tag;
				url = addParamToUrl(url,"Popup=1");
				window.open(url);
			}
		
		}
		
	}
	
	function findHiddenControl(controlID)
	{
		var elem;
		var elemId;
		var arrElements = document.body.getElementsByTagName('INPUT');
		
		//iterate over elements:
		for (var i=0; i<arrElements.length; i++)
		{
			//get pointer to current element:
			elem = arrElements[i];
			elemId = elem.id;

			if (elemId.search(controlID) != -1)
			{
				return elem;
			}
		}
		
		return null;
	}
	
	function findControl(controlID, tagName)
	{
		var elem;
		var elemId;
		var arrElements = document.body.getElementsByTagName(tagName);
		
		//iterate over elements:
		for (var i=0; i<arrElements.length; i++)
		{
			//get pointer to current element:
			elem = arrElements[i];
			elemId = elem.id;

			if (elemId.search(controlID) != -1)
			{
				return elem;
			}
		}
		
		return null;
	}
	
	function doWait(millis) 
	{
		date = new Date();
		var curDate = null;

		do { var curDate = new Date(); } 
		while(curDate-date < millis);
	} 
	
	function removeParamFromUrl(strURL, param)
	{
		var url = strURL;
		var pos;
		var pos2;
		var queryStr;
		var lastPart;
		var firsPart;
		
		pos = url.indexOf('?');
		if (pos == -1)
		{
			return url;
		}
		
		queryStr = url.substring(pos + 1, url.length);
		firstPart = url.substring(0, pos);
		
		// convert to lowercase
		queryStr = queryStr.toLowerCase();
		pos = queryStr.indexOf(param.toLowerCase() + '=');
		
		if (pos != -1)
		{
			// determine length of the complete param
			lastPart = queryStr.substring(pos, queryStr.length);
			pos2 = lastPart.indexOf('&');
			if (pos2 != -1)
			{
				if (pos == 0) // it's the first param
				{
					lastPart = queryStr.substring(pos2 + 1, queryStr.length);
				}
				else // it's somewhere in the middle
				{
					lastPart = queryStr.substring(0, pos) + lastPart.substring(pos2 + 1, lastPart.length);
				}
			}
			else
			{
				// it's the last param
				lastPart = queryStr.substring(0, pos-1);
			}
			
			if (lastPart != '')
				return firstPart + '?' + lastPart;
			else
				return firstPart;	
			
		}
		else
		{
			return url;
		}
		
	}
	
	
	function findParamInUrl(strURL, param)
	{
		var url = strURL;
		var pos;
		var queryStr;
		var arr;
		
		pos = url.indexOf('?');
		if (pos == -1)
		{
			return -1;
		}
		
		queryStr = url.substring(pos + 1, url.length); // this is the Querystring part of the url
		
		// put all parameters in array
		paramArray = queryStr.split("&");
		
		for (var i = 0; i<paramArray.length; i++)
		{
			arr = paramArray[i].split("=");
			if (arr[0].toLowerCase() == param.toLowerCase())
			{
				return arr[1].toString();
			}
		}
		
		return -1;
	}
	
	function findInputControlByShortID(controlID)
	{
		return findControlByShortID(controlID, 'INPUT');
	}

	function findTextAreaControlByShortID(controlID)
	{
		return findControlByShortID(controlID, 'TEXTAREA');
	}

	function findDivControlByShortID(controlID)
	{
		return findControlByShortID(controlID, 'DIV');
	}
	
	function findGridByShortID(gridId)
	{
		var gridName = getGridFullName(gridId);
		var grid = igtbl_getGridById(gridName);
		
		return grid;
	}
	
	// zoek een control buiten de huidige usercontrol
	// om de zoektijd te beperken moet het type opgegeven worden b.v. INPUT of DIV
	// om naamconficten te beperken kan de .ID ook nog uitgebreid worden naar [usercontrolnaam]_[control.id]
	function findControlByShortID(controlID, tagName)
	{
		var elem;
		var elemId;
		var arrElements = document.body.getElementsByTagName(tagName);
		
		//iterate over elements:
		for (var i=0; i<arrElements.length; i++)
		{
			//get pointer to current element:
			elem = arrElements[i];
			elemId = elem.id;

			if (elemId.search(controlID) != -1)
			{
				return elem;
			}
		}
		
		return null;
	}
		
		
	function findControlByShortIDInWindow(controlID, tagName, oWindow)
	{
		var elem;
		var elemId;
		var arrElements;
		
		if (oWindow == null)
			arrElements = document.body.getElementsByTagName(tagName);
		else
			arrElements = oWindow.document.body.getElementsByTagName(tagName);
		
		//iterate over elements:
		for (var i=0; i<arrElements.length; i++)
		{
			//get pointer to current element:
			elem = arrElements[i];
			elemId = elem.id;

			if (elemId.search(controlID) != -1)
			{
				return elem;
			}
		}
		
		return null;
	}
	
	function doOnHelp()
	{
		var url = "frmHelpTeksten.aspx?ContextID=" + helpContextID;
		
		window.open(url);
		
		// prevent the internet explorer help showing up
		event.returnValue = false;
	}
	
	
	function doOnHelp2(contextID)
	{
		var url = "frmHelpTeksten.aspx?ContextID=" + contextID;
		
		if (parseInt(contextID) != -1)
		{			
			window.open(url);
		}
		else
		{
			window.open(url);
		}
		
		// prevent the internet explorer help showing up
		event.returnValue = false;
	}
	
	function changeHelpContextFromTabClick(oWebTab, oTab, oEvent)
	{
		//If a tabSheet is clicked, change the Help Context ID.
			
		var attrColl = oWebTab.element.attributes;
		var attr = attrColl['helpContextString'];
		
		if (attr != null)
		{
			var helpString = attr.value;
			if (helpString != null)
			{
				var arr = helpString.split("_");
				var index = oTab.index;
				var arr2 = arr[index].split("=");
				var contextID = arr2[1];
				
				if (contextID != null && contextID != "")
				{
					helpContextID = contextID;
				}
			}
		}
    }

    function checkExceptions() {
        var ctrl = document.getElementById('hiddenExceptions');

        if (ctrl == null || ctrl.value == '') {
            ctrl = document.getElementById('hiddenExceptionsAjax');
        }

        if (ctrl != null) {
            if (ctrl.value != '') {
                showExceptionDialog(ctrl.value);
            }
        }
    }
    

    function checkValidations() {
        var ctrl = findInputControlByShortID('hiddenValidations');
        if (ctrl != null) {
            if (ctrl.value != '') {
                showValidationDialog(ctrl.value);
            }
        }
    }

    function checkMessages() {
        var ctrl = findInputControlByShortID('hiddenMessages');
        if (ctrl != null) {
            if (ctrl.value != '') {
                showMessageDialog(ctrl.value);
            }
        }
    }

    function __raisePostBackEvent(eventName, targetControl) {
           var ctrl = findInputControlByShortID('hiddenPostBack');
        if (ctrl != null) {
            var frm = document.forms[0];

            if (eventName.indexOf('event') != -1) {
                var pos1 = eventName.indexOf('(');
                var pos2 = eventName.indexOf(')');
                var arg = eventName.substring(pos1 + 1, pos2);

                var val = eval(arg);

                val = val + ', ' + event.srcElement.id;
                
                eventName = eventName.replace(arg, val);
            }
            
            ctrl.value = eventName;
            if (targetControl != null) {
                ctrl.value += "$$" + targetControl.id;
            }

            frm.submit();
        }

        return false;
    }

    var __validationFunctionName;
    
    function __raisePostBackEventWithValidate(eventName, targetControl) {
        var result = true;
        if (__validationFunctionName != null && __validationFunctionName != "") {
            result = eval(__validationFunctionName);
        }

        if (result) {
            __raisePostBackEvent(eventName, targetControl);
        }
    }
    
    function __attachEvent(eventName, func, ctrl) {
        var control = window;
        if (ctrl != null) {
            control = ctrl;
        }
       
        if (window.addEventListener) { // Mozilla, Netscape, Firefox
            var name = eventName.replace("on", "");
	        control.addEventListener(name, func, false);
        } else { // IE
	        control.attachEvent(eventName, func);
        }
    }

    function showDialog(message, dialogType) {
        switch (dialogType)
        {
            case 1:
                showDialogInLayer(message, dialogType);
                break;
            case 2:
                showDialogInLayer(message, dialogType);
                //window.alert(message);
                break;
            default:
                showDialogInLayer(message, dialogType);
                //window.alert(message);
                break;                
        }
    }
    
    function showValidationDialog(message){
        showDialog(message, 1);
    }
    
    function showExceptionDialog(message){
        showDialog(message, 2);
    }

    function showMessageDialog(message) {
        showDialog(message, 3);
    }

    function showDialogInLayer(message, dialogType) {

        if (typeof initModalDialog == 'function') {
            var title = "";
            switch (dialogType) {
                case 1:
                    title = "Onjuiste gegevens";
                    break;
                case 2:
                    title = "Er gaat iets mis...";
                    break;
                case 3:
                    title = "Info...";
                    break;                   
            }
            
            initModalDialog();
            message = message.replace(/(\n)/g, "<br/>");
            showModalDialog(title, "<p>" + message + "</p>", 400, null, null); return false;
        }
        else {
            alert(message)
        }
    }

    function callWebService(webServiceUrl, webMethodName, requestXml) {
        
        var oXmlHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP");
        var webService = webServiceUrl;//  + "?op=" + webMethodName;   //"http://" + webServerIP + "/Typhone.WebServices/Postcode/PostcodeService.asmx?op=GetStraatPlaats'"
        oXmlHttp.open("POST", webService, false);
        oXmlHttp.setRequestHeader("Content-Type", "text/xml");
        oXmlHttp.setRequestHeader("SOAPAction", "http://tempuri.org/" + webMethodName);
        oXmlHttp.setRequestHeader("Content-Length", "200");

        var S = "";
        S = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>";
        S += "<soap:Body>";
        S += "<" + webMethodName + " xmlns='http://tempuri.org/'>";
        S += requestXml;
        S += "</" + webMethodName + ">"
        S += "</soap:Body>";
        S += "</soap:Envelope>";

        oXmlHttp.send(S);

        return oXmlHttp.responseXML;
    }

    function selectSingleNodeValue(xmlDoc, elementPath) {

        //return getFirstNodeValueByXpath(xmlDoc, elementPath);
        var node = selectSingleNode(xmlDoc, elementPath);

        if (node != null) {
            if (node.text != null) // IE
                return node.text;
            else
                return node.textContent;
        }
        else {
            return "NodeNotFound";
        }
    }
    
    function selectSingleNode(xmlDoc, elementPath) {

        return getFirstNodeByElementName(xmlDoc, elementPath.replace("//", ""));
    }

    function getFirstNodeByElementName(xmlDoc, elementName) {

        var nodes = xmlDoc.getElementsByTagName(elementName);

        if (nodes != null && nodes.length > 0) {
            return xmlDoc.getElementsByTagName(elementName)[0];
        }
        else {
            return null;
        }
    }

    var mustPreventRefresh = false;
    function checkKeyDown(e) {
        var isIE = (window.event != null);

        if (mustPreventRefresh) {
            var keycode = (isIE) ? event.keyCode : e.keyCode;

            if (isIE) {
                if (keycode == 116) {
                    event.keyCode = 0;
                    event.returnValue = false;
                    return false;
                }
            }
            else {
                if (keycode == 116) {
                    return false;
                }
            }
        }
    }
    




