﻿// Blank funcion, just to change de mouse icon on the course with chapters images
function openChapters() {}

// Open the course's window
// @param address The course's path
// @param w Course's width
// @param h Course's Height
// @param name Course window's name
function openCourse(address, w, h, name) {
	var param = "width=" + w + ", height=" + h + ", left=0, top=0, menubar=no, status=no, location=no, toolbar=no, scrollbars=no, resizable=yes";
	courseWindow = window.open(address, name, param);
	courseWindow.focus()
}

// Toggle the display option from one div
// @param wichLayer DOM element to toggle the display
function toggleDisplay( domElement ) {
	var elem, vis;
 	if( document.getElementById ) // this is the way the standards work
		elem = document.getElementById( domElement );
	else if( document.all ) // this is the way old msie versions work
    	elem = document.all[domElement];
	else if( document.layers ) // this is the way nn4 works
		elem = document.layers[domElement];

	vis = elem.style;
  	
	// if the style.display value is blank we try to figure it out here
	if(vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
    	vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
		
  vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}


// show all files on the course's directory 
function showUpload(folderCourse) {
	// reset the dialog content
	dojo.byId("uploadContent").innerHTML = "<p>Loading... Please wait.</p><p align='center'><img src='modules/EukleiaModule/images/loader.gif' /></p>";

	// update course's folder in user data form     
    document.forms.userData.folderCourse.value = folderCourse;
    
    dojo.xhrPost( {
    	// The following URL must match that used to test the server.
        url: "modules/EukleiaModule/extra.upload.php", 
        handleAs: "text",
        timeout: 5000, // Time in milliseconds
        form:"userData",

        // The LOAD function will be called on a successful response.
        load: function(response, ioArgs) { 
          dojo.byId("uploadContent").innerHTML = response;
          return response;
        },

        // The ERROR function will be called in an error case.
        error: function(response, ioArgs) {
          console.error("HTTP status code: ", ioArgs.xhr.status);
          return response;
          }
        });
  }


// show all files on the course's directory 
function showDownload(folderCourse) {
	// reset the dialog content
	dojo.byId("downloadContent").innerHTML = "<p>Loading... Please wait.</p><p align='center'><img src='modules/EukleiaModule/images/loader.gif' /></p>";

	// update course's folder in user data form     
    document.forms.userData.folderCourse.value = folderCourse;
    
    dojo.xhrPost( {
    	// The following URL must match that used to test the server.
        url: "modules/EukleiaModule/extra.download.php", 
        handleAs: "text",
        timeout: 5000, // Time in milliseconds
        form:"userData",

        // The LOAD function will be called on a successful response.
        load: function(response, ioArgs) { 
          dojo.byId("downloadContent").innerHTML = response;
          return response;
        },

        // The ERROR function will be called in an error case.
        error: function(response, ioArgs) {
          console.error("HTTP status code: ", ioArgs.xhr.status);
          return response;
          }
        });
  }

function basicFeedbackUpload() {
	toggleDisplay('uploadInfo');
	toggleDisplay('uploadContent');
	document.forms[3].submit();
}

// ============ DUAL LIST REGISTRATION SCRIPT ================

function compareOptionValues(a, b)  { 
	// Radix 10: for numeric values
	// Radix 36: for alphanumeric values
	var sA = parseInt( a.value, 36 );  
	var sB = parseInt( b.value, 36 );  
	
	return sA - sB;
}

// compare two options within a list by TEXT
function compareOptionText(a, b) { 
  // Radix 10: for numeric values
  // Radix 36: for alphanumeric values
  var sA = parseInt( a.text, 36 );  
  var sB = parseInt( b.text, 36 );  

  return sA - sB;
}

function moveDualList(srcList, destList, moveAll) {
	// Do nothing if nothing is selected
  	if ((srcList.selectedIndex == -1) && (moveAll == false )) {
  		return;
  	}

	newDestList = new Array( destList.options.length );
	var len = 0;
	
	for(len = 0; len < destList.options.length; len++) {
		if (destList.options[len] != null) {
			newDestList[len] = new Option(destList.options[len].text, destList.options[len].value, destList.options[len].defaultSelected, destList.options[len].selected);
		}
	}
	
	for(var i = 0; i < srcList.options.length; i++) { 
        // Statements to perform if option is selected
		if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
        	// Incorporate into new list
        	newDestList[len] = new Option(srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected);
			len++;
		}
	}
	
	// Sort out the new destination list
	
	newDestList.sort( compareOptionValues );   // BY VALUES
	// newDestList.sort( compareOptionText );   // BY TEXT
	
	// Populate the destination with the items from the new array
	for (var j = 0; j < newDestList.length; j++) {
		if (newDestList[j] != null) {
			destList.options[ j ] = newDestList[ j ];
		}
	}
	
	// Erase source list selected elements
	
	for (var i = srcList.options.length - 1; i >= 0; i--) { 
		if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)) {
			// Erase Source
			//srcList.options[i].value = "";
			//srcList.options[i].text  = "";
			srcList.options[i] = null;
		}
	}
}


// === registration validate ===

function validateRegistration(delim, selStr, hidStr) {
	// place in hidden to get all selected courses
	var selObj = document.getElementById(selStr);
	var hideObj = document.getElementById(hidStr);
	var error = "";
	
	hideObj.value = '';
	for (var i=0; i<selObj.options.length; i++) {
		hideObj.value = hideObj.value == '' ? selObj.options[i].value : hideObj.value + delim + selObj.options[i].value;
	}	

	if (document.registrationForm.firstName.value.length == 0)
		error += "<div class='error'>First Name is compulsory</div>";

	if (document.registrationForm.surname.value.length == 0)
		error += "<div class='error'>Surname is compulsory</div>";

	if (document.registrationForm.company.value.length == 0)
		error += "<div class='error'>Company is compulsory</div>";

	if (document.registrationForm.email.value.length == 0)
		error += "<div class='error'>E-mail is compulsory</div>";

	if (document.registrationForm.telephone.value.length == 0)
		error += "<div class='error'>Telephone is compulsory</div>";

	if (document.registrationForm.acceptTerms.checked == false)
		error += "<div class='error'>You need to accept the terms and conditions</div>";

	if (document.registrationForm.hiddenSelected.value.length == 0)
		error += "<div class='error'>You need to choose at least one course</div>";

	if (!(error == "")) {
		var messageDiv = document.getElementById("message");
		messageDiv.innerHTML = error;
		messageDiv.style.display = "block";
		return false;
	} else
		return true;
}


// === add course validation ===


function validateCourse(){
	var courseWidth = document.getElementById("m1_edtCourseWidth");
	var courseHeight = document.getElementById("m1_edtCourseHeight");
	alert("test");
	//if (courseWidth == '') || (courseWidth == 0)
}
