
var xmlRequest = new xmlObj(false);

/* -------------------------------------------------------------------------------------------------------------------- */
/* $_																													*/
/* -------------------------------------------------------------------------------------------------------------------- */
function $_(id) 
{
	return document.getElementById(id);
}

/* -------------------------------------------------------------------------------------------------------------------- */
/* smart_onFocus																										*/
/* -------------------------------------------------------------------------------------------------------------------- */
function smart_onFocus (oField, emptyValue)
{
	if (oField.value == emptyValue)
	{
		if (oField.id == "enterPassword")
		{
			$_("password").style.display 	  = "";
			$_("enterPassword").style.display = "none";
			$_("password").focus ();
		}
		else
		{
			oField.value = "";
		}
	}
}

/* -------------------------------------------------------------------------------------------------------------------- */
/* smart_onBlur																											*/
/* -------------------------------------------------------------------------------------------------------------------- */
function smart_onBlur (oField, emptyValue)
{
	if (oField.value == "")
	{
		if (oField.id == "password")
		{
			$_("enterPassword").style.display = "";
			$_("password").style.display 	 = "none";
		}
		else
		{
			oField.value = emptyValue;
		}
	}
}

var currLang;

/* -------------------------------------------------------------------------------------------------------------------- */
/* smart_submitTopLogin																									*/
/* -------------------------------------------------------------------------------------------------------------------- */
function smart_submitTopLogin (lang)
{
	var oForm = $_("topLoginForm");

	var formValidator = new Validator("topLoginForm");

	formValidator.clearAllValidations ();
	
	currLang = lang;

	switch (lang)
	{
		case "ENG"	:
			requiredUsername	= "Please enter your name";
			requiredPassword	= "Please enter your password";
			break;

		case "ESP"	:
			requiredUsername	= "Nombre";
			requiredPassword	= "Contraseña";
			break;
	}

	formValidator.addValidation("username",	"required",		requiredUsername);
	formValidator.addValidation("password",	"required",		requiredPassword);

	if (formValidator.validate ())
	{
		var xml	= "<data>" +
						"<command>private.checkLogin</command>" +
						"<username>" + oForm.username.value 	+ "</username>" +
						"<password>" + oForm.password.value 	+ "</password>" +
				  "</data>";

		xmlRequest.init (xml);
		xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "smart_afterCheckLogin");
	}

	return false;
}

/* ----------------------------------------------------------------------------------------------------------------	*/
/* smart_afterCheckLogin																							*/
/* ----------------------------------------------------------------------------------------------------------------	*/
function smart_afterCheckLogin (i)
{
	xmlRequest.init(commonDecode(asyncHttpObjs[i].responseText));

	var oForm = $_("topLoginForm");

	try
	{
		var returnCode 	= xmlRequest.getValue("returnCode");

		if (returnCode == "OK")
		{
			oForm.submit();
			return false;
		}
		else if (returnCode == "WRONG_USERNAME_OR_PASSWORD")
		{
			switch (currLang)
			{
				case "ENG"	: msg = "Wrong login details. Please try again";
							  break;

				case "ESP"	: msg = "Datos incorrectos. Por favor inténtalo de nuevo";
							  break;
			}

			alert (msg);
			oForm.email.focus ();
			return false;
		}
	}
	catch (e)
	{
	}

	return false;
}

/* -------------------------------------------------------------------------------------------------------------------- */
/* smart_submitNewsletterRegister																						*/
/* -------------------------------------------------------------------------------------------------------------------- */
function smart_submitNewsletterRegister (lang, emptyName, emptyEmail)
{
	var oForm = $_("newsLetterForm");

	var formValidator = new Validator("newsLetterForm");

	formValidator.clearAllValidations ();
	
	currLang = lang;

	switch (lang)
	{
		case "ENG"	:
			requiredName		= "Please enter your name";
			requiredEmail		= "Please enter your email";
			validEmail			= "Please enter a valid email";
			break;

		case "ESP"	:
			requiredName		= "Nombre";
			requiredEmail		= "Correo electrónico";
			validEmail			= "Por favor ingrese un email válido";
			break;
	}

	if (oForm.firstname.value == emptyName)
	{
		alert (requiredName);
		oForm.firstname.focus ();
		return false;
	}

	if (oForm.email.value == emptyEmail)
	{
		alert (requiredEmail);
		oForm.email.focus ();
		return false;
	}

	formValidator.addValidation("firstname",	"required",		requiredName);
	formValidator.addValidation("email",		"required",		requiredEmail);
	formValidator.addValidation("email",		"email",		validEmail);

	return formValidator.validate ();
}

/* -------------------------------------------------------------------------------------------------------------------- */
/* smart_submitContactForm																								*/
/* -------------------------------------------------------------------------------------------------------------------- */
function smart_submitContactForm (lang)
{
	var oForm = $_("contactForm");

	var formValidator = new Validator("contactForm");

	formValidator.clearAllValidations ();
	
	currLang = lang;

	switch (lang)
	{
		case "ENG"	:
			requiredName		= "Please enter your name";
			requiredEmail		= "Please enter your email";
			validEmail			= "Please enter a valid email";
			break;

		case "ESP"	:
			requiredName		= "Nombre";
			requiredEmail		= "Correo electrónico";
			validEmail			= "Por favor ingrese un email válido";
			break;
	}

	if (oForm.firstname.value == oForm.emptyName.value)
	{
		alert (requiredName);
		oForm.firstname.focus ();
		return false;
	}

	if (oForm.email.value == oForm.emptyEmail.value)
	{
		alert (requiredEmail);
		oForm.email.focus ();
		return false;
	}

	formValidator.addValidation("firstname",	"required",		requiredName);
	formValidator.addValidation("email",		"required",		requiredEmail);
	formValidator.addValidation("email",		"email",		validEmail);

	if (formValidator.validate ())
	{
		if (oForm.phone.value == oForm.emptyPhone.value)
			oForm.phone.value = "";

		if (oForm.desc.value == oForm.emptyDesc.value)
			oForm.desc.value = "";

		return true;
	}

	return false;

}

/* -------------------------------------------------------------------------------------------------------------------- */
/* smart_showHideTalkbacks																								*/
/* -------------------------------------------------------------------------------------------------------------------- */
function smart_showHideTalkbacks (lang)
{
	var oLink 		= $_("showTalkbacksText");
	var oTalkbacks	= $_("essayTalkbacks");

	switch (lang)
	{
		case "ENG"	: showText	= "View comments";
					  hideText	= "Hide comments";
					  break;

		case "ESP"	: showText	= "Ver comentarios";
					  hideText	= "Ocultar comentarios";
					  break;
	}

	if (oTalkbacks.style.display == "")
	{
		oTalkbacks.style.display	= "none";
		oLink.innerHTML				= showText;
	}
	else
	{
		oTalkbacks.style.display	= "";
		oLink.innerHTML				= hideText;
	}
}

