﻿// JScript File

function clearValue(object, text)
{
   if (object.value == text)
   {
        object.value = '';
   }
}

function fillValue(object, text)
{
   if (object.value == text || object.value == '')
   {
        object.value = text;
   }
}

function setSelected(object)
{
    document.getElementById(object).className='here';
}

function wsredirect(url)
{
    window.location.href=url;
}

function togglePanel(object)
{
    if (document.getElementById(object).style.display == '')
        document.getElementById(object).style.display='none';
    else
        document.getElementById(object).style.display = '';
}


function limitTextArea(textarea, maxlength, toUpdate, evt)
{  
  var expression = '[a-zA-Z0-9 !"#$%&()*+,-./:;<=>@^_{|}?\\[\\]\\\\\']{0,' + maxlength +'}';
  var re = new RegExp(expression);
  var m = re.exec(textarea.value);
  if (m != null)
  {
    var str = m[0];
    if (textarea.value.length == m[0].length)
    {
        document.getElementById(toUpdate).innerHTML = '[' + (textarea.value.length ) + ' / ' + maxlength + ']';
        return true;
    }
    else
    {
        textarea.value = m[0];
        if (textarea.value.length == maxlength)
        {
            var errorLabel = document.getElementById('lockMessageCharacterNotAllowed');
            if (errorLabel != null)
            {
                errorLabel.style.display = "block";
                errorLabel.innerHTML= 'You have exceeded ' + maxlength + ' characters...';
                setTimeout('removeLockErrorLabel ()', 2000);
            }
        }
        else
        {
            textarea.style.backgroundColor = "#FFCECE";
            setTimeout('normalizeLockMessage (\'' + textarea.id + '\', "#ffffff")', 500);

            var errorLabel = document.getElementById('lockMessageCharacterNotAllowed');
            if (errorLabel != null)
            {
                errorLabel.style.display = "block";
                errorLabel.innerHTML = 'The character you typed is not allowed...';
                setTimeout('removeLockErrorLabel ()', 2000);
            }
        }
        return false;
        
    }
  }
  return false;
}

function normalizeLockMessage(textareaid, color)
{
   var obj = document.getElementById(textareaid);
   if (obj != null) obj.style.backgroundColor = color;
}

function removeLockErrorLabel()
{
    var errorLabel = document.getElementById('lockMessageCharacterNotAllowed');
    if (errorLabel != null) errorLabel.style.display = "none";
}


function limitTextArea2(textarea, maxlength, toUpdate, evt)
{  
    //alert(evt.keyCode);

    if (evt.keyCode == 13)
    {
        return false;
    }
    
    //backspace
    var len = textarea.value.length;
    if (evt.keyCode != 8 && evt.keyCode != 16 && evt.keyCode != 17 && evt.keyCode != 18 && evt.keyCode != 37 && evt.keyCode != 38 && evt.keyCode != 39 && evt.keyCode != 40)
        len++;
    
    if(len>maxlength)
    {
         //textarea.value=textarea.value.substring(0,maxlength);
         return false;
     }
    document.getElementById(toUpdate).innerHTML = '[' + (textarea.value.length ) + ' / ' + maxlength + ']';
    return true;
}

function showLength(textarea, maxlength, toUpdate, evt, toUpdate)
{
     var toRet = true;
     if(textarea.value.length>=maxlength+1)
     {
         textarea.value=textarea.value.substring(0,maxlength);
         toRet = false;
     }

     var str=new String()
     str=textarea.value;
     var found = false;
     
     for(i=0;i<textarea.value.length;i++)
     {
        if(textarea.value.charCodeAt(i)>256) 
        {
            textarea.value=textarea.value.substring(0,i) + textarea.value.substring(i+1);
            i--;
            found = true;
        }
     }
     if(found)
        alert(onlyEnglish);
        
    document.getElementById(toUpdate).innerHTML = '[' + (textarea.value.length ) + ' / ' + maxlength + ']';
    return toRet;
}

function showLengthNE(textarea, maxlength, toUpdate, evt, toUpdate)
{
     var toRet = true;
     if(textarea.value.length>=maxlength+1)
     {
         textarea.value=textarea.value.substring(0,maxlength);
         toRet = false;
     }

     var str=new String()
     str=textarea.value;
     var found = false;
     
    document.getElementById(toUpdate).innerHTML = '[' + (textarea.value.length ) + ' / ' + maxlength + ']';
    return toRet;
}

function setPassword(object, text)
{
    var passwordTextBox = document.getElementById(object);
    if (passwordTextBox == null) return;
    passwordTextBox.value=text;
}

function leftTrim(sString) 
{
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    return sString;
}

function rightTrim(sString) 
{
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

function trim(sString) 
{
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}

/* Update panel for dynamic search */

var mytimeout = 0;
var lastSearch = '';
function updatePanel(searchbox, eventArgs)
{
    var newText = trim(searchbox.value);
    if (lastSearch == newText) return;
    
    if (mytimeout != 0)
     clearTimeout(mytimeout);
    
    mytimeout = setTimeout('postsearch("' + eventArgs + '")', 500);
    
    lastSearch = newText;
}



/* CLOSED_IMAGE - the image to be displayed when the sublists are closed
 * OPEN_IMAGE   - the image to be displayed when the sublists are opened
 */
CLOSED_IMAGE='/images/singleplus.gif';
OPEN_IMAGE='/images/singleminus.gif';

/* makeCollapsible - makes a list have collapsible sublists
 * 
 * listElement - the element representing the list to make collapsible
 */
function makeCollapsible(listElement, appPath){

  // removed list item bullets and the sapce they occupy
  listElement.style.listStyle='none';
  listElement.style.marginLeft='0';
  listElement.style.paddingLeft='0';

  // loop over all child elements of the list
  var child=listElement.firstChild;
  while (child!=null){

    // only process li elements (and not text elements)
    if (child.nodeType==1){

      // build a list of child ol and ul elements and hide them
      var list=new Array();
      var grandchild=child.firstChild;
      while (grandchild!=null){
        if (grandchild.tagName=='OL' || grandchild.tagName=='UL'){
          grandchild.style.display='none';
          list.push(grandchild);
        }
        grandchild=grandchild.nextSibling;
      }

      // add toggle buttons
      var node=document.createElement('img');
      node.setAttribute('src',appPath + CLOSED_IMAGE);
      node.setAttribute('class','collapsibleClosed');
      node.onclick=createToggleFunction(node,list, appPath);
      child.insertBefore(node,child.firstChild);

    }

    child=child.nextSibling;
  }

}

/* createToggleFunction - returns a function that toggles the sublist display
 * 
 * toggleElement  - the element representing the toggle gadget
 * sublistElement - an array of elements representing the sublists that should
 *                  be opened or closed when the toggle gadget is clicked
 */
function createToggleFunction(toggleElement,sublistElements, appPath){

  return function(){

    // toggle status of toggle gadget
    if (toggleElement.getAttribute('class')=='collapsibleClosed'){
      toggleElement.setAttribute('class','collapsibleOpen');
      toggleElement.setAttribute('src',appPath + OPEN_IMAGE);
    }else{
      toggleElement.setAttribute('class','collapsibleClosed');
      toggleElement.setAttribute('src',appPath + CLOSED_IMAGE);
    }

    // toggle display of sublists
    for (var i=0;i<sublistElements.length;i++){
      sublistElements[i].style.display=
          (sublistElements[i].style.display=='block')?'none':'block';
    }

  }

}

var uservoiceOptions = {
    /* required */
    key: 'wavesecure',                //domain
    host: 'feedback.wavesecure.com',  //domain.forumurl
    forum: '3560',                    //forum ID
    showTab: true,                    //use tab or hyperlink
    /* optional */
    alignment: 'right',                //left or right for tab
    background_color: '#f00',          //bg color of tab
    text_color: 'white',              //text color of tab
    hover_color: '#000',              //hover color of tab
    lang: 'en'                        //language
};

function _loadUserVoice() {
    var s = document.createElement('script');
    s.setAttribute('type', 'text/javascript');
    s.setAttribute('src', ("https:" == document.location.protocol ? "https://" : "http://") + "cdn.uservoice.com/javascripts/widgets/tab.js");
    document.getElementsByTagName('head')[0].appendChild(s);
}

_loadSuper = window.onload;
window.onload = (typeof window.onload != 'function') ? _loadUserVoice : function() { _loadSuper(); _loadUserVoice(); }; 