﻿function doNext(str_Url){
    document.location = str_Url;
}

function getParameterValue(str_Url, str_ParamName){
	 var int_Index = str_Url.indexOf(str_ParamName);
	 if(int_Index == -1){return;}
	    
	 var str_Sub = str_Url.substring((int_Index + str_ParamName.length + 1), str_Url.length);
	 int_Index = str_Sub.indexOf("&");
	 if(int_Index == -1){return str_Sub;}
	 return str_Sub.substring(0, int_Index);
}

function doLaunchPlayer(str_CourseID, str_OrgID){
    // LMS generates the following fields
	var str_LearnerName = generateLearnerName();					// required
	var str_LearnerID = 	generateGuid(); 				// required and unique
	var str_SessionID = 	"generatedByLMS"; 				// optional
	var str_DomainID = 		"sqlserver"; 				 	// required and configurable
	var str_SkinID = 		"default"; 					// optional and chooseable in form
	var str_Culture = 		"en_US"; 						// optional and chooseable in form
						
	// Update chooseable parameter values from form
	var obj_Form = document.forms['playerForm'];
	str_SkinID = obj_Form.elements['skinID'].options[obj_Form.elements['skinID'].selectedIndex].value;
	str_Culture = obj_Form.elements['culture'].options[obj_Form.elements['culture'].selectedIndex].value;

	// Replace with deployment URL of Icodeon SCORM Player
	var str_Root = "http://www.scormtech.com"
	var str_Name = "IcodeonPlayer";
	var str_Url = str_Root + "/skins/LMSMain.aspx";
				
	// Add URL parameters as query string
	str_Url += "?learnerID=" + str_LearnerID;
	str_Url += "&learnerName=" + str_LearnerName;
	str_Url += "&courseID=" + str_CourseID;
	if(typeof(str_OrgID) != "undefined" && str_OrgID.length != 0){
        str_Url += "&orgID=" + str_OrgID;
	}
	str_Url += "&sessionID=" + str_SessionID;
	str_Url += "&skinID=" + str_SkinID;
	str_Url += "&culture=" + str_Culture;
	str_Url += "&domainID=" + str_DomainID;
				
	// Launch
	if(doBrowserCheck()){
	    openWindow(str_Url, str_Name);
	}
	else{
        alert("Icodeon SCORM Player does not currently support this Browser");
	}
	
	//alert("Icodeon SCORM Player not available during website upgrade. Please try later - thank you.");
}


/**
 * Method to generate a unique ID for SCORM 
 * tracking as cmi.learner_id
 */  
var str_Guid = undefined;
function generateGuid(){
    if(typeof(str_Guid) == "undefined"){
        var result, i, j;
        result = '';
        for(j=0; j<32; j++){
            if( j == 8 || j == 12|| j == 16|| j == 20){
                result = result + '-';
            }
            i = Math.floor(Math.random()*16).toString(16).toUpperCase();
            result = result + i;
        }
        str_Guid = result;
     }
     return str_Guid;
}

/**
 * Method to generate a SCORM conmformant learner name
 * for SCORM tracking as cmi.learner_name
 */      
function generateLearnerName(){
	var obj_Form = document.forms['playerForm'];
	var str_FirstName = obj_Form.elements['firstName'].value;
	var str_FamilyName = obj_Form.elements['familyName'].value;
	return str_FamilyName + "," + str_FirstName;
}

/**
 * Method to open a browser window for the SCORM Player application
 *
 * @param String str_Url the launch URL and parameter string
 * @param String str_Name the lname of the window
 */
function openWindow(str_Url, str_Name){
			
    var int_Width = screen.availWidth-50
	var int_Height = screen.availHeight-100;
	var int_Left = (screen.width/2) - (int_Width/2);
	var int_Top = (screen.height/2) - (int_Height/2);
	
	var str_Attributes = 'width=' + int_Width + ",";
	str_Attributes += 'height=' + int_Height + ",";
	str_Attributes += 'left=' + int_Left + ",";
	str_Attributes += 'top=' + int_Top + ",";
	str_Attributes += 'screenX=' + int_Left + ",";
	str_Attributes += 'screenY=' + int_Top + ",";
	str_Attributes += 'toolbar=no,';
	str_Attributes += 'resizable=yes,';
	str_Attributes += 'scrollbars=no,';
	str_Attributes += 'location=no,';
	str_Attributes += 'directories=no,';
	str_Attributes += 'status=yes,';
	str_Attributes += 'menubar=no,';
	str_Attributes += 'copyhistory=yes';
	
	var win_Player = window.open(str_Url,str_Name,str_Attributes);
	win_Player.focus();
	return win_Player;
}

function doBrowserCheck(){
	var d, dom, ie, ie4, ie5x, moz, mac, win, lin, old, ie5mac, ie5xwin, op;

	d = document;
	n = navigator;
	na = n.appVersion;
	nua = n.userAgent;
	win = ( na.indexOf( 'Win' ) != -1 );
	mac = ( na.indexOf( 'Mac' ) != -1 );
	lin = ( nua.indexOf( 'Linux' ) != -1 );
	
	if (!d.layers ){
		dom = ( d.getElementById );
		op = ( nua.indexOf( 'Opera' ) != -1 );
		konq = ( nua.indexOf( 'Konqueror' ) != -1 );
		saf = ( nua.indexOf( 'Safari' ) != -1 );
		moz = ( nua.indexOf( 'Gecko' ) != -1 && !saf && !konq);
		ie = ( d.all && !op );
		ie4 = ( ie && !dom );

	    ie5x = ( d.all && dom );
	    ie5mac = ( mac && ie5x );
	    ie5xwin = ( win && ie5x );
	}
	return true;
}