﻿var $j = jQuery.noConflict();
function ShowUploading()
{
  // prevent multiple clicks on submit button
  if ($j('#upload').is(':visible'))
    return false;
  
  $j('#upload').show('slow');
  return true;
}

function ShowError()
{
  $j('#upload').hide();
}

String.prototype.wordCount = function() {
  var count = 0;
  var result = this.match(/\s+/g);
  if (result != null)
    count = result.length;
  return count;
}

function ShowRemainingWordCount(textbox, label, maxWords, e)
{
  var words = $j(textbox).val().wordCount();
  var diff = maxWords - words;
  
  if (diff <= 0)
  {
     var words_typed = $j(textbox).val().split(" ");
     var limited_string = "";
     for (var i=0; i < maxWords; i++)
     {
       limited_string = limited_string + words_typed[i] + " ";
     }
     $j(textbox).val(limited_string);
     $j(textbox).focus();
     
     // re-calc word count difference
     words = $j(textbox).val().wordCount();
     diff = maxWords - words;
  }
  
  $j(label).html(diff + " words remaining");
  
  return (diff > 0);
}



var bName = navigator.appName;
function taLimit(taObj, Cnt, maxL)
{
    objCnt = createObject(Cnt);
    if (taObj.value.length > maxL)
        taObj.value = taObj.value.substring(0, maxL);
    else
    {
        if(bName == "Netscape")	
            objCnt.textContent=maxL-taObj.value.length;
        else
            objCnt.innerText=maxL-taObj.value.length;
    }
}

function createObject(objId)
{
    if (document.getElementById) return document.getElementById(objId);
    else if (document.layers) return eval("document." + objId);
    else if (document.all) return eval("document.all." + objId);
    else return eval("document." + objId);
}

function Roll(imgID, imgName)
{
    document.getElementById(imgID).setAttribute("src", imgName);
}


var checkBing = function() {
  if (typeof(WLSearchBoxScriptReady) !== 'undefined') {
    try { WLSearchBoxScriptReady(); } catch (e) { }
  } else {
    setTimeout(checkBing, 2000);
  }
}


var CatchKey = function(id, event) {
  var btn = document.getElementById(id);
  if (event.which == 13) {           
     event.returnValue=false;
     event.cancel = true;
     event.cancelBubble = true;
     btn.click();
  }
}

var WLSearchTemp;

jQuery(function($) {
    checkBing(); 

    if ($('#recaptcha_response_field').length > 0) {
        $('#recaptcha_response_field').keydown(function(event) {
          if (event.which == 13) {
             event.returnValue=false;
             event.cancel = true;
             event.cancelBubble = true;
             $('input.submit').click();
          }   
        });
    }
  
    $j("input:not('#WLSearchBoxInput')").focus(function() {
        if (WLSearch.Browser.IsFireFoxMozilla()) {
            WLSearchTemp = WLSearch;
            WLSearch = null;
        }
    }).blur(function() {
        if (WLSearchTemp != null) {
            WLSearch = WLSearchTemp;
            WLSearchTemp = null;
        }
    });
  
});
