function GenericConfirmation(message)
{    
    return (confirm(message));
}

function GenericInformation(message)
{    
    alert(message);
}

function GenericError(message)
{    
    alert(message);
}

function DeleteConfirmation()
{
    return(confirm("Are you sure you wish to Delete?"));
}

function NewWindow(url, name, w, h, scrollbars)
{
    NewWindow(url, name, w, h, scrollbars, 'yes');
}

function NewWindow(url, name, w, h, scrollbars, resizable) 
{
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scrollbars+',resizable='+resizable
    win = window.open(url, name, winprops)
    if (parseInt(navigator.appVersion) >= 4)
        win.window.focus();
}

function GetElementById(id) 
{
    return document.getElementById(id);
}

function showElements(display) 
{
    for (argIndex=1; argIndex<showElements.arguments.length; argIndex++) 
    {
        var element = document.getElementById(showElements.arguments[argIndex]);
        if (element) 
        {
            if (display)
                element.style.display='';
            else
                element.style.display='none';
        }
    }
}

function toggleElements() 
{
    for (argIndex=0; argIndex<toggleElements.arguments.length; argIndex++) 
    {
        var element = document.getElementById(toggleElements.arguments[argIndex]);
        if (element) 
        {
            if (element.style.display=='none')
                element.style.display='';
            else
                element.style.display='none';
        }
    }
}

function toggleElement(elementId) 
{

    var element = document.getElementById(elementId);
    if (element) 
    {
        if (element.style.display=='none')
            element.style.display='';
        else
            element.style.display='none';
    }
}

function toggleImage(imageId, image1, image2) 
{
    var image = document.getElementById(imageId);
    if (image.src.indexOf(image1)==(image.src.length-image1.length)) 
    {
        image.src=image2;
    } 
    else 
    { 
        image.src=image1;
    }
}

function enableElements(enable) 
{
    for (argIndex=1; argIndex<enableElements.arguments.length; argIndex++) 
    {
        var element = document.getElementById(enableElements.arguments[argIndex]);
        if (element) 
        {
            if (enable)
                element.disabled=false;
            else
                element.disabled=true;
        }
    }
} 

function clearSelectOptions(id) 
{
    var select = document.getElementById(id);
    select.options.length = 0;
}

function addSelectOption(id, name, value) 
{
    var select = document.getElementById(id);
    select.options[select.options.length] = new Option(name, value);
}

function getSelectOption(id, index) 
{
    var select = document.getElementById(id);
    return select.options[index]; 
}

function openWindow(href, name, attributes) 
{
    var newWindow = window.open(href,name,attributes);
    newWindow.focus();
} 

function ControlStringEndsWith(text, endsWith) 
{
    if (text==null || endsWith == null) 
    {
        return false;
    }
    if (text.length == 0 || endsWith.length == 0) 
    {
        return false;
    }
    if (text.length < endsWith.length) 
    {
        return false;
    }
    var substrPosition = text.length - endsWith.length;
    return (text.substr(substrPosition) == endsWith);    
}

function GetControlTypeByLastPartOfId(parent, controlType, controlId)
{
    var ctrls = parent.getElementsByTagName(controlType);
    for (i=0; i < ctrls.length; i++)
    {
        if (ControlStringEndsWith(ctrls[i].id, controlId))
        {
            return ctrls[i];
        }
    }
    return null;
}

function ToggleTaskItem(divID,imageId,controlId)
{
    ToggleItem(divID,imageId,"tr",controlId);
}

function ToggleItem(divID, imageId, controlType, controlId)
{        
    var div = GetControlTypeByLastPartOfId(document, 'div', divID);
    if (div)
    {
        //var image = GetControlTypeByLastPartOfId(div, 'img', imageId);
        var image = document.getElementById(imageId);
        var element = GetControlTypeByLastPartOfId(div, controlType, controlId);
        if (element)
        {
            if(image) 
            {
                if (element.style.display=='none')
                {
                    element.style.display='';
                    image.src = '../images/minus.gif';
                }
                else
                {
                    element.style.display='none';
                    image.src = '../images/plus.gif';
                }
            }
            else
                alert("ToggleItem Image:'"+imageId+"' Not Found");
        }
        else
            alert("ToggleItem Element:'"+controlId+"' Not Found");
    }
    else
        alert("ToggleItem Div:'"+divID+"' Not Found");
}

function LimitTextArea(textFieldId, limitNum) 
{
    var obj = GetControlTypeByLastPartOfId(document, 'textarea', textFieldId);
    if (obj != null)
    {
	    if (obj.value.length > limitNum) 
	    {
		    obj.value = obj.value.substring(0, limitNum - 1);
        }
    }
}

function ShowItem(imageId, controlId, hiddenControlId, sectionDescription)
{
    var image = document.getElementById(imageId);
    var control = document.getElementById(controlId);
    var hidden = document.getElementById(hiddenControlId);
    
    if (image && control && hidden)
    {
        if (hidden.value == 'true' || hidden.value == 'True')
        {
            control.style.display='';
            image.src = '../images/minus.gif';
            image.alt = "Hide " + sectionDescription;
            
        }
        else
        {
            control.style.display='none';
            image.src = '../images/plus.gif';
            image.alt = "Show " + sectionDescription;
        }
    }
}

function GetHiddenStatus(hiddenControlId)
{   
    var hidden = document.getElementById(hiddenControlId);
    if (hidden)
    {
        if (hidden.value == 'true' || hidden.value == 'True')
        {
            return true;
        }
    }
    return false;
}

function ToggleItemWithHidden(imageId, controlId, hiddenControlId, sectionDescription)
{   
    var image = document.getElementById(imageId);
    var control = document.getElementById(controlId);
    var hidden = document.getElementById(hiddenControlId);
    
    if (image && control && hidden)
    {
        if (hidden.value == 'true' || hidden.value == 'True')
        {
            control.style.display='none';
            image.src = '../images/plus.gif';
            hidden.value = 'False';
            image.alt = "Show " + sectionDescription;
        }
        else
        {
            control.style.display='';
            image.src = '../images/minus.gif';
            hidden.value = 'True';
            image.alt = "Hide " + sectionDescription;
        }
    }
}

function ForceItemWithHidden(forceValue, imageId, controlId, hiddenControlId, sectionDescription)
{   
    var image = document.getElementById(imageId);
    var control = document.getElementById(controlId);
    var hidden = document.getElementById(hiddenControlId);
    
    if (image && control && hidden)
    {
        if (forceValue)
        {
            control.style.display='none';
            image.src = '../images/plus.gif';
            hidden.value = 'False';
            image.alt = "Show " + sectionDescription;
        }
        else
        {
            control.style.display='';
            image.src = '../images/minus.gif';
            hidden.value = 'True';
            image.alt = "Hide " + sectionDescription;
        }
    }
}

function ToggleActionSpecial(imageId, controlIdList, hiddenControlId, sectionDescription)
{
    forceValue = GetHiddenStatus(hiddenControlId);
    var myArray = controlIdList.split(',');
    while(myArray.length > 0)
    {
        ForceItemWithHidden(forceValue, imageId, myArray.pop(), hiddenControlId, sectionDescription);
    }    
}

function ShowActionSpecialItem(imageId, controlIdList, hiddenControlId, sectionDescription)
{
    var myArray = controlIdList.split(',');
    while(myArray.length > 0)
    {
        ShowItem(imageId, myArray.pop(), hiddenControlId, sectionDescription);
    }    
}

// this function makes checkboxes behave like radio buttons
// this function needs to be registered to onclick event of checkbox
// meanwhile, a hidden field needs to be registerd to store id of current checked checkbox
// in addition, delete button id need to be passed of which is disabled when no selection made
function checkboxInRepeartorOnClickSingleSelection(checkbox, hiddenFieldId, deleteButtonId)
{
    // get hidden field stores id of current selected checkbox
    var hiddenField = document.getElementById(hiddenFieldId);
    // get button which will be hidden/displayed when checkboxes are check/unchecked
    var deleleButton = document.getElementById(deleteButtonId);
    // only interested when hidden field is found
    if (hiddenField != null)
    {
        // if checkbox is checked
        if (checkbox.checked)
        {
            // enable the delete button
            deleleButton.disabled = false;
            
            // get pre checked checkbox id
            var checkboxId = hiddenField.value;
            
            // only interested if is not empty
            if (checkboxId != '')
            {
                // get pre checked checkbox
                var oldCheckbox = document.getElementById(checkboxId);
                
                // set pre checkbox unchecked
                oldCheckbox.checked = false;
            }
            
            // set current checkbox name to hidden field
            hiddenField.value = checkbox.name;
        }
        else
        {
            // disable delete button if its not checked
            deleleButton.disabled = true;
            
            // reset the hidden field
            hiddenField.value = '';
        }
    }
}

//This function hides and displays a button based on the multiple checks in a list
function checkboxInRepeatorOnClickMultipleSelection_HideShow(checkbox, hiddenFieldId, deleteButtonId)
{
    var hiddenField = document.getElementById(hiddenFieldId);
    var deletebutton = document.getElementById(deleteButtonId);
    
    if (hiddenField != null)
    {
        var iVal = parseInt(hiddenField.value);
        
        if (checkbox.checked)
            iVal = iVal + 1;
        else
            iVal = iVal - 1;    
        
        if (iVal > 0)
            deletebutton.style.display="";
        else        
            deletebutton.style.display="none";
            
            
        hiddenField.value = iVal;            
    }
}


// this function enable/disable a button when checkboxes are checked/unchecked
// a hidden field needs to be registered which stores count of checkboxes that checked
// a button id need to be passed into this function so the button can be enabled/disabled
// when count changes
function checkboxInRepeartorOnClickMultipleSelection(checkbox, hiddenFieldId, deleteButtonId)
{
    // get hidden field 
    var hiddenField = document.getElementById(hiddenFieldId);
    // get the button
    var deleleButton = document.getElementById(deleteButtonId);
    
    // only interest when hidden field can be found
    if (hiddenField != null)
    {
        // get value stores in the hidden field
        var iVal = parseInt(hiddenField.value);
        
        // if checkbox is checked
        if (checkbox.checked) // increase count
            iVal = iVal + 1;
        else // decrease otherwise
            iVal = iVal - 1;    
        
        // if count greater than 0, enable button. disable otherwise
        if (iVal > 0)
            deleleButton.disabled = false;
        else
            deleleButton.disabled = true;
           
        // update the hidden field            
        hiddenField.value = iVal;            
    }
}


// this function is to handle email button which brings outlook with email addresses that 
// were concated when checkboxes are check/unchecked
// it requires a hidden field which stores email addresses in a semi column delimetered string.
function checkboxInRepeartorOnClickEmailButton(checkbox, hiddenFieldId, emailButtonId, emailAddress)
{
    // get the hidden field
    var hiddenField = document.getElementById(hiddenFieldId);
    // get email button
    var emailButton = document.getElementById(emailButtonId);
    // only interested when hidden field is not null
    if (hiddenField != null)
    {        
        // get email addresses stored in the hidden field
        var email = hiddenField.value;
        
        // if checkbox is checked
        if (checkbox.checked)
        {
            // append email
            email = email + emailAddress + ";";
        }
        else
        {
            // replace email with empty string
            var reg = emailAddress+";";
            email = email.replace(reg, "");
        }
        
        // disable email button when no email is there, enable otherwise
        if (email == "")
            emailButton.disabled = true;
        else
            emailButton.disabled = false;
            
        // update hidden field
        hiddenField.value = email;            
    }
}


// To assist duration calculation
// this function is to update time for TimeControl and store the value in minutes in a hidden field
// id of textboxes for hour and minute need to be provided. also id of radio buttons for 
// AM and PM need to be provided. a hidden field need to be registered to store the time value
function updateTimeControlTimeHiddenField(hourId, minuteId, amId, pmId, hiddenFieldId)
{
    // get object by id provided
    var hour = document.getElementById(hourId);
    var minute = document.getElementById(minuteId);
    var am = document.getElementById(amId);
    var pm = document.getElementById(pmId);
    var hiddenField = document.getElementById(hiddenFieldId);
    // return from function if any of these object could not be found
    if (hour == null || minute == null || am == null || pm == null || hiddenField == null) return;
    // return from function if hour is not entered
    if (hour.value.length == 0) return;
    // return from function if minute is not entered
    if (minute.value.length == 0) return;
    // convert text value into number value
    var iHour = parseInt(hour.value, 10);
    // convert text value into number value
    var iMinute = parseInt(minute.value, 10);
    // if pm radio button is checked, increase hour by 12
    if (pm.checked)
    {
        if (iHour == 12)
            iHour = 0;
        iHour += 12;
    }
    // convert time into minutes
    var time = iHour * 60 + iMinute;        
    // store value into hidden field
    hiddenField.value = time;
}


// To assist duration calculation
// this function is to calculate duration between start time and finish time, and put the difference
// into duration textbox in format of HH:MM
// two hidden fields need to be registerd for start and finish time respectively. the duration textbox
// id need to be provide for display the time difference
function calculateDuration(startHiddenFieldId, finishHiddenFieldId, durationTextboxId)
{
    // if startHiddenField is not assigned, this is a start time
    if (startHiddenFieldId == '') return;
    // if durationTextboxId is not set, there is no point to calculate
    if (durationTextboxId == '') return;
    // get hidden field for time control
    var startHiddenField = document.getElementById(startHiddenFieldId);
    var finishHiddenField = document.getElementById(finishHiddenFieldId);
    var durationTextbox = document.getElementById(durationTextboxId);
    // return from fuction if hidden field is not registered
    if (startHiddenField == null) return;
    if (finishHiddenField == null) return;
    if (durationTextbox == null) return;
    // return from fucntion if no time value is stored in the hidden field
    if (startHiddenField.value == '') return;
    if (finishHiddenField.value == '') return;   
    // convert text value into number value
    var iStartTime = parseInt(startHiddenField.value, 10);
    var iFinishTime = parseInt(finishHiddenField.value, 10);
    // return from function if currentTime is less than start time.
    if (iFinishTime <= iStartTime) 
    {
        durationTextbox.value = '';
        return;
    }
    // get the time different
    var diff = iFinishTime - iStartTime;
    // set hour to 0
    var iHour = 0;
    // get minute by modulus
    var iMinute = diff % 60;
    if (diff >= 60)
    {
        iHour = (diff - iMinute) / 60
    }
    // return from function if iHour > 24
    if (iHour > 24) return;
    // set hour string
    var hour = iHour.toString();
    if (hour.length == 1)
        hour = '0' + hour;
    // set minute string
    var minute = iMinute.toString();
    if (minute.length == 1)
        minute = '0' + minute;
    // set duration value
    durationTextbox.value = hour + ':' + minute;
}


// this method brings up client's default email programe, most likely Outlook, with to field populated
function sendEmails(hiddenFieldId)
{	    
    // get the hidden field
    var hiddenField = document.getElementById(hiddenFieldId);
    
    // get email address from hidden field
    var emails = hiddenField.value;
    
    // return from function if nothing is stored
    if (emails == "") return;
    
    // bring up the email programe
    document.location.href = "mailto:" + emails;
    
    return false;
}

// this function enable/disable text box
function enableInactiveReason(checkbox, reasonId)
{
    // get the textbox
    var txtReason = document.getElementById(reasonId);
    
    // enable/disable textbox
    txtReason.disabled = !checkbox.checked;
}

// function to test numeric value range
// for textfield onkeypress
function onTimeValidate(min, max) 
{
  var newChar  = String.fromCharCode(window.event.keyCode);
  var input    = window.event.srcElement;
  if (!newChar.match(/[\d]/)) 
  {
    return false;
  }
  if (document.selection && document.selection.type != 'None') 
  {
    return true;
  }
  var newValue = input.value + newChar;
  return (parseInt(newValue) >= min && parseInt(newValue) <= max);
}

function stripNonNumerics(e, textFieldId) 
{   
    var txtBox = document.getElementById(textFieldId).value;
    var newTxt = '';
    
    for (var i = 0; i < txtBox.length; i++) {
        if (txtBox.charAt(i).match(/[\d]/)) {
            newTxt = newTxt + txtBox.charAt(i);
        }    
    }
    if (newTxt != txtBox)
    { 
        document.getElementById(textFieldId).value = newTxt;
    }
}

function stripNonReals(e, textFieldId) 
{   
    var txtBox = document.getElementById(textFieldId).value;
    var newTxt = '';
    
    for (var i = 0; i < txtBox.length; i++) {
        if (txtBox.charAt(i).match(/[\d\.]/)) {
            newTxt = newTxt + txtBox.charAt(i);
        }    
    }
    if (newTxt != txtBox)
    { 
        document.getElementById(textFieldId).value = newTxt;
    }
}

function getKey(event) {
	if (window.event) {
		return window.event.keyCode;
	} else if (event) {
		return event.which;
	}
}

function isControlKey(key) {
	if ((key == null) || (key == 0) || (key == 8) ||
			(key == 9) || (key == 13) || (key == 27)
			|| (key == 37) || (key == 39)) {
		return true;
	}
	return false;
}

// Validate a time in real time.
function validateTimeRealtime(e) {
    return validateRealtime("0123456789:", e);
}

// Validate a date in real time.
function validateDateRealtime(e) {
    return validateRealtime("0123456789/", e);
}

// Validate an integer number in real time.
function validateIntegerRealtime(e) {
    return validateRealtime("0123456789", e);
}

// Validate a real number in real time.
function validateRealRealtime(e) {
    return validateRealtime("0123456789.", e);
}

// validate a field as data is entered. AllowableCharacters is a string
// of the chars that may be entered. e is the event.
// Use as onkeypress="return validateRealtime('chars', event)"
function validateRealtime(allowableCharacters, e) {
    var key = getKey(e);
    var keychar = String.fromCharCode(key).toLowerCase();

    if (isControlKey(key)) {
        return true;
    }

    if (((allowableCharacters).indexOf(keychar) > -1)) {
        return true;
    }
    return false;
}

function displayElements(display) 
{
    for (argIndex=1; argIndex<displayElements.arguments.length; argIndex++) 
    {
        var element = document.getElementById(displayElements.arguments[argIndex]);
        if (element) 
        {
            if (display)
                element.style.display='';
            else
                element.style.display='none';
        }
    }
} 

