function recordAction(basePath, strType, strAction, strFrom) {
	var selectBox = document.forms["viewForm"]["selectorCB"];
	if (selectBox != undefined) {
		//first count the number of selected elements
		if(selectBox.length) {
			var counter = 0;
			for(i=0; i<selectBox.length; i++) {
				if(selectBox[i].checked) {
					counter++;
					selectedOption = selectBox[i];
				}
			}
		} else {
			//checkbox has no length (so just one checkbox)
			if(selectBox.checked) { 
				var counter = 1;
				selectedOption = selectBox;
			} else {
				var counter = 0;
			}
		}
		if(counter!=1) {
			alert("Gelieve exact \xe9\xe9n lijn aan te vinken!");
		} else {
			recordId = selectedOption.value;
			if (strAction == "copy") {
				newUrl = basePath + strType + "/" + strAction + "/id/" + recordId + "/from/" + strFrom;
				window.location = newUrl;
			} else {
				document.forms["modifyForm"]["recordId"].value = recordId;
				//fill form with default values (this function must be inplemented on every page separately)
				fillModifyForm(recordId);
				showModifyDialog();
			}
		}
	}
}

function deleteRecords(basePath, strType, strFrom) {
	var selectBox = document.forms["viewForm"]["selectorCB"];
	if (selectBox != undefined) {
		//gather the ID's
		var counter = 0;
		var isFirst = true;
		var allIds = "";
		if(selectBox.length) {
			for(i=0; i<selectBox.length; i++) {
				if(selectBox[i].checked) {
					if(isFirst) {
						allIds = selectBox[i].value;
						isFirst = false;
					} else {
						allIds += "|" + selectBox[i].value;
					}
				}
			}
		} else {
			//there is only one checkbox
			if(selectBox.checked) {
				allIds = selectBox.value;
			}
		}
		
		if(allIds=="") {
		} else {
			if(confirm("Bent u zeker dat u de geselecteerde lijn(en) wilt verwijderen?\nBijhorende relaties worden ook verwijderd!")) {
				newUrl = basePath + strType + "/delete/ids/" + allIds + "/from/" + strFrom;
				window.location = newUrl;
			}
		}
	}
}

function activateRecords(basePath, strType, blnOnOff, strFrom) {
	var selectBox = document.forms["viewForm"]["selectorCB"];
	if (selectBox != undefined) {
		//gather the ID's
		var counter = 0;
		var isFirst = true;
		var allIds = "";
		if(selectBox.length) {
			for(i=0; i<selectBox.length; i++) {
				if(selectBox[i].checked) {
					if(isFirst) {
						allIds = selectBox[i].value;
						isFirst = false;
					} else {
						allIds += "|" + selectBox[i].value;
					}
				}
			}
		} else {
			//there is only one checkbox
			if(selectBox.checked) {
				allIds = selectBox.value;
			}
		}
		if(allIds=="") {
			alert("Gelieve minstens \xe9\xe9n lijn aan te vinken!");
		} else {
			if (blnOnOff) {
				strActive = "actief";
			} else {
				strActive = "inactief";
			}
			if(confirm("Bent u zeker dat u de geselecteerde lijn(en) op " + strActive + " wilt zetten?")) {
				newUrl = basePath + strType + "/activate/ids/" + allIds + "/from/" + strFrom + "/active/" + blnOnOff;
				window.location = newUrl;
			}
		}
	}
}

function printView() {
	javascript:print();
}
function strLeft(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function strRight(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
/**
 * addEvent written by Dean Edwards, 2005
 * with input from Tino Zijdel
 *
 * http://dean.edwards.name/weblog/2005/10/add-event/
 **/
function addEvent(element, type, handler) {
	// assign each event handler a unique ID
	if (!handler.$$guid) handler.$$guid = addEvent.guid++;
	// create a hash table of event types for the element
	if (!element.events) element.events = {};
	// create a hash table of event handlers for each element/event pair
	var handlers = element.events[type];
	if (!handlers) {
		handlers = element.events[type] = {};
		// store the existing event handler (if there is one)
		if (element["on" + type]) {
			handlers[0] = element["on" + type];
		}
	}
	// store the event handler in the hash table
	handlers[handler.$$guid] = handler;
	// assign a global event handler to do all the work
	element["on" + type] = handleEvent;
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	// delete the event handler from the hash table
	if (element.events && element.events[type]) {
		delete element.events[type][handler.$$guid];
	}
};

function handleEvent(event) {
	var returnValue = true;
	// grab the event object (IE uses a global event object)
	event = event || fixEvent(window.event);
	// get a reference to the hash table of event handlers
	var handlers = this.events[event.type];
	// execute each event handler
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};
// end from Dean Edwards
/**
 * Creates an Element for insertion into the DOM tree.
 * From http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
 *
 * @param element the element type to be created.
 *				e.g. ul (no angle brackets)
 **/
function createElement(element) {
	if (typeof document.createElementNS != 'undefined') {
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
	if (typeof document.createElement != 'undefined') {
		return document.createElement(element);
	}
	return false;
}

/**
 * "targ" is the element which caused this function to be called
 * from http://www.quirksmode.org/js/events_properties.html
 **/
function getEventTarget(e) {
	var targ;
	if (!e) {
		e = window.event;
	}
	if (e.target) {
		targ = e.target;
	} else if (e.srcElement) {
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) { // defeat Safari bug
		targ = targ.parentNode;
	}

	return targ;
}
/**
 * Created By Ken Janssen
 **/
function checkDate(date, format, separator) {
	arrEls = format.split(separator);
	arrDate = date.split(separator);
	for(i=0; i<	arrEls.length; i++) {
		if(arrEls[i].length != arrDate[i].length || isNaN(arrDate[i]))
			return false;
	}
	for(i=0; i<	arrEls.length; i++) {
		if(arrEls[i]=="yyyy") {
			if(parseInt(arrDate[i],10)<1000 || parseInt(arrDate[i],10)>3000) return false;
		}
		if(arrEls[i]=="mm") {
			if(parseInt(arrDate[i],10)<1 || parseInt(arrDate[i],10)>12) return false;
		}
		if(arrEls[i]=="dd") {
			if(parseInt(arrDate[i],10)<1 || parseInt(arrDate[i],10)>31) return false;
		}
	}
	return true;
}
function removeOptionsFromSelect(elSel) {
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
      elSel.remove(i);
      //elSel.options[i] = null;
  }
}
// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}
//select all checkboxes for element
function selectAllInCB(theCB, checkedTrueFalse) {
	if(!theCB) return false;
	if(theCB.length==undefined) theCB.checked = checkedTrueFalse;
	else {
		for(i=0; i<theCB.length; i++) {
			theCB[i].checked = checkedTrueFalse;
		}
	}
}

//select all elements in an html select
function selectAllInSelect(htmlSelect, blnSelected) {
	if (htmlSelect) {
		if (htmlSelect.length == undefined) {
			htmlSelect.selected = blnSelected;
		} else {
			for (var i = 0; i < htmlSelect.length; ++i) {
				htmlSelect[i].selected = blnSelected;
			}
		}
	}
}

//dialog code

function showModifyDialog() {
	dialog.render(document.body);
	dialog.show();
	dialog.center();
}
			
function getModifyDialog(validationForm) {
	var modifyDialog = new YAHOO.widget.Dialog("modify", { 
		effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.25}, 
		fixedcenter:false,
		modal:true,
		visible:false,
		draggable:true,
		postmethod:"form"}
	);
				
	var handleCancel = function() {
		this.cancel();
	}	
	var handleSave = function() {
		switch (validationForm) {
			case "abbreviation":
				errormsg = validate_abbreviationEditForm("modifyForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "coursegroup":
				errormsg = validate_coursegroupEditForm("modifyForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "contact":
				errormsg = validate_contactEditForm("modifyForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "education":
				errormsg = validate_educationEditForm("modifyForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "faculty":
				errormsg = validate_facultyEditForm("modifyForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "institution":
				errormsg = validate_institutionEditForm("modifyForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "investigationtype":
				errormsg = validate_investigationtypeEditForm("modifyForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "keyword":
				errormsg = validate_keywordEditForm("modifyForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "mediator":
				errormsg = validate_mediatorEditForm("modifyForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "organization":
				errormsg = validate_organizationEditForm("modifyForm");
				if (errormsg == "" || errormsg == null) {
					//TODO check if name exists
					//this.submit();
					checkOrganizationName_ModifyOrg(dialog.getData().orgName);
				} else {
					alert(errormsg);
				}
				break;
			case "responsible":
				errormsg = validate_responsibleEditForm("modifyForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "school":
				errormsg = validate_schoolEditForm("modifyForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
		}
	}
			
	var modifyButtons = [{text:"Wijzigen", handler:handleSave}, {text:"Annuleren", handler:handleCancel}];
			
	modifyDialog.cfg.queueProperty("buttons", modifyButtons);
	
	return modifyDialog;
}

//new dialog code

function showNewDialog() {
	newDialog.render(document.body);
	newDialog.show();
	newDialog.center();
}
			
function getNewDialog(validationForm) {
	var newDialog = new YAHOO.widget.Dialog("new", { 
		effect:{effect:YAHOO.widget.ContainerEffect.FADE, duration:0.25}, 
		fixedcenter:true,
		modal:true,
		visible:false,
		draggable:true,
		postmethod:"form"}
	);
				
	var handleCancel = function() {
		this.cancel();
	}	
	var handleSave = function() {
		switch (validationForm) {
			case "abbreviation":
				errormsg = validate_abbreviationEditForm("newForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "coursegroup":
				errormsg = validate_coursegroupEditForm("newForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "contact":
				errormsg = validate_contactNewForm("newForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "education":
				errormsg = validate_educationEditForm("newForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "faculty":
				errormsg = validate_facultyEditForm("newForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "institution":
				errormsg = validate_institutionEditForm("newForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "investigationtype":
				errormsg = validate_investigationtypeEditForm("newForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "keyword":
				errormsg = validate_keywordEditForm("newForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "mediator":
				errormsg = validate_mediatorEditForm("newForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "organization":
				errormsg = validate_organizationEditForm("newForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "responsible":
				errormsg = validate_responsibleEditForm("newForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
			case "school":
				errormsg = validate_schoolEditForm("newForm");
				if (errormsg == "" || errormsg == null) {
					this.submit();
				} else {
					alert(errormsg);
				}
				break;
		}
	}
			
	var newButtons = [{text:"Opslaan", handler:handleSave}, {text:"Annuleren", handler:handleCancel}];
			
	newDialog.cfg.queueProperty("buttons", newButtons);
	
	return newDialog;
}
function getXmlHttp() {
	var xmlHttp;
	try {
		xmlHttp = new XMLHttpRequest();
		} catch (e) {
			try {
 				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
 			} catch (e) {
 				try {
   					xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
   				} catch (e) {
   					alert("Your browser does not support AJAX!");
   					return false;
   				}
 			}
		}
		return xmlHttp;
}