//Workaround for IE AxtiveX security warning.
//Writes the Flash to the page.
function insertFlash(filename, width, height) {
    document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + width + '" height="' + height + '" id="carbonite" align="left">\n');
    document.write('<param name="allowScriptAccess" value="sameDomain" />\n');
    document.write('<param name="movie" value="/flash/' + filename + '" />\n');
    document.write('<param name="menu" value="false" />\n');
    document.write('<param name="quality" value="high" />\n');
    document.write('<param name="bgcolor" value="#ffffff" />\n');
    document.write('<param name="wmode" value="transparent" />\n');
    document.write('<embed src="/flash/' + filename + '" menu="false" quality="high" bgcolor="#ffffff" wmode="transparent" width="' + width + '" height="' + height + '" name="carbonite" align="left" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />\n');
    document.write('</object>\n');
}

//Generic popup window
var _POPUP_FEATURES = 'location=0,statusbar=0,menubar=0,width=520,height=460,resizable=yes,scrollbars=yes';
function popup(url, target, features) {
    // pops up a window containing url optionally named target, optionally having features
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target)) target = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
    return theWindow;
}
function isUndefined(v) {
    var undef;
    return v === undef;
}

/*-----------------------------------------------------------
Toggles element's display value
Input: any number of element id's
Output: none 
---------------------------------------------------------*/
function toggleDisp() {
    for (var i = 0; i < arguments.length; i++) {
        var d = $(arguments[i]);
        if (d.style.display == 'none')
            d.style.display = 'block';
        else
            d.style.display = 'none';
    }
}
/*-----------------------------------------------------------
Toggles tabs/bullets - Closes any open tabs, and then opens current tab
Input:     1.The number of the current tab
2.The number of tabs
3.(optional)The number of the tab to leave open
4.(optional)Pass in true or false whether or not to animate the open/close of the tabs
Output: none 
---------------------------------------------------------*/
function toggleTab(num, numelems, opennum, animate) {
    if ($('bulletContent' + num).style.display == 'none') {

        for (var i = 1; i <= numelems; i++) {
            if ((opennum == null) || (opennum != i)) {
                var temph = 'bullet' + i;
                $(temph).parentNode.className = '';
                var h = $(temph);
                if (!h) {
                    var h = $('bulletActive');
                    h.id = temph;
                }
                var tempc = 'bulletContent' + i;
                var c = $(tempc);
                if (c.style.display != 'none') {
                    if (animate || typeof animate == 'undefined')
                        Effect.toggle(tempc, 'appear', { duration: 0.2, queue: { scope: 'menus', limit: 3} });
                    else
                        toggleDisp(tempc);
                }
            }
        }
        var h = $('bullet' + num);
        if (h) {
            h.parentNode.className = 'active';
        }
        h.blur();
        var c = $('bulletContent' + num);
        c.style.marginTop = '2px';
        if (animate || typeof animate == 'undefined') {
            Effect.toggle('bulletContent' + num, 'appear', { duration: 0.2, queue: { scope: 'menus', position: 'end', limit: 3} });
        } else {
            toggleDisp('bulletContent' + num);
        }
    }
}



// Generic clear/restore form field on focus
function clearField(t) {
    if (t.defaultValue == t.value) t.value = '';
}
function restoreField(t) {
    if (t.value == '') t.value = t.defaultValue;
}

//Open and close the Language select box
openIntl = function() {
    theboxUL = document.getElementById('intlSitesSelect');
    if (!document.getElementById('intlSitesBehind')) {
        var tempDiv = document.createElement('div');
        tempDiv.setAttribute('id', 'intlSitesBehind');
        tempDiv.setAttribute('onclick', 'openIntl()');
        tempDiv.onclick = function() { openIntl(); this.parentNode.removeChild(this); };
    }
    if (theboxUL.className == 'intlSites openIntl') {
        theboxUL.className = 'intlSites';
    }
    else {
        theboxUL.className = 'intlSites openIntl';
        theboxUL.parentNode.insertBefore(tempDiv, theboxUL);
        var behindDiv = document.getElementById('intlSitesBehind');
        (document.documentElement.clientHeight > document.body.clientHeight) ? behindDiv.style.height = document.documentElement.clientHeight + 'px' : behindDiv.style.height = document.body.clientHeight + 'px';
        (document.documentElement.clientWidth > document.body.clientWidth) ? behindDiv.style.width = document.documentElement.clientWidth + 'px' : behindDiv.style.width = document.body.clientWidth + 'px';
    }
}

//Multiple Onload Events
function dss_addLoadEvent(fn) {
    if (typeof (fn) != "function") return;
    var tempFunc = window.onload;
    window.onload = function() {
        if (typeof (tempFunc) == "function") tempFunc();
        fn();
    }
}

//Function used to toggle input field type, used on Password fields
function changeInputType(
  oldElm, // a reference to the input element
  iType, // value of the type property: 'text' or 'password'
  iValue, // the default value, set to 'password' in the demo
  blankValue, // true if the value should be empty, false otherwise
  noFocus) {  // set to true if the element should not be given focus
    if (!oldElm || !oldElm.parentNode || (iType.length < 4) ||
    !document.getElementById || !document.createElement) return;
    if (oldElm.value != '' && oldElm.type == 'password') return;

    var newElm = document.createElement('input');
    newElm.type = iType;
    if (oldElm.name) newElm.name = oldElm.name;
    if (oldElm.id) newElm.id = oldElm.id;
    if (oldElm.className) newElm.className = oldElm.className;
    if (oldElm.size) newElm.size = oldElm.size;
    if (oldElm.tabIndex) newElm.tabIndex = oldElm.tabIndex;
    if (oldElm.accessKey) newElm.accessKey = oldElm.accessKey;
    newElm.onfocus = function() {
        return function() {
            if (this.hasFocus) return;
            var newElm = changeInputType(this, 'password', iValue,
      (this.value.toLowerCase() == iValue.toLowerCase()) ? true : false);
            if (newElm) newElm.hasFocus = true;
        } 
    } ();
    newElm.onblur = function() {
        return function() {
            if (this.hasFocus)
                if (this.value == '' || (this.value.toLowerCase() == iValue.toLowerCase())) {
                changeInputType(this, 'text', iValue, false, true);
            }
        } 
    } ();
    // hasFocus is to prevent a loop where onfocus is triggered over and over again
    newElm.hasFocus = false;
    oldElm.parentNode.replaceChild(newElm, oldElm);
    if (!blankValue) newElm.value = iValue;
    if (!noFocus || typeof (noFocus) == 'undefined') {
        window.tempElm = newElm;
        setTimeout("tempElm.hasFocus=true;tempElm.focus();", 1);
    }
    return newElm;
}

function textCounter(field, cntfield, maxlimit) {
    if (field.value.length > maxlimit) {
        field.value = field.value.substring(0, maxlimit);
    }
    else {
        document.getElementById("count").innerHTML = maxlimit - field.value.length;
    }
}

function add_email() {
    var add = document.getElementById('add_email');
    var ni = document.getElementById('additional_emails');
    var numi = document.getElementById('theValue');
    var num = (document.getElementById('theValue').value - 1) + 2;
    numi.value = num;

    var newinput = document.createElement('input');
    var newinputId = 'tbxEmailAddress' + num;
    newinput.setAttribute('id', newinputId);
    newinput.setAttribute('name', newinputId);
    newinput.setAttribute('class', 'txtfield email');
    newinput.setAttribute('type', 'text');
    add.parentNode.insertBefore(newinput, add);
}
function textMaxLength(obj, maxLength, evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    var max = maxLength - 0;
    var text = obj.value;
    if (text.length > max) {
        var ignoreKeys = [8, 46, 37, 38, 39, 40, 35, 36];
        for (i = 0; i < ignoreKeys.length; i++) {
            if (charCode == ignoreKeys[i]) {
                return true;
            }
        }
        return false;
    } else {
        return true;
    }
} 