/*
// handles right side window tabs.
var currTabSelection;

// changes the selection of the current tab.
function changeTabSelection(newTab)
{
	if (currTabSelection == undefined)
        {
            getCurrentTabSelection();
        }
        if (currTabSelection != newTab)
	{
		document.getElementById("tab_"+newTab).className = "tabselect";
		document.getElementById("tab_"+currTabSelection).className = "tabdeselect";

                document.getElementById("tabcontent_"+newTab+"_div").style.visibility = "visible";
                document.getElementById("tabcontent_"+currTabSelection+"_div").style.visibility = "hidden";

                // if the new opened tab has an iframe, refresh that iframe
                if (document.getElementById("tabcontent_"+i) != null)
                {
                    document.getElementById("tabcontent_"+i).location.reload();
                }
                currTabSelection = newTab;
	}
}

// find the currently selected tab.
function getCurrentTabSelection()
{
    // we don't search any further than 10 tabs
    for (i = 0;i < 10; i++)
    {
        // check if the tab exists, if not, we're done searching because we probably reached the last tab.
        if (document.getElementById("tab_"+i) != null)
        {
            // if the found tab is selected, it must have 'tabselect' as classname
            if (document.getElementById("tab_"+i).className == 'tabselect')
            {
                currTabSelection = i;
                i = 11;
            }
        }
        else
        {
            i = 11;
        }
    }
}
*/
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
 }
 MM_reloadPage(true);

 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_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
   for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
     if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
     obj.visibility=v; }
 }

 function selectCell(thisRef) {
 	var thisRow = thisRef.parentNode.parentNode;
 	var selectCell = thisRef.parentNode;
 	for (var i=0; i<thisRow.cells.length; i++) {
		var thisCell = thisRow.cells[i];
		var aTag = thisCell.getElementsByTagName('a')[0];

		if(aTag && aTag.className.indexOf("submenu") != -1) {
			if(thisCell == selectCell)
				aTag.className = "submenuselect";
			else
				aTag.className = "submenu";
		}
 	}
 }

/*
 * this function changes tabs view. Selects thisRef and sets the other as
 * unselected. A bit more complicated because there could be on ORANGE tab in
 * some pecial cases and this has to be handled. All ORANGE styles are prepended
 * by 'orange_' so if there is 'orange' in the original style, prepend it again!
 */
 function selectCellOverlayTabs(thisRef) {
 	var thisRow = thisRef.parentNode.parentNode;
 	var selectCell = thisRef.parentNode;

	var prevCell = null;
	var prevprevCell = null;
	var thisCell = null;

	for (var i=0; i<thisRow.cells.length; i++) {
		prevprevCell = prevCell;
		prevCell = thisCell;
		thisCell = thisRow.cells[i];

		/* do not change class of the filler cells */
		if (thisCell.className.indexOf("filler") == -1) {
			thisCell.style.borderBottomColor = "#666666";
			if (thisCell.className != "new_assignments_menu") {
				if(thisCell.className.indexOf("orange") != -1)
					thisCell.className = "orange_";
				else
					thisCell.className = "";
				if(thisCell == selectCell)
					thisCell.className += "new_assignments_menu_selected";
				else
					thisCell.className += "new_assignments_menu_not_selected";
			}
		}

		if (prevCell)
			var img = prevCell.getElementsByTagName('img')[0];

		if (img && img.src.indexOf('_orange') == -1) { 
			if (img.src.indexOf('right') != -1) { 
				if (prevprevCell == selectCell) 
					img.src = "images/new_student_gui/tab_right_white.png";
				else
					img.src = "images/new_student_gui/tab_right_gray.png";
			}
			else if (thisCell == selectCell) {
				if (img.src.indexOf('left') != -1)
					img.src = 'images/new_student_gui/tab_left_white.png';
				else if (img.src.indexOf('middle') != -1)
					img.src = 'images/new_student_gui/tab_middle_gray_white.png';
			}
			else {
				if (img.src.indexOf('left') != -1)
					img.src = 'images/new_student_gui/tab_left_gray.png';
				else if (img.src.indexOf('middle') != -1 &&
					prevprevCell &&
					prevprevCell == selectCell)
					img.src = 'images/new_student_gui/tab_middle_white_gray.png';
				else if (img.src.indexOf('middle') != -1)
					img.src = 'images/new_student_gui/tab_middle_gray_gray.png';
			}
		}

		var aTag = thisCell.getElementsByTagName('a')[0];
		if(aTag && aTag.className.indexOf("submenu") != -1) {
			if(aTag.className.indexOf("orange") != -1)
				aTag.className = "orange_";
			else
				aTag.className = "";
			if(thisCell == selectCell)
				aTag.className += "submenuselect";
			else
				aTag.className += "submenu";
		}
	}

	var img = thisCell.getElementsByTagName('img')[0];
	if (img && img.src.indexOf("orange") == -1) {
		if (prevCell == selectCell)
			img.src = "images/new_student_gui/tab_right_white.png";
		else
			img.src = "images/new_student_gui/tab_right_gray.png";
	}
 }

 img_tmp1 = new Image();
 img_tmp1.src = 'images/new_student_gui/orange_tab_left_gray.png';
 img_tmp2 = new Image();
 img_tmp2.src = 'images/new_student_gui/orange_tab_middle_gray_gray.png';
 img_tmp3 = new Image();

 function selectOrangeCellOverlayTabs(thisRef) {
 	var thisRow = thisRef.parentNode.parentNode;
 	var selectCell = thisRef.parentNode;
	var thisCell = null;
	var i;

	for (i=0; i < thisRow.cells.length - 3; i++) {
		thisCell = thisRow.cells[i];

		thisCell.style.borderBottomColor = "#ff9900";
		if (thisCell.className != "new_assignments_menu") {
			if(thisCell == selectCell)
				thisCell.className = "new_assignments_menu_selected";
			else {
				thisCell.className = "new_assignments_menu_not_selected";
			}
		}

		var img = thisCell.getElementsByTagName('img')[0];

		if (img) {
			if (img.src.indexOf('left') != -1)
				img.src = 'images/new_student_gui/orange_tab_left_gray.png';
			else if (img.src.indexOf('middle') != -1)
				img.src = 'images/new_student_gui/orange_tab_middle_gray_gray.png';
			else if (img.src.indexOf('right') != -1)
				img.src = "images/new_student_gui/orange_tab_right_gray.png";
		}
                

		var aTag = thisCell.getElementsByTagName('a')[0];
		if(aTag && aTag.className.indexOf("submenu") != -1) {
			if(thisCell == selectCell)
				aTag.className = "submenuselect";
			else
				aTag.className = "submenu";
		}
	}

	for (i=0; i < thisRow.cells.length; i++) 
            thisRow.cells[i].style.borderBottomColor = "#ff9900";
                
	selectCell.className = "orange_new_assignments_menu_selected";
        
 }

 function changeTabSelection(newTab)
 {
 	if (currTabSelection != newTab)
 	{
 		document.getElementById(newTab).className = "tabselect";
 		document.getElementById(currTabSelection).className = "tabdeselect";
 		currTabSelection = newTab;
 	}
 }

 function openPopupWindow(url, windowName, width, height)
 {
    window.open(url,windowName ,'screenX=1,left=1,screenY=1,top=1,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,copyhistory=0,width='+width+',height='+height);
 }

 function isFieldEmpty(fieldId, formId)
{
	formObject = document.getElementById(formId);
        fieldTxt = formObject.elements[fieldId].value;

	fieldTxt = fieldTxt.replace(/ /g, "");
	if (fieldTxt == "")
	{
		return true;
	}
	else
	{
		return false;
	}
}

// expects dd-mm-yyyy

function isStringDate(dateStr)
{
        var datePat = /^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/;
        var matchArray = dateStr.match(datePat); // is the format ok?

        if (matchArray == null)
        {
            return false;
        }

        month = matchArray[3]; // parse date into variables
        day = matchArray[5];
        year = matchArray[1];
        //alert(day+", "+month+", "+year);
        return isDate(day, month, year);
}

function isDate(day, month, year)
{
        if (month < 1 || month > 12)
        {
                return false;
        }

        if (day < 1 || day > 31)
        {
                return false;
        }

        if ((month==4 || month==6 || month==9 || month==11) && day==31)
        {
                return false;
        }

        if (month == 2) // februari check
        {
                var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
                if (day > 29 || (day==29 && !isleap))
                {
                        return false;
                }
        }

        return true; // date is ok
}

function generateDateStringFromDateElement(elementId)
{
    day = document.getElementById(elementId+"_day").value;
    month = document.getElementById(elementId+"_month").value;
    year = document.getElementById(elementId+"_year").value;

    document.getElementById(elementId).value = generateDateString(day,month,year);
}

function generateDateString(day, month, year)
{
    return year+"-"+month+"-"+day;
}

function openVideoCatalog(urlParams)
{
       if (!urlParams) { var urlParams = ''; }
	window.open('?section=videocatalogus'+urlParams, 'VideoCatalog', 'left=100, top=100, width=750, height=400, resizable=no, toolbar=no, scrollbars=no, menubar=no')
}

function openReferenceCatalog(urlParams)
{
       if (!urlParams) { var urlParams = ''; }
	window.open('?section=bronnencatalogus'+urlParams, 'ReferenceCatalog', 'width=750, height=400, resizable=no, toolbar=no, scrollbars=no, menubar=no')
}

function openModuleCatalog()
{
        window.open('?section=modulecatalogue', 'ModuleCatalog', 'width=750, height=400, resizable=no, toolbar=no, scrollbars=no, menubar=no');
}

function setFocus(id) {
    document.getElementById(id).focus();
}

// Converts time from milliseconds to a nice display string
function convertTime(msec) 
{
	if ( msec != null )
	{
        hour = Math.floor( msec/3600000 );
        msec -= hour*3600000;
        min = Math.floor( msec/60000 );
        msec -= min*60000;
        sec = Math.floor( msec/1000 );
		if (hour< 10) { h_ext = "0"; } else { h_ext = ""; }
		if (min < 10) {	m_ext = "0"; } else { m_ext = ""; }
		if (sec < 10) {	s_ext = "0"; } else { s_ext = ""; }
        if ( hour > 0 ) {
		// Three lines below are used to create the 01:07:09 time layout
			if (hour< 10) { h_ext = "0"; } else { h_ext = ""; }
			if (min < 10) {	m_ext = "0"; } else { m_ext = ""; }
			if (sec < 10) {	s_ext = "0"; } else { s_ext = ""; }
            return h_ext+hour+":"+m_ext+min+":"+s_ext+sec;
		}
        if ( min > 0 ){
			return "00:"+m_ext+min+":"+s_ext+sec;
	 	}
		return "00:00:"+s_ext+sec;
	}
	else return 0;
}

function pasteTime(targetSrcId)
{
	document.getElementById(targetSrcId).value = document.getElementById("videoTime").value;
}

function pasteTimeMs(targetSrcIdMs)
{
	document.getElementById(targetSrcIdMs).value = document.getElementById("videoTimeMs").value;
}

function convertTimeToMs(tag)
{
	var hours ='';
	var minutes = '';
	var seconds = '';
    
    var colon = 0;
	
	input = tag.value; // Should be string of type hh:mm:ss
	for (i = 1; i <= input.length; i++)
	{
        var c = input.substr(input.length-i, 1);
        if (c >= '0' && c <= '9')
        {
			//alert("Number: " + c);
            if (colon == 0) seconds = c + '' + seconds;
            else if (colon == 1) minutes = c + '' + minutes;
            else if (colon == 2) hours = c + '' + hours;
			else if (colon > 2) alert("Geen goede tijd. Gebruik uu:mm:ss formaat. De minuten en/of uren weglaten mag.");
        }
        else 
        {
			//alert("Colon: " + c);
            colon++;
        }
	}
    //alert("Seconds: " + seconds + "\nMinutes: " + minutes + "\nHours: " + hours);
    var toomanyseconds = Math.floor(seconds / 60);
    seconds -= (toomanyseconds * 60);
    minutes = new Number(toomanyseconds) + new Number(minutes); 
	//alert(toomanyseconds);
    //alert(minutes);
    var toomanyminutes = Math.floor(minutes / 60);
    minutes -= (toomanyminutes * 60);
    hours = new Number(toomanyminutes) + new Number(hours); 
	
	//if (seconds == '') alert("XGeen goede tijd. Gebruik uu:mm:ss formaat. De minuten en/of uren weglaten mag.\nBijvoorbeeld:\n2:23 of 45");
	
	var totalMs = 0;
	if (hours != '') totalMs += hours * 60 * 60 * 1000;
	if (minutes != '') totalMs += minutes * 60 * 1000;
	if (seconds != '') totalMs += seconds * 1000;
	
	playerframe.contentWindow.markedTime = totalMs;
	alert(totalMs);
	
	var tagId = tag.id;
	var IdNumber = tagId.substr(tagId.length - 1, 1);
	//alert(IdNumber);
	document.getElementById("pasteTimeMsField_"+IdNumber).value = totalMs;
	//pasteTimeMs("pasteTimeMs_"+IdNumber);
	//alert("pasteTimeMs"+IdNumber);
	//alert("Value: " + document.getElementById("pasteTimeMs"+IdNumber).value);
	
	
}

function openDividossier(moduleId) {
	window.open('?section=dividossier_assignment&moduleId='+moduleId, 'Dividossier', 'width=600, height=600, resizable=no, toolbar=no, scrollbars=yes, menubar=no');
}
