﻿function ErrorBox(id, placeholder, InvalidMaxSalary, InvalidMinSalary, InValidSalaryOrder, InvalidTown, SuggestionsFound, suggestions, LocationBox) {
	this.ErrorBoxID			= id;
	this.PlaceHolder		= placeholder;
	this.LocationBox		= LocationBox;
	placeholder.innerHTML	= this.WriteBox(InvalidMaxSalary, InvalidMinSalary, InValidSalaryOrder, InvalidTown, SuggestionsFound, suggestions, this.LocationBox);
	this.ErrorBoxDiv		= document.getElementById(id);
	this.ErrorBoxDiv.style.display = "none";
}

ErrorBox.prototype.Display = function() {
    $(this.ErrorBoxDiv).fadeIn(400);
}

function CloseErrorBox(id)
{
	document.getElementById(id).style.display = "none";
}

ErrorBox.prototype.WriteBox = function (InvalidMaxSalary, InvalidMinSalary, InValidSalaryOrder, 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();
		}
	}
	
	if(InvalidMaxSalary || InvalidMinSalary || InValidSalaryOrder)
	{
		ErrorBoxHTML += this.GetSalaryError(InvalidMaxSalary, InvalidMinSalary, InValidSalaryOrder);
	}

	ErrorBoxHTML += this.GetBoxEnd();
	return ErrorBoxHTML;
}
	
ErrorBox.prototype.GetLocationHeader = function(TownName)
{
	return "<div class=\"errorMessage\"><b>You entered <span>" + TownName + "</span></b></div>";
}

ErrorBox.prototype.GetLocationNoSuggestions = function()
{
	return "<div class=\"noSuggestions\">No location suggestions found</div>";
}

ErrorBox.prototype.GetLocationSuggestions = function(suggestions)
{
	return "<div class=\"alternativeLocation\"><label for=\"SelectAlternativeLocation\">Please confirm your location from the following suggestions:<br /><br /></label><select id=\"SelectAlternativeLocation\" title=\"Select an alternative:\" onchange=\"Javascript:CopyValue('SelectAlternativeLocation','" + this.LocationBox.id+"','" + this.ErrorBoxID + "')\">" + suggestions + "</select>&nbsp;</div>";
}

ErrorBox.prototype.GetSalaryError = function(InvalidMaxSalary, InvalidMinSalary, InValidSalaryOrder) {
    var SalaryMessage;
    if (InValidSalaryOrder) {
        SalaryMessage = "The maximum salary should be higher than the minimum salary";
    }
    else if (InvalidMaxSalary && InvalidMinSalary) {
        SalaryMessage = "Please enter a valid min and max salary (i.e. numbers with no punctuation)";
    }
    else if (InvalidMaxSalary && !InvalidMinSalary) {
        SalaryMessage = "Please enter a valid maximum salary (i.e. numbers with no punctuation)";
    }
    else {
        SalaryMessage = "Please enter a valid minumum salary (i.e. numbers with no punctuation)";
    }

    return "<div class=\"errorMessage\"><b>" + SalaryMessage + "</b></div>";
}

ErrorBox.prototype.GetBoxHeader = function()
{
	return "<div class=\"errorPopup\" id=\""+ this.ErrorBoxID + "\"><div class=\"heading\"><h3>Note:</h3><span><a href=\"Javascript:CloseErrorBox('" + this.ErrorBoxID + "')\">Close</a></span></div>";
}

ErrorBox.prototype.GetBoxEnd = function()
{
	return "</div>"; 
}

function CopyValue(fromID, toID, ErrorBox)
{
	document.getElementById(toID).value = document.getElementById(fromID).value;
	CloseErrorBox(ErrorBox)
}