function datumChk(vStr) {
    // date format (dd-mm-yyyy)
    var nYear  = vStr.substr(7,4);
    var nMonth = vStr.substr(4,2);
    var nDay   = vStr.substr(0,2);
    alert(nYear + '-' + nMonth + '-' + nDay);
}

function pcChk(form, Ctrl, cValue) {
	p = (Ctrl.value).toUpperCase()
	if( (p.length!=6) || (!(checkChar(p,0,'1','9') && checkChar(p,1,'0','9') && checkChar(p,2,'0','9') && checkChar(p,3,'0','9') && checkChar(p,4,'A','Z')&& checkChar(p,5,'A','Z'))) )
		{
		return (false);
	} else
		Ctrl.value = p
		return (true);
}

function ChkSofiNr(vObj) {
    vObj.value = StrTran(vObj.value, '.', '');
}

function CapFirst(frmObj) {
var index;
var tmpStr;
var tmpChar;
var preString;
var postString;
var strlen;
tmpStr = frmObj.value.toLowerCase();
strLen = tmpStr.length;
if (strLen > 0)  {
for (index = 0; index < strLen; index++)  {
if (index == 0)  {
tmpChar = tmpStr.substring(0,1).toUpperCase();
postString = tmpStr.substring(1,strLen);
tmpStr = tmpChar + postString;
}
else {
tmpChar = tmpStr.substring(index, index+1);
if (tmpChar == " " && index < (strLen-1))  {
tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
preString = tmpStr.substring(0, index+1);
postString = tmpStr.substring(index+2,strLen);
tmpStr = preString + tmpChar + postString;
         }
      }
   }
}
frmObj.value = tmpStr;
}

function CapFirstOne(Ctrl) {
	p = (Ctrl.value).substring(0,1)
	c = (Ctrl.value).substring(1)
	Ctrl.value = p.toUpperCase()+c.toLowerCase()
	return (true)
}

function CapAll(Ctrl) {
	Ctrl.value = Ctrl.value.toUpperCase()
	return (true)
}

function LowAll(Ctrl) {
	Ctrl.value = Ctrl.value.toLowerCase()
	return (true)
}

function checkChar(S,P,start,eind)
{
	return(!((S.charAt(P)<start) || (S.charAt(P)>eind)))
}

function ChkTel(form, Ctrl, cValue) {
	if(Ctrl.value!="")
		{
			p = (Ctrl.value)
			if( (p.length!=10) || (!(checkChar(p,0,'0','0') && checkChar(p,1,'0','9') && checkChar(p,2,'0','9') && checkChar(p,3,'0','9'))) )
			{
				return false
			}
		}
	return true
}

function ChkDate(form, Ctrl, cValue) {
	if(Ctrl.value!="")
		{
			p = (Ctrl.value)
			if( p.length!=10 )
			{
				return false
			}
		}
	return true
}

function VlVal(Ctrl) {
	// Zet punten tussen de letters en in hoofdletters
	if(Ctrl.value!="")
		{
			var cNew = ""
			p = StrTran(StrTran(Ctrl.value, ".", "")," ","")
			for ( var i = 0; i<p.length ; i++ ) {
				cNew = cNew + p.substring(i,i+1) + "."
			}
			Ctrl.value = cNew.toUpperCase()
	}
	return true
}

function StrTran(fullS,oldS,newS) {
	// Replaces oldS with newS in the string fullS   
	for (var i=0; i<fullS.length; i++) {      
		if (fullS.substring(i,i+oldS.length) == oldS) {         
			fullS = fullS.substring(0,i)+newS+fullS.substring(i+oldS.length,fullS.length)      
		}   
	}   
	return fullS
}

function DeleteLink(url,bconfirm) {
	if (bconfirm) {
		if (confirm('Weet u het zeker?')) {
			window.location.href = url;
		}
	}
	else {
		window.location.href = url;
	}
}


function getVoorletter(vSrc, vDest) {
    var f = vDest.form;
    var s = f.elements[vSrc].value.substr(0,1);
    if ((vDest.value.length <= 0) && (s.length > 0)) {
        vDest.value = s.toUpperCase() + '.';
    }
}

function setVoorletter(vSrc, vDest) {
    var f = vSrc.form;
    var s = vSrc.value.substr(0,1);
    if ((f.elements[vDest].value.length <= 0) && (s.length > 0)) {
        f.elements[vDest].value = s.toUpperCase() + '.';
    }
}

function ValidChars(vObj, vChars) {
    var s = vObj.value;
    for (i=0;i<s.length;i++) {
        if (vChars.indexOf(s[i])==-1) {
            return false;
        }
    }
    return true;
}
function CharOnly(vObj, vChars) {
    s = new String(vObj.value);
    for (i=0;i<s.length;i++) {
        if (vChars.indexOf(s.substr(i,1))==-1) {
            s = s.substr(0, i) + s.substr(i + 1);
            i = -1;
        }
    }
    vObj.value = s;
}



function validatepositivenumber(sValue)
{
  var objMatch = new RegExp("^[0-9]+$").exec(sValue);
  return objMatch;
}

function validatepassword(sValue)
{
  var objMatch = new RegExp("^[0-9A-Za-z]{5,20}$").exec(sValue);
  return objMatch;
}

function validatealphanumber(sValue)
{
  var objMatch = new RegExp("^[0-9A-Za-z .'-]+$").exec(sValue);
  return objMatch;
}

function validatenormalchars(sValue)
{
  var objMatch = new RegExp("^[A-Za-z .]+$").exec(sValue);
  return objMatch;
}

function validatephonenumber(sValue)
{
  var objMatch = new RegExp("^[0-9]+$").exec(sValue);
  return objMatch;
}

function validatezipcode(sValue)
{
  var objMatch = new RegExp("([0-9]{4})([A-Za-z]{2})").exec(sValue);
  return objMatch;
}

function validateemail(sValue)
{
  var objMatch = new RegExp("^[A-Za-z0-9_-]+([.][A-Za-z0-9_-]+){0,4}[@][A-Za-z0-9_-]+([.][A-Za-z0-9_-]+){1,3}$").exec(sValue);
  return objMatch;
}

function expandRadio (p_oSender) {
	var f = p_oSender.form;
	
	// collapse all
	for (var i=0; i<f.elements.length; i++) {
		if (f.elements[i].type=='radio' && f.elements[i].name==p_oSender.name) {
			document.getElementById('expand_' + f.elements[i].id).style.display = 'none';
		}
	}	
	
	// expand selected
	document.getElementById('expand_' + p_oSender.id).style.display = 'block';
}

var g_sFormId = 0;
var g_oForm;
function setForm(p_sElementId, p_bShow) {
	var oElement = document.getElementById(p_sElementId);
	oElement.style.display = 'none';
	if (p_bShow) {
		var f = document.getElementById(g_sFormId);
		f.x_act.value = 'insert';
		f.x_id.value = '';
		f.do_submit.value = 'Toevoegen';
		oElement.style.display = 'block';
	}
}

function getList (p_oSender, p_sDestinationName, p_lParams) {
	var f = p_oSender.form;
	var lst = '-1~- probleem bij ophalen lijst -';
	var req = new XMLHttpRequest();
	
	if (p_oSender.value!=-1) {
		p_oSender.disabled = true;
		f[p_sDestinationName].disabled = true;
		if (req) {
			req.onreadystatechange = function() {
				if (req.readyState == 4 && (req.status == 200 || req.status == 304)) {
					if (req.responseText.length) {
						lst = new String(req.responseText);
						lst.trim();
					}
					putList(f, p_sDestinationName, lst);
					p_oSender.disabled = false;
					f[p_sDestinationName].disabled = false;
				}
			};
			sUrl = '/xmlhttprequest/dwslist.cfm?form=' + g_sFormId
				+ '&type=' + p_sDestinationName 
				+ '&value=' + p_oSender.value 
				+ '&kmo=' + f.kmo.value
				;
			if (arguments.length>2) {
				sUrl += '&' + p_lParams;
			}
			req.open('GET', sUrl);
			req.send(null);
		}
	} else {
		f[p_sDestinationName].disabled = true;
	}
}

function putList (p_oFrm, p_sDestinationName, p_lOptions) {
	var f = p_oFrm;
	var a = p_lOptions.split('|');

	// delete all options from destination
	f[p_sDestinationName].options.length = 0;

	// fill it with new options
	var x=0;
	for (i=0;i<a.length;i++) {
		b = a[i].split('~');
		f[p_sDestinationName].options[x] = new Option(b[1],b[0]);
		x++;
	}
	f[p_sDestinationName].disabled = false;
}

function getOptionTxt(p_sSelect) {
	var f = g_oForm;
	var sOptionTxt = new String();
	var oSelect = f[p_sSelect];
	var nOption = oSelect.selectedIndex;
	var oOption = oSelect.options[nOption];
	var sOptionTxt = oOption.text;
	
	return sOptionTxt;
}


// Add trim methods to String class
// Removes leading whitespaces
String.prototype.lTrim = function () {
	var re = /\s*((\S+\s*)*)/;
	return this.replace(re, "$1");
}

// Removes ending whitespaces
String.prototype.rTrim = function() {
	var re = /((\s*\S+)*)\s*/;
	return this.replace(re, "$1");
}

// Removes leading and ending whitespaces
String.prototype.trim = function () {
	return this.lTrim(this.rTrim());
}

function storeValueLabel(p_oSender) {
	g_oForm = p_oSender.form;
	document.getElementById(p_oSender.id + '_label').value = getOptionTxt(p_oSender.name);
}
