﻿function ShowHideCollapser(id) {
	
	var collapser = $(id);
	var collapserFrame = collapser.down("div.CollapserFrame");
	var collapserIcon = collapser.down("div.CollapserIcon");
		
	if (collapserFrame.style.display == 'none') {
		collapserFrame.style.display = 'block';
		collapserIcon.removeClassName("CollapserIconClosed").addClassName("CollapserIconOpen");
		Sureflix.Utilities.SetCookie(id,"true");
	} else {
		collapserFrame.style.display = 'none';
		collapserIcon.removeClassName("CollapserIconOpen").addClassName("CollapserIconClosed");
		Sureflix.Utilities.SetCookie(id,"false");		
	}
}
	
function SignInFocus() {
	document.thisForm.Password.onkeydown = SignInKeyDown;
	document.thisForm.Name.onkeydown = SignInKeyDown;
}

function SignInKeyDown(e) {
	var characterCode
	if (e && e.which) {           // NN4 specific code
		e = e
		characterCode = e.which
	} else {
		e = event
		characterCode = e.keyCode // IE specific code
	}

	if (characterCode == 13) {
		SignIn();
		return(false);
	}
	else {
		return(true);
	}
}
		
function SignIn() {
	var form = document.thisForm;
	if ($F("SureWalletUserName") == "") {
		alert(jsEnterUsername);
		$("SureWalletUserName").focus()
		$("SureWalletUserName").select()
	} else if ($F("SureWalletPassword") == "") {
		alert(jsEnterPassword);
		$("SureWalletPassword").focus();
		$("SureWalletPassword").select();
	} else {
		var unencrypted = $F("SureWalletUserName").toLowerCase() + ":" + $F("SureWalletPassword").toLowerCase();
		var encrypted = calcMD5(unencrypted);
		form.Encrypted.value = encrypted;
		if (IsSelected($("SureWalletSaveName"))) form.SaveName.value = "on";
		if (IsSelected($("SureWalletSavePassword"))) form.SavePassword.value = "on";

		var actionUrl = "SignIn.asp?SignIn=True&Source=" + URLencode(location.toString());

		if (GetQSParam("Console") == "0") {
			form.target = "PPMPlayer";
			actionUrl = "SignIn.asp?SignIn=True&Source=" + URLencode("player.asp?Console=0");
		} else {
			form.target = "_self";
		}
		form.action = Sureflix.Utilities.editQSParam(actionUrl, "Console", GetQSParam("Console"));
		form.submit();
	}
}

function SignOut() {
	form = document.thisForm;

	var actionUrl = "SignIn.asp?SignIn=Out&Source=" + URLencode(location.toString());
	form.target = "_self";
	form.action = Sureflix.Utilities.editQSParam(actionUrl,"Console",GetQSParam("Console"));
	form.submit();
}

function ShowWalletSignIn() {
	SureWalletOfferSection.style.display='none';
	SureWalletSignInSection.style.display='block';
	if (thisForm.Name.value.toString().length == 0)	{
		$("SureWalletUserName").focus()
		$("SureWalletUserName").select()
	} else {
		$("SureWalletPassword").focus();
		$("SureWalletPassword").select();
	}
}
	
/** 
 * Opens a wallet link ("Send Password", "New User", etc.) into a frame specified in the query string.
 * @param	{string} pageURL The page to link to.
 */
function WalletLink(pageURL) {
	var walletSource;
	
	var form = document.thisForm;
	var target = location.toString().toQueryParams()["Target"];
	if (target != undefined) {
		form.target = target;
		var targetURL = "";
		//Hernan: Pages outside the domain will give an error when trying to get the document object
		//ex: ccbill
		try{
			var targetDocument = GetFrameDocument(target);
			targetURL = targetDocument.location.pathname + targetDocument.location.search;
		}catch(e){}
		
		walletSource = GetQSValue("Source", targetURL);
		if (walletSource == "") {
			walletSource = targetURL;
		}
		walletSource = URLencode(walletSource);
	} else {
		walletSource = source;
	}
	
	pageURL = Sureflix.Utilities.editQSParam(pageURL, "Source", walletSource);
	if (target != undefined) {
		pageURL = Sureflix.Utilities.editQSParam(pageURL, "Console", "0");
	}
	form.action = pageURL;
	form.submit();
}

			
function SaveNameChanged() {
	ToggleSelect($("SureWalletSaveName"));
	
	//if save name is unselected, save password should also be unselected
	if (!IsSelected($("SureWalletSaveName")) && IsSelected($("SureWalletSavePassword"))) {
		ToggleSelect($("SureWalletSavePassword"));
	}
}
			
function SavePasswordChanged() {
	ToggleSelect($("SureWalletSavePassword"));
	
	//if save password is selected, save name should also be selected
	if (IsSelected($("SureWalletSavePassword")) && !IsSelected($("SureWalletSaveName"))) {
		ToggleSelect($("SureWalletSaveName"));
	}
}