var uriWindow;
function windowURI(url, w, h,time,resizable)	// windowURI(link,width,height,time(close the new window after x seconds),resizable(1 -> true)
{
	if(time == undefined)
		time = 0;

	if(resizable == undefined)
		resizable = 1;

	uriWindow = window.open(url,"Window_print","menubar=no,scrollbars=1,left=10,top=0,width="+w +" ,height="+h +",resizable="+resizable);

	if(time != 0 && time != -1)
	{
		setTimeout('uriWindow.close();',time);
	}
}

function toggleAllButActive(toggle_class, active_item_id)
{
	allDivs = document.getElementsByTagName('div');
	for(i = 0; i < allDivs.length; i++)
	{
		if(allDivs[i].className && allDivs[i].className == toggle_class)
		{
			if(allDivs[i].id != active_item_id)
			{
				allDivs[i].style.display = "none";
			}
			else
			{
				allDivs[i].style.display = "block";
			}
		}
	}

	return false;
}

function checkEditPage()
{
	if (document.getElementById('forcetypeName') && document.getElementById('forcetypeName').value != '')
	{
		forceTypeChecked = /^\w*$/i.test(document.getElementById('forcetypeName').value);

		if (!forceTypeChecked)
		{
				alert('Verkorte URL bevat characters die niet gebruikt kunnen worden als URL.');
				return false;
		}
		else
		{
			forcetypeName = document.getElementById('forcetypeName').value;
			forcetypeName.toLowerCase();

			if (reservedForcetypeNames && reservedForcetypeNames.contains(forcetypeName))
			{
				alert('Verkorte URL is al ingebruik door een andere pagina.');
				return false;
			}
		}
	}
	return true;
}

function windowPrint(url, w, h)
{
	//alert(url);
	printWindow=window.open(url,"Window_print","menubar=no,scrollbars=1,left=10,top=1,width="+w +" ,height="+h +",resizable=1")
}

var exampleWindow;
function windowExample(url, w, h)
{
	//alert(url);
	if(exampleWindow)
		exampleWindow.focus();
	exampleWindow=window.open(url,"Window_print","menubar=no,scrollbars=1,left=10,top=1,width="+w +" ,height="+h +",resizable=1")
}

function openWindow(text)
{
	while((text.indexOf("%BR%") != -1))
	{
		text = text.replace("%BR%","<BR>");
	}
	myWin = window.open("","nCt", "left=400, top=300,width=180,height=30,status=no,toolbar=no,menubar=no,scrollbars=auto,resizable=yes");
	myWin.document.write("<html><head><title>Note</title></head>");
	myWin.document.write ("<body><script>function closeWindow(){window.close()} \n\n setTimeout(\"closeWindow()\",1000);</script>"+text+"</body></html>");
	//setTimeout("myWin.close()",200);
	//myWin.document.write("<center><p><input type='button' value='Afsluiten' name='close' onClick='window.close()'></p><center>");
}

function CheckTab(el)
{
	if ((document.all) && (9==event.keyCode) && (event.ctrlKey))
	{
		el.selection=document.selection.createRange();
	    setTimeout("ProcessTab('" + el.id + "')",0);
	}
	if ((document.all) && (83==event.keyCode) && (event.ctrlKey))
	{
		document.forms[0].submit();
		openWindow('De pagina is opgeslagen');
	}
}

function ProcessTab(id)
{
	document.all[id].selection.text=String.fromCharCode(9);
	document.all[id].focus();
}

function changeColor(el, color1, color2)
{
	//alert(el);
	document.getElementById(el).style.background = (document.getElementById(el).style.background == color1) ? color2 : color1;
}

function test(cell,color1,color2)
{
	cell.style.background = (cell.style.background == color1) ? color2 : color1;
}

function checkUserForm()
{
	if(document.forms[0].username.value=="")
	{
		alert('U moet een gebruikersnaam opgeven!');
		return false;
	}

	/*if(document.forms[0].email.value=="")
	{
		alert('U moet email opgeven!');
		return false;
	}*/

	if(document.forms[0].password.value=="")
	{
		alert('U moet password opgeven!');
		return false;
	}

	var value = false;

	for(i=0;i<document.forms[0].elements.length;i++)
	{
		if(document.forms[0].elements[i].type=='checkbox')
		{
			if(document.forms[0].elements[i].checked==true)
			{
					value=true;
					break;
			}
		}
	}
	if(value==false)
	{
		alert('U dient minimaal 1 groep te selecteren!');
		return false;
	}
	return true;
}

function checkUpdateUserPropertiesForm()
{
	if(document.forms[0].username.value=="")
	{
		alert('U moet een gebruikersnaam opgeven!');
		return false;
	}

	/*if(document.forms[0].email.value=="")
	{
		alert('U moet email opgeven!');
		return false;
	}*/

	if(document.forms[0].password.value=="")
	{
		alert('U moet password opgeven!');
		return false;
	}

	return true;
}

function checkGroupForm()
{
	if(document.forms[0].name.value=="")
	{
		alert('U moet een naam voor de groep opgeven!');
		return false;
	}

	if(document.forms[0].profileId.value=="")
	{
		alert('U moet een profiel selecteren!');
		return false;
	}
}

function checkDescriptionAmount(maxAmount)
{
	if(document.descForm.personAmount.value<maxAmount)
	{
		return true;
	}
	else
	{
		alert("U kunt niet meer dan " + maxAmount + " personen inschrijven");
		return false;
	}
}

function TrackCount(fieldObj,countFieldName,maxChars)
{
	var countField = eval("fieldObj.form."+countFieldName);
	var diff = maxChars - fieldObj.value.length;

	// Need to check & enforce limit here also in case user pastes data
	if (diff < 0)
	{
		fieldObj.value = fieldObj.value.substring(0,maxChars);
		diff = maxChars - fieldObj.value.length;
	}
	countField.value = diff;
}

function LimitText(fieldObj,maxChars)
{
	var result = true;
	if (fieldObj.value.length >= maxChars)
		result = false;

	if (window.event)
		window.event.returnValue = result;
	return result;
}

function imgOn(menuObj)
{
	name = menuObj.name;
	leftImage = "img"+name+1;
	rightImage = "img"+name+2;
	document.images[leftImage].src="images/menu_low.gif";
	document.images[rightImage].src="images/menu2_low.gif";
}

function imgOut(menuObj)
{
	name = menuObj.name;
	leftImage = "img"+name+1;
	rightImage = "img"+name+2;
	document.images[leftImage].src="images/menu_hi.gif";
	document.images[rightImage].src="images/menu2_hi.gif";
}

function imgSubOn(menuObj)
{
	name = menuObj.name;
	leftImage = "subimg"+name+1;
	rightImage = "subimg"+name+2;
	document.images[leftImage].src="images/menu_sub_low.gif";
	document.images[rightImage].src="images/menu2_low.gif";
}

function imgSubOut(menuObj)
{
	name = menuObj.name;
	leftImage = "subimg"+name+1;
	rightImage = "subimg"+name+2;
	document.images[leftImage].src="images/menu_sub_hi.gif";
	document.images[rightImage].src="images/menu_sub2_hi.gif";
}

function checkAllFields(element)
{
	if(confirm("Hiermee gaat een eventuele selectie verloren"))
	{
		for(var i=0;i<document.forms[0].elements.length;i++)
		{
			document.forms[0].elements[i].checked=checkFields;
		}
		checkFields = !checkFields;
		if(checkFields)
			element.value = "alles aan";
		else
			element.value = "alles uit";
	}
}

function emptyForm()
{
	for(var i=0;i<document.forms[2].elements.length;i++)
	{
		if(document.forms[2].elements[i].type=="text")
			document.forms[2].elements[i].value="";
	}
}

function changeImage(img, newSrc)
{
	img.src = newSrc;
}

function open_new_window(location)
{
	new_window = window.open(location,"mailafriend","width=400,height=450,scrollbars=yes,left=10,top=10");
}

function checkAll(formName)
{
	for(var i=0;i<document.forms[formName].elements.length;i++)
	{
		if(document.forms[formName].elements[i].type=='checkbox' && document.forms[formName].elements[i].alt!='none')
			document.forms[formName].elements[i].checked = true;
	}
}

function unCheckAll(formName)
{
	for(var i=0;i<document.forms[formName].elements.length;i++)
	{
		if(document.forms[formName].elements[i].type=='checkbox' && document.forms[formName].elements[i].alt!='none')
			document.forms[formName].elements[i].checked = false;
	}
}

function checkElement(elementId)
{
	document.getElementById(elementId).checked = true;
}

/*function checkForm(formName, userId)
{
	var found = false;
	for(var i=0;i<document.forms[formName].elements.length;i++)
	{
		if(document.forms[formName].elements[i].type=='checkbox')
		{
			if(document.forms[formName].elements[i].checked==true)
			{
				found = true;
				break;
			}
		}
	}
	if(!found)
	{
		alert('Gelieve minimaal 1 gebruiker aan te vinken');
		checkElement('author_'+userId);
	}
	return found;
}*/

dropDownEdited = false;

function setEndHour(beginDropDown)
{
	var endHouresDropDown = document.getElementById('endhoures');
	var startMinutesDropDown = document.getElementById('startminutes');
	if(endHouresDropDown.selectedIndex<beginDropDown.selectedIndex)
	{
		endHouresDropDown.options[beginDropDown.selectedIndex].selected = true;
	}
	setEndMinute(startMinutesDropDown);
}

function setEndMinute(beginDropDown)
{
	var startHouresDropDown = document.getElementById('starthoures');
	var endHouresDropDown = document.getElementById('endhoures');
	var endMinutesDropDown = document.getElementById('endminutes');
	if(startHouresDropDown.selectedIndex==endHouresDropDown.selectedIndex)
	{
		if(endMinutesDropDown.selectedIndex<beginDropDown.selectedIndex)
		{
			endMinutesDropDown.options[beginDropDown.selectedIndex].selected = true;
		}
	}
}

function checkEmpty(elementNames)
{
	var errorElements = new Array();
	var j=0;
	for(var i=0;i<elementNames.length;i++)
	{
		if(document.getElementById(elementNames[i]).value=='')
		{
			errorElements[j] = elementNames[i];
			j++;
		}
	}
	return errorElements;
}

function checkAgenda()
{
	var elementArray = new Array("mainhead");
	var errorElements = checkEmpty(elementArray);
	var errorMessage = '';
	if(errorElements.length != 0)
	{
		for(var k=0;k < errorElements.length;k++)
		{
			if(errorElements[k]=='mainhead')
			{
				errorMessage += 'Gelieve een titel in te vullen';
			}
		}
		alert(errorMessage);
		return false;
	}
	return true;
}

var currentlySelected = -1;
function selectOne(dropDown,optionid)
{
	if(currentlySelected!=-1 || optionid)
	{
		for(var i=0;i<dropDown.options.length;i++)
		{
			if(optionid)
			{
				if(dropDown.options[i].id==optionid)
					dropDown.options[i].selected = true;
				else
					dropDown.options[i].selected = false;
			}
			else
			{
				if(i==currentlySelected)
					dropDown.options[i].selected = false;
				if(i==dropDown.selectedIndex)
					dropDown.options[i].selected = true;
			}
		}
	}
	currentlySelected = dropDown.selectedIndex;
}

function mouseOver(adminButton)
{
	var splittedSrc = adminButton.src.split("/");
	var restSrc = "";
	fileName = splittedSrc[splittedSrc.length-1];
	for(var i=0;i<splittedSrc.length-1;i++)
	{
		restSrc += splittedSrc[i] + "/";
	}
	var splittedFileName = fileName.split(".");
	adminButton.src =  restSrc + splittedFileName[0] + "_hov" + "." + splittedFileName[1];
}

function mouseOut(adminButton)
{
	var splittedSrc = adminButton.src.split("/");
	var restSrc = "";
	fileName = splittedSrc[splittedSrc.length-1];
	for(var i=0;i<splittedSrc.length-1;i++)
	{
		restSrc += splittedSrc[i] + "/";
	}
	var splittedFileName = fileName.split(".");
	var newFileName = splittedFileName[0].substring(0,splittedFileName[0].length-4);
	adminButton.src =  restSrc + newFileName + "." + splittedFileName[1];
}

function checkGroups()
{
	for(var i=0;i<document.forms.sendGroups.elements.length;i++)
	{
		if(document.forms.sendGroups.elements[i].type=="checkbox" && document.forms.sendGroups.elements[i].checked)
		{
			return true;
		}
	}
	alert('Gelieve een groep te kiezen.');
	return false;
}

function checkGroupAmount()
{
	var currentForm = document.forms[0];
	for(var i=0;i<currentForm.elements.length;i++)
	{
		if(currentForm.elements[i].type=="checkbox")
		{
			if(currentForm.elements[i].checked)
				return true;
		}
	}
	return confirm("Deze gebruiker is nu niet meer gekoppeld aan een groep. U vindt hem nu alleen nog terug in het overzicht van alle gebruikers.");
}

function displayFormat(dropDown)
{
	if(dropDown.options[dropDown.selectedIndex].value == 322)
		document.getElementById("obl_format").style.display = "";
	else
		document.getElementById("obl_format").style.display = "non";
}

function executeAction(action,actionObject)
{
	//alert(actionObject.id + ":" + action);
	switch(action)
	{
		case "display":
		{
			if(actionObject.style.display!="")
				actionObject.style.display = "";
			break;
		}
		case "hide":
		{
			if(actionObject.style.display!="none")
				actionObject.style.display = "none";
			break;
		}
	}
}

function getElementValue(elementName)
{
	return document.getElementById(elementName).value;
}

function getElementLength(elementName)
{
	//alert(document.getElementById(elementName).value.length);
	return document.getElementById(elementName).value.length;
}

function setVisibility(elementId,visible)
{
	if(visible)
		document.getElementById(elementId).style.display = "";
	else
		document.getElementById(elementId).style.display = "none";
}

function displayFormat(dropDown)
{
	if(dropDown.options[dropDown.selectedIndex].value == 1656)
		document.getElementById("obl_format").style.display = "";
	else
		document.getElementById("obl_format").style.display = "none";
}

function checkEmptyId(inputId)
{
	var translationArray = new Array(2);
	translationArray['content'] = '[[TRANSLATION.error_content]]';
	translationArray['mainhead'] = '[[TRANSLATION.error_mainhead]]';

	if(document.getElementById(inputId).value == '')
	{

		alert(translationArray[inputId]);
		return false;
	}
	return true;
}

function copyInputTo(copyFromElement,copyElementId) /* Copy the value from an element to another element onchange (e.g. menutitle to mainhead) */
{
	document.getElementById(copyElementId).value = document.getElementById(copyFromElement).value;
}

function createTarget(t)
{
    window.open("", t, "width=600,height=550,scrollbars=1");
    return true;
}

function getHTTPObject() {

  var xmlhttp;

  /*@cc_on

  @if (@_jscript_version >= 5)

    try {

      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e) {

      try {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (E) {

        xmlhttp = false;

      }

    }

  @else

  xmlhttp = false;

  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {

    try {

      xmlhttp = new XMLHttpRequest();

    } catch (e) {

      xmlhttp = false;

    }

  }

  return xmlhttp;

}

function popUp(URL, width, height)
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+width+",height="+height+",left = 320,top = 272');");
}

function checkNumeric(textfield,message)		// Checks whether the userinput is numeric or not
{
	inputstring = textfield.value;
	for (i = 0; i < inputstring.length; i++)
	{
		ch = inputstring.substring(i, i+1);
		if (ch < "0" || ch > "9")
		{
			if(message != '')
				alert(message);
			textfield.value = textfield.value.substring(0, i);
			textfield.focus();
			return false;
		}
	}
	return true;
}


function handleLoginSubmit(form)
{

	if(!md5_vm_test())
	{
		return true;
	}
	else
	{

		p = document.getElementById('password').value;
		c = document.getElementById('challenge').value;

		document.getElementById('passwordS').value = hex_md5(hex_md5(p) + c);
		document.getElementById('loginnameS').value = document.getElementById('loginname').value
		document.getElementById('shadowForm').submit();
		return false;
	}

}

function generateRandomString(length)
{

	var retVal = '';

	for (i=0; i < length; i++)
	{
		retVal += String.fromCharCode((parseInt(Math.random() * 1000) % 94) + 33);
	}

	return retVal;
}

var emptiedElements = new Array();
function js_in_array(the_needle, the_haystack)
{
	var the_hay = the_haystack.toString();
	if(the_hay == ''){
		return false;
	}
	var the_pattern = new RegExp(the_needle, 'g');
	var matched = the_pattern.test(the_haystack);
	return matched;
}

function emptyValue(element)
{
	if(!js_in_array(element.id,emptiedElements))
	{
		emptiedElements[emptiedElements.length] = element.id;
		element.value='';
	}
}

var fixedModuleWindows = new Array();

function showModuleInfoDiv(elmId)
{
	if (!js_in_array(elmId, fixedModuleWindows))
	{
		scrollTop = window.getScrollTop();
		topCoord = scrollTop+20;

		moduleInfoDiv = document.getElementById(elmId);
		moduleInfoDiv.parentNode.removeChild(moduleInfoDiv);
		document.body.appendChild(moduleInfoDiv);
		moduleInfoDiv.style.top = topCoord+'px';
		moduleInfoDiv.style.left = '20px';
		moduleInfoDiv.style.display = 'block';

	}
}

function clickModuleWindow(elmId)
{
	if (js_in_array(elmId, fixedModuleWindows))
	{
		// Remove this module window from the array
		tempArray = new Array();

		for (i=0; i<fixedModuleWindows.length; i++ )
		{
			if (fixedModuleWindows[i] != elmId)
			{
				tempArray[tempArray.length] = fixedModuleWindows[i];
			}
		}

		fixedModuleWindows = tempArray;
		delete tempArray;

		document.getElementById(elmId).style.display = 'none'; // Hide the module window
	}
	else
	{
		fixedModuleWindows[fixedModuleWindows.length] = elmId; // Put this module window in the array
		new Drag.Move($(elmId), {'container': bodyC});
		$(elmId).style.cursor = 'move';
	}
}

function moduleWindowOut(elmId)
{
	if (!js_in_array(elmId, fixedModuleWindows))
	{
		document.getElementById(elmId).style.display = 'none'; // Hide the module window
	}
}