﻿
function IsError(html)
{
	/// <summary>Determines whether the given string is a JSON object containing error messages. Performs regular expression match</summary>
	/// <param name="html" type="string">The potential json object in string form</param>

	// Test for Json array using regex
	var match = /^\[\{.*\}\]$/.test(html)
	return match;
}

function IsJsonError(json)
{
	/// <summary>Determines whether the given json object is an array of error messages</summary>
	/// <param name="json" type="object">The potential array of error messages</param>

	//we expect a collection of error messages, so the root object will be an array
	if (json != undefined && json.length > 0 && json[0].ErrorMessage != undefined)
	{
		return true;
	}
	
	return false;
}

function ShowError(errorsJson, customAction)
{
	/// <summary>
	/// Shows specified error in a message box by default, or pass optional customAction(errors) delegate.
	/// Handles a json object in string form
	/// </summary>
	/// <param name="errorsJson" type="string">A JSON object in string form</param>
	/// <param name="customAction" type="function" optional="true">A custom action to perform on the error collection</param>
	
	var errors = eval(errorsJson);

	ShowJsonError(errors, customAction);
}

function ShowJsonError(errors, customAction)
{
	/// <summary>
	/// Shows specified error in a message box by default, or pass optional customAction(errors) delegate.
	/// Handles a json object which is expected to be an array of error messages
	/// </summary>
	/// <param name="errors" type="object">A JSON object - collection of error messages</param>
	/// <param name="customAction" type="function" optional="true">A custom action to perform on the error collection</param>
	
	if (customAction === undefined)
	{
		// Default action: show in message box
		var msg = "";
		for (var i = 0; i < errors.length; i++)
		{
			if (i > 0) msg += "\n";
			msg += errors[i].ErrorMessage;
		}
		alert(msg);
	}
	else
	{
		// Custom action specified
		customAction(errors);
	}
}

function RemoveBad(strTemp) {
    if (strTemp != null)
        strTemp = strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-/g, "");
    return strTemp;
} 
