/**
 * Functie om info naar de Firebug Console te loggen
 **/
var enableLogging = "debug"; //'debug', 'off'
function writeLogEntry(level, msg) {
	try{
		if(enableLogging=="debug" && level=="debug") {
			console.debug("[DEBUG] " + msg);
		}
		if(level=="error") {
			console.error(msg);
		}
	} catch(e) { /*ignore errors*/}
}
/**
 * 	
 **/
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);
    }
}
/**
 * checks a string for a date format
 **/
function checkDateFormat(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;
}
/**
 * checks is given parameters compose a valid date
 **/
function isValidDate(Day,Mn,Yr){
    var DateVal = Mn + "/" + Day + "/" + Yr;
    var dt = new Date(DateVal);
	//check
    if(dt.getDate()!=Day)
        return(false);
    else if(dt.getMonth()!=Mn-1)
        return(false);
    else if(dt.getFullYear()!=Yr)
        return(false);
    //valid
    return(true);
 }

// 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;
		}
	}
}

//@RightBack equivalent
function rightBack(sourceStr, keyStr){
	arr = sourceStr.split(keyStr);
	return (sourceStr.indexOf(keyStr) == -1 | keyStr=='') ? '' : arr.pop()
}

//@LeftBack equivalent
function leftBack(sourceStr, keyStr){
	arr = sourceStr.split(keyStr)
	arr.pop();
	return (keyStr==null | keyStr=='') ? '' : arr.join(keyStr)
}
/**
	removes all <option>s of a <select>
**/
function removeOptionsFromSelect(elSel) {
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
      elSel.remove(i);
      //elSel.options[i] = null;
  }
}
//adds an option to a select box
function addOption(selectbox,text,value ) {
	var optn = document.createElement("option");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}

/**
 ** Toont een klein dialoog (wordt gebruikt bij pagina's die langer moeten laden)
 ** @param customMsg: optioneel, om een eigen tekst mee te geven naast de 'inladen' afbeelding
 **/
function showPleaseWait(extra, loadingImg) {
  	//aanmaken indien nodig
	var pleaseWaitDialog = dijit.byId("pleaseWaitDialog");
	if(!pleaseWaitDialog) {
		var titel = "Een ogenblik aub...";
		var pleaseWaitDialog = new dijit.Dialog({id:"pleaseWaitDialog", title: titel });
		dojo.body().appendChild(pleaseWaitDialog.domNode);
	}
	//inhoud zetten
	var dialogContent = '<img src="'+loadingImg+'" />';
	if(arguments[0]) dialogContent += " " + arguments[0];
	pleaseWaitDialog.setContent(dialogContent);
	//tonen
	pleaseWaitDialog.show();
}
