﻿// JScript File
/**
Description: The Class is contains functions for interacting with player
author: Zubair Masoodi
Dated: 05/12/2008
*/
function Player(userID)
{
   this.onError = Player.prototype.onError;
	
	
	
}
//var to hold an instance userID
Player.prototype._userID = undefined;

//where to load the data from
Player.prototype._pageURL = "../tests/Info/TestActions_Ajax.aspx";

//var to hold an instance of the XMLHTTPRequest object
Player.prototype._request = undefined;

/**
*	Tells the class to load its data and render the results.
*/

Player.prototype.send = function(userID)
{
	this._userID = userID;
    this._request = this._getXMLHTTPRequest();
	var _this = this;
	this._request.onreadystatechange = function(){_this._onData()};
	this._request.open ('POST',this._generateDataUrl(),true);
	
	this._request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    this._request.setRequestHeader("Content-length",this._pageURL.length);
    
    this._request.send(this._pageURL);
 
     
}

/*
*	Tells the class to act whenever any error is generated
*/
Player.prototype.onError = function(status,statusText,show){
        
}

//renders the entire content
Player.prototype._render = function(_responseXML){
	   	    
	var xmlDoc = _responseXML;
	if(xmlDoc.getElementsByTagName("User")[0].childNodes[0]!=null)	{
	   
	    var User= xmlDoc.getElementsByTagName("User")[0].childNodes[0].data;
	   
	   }

	this._setCookie("Users",User,20,"/","","");
  
	
}
Player.prototype._setCookie = function(name, value, expires, path, domain, secure)
{
  
//    var exdate=new Date();
//    exdate.setDate(exdate.getDate()+ expiredays);
//    document.cookie = c_name+ "=" + value;
//    ((expiredays==null) ? "" : "path=/;expires="+exdate.toGMTString());

        // set time, it's in milliseconds
        var today = new Date();
        today.setTime( today.getTime() );

        
        if ( expires )
        {
        expires = expires * 1000 * 60 * 60 * 24;
        }
        var expires_date = new Date( today.getTime() + (expires) );

        document.cookie = name + "=" + value +
        ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
        ( ( path ) ? ";path=" + path : "" ) +
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ( ( secure ) ? ";secure" : "" );

    

}

Player.prototype._generateDataUrl = function(){
	return this._pageURL + "?u=" + this._userID ;
}
// send data back to flash movie
Player.prototype._clientCallback = function()
{
	
	if(document.getElementById("CreateAssessments")!=null)
	{
	    window.CreateAssessments = $('CreateAssessments');
	    this._getMovieName("CreateAssessments").AjaxCallBackHandler("x"); 
	   

	}
	
}
//get the flash movie name 
Player.prototype._getMovieName = function(movieName)
{
    
    if (navigator.appName.indexOf("Microsoft") != -1) {
        
        return window[movieName]
    }
    else {
         
        return document[movieName]
    }

}

//callback for when the data is loaded from the server
Player.prototype._onData = function()
{
 
	if(this._request.readyState == 4)
	{
		
		if(this._request.status == "200")
		{
					
			this._render(this._request.responseXML);

		}
		else
		{
			//check if an error callback handler has been defined
			if(this.onError != undefined)
			{
				//pass an object to the callback handler with info
				//about the error
				
				this.onError({status:this._request.status,statusText:this._request.statusText,show:true});
			}
		}

		//clean up
		delete this._request;
	}
}


//returns an XMLHTTPRequest instance (based on browser)
Player.prototype._getXMLHTTPRequest = function()
{
	var xmlHttp;
	try
	{
		xmlHttp = new ActiveXObject("Msxml2.XMLHttp");
	}
	catch(e)
	{
		try
		{
			xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
		}
		catch(e2)
		{
		}
	}

	if(xmlHttp == undefined && (typeof XMLHttpRequest != 'undefined'))
	{
		xmlHttp = new XMLHttpRequest();
	}

	return xmlHttp;
}


