﻿// Checks to see if the client is Internet Explorer
// Return a boolean (true = IE) (False = Other)
function isIE()
{
    return document.all ? true : false;
}

// Function to work around the inherited problem of related asp:radiobuttons having
// unique groupnames due to their parent nodes implementing the INamingContainer interface
function SetUniqueRadioButton(current)
{ 
   var rootControl = /^\w+/.exec(current.name);
   var groupName = /\w+$/.exec(current.name);
   var regExp = new RegExp(rootControl + '.*' + groupName); 
   
   for(i = 0; i < document.forms[0].elements.length; i++) 
   { 
      e = document.forms[0].elements[i];
      if (e.type == 'radio') 
      { 
         if (regExp.test(e.name)) 
         { 
            e.checked = false; 
         } 
      } 
   } 
   
   current.checked = true; 
}

// Function to retrieve all elements of a specified type from a container element
// For example, get all child nodes in a table which are of type checkbox
function getChildNodesByType(element, type)
{
    var output = new Array();
    
    if (element.hasChildNodes())
        for (var i=0; i<element.childNodes.length; i++)
        {
            if (element.childNodes[i].nodeName == type || element.childNodes[i].type == type)
                output.push(element.childNodes[i]);
            
            if (element.childNodes[i].hasChildNodes())
                output = output.concat(getChildNodesByType(element.childNodes[i], type));
        }

    return output;
}

// Raises an alert to confirm raised click event of a delete button
function confirmDelete(e, title)
{
    var message = "DELETE " + title.toUpperCase() + '\n' +
                  "Are you sure? This " + title.toLowerCase() + " and any related data \n" +
                  "will be irrevocably removed.\n\n" +
                  "" +
                  "OK - Yes, delete this " + title.toLowerCase() + "\n" +
                  "CANCEL - No, keep this " + title.toLowerCase();

    if (!confirm(message))
        if (isIE())
        e.returnValue = false;
    else
        e.preventDefault(true);

}

// Gets the width of the clients browser window (not their screen resolution)
function getWindowWidth() {
		var windowWidth = 0;
		if (typeof(window.innerWidth) == 'number') {
			windowWidth = window.innerWidth;
		}
		else {
			if (document.documentElement && document.documentElement.clientWidth) {
				windowWidth = document.documentElement.clientWidth;
			}
			else {
				if (document.body && document.body.clientWidth) {
					windowWidth = document.body.clientWidth;
				}
			}
		}
		return windowWidth;
	}


// Clears an input field of its default value or sets its default value if it is empty
function handleDefaultValue(sender, defaultValue)
{

    if (sender.value == defaultValue)
        sender.value = '';
    else if (sender.value == '')
        sender.value = defaultValue;

}

//Assesses a string and returns a guaranteed friendly URL
function getSafeUrlFromValue(sender)
{
    return sender.replace(/[^\w_-]+/g, "-").toLowerCase();
}