////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CUSTOM
////////////////////////////////////////////////////////////////////////////////////////////////////////////

var aiim = {};
aiim.widget = {};
aiim.util = {};
aiim.util.getElement = function(el) {
	if (el && typeof el == "string")
		return document.getElementById(el);
	return el;
};
aiim.util.getElementChildren = function(el) {
	var children = [];
	var child = el.firstChild;
	while (child)
	{
		if (child.nodeType == 1)
			children.push(child);
		child = child.nextSibling;
	}
	return children;
};
aiim.util.browser = {
    init: function () {
		this.agent = this.searchString(this.dataBrowser) || "unknown";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "unknown";
	},
	searchString: function (data) {
	    var dataLen = data.length;
		for (var i=0;i<dataLen;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{	// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 	// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	]
};
aiim.util.browser.init();
aiim.trans = {};
aiim.trans.defaultTransition = function(time,begin,finish,duration) { 
    time/=duration;
    return begin + ((2.001-time) * time * finish);
};
/*= popup
--------------------------------------------------*/
aiim.widget.popup = function(el,opt) {
    this.el = aiim.util.getElement(el);
    this.bgnY = this.curY = opt.from;
    this.dstY = opt.to;
    this.duration = opt.dur;
    this.startTime = 0;
    this.trans = aiim.trans.defaultTransition;
    this.interval = 1000/40; //fps
    this.timeoutId = 0;
    var me = this;
    this.intervalFunc = function() { me.step(); }
    this.animate = true;
    this.blocked = false;
};
aiim.widget.popup.prototype.start = function () {
    this.stop();
    this.curY = this.el.offsetTop;
    this.startTime = (new Date()).getTime();
    if(this.animate)
        this.timeoutId = setTimeout(this.intervalFunc, this.interval);
    else
        this.timeoutId = setTimeout(this.intervalFunc, 0);
};
aiim.widget.popup.prototype.stop = function () {
    if(this.timeoutId)
        clearTimeout(this.timeoutId);
	this.timeoutId = 0;
};
aiim.widget.popup.prototype.step = function () {
    var elapsedTime = (new Date()).getTime() - this.startTime;
    var done = elapsedTime >= this.duration;
    var y;
    if(done)
        y = this.curY = this.endY;
    else {
        y = this.trans(elapsedTime,this.curY,this.endY-this.curY,this.duration);
    }
    this.el.style.top = y + 'px';
	if (!done)
		this.timeoutId = setTimeout(this.intervalFunc,this.interval);
};
aiim.widget.popup.prototype.open = function () {
    this.endY = this.dstY;
    this.start();
};
aiim.widget.popup.prototype.close = function () {
    if(!this.blocked)
    {
        this.endY = this.bgnY;
        this.start();
    }
};
aiim.widget.popup.prototype.stayopen = function (bool) {
    this.animate = !bool;
    this.blocked = bool;
};
/*= slider
--------------------------------------------------*/
aiim.widget.slider = function(el,opt) {
    this.el = aiim.util.getElement(el);
    this.curPanel = null;
    if(typeof opt.def != 'undefined') { //default index
		if (typeof opt.def == 'number')
			this.curPanel = this.getPanels()[opt.def];
		else
		    this.curPanel = this.getPanels()[0];
    }
    this.curPanel.style.display = 'block';
};
aiim.widget.slider.prototype.getGroup = function() {
	return aiim.util.getElementChildren(this.el)[0];
};
aiim.widget.slider.prototype.getPanels = function() {
	return aiim.util.getElementChildren(this.getGroup());
};
aiim.widget.slider.prototype.isKnownPanel = function(el)
{
    if(typeof el != 'undefined')
    {
        var len = this.getPanels().length;
        for(var i=0; i<len; ++i)
        {
            if(el = this.getPanels()[i])
                return true;
        }
    }
    return false;
}
aiim.widget.slider.prototype.showPanel = function(tabIndex) {
    try {
        if(typeof tabIndex == 'string')
        {
            var el = document.getElementById(tabIndex);
            if(typeof el != 'undefined' && this.isKnownPanel(el))
            {
                this.curPanel.style.display = 'none';
                this.curPanel = el;
            }
        }
        else if(typeof tabIndex == 'number')
        {
            this.curPanel.style.display = 'none';
            var el = this.getPanels()[tabIndex];
            if(typeof el != 'undefined')
            {   
                this.curPanel.style.display = 'none';
                this.curPanel = el;
            }

        }
        this.curPanel.style.display = 'block';
    }
    catch (ex) {}
};
/*= tabbed panels
--------------------------------------------------*/
aiim.widget.tabbed = function (el,opt) {
    this.el = aiim.util.getElement(el);
    this.mySlider = new aiim.widget.slider(this.getContentGroup(),{def:opt.def});
}; 
aiim.widget.tabbed.prototype.getTabGroup = function() {
	if (this.el) {
		var children = aiim.util.getElementChildren(this.el);
		if (children.length)
			return children[0];
	}
	return null;
};
aiim.widget.tabbed.prototype.getTabs = function() {
	var tabs = [];
	var tg = this.getTabGroup();
	if (tg) tabs = aiim.util.getElementChildren(tg);
	return tabs;
};
aiim.widget.tabbed.prototype.getContentGroup = function() {
	if(this.el) {
	    var children = aiim.util.getElementChildren(this.el);
	    if (children.length) {
	        return children[1];
	    }
	}
	return null;
};
aiim.widget.tabbed.prototype.showPanel = function(tabIndex){
    this.mySlider.showPanel(tabIndex);
};
/*= menu 
--------------------------------------------------*/
aiim.widget.menu = function (el,opt) {
    this.el = aiim.util.getElement(el);
    this.right = this.el.offsetWidth + this.el.offsetLeft;
    
    this.items = [];
    var lis = this.el.getElementsByTagName('li');
    for(var i=0;i<lis.length;++i) {
        this.items[i] = new aiim.widget.menu.li(lis[i],{menu:this});
    }
    for(var i=0;i<lis.length;++i) {
        this.items[i].el.style.position = 'static';
    }
};
aiim.widget.menu.li = function (el,opt) {
    this.el = aiim.util.getElement(el);
    this.menu = ((typeof opt != 'undefined' && opt.menu) ? opt.menu : null);

    var uls = this.el.getElementsByTagName('ul');
    this.submenu = null;
    if(uls.length>0) {
        this.submenu = new aiim.widget.menu.ul(uls[0],{menu:this.menu});
    }
    
    var on, off;
    var me = this;    
    
    if(this.submenu) {
        on = function () {
            me.togglePosition('relative');
            me.el.className += (aiim.util.browser.agent == 'Explorer' ? ' hover' : '');
            //me.el.className += ' hover';
            me.submenu.open(); 
        };
        off = function () {
            me.togglePosition(aiim.util.browser.agent == 'Explorer' ? 'static' : 'relative');
            me.el.className = me.el.className.replace(' hover', '');
            me.submenu.close(); 
        };
    } else {
        on = function () {
            me.togglePosition('relative');
            me.el.className += (aiim.util.browser.agent == 'Explorer' ? ' hover' : '');
            //me.el.className += ' hover';
        };
        off = function () {
            me.togglePosition(aiim.util.browser.agent == 'Explorer' ? 'static' : 'relative');
            me.el.className = me.el.className.replace(' hover', '');
        };
    }
    this.el.onmouseover = on;
    this.el.onmouseout = off;
    
    //this.el.style.position = 'static';
};
aiim.widget.menu.li.prototype.togglePosition = function (pos) {
//    if(this.el.style.position == 'relative') {
//        this.el.style.position = 'static';
//    } else {
//        this.el.style.position = 'relative';
//    }
    this.el.style.position = pos;
};
aiim.widget.menu.ul = function (el,opt) {
    this.el = aiim.util.getElement(el);
    this.menu = ((typeof opt != 'undefined' && opt.menu) ? opt.menu : null);
    if(this.menu) {
        var el = this.el;
        var left = 0;
        while(el != null && el.offsetParent != this.menu.el.offsetParent) {
            left += el.offsetLeft;
            el = el.offsetParent;
        }
        if(left + this.el.offsetWidth > this.menu.right) {
            var parent = this.el.parentNode;
            var offsetX = parent.offsetWidth - (this.el.offsetLeft - parent.offsetLeft);
            this.el.className += ' right';
        }
    } 
    if(aiim.util.browser.agent == 'Explorer' && aiim.util.browser.version < 7) {
        this.createIframe();
    }
};
aiim.widget.menu.ul.prototype.createIframe = function () {
    this.iframe = document.createElement('iframe');
	this.iframe.src = 'javascript:false';
	this.iframe.tabIndex = '-1';
	this.iframe.style.left = this.el.offsetLeft + 'px';
	this.iframe.style.top = this.el.offsetTop + 'px';
	this.iframe.style.width = this.el.offsetWidth + 'px';
	this.iframe.style.height = this.el.offsetHeight + 'px';
	this.iframe.style.visibility = 'hidden';
	this.iframe.style.filter = 'Alpha(opacity=0)';
	this.el.parentNode.appendChild(this.iframe);
};
aiim.widget.menu.ul.prototype.toggleIframe = function () {
    if(this.iframe != null) {
        if(this.iframe.style.visibility == 'hidden') {
            this.iframe.style.visibility = 'visible';
        } else {
            this.iframe.style.visibility = 'hidden';
        }
    }
};
aiim.widget.menu.ul.prototype.open = function () {
    this.el.style.visibility = 'visible';
    this.toggleIframe();
};
aiim.widget.menu.ul.prototype.close = function () {
    this.el.style.visibility = 'hidden';
    this.toggleIframe();
};
/*= pop desc box
--------------------------------------------------*/
aiim.widget.box = function (opt) {
    this.hasDropShadow = false;
    if(typeof opt != 'undefine') {
        if(typeof opt.shdw != 'undefined') {
            this.hasDropShadow = opt.shdw;
        }
    }    
    this.container = document.createElement("div");
    this.container.className = "info";
    if(this.hasDropShadow) {
        this.container.className += " drpshdw";
    }
    this.content = document.createElement("div");
    this.content.className = "content";
    
    this.container.appendChild(this.content);
};
aiim.widget.box.prototype.show = function () {
    this.container.style.display = 'block';  
};
aiim.widget.box.prototype.hide = function () {
    this.container.style.display = 'none';  
};
/*= content custom functions
--------------------------------------------------*/
function disclose(el) {
    var el = aiim.util.getElement(el);
    if(el.style.display != 'none') {
        el.style.display = 'none'
    } else {
        el.style.display = 'block'
    }
}
function info_s(el,opt) {
    var el = aiim.util.getElement(el);
    var parent = el.parentNode;
    if(typeof el.info != 'undefined') {
        el.info.show();
    } else {
        el.info = new aiim.widget.box({shdw:true});
        if(typeof opt != 'undefined') {
            if(typeof opt.html == 'string') {
                el.info.content.innerHTML = opt.html;
            }
            if(typeof opt.w == 'number') {
                el.info.content.style.width = opt.w + 'px';
            }
        }
        parent.insertBefore(el.info.container,el);
        el.info.show()
    }
}
function info_h(el) {
    var el = aiim.util.getElement(el);
    var parent = el.parentNode;
    if(typeof el.info != 'undefined') {
        el.info.hide();
    }
}
function scrollTop() {
    if (document.documentElement && document.documentElement.scrollTop)
	    document.documentElement.scrollTop = 0;
    else if (document.body)
	    document.body.scrollTop = 0;
}
function OpenInNewWindow(url)
{
    window.open(url,"_blank");
}
function postToNewWindow(t) { 
    var myForm = document.forms['aspnetForm'];
    if(typeof t=="undefined")
        t = "_blank";
    myForm.target = t;
    setTimeout("document.forms['aspnetForm'].target=''",0)
}
function DisableOnSubmit(btnObj,msg)
{
    if(typeof(msg)=='undefined')
    {
        msg = '';
    }
    var btnId = btnObj.getAttribute("id");
    setTimeout("document.getElementById('" + btnId + "').disabled=true", 0);
    setTimeout("__disableOnSubmit('" + btnId + "','" + msg + "');", 0);
}
function __disableOnSubmit(btnObj,msg)
{
    if(typeof(btnObj)=='string')
    {
        btnObj = document.getElementById(btnObj);
    }
    
    if(Page_IsValid)
    {
        btnObj.disabled = true;
        
        var btnId = btnObj.getAttribute("id");
        var imgId = btnId + "_image";
        var parent = btnObj.parentNode;
        var span = document.createElement('span');
        span.className = "ajaxloader";
        span.innerHTML = (typeof(msg)=='string' && msg!='') ? msg : 'Processing...';
        
        if(btnObj.nextSibling!=null)
        {
            parent.insertBefore(span, btnObj.nextSibling);
        }
        else
        {
            parent.appendChild(span);
        }
        
        if(document.images)
        {
            var img = new Image;
            parent.insertBefore(img, span);
            img.className = 'ajaxloaderImage';
            img.setAttribute("alt", "Please wait.");
            img.setAttribute("id", imgId);
            img.src = "/styles/img/ajax-loader.gif";
        }
        //
        
        setTimeout("document.getElementById('" + imgId + "').src='/styles/img/ajax-loader.gif';", 0);
    }
    else
    {
        btnObj.disabled = false;
    }
}
function doFeedBurner(targetID,containerID,e)
{
    var textbox = document.getElementById(targetID);
    var container = document.getElementById(containerID);
    if(typeof(textbox)!='undefined' && typeof(container)!='undefined')
    {
        if(typeof(e)!='undefined')
        {
            var keynum;
            if(window.event) 
            { 
                keynum = e.keyCode; 
            } 
            else if (e.which)
            { 
                keynum = e.which; 
            }
            if(keynum > 0 && keynum != 13)
            {
                return false;
            }
        }
        var searchString = textbox.value;
        if(typeof(searchString)!='undefined' && searchString.length>0)
        {
            var newForm = document.createElement('form');
            //newForm.action = "http://www.feedblitz.com/f/f.fbz?AddNewUserDirect";
            newForm.action = "http://www.aiim.org/eNewsletters.aspx"
            newForm.method = "POST";
            newForm.innerHTML = container.innerHTML;
            //newForm.target = "_blank";
            var q = newForm.elements['EMAIL'];
            if(typeof(q)!='undefined')
            {
                q.value = searchString
            }
            document.body.appendChild(newForm)
            newForm.submit();
            document.body.removeChild(newForm)
        }
    }
}
function doTheGoogle(targetID,containerID,e)
{
    var textbox = document.getElementById(targetID);
    var container = document.getElementById(containerID);
    if(typeof(textbox)!='undefined' && typeof(container)!='undefined')
    {
        if(typeof(e)!='undefined')
        {
            var keynum;
            if(window.event) 
            { 
                keynum = e.keyCode; 
            } 
            else if (e.which)
            { 
                keynum = e.which; 
            }
            if(keynum > 0 && keynum != 13)
            {
                return false;
            }
        }
        var searchString = textbox.value;
        if(typeof(searchString)!='undefined' && searchString.length>0)
        {
            var newForm = document.createElement('form');
            newForm.action = "http://googlebox.aiim.org/search";
            newForm.method = "GET";
            newForm.innerHTML = container.innerHTML;
            var q = newForm.elements['q'];
            if(typeof(q)!='undefined')
            {
                q.value = searchString
            }
            document.body.appendChild(newForm)
            newForm.submit();
            document.body.removeChild(newForm)
        }
    }
}
function enableZoomImages(keyword, match)
{
    var images = document.getElementsByTagName("img");
    var imagesLen = images.length;
    for(var i=0;i<imagesLen;++i)
    {
        var img = images[i];
        var parent = img.parentNode;
        var css = img.className;
        if(css.length>0 && css.indexOf(keyword)>-1)
        {
            var link = document.createElement('a');
            link.className = 'enlargeImage';
            link.innerHTML = '<div>(+) Enlarge image</div>';
            var src = img.getAttribute('src');
            src = src.replace(match, '.');
            link.setAttribute('href', src);
            link.setAttribute('target', 'blank');
            link.appendChild(img);
            parent.appendChild(link);
        }      
    }
}
function printThis()
{
    var bag = document.createElement("span");
    var c = document.createElement("span");
    c.className = "printThis"
    bag.appendChild(c)
    var a = document.createElement("a");
    a.innerHTML = "Print";
    a.setAttribute("href", "#");
    a.setAttribute("onclick", "window.print();return false;");
    c.appendChild(a);
    
    document.write(bag.innerHTML);
}

////
var url = function (url) { this.url = url; }
url.prototype.go = function(target) {
    if(typeof target == 'string') 
        window.open(this.url, target);
    else 
        window.location = this.url;
};

////
var topiclinks = function(el,opt) {
    this.element = aiim.util.getElement(el);
    if(typeof this.element != 'undefined' && this.element != null)
    {
        this.items = this.element.getElementsByTagName('span');
        this.maxvisible = typeof opt != 'undefined' && typeof opt.maxvisible != 'undefined' ? opt.maxvisible : 0;
        this.button = typeof opt != 'undefined' && typeof opt.button != 'undefined' ? aiim.util.getElement(opt.button) : undefined;

        if(this.maxvisible >= 0 && this.maxvisible < this.items.length)
        {
            this.hideall();
        }
        else
        {
            this.hide(this.button);
        }
    }
};
topiclinks.prototype.show = function (el) {
    el = aiim.util.getElement(el);
    el.style.display = '';

    this.hide(this.button)
};
topiclinks.prototype.hide = function (el) {
    el = aiim.util.getElement(el);
    el.style.display = 'none';
};
topiclinks.prototype.showall = function () {
    var len = this.items.length;
    for(var i=0; i<len; ++i)
    {
        this.show(this.items[i]);
    }
};
topiclinks.prototype.hideall = function () {
    var len = this.items.length;
    for(var i=this.maxvisible; i<len; ++i)
    {
        this.hide(this.items[i]);
    }
};
function encodeHtml(el) {
    el = aiim.util.getElement(el);
    if(typeof el.value != 'undefined')
    {
        var content = el.value
        content = content.replace(/</g,'&lt;');
        content = content.replace(/>/g,'&gt;');
        el.value = content;
    }
}
function WebForm_FireDefaultLinkButton(event, target) { 
    if (event.keyCode == 13 && !(event.srcElement && (event.srcElement.tagName.toLowerCase() == "textarea"))) { 
        var defaultButton; 
        if (__nonMSDOMBrowser) { 
            defaultLinkButton = document.getElementById(target); 
        } 
        else { 
            defaultLinkButton = document.all[target]; 
        } 
        if (defaultLinkButton && typeof(defaultLinkButton.href) != "undefined") { 
            window.location = defaultLinkButton.href;
            event.cancelBubble = true; 
            if (event.stopPropagation) event.stopPropagation(); 
            return false; 
        } 
    } 
    return true; 
 }
function loadStates(val, el)
{
    el = document.getElementById(el); // container object
    var dropdownlist;var textbox;
    
    // gets input objects
    var nodes = el.childNodes;
    var len = el.childNodes.length;
    for(var i = 0;i<len;++i)
    {
        var n = nodes[i];
        if(n.nodeType == 1) 
        {
            if(n.nodeName=="SELECT")
                dropdownlist = n;  
            else if(n.nodeName=="INPUT")
                textbox = n;
        }
    }
    
    // controls what to display
    if(val==222 || val==36 || val==14) 
    {
        dropdownlist.innerHTML = ''; // clear first
        
        // add stuff to drop down list
        var statesLen = states['s'+val].length;
        for(var i = 0;i < statesLen; i++)
        {
            var s = states['s'+val][i].split("|");
            var n = document.createElement("option");
            n.innerHTML = s[1];
            n.value = s[0];
            dropdownlist.appendChild(n);                    
        }
        textbox.style.display = 'none';
        dropdownlist.style.display = '';
    }
    else
    {
        textbox.style.display = '';
        dropdownlist.style.display = 'none';
    }
}
function loadStates(val, el)
{
    el = document.getElementById(el); // container object
    var dropdownlist;var textbox;
    
    // gets input objects
    var nodes = el.childNodes;
    var len = el.childNodes.length;
    for(var i = 0;i<len;++i)
    {
        var n = nodes[i];
        if(n.nodeType == 1) 
        {
            if(n.nodeName=="SELECT")
                dropdownlist = n;  
            else if(n.nodeName=="INPUT")
                textbox = n;
        }
    }
    
    // controls what to display
    if(val==222 || val==36 || val==14) 
    {
        dropdownlist.innerHTML = ''; // clear first
        
        // add stuff to drop down list
        var statesLen = states['s'+val].length;
        for(var i = 0;i < statesLen; i++)
        {
            var s = states['s'+val][i].split("|");
            var n = document.createElement("option");
            n.innerHTML = s[1];
            n.value = s[0];
            dropdownlist.appendChild(n);                    
        }
        textbox.style.display = 'none';
        dropdownlist.style.display = '';
    }
    else
    {
        textbox.style.display = '';
        dropdownlist.style.display = 'none';
    }
}


function SwapStateProvincesInput(c,el1,el2,onload)
{
    el1 = document.getElementById(el1);
    el2 = document.getElementById(el2);
    
    if(c==222 || c==36 || c==14) 
    {
        el2.innerHTML = '';
        
        //create 'not selected' item
        var n = document.createElement("option");
        n.innerHTML = 'Not selected';
        n.value = '';
        el2.appendChild(n);
        
        var len = states['s'+c].length;
        for(var i = 0;i < len; i++)
        {
            var s = states['s'+c][i].split("|");
            var n = document.createElement("option");
            n.innerHTML = s[1];
            n.value = s[0];
            el2.appendChild(n);                    
        }
        el2.style.display = '';
        if (onload == 'true')
        {
            el2.value = el1.value;
        }
        el1.value = el2.value;
        el1.style.display = 'none';
    }
    else
    {
        el2.style.display = 'none';
        if (onload == 'false')
        {
            el1.value = '';
        }
        el1.style.display = '';
    }
}


function ReValidate(el)
{
    if(typeof el == 'object')
        el = el.id
        
    if(typeof Page_Validators != 'undefined')
    {
        var len = Page_Validators.length;
        for(var i = 0; i < len; ++i)
            if(Page_Validators[i].controltovalidate == el)
                ValidatorValidate(
                    Page_Validators[i], 
                    typeof Page_Validators[i].validationGroup == 'string' ? Page_Validators[i].validationGroup : '', 
                    null
                ); 
                
    }
}
