var actieveHoofdItem=0;
var actieveSubItem=0;
var radioPlayerWindow="undefined";
var videoPlayerWindowD4L="undefined";

//consts for menu array index
var MENU_URL=0;
var MENU_ID=1;
var MENU_PARENT_ID=2;
var menu=new Array();

/**
 * Download counter
 */
function dc(sender) {
	if (sender.href) {
		var img=new Image;
		img.src="/scripts/downloadcounter.php?doc="+sender.href;
	}	
}	

function videoPlayer(url,title) {
  videoPlayerWindow=window.open("/videoplayer.php?fragment="+url+"&title="+title,'popup_videoplayer','width=600,height=520,scrollbars=no,status=no,menubar=no,location=no,resizable=yes,titlebar=no');
  videoPlayerWindow.focus();
}	

function audioPlayer(url,title) {
	radioPlayerWindow=window.open("/audioplayer.php?fragment="+url+"&title="+title,'popup_audioplayer','width=280,height=70,scrollbars=no,status=no,menubar=no,location=no,resizable=no,titlebar=no');
  radioPlayerWindow.focus();
}	


function initMenu(currURL)
{
  for (var i=0;i<menu.length;i++)
  {
    if (menu[i][MENU_URL]==currURL)
    {
      //Is dit menu-item een submenuitem of een hoofdmenu item
      if (menu[i][MENU_PARENT_ID]==0)
      {
        //Hoofdmenu-item
        activateMainMenu(menu[i][MENU_ID]);
      }
      else
      {
        //Submenu-item 
        activateSubMenu(menu[i][MENU_ID],menu[i][MENU_PARENT_ID]);
      }  
      break;
    }  
  }  
}  

function closeOpenMenus()
{
  for (var i=0;i<menu.length;i++)
  {
     if (menu[i][MENU_URL]=="")
     {
       document.getElementById("menuopen_"+menu[i][MENU_ID]).className="closedSubMenu";
     } 
  }  
}

function activateMainMenu(id)
{
  document.getElementById("mainmenu_"+id).className="menuSelected";
  actieveHoofdItem=id;
  actieveSubItem=0;
}

function activateSubMenu(sub_id,parent_id)
{
  document.getElementById("submenu_"+sub_id).className="menuSubSelected";       //geef het submenuitem de goede stijl
	document.getElementById("menuopen_"+parent_id).className="openedSubMenu";     //open de parent-div
  document.getElementById("mainmenu_"+parent_id).className="menuOpenSelected";  //geef het parent-item de goede stijl
  actieveSubItem=sub_id;
}

function toggleOpenMenu(id)
{
  var c=document.getElementById("menuopen_"+id).className;

  if (c=="closedSubMenu")
  {
    //sluit evt. geopend menu
    closeOpenMenus();
    document.getElementById("menuopen_"+id).className="openedSubMenu";
  }  
  else
  {
    document.getElementById("menuopen_"+id).className="closedSubMenu";
  } 
}
    
function hoverSubMenu(id)
{
  if (id!=actieveSubItem)
  {
    document.getElementById("submenu_"+id).className="menuSubHover";
  }  
}
  
function hoverMainMenu(id)
{
  if (id!=actieveHoofdItem)
  {
    document.getElementById("mainmenu_"+id).className="menuHover";
  }  
}  

function dimMainMenu(id)
{
  if (id!=actieveHoofdItem)
  {
    document.getElementById("mainmenu_"+id).className="menu";
  }  
}  

function dimSubMenu(id)
{
  if (id!=actieveSubItem)
  {
    document.getElementById("submenu_"+id).className="menuSub";
  }  
}  

function hoverMenuOpen(id)
{
  if (id!=actieveHoofdItem)
  {
    document.getElementById("mainmenu_"+id).className="menuOpenHover";
  }
}  

function getParentId(sub_id)
{
  for (var i=0;i<menu.length;i++)
  {
    if (menu[i][MENU_ID]==sub_id)
    {
      return menu[i][MENU_PARENT_ID]
    }
  }  
  
  return 0;
}  

function dimMenuOpen(id)
{
  if (id!=actieveHoofdItem)
  {
    //hier checken of we terug moeten naar menuOpen of naar menuOpenSelected
    if (actieveSubItem==0 || getParentId(actieveSubItem)!=id)
    {
      document.getElementById("mainmenu_"+id).className="menuOpen";
    }    
    else
    {
      document.getElementById("mainmenu_"+id).className="menuOpenSelected";  //geef het parent-item de goede stijl
    }    
  }
}  


function loadPage(aurl)
{
  document.location.href=aurl;
}

//Pass a form element (not just the name) and a regular expression pattern
function validateField(formElem,pattern) 
{
  var re = new RegExp(pattern);
  var fv = formElem.value;

  return re.test(fv);
}

//Pass a value and a regular expression pattern
function validateValue(fv,pattern) 
{
  var re = new RegExp(pattern);

  return re.test(fv);
}

function getPatternByName(patternName)
{
  switch (patternName) {
  	case "notempty":
      return "^.";
      break;
    case "email":
      return "^[0-9a-zA-Z][-_0-9a-zA-Z.]*@[-_0-9a-zA-Z.]+[.][a-zA-Z]+$";
      break;
  	case "pagename":
      return "^[_a-z0-9]{1,50}$";
      break;
  	case "authenticate":
      return "^[a-zA-Z0-9]{5,16}$";
      break;
   	case "nldate":
      return "^[0-3]?[0-9][-][01]?[0-9][-][12][0-9]{3}$";
      break;
   	case "date":
      return "^[12][0-9]{3}[-][01]?[0-9]-[0-3]?[0-9]$";
      break;
  	case "integer":
      return "^[+-]?[0-9]+$";
      break;
  	case "posinteger":
      return "^[0-9]+$";
      break;
  	case "float":
      return "^[+-]?[0-9]+([.][0-9]+)?$";
      break;
  	case "nlfloat":
      return "^[+-]?[0-9]+([,][0-9]+)?$";
      break;    	
  	case "symbol":
      return "^[a-zA-Z0-9]{3,}$";
      break;
  	case "aspfile":
      return "^[a-zA-Z0-9_]{3,}\.asp$";
      break;
  	case "name":
      return "^[a-zA-Z0-9 ]{3,}$";
      break;
  	case "digit8":
      return "^[0-9]{8}$";
      break;
  	default: 
   		alert("Unknown validator added to a form element: '" + patternName+"'");
	} 	   
}
	
function validateForm(aform)
{
	var s="";

	//check if form has validator arr
	if (aform.validatorArr)
	{
		//Iterate over Form elements
		for (var i=0;i<aform.validatorArr.length;i++)
		{
			eval("var anelem=document.forms."+aform.name+"."+aform.validatorArr[i][1]);
		
			var pattern=getPatternByName(aform.validatorArr[i][2]);
			
			if (aform.validatorArr[i][4]=="radio")
			{
				var fv="";

				eval("var chkArray=aform."+aform.validatorArr[i][1]+";");
				
				for (j=0;j<chkArray.length;j++)
				{
					if (chkArray[j].checked)
					{
						fv=chkArray[j].value;
					}
				}

				if (validateValue(fv,pattern) == false)
				{
					s+=aform.validatorArr[i][3]+"\n";
				}	
				
			}
			else
			{	
				if (validateField(anelem,pattern) == false)
				{
					s+=aform.validatorArr[i][3]+"\n";
				}	
			}	
		}	
		
		//Report errors
		if (s!="")
		{
			alert(s);
			return false;
		}	

	}	
	
	return true;
}
	
//addValidator("mainform","naam","notempty","Uw naam mag niet leeg zijn.");
function addValidator(aformname,aformelement,avalidator,anerrormsg)
{

	eval("var anelem=document.forms."+aformname+"."+aformelement);

	//Radiogroup or a single element
	if (anelem[0])
	{
		atype=anelem[0].type;
	}
	else
	{
		atype=anelem.type;
	}	
	
	//check if validatorArr has been added to the form
	if (!document.forms[aformname].validatorArr)
	{
		document.forms[aformname].validatorArr= new Array();	
	}	
	
	//add new array to a new last position in the array
	document.forms[aformname].validatorArr[document.forms[aformname].validatorArr.length]=
		new Array(aformname,aformelement,avalidator,anerrormsg,atype);
}

function zoeken()
{
  document.forms["search_form"].submit();
}

function openRadio(ver)
{
  if (radioPlayerWindow=="undefined")
  {
    radioPlayerWindow=window.open("/radio"+ver,'dance4life_radio','left=0,top=0,width=244,height=330,scrollbars=no,status=no,menubar=no,location=no,resizable=no,titlebar=no');
  }

  radioPlayerWindow.focus();
      
}  

function radioPlayer()
{
  openRadio('');
}
  
function fotoalbum(aurl)
{
  var w=window.open(aurl,'fotoalbum','left=0,top=0,width=698,height=555,scrollbars=yes,status=no,menubar=no,location=no,resizable=no,titlebar=no');
  w.focus();
}

function videoPlayerD4L(aurl)
{
  videoPlayerWindowD4L=window.open(aurl,'d4l_video','width=375,height=375,scrollbars=no,status=no,menubar=no,location=no,resizable=no,titlebar=no');
  videoPlayerWindowD4L.focus();
}  

function videoPlayerD2IS(aurl)
{
  videoPlayerWindowD4L=window.open(aurl,'d4l_video','toolbar=no,location=no,status=no,menubar=no,scrollbars=auto,resizable=yes,width=590,height=575');
  videoPlayerWindowD4L.focus();
}  

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
 
function startTimer(teller) {
	if($("time")) {
		$("time").innerHTML = teller;
	}
	teller++;
	setTimeout("startTimer("+teller+")",12000); // 12000 msecs
}

function getSelectedRadio(aradioName)
{
	var chkArray = MM_findObj(aradioName);
	
	//exception for a single radiobutton
	if ((chkArray.value != null) && (chkArray.checked))
	{
		return chkArray.value;
	}
	
	for (i=0;i<chkArray.length;i++)
	{
		if (chkArray[i].checked)
		{
			return chkArray[i].value;
		}
	}

	return -1;
}

function groupShow(groep)
{
            hideOtherGroups("groep"+groep);
            currgroep=groep;
 
            //toggle clicked div
            var div_vragen=document.getElementById("groep"+groep);
            div_vragen.style.display=(div_vragen.style.display=='none')?div_vragen.style.display='block':div_vragen.style.display='none';
}
 
function hideOtherGroups(groep)
{
            divs = document.getElementsByTagName("div");
            for (var i=0; i<divs.length; i++) 
            {
              if (divs[i].id!=groep && divs[i].id.substr(0, 5)=="groep" && divs[i].style.display!="none")
              {
                        divs[i].style.display="none";
                        }           
            }
}
