﻿// ------------------------------------------------------------------------------------------------
// BrighterTools JavaScript Library
// Copyright © 2009. BrighterTools Limited.
// ------------------------------------------------------------------------------------------------

// Pops up the Html Calendar control for the specfied input control
function popupCalendar(inputControlID)
{
    // Create Calendar
    calendar = new Epoch('calendar', 'popup', document.getElementById(inputControlID));
    calendar.showWeeks = false;
    // Popup Calendar
    calendar.toggle();
    // Focus Input control (this helps with closing the popup when clicking outside the calendar)
    document.getElementById(inputControlID).focus();
}

// Inserts todays date into the specified input control
function insertTodaysDate(inputControlID)
{
    var currentDate = new Date();
    document.getElementById(inputControlID).value = currentDate.getDate().toString() + '/' + (currentDate.getMonth() + 1).toString() + '/' + currentDate.getFullYear().toString();
}

// Removes leading whitespace
function lTrim(value)
{
    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");
}

// Removes ending whitespace
function rTrim(value)
{
    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");
}

// Removes leading and ending whitespace
function trim(value)
{
    return lTrim(rTrim(value));
}

// Removes spaces from the controls value
function removeSpaces(control) 
{
    var originalValue = control.value;
    originalValue = originalValue.split(' ').join('');
    control.value = trim(originalValue);
}

// Removes Spaced from Text
function removeSpacesFromText(text) 
{
    var newValue = text;
    newValue = newValue.split(' ').join('');
    return trim(newValue);
}

// Removes ocurances of specified text from the controls value
function removeText(control, text)
{
    var originalValue = control.value;
    originalValue = originalValue.split(text.toLowerCase()).join('');
    originalValue = originalValue.split(text.toUpperCase()).join('');
    control.value = trim(originalValue);
}

// Returns true if specified value contains only digits, else retuns false
function isNumeric(value)
{
    var regularExpression = /^\d*$/; //Check for entirely digits.
    return regularExpression.test(value);
}

// Gets the Browsers timezone offset in minutes
function getClientTimezoneOffsetInMinutes()
{
    var rightNow = new Date();
    var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
    var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
    var temp = date1.toGMTString();
    var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
    var temp = date2.toGMTString();
    var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
    var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
    var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);
    return hoursDiffStdTime;
}

// Returns true if the clients timzone has daylight saving
function getClientTimezoneHasDaylightSaving()
{
    var rightNow = new Date();
    var date1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
    var date2 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
    var temp = date1.toGMTString();
    var date3 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
    var temp = date2.toGMTString();
    var date4 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
    var hoursDiffStdTime = (date1 - date3) / (1000 * 60 * 60);
    var hoursDiffDaylightTime = (date2 - date4) / (1000 * 60 * 60);
    if (hoursDiffDaylightTime == hoursDiffStdTime)
    {
        return false;
    }
    else 
    {
        return true;
    }
}

function setFavicon(url) {
    var link = document.createElement('link');
    link.type = 'image/x-icon';
    link.rel = 'shortcut icon';
    link.href = url; 
    document.getElementsByTagName('head')[0].appendChild(link);
}
