
var reg_pjusername = null;

function loadRegCommon ( startMode, userName, userPWD, remember ) 
{
   var swfContentPath = "/content/game3/LoginPanel.swf"; 
	  
   var so = new SWFObject( swfContentPath, "popupRegister", "600", "400", "8" ); 
   so.addParam    ("quality"          , "high");
   so.addParam    ("wmode"            , "transparent" ); 
   so.addParam    ("allowScriptAccess", "always");
   so.addParam    ("salign"           , "tl" ); 
	   	   
   so.addVariable ( "regSkin"  , m_contentPath + "EndGameRecs.xml" ) ;     
   so.addVariable ( "startView", startMode ) ;     
   so.addVariable ( "affId"    , m_affiliateId ) ;     
   so.addVariable ( "loadFn"   , "flash_regLoaded" ) ; 
   
   if ( userName != null ) 
   {
     so.addVariable ( "loginName" , userName ) ; 
     so.addVariable ( "loginPwd"  , userPWD  ) ; 
     so.addVariable ( "remember"  , remember ) ; 
   }
	   
   so.write( "registrationContainer" );	
}

function loadRegistrationPanel () 
{
  loadRegCommon("reg", null, null, null) ;
}  

function loadRegLoginPanel () 
{
  loadRegCommon("login", null, null, null) ;
} 

function loginViaPanel ( userName, userPWD, remember ) 
{
  // alert (userName + " : " + userPWD + " : " + remember ) ;
  loadRegCommon("login", userName, userPWD, remember) ;
}  



function flash_regLoaded ( moduleName ) 
{
   var theSwf   
   if ( moduleName == "LoginPanel" )
   {
     theSwf = document.getElementById ("popupRegister");
     if ( theSwf != null ) 
	 {
		 theSwf.focus(); 
	 }
   }
   
}


 var m_userChanged_Callback = null;
 
 function reg_setUserChangedCallback ( _callback ) 
 {
	 m_userChanged_Callback = _callback;
 }


function flash_hideLoginPanel ( loginStatusChanged, pjuid, userName )
{
   reg_hideRegistrationScreen() ;
   if ( loginStatusChanged || loginStatusChanged == "true" )
   {   
	   //  alert ("LoggedIn: " + userName ) ;
	   setUserIsLoggedIn  ( userName ) ;
	   m_pjuid                  = pjuid ;
	   m_pjuserName             = userName;  
       EASY_TO_FIND_PJUID       = pjuid;
       EASY_TO_FINE_PJUSER_NAME = userName;
	   if ( m_reg_afterRegistrationCallback ) 
	   {
		 m_reg_afterRegistrationCallback (pjuid);
		 if ( m_userChanged_Callback )
		   m_userChanged_Callback ( pjuid, userName ) ;
	   } 
	   else 
	   {
	     refreshPage(true) ;    // call function in allPages.js
	   }
   }
}

function flash_showTermsAndConditions ()
{
	showTermsAndConditions() ;
}

var m_PopupInfoPath = "/content/affiliates/popjax/" ;

function showTermsAndConditions () 
{
  var popupDialog  = document.getElementById ( "popupDialog" );
  if ( popupDialog != null )
  {
    popupDialog.style.display = "block";
  
    var popupDialogTitle = document.getElementById ( "popupDialogTitle" );
    if ( popupDialogTitle != null )
      popupDialogTitle.innerHTML = "Terms and Conditions";
  
    var popupDialogContentsHolder = document.getElementById ( "popupDialogContentsHolder" );
    if ( popupDialogContentsHolder != null )
      popupDialogContentsHolder.src = m_PopupInfoPath + "TermsAndConditions.html";
  }
}

function closePopUp () {
  var popupDialog  = document.getElementById ( "popupDialog" );
  popupDialog.style.display = "none";   
}


 var m_reg_afterRegistrationCallback = null;
 
 function reg_setAfterRegistrationCallback ( _callback ) 
 {
	 m_reg_afterRegistrationCallback = _callback;
 }
   
function reg_showRegistrationScreen () 
{
	 var login_loginDialog = document.getElementById ("login_loginDialog");
	 if (  login_loginDialog != null ) {
		login_loginDialog.style.display = "none";
	 }
	 
	 loadRegistrationPanel() ;
	 
	 var reg_registrationDialog = document.getElementById ("registrationContainer");
	 if (  reg_registrationDialog != null ) {
	   reg_registrationDialog.style.display = "block";
	 }
}
function reg_showRegLoginScreen () 
{
	 var login_loginDialog = document.getElementById ("login_loginDialog");
	 if (  login_loginDialog != null ) {
		login_loginDialog.style.display = "none";
	 }
	 
	 loadRegLoginPanel() ;
	 
	 var reg_registrationDialog = document.getElementById ("registrationContainer");
	 if (  reg_registrationDialog != null ) {
	   reg_registrationDialog.style.display = "block";
	 }
}

function reg_driveLoginScreen ( _usr, _pwd, _remember) 
{
	 var login_loginDialog = document.getElementById ("login_loginDialog");
	 if (  login_loginDialog != null ) {
		login_loginDialog.style.display = "none";
	 }
	 
	 loginViaPanel(_usr, _pwd, _remember) ;
	 
	 var reg_registrationDialog = document.getElementById ("registrationContainer");
	 if (  reg_registrationDialog != null ) {
	   reg_registrationDialog.style.display = "block";
	 }
}

 function reg_hideRegistrationScreen () {

	 var reg_registrationDialog = document.getElementById ("registrationContainer");
	 if (  reg_registrationDialog != null ) {
	   reg_registrationDialog.style.display = "none";   
	 }
     cancelPopUpLogIn() ;
 }

   function login_showLoginScreen () {
	   
	   var reg_registrationDialog = document.getElementById ("registrationContainer");
	   if (  reg_registrationDialog != null ) {
	     reg_registrationDialog.style.display = "none";
	   }
	   
	   var login_loginDialog = document.getElementById ("login_loginDialog");
	   if (  login_loginDialog != null ) {
	     login_loginDialog.style.display = "block";
	   }
   }

   function login_hideLoginScreen () {
	   
	   var login_loginDialog = document.getElementById ("login_loginDialog");
	   if (  login_loginDialog != null ) {
	     login_loginDialog.style.display = "none";
	   }
   }

   var m_reg_landingPageUrl = "http://www.videojax.com/videocube/index.htm";
	
   function reg_initRegistrationPage () {
      // reg_initToolTips ();
   }
   

/*********************************************************************************
*
*                              POP UP LOGIN FORM STUFF

**********************************************************************************/

   function login_initLoginPage () {
      login_initToolTips ();
   }


   function login_initToolTips () {
	 	  
   }
   
   var m_login_afterLoginCallback = null;
   
   function login_setAfterLoginCallback ( _callback ) {
	   m_login_afterLoginCallback = _callback;
   }
   
   function login_showLoginFormError ( errorText ) {
	   var login_formError = document.getElementById ( "login_formError" );
   	   if (  login_formError != null ) 
       {
	     login_formError.style.display = "block";
	     login_formError.innerHTML = errorText;
	   }
   }

   function login_hideLoginFormError () {
	   var login_formError = document.getElementById ( "login_formError" );
   	   if (  login_formError != null ) {
	     login_formError.style.display = "none";
	   }
   }

   function _private_reg_hideSpinner () {
	  if ( m_reg_spinnerDiv != null ) {
	     m_reg_spinnerDiv.parentNode.removeChild(m_reg_spinnerDiv);
		 m_reg_spinnerDiv = null;
	  }
   }

	/**
	* The URL of the "log in" service for VideoJax.com
	*/
	// var loginUrl = "./dummyData/loginResponse.html"; ///videocube/login.htm";
	var m_login_loginUrl = "/videocube/login.htm";
	
	function login_handleLoginWithBackEndComplete (pjuid) {	
	
	   hideNewPopUpLoginScreen ();
	   hideSpinnerLoginDiv (); 
	   hideSpinner ( "spinnerDuringLoginDiv" );	   

	  // alert ( "CALLBACK: " + afterLoginCallback );
	   if ( m_login_afterLoginCallback ) {
		   m_login_afterLoginCallback (pjuid);
	   } else {
		  // alert ( "NO LOGIN CALLBACK" );
	   }
	}

	function login_getLogInPostQueryStringFromPopUp () {
	   var userId   = $("login_userName");
	   var password = $("login_password");
	   reg_pjusername = userId;
	   
	   var loginString = "mode=1&player.name=" + userId.value + "&player.password=" + password.value + "&x=99&y=10";
	   
	   return loginString;		
	}

	/**
	* This does an HTTP POST to the login service in order to log the user in.
	*/
	function login_postLoginFormData() {
	    var strURL = m_login_loginUrl;
		
		login_hideLoginFormError ();
				
		var xmlHttpReq = false;
		var self = this;
		// Mozilla/Safari
		if (window.XMLHttpRequest) {
			self.xmlHttpReq = new XMLHttpRequest();
		}
		// IE
		else if (window.ActiveXObject) {
			self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
		self.xmlHttpReq.open('POST', strURL, true);
		self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		self.xmlHttpReq.onreadystatechange = function() {

			if (self.xmlHttpReq.readyState == 4) {
				if ( self.xmlHttpReq.responseText.indexOf ( "class=\"error\"" ) > 0 ) {
					login_showLoginFormError ( "User id or password incorrect" );
				} else {
				   try {
					   setUserIsLoggedIn ( $("userId").value );  

					   window["m_pjuserName"] = getCookie ( "userIdString" );  

                       var userId = $("userId");
				       var login_userNameValue = $("login_userName").value;
					   var pjuid = null;
					   var pjuidIdxStart = self.xmlHttpReq.responseText.indexOf ("EASY_TO_FIND_PJUID=");
					   
					   if ( pjuidIdxStart >= 0 ) {
						  pjuidIdxStart += "EASY_TO_FIND_PJUID=\"".length;
						  var pjuidIdxEnd = self.xmlHttpReq.responseText.indexOf ("\"", pjuidIdxStart+1);
						  pjuid = self.xmlHttpReq.responseText.substr(pjuidIdxStart, (pjuidIdxEnd - pjuidIdxStart) );
					   } else {
						   //alert ( "COULD NOT FIND PJUID" );
					   }

						/**
						* <<< START OF NEW CODE TO SHOW USER NAME WHETHER LOG IN WITH NAME OR EMAIL ADDRESS >>>
						*/
					   var userName = null;
					   var userNameStart = self.xmlHttpReq.responseText.indexOf ("EASY_TO_FINE_PJUSER_NAME=");
					   if ( userNameStart >= 0 ) {
						  userNameStart += "EASY_TO_FINE_PJUSER_NAME=\"".length;
						  var userNameEnd = self.xmlHttpReq.responseText.indexOf ("\"", userNameStart+1);
						  userName = self.xmlHttpReq.responseText.substr(userNameStart, (userNameEnd - userNameStart) );
					   } else {
						   //alert ( "COULD NOT FIND PJUID" );
					   }
						if ( userName != null ) {
							var userIdField = document.getElementById ( "userId" );
							userIdField.value = userName;
						}
						
					    // SCOTT 9/25.  THIS CAUSED LOG OUT NOT TO WORK setCookie ( "pjuid", pjuid );
					   window["m_pjuid"] = pjuid;
					   hideLogInForm ();				   
					   login_handleLoginWithBackEndComplete (pjuid);
					   
				   } catch ( innerLogin ) {
					   // alert ( "EXC INNER LOGIN: " + innerLogin );
				   }
				}

				
				// alert ( " RESP: " + self.xmlHttpReq.responseText );
			}
		}
		self.xmlHttpReq.send(login_getLogInPostQueryStringFromPopUp());
	}
