﻿// If this page is loaded inside a frame, load the page in the top window
//if (top.location != self.location)
//{
//	top.location = self.location;
//}

// 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("Website Error: No 'url' parameter passed to 'Open' JavaScript function.");
		return;
	}

	if (name == null)
	{
		name = "Popup";
	}

	if (features == null)
	{
		features = "width=780,height=468,resizable=yes,scrollbars=yes,left=20,top=20";
	}

	window.open(url, name, features).focus();
}

function zipChangeHandler(id) {
    var element = document.getElementById(id);
    element.value = FormatZipCode(Trim(element.value));
}
function phoneChangeHandler(id) {
    var element = document.getElementById(id);
    element.value = FormatPhone(Trim(element.value));
}
function emailTextChangeHandler(id) {
    var element = document.getElementById(id);
    element.value = Trim(FormatEmailText(element.value));
}
function textChangeHandler(id) {
    var element = document.getElementById(id);
    element.value = Trim(FormatText(element.value));
}
function trimChangeHandler(id) {
    var element = document.getElementById(id);
    element.value = Trim(element.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 e-mail text with all caps to lower case
FormatEmailText = function (Text) {
    returnText = "";
    if (Text.toUpperCase() == Text) {
        var arrText = Text.split(" ");
        for (i = 0; i < arrText.length; i++) {
            if (arrText[i].toUpperCase() == arrText[i]) {
                returnText += arrText[i].substring(0, arrText[i].length).toLowerCase();
            }
            else {
                returnText += arrText[i];
            }
        }
    }
    else {
        returnText = Text;
    }
    return returnText;
}

//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;
}
