﻿/// <reference path="jquery-1.2.6.js" />
/// <reference path="../dnn.js" />

/// <summary>
/// GeoReps javascript namespace definition
/// </summary>
var GeoReps = {};

GeoReps.LimitNumberOfCharacters = function(field, max, elementID, message)
{
	var span;

	if (field.value.length > max)
	{
		if (message != "")
		{
			message = String.format(message, max);
			alert(message);
		}
		field.value = field.value.substring(0, max);
	}

	span = $(elementID);
	if (span) span.innerHTML = (max - field.value.length).toString() + " characters left";
}

/// <summary>
/// GeoReps.Web javascript namespace definition
/// </summary>
GeoReps.Web = {};
/// <summary>
/// GeoReps.Web.UI javascript namespace definition
/// </summary>
GeoReps.Web.UI = {};
/// <summary>
/// GeoReps.Web.UI.UserControls javascript namespace definition
/// </summary>
GeoReps.Web.UI.UserControls = {};

/// <summary>
/// GeoReps.generic javascript namespace definition
/// </summary>
GeoReps.generic = {};

GeoReps.generic.list = function()
{
	/// <summary>
	/// Javascript implementation of the .NET generic list object.
	/// </summary>
	this.items = new Array();
}

GeoReps.generic.list.prototype.getItems = function()
{
	/// <summary>
	/// Gets the arrary of items in this generic collection.
	/// </summary>
	return this.items;
}

GeoReps.generic.list.prototype.add = function(item)
{
	/// <summary>
	/// Adds an item to the end of the generic collection.
	/// </summary>
	if (this.items.length != 0)
	{
		var oldlen = this.items.length;
		var tmp = new Array(oldlen + 1);
		var i = 0;
		for (i = 0; i < this.items.length; i++)
		{
			tmp[i] = this.items[i];
		}
		tmp[(tmp.length - 1)] = item;
		this.items = new Array(tmp.length);
		for (i = 0; i < tmp.length; i++)
		{
			this.items[i] = tmp[i];
		}
		tmp = null;
	} 
	else
	{
		this.items = new Array(1);
		this.items[0] = item;
	}
}

GeoReps.generic.list.prototype.insert = function(item, index)
{
	/// <summary>
	/// Inserts an item at the specified index location.
	/// </summary>
	if (this.items.length != 0)
	{
		var oldlen = this.items.length;
		var tmp = new Array(oldlen + 1);
		var i = 0;
		var j = 0;
		for (i = 0; i < tmp.length; i++)
		{
			if (i == index)
			{
				tmp[i] = item;
			} else
			{
				tmp[i] = this.items[j];
				j++;
			}
		}
		this.items = new Array(tmp.length);
		for (i = 0; i < tmp.length; i++)
		{
			this.items[i] = tmp[i];
		}
		tmp = null;
	}
}

GeoReps.generic.list.prototype.addRange = function(objectArray)
{
	/// <summary>
	/// Adds an arrary of objects to the generic collection.
	/// </summary>
	if (this.items.length != 0)
	{
		var oldlen = this.items.length;
		var tmp = new Array(oldlen + objectArray.length);
		var i = 0;
		for (i = 0; i < this.items.length; i++)
		{
			tmp[i] = this.items[i];
		}
		for (i = 0; i < objectArray.length; i++)
		{
			tmp[(i + oldlen)] = objectArray[i];
		}
		this.items = new Array(tmp.length);
		for (i = 0; i < tmp.length; i++)
		{
			this.items[i] = tmp[i];
		}
		tmp = null;
	} 
	else
	{
		var iloop = 0;
		this.items = new Array(objectArray.length);
		for (iloop = 0; iloop < objectArray.length; iloop++)
		{
			this.items[iloop] = objectArray[iloop];
		}
	}
}

GeoReps.generic.list.prototype.find = function(item)
{
	/// <summary>
	/// Finds the specified item in the generic collection.
	/// </summary>
	var index = -1;
	var i = 0;
	for (i = 0; i < this.items.length; i++)
	{
		if (this.compare(this.items[i], item))
		{
			index = i;
			break;
		}
	}
	return index;
}

GeoReps.generic.list.prototype.compare = function(a, b)
{
	/// <summary>
	/// Used by the find and remove functions to determine if objects in the generic collection are equal.
	/// Should be overriden in order to provide comparison of complex objects.
	/// </summary>
	return (a == b);
}

GeoReps.generic.list.prototype.remove = function(item)
{
	/// <summary>
	/// Removes the specified item.
	/// </summary>
	var index = this.find(item);
	if (index != -1)
	{
		var tmp = new Array((this.items.length - 1));
		var i = 0;
		var j = 0;
		for (i = 0; i < this.items.length; i++)
		{
			if (i != index)
			{
				tmp[j] = this.items[i];
				j++;
			}
		}
		this.items = new Array(tmp.length);
		for (i = 0; i < tmp.length; i++)
		{
			this.items[i] = tmp[i];
		}
		tmp = null;
		this.count--;
	}
}

GeoReps.generic.list.prototype.removeAt = function(index)
{
	/// <summary>
	/// Removes an item from the specified index location.
	/// </summary>
	if (this.items.length == 1)
	{
		this.items = null;
		this.items = new Array();
	} 
	else
	{
		var tmp = new Array((this.items.length - 1));
		var i = 0;
		var j = 0;
		for (i = 0; i < this.items.length; i++)
		{
			if (i != index)
			{
				tmp[j] = this.items[i];
				j++;
			}
		}
		this.items = new Array(tmp.length);
		for (i = 0; i < tmp.length; i++)
		{
			this.items[i] = tmp[i];
		}
		tmp = null;
	}
}

GeoReps.generic.list.prototype.join = function(separator)
{
	/// <summary>
	/// Joins the contents of the generic collection using the specified separator.
	/// </summary>
	var i = 0;
	var result = "";
	for (i = 0; i < this.items.length; i++)
	{
		if (i == (this.items.length - 1))
		{
			result += this.items[i];
		} 
		else
		{
			result += this.items[i] + separator;
		}
	}
	return result;
}

GeoReps.generic.list.prototype.clear = function()
{
	/// <summary>
	/// Resets the generic collection back to its default empty state.
	/// </summary>
	this.items = new Array();
}
