/*--------------------------------------------------------------------
Basic Javascript Sheet

Version: 1.0
Copyright: 2008, dicode® VOF - understanding internet
W: www.dicode.nl
E: info@dicode.nl
T: 0570-750680
--------------------------------------------------------------------*/

/* Custom Scripts
--------------------------------------------------------------------*/









/* Global
--------------------------------------------------------------------*/

$(document).ready(function() {
	
	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()");
			
//	$.ifixpng(siteurl + 'images/beheer/transparant.gif');
//	$('img[src$=png], #topimagehome, #search').ifixpng();
	//FormElements.init();
});

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);
}

/* function default checkFrm */
function checkForm( arr ) {
	
	var frm = document.getElementById('id_frm');
	
	for( i=0; i < arr.length; i++ ) {
		
		/* split key->value */
		tmp = arr[i].split("->");
		
		/* key: email */
		if( tmp[0] == 'email' && frm[tmp[0]].value != '' && !isEmail(frm[tmp[0]].value) ) {
			alert("Geef een geldig e-mailaddres op: `"+ tmp[1] + "`");	
			frm[tmp[0]].focus();
			return false;
		}
		
		/* key: default */
		if( frm[tmp[0]].value == '' ) {
			alert("Je hebt het veld `"+ tmp[1] + "` niet ingevuld.");	
			frm[tmp[0]].focus();
			return false;
		}
		
	}
	
	$('id_check').value = 7552;
	
	return true;
	
}

/* function default checkFrm */
function checkFormAlbum( arr ) {
	
	var frm = document.getElementById('id_frm_album');
	
	for( i=0; i < arr.length; i++ ) {
		
		/* split key->value */
		tmp = arr[i].split("->");
		
		/* key: email */
		if( tmp[0] == 'email' && frm[tmp[0]].value != '' && !isEmail(frm[tmp[0]].value) ) {
			alert("Geef een geldig e-mailaddres op: `"+ tmp[1] + "`");	
			frm[tmp[0]].focus();
			return false;
		}
		
		/* key: default */
		if( frm[tmp[0]].value == '' ) {
			alert("Je hebt het veld `"+ tmp[1] + "` niet ingevuld.");	
			frm[tmp[0]].focus();
			return false;
		}
		
	}
	
	$('id_check').value = 7552;
	
	return true;
	
}

/**
 * switchDiv
 *
 * Function for displaying or hiding an element.
 *
 * @param strId	The id of the element to hide or display
 */
function switchDiv( strDiv ) {
	if( $(strDiv).style.display == '')
		$(strDiv).style.display = 'none';
	else
		$(strDiv).style.display = '';
}
