var popwindows = new Array(); 
function count(arr){
	var i = 0;
	if(typeof(arr) == "object"){
		for(key in arr){
			i ++;
		}
	}
	return i;
}
function insert(n){
	n.fverwerken.value='verwerken';
	if (typeof(tempValue) == "object" && count(tempValue) > 0) {
		n.serialize_new.value = serialize(tempValue).replace(/\"/g,'\\"');
		n.serialize_old.value = serialize(form_source);
	}
	n.submit();
}

function check_menu(n, type) {
	err = "nee";
	if (n.MENUITEM.value == "") {
		alert(check_studyjobproject_2);
		err = "ja";
	}
	if(err == "nee") {
		if(type == "update"){
			n.MM_insert.value = "menu_update";
		}
		if(type == "insert"){
			n.MM_insert.value = "menu_insert";
		}
		n.submit();
	}
}

function check_groep(n,type){
	err = "nee";
	if (n.CODE.value == "") {
		alert(check_groep_1);
		err = "ja";
	}
	if ((n.OMSCHRIJVING.value == "") && (err == "nee")) {
		alert(check_groep_2);
		err = "ja";
	}
	if (err == "nee"){
		if(type == "update"){
			n.MM_insert.value = "groep_update";
		}
		if(type == "insert"){
			n.MM_insert.value = "groep_insert";
		}
		warning_string = check_groep_code;
		select = "select GRP_ID from GROEPEN where GRP_ID <> '" + n.record_id.value + "' and CODE = '" + n.CODE.value + "'";
		check_SendQuery(select, warning_string);
	}
}

function check_language(n,type){
	err = "nee";
	if (n.CODE.value == "") {
		alert(check_language_1);
		err = "ja";
	}
	if ((n.OMSCHRIJVING.value == "") && (err == "nee")) {
		alert(check_language_2);
		err = "ja";
	}
	if (err == "nee"){
		if(type == "update"){
			n.MM_insert.value = "language_update";
		}
		if(type == "insert"){
			n.MM_insert.value = "language_insert";
		}
		warning_string = check_language_code;
		select = "select LGG_ID from LANGUAGES where LGG_ID <> '" + n.record_id.value + "' and CODE = '" + n.CODE.value + "'";
		check_SendQuery(select, warning_string);
	}
}

function submit_form(n){
	n.submit();
}


function check_woorden(n,type) {
	err = "nee";
	if (n.CODE.value == "") {
		alert(check_woorden_1);
		err = "ja";
	}
	if ((n.NED.value == "") && (err == "nee")) {
		alert(check_woorden_2);
		err = "ja";
	}
	if ((n.ENG.value == "") && (err == "nee")) {
		alert(check_woorden_3);
		err = "ja";
	}
	if ((n.CHN.value == "") && (err == "nee")) {
		alert(check_woorden_4);
		err = "ja";
	}
	if (err == "nee"){
		if(type == "update"){
			n.MM_insert.value = "woorden_update";
		}
		if(type == "insert"){
			n.MM_insert.value = "woorden_insert";
		}
		warning_string = check_woorden_code + ': '+ n.CODE.value;
		select = "select TLW_ID from TAAL_WOORDEN where TLW_ID <> '" + n.record_id.value + "' and CODE = '" + n.CODE.value + "'";
		check_SendQuery(select, warning_string);
	}
}

/* sprint and zoke function for right part in the admin page*/
function spring(pag,tot){
	document.form.MM_insert.value = "";
	document.form.paginanummer.value = pag;
	document.form.submit();
}
function zoek_rec(){
	document.form.MM_insert.value = "";
	document.form.paginanummer.value = "0";
	document.form.submit();
}

/* sort function for right part in the admin page*/
function sort(kolom) {
	if (document.form.sorteer.value == kolom) {
		document.form.sorteer.value = kolom+' DESC';
	} else {
		document.form.sorteer.value = kolom;
	}
	document.form.MM_insert.value = "";
	document.form.submit();
}

/* add new, modify, remove javascript function in the admin page*/
function record_new(){
	document.form.MM_insert.value = "record_new";
	document.form.record_id.value = "";
	document.form.submit();
}
function record_modify(id){
	document.form.MM_insert.value = "record_modify";
	document.form.record_id.value = id;
	document.form.submit();
}
function record_remove(id){
	document.form.MM_insert.value = "record_remove";
	document.form.record_id.value = id;
	document.form.submit();
}

/*check code or name in the admin page*/
var check_req;
function check_Initialize(){
	try{
		check_req=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e){
		try{
			check_req=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc){
			check_req=null;
		}
	}
	if(!check_req&&typeof XMLHttpRequest!="undefined"){
		check_req= new XMLHttpRequest();
	}
}
function check_SendQuery(select, warning_string, module){
	check_Initialize();
	if (module == "HELP")
	{
		varurl="../includes/check_data.php?select=" + select + "&module="+ module + "&warning_string=" + warning_string + "&" + new Date().getTime();
	} else {
		varurl="../../includes/check_data.php?select=" + select + "&module="+ module + "&warning_string=" + warning_string + "&" + new Date().getTime();
	}
	if(check_req!=null){
		check_req.onreadystatechange = function(){
			if (check_req.readyState == 4){
				if (check_req.status == 200){
					if(check_req.responseText == "no data"){
						document.form.submit();
					}else{
						alert (check_req.responseText);
					}
				}else{
					alert("There was a problem retrieving data!");
				}
			}

		};
		check_req.open("GET", varurl, true);
		check_req.send(null);
	}
}

/**** end check function ****/
function refresh(){
	varurl = base_url + "includes/sessieheader.inc.php?now=" + new Date().getTime();
	check_Initialize();
	if(check_req!=null){
		check_req.onreadystatechange = function(){};
		check_req.open("GET", varurl, true);
		check_req.send(null);
	}
	setTimeout("refresh()",30000);
}
if (typeof(base_url) != "undefined" && base_url != ""){
	refresh();
}
function iso_date(date, format){
	if(typeof(format) == "undefined"){
		format = s_date_format;
	}
	var year;
	var month;
	var day;
	if(date != ""){
		dateArray = date.split('-');
		if(format.substring(1, 2) == "Y"){
			year = dateArray[0];
		}else if(format.substring(1, 2) == "m"){
			month = dateArray[0];
		}else if(format.substring(1, 2) == "d"){
			day = dateArray[0];
		}
		if(format.substring(4, 5) == "Y"){
			year = dateArray[1];
		}else if(format.substring(4, 5) == "m"){
			month = dateArray[1];
		}else if(format.substring(4, 5) == "d"){
			day = dateArray[1];
		}
		if(format.substring(7, 8) == "Y"){
			year = dateArray[2];
		}else if(format.substring(7, 8) == "m"){
			month = dateArray[2];
		}else if(format.substring(7, 8) == "d"){
			day = dateArray[2];
		}
		date_return = year + "-" + month + "-" + day;
	}else{
		date_return = "";
	}
	return date_return
}

function user_date(date, format){
	if(typeof(format) == "undefined"){
		format = s_date_format;
	}
	var first;
	var second;
	var third;
	if(format.substring(1, 2) == "Y"){
		first = date.substring(0,4);
	}else if(format.substring(1, 2) == "m"){
		first = date.substring(5,7);
	}else if(format.substring(1, 2) == "d"){
		first = date.substring(8,10);
	}
	if(format.substring(4, 5) == "Y"){
		second = date.substring(0,4);
	}else if(format.substring(4, 5) == "m"){
		second = date.substring(5,7);
	}else if(format.substring(4, 5) == "d"){
		second = date.substring(8,10);
	}
	if(format.substring(7, 8) == "Y"){
		third = date.substring(0,4);
	}else if(format.substring(7, 8) == "m"){
		third = date.substring(5,7);
	}else if(format.substring(7, 8) == "d"){
		third = date.substring(8,10);
	}
	date_return = first + "-" + second + "-" + third;
	return date_return
}

function is_leap_year(year){
	if(((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0){
		return 1
	}else{
		return 0
	}
}
function iso_week_days(yday, wday) {
	return yday - ((yday - wday + 382) % 7) + 3;
}
function getDateObj(date){
	var array = date.split("-");
	var dt = new Date(array[0],array[1]-1,array[2]);
	return dt;
}
function get_week_number(date){
	var array = date.split("-");
	var dt = new Date(array[0],array[1]-1,array[2]);
	var y = dt.getFullYear();
	var ft = new Date(y,'00','01');
	var tt = new Date('1970','00','01');
	ft_timestamp = ft.valueOf();
	dt_timestamp = dt.valueOf();

	startweek = ft.getDay();
	wday = dt.getDay();
	var yday = Math.round((dt_timestamp - ft_timestamp)/(24*60*60*1000)) ;
	var timestamp = Math.round((dt.valueOf() - tt.valueOf())/(1000));
	days = iso_week_days(yday, wday);
	if (days < 0) {
		yday  += 365 + is_leap_year(--y);
		days = iso_week_days(yday, wday);
	} else {
		yday -= 365 + is_leap_year(y);
		d2 = iso_week_days(yday, wday);
		if (d2 >=0) {
			days = d2;
		}
	}
	return_array = (Math.floor(days/7) + 1) +  '-' + timestamp + '-' + y;
	return  return_array;
}

//change the project id
function chang_pjt_id(){
	document.dossier.submit();
}

function inArray(value, arr){
	for(key in arr){
		if(arr[key] === value){return true;}
	}
	return false;
}

var assignmentXmlHttp;
function assignmentMakeRequest(url) {
	assignmentXmlHttp = assignmentGetXmlHttp();
	if (!assignmentXmlHttp) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	assignmentXmlHttp.onreadystatechange = function(){
		if (assignmentXmlHttp.readyState == 4) {
			if (assignmentXmlHttp.status == 200) {
				if(assignmentXmlHttp.responseText == "locked"){
					alert('the file is locked and that you cannot upload a new version');
				}else if(assignmentXmlHttp.responseText == "locked_self"){
					if(confirm("the file is locked by yourself, are you sure that upload a new version?")){
						document.form.MM_insert.value = "document_upload";
						document.form.submit();
					}
				}else{
					document.form.MM_insert.value = "document_upload";
					document.form.submit();
				}
			} else {
				alert('There was a problem with the request.');
			}
		}
	};
	assignmentXmlHttp.open('get', url, true);
	assignmentXmlHttp.send(null);
}

function assignmentGetXmlHttp(){
	var A = null;
	try{
		A = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			A = new ActiveXObject("Microsoft.XMLHTTP");
		} catch(oc){
			A = null;
		}
	}
	if(!A && typeof XMLHttpRequest != "undefined") {
		A = new XMLHttpRequest();
	}
	return A;
}

function open_en_lock(dcm_id){
	document.form.id.value = dcm_id;
	document.form.MM_insert.value = "document_lock";
	document.form.submit();
}

function unlocken(dcm_id) {
	var x = confirm('Document unlocken zonder een nieuwe versie te uploaden? ');
	if(x == true) {
		document.form.MM_insert.value = 'document_unlock';
		document.form.id.value = dcm_id;
		document.form.submit();
	}
}

function verwijderen(dcm_id) {
	var x = confirm('Document permanent verwijderen? ');
	if(x == true) {
		document.form.MM_insert.value = 'document_remove';
		document.form.id.value = dcm_id;
		document.form.submit();
	}
}

String.prototype.getTime = function (format) {
	if(typeof(format) == 'undefined') {
		format = '%d-%m-%Y';
	}
	var formats = format.split('-');
	var dates = this.split('-');
	for(var i = 0; i < 3; i++) {
		if(formats[i]=='%d') {
			var day = dates[i];
		} else if(formats[i]=='%m') {
			var month = dates[i];
		} else if(formats[i]=='%Y') {
			var year = dates[i];
		}
	}
	var date = new Date(year, month-1, day);
	return date.getTime();
}

//remove value from array
 function removevarfromarray(v,ar) {
	for(i=0; i<ar.length; i++) {
		if(ar[i]==v) {
		  ar.splice(i,1);
		}
	}
	return ar;
}
  
// search string in array
function in_array(stringtosearch, arraytosearch) {
	for (s = 0; s <arraytosearch.length; s++) {
	thisEntry = arraytosearch[s].toString();
	if (thisEntry == stringtosearch) {
	return true;
	}
	}
	return false;
} 

if(opener!=null&&(opener.location.pathname.indexOf('planning_overzicht.php')!=-1||opener.location.pathname.indexOf('maandkalender.php')!=-1))  //if there is opener window rewrite these functions
{
	//when close the window remove the record.
	window.onunload=function()
	{
		if(in_array(window.name,opener.popwindows))
		{
		  removevarfromarray(window.name,opener.popwindows);
		}
	}
	
	//when load the window add window name into the array.
	window.onload=function()
	{
		if(!in_array(window.name,opener.popwindows))
		{
		  opener.popwindows.push(window.name);
		}
	}

    //write the close function to remove the window name from the array
	var _windowClose=window.close;
	window.close=function()
	{
		if(in_array(window.name,opener.popwindows))
		{
		 removevarfromarray(window.name,opener.popwindows);
		}
	_windowClose();
	} 
}

