﻿function FocusInput(e, tip)
{
    if (e.value == tip)
    {
        e.value = '';
        e.setAttribute("class", "inputfocus");
        e.className = "inputfocus";
    }
}
function BlurInput(e, tip)
{
    if (e.value == '')
    {
        e.value = tip;
        e.setAttribute("class", "inputblur");
        e.className = "inputblur";
    }
}

function m_HideShowObject(p_oWho, p_oIcon)
{
    if (typeof(p_oIcon) == 'undefined') p_oIcon = null;

    if (p_oWho.style.display == 'none' || p_oWho.style.display == '')
    {
        p_oWho.style.display = 'block'
        if (p_oIcon != null)
        {
            p_oIcon.src = '/App_Graphics/minus_compact.png';
        }
    }
    else
    {
        p_oWho.style.display = 'none';
        
        if (p_oIcon != null)
        {
            p_oIcon.src = '/App_Graphics/plus_compact.png';
        }
    }
}

function m_LocatePathNode(p_cCourseID)
{
    var oPathNode = document.getElementById("pathnode"+p_cCourseID)
    
	m_HideShowPathNode(p_cCourseID,'show');
	window.scrollTo(0,oPathNode.offsetTop - 100)// leave a little space
		
	setTimeout("m_HideShowPathNode('"+p_cCourseID+"')",800);
	setTimeout("m_HideShowPathNode('"+p_cCourseID+"')",950);
	setTimeout("m_HideShowPathNode('"+p_cCourseID+"')",1100);
	setTimeout("m_HideShowPathNode('"+p_cCourseID+"')",1250);
}

function m_HideShowPathNode(p_cCourseID, p_cToggle)
{
    if (typeof(p_cToggle) == 'undefined') p_cToggle = '';
    
    var oPathNode = document.getElementById("pathnode"+p_cCourseID)
    
    if (p_cToggle != 'show' && (oPathNode.style.backgroundColor == 'red' || oPathNode.style.backgroundColor == "#cc0000" || oPathNode.style.backgroundColor == "rgb(204, 0, 0)"))
    {
        oPathNode.style.backgroundColor = '';
    }
    else
    {
        oPathNode.style.backgroundColor = 'red';
    }
    
    lp_m_SetTitleStyles(oPathNode)        
}

function LoadLearningPaths()
{
    lp_v_cStaticBackground = "#6787B3";
    var blocks = document.getElementsByTagName("learningblock");
    for (var i=blocks.length-1; i>=0; i--)
    {// using reverse order correctly positions the root block.
        with(blocks[i].nextSibling.childNodes[0].childNodes[0])
        {
            iOpen = parseInt(childNodes[1].childNodes[0].getAttribute('open'));
            iEnroll = parseInt(childNodes[1].childNodes[0].getAttribute('enroll'));

            // add Event Handler to the "Click here to open" link
            if (navigator.appName == 'Microsoft Internet Explorer')
            {
    	        //childNodes[1].childNodes[1].getElementsByTagName('div')[0].onmouseup = OnSelectPath;
    	        childNodes[1].childNodes[0].childNodes[iOpen].onmouseup = OnSelectPath;
	        }
		    else
		    { // mozilla
		        //childNodes[1].childNodes[0].getElementsByTagName('div')[0].addEventListener('mouseup', OnSelectPath, false);
		        childNodes[1].childNodes[0].childNodes[iOpen].addEventListener('mouseup', OnSelectPath, false);
		    }
		    
		    // Hide the line and "Click to open" link for the first block
            if (i==0)
            {
               childNodes[0].style.display = 'none'; // hide the line
               //childNodes[1].childNodes[1].style.display = 'none'; // hide the title
               childNodes[1].childNodes[0].childNodes[iOpen].style.display = 'none'; // hide the link
            }

            // Load the learning Paths
            //childNodes[1].childNodes[0].setAttribute("id", "forest");// learningblock.table.tbody.tr.td
            childNodes[1].childNodes[1].setAttribute("id", "forest");// learningblock.table.tbody.tr.td
            //lp_m_loadLearningPath(childNodes[1].childNodes[0]);
            lp_m_loadLearningPath(childNodes[1].childNodes[1]);
            
            // Hide all subsequent Paths
            if (i>0)
            {
                //childNodes[1].childNodes[0].style.display = 'none';
                childNodes[1].childNodes[1].style.display = 'none';
                
                childNodes[1].childNodes[0].childNodes[iEnroll].style.display = 'none';
                
            }
        
        }
    }

    if (blocks.length > 0)
    {
        //SelectedBlock = blocks[0].nextSibling.childNodes[0].childNodes[0].childNodes[1].childNodes[1]
        SelectedBlock = blocks[0].nextSibling.childNodes[0].childNodes[0].childNodes[1].childNodes[0]
    }
}

SelectedBlock = null;
function OnSelectPath(mozillaevent){

    iOpen = parseInt(SelectedBlock.getAttribute('open'));
    iEnroll = parseInt(SelectedBlock.getAttribute('enroll'));

    // This is the Block that is currently open
 
        // Hide the Learning Path
        //SelectedBlock.parentNode.childNodes[0].style.display = 'none'; 
        SelectedBlock.parentNode.childNodes[1].style.display = 'none';
        
        // Show the Block title
        //SelectedBlock.style.display = ''; // return display property to default
        
        // Show the link
        //SelectedBlock.getElementsByTagName('div')[0].style.display = ''; // return display property to default
        SelectedBlock.childNodes[iOpen].style.display = ''; // return display property to default

        // Hide the Enrollment
        SelectedBlock.childNodes[iEnroll].style.display = 'none';
                

    // Find the Block that is to be opened
	    // set the mozilla event object to the same object name as IE
	    if (navigator.appName == 'Microsoft Internet Explorer')
	    {
	        var e = window.event;
	        SelectedBlock =  e.srcElement.parentNode;
	    }
	    else
	    {
	        var e = mozillaevent;
	        //SelectedBlock =  e.target.parentNode.parentNode;
	        SelectedBlock =  e.target.parentNode;
	    }

    // Open the Block which was clicked.
        
        // Hide the Block title
        //SelectedBlock.style.display = 'none'; 
        
        // Show the Learning Path
        //SelectedBlock.parentNode.childNodes[0].style.display = ''; // return display property to default
        SelectedBlock.parentNode.childNodes[1].style.display = ''; // return display property to default
        
        // Hide the link
        //SelectedBlock.getElementsByTagName('div')[0].style.display = 'none'; 
        SelectedBlock.childNodes[iOpen].style.display = 'none'; 
        
        // Show the Enrollment
        SelectedBlock.childNodes[iEnroll].style.display = ''; // return display property to default

}

function ClientCallback(result, context)
{
    //alert(result);
    context.innerHTML = "Wish List";
    context.title = "View my Wish List";
}


function OnClickEnroll(courseID, context)
{

    if (context.title.indexOf('View') > -1)
   {
    document.location.href = "enrollments.aspx";
   }
   else
   {
        context.innerHTML = "Adding...";
        EnrollRequest(courseID, context);
   }
}

CallbackContext = null;
function SendCallback(callcontrol, value1, value2, context)
{
    CallbackContext = context;

    //var l_cLocation = document.location.protocol + "//" + document.location.host + "/learningpath/";
    var l_cLocation = document.location.protocol + "//" + document.location.host + "/lpstaging/";
	var l_cPath = l_cLocation;
		
	if (navigator.appName == 'Microsoft Internet Explorer')
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

		xmlhttp.open("POST", l_cPath + "services.asmx",true);
		xmlhttp.onreadystatechange=CallBackResponse;
			
		xmlhttp.setRequestHeader("Man", "POST "+ l_cPath +"services.asmx HTTP/1.1")
		xmlhttp.setRequestHeader("MessageType", "CALL")
		xmlhttp.setRequestHeader("Content-Type", "text/xml")
			
		xmlhttp.send(''+
				'<?xml version="1.0" encoding="utf-8"?>'+
				'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+
					'<soap:Body>'+
						'<Callback xmlns="tralearningpath">'+
							'<callcontrol>'+callcontrol+'</callcontrol>'+
							'<value1>'+value1+'</value1>'+
							'<value2>'+value2+'</value2>'+
						'</Callback>'+
					'</soap:Body>'+
				'</soap:Envelope>');
	}
	else
	{
			
		var mySOAPCall = new SOAPCall();
		
		mySOAPCall.transportURI = l_cPath+"services.asmx"
		mySOAPCall.actionURI = "tralearningpath/Callback";
			
		// SOAP parameters
		var param1 = new SOAPParameter();
		param1.value = arg;
		param1.name = "callcontrol";
			
		var param2 = new SOAPParameter();
		param2.value = value;
		param2.name = "value1";

		var param2 = new SOAPParameter();
		param2.value = value;
		param2.name = "value2";
					
		// combine the 2 params into an array
		var myParamArray = [param1, param2];
			
		mySOAPCall.encode(0,"Callback", "tralearningpath",0, null,myParamArray.length, myParamArray);
		var returnObject = mySOAPCall.asyncInvoke(CallBackResponse); 
	}
}

function CallBackResponse()
{

    var l_return = null;
    
	if (navigator.appName == 'Microsoft Internet Explorer')
	{
		if (xmlhttp.readyState==4)
		{
			// get	the return envelope
			var	szResponse	= xmlhttp.responseText;
			
			var objXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			// load	the return into an XML data island
			objXmlDoc.loadXML(szResponse);
			
			if (objXmlDoc.parseError.errorCode != 0)
			{
				var xmlErr =	objXmlDoc.parseError;
				l_return = "You have error " + xmlErr.reason;
			}
			else
			{
				l_return = unescape(xmlhttp.responseText);
			}
    }
	}
	else
	{
		if (pError != 0)
		{ 
			l_return = "Service failure";
		}
		else 
		{
			var fault = pReturn.fault; 
			if (fault != null)
			{ 
				var code = fault.faultCode; 
				var msg = fault.faultString; 
				l_return ="SOAP Fault:\n\nCode: "  + code + "\n\nMessage: " + msg
			}
			else 
			{
				// Examples usually use a method similar to:
				//	var response = new Array();
				//	response = pReturn.getParameters(false, {});
				//	var l_return = "Return value: " + response[0].value;
				 
				
				// This method works for me.
				l_return += '<br><br>message: '+ unescape(m_NSxml(pReturn.message));
				l_return += '<br><br>envelope: '+ unescape(m_NSxml(pReturn.envelope));
				l_return += '<br><br>body: '+ unescape(m_NSxml(pReturn.body));
				
			}
		}
	}
	
	if (l_return != null)
	{
	    // Do something with the return
        ClientCallback(l_return, CallbackContext); // Created by Page_Load
    }
}

/*
function TabMouseOver(me)
{
    var l_src = null;
    if(me.filters.length > 0)
    {
        l_src = me.filters[0].src;
    }
    else
    {
        l_src = me.src;
    }

	if(l_src.indexOf("active") == -1)
	{
	    var l_Prefix = null;
	    if (l_src.indexOf("btn") > -1)
	    {
	        l_Prefix = "btn"
	    }
	    else if (l_src.indexOf("tab") > -1)
	    {
	        l_Prefix = "tab"
	    }
	    
	    if (l_Prefix != null)
	    {
		    //me.src = "../graphics/" + l_Prefix + "_highlight.png"
		    var l_NewSrc = "../graphics/" + l_Prefix + "_highlight.png"
		    
		    if(me.filters.length > 0)
            {
                me.filters[0].src = l_NewSrc;
            }
            else
            {
                me.src = l_NewSrc;
            }
		}
	}
}

function TabMouseOut(me)
{
    if(me.src.indexOf("active") == -1)
	{
	    var l_Prefix = null;
	    if (me.src.indexOf("btn") > -1)
	    {
	        l_Prefix = "btn"
	    }
	    else if (me.src.indexOf("tab") > -1)
	    {
	        l_Prefix = "tab"
	    }
	    
	    if (l_Prefix != null)
	    {
		    me.src = "../graphics/" + l_Prefix + "_separator.png"
		}
	}
}
*/

swapcounter = 0
function SwapImg(me,newimg)
{

    var l_src = null;
    if(me.filters && me.filters.length > 0)
    {
        l_src = me.filters[0].src;
    }
    else if (me.tagName.toLowerCase() != "img")
    {
        l_src = me.style.background;
    }
    else
    {
        l_src = me.src;

    }

	if(l_src.indexOf("active") == -1)
	{
	    if(me.filters && me.filters.length > 0)
        {
             me.filters[0].src = newimg;
        }
        else if (me.tagName.toLowerCase != "img")
        {
            me.style.background = "url(" + newimg +") top right no-repeat";
        }
        else
        {
           me.src = newimg;
       }
	}
	
}

function onPageLoad() {

    /// This is a work-around for an IE 6 repaint issue.
    /// Some objects are not displayed until the screen
    /// is repainted. Resize the screen to force a repaint.
    if (navigator.appName == 'Microsoft Internet Explorer')
    {
        if (parseFloat(navigator.appVersion.substring(navigator.appVersion.indexOf('MSIE') + 5.5, navigator.appVersion.indexOf('MSIE') + 8)) < 7)
        {
            window.resizeBy(1, 1);
        }
    }
}