/* default js scripts */

window.onload = init;

function init() {
	
	var arr = document.getElementsByTagName('a');
	for(i = 0; i < arr.length; i++)
		arr[i].onfocus = new Function("this.blur()");
	
	var arr = document.getElementsByTagName('input');
	for(i = 0; i < arr.length; i++)
		if(arr[i].className == 'button')
			arr[i].onfocus = new Function("this.blur()");
			
}

function setMenu() {
	var arr = document.getElementById("menu").getElementsByTagName("a");

	var blnFound = false;	
	for( i = 0; i < arr.length; i++ ) {
		if( arr[i].name == currLoc ) {
			arr[i].parentNode.className = "active";
			blnFound = true;
		} else {
			arr[i].parentNode.onmouseover = new Function("this.className = 'hover'");
			arr[i].parentNode.onmouseout = new Function("this.className = ''");
		}
	}
	
	if( !blnFound ) {
		arr[0].parentNode.className = "active";
		arr[0].parentNode.onmouseover = new Function("this.className = 'active'");
		arr[0].parentNode.onmouseout = new Function("this.className = 'active'");
	}
	
}

function redirect( strURL ) {
	window.location = strURL;
}

function selectOptionByValue(strValue, obj) {
	var i = 0
	while ( i < obj.options.length) {
		if(obj.options[i].value == strValue) {
			obj.selectedIndex = i;
			return;
		}
		i++;
	}
	obj.selectedIndex = 0;
}

function removeOptionByValue(strValue, obj) {
	var i = 0
	while ( i < obj.options.length) {
		if(obj.options[i].value == strValue) {
			obj.options[i] = null;
			return;
		}
		i++;
	}
}

function getkey(e) {
	if (window.event)
		return window.event.keyCode;
	else if (e)
		return e.which;
	else
		return null;
}

function validChars(e, goods) {
	var key, keychar;
	key = getkey(e);
	if (key == null) 
		return true;

	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	goods = goods.toLowerCase();
	if (goods.indexOf(keychar) != -1)
		return true;

	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		return true;
	
	return false;
}

function isDate(intDay, intMonth, intYear) {
	var daysInMonth = new Array( 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	if( intDay == ''  &&  intMonth == ''  &&  intYear == '')
		return false;
	
	if( isNaN(intDay) || isNaN(intMonth) || isNaN(intYear))
		return false;

	if( intMonth < 1 || intMonth > 12 )
		return false;
	
	if( intYear < 1753 || intYear > 2100)
		return( false);
	
	if( intMonth == 2 && intDay == 29)
	   if(!(intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0))) 
	   	return false;
	
	if( intDay < 1  ||  intDay > daysInMonth[intMonth - 1])
		return false;
	
	return true;
}

function isEmail(strEmail) {
	var AtSym    = strEmail.indexOf('@')
	var Period   = strEmail.lastIndexOf('.')
	var Space    = strEmail.indexOf(' ')
	var Length   = strEmail.length - 1
	
	if( AtSym < 1 || Period <= AtSym + 1 || Period == Length || Space != -1 )
	 return false;

	return true;
}

function isURL(strUrl) {

  if (strUrl.indexOf(" ") != -1)
    return false;

  if (strUrl.indexOf(".") == -1)
    return false;
  else if (strUrl.indexOf(".") == 0)
    return false;
  else if (strUrl.charAt(strUrl.length - 1) == ".")
    return false;

  if (strUrl.indexOf("/") != -1) {
    strUrl = strUrl.substring(0, strUrl.indexOf("/"));
    if (strUrl.charAt(strUrl.length - 1) == ".")
      return false;
  }

  if (strUrl.indexOf(":") != -1) {
    if (strUrl.indexOf(":") == (strUrl.length - 1))
      return false;
    else if (strUrl.charAt(strUrl.indexOf(":") + 1) == ".")
      return false;
    strUrl = strUrl.substring(0, strUrl.indexOf(":"));
    if (strUrl.charAt(strUrl.length - 1) == ".")
      return false;
  }

  return true;

}

function isIp(strIp) {
	ipArray = strIp.split('.');
	if(ipArray.length != 4) {
		alert("U dient een correct IP-adres op te geven (xxx.xxx.xxx.xxx)");
		return false;
	}
	for (var i = 0; i < ipArray.length; i++)
		if (ipArray[i] < 0 || ipArray[i] > 255 || ipArray[i] == '') {
			alert("U dient een correct IP-adres op te geven (xxx.xxx.xxx.xxx en xxx < 256)");
			return false;
		}
	return true;
}

function checkZip( strZip, strFormat){
    var good, pcPos, pcChar, fmtPos, fmtChar, strNew;

    /* Lege strFormat string? dan is alles goed */
    if( strFormat == null  ||  strFormat == '') return strZip;

    strNew = '';
    fmtPos = 0;
    pcPos  = 0;
    good   = true;
    while( fmtPos < strFormat.length  &&  pcPos <= strZip.length  &&  good) {
        fmtChar = strFormat.charAt( fmtPos);
        pcChar  = strZip.charAt( pcPos);
        copy = true;

        if( fmtChar == '9') {
            good = (pcChar >= '0' &&  pcChar <= '9');
        }
        else if( fmtChar == 'a' ||  fmtChar == 'A') {
            good = ((pcChar >= '0'  &&  pcChar <= '9')  ||  (pcChar >= 'a' &&  pcChar <= 'z')  ||  (pcChar >= 'A'  &&  pcChar <= 'Z'));
        }
        else if( fmtChar == '*') {          /* We hebben een ster, dus we zijn klaar, kopieer de rest van de strZip */
            strNew  += strZip.substring( pcPos, strZip.length);
            fmtPos += 1;
            pcPos   = strZip.length;
            copy = false;
        }
        else if( fmtChar != pcChar) {       /* We hebben niet het gewenst format character, dus voeg het toe aan de nieuwe */
            strNew  += fmtChar;
            fmtPos += 1;
            copy = false;
        }

        if( good  &&  copy) {
            strNew += pcChar;
            pcPos += 1;
            fmtPos += 1;
        }
    }

    good = (pcPos == strZip.length  &&  fmtPos == strFormat.length);
    return (good ? strNew : null);
} 

/* custom scripts */
function popUp( url ) {
	var objWindow = window.open(url, 'popup');
}

function smileyHelp() {
	var objWindow = window.open('/smileyhelp/', 'smileyhelp', 'height=600, width=250, status=yes, toolbar=no, menubar=no, location=no, scrollbars=yes');
}

function addSmiley( strCode ) {
	window.opener.addSmileyToText( strCode );
	window.close();	
}

function addSmileyToText( strCode ) {
	if( typeof document.addmessage != "undefined" && document.addmessage ) {
		var frm = document.addmessage;

		frm["message"].value = frm["message"].value + strCode;
	}
}

function checkAddMessageForm() {
	
	if( typeof document.addmessage != "undefined" && document.addmessage ) {
		var frm = document.addmessage;
		
		if( frm["name"].value == "" ) {
			alert("U dient uw naam op te geven.");
			frm["name"].focus();
		} else if( frm["email"].value != "" && ! isEmail(frm["email"].value) ) {
			alert("U dient een geldig e-mailadres op te geven.");	
			frm["email"].focus();
		} else if( frm["message"].value == "" ) {
			alert("U dient een bericht op te geven.");	
			frm["message"].focus();
		} else {
			frm["secret"].value = "blabla";
			frm.submit();
		}
	}
}
