﻿function CandidateErrorBox(id, placeholder, InvalidTown, SuggestionsFound, suggestions, LocationBox)
{
	this.ErrorBoxID			= id;
	this.PlaceHolder		= placeholder;
	this.LocationBox		= LocationBox;
	placeholder.innerHTML	= this.WriteBox(InvalidTown, SuggestionsFound, suggestions, this.LocationBox);
	this.ErrorBoxDiv		= document.getElementById(id);
	changeOpac(0, this.ErrorBoxID) 
	
}

CandidateErrorBox.prototype.Display = function()
{	
	this.ErrorBoxDiv.style.display = "inline";
	opacity(this.ErrorBoxID, 0, 100, 400);
	
}

function CloseErrorBox(id)
{
	document.getElementById(id).style.display = "none";
}

//Opacity stuff
function opacity(id, opacStart, opacEnd, millisec) 
{ 
	//speed for each frame 
	var speed = Math.round(millisec / 100); 
	var timer = 0; 

	//determine the direction for the blending, if start and end are the same nothing happens 
	if(opacStart > opacEnd) { 
		for(i = opacStart; i >= opacEnd; i--) { 
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
			timer++; 
		} 
	} else if(opacStart < opacEnd) { 
		for(i = opacStart; i <= opacEnd; i++) 
			{ 
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
			timer++; 
		} 
	} 
} 

//change the opacity for different browsers 
function changeOpac(opacity, id) 
{
	try
	{
		var object = document.getElementById(id).style; 
		object.display = "inline";
		object.opacity = (opacity / 100); 
		object.MozOpacity = (opacity / 100); 
		object.KhtmlOpacity = (opacity / 100); 
		object.filter = "alpha(opacity=" + opacity + ")";
	}catch(ex){}
}

CandidateErrorBox.prototype.WriteBox = function (InvalidTown, SuggestionsFound, suggestions,LocationBox)
{
	var ErrorBoxHTML = this.GetBoxHeader();
	
	if(InvalidTown)
	{
		ErrorBoxHTML +=	this.GetLocationHeader(LocationBox.value);
		if(SuggestionsFound)
		{
			ErrorBoxHTML += this.GetLocationSuggestions(suggestions);
		}
		else
		{
			ErrorBoxHTML += this.GetLocationNoSuggestions();
		}
	}

	ErrorBoxHTML += this.GetBoxEnd();
	return ErrorBoxHTML;
}
	
CandidateErrorBox.prototype.GetLocationHeader = function(TownName)
{
	return "<div class=\"errorMessage\"><b>We could not recognise <span>" + TownName + "</span></b></div>";
}

CandidateErrorBox.prototype.GetLocationNoSuggestions = function()
{
	return "<div class=\"noSuggestions\">No suggestions found</div>";
}

CandidateErrorBox.prototype.GetLocationSuggestions = function(suggestions)
{
	return "<div class=\"alternativeLocation\"><label for=\"SelectAlternativeLocation\">Please select an alternative:</label><select id=\"SelectAlternativeLocation\" title=\"Select an alternative:\" onchange=\"Javascript:CopyValue('SelectAlternativeLocation','" + this.LocationBox.id+"','" + this.ErrorBoxID + "')\">" + suggestions + "</select>&nbsp;</div>";
}

CandidateErrorBox.prototype.GetBoxHeader = function()
{
	return "<div class=\"errorPopup\" id=\""+ this.ErrorBoxID + "\"><div class=\"heading\"><h3>Validation error<span><a href=\"Javascript:CloseErrorBox('" + this.ErrorBoxID + "')\"><img src=\"/resources/images/core/validationErrorClose.gif\" alt=\"close\" height=\"9\" width=\"9\" /></a></span></h3></div>";
}

CandidateErrorBox.prototype.GetBoxEnd = function()
{
	return "</div>"; 
}

function CopyValue(fromID, toID, ErrorBox)
{
	document.getElementById(toID).value = document.getElementById(fromID).value;
	CloseErrorBox(ErrorBox)
}