// JavaScript Document
/*
 * Main Ajax call.
 */
var submitChatForm = function(form)
{
	refForm	= form;

	if ( validate(form) ){
		new Ajax.Request(form.action, {
				method:		'POST',   
				parameters: Form.serialize(form),
				onComplete:	resultsAction,
				onFailure:	function(req) {alert("Sorry but there has been an error. Please try again.")}
			});

		disableForm(true);
	}
}

var submitCaptchaForm = function(form)
{
	refForm	= form;
	
	if (validateCaptchaForm(form)) {
		new Ajax.Request(form.action, {
				method:		'POST',   
				parameters: Form.serialize(form),
				onComplete:	captcahResultsAction,
				onFailure:	function(req) {alert("Sorry but there has been an error. Please try again.")}
			});
		
		disableForm(true);
	}
}

var refreshCaptchaBtn = function(show, showId)
{
	var url = '/default/chat/refreshcaptcha/alias/' + show;

	new Ajax.Request(url, {
		method: 'POST',
		onSuccess: function(req, json)
		{
			$('captchaImg_' + showId).src = '/images/captcha/' + json.captchaId + '.png';
			$('captchaId_' + showId).value = json.captchaId;
		}
	});
}

var captcahResultsAction = function(req, json)
{
	disableForm(false);
	startCaptcha(json.captchaId, json.showId, json.codeResultImg);
}

/*
 * resultsAction for the main Ajax call.
 */
var resultsAction = function(req, json)
{
	disableForm(false);
	updateToken(json.token);
	refreshCaptcha(json.captchaId, json.showId, json.submitSuccess, json.captchaPostQuestion, json.captchaPostName, json.userSession);
	displayMessage(json.message);
	//json.captchaId, json.submitSuccess
	
}
 
var validateCaptchaForm = function(form)
{
	msg	= "";
	
	try {
		codeField	= form.elements['captcha[input]'];
		
		if ("Enter Code" == codeField.value) {
			codeField.value	= "";
		}
		
		if (!form.elements['captcha[input]'].value) {
			msg	+= "You must enter the code\n";
		}

	} catch (e) {
		// No question field
	}
	
	if (msg) {
		alert(msg);
		return false;
	}
	
	return true;
}
 
/*
 * Validate the form
 */
var validate = function(form)
{
	msg	= "";
	
	try {
		nameField		= form.elements['post[name]'];
		
		if ("Your name" == nameField.value) {
			nameField.value	= "";
		}
		
		if (!form.elements['post[name]'].value) {
			msg	+= "Please enter your name\n";
		}
	} catch (e) {
		// No name field
	}
	
	try {
		questionField	= form.elements['post[question]'];
		
		if ("Your question" == questionField.value) {
			questionField.value	= "";
		}
		
		if (!form.elements['post[question]'].value) {
			msg	+= "You must enter a question\n";
		}

	} catch (e) {
		// No question field
	}
	
	try {
		codeField	= form.elements['captcha[input]'];
		
		if ("Enter Code" == codeField.value) {
			codeField.value	= "";
		}
		
		if (!form.elements['captcha[input]'].value) {
			msg	+= "You must enter the code\n";
		}

	} catch (e) {
		// No question field
	}
	
	if (msg) {
		alert(msg);
		return false;
	}
	
	return true;
}


/*
 * Tab links for the main chat page
 */
var tab = function(href, divId)
{
	// Clear the links background
	links	= href.parentNode.getElementsByTagName('a');
	for (a = 0; a < links.length; a++) {
		links[a].style.background	= "transparent";
	}
	
	// Set the background color of the selected link
	href.style.background	= "#FFF";
	
	// Hide all the divs
	contentDiv	= document.getElementById(divId);
	contentDivs	= contentDiv.parentNode.getElementsByTagName('div');
	for (a = 0; a < contentDivs.length; a++) {
		contentDivs[a].style.display	= "none";
	}
	
	// Display the selected div
	contentDiv.style.display	= "";
		
	return false;
}


/*
 * Tab links for the main chat page
 */
var tabByClass = function(href, divId)
{
	// Clear the links background
	links	= href.parentNode.getElementsByTagName('a');
	for (a = 0; a < links.length; a++) {
		links[a].className	= "tabUp";
	}
	
	// Set the background color of the selected link
	href.className	= "tabDown";
	
	// Hide all the divs
	contentDiv	= document.getElementById(divId);
	contentDivs	= contentDiv.parentNode.getElementsByTagName('div');
	for (a = 0; a < contentDivs.length; a++) {
		if (contentDivs[a].id) {
			contentDivs[a].style.display	= "none";
		}
	}
	
	// Display the selected div
	contentDiv.style.display	= "";
		
	return false;
}
