﻿
// sets the limit on a textArea control
function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
    // otherwise, update 'characters left' counter
    else 
    countfield.value = maxlimit - field.value.length;
}

// opens the link in an external browser window
function externalLinks() { 
    if (!document.getElementsByTagName) return; 
    var anchors = document.getElementsByTagName("a"); 
    for (var i=0; i < anchors.length; i++) { 
        var anchor = anchors[i]; 
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
            anchor.target = "_blank"; 
        }
    } 
} 

/// Opens a new window with the specified attributes
function openWindow(url, name, width, height) {
    if(width == 0 && height == 0) {
        width = 450;
        height = 320;
    }   
    var win = window.open(url, name,
        'width=' + width + ', ' +
        'height=' + height + ', ' +
        'location=no, menubar=no, ' +
        'status=no, toolbar=no, scrollbars=yes, resizable=yes');
    win.focus();
}

// Returns true if the user to confirms
function confirmation() {
    var agree = confirm("Do you really want to delete this item?");
    if (agree) {
      return true;
    }
    else {
      return false;
    }
}

function checkDelete()
{
    return confirm("Are you sure you want to delete this record? This action cannot be undone.");
}

// Changes the div's visibilty
function ChangeDivVisibility(divName) {
    if (document.getElementById(divName) != null &&
        document.getElementById(divName).style.display == 'block') {
        SetDivVisibility(divName, 'none')
    }
    else {
        SetDivVisibility(divName, 'block')
    }
}

// Changes the div's visibilty
function SetDivVisibility(divName, displayType) {
    document.getElementById(divName).style.display = displayType;
}


function backButtonOverride() {
    // Work around a Safari bug
    // that sometimes produces a blank page
    setTimeout("backButtonOverrideBody()", 1);

}

// Always pushes the user forward again in every major web browser, including Opera, 
// Safari, Firefox, and Internet Explorer
function backButtonOverrideBody() {
    // Works if we backed up to get here
    try {
        history.forward();
    } catch (e) {
        // OK to ignore
    }
    // Every quarter-second, try again. The only
    // guaranteed method for Opera, Firefox,
    // and Safari, which don't always call
    // onLoad but *do* resume any timers when
    // returning to a page
    setTimeout("backButtonOverrideBody()", 500);
}

// Gives the destination element the same value as the sources src location
function changeSourceSrcToDesinationValue(source, destination)
{
    try {
        var source = document.getElementById(source).src;
        document.getElementById(destination).value = source;
    } catch (e) {
        // OK to ignore
    }
}

/// Opens constant contact in another window
function openConstantContact() { 
    win = window.open('http://ui.constantcontact.com/d.jsp?m=1101340626487&p=oi&ea=', 
    'Contact', 'toolbar=no, scrollbars=yes, resizable=yes');
}

function enterPressed(e) {
// Code adapted from Jennifer Madden
// http://jennifermadden.com/162/examples/stringEnterKeyDetector.html

  var characterCode
  if(e && e.which){           // NN4 specific code
    e = e
    characterCode = e.which
  }
  else {
    e = event
    characterCode = e.keyCode // IE specific code
  }
  if (characterCode == 13) return true   // Enter key is 13
  else return false
}