/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var xmlHttp
var currentElement
var addedCourseID
var instituionName
var siteUrl = "/elms/en/"
var xg = Ext.grid;

Ext.form.XmlErrorReader = function(){
    Ext.form.XmlErrorReader.superclass.constructor.call(this, {
            record : 'nameValue',
            success: '@success'
        }, [{name:'id', mapping:'name'}, 
            {name: 'msg', mapping:'value'}]
    );
};
Ext.extend(Ext.form.XmlErrorReader, Ext.data.XmlReader);

function getxmlHTTP(){
	  try{
        xmlHttp=new XMLHttpRequest();
    }catch (e){
        try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }catch (e){
            try{
                xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }catch (e){
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
}

function addCourse(institutionEntID){
    //x = confirm("Are you sure?");
    //if(x){
        addedCourseID = 'course' + institutionEntID;
        xmlHttp.onreadystatechange=displayCourseCatalog;
        xmlHttp.open("GET",siteUrl + "addcourse?institutionEntID=" + institutionEntID,true);
        xmlHttp.send(null);
    //}
}




function displayCourseCatalog(){
    var xml;
    if (xmlHttp.readyState!=4){
        return;
    }
    xml=xmlHttp.responseXML;
    if(xml == null){
        alert('Session time out. Please login again');
    }
    addCourseLinkNode = document.getElementById(addedCourseID);
    parentNode = addCourseLinkNode.parentNode;

    parentNode.removeChild(addCourseLinkNode);
    parentNode.innerHTML = "<img src='/en/images/added.gif' />";
}

function takeCourse(institutionEntID){
	
    //x = confirm("Are you sure?");
    //if(x){
        window.open('/en/courses/' + institutionEntID + '/' + institutionEntID + '.html','Course', 'resizable=1, location=no,toolbar=no,menubar=no' );
    //}
  
}


function display(xml, xsl, elementID){
    if (window.ActiveXObject){
        ex=xml.transformNode(xsl);
        displayArea = document.getElementById(elementID);
        removeChildNodes(displayArea);
        displayArea.innerHTML=ex;
    }else if (document.implementation && document.implementation.createDocument){
        xsltProcessor=new XSLTProcessor();
        xsltProcessor.importStylesheet(xsl);
        resultDocument = xsltProcessor.transformToFragment(xml,document);
        displayArea = document.getElementById(elementID);
        removeChildNodes(displayArea);
        displayArea.appendChild(resultDocument);
    }
}

function removeChildNodes(ctrl)
{
    while (ctrl.childNodes[0])
    {
        ctrl.removeChild(ctrl.childNodes[0]);
    }
}


function showHide(elementID){
    currentNode = document.getElementById(elementID);

    if (currentNode.style.display == 'none'){
        currentNode.style.display = '';

    } else {
        currentNode.style.display = 'none';
    }
}


function changeColor(elementID){
	
	if(elementID == 'Welcome'){
		document.getElementById('gridCourses').style.display = 'none';
		document.getElementById('WelcomePageContainer').style.display = '';
	}else{
		document.getElementById('WelcomePageContainer').style.display = 'none';
		document.getElementById('gridCourses').style.display = '';
	}
	currentElement = document.getElementById('page_id').innerHTML = elementID;
}

function inputUserName(){
	
		firstname = prompt('please type your First Name');
		while( firstname == '' || firstname == null){
			firstname = prompt('please type your First Name');
		}
		
		lastname = prompt('please type your Last Name');
		while( lastname == '' || lastname == null){
			lastname = prompt('please type your Last Name');
		}
		x = confirm('name:' + firstname + ' ' + lastname);
		if(x){
		  return firstname + "," + lastname;
			
		}else{
			checkUserName();
		}
	
}

function trim(str) {
        return str.replace(/^\s+|\s+$/g,"");
    }