// SMStringUtilities

function SMStringUtilities()
{
}

SMStringUtilities.Trim = function(str)
{
	var whitespaces = new Array(" ", "\n", "\r", "\t");
	var changed = true;
	
	while (changed === true)
	{
		changed = false;
		
		for (var i = 0 ; i < whitespaces.length ; i++)
		{
			while (str.substring(0, 1) === whitespaces[i])
			{
				str = str.substring(1, str.length);
				changed = true;
			}
			
			while (str.substring(str.length - 1, str.length) === whitespaces[i])
			{
				str = str.substring(0, str.length - 1);
				changed = true;
			}
		}
	}
	
	return str;
}

SMStringUtilities.ReplaceAll = function(str, search, replace)
{
	var offset = 0;
	var index = str.indexOf(search, offset);
	
	while (index > -1)
	{
		str = SMStringUtilities.Replace(str, search, replace, offset);
		
		offset = ((replace.length > 0) ? index + replace.length : index);
		index = str.indexOf(search, offset);
	}
	
	return str;
}

SMStringUtilities.Replace = function(str, search, replace, offset)
{
	var startText = str.substring(0, offset);
	var endText = str.substring(offset);
	
	endText = endText.replace(search, replace);
	
	return startText + endText;
}

// SMDom

function SMDom()
{
}

SMDom.GetInnerValue = function(elmId)
{
	var elm = this.GetElement(elmId);
	
	if (elm === null)
		return null;
	
	return elm.innerHTML;
}

SMDom.SetAttribute = function(elmId, attr, value)
{
	var elm = this.GetElement(elmId);
	
	if (elm === null)
		return false;
	
	if (attr.toLowerCase() === "value")
		elm.value = value;
	else if (attr.toLowerCase() === "checked")
		elm.checked = value;
	else
		elm.setAttribute(attr, value);
	
	return true;
}

SMDom.GetAttribute = function(elmId, attr)
{
	var elm = this.GetElement(elmId);
	
	if (elm === null)
		return null;
	
	if (attr.toLowerCase() === "value")
		return elm.value;
	else if (attr.toLowerCase() === "checked")
		return elm.checked;
	
	return elm.getAttribute(attr);
}

SMDom.SetStyle = function(elmId, property, value)
{
	var elm = this.GetElement(elmId);
	
	if (elm === null)
		return false;
	
	if (SMBrowser.GetBrowser() === "MSIE")
	{
		if (property.toLowerCase() === "display" && (value === "inherit" || value === "inline-table" || value === "run-in" || value === "table" || value === "table-caption" || value === "table-cell" || value === "table-column" || value === "table-column-group" || value === "table-row" || value === "table-row-group"))
			value = "block";
	}
	
	elm.style[property] = value;
	
	return true;
}

SMDom.GetStyle = function(elmId, property)
{
	var elm = this.GetElement(elmId);
	
	if (elm === null)
		return null;
	
	return elm.style[property];
}

SMDom.ElementExists = function(id)
{
	var elm = document.getElementById(id);
	
	if (elm !== null)
		return true;
	else
		return false;
}

SMDom.GetElement = function(id)
{
	var elm = document.getElementById(id);
	
	if (elm === null)
		return null;
	
	return elm;
}

// SMEventHandler

function SMEventHandler()
{
}

SMEventHandler.AddEventHandler = function(element, event, eventFunction)
{
	if (element.addEventListener)
		element.addEventListener(event, eventFunction, false);
	else if (element.attachEvent)
		element.attachEvent("on" + event, eventFunction);
}

// SMCookie

function SMCookie()
{
}

SMCookie.SetCookie = function(name, value, seconds)
{
	if (value.indexOf(';') > -1)
	{
		alert("Unable to set cookie - value contains illegal character: ';'");
		return false;
	}
	
	var date = new Date();
	date.setTime(date.getTime() + (seconds * 1000));
	document.cookie = name + "=" + value + "; expires=" + date.toGMTString() + "; path=/";
	
	return true;
}

SMCookie.GetCookie = function(name)
{
	var name = name + "=";
	var cookies = document.cookie.split(";");
	var cookie = null;
	
	for (i = 0 ; i < cookies.length ; i++)
	{
		cookie = cookies[i];
		
		while (cookie.charAt(0) === " ")
			cookie = cookie.substring(1, cookie.length);
		
		if (cookie.indexOf(name) === 0)
			return cookie.substring(name.length, cookie.length);
	}
	
	return null;
}

SMCookie.RemoveCookie = function(name)
{
	return this.SetCookie(name, "", -1);
}

// SMMessageDialog

function SMMessageDialog()
{
}

SMMessageDialog.ShowMessageDialog = function(content)
{
	alert(content);
}

SMMessageDialog.ShowMessageDialogOnLoad = function(content)
{
	SMEventHandler.AddEventHandler(window, "load", function() { alert(content); });
}

SMMessageDialog.ShowConfirmDialog = function(content)
{
	return confirm(content);
}

SMMessageDialog.ShowInputDialog = function(content, value)
{
	return prompt(content, value);
}

// SMWindow

function SMWindow(identifier)
{
	this.id = identifier;
	this.url = "";
	this.content = "";
	this.onload = null;
	this.width = 320;
	this.height = 240;
	this.displayToolBar = false;
	this.displayLocation = false;
	this.displayMenuBar = false;
	this.displayStatusBar = false;
	this.displayScrollBars = true;
	this.resizable = true;
	this.positionLeft = null;
	this.positionTop = null;
	this.centerWindow = true;
	
	this.instance = null;
	
	this.SetUrl = function(url)
	{
		this.url = url;
	}
	
	this.SetContent = function(content)
	{
		this.content = content;
	}
	
	this.SetOnload = function(func)
	{
		this.onload = func;
	}
	
	this.SetSize = function(width, height)
	{
		this.width = width;
		this.height = height;
	}
	
	this.SetDisplayToolBar = function(value)
	{
		this.displayToolBar = value;
	}
	
	this.SetDisplayLocation = function(value)
	{
		this.displayLocation = value;
	}
	
	this.SetDisplayMenuBar = function(value)
	{
		this.displayMenuBar = value;
	}
	
	this.SetDisplayStatusbar = function(value)
	{
		this.displayStatusbar = value;
	}
	
	this.SetDisplayScrollBars = function(value)
	{
		this.displayScrollBars = value;
	}
	
	this.SetResizable = function(value)
	{
		this.resizable = value;
	}
	
	this.SetPosition = function(pixelsLeft, pixelsTop)
	{
		this.positionLeft = pixelsLeft;
		this.positionTop = pixelsTop;
	}
	
	this.SetCenterWindow = function(value)
	{
		this.centerWindow = value;
	}
	
	this.Show = function()
	{
		if (this.centerWindow === true)
		{
			this.positionLeft = (screen.width / 2) - (this.width / 2);
			this.positionTop = (screen.height / 2) - (this.height / 2);
		}
		
		var options = "width=" + this.width + ",height=" + this.height;
		
		options += ",toolbar=" + ((this.displayToolBar === true) ? "yes" : "no");
		options += ",location=" + ((this.displayLocation === true) ? "yes" : "no");
		options += ",menubar=" + ((this.displayMenuBar === true) ? "yes" : "no");
		options += ",status=" + ((this.displayStatusBar === true) ? "yes" : "no");
		options += ",scrollbars=" + ((this.displayScrollBars === true) ? "yes" : "no");
		options += ",resizable=" + ((this.resizable === true) ? "yes" : "no");
		
		if (this.positionLeft !== null)
			options += ",left=" + this.positionLeft;
		if (this.positionTop !== null)
			options += ",top=" + this.positionTop;
		
		this.instance = window.open(this.url, this.id, options);
		
		if (this.instance == null)
			alert("Unable to open window - please disable pop-up blocker if installed");
		
		if (this.content !== "")
		{
			this.instance.document.write(this.content);
			this.instance.document.close();
		}
		
		if (this.onload !== null)
			this.instance.onload = this.onload;
	}
	
	this.Close = function()
	{
		if (this.instance === null)
			return;
		
		this.instance.close();
	}
	
	this.GetInstance = function()
	{
		return this.instance;
	}
}

// SMHttpRequest

function SMHttpRequest(url, async) // url, true|false
{
	this.url = url;
	this.async = async;
	
	this.httpRequest = getHttpRequestObject();
	this.usingCustomHeaders = false;
	this.data = null;
	
	this.AddHeader = function(key, value)
	{
		this.httpRequest.setRequestHeader(key, value)
		this.usingCustomHeaders = true;
	}
	
	this.SetData = function(data)
	{
		this.data = data;
	}
	
	this.Start = function()
	{
		var method = ((this.data == null || this.data == "") ? "GET" : "POST");
		this.httpRequest.open(method, this.url, this.async);
		
		if (method == "POST" && this.usingCustomHeaders == false)
			this.httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		
		this.httpRequest.send(this.data);
	}
	
	this.GetResponseXml = function()
	{
		return this.httpRequest.responseXML;
	}
	
	this.GetResponseText = function()
	{
		return this.httpRequest.responseText;
	}
	
	this.SetStateListener = function(func)
	{
		this.httpRequest.onreadystatechange = func;
	}
	
	this.GetCurrentState = function() // 0 = unsent, 1 = opened, 2 = headers received, 3 = loading, 4 = done
	{
		return this.httpRequest.readyState;
	}
	
	this.GetHttpStatus = function()
	{
		return this.httpRequest.status;
	}
	
	function getHttpRequestObject()
	{
		if (window.XMLHttpRequest)
			return new XMLHttpRequest();
		else if (window.ActiveXObject)
			return new ActiveXObject("Microsoft.XMLHTTP");
		else
		{
			alert("Http Request object not supported");
			return null;
		}
	}
}

// SMBrowser

function SMBrowser()
{
}

SMBrowser.GetBrowser = function()
{
	if (navigator.userAgent.indexOf("Safari") > 0)
		return "Safari";
	if (navigator.userAgent.indexOf("MSIE") > 0)
		return "MSIE";
	if (navigator.userAgent.indexOf("Mozilla") > 0)
		return "Firefox";
	
	return "Unknown";
}
