﻿// Global var to store Xml
var xmlDoc;

function importCoursesXml() {
	new Ajax.Request("xml/Courses.xml", {
		method: 'get',
		onSuccess: function(transport) {
			xmlDoc = transport.responseXML;
			createTable(true);
		}
	});
}

function createTable(onLoad) {
    var i, j, pageLoad = onLoad, course = xmlDoc.getElementsByTagName('Course');
    
    // Delete all rows if not onload    
    if (!pageLoad) {
		var courseData = $('courseData').immediateDescendants();
		for (i = 0; i < courseData.length; i++) {     
			courseData[i].remove();
		}
    }

    // Are the checkboxes all checked?
	var checkBoxes = $("optionsArea").getElementsByClassName("ckbox");
	for (i = 0; i < checkBoxes.length; i++) {
        pageLoad = true; 
		if (!checkBoxes[i].checked) {
	        pageLoad = false; 
			break;
		}
    }

    // Iterate Courses   
	for (i = 0; i < course.length; i++) {
	    var includeCourse = false;
	    var conditions = '';
        var count = 0;
        
		for (j = 0; j < course[0].childNodes.length; j++) {
			switch (course[i].childNodes[j].nodeName) {
        		case "FacultyCode": 
					var facultyCode = course[i].childNodes[j].firstChild.nodeValue; 
					break;
        		case "Faculty": 
					var facultyDesc = course[i].childNodes[j].firstChild.nodeValue; 
					break;
        		case "School": 
					var school = course[i].childNodes[j].firstChild.nodeValue; 
					break;
        		case "CourseCode": 
					var courseCode = course[i].childNodes[j].firstChild.nodeValue; 
					break;
        		case "CourseDescription": 
					var courseDesc = course[i].childNodes[j].firstChild.nodeValue; 
					break;
        		case "IncludesLanguages": 
					var incLangs = course[i].childNodes[j].firstChild.nodeValue; 
					break;
        		case "StudyMode": 
					var studyMode = course[i].childNodes[j].firstChild.nodeValue; 
					break;
        		case "DegreeStyle": 
					var degreeStyle = course[i].childNodes[j].firstChild.nodeValue; 
					break;
        		case "CoursePageURL": 
					var url = course[i].childNodes[j].firstChild.nodeValue; 
					break;
        		case "CourseLevel": 
					var level = course[i].childNodes[j].firstChild.nodeValue; 
					break;
    		}
		}

	    var arr = new Array(facultyDesc, school, courseDesc, url);

		if (!pageLoad) {
	        // We need to filter
			if (!checkBoxes[0].checked) { //Faculty of Business &amp; Management
				if(count >= 1) {conditions += ' && ';}
				conditions += 'facultyCode!=\'BM\'';
				count += 1;
			}
			
			if (!checkBoxes[1].checked) { //Faculty of Humanities, Arts &amp; Social Sciences
				if(count >= 1) {conditions += ' && ';}
				conditions += 'facultyCode!=\'HA\'';
				count += 1;
			}
            
			if (!checkBoxes[2].checked) { //European Business School London
				if(count >= 1) {conditions += ' && ';}
				conditions += 'school!=\'EBSL\'';
				count += 1;
			}
			
			if (!checkBoxes[3].checked) { //Regent's Business School
				if(count >= 1) {conditions += ' && ';}
				conditions += 'school!=\'RBS\'';
				count += 1;
			}
			
			if (!checkBoxes[4].checked) { //Regent's American College
				if(count >= 1) {conditions += ' && ';}
				conditions += 'school!=\'RACL\'';
				count += 1;
			}
			
			if (!checkBoxes[5].checked) { //Webster Graduate School London
				if(count >= 1) {conditions += ' && ';}
				conditions += 'school!=\'WGS\'';
				count += 1;
			}
			
			if (!checkBoxes[6].checked) { //School of Psychotherapy and Counselling
				if(count >= 1) {conditions += ' && ';}
				conditions += 'school!=\'SPC\'';
				count += 1;
			}
			
			
			// Added 15th Feb 2010 JP 
			if 
			(!checkBoxes[7].checked) { //LSFMP
				if(count >= 1) {conditions += ' && ';}
				conditions += 'school!=\'LSFMP\'';
				count += 1;
			}
			
			
			if (!checkBoxes[8].checked) { //Foundation Courses
				if(count >= 1) {conditions += ' && ';}
				conditions += 'level!=\'FND\'';
				count += 1;
			}

			if (!checkBoxes[9].checked) { //Undergraduate Courses
				if(count >= 1) {conditions += ' && ';}
				conditions += 'level!=\'UG\'';
				count += 1;
			}

			if (!checkBoxes[10].checked) { //Postgraduate Courses
				if(count >= 1) {conditions += ' && ';}
				conditions += 'level!=\'PG\'';
				count += 1;
			}

			if (!checkBoxes[11].checked) { //Courses with Languages</label></li>
				if(count >= 1) {conditions += ' && ';}
				conditions += 'incLangs!=\'Y\'';
				count += 1;
			}

			if (!checkBoxes[12].checked) { //Full Time Courses</label></li>
				if(count >= 1) {conditions += ' && ';}
				conditions += 'studyMode!=\'FT\'';
				count += 1;
			}

			if (!checkBoxes[13].checked) { //Part Time Courses</label></li>
				if(count >= 1) {conditions += ' && ';}
				conditions += 'studyMode!=\'PT\'';
				count += 1;
			}

			if (!checkBoxes[14].checked) { //American Degrees</label></li>
				if(count >= 1) {conditions += ' && ';}
				conditions += 'degreeStyle!=\'US\'';
				count += 1;
			}

			if (!checkBoxes[15].checked) { //UK Degrees</label></li>
				if(count >= 1) {conditions += ' && ';}
				conditions += 'degreeStyle!=\'UK\'';
				count += 1;
			}

            // Evaluate the conditions    	    
    	    if(conditions.length > 0) {
	            includeCourse = eval(conditions);	        
            }
	        
	        // If we have a match add row
	        if(includeCourse) {
	            addrow('courseData', arr);
	        }
	    } 
	    else 
	    {
	        // Page load - add all rows
	        addrow('courseData', arr);
	    }
	}
	//alert(document.getElementById('courseData').rows.length);
}   

function addrow(tablename, arr) 
{
    var className;
    
    var tbl = document.getElementById(tablename);
    var lastRow = tbl.rows.length;
    var row = tbl.insertRow(lastRow);
      
    switch (arr[1].toLowerCase())
    {
        case "ebsl": 
            className = "ebsl";
            break;
        case "racl": 
            className = "rac";
            break;
        case "rbs": 
            className = "rbsl";
            break;
        case "spc": 
            className = "spc";
            break;
        case "wgs": 
            className = "wgsl";
            break;
		case "lsfmp":
		    className = "lsfmp";
			break;
        default : 
            className = "ebsl";
            break;
    }
    row.className = className;
    
    for (r = 0; r < arr.length -1; r++) 
    {   
        var cell = row.insertCell(r);
        className = ("colwidth" + (r+1)) + "B";
        cell.className = className;
        //cell.innerHTML = arr[r];
        cell.innerHTML = "<a href='" + arr[3] + "' target=''>" + arr[r] + "</a>";
    }
}