﻿var isIE = (window.navigator.userAgent.indexOf("MSIE") > 0);

   if (! isIE) {
      HTMLElement.prototype.__defineGetter__("innerText", 
              function () { return(this.textContent); });
      HTMLElement.prototype.__defineSetter__("innerText", 
              function (txt) { this.textContent = txt; });
   }

/***************************************************
Implement new functionality for the string object
***************************************************/
String.prototype.isValidDate = function() {
    var IsoDateRe = new RegExp("^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4}|[0-9]{2})$");
    var matches = IsoDateRe.exec(this);
    if (!matches) return false;  
    var yearParsed = "";
    if (matches[3].length == 2)
    {
        if (parseInt(matches[3]) < 50)
            yearParsed = "20" + matches[3];
        else
            yearParsed = "19" + matches[3];
    }
    else
        yearParsed = matches[3];
    
    if (parseInt(yearParsed) < 1753 || parseInt(yearParsed) > 9999)
        return false;
        
    var composedDate = new Date(yearParsed, (matches[1] - 1), matches[2]);
    return ((composedDate.getMonth() == (matches[1] - 1)) &&
          (composedDate.getDate() == matches[2]) &&
          (composedDate.getFullYear() == yearParsed));
}
String.prototype.isValidInteger = function() {
    var regExPattern = new RegExp("^(0|-?[1-9][0-9]*)$");
    if (!regExPattern.exec(this))
        return false;
    else
        return true;
}
String.prototype.isValidPrice = function() {
    var pricePattern = new RegExp("^-?(0(\.[0-9]{2})?|[1-9][0-9]*(\.[0-9]{2})?)$");
    if (!pricePattern.exec(this))
        return false;
    else
        return true;
}
String.prototype.isValidTime = function() {
    var pricePattern = new RegExp("^(0[1-9]|1[0-9]):[0-9]{2} (AM|PM)$");
    if (!pricePattern.exec(this))
        return false;
    else
        return true;
}
String.prototype.isValidZipCode = function() {
    var pricePattern = new RegExp("^[0-9]{5}(-[0-9]{4})?$");
    if (!pricePattern.exec(this))
        return false;
    else
        return true;
}
String.prototype.isValidEmail = function() {
    var emailPattern = new RegExp("^([0-9a-zA-Z]([-.\w_]*[0-9a-zA-Z])*@(([0-9a-zA-Z])+([-\w]*[0-9a-zA-Z])*\.)+[a-zA-Z]{2,9})$");
    if (!emailPattern.exec(this))
        return false;
    else
        return true;
}

String.prototype.nl2br = function ()
{
    // Replace out the new line character.
    return this.replace(/\n/g, "<br />");
}

function ShowCharsLeftForMultiLineTextBoxes(evt, obj, charsLeftArea, maxLength) {
    obj             = $(obj);
    charsLeftArea   = $(charsLeftArea);
	var nCharLeft = maxLength - obj.value.length;
	if (nCharLeft < 0)
	{
		 obj.value =  obj.value.substring(0, maxLength);
		 nCharLeft = 0;
	}
    charsLeftArea.update(nCharLeft + " character(s) remaining");
};

function WireUpMultiLineTextBoxesToShowCharsLeft()
{
    var multiLineTextBoxes = $A(document.getElementsByClassName("multiline-charsleft"));
    multiLineTextBoxes.each(
        function(o)
        {
            // Check if this is a text area
            if (o.type == "textarea")
            {
                var maxLength = parseInt(o.title);
                if (!isNaN(maxLength))
                {
                    // Create an HTML element to show chars left
                    new Insertion.After(o.id, "<div id='" + o.id + "CharsLeft'></div>");
                    // Event handlers
                    Event.observe(o.id, 'change', ShowCharsLeftForMultiLineTextBoxes.bindAsEventListener(this, o, $(o.id + 'CharsLeft'), maxLength));
                    Event.observe(o.id, 'keyup', ShowCharsLeftForMultiLineTextBoxes.bindAsEventListener(this, o, $(o.id + 'CharsLeft'), maxLength));
                    Event.observe(o.id, 'keydown', ShowCharsLeftForMultiLineTextBoxes.bindAsEventListener(this, o, $(o.id + 'CharsLeft'), maxLength));
                    // Do the initial call
                    ShowCharsLeftForMultiLineTextBoxes(this, o, $(o.id + 'CharsLeft'), maxLength);
                }
            }
        }
    );
}

function WireUpProfileArchiveLinks(profileArchiveManager) {
    var profileFields = $A(document.getElementsByClassName("profile-archive-field"));
    profileFields.each(
        function(o) {
            if (o.type == "textarea") {
                // create HTML element to for profile archive link
                new Insertion.After(o.id, "<a href='javascript: void(0);' id='" + o.id + "ProfileArchiveLink'>See profile history</a>");

                var fieldName = o.id.substring(o.id.lastIndexOf('_') + 1).replace('TextBox', '');

                // wire up click event to the link control
                Event.observe(o.id + 'ProfileArchiveLink', 'click', profileArchiveManager.displayArchiveProfileHandler.bindAsEventListener(this, profileArchiveManager, profileArchiveManager.memberId, fieldName, 3, 0));
            }
        }
    );
}

function openNewWin(page, win_name, width, height) 
	{
	var bName = navigator.appName;
	var version = parseInt(navigator.appVersion);
	var bWidth; 
	var bHeight;
	var Width;
	var Height

	if (bName == "Netscape" && version >= 4) {
		bWidth = (197 + width);
		bHeight = (140 + height);
	}
	if (bName == "Netscape" && version < 4) {
		bWidth = (219 + width);	 
		bHeight = (179 + height);
	}
	if (bName == "Microsoft Internet Explorer" && version >= 4) {
		bWidth = (200 + width);
		bHeight = (140 + height); 	
	}
	if (bName == "Microsoft Internet Explorer" && version < 4) {
		bWidth = (210 + width);	 
		bHeight = (156 + height);
	}
	this.window.name = "opener";
	var remote = window.open(page,win_name,"width=" + bWidth + ",height=" + bHeight + ",scrollbars=yes,resizable=yes");
	this.window.name = "opener";
	}

function EmailToFriend(MemberID)
    {
    var page = "/MainSite/Members/Popup/EmailToFriend.aspx?ID=" + MemberID;
    var win_name = "EmailToFriend";
    var width = 350;
    var height = 150;
    openNewWin(page, win_name, width, height) 
    } 
    
function tabberGetContainer(elm)
{
    var tabContainer = null;
    elm.ancestors().each(
        function (o)
        {
            if (o.className.strip() == "tabberlive")
                tabContainer = $(o);
        }
    );
    return tabContainer;
}

function ShowHide(ID)
{
    var objSH = $(ID);
    if (objSH != null)
    {
        objSH.toggle();
    }
}

function SetSelectedItemInDropDownList(ddl, selectedValue)
{
    ddl = $(ddl);
    for (var i = 0; i < ddl.options.length; i++)
    {
        if (ddl.options[i].value == selectedValue)
        {
            ddl.options[i].selected = true;
            break;
        }
    }
}

function ValidateDateForCustomValidator(sender, args)
{
    args.IsValid = args.Value.isValidDate();
}
