


// Replaces the value of a query string variable in the current URL.
// If the variable is not present, it is appended to the end of the URL.
// Returns the new full URL.
function ReplaceQsVariable(queryString, qsVarName, qsVarValue)
{
	try
	{
		var newQs = qsVarName + "=" + qsVarValue;
		var qsRegEx = new RegExp(qsVarName + "\\=[^\\&]*");
		
		var newQs;
		
		if (queryString.match(qsRegEx))
		{
			newQs = queryString.replace(qsRegEx, newQs);
		}
		else
		{
			var qsSeparator = (queryString == "") ? "?" : "&";
			newQs = queryString + qsSeparator + qsVarName + "=" + qsVarValue;
		}
		
		return newQs;
	}
	catch (e)
	{
		alert(e);
		return queryString;
	}
}

function UpdateQsAndRedirect(qsVarName, qsVarValue)
{
	try
	{
		var newQs = ReplaceQsVariable(document.location.search, qsVarName, qsVarValue);
		document.location = document.location.pathname + newQs;
	}
	catch (e)
	{
		alert(e);
	}
}


// Gets an input field whose name ends in [baseID].
function GetInputControl(baseID)
{
	return GetControlByID("input", baseID);
}

// Gets a field whose name ends in [baseID].
// i.e. This function brings back a control by ID, regardless of
//      nesting level of the control (within user controls).
function GetControlByID(tagType, baseID)
{
	var fields = document.getElementsByTagName(tagType);
	
	for (var i = 0; i < fields.length; i++)
	{
		var tag = fields[i];
		var id = tag.id;
		if (id.substr(id.lastIndexOf("_") + 1) == baseID)
		{
			return tag;
		}
	}
	
	return null;
}

function GetControlByName(tagType, baseID)
{
	var fields = document.getElementsByTagName(tagType);
	
	for (var i = 0; i < fields.length; i++)
	{
		var tag = fields[i];
		var name = tag.name;
		if (name.substr(name.lastIndexOf("_") + 1) == baseID)
		{
			return tag;
		}
	}
	
	return null;
}

function OpenSendMail(urlInput)
{
    var div = GetControlByID('div','divSendEmail');
    var url = GetControlByID('input','url');
    div.style.visibility = 'Visible';
    div.style.top = getScrollTop() + 100;
    url.value = urlInput;
}
function HideSendMail()
{
    var div = GetControlByID('div','divSendEmail');
    div.style.visibility = 'Hidden';
    return false;
}

function BeforePost()
{
    var txtYourEmail = GetControlByID('input','txtYourEmail');
    var txtFriendEmail = GetControlByID('input','txtFriendEmail');

    if (txtFriendEmail.value == '' || txtYourEmail.value == '')
    {
        alert('**Please supply TO & FROM email addresses**');
        return false;
    }
    return true;
}

function OpenContactSendMail(urlInput, modeInput, idInput)
{
    var div = GetControlByID('div','divContactSendEmail');
    var url = GetControlByID('input','urlContact');
    var mode = GetControlByID('input','mode');
    var id = GetControlByID('input','eventID');
    div.style.visibility = 'Visible';
    div.style.top = getScrollTop() + 100;
    url.value = urlInput;
    mode.value = modeInput;
    id.value = idInput;
}
function HideContactSendMail()
{
    var div = GetControlByID('div','divContactSendEmail');
    div.style.visibility = 'Hidden';
    return false;
}
function ShowContactSendMail()
{
    var div = GetControlByID('div','divContactSendEmail');
    div.style.visibility = 'Visible';
    alert("Incorrect validation code, please try again");
}
function ContactBeforePost()
{
    var txtYourEmail = GetControlByID('input','txtContactYourEmail');

    if (txtYourEmail.value == '')
    {
        alert('**Please email addresses**');
        return false;
    }
    return true;
}

function getScrollTop()
{
if (document.documentElement && !document.documentElement.scrollTop)
    return document.body.scrollTop;
// IE6 +4.01 but no scrolling going on

else if (document.documentElement && document.documentElement.scrollTop)
    return document.documentElement.scrollTop;

// IE6 +4.01 and user has scrolled
else if (document.body && document.body.scrollTop)
    return document.body.scrollTop;
}

function OpenJoinEvent(urlInput, idInput)
{
    var div = GetControlByID('div','divJoinEvent');
    var url = GetControlByID('input','urlJoinEvent');
    var id = GetControlByID('input','eventIDJoinEvent');
    div.style.visibility = 'Visible';
    div.style.top = getScrollTop() + 100;
    url.value = urlInput;
    id.value = idInput;
}

function HideJoinEvent()
{
    var div = GetControlByID('div','divJoinEvent');
    div.style.visibility = 'Hidden';
    return false;
}

function JoinEventBeforePost()
{
    var txtYourEmail = GetControlByID('input','txtJoinEventYourEmail');

    if (txtYourEmail.value == '')
    {
        alert('**Please email addresses**');
        return false;
    }
    return true;
}



function checkEnter(e)
{ //e is event object passed from function invocation
	try
	{
		var characterCode // literal character code will be stored in this variable

		if(e && e.which)
		{ 
			//if which property of event object is supported (NN4)
			e = e
			characterCode = e.which //character code is contained in NN4's which property
		}
		else
		{
			e = event
			characterCode = e.keyCode //character code is contained in IE's keyCode property
		}

		if(characterCode == 13)
		{ 
			//if generated character code is equal to ascii 13 (if enter key)
			PerformSearch();
			return false 
		}
		else
		{
			return true 
		}
	}
	catch(e)
	{
		alert(e);
		return false;
	}
}

function ClearText(obj)
{
    if (obj.value == obj.title) obj.value = "";
}

function PopulateText(obj)
{
    
    if (obj.value == "") obj.value = obj.title;
}

function searchclear()
{
	//var val = document.forms[0].elements["HeaderControl1_SearchControl1_txtSearch"];
	var val = GetControlByID("input", "txtSearch");
	if(val.value == "Search")
	{
		val.value = "";
	}
}
function searchpopulate()
{
	//var val = document.forms[0].elements["HeaderControl1_SearchControl1_txtSearch"]
	var val = GetControlByID("input", "txtSearch");
	if (val.value == "") val.value = "Search";
}

function URLEncode(text)
{
	try
	{
		// The Javascript escape and unescape functions do not correspond
		// with what browsers actually do...
		var SAFECHARS = "0123456789" +					// Numeric
						"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
						"abcdefghijklmnopqrstuvwxyz" +
						"-_.!~*'()";					// RFC2396 Mark characters
		var HEX = "0123456789ABCDEF";

		var plaintext = text;
		var encoded = "";
		for (var i = 0; i < plaintext.length; i++ ) {
			var ch = plaintext.charAt(i);
			if (ch == " ") {
				encoded += "+";				// x-www-urlencoded, rather than %20
			} else if (SAFECHARS.indexOf(ch) != -1) {
				encoded += ch;
			} else {
				var charCode = ch.charCodeAt(0);
				if (charCode > 255) {
					alert( "Unicode Character '" 
							+ ch 
							+ "' cannot be encoded using standard URL encoding.\n" +
							"(URL encoding only supports 8-bit characters.)\n" +
							"A space (+) will be substituted." );
					encoded += "+";
				} else {
					encoded += "%";
					encoded += HEX.charAt((charCode >> 4) & 0xF);
					encoded += HEX.charAt(charCode & 0xF);
				}
			}
		} // for

		return encoded;
	}
	catch (e)
	{
		alert(e);
		return text;
	}
};
