/*--------------------------------------------------------------------
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
--------------------------------------------------------------------*/

$(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()");
			
	//FormElements.init();

	$(".popupwindow").each( function(index) {
		var myprofiles = {
			fullfront: {
				height:768,
				width:1024,
				toolbar:1,
				scrollbars:1,
				status:1,
				resizable:1,
				left:0,
				top:0,
				center:1,
				createnew:0,
				location:1,
				menubar:1
			}
		};
		$(this).popupwindow(myprofiles);
	});
	
	$('tr.hover').hover(
		function() {
			$(this).css('background-color', '#d9e4ef');
		}, 
		function() {
			$(this).css('background-color', '#fff');
		}
	);

    $('img.btnhover').hover(
    	function() {
			$(this).attr('src', $(this).attr('src').replace('.jpg', '-hover.jpg'));
    	},
    	function() {
			$(this).attr('src', $(this).attr('src').replace('-hover.jpg', '.jpg'));
    	}
    );

});

function standen_initCallback(carousel) {

    $('#id_standen_heren').bind('click', function() {
        carousel.scroll(1, true);
        carousel.startAuto(0);
    });

    $('#id_standen_dames').bind('click', function() {
        carousel.scroll(2, true);
        carousel.startAuto(0);
    });

    $('#id_standen_recreanten').bind('click', function() {
        carousel.scroll(3, true);
        carousel.startAuto(0);
    });

    $('#id_standen_jeugd').bind('click', function() {
        carousel.scroll(4, true);
        carousel.startAuto(0);
    });

    $('#id_standen_minis').bind('click', function() {
        carousel.scroll(5, true);
        carousel.startAuto(0);
    });

	var maxHeight = 0
	$('.teams').each(function() {
		maxHeight = maxHeight < $(this).height() ? $(this).height() : maxHeight;
	});
	
	$('.jcarousel-item').css('height', $.sprintf('%dpx', maxHeight));

};

/* Functions for displaying banners
--------------------------------------------------------------------*/
var oBanners = new Array(); /* Array with all the banners on the page */
var oCurr = new Array(); /* Array with all the banners currently displayed on the page */

function banners(data) {
	oBanners[data.bannerid] = data;
	Refresh(data.bannerid, data.timeout);
}

function RandomID( total, bannerid) {
	var retval = Math.floor( Math.random() * total );

	if( total > 1 )
		while( typeof(oBanners[bannerid].data[retval]) == 'undefined' || oCurr[bannerid] == oBanners[bannerid].data[retval].itemid )
			retval = Math.floor( Math.random() * total );
	
	return retval;
}

function Refresh( bannerid, timeout ) {
	setBanner(bannerid)
	setTimeout($.sprintf('Refresh(%d, %d);', bannerid, timeout), timeout);
}

function setBanner( bannerid ) {
	var i = RandomID(oBanners[bannerid].data.length, bannerid);
	var obj = oBanners[bannerid].data[i];

	$($.sprintf('#id_banner_%d', bannerid)).data('url', obj.link);
	$($.sprintf('#id_banner_%d', bannerid)).click(function() {
		if( $(this).data('url').indexOf('http://') >= 0 )
			window.open($(this).data('url'));
		else
			window.location = $(this).data('url');
	});

	$('img', $.sprintf('#id_banner_%d', bannerid)).ImageSwitch({
			Type:'FadeIn', 
			NewImage: $.sprintf('%scontent/banners/%d/banner_%s', siteurl, bannerid, obj.source), 
			EffectOriginal: false 
		}); 

	oCurr[bannerid] = obj.itemid;
}

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

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').val('7552');
	$('#id_check1').val('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 = '';
}

