﻿var Current_Page = window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1).toLowerCase();

function goToThema()
{
	var listThemas = document.getElementById("listThemaSelect");
	
	if (listThemas)
	{
		if (Current_Page.indexOf("nieuws") > -1)
		{
			document.location.href = "nieuws.aspx?tid=" + listThemas.value;
		}
		else if (Current_Page.indexOf("kalender") > -1)
		{
			document.location.href = "activiteitenkalender.aspx?tid=" + listThemas.value;
		}
		else if (Current_Page.indexOf("kwaliteitshandboek_procedures") > -1)
		{
			document.location.href = "kwaliteitshandboek.aspx?tid=" + listThemas.value;
		}
		else
		{
			document.location.href = "content.aspx?tid=" + listThemas.value;
		}
	}
	else
	{
		alert("Thema selectie niet gevonden.")
	}
}

function goToManualThema()
{
	var listThemas = document.getElementById("listManualThemaSelect");
	
	if (listThemas)
	{
		document.location.href = Current_Page + "?mtid=" + listThemas.value;
	}
	else
	{
		alert("Thema selectie niet gevonden.")
	}
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function confirmUrl(url, text)
{
    if(confirm(text))
    {
        location.href = url;
    }
    return false;
}

function getElementsByClass(searchClass,node,tag) 
{
	var classElements = new Array();
	
	if ( node == null )
		node = document;
		
	if ( tag == null )
		tag = '*';
		
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	
	for (i = 0, j = 0; i < elsLen; i++) 
	{
		if ( pattern.test(els[i].className) ) 
		{
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function getIdFromName(strId)
{
    return strId.substring(strId.lastIndexOf("_")+1, strId.length)
}

function ChangeClass(element, orgCssClass, newCssclass)
{
   element.className = element.className.replace(orgCssClass, newCssclass);
}

function changeTitle(nummerId, titelId)
{
    var nummerEl = document.getElementById(nummerId);
    var titelEl = document.getElementById(titelId);
    var spanEls = getElementsByClass("formTitle");
    
    for (var i = 0; i < spanEls.length; i++) 
	{
	    if ( nummerEl.value.length > 0 )
            spanEls[i].innerHTML = "(" + nummerEl.value + ") " + titelEl.value;
        else
            spanEls[i].innerHTML = titelEl.value;
	}    
}

function ShowModalPopup(formId, hiddenFieldId, popupId) 
{
    var formIdField = document.getElementById(hiddenFieldId);
    
    if ( formIdField )
    {
        formIdField.value = formId;
    }
    $find(popupId).show(); 
}

function HideModalPopup(id) 
{
    $find(id).hide(); 
}


function trimString(str)
{
	if( str )
	{
		return str.replace(/^\s+/,'').replace(/\s+$/,'');
	}
	else
	{
		return str
	}
}


/* #############################################################
 Set checboxes by there id
############################################################# */
function SetCheckBoxesByID(boxID, action)
{
	var i, formElements = document.forms[0].elements;
	
	for(i = 0; i < formElements.length; i++)
	{
		if( (formElements[i].type == 'checkbox') && (formElements[i].id.indexOf(boxID) > -1) )
		{
			// check / uncheck
			if( action == 0 || action == 1 )
			{
				formElements[i].checked = action;
			}
			// toggle
			else if( action == 2 )
			{
				formElements[i].checked = !formElements[i].checked;
			}
		}
	}
}


function getRef(obj){
	return(typeof obj == "string") ? document.getElementById(obj) : obj;
}

function sitchImage(imgName, imgSrc)
{
	var obj = getRef(imgName);
	if( obj ) obj.src = imgSrc;
}

function openWindow(src, title, width, height)
{
	window.open(src, title, "width="+width+",height="+height+",left=20,top=20,status=no,toolbar=no");
}

function OpenPics(url, iwidth, iheight)
{
	window.open(url,"afbeelding","width="+iwidth+",height="+iheight+",left=125,top=100");
}

function getFirstElementByClassName(findClass)
{
	var aElm = document.body.getElementsByTagName('*');
	var objFound;
	for (var i = 0; i < aElm.length; i++)
	{
		if(aElm[i].className == findClass)
		{
			objFound = aElm[i];
			break;
		}
	}
	
	return objFound;
}




function stripHTML(oldString)
{
  return oldString.replace(/<&#91;^>&#93;*>/ig, "");
}

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Robert Nyman | http://robertnyman.com/ */
function removeHTMLTags(strValue)
{
	var strInputCode = strValue;
	
 		/* 
  			This line is optional, it replaces escaped brackets with real ones, 
  			i.e. < is replaced with < and > is replaced with >
 		*/	
 	 	//strInputCode = strInputCode.replace(/&(lt|gt);/g, function (strMatch, p1){
 		// 	return (p1 == "lt")? "<" : ">";
 		//});
 		
 	var strTagStrippedText = strInputCode.replace(/<\/?[^>]+(>|$)/g, "");
   	return strTagStrippedText;
}

function clearField(field)
{
    if ( field.value == " - vul een zoekterm in - " )
    {
        field["initvalue"] = field.value;
        field.value = "";
        field.focus();
    }

}

function resetField(field)
{
    if ( field.value == "" )
    {
        
        if (field["initvalue"] == undefined) {
            field.value = ' - vul een zoekterm in - '
        }
        else {
            field.value = field["initvalue"];
        }
    }
}

function toggleAnswer(id)
{
    var elQuestion = document.getElementById("vraag_"+id);
    var elAnswer = document.getElementById("antwoord_"+id);
    
    if (elQuestion)
    {
        if (elQuestion.className == "faqclosed")
            elQuestion.className = "faqopen";
        else
            elQuestion.className = "faqclosed";
    }    
    
    if (elAnswer)
    {
        if (elAnswer.style.display == "none")
            elAnswer.style.display = "";
        else
            elAnswer.style.display = "none";
    }
    
    return false;
   }

var HIDDEN_PORTAL_LAYOUT_ID = '#hiddenPortalLayout';
var DEFAULT_MIN_HEIGHT = '182px';

function initPortalLayout() {

	$('.collapsed').each(function() {

		var icon = $(this).find('h2 a');

		icon.attr('title', 'open dit blok')
		icon.text('title', 'open dit blok')
		icon.css('background-image', 'url(/lib/images/portal_plus.png)');
		$(this).find('ul').hide();
		var strCollapsedHeight = $(this).find('h2').height() + 'px';
		$(this).css('min-height', strCollapsedHeight);
	})

	$('.sortable').sortable({
		connectWith: '.connectedSortable',
		handle: 'h2'
	});

	$('.sortable').bind('sortupdate', function(event, ui) {
		updateLayoutUI();
	});

	$('.portalset h2 a').click(function() {

		if (DEFAULT_MIN_HEIGHT == $(this).parent().parent().css('min-height')) {
			$(this).attr('title', 'open dit blok')
			$(this).text('title', 'open dit blok')
			$(this).css('background-image', 'url(/lib/images/portal_plus.png)');
			$(this).parent().next('ul').hide();
			var strCollapsedHeight = $(this).parent().height() + 'px';
			$(this).parent().parent().css('min-height', strCollapsedHeight);

		} else {
			$(this).attr('title', 'sluit dit blok')
			$(this).text('title', 'sluit dit blok')
			$(this).css('background-image', 'url(/lib/images/portal_min.png)');
			$(this).parent().next('ul').show();
			$(this).parent().parent().css('min-height', DEFAULT_MIN_HEIGHT);
		}

		updateLayoutUI();

		return false;
	});
}

function updateLayoutUI() {

	$('#ajaxloading').show();
	
	var strLayout = '';

	$('.sortable').each(function() {
		strLayout += '[' + $(this).attr('rel') + ']';
		$(this).find('.portalset').each(function() {
			strLayout += '{'
			strLayout += $(this).attr('rel');
			strLayout += '}'
			strLayout += '<'
			if ($(this).css('min-height') != DEFAULT_MIN_HEIGHT)
				strLayout += '0';
			else
				strLayout += '1';
			strLayout += '>';
			strLayout += '|';
		});
		strLayout += '#';
	});

	$(HIDDEN_PORTAL_LAYOUT_ID).val(strLayout);

	$.post(
		'/AJAX/SavePortalLayout.aspx',
		{ layout: $(HIDDEN_PORTAL_LAYOUT_ID).val() },
		function(data) {
			$('#ajaxloading').hide();
			if (data.toString().indexOf('success') == -1) {
				alert(data);
			}
		}
	)
}

$(document).ready(function() {
	initPortalLayout();
});
