function PlayFreeTrial(bShowKeyHolePostGameHtml){
	var oX=window.external;
	var errorMessage="";
	
	if(oX.IsKeyhole){
		var keyholeVersion=oX.ProtectedParameter("KEYHOLE_VERSION");
		var brand=oX.ProtectedParameter("BRAND");
		var sku=oX.ProtectedParameter("SKU");
		var resultCode = oX.UnlockGameForTrial(keyholeVersion,brand,sku);
		if(resultCode==100){
			if(oX.LaunchGame(keyholeVersion,brand,sku,bShowKeyHolePostGameHtml?"keyhole:postgame.html":"keyhole:main.html")){
				//game is launched - this page will automatically be closed
			}else{
				//???failed to launch game
				errorMessage="???failed to launch game";
			}
		}else{
			//???map result code to error message
            errorMessage=GetUnlockCodeMessage(resultCode, brand);
		}
		oX=null;
	}else{
		errorMessage="Sorry, can only play free trial in keyhole";
	}
	
	if(errorMessage.length>0){
		document.getElementById("errorBlanketMessage").innerHTML = 
			'<h3>Error</h3>'+
			'<p>'+errorMessage+'</p>'+
			'<p><a href="#" onclick="toggleDisplay(\'errorBlanket\');return false;" class="smlBtn">Close</a></p>'+
			'';
		toggleDisplay('errorBlanket');
	}
}


function EnterPurchasePath(purchasePathUrl, keyholeVersion, brand, sku, locale){
	var oX=window.external;
	if(oX.IsKeyhole){
		//override keyhole version, brand, sku with protected values
		keyholeVersion=oX.ProtectedParameter("KEYHOLE_VERSION");
		brand=oX.ProtectedParameter("BRAND");
		sku=oX.ProtectedParameter("SKU");
	}
	window.location.href = purchasePathUrl+"&"+
							"khversion="+keyholeVersion+"&"+
							"brand="+brand+"&"+
							"sku="+sku+"&"+
							"locale="+locale;
}

function CreateUiServerParams(keyholeVersion, brand, sku, locale){

      var oX=window.external;
      var width=780;
      var height=600;
      var htmlTitleBar=0;
      var htmlBorder=0;      

      if(oX.IsKeyhole){
            var clientWidth=oX.ExternalWindowClientWidth;
            var clientHeight=oX.ExternalWindowClientHeight;
            var bExternalWindowHasTitleBar=oX.ExternalWindowHasTitleBar;
            var bExternalWindowHasBorder=oX.ExternalWindowHasBorder;

            //override keyhole version, brand, sku with protected values
            keyholeVersion=oX.ProtectedParameter("KEYHOLE_VERSION");
            brand=oX.ProtectedParameter("BRAND");
            sku=oX.ProtectedParameter("SKU");

            if(typeof(clientWidth)!="undefined"){
                  width=clientWidth;
            }

            if(typeof(clientHeight)!="undefined"){
                  height=clientHeight;
            }

            if(typeof(bExternalWindowHasTitleBar)!="undefined"){
                  htmlTitleBar=(bExternalWindowHasTitleBar?"0":"1");
            }

            if(typeof(bExternalWindowHasBorder)!="undefined"){
                  htmlBorder=((bExternalWindowHasTitleBar||bExternalWindowHasBorder)?"0":"1");
            }
      }

      return "khversion="+keyholeVersion+"&"+
                  "brand="+brand+"&"+
                  "sku="+sku+"&"+
                  "locale="+locale+"&"+
                  "width="+width+"&"+
                  "height="+height+"&"+
                  "htmlTitleBar="+htmlTitleBar+"&"+
                  "htmlBorder="+htmlBorder;

}

function UnlockGameWithToken( token )
{
    var oX=window.external;
    var errorMessage="";
    var keyholeVersion=oX.ProtectedParameter("KEYHOLE_VERSION");
    var brand=oX.ProtectedParameter("BRAND");
    var sku=oX.ProtectedParameter("SKU");

    //alert( "UnlockGameWithToken() token=" + token + " keyholeVersion=" + keyholeVersion + " brand=" + brand + " sku=" + sku );

    if( oX.IsKeyhole )
    {
        var resultCode=oX.UnlockGameWithToken(keyholeVersion,brand,sku,token);
        //alert( "result code returned by oX.UnlockGameWithToken() = " + resultCode );
        if( resultCode != 100 )
        {
            //???map result code to error message
            //???highlight certain elements; make labels red
            //???set focus
            errorMessage = GetUnlockCodeMessage(resultCode, brand);
            //document.getElementById("licenseKeyLabel").className = "error";
        }
    }
}

function LaunchGame()
{
    var oX=window.external;
    var errorMessage="";
    var keyholeVersion=oX.ProtectedParameter("KEYHOLE_VERSION");
    var brand=oX.ProtectedParameter("BRAND");
    var sku=oX.ProtectedParameter("SKU");
    var urlPostGame="";

    if ( oX.IsKeyhole && oX.LaunchGame( keyholeVersion,brand,sku,urlPostGame ) )
    {
        //game is launched - this page will automatically be closed
    }
    else
    {
        // Error during launch attempt.
        alert( "Failed to launch game. Please close game and restart." );
    }

}

function CloseKeyhole() {
    var oX=window.external;
    if(oX.IsKeyhole){
        oX.Close();
    }
}

function onTitlebarMouseDown() {
    var oX=window.external;
    if(oX.IsKeyhole){
        oX.ExternalWindowTitleBarLeftButtonDown();
    }
}

function SetEncryptedCredentialsProperty(encryptedCredentials){
    var oX = window.external;
    if(oX.IsKeyhole){
		var encryptedCredentialsPropertyBrand=oX.EncryptedCredentialsPropertyBrand;
		var encryptedCredentialsPropertySku=oX.EncryptedCredentialsPropertySku;
		var encryptedCredentialsPropertyName=oX.EncryptedCredentialsPropertyName;
    
		oX.SetProperty("keyhole", encryptedCredentialsPropertyBrand, encryptedCredentialsPropertySku, encryptedCredentialsPropertyName, encryptedCredentials);
	}
}

function GetEncryptedCredentialsProperty()
{
    var oX = window.external;
    var credentials = "";

    if(oX.IsKeyhole){
		var encryptedCredentialsPropertyBrand=oX.EncryptedCredentialsPropertyBrand;
		var encryptedCredentialsPropertySku=oX.EncryptedCredentialsPropertySku;
		var encryptedCredentialsPropertyName=oX.EncryptedCredentialsPropertyName;
		
		credentials = oX.GetProperty("keyhole", encryptedCredentialsPropertyBrand, encryptedCredentialsPropertySku, encryptedCredentialsPropertyName);
    }
    return credentials;
}

function GetDecryptedEncryptedCredentialsProperty()
{
    var oX = window.external;
    var username = "";
    var password = "";

    if(oX.IsKeyhole){
		var credentials=GetEncryptedCredentialsProperty();

		username = oX.DecryptEncryptedCredentialsUsername(credentials);
		password = oX.DecryptEncryptedCredentialsPassword(credentials);
    }
    return Array(username, password);
}

function SetRememberMeProperties(bRememberMe, encryptedCredentials){
    var oX = window.external;
    if(oX.IsKeyhole){
		var encryptedCredentialsPropertyBrand=oX.EncryptedCredentialsPropertyBrand;
		var encryptedCredentialsPropertySku=oX.EncryptedCredentialsPropertySku;
    
		oX.SetProperty("keyhole", encryptedCredentialsPropertyBrand, encryptedCredentialsPropertySku, "RememberMe", bRememberMe.toString());
		SetEncryptedCredentialsProperty(bRememberMe?encryptedCredentials:"");
	}
}

function GetRememberMeProperties()
{
    var oX = window.external;
    var bRememberMe = false;
    var username = "";
    var password = "";

    if(oX.IsKeyhole){
		var encryptedCredentialsPropertyBrand=oX.EncryptedCredentialsPropertyBrand;
		var encryptedCredentialsPropertySku=oX.EncryptedCredentialsPropertySku;

        bRememberMe = (oX.GetProperty("keyhole", encryptedCredentialsPropertyBrand, encryptedCredentialsPropertySku, "RememberMe") == "true");
        if(bRememberMe){
			var arrCredentials=GetDecryptedEncryptedCredentialsProperty();
            username = arrCredentials[0];
            password = arrCredentials[1];
        }
    }
    return Array(bRememberMe, username, password);
}

function SetPreventAutoLaunchProperty(bPreventAutoLaunch)
{
    var oX = window.external;

    if(oX.IsKeyhole){
		var preventAutoLaunchPropertyBrand=oX.PreventAutoLaunchPropertyBrand;
		var preventAutoLaunchPropertySku=oX.PreventAutoLaunchPropertySku;
		var preventAutoLaunchPropertyName=oX.PreventAutoLaunchPropertyName;
		var preventAutoLaunchPropertyValue=oX.PreventAutoLaunchPropertyValue;

        if(preventAutoLaunchPropertyName.length>0){
			oX.SetProperty("keyhole", preventAutoLaunchPropertyBrand, preventAutoLaunchPropertySku, preventAutoLaunchPropertyName, preventAutoLaunchPropertyValue);
		}
    }
}

function GetPreventAutoLaunchProperty()
{
    var oX = window.external;
    var bPreventAutoLaunch;

    if(oX.IsKeyhole){
		var preventAutoLaunchPropertyBrand=oX.PreventAutoLaunchPropertyBrand;
		var preventAutoLaunchPropertySku=oX.PreventAutoLaunchPropertySku;
		var preventAutoLaunchPropertyName=oX.PreventAutoLaunchPropertyName;
		var preventAutoLaunchPropertyValue=oX.PreventAutoLaunchPropertyValue;

        bPreventAutoLaunch = (preventAutoLaunchPropertyName.length>0)&&(oX.GetProperty("keyhole", preventAutoLaunchPropertyBrand, preventAutoLaunchPropertySku, preventAutoLaunchPropertyName) == preventAutoLaunchPropertyValue);
    }else{
		bPreventAutoLaunch = true;
    }
    return bPreventAutoLaunch;
}

