﻿/*
	Usrs.js : Utilidades para Usuarios
*/

// -- Fn_BookQueryCancel
function Fn_BookQueryCancel(ProvPK, BookPK)
{
	if (Ds.$("fConfirmCancel").checked == false)
	{
		alert("Es necesario marcar la casilla \"Confirmar la cancelación de la reserva\".");
		return false;
	}

	window.location = "http://www.imagotel.com/Book/" + ProvPK + "/Cancel/" + BookPK;
	return false;
}

// -- Fn_Suscription
function Fn_Suscription()
{
	if (!ValidateForm("fEmail,fName", false))
		return false;

	return Fn_Submit("formBoletin", "Boletin_Post");
}

// -- Fn_TuCuenta
function Fn_TuCuenta()
{
	if (!ValidateForm("fEmail,fPass", false))
		return false;

	return Fn_Submit("formTCLogin", "TuCuenta_Login");
}

// -- Fn_TCUpdate
function Fn_TCUpdate()
{
	if (!ValidateForm("fName,fEmail,fPass,fNIF,fTlf,fTlfMovil", false))
		return false;

	return Fn_Submit("formTCData", "TuCuenta_Update");
}

// -- Fn_BookInfo
function Fn_BookInfo()
{
	if (!ValidateForm("fEmail,fLoc,fExp", false))
		return false;

	return Fn_Submit("formBookInfo", "BookInfo");
}

// -- Fn_PPoints
function Fn_PPoints()
{
	if (!ValidateForm("fEmail,fPass", false))
		return false;

	return Fn_Submit("formPPoints", "ProgramaDePuntos");
}

// -- Fn_RememberMe
function Fn_RememberMe()
{
	if (!ValidateForm("fRMEmail", true))
		return false;

	return Fn_Submit("formRM", "RememberMe");
}

// -- Fn_Submit : Enviar Formulario
function Fn_Submit(fName, fAction)
{
	ShowWait();

	document.forms[fName].action = "http://www.imagotel.com/Reservas/" + fAction;
	document.forms[fName].submit();

	return false;
}

// -- ValidateForm
function ValidateForm(sFields,bRememberMe)
{
	try
	{
		var aFields = sFields.split(",");

		for (var n = 0; n < aFields.length; n++)
		{
			Field_CheckText(aFields[n]);

			if (bRememberMe)
			{
				if (!ValidateRMPIN())
					return false;
			}
			else
			{
				if (!ValidatePIN())
					return false;
			}
		}
		// --- OK
		return true;
	}
	catch (e) { alert(e); }

	return false;
}

// -- PIN
function ValidatePIN()
{
	Field_CheckText("fPin");

	if (Ds.$("fPin").GetValue().Trim() != Ds.$("fHPin").GetValue())
		throw "PIN incorrecto.";

	return true;
}
function ValidateRMPIN()
{
	Field_CheckText("fRMPin");

	if (Ds.$("fRMPin").GetValue().Trim() != Ds.$("fHRMPin").GetValue())
		throw "PIN incorrecto.";

	return true;
}

// -- Field_CheckText: Genérico
function Field_CheckText(sField)
{
	var e     = Ds.$(sField);
	var sText = "";
	var sVal  = "" + e.GetValue();

	if (null == e.GetValue() || "" == sVal.Trim())
	{
		sText   = "Todos los campos son obligatorios.";
		e.value = ""
		e.Focus();
		throw sText;
	}
}

function ShowWait()
{
	var pDiv = Ds.$(document.createElement("div"));
	//var pImg = document.createElement("img");

	//pImg.setAttribute("src", "/images/intersticial.gif")
	pDiv.SetClassName("Intersticial");

	pDiv.appendChild(Img);
	Ds.$("Lay_BodyHeader").appendChild(pDiv);

	Ds.$("Lay_BodyContent").SetStyle({ display: "none" });
	pDiv.SetStyle({ display: 'block' });
}

var Img = new Image();
Img.src = "/images/intersticial.gif";


