﻿// If this page is loaded inside a frame, load the page in the top window
var topHost = top.location.hostname.toLowerCase();
if (topHost.indexOf(".irmi.com") > -1) {
    if (top.location != self.location)
    { top.location = self.location; }
}
// Get cookie value
function getCookie(name) {
    var cookie = "";
    var all_cookies = document.cookie;
    var label = name + "=";
    var offset = all_cookies.indexOf(label);
    if (offset != -1) {
        var start = offset + label.length;
        var end = all_cookies.indexOf(";", start);
        if (end == -1)
        { end = all_cookies.length; }
        cookie = all_cookies.substring(start, end);
    }
    cookie = unescape(cookie);
    return cookie;
}
function setPermanentCookie(name, value) {
    var theDate = new Date();
    var oneYearLater = new Date(theDate.getTime() + 31536000000);
    var expiryDate = oneYearLater.toGMTString();
    document.cookie = name + '=' + value + ';expires=' + expiryDate + ';path=/';
}
function setCookie(name, value) {
    document.cookie = name + '=' + value + ';path=/';
}
function jsem(value) {
    document.write('<a href="mailto:' + value + '">' + value + '</a>');
}
// Add page to Favorites
function AddToFavorites()
{ window.external.AddFavorite(location.href, document.title); }
// Close window
function Close()
{ window.close(); }
// E-mail page
function Email()
{ window.navigate("mailto:?subject=Information from IRMI&body=Here is something you might find interesting from IRMI: " + location.href); }
// Print page
function Print()
{ window.print(); }
// Open window
function Open(url, name, features) {
    if (url == null) {
        window.alert("Web Site Error: No 'url' parameter passed to 'Open' JavaScript function.");
        return;
    }
    if (name == null)
    { name = "Popup"; }
    if (features == null)
    { features = "width=780,height=560,resizable=yes,scrollbars=yes,left=20,top=20"; }
    window.open(url, name, features).focus();
}
function ClickSearchButton(buttonName, e) {
    //the purpose of this function is to allow the enter key to 
    //point to the correct button to click.
    var key;

    if (window.event)
        key = window.event.keyCode;     //IE
    else
        key = e.which;     //firefox

    if (key == 13) {
        //Get the button the user wants to have clicked
        var btn = document.getElementById(buttonName);
        if (btn != null) { //If we find the button click it
            btn.click();
            event.keyCode = 0
        }
    }
}
function zipChangeHandler(id) {
    var myElement = document.getElementById(id);
    myElement.value = FormatZipCode(Trim(myElement.value));
}
function phoneChangeHandler(id) {
    var myElement = document.getElementById(id);
    myElement.value = FormatPhone(Trim(myElement.value));
}
function textChangeHandler(id) {
    var myElement = document.getElementById(id);
    myElement.value = FormatText(Trim(myElement.value));
}
function trimChangeHandler(id) {
    var myElement = document.getElementById(id);
    myElement.value = Trim(myElement.value);
}
//trim spaces for beginning and end of a string
Trim = function(str) {
    str = str.replace(/^\s+/, '');
    for (var i = str.length - 1; i >= 0; i--) {
        if (/\S/.test(str.charAt(i))) {
            str = str.substring(0, i + 1);
            break;
        }
    }
    return str;
}
//formats 5 and 9 digit zip codes
FormatZipCode = function(Zip) {
    var zipReturn = Zip;
    // If the field exists and is not null
    if (Zip != null) {
        // Remove any non-numeric characters
        Zip = Zip.replace(/[^0-9]/g, "");

        // If the number is a length we expect and support, format the number
        switch (Zip.length) {
            case 5:
                zipReturn = Zip;
                break;

            case 9:
                zipReturn = Zip.substr(0, 5) + "-" + Zip.substr(5, 8);
                break;
        }
    }
    return zipReturn;
}
//formats the phone number without an extension
FormatPhone = function(Phone) {
    var phoneReturn = Phone;

    if (Phone != null) {
        // If the number is a length we expect and support, format the number
        Phone = Phone.replace(/[^0-9]/g, "");
        Phone = Phone.replace(/^1/, "");
        if (Phone.length >= 10) {
            phoneReturn = "(" + Phone.substr(0, 3) + ") " + Phone.substr(3, 3) + "-" + Phone.substr(6, 4);
            if (Phone.length > 10) {
                phoneReturn += " x " + Phone.substr(10, (Phone.length - 10));
            }
        }
    }
    return phoneReturn;
}

//reformats text with all caps or all lower case to proper case
FormatText = function(Text) {
    returnText = "";
    if (Text.toUpperCase() == Text || Text.toLowerCase() == Text) {
        var arrText = Text.split(" ");
        for (i = 0; i < arrText.length; i++) {
            if (arrText[i].toUpperCase() == arrText[i] || arrText[i].toLowerCase() == arrText[i]) {
                returnText += arrText[i].substring(0, 1).toUpperCase() + arrText[i].substring(1, arrText[i].length).toLowerCase() + " ";
            }
            else {
                returnText += arrText[i] + " ";
            }
        }
    }
    else {
        returnText = Text;
    }
    return returnText;
}

// Create a cookie with javascript to differentiate between users and search engines
if (getCookie('SystemStatus') == '') { setPermanentCookie('SystemStatus', '0'); }

