
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
  if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function toggleBorder(obj, color) {
  obj.style.border = color + ' 1px solid';
}

function popup(url, name, width, height) {
  wleft = (screen.width - width) / 2;
  wtop = (screen.height - height) / 2;
  var settings = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width="
                 + width + ",height=" + height + ',left=' + wleft + ',top=' + wtop; 
  window.open(url, name, settings);
}

function augment(oSelf, oOther) {
  if (oSelf == null) {
    oSelf = {};
  }
  for (var i = 1; i < arguments.length; i++) {
    var o = arguments[i];
    if (typeof(o) != 'undefined' && o != null) {
      for (var j in o) {
        oSelf[j] = o[j];
      }
    }
  }
  return oSelf;
}

function cleanPercentage(obj) {
  num = obj.value;
  if (num == Infinity) {
    num = "0";
  } else {
    num = num.toString().replace(/\%|\,/g,'');
    if (isNaN(num)) {
      num = "0";
    }
  }
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num * 100 + 0.50000000001);
  dec = num % 100;
  num = Math.floor(num / 100).toString();
  if (dec < 10) {
    dec = "0" + dec;
  }
  return parseFloat(((sign)?'':'-') + num + '.' + dec);
}

function cleanCurrency(obj) {
  num = obj.value;
  if (num == Infinity) {
    num = "0";
  } else {
    num = num.toString().replace(/\$|\,/g,'');
    if (isNaN(num)) {
      num = "0";
    }
  }
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num * 100 + 0.50000000001);
  cents = num % 100;
  num = Math.floor(num / 100).toString();
  if (cents < 10) {
    cents = "0" + cents;
  }
  return parseFloat(((sign)?'':'-') + num + '.' + cents);
}

function cleanNumber(obj) {
  num = obj.value;
  if (num == Infinity) {
    num = "0";
  } else {
    num = num.toString().replace(/\,/g,'');
    if (isNaN(num)) {
      num = "0";
    }
  }
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num * 100 + 0.50000000001);
  num = Math.floor(num / 100).toString();
  return parseFloat((((sign)?'':'-') + num));
}

function cleanFloat(obj) {
  num = obj.value;
  if (num == Infinity) {
    num = "0";
  } else {
    num = num.toString().replace(/\,/g,'');
    if (isNaN(num)) {
      num = "0";
    }
  }
  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num * 100 + 0.50000000001);
  dec = num % 100;
  num = Math.floor(num / 100).toString();
  if (dec < 10) {
    dec = "0" + dec;
  }
  return parseFloat((((sign)?'':'-') + num + '.' + dec));
}

function formatNumber(obj, oOptions) {
  var oOptions = augment({format: "#,#"}, oOptions);
  var num = cleanNumber(obj);
  obj.value = num.numberFormat(oOptions.format);
}

function formatCurrency(obj, oOptions) {
  var oOptions = augment({format: "$#,#.00"}, oOptions);
  var num = cleanCurrency(obj);
  obj.value = num.numberFormat(oOptions.format);
}

function formatFloat(obj, oOptions) {
  var oOptions = augment({format: "#,#.00"}, oOptions);
  var num = cleanFloat(obj);
  obj.value = num.numberFormat(oOptions.format);
}

function formatPercentage(obj, oOptions) {
  var oOptions = augment({format: "#.00%"}, oOptions);
  var num = cleanPercentage(obj);
  obj.value = (num / 100).numberFormat(oOptions.format);
}

function urlEncode(sStr) {
  return escape(sStr).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
}

function SetCookie(cookieName,cookieValue,nDays) {
  var today = new Date();
  var expire = new Date();
  if (nDays==null || nDays==0) nDays=1;
  expire.setTime(today.getTime() + 3600000*24*nDays);
  document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
}

function ValidateColor(string) {
  string = string || '';
  string = string + "";
  string = string.toUpperCase();
  var chars = '0123456789ABCDEF';
  var out   = '';
  for (var i=0; i<string.length; i++) {
    var schar = string.charAt(i);
    if (chars.indexOf(schar) != -1) {
      out += schar;
    }
  }
  if (out.length != 6) {
    return null;
  }
  return out;
}

function updateColorSample(txtid, sampleid) {
  var color = ValidateColor(document.getElementById(txtid).value);
  if (color != null) {
    document.getElementById(sampleid).style.backgroundColor = '#' + color;
  }
}

function updateTinyMCEObjectForAJAX(targetGridId, editorContainerId, editorId, theData) {
  var theDiv = document.getElementById(targetGridId);
  if (tinyMCE.getInstanceById(editorId)) {
    var containerNode = document.getElementById(editorContainerId);
    var editorNode = document.getElementById(editorId);
    if (editorNode != null) {
      containerNode.removeChild(editorNode);
    }
    try {
      tinyMCE.remove(tinyMCE.getInstanceById(editorId));
    } catch (err) { }
  }
  theDiv.innerHTML = theData;
  tinyMCE.idCounter = 0;
  tinyMCE.execCommand("mceAddControl", true, editorId);
}

function loadMap(elementid, streetaddress, cityaddress, locationname) {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById(elementid));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());      
    geocoder = new GClientGeocoder();
    geocoder.getLatLng(
      streetaddress,
      function(point) {
        if (point) {
          map.setCenter(point, 13);
          var marker = new GMarker(point);
          map.addOverlay(marker);
          //marker.openInfoWindowHtml("<h3>" + locationname + "</h3><br>" + streetaddress);
        } else {
            geocoder.getLatLng(
              cityaddress,
              function(point) {
                if (point) {
                  map.setCenter(point, 9);
                  var marker = new GMarker(point);
                  map.addOverlay(marker);
                  //marker.openInfoWindowHtml("<h3>" + locationname + "</h3><br>" + cityaddress);
                }/* else {
                  document.getElementById(elementid).style.display = "none";
                }*/
              }
            );
        }
      }
    );
  }
}

function insertAtCursor(myField, myValue) { 
  if (document.selection) { 
    myField.focus(); 
    sel = document.selection.createRange(); 
    sel.text = myValue; 
  } else if (myField.selectionStart > 0) { 
    var startPos = myField.selectionStart; 
    var endPos = myField.selectionEnd; 
    myField.value = myField.value.substring(0,startPos) + myValue + myField.value.substring(endPos, myField.value.length); 
  } else { 
    myField.value += myValue; 
  } 
} 

function limitWordCount(theEditFieldID, theLimit) {
  var theEditField = document.getElementById(theEditFieldID);
  var char_count = theEditField.value.length;
  var fullStr = theEditField.value + " ";
  var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
  var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
  var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9.!]+/gi;
  var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
  var splitString = cleanedStr.split(" ");
  var word_count = splitString.length -1;
  if (fullStr.length < 2) {
    word_count = 0;
  }
  if (word_count <= theLimit) {
    return true;
  } else {
    var newsplitString = splitString.slice(0, theLimit);
    theEditField.value = newsplitString.join(" ");
    return false;
  }
}

function updateWordCount(theEditFieldID, theUpdateFieldID, theLimit) {
  var theEditField = document.getElementById(theEditFieldID);
  var char_count = theEditField.value.length;
  var fullStr = theEditField.value + " ";
  var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
  var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
  var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9.!]+/gi;
  var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
  var splitString = cleanedStr.split(" ");
  var word_count = splitString.length -1;
  if (fullStr.length < 2) {
    word_count = 0;
  }
  if (word_count > theLimit) {
    var newsplitString = splitString.slice(0, theLimit);
    theEditField.value = newsplitString.join(" ");
    word_count = theLimit;
  }
  var theUpdateField = document.getElementById(theUpdateFieldID);
  if (theUpdateField != null) {
    theUpdateField.innerHTML = "(" + word_count.toString() + " of " + theLimit.toString() + ") words";
  }
}

function updateCharacterCount(theEditFieldID, theUpdateFieldID, theLimit) {
  var theEditField = document.getElementById(theEditFieldID);
  var char_count = theEditField.value.length;
  var char_remaining = (theLimit - char_count);
  var theUpdateField = document.getElementById(theUpdateFieldID);
  if (theUpdateField != null) {
    theUpdateField.innerHTML = char_remaining.toString();
    if (char_remaining < 1) {
      theUpdateField.className = "character_counter_negative";
    } else if (char_remaining < 11) {
      theUpdateField.className = "character_counter_close";
    } else {
      theUpdateField.className = "character_counter";
    }
  }
}

function keepAlive(url) {
  AJAXRequestPost(url, "id=" + Math.random(), {updatediv:"keepAliveDiv"});
}

function CopyToClipboard(theInput) {
  var theClipboard = document.getElementById("jvf_clipboard");
  if (theClipboard != null) {
    theClipboard.innerText = Base64.decode(theInput);
    Copied = theClipboard.createTextRange();
    Copied.execCommand("RemoveFormat");
    Copied.execCommand("Copy");
  }
}

function toggleExpandingDiv(controlToggleId, controlShortDivId, controlDivId) {
  var theControlToggle = document.getElementById(controlToggleId);
  if (theControlToggle.src.indexOf("arrow_up") != -1) {
    theControlToggle.src = theControlToggle.src.replace(new RegExp("arrow_up"), "arrow_down");
    $("#" + controlDivId).hide();
    $("#" + controlShortDivId).show;
  } else {
    theControlToggle.src = theControlToggle.src.replace(new RegExp("arrow_down"), "arrow_up");
    $("#" + controlDivId).show();
    $("#" + controlShortDivId).hide();
  }
}

function loadURL(theURL) {
  window.location.href = theURL; 
}

function GenerateThumbnailLink(theURL, theFilename, theFilePath, theHeightControlID, theWidthControlID, theLinkControlID) {
  var theHeight = document.getElementById(theHeightControlID);
  var theWidth = document.getElementById(theWidthControlID);
  var theLink = document.getElementById(theLinkControlID);
  if ((theHeight != null) && (theWidth != null) && (theLink != null)) {
    theLink.value = theURL + Base64.encode("height=" + theHeight.value + "|width=" + theWidth.value + "|file=" + theFilePath + "|fh=1") + "/" + theFilename;
  }
}

function toggleObject(controlToggleId) {
  var theControlToggle = document.getElementById(controlToggleId);
  var theValue = theControlToggle.getAttribute("toggle");
  if (theValue == "1") {
    theControlToggle.setAttribute("toggle", "0");
    $("#" + controlToggleId).show();
    return true;
  } else {
    theControlToggle.setAttribute("toggle", "1");
    $("#" + controlToggleId).hide();
    return false;
  }
}

function toggleObjectWithLabel(controlToggleId, labelId, onText, offText) {
  var theControlToggle = document.getElementById(controlToggleId);
  var theLabel = document.getElementById(labelId);
  var theValue = theControlToggle.getAttribute("toggle");
  if (theValue == "0") {
    if (theLabel != null) {
      theLabel.innerHTML = onText;
    }
    theControlToggle.setAttribute("toggle", "1");
    $("#" + controlToggleId).hide();
    return false;
  } else {
    if (theLabel != null) {
      theLabel.innerHTML = offText;
    }
    theControlToggle.setAttribute("toggle", "0");
    $("#" + controlToggleId).show();
    return true;
  }
}

function toggleObjectAndInputWithLabel(controlToggleId, labelId, inputId, onText, offText) {
  var theInput = document.getElementById(inputId);
  if (theInput != null) {
    theInput.value = toggleObjectWithLabel(controlToggleId, labelId, onText, offText);
  }
}

function toggleRangeFields(selectId, controlToggleId) {
  var theSelect = document.getElementById(selectId);
  if (theSelect != null) {
    if (theSelect.value == "B") {
      $("#" + controlToggleId).show();
    } else {
      $("#" + controlToggleId).hide();
    }
  }
}

function updateChildSelect(selectId, updateControlId, url) {
  var theSelect = document.getElementById(selectId);
  if (theSelect != null) {
    AJAXRequest(url + theSelect.value, {updatediv:updateControlId});
  }
}

function startAJAXDivUpdate(indicatorid, containerid, req) {
  var progressDiv = document.getElementById(indicatorid);
  var divView = document.getElementById(containerid);
  progressDiv.style.width = divView.offsetWidth + "px";
  progressDiv.style.height = divView.offsetHeight + "px";
  $("#" + indicatorid).show();
  $("#" + indicatorid).fadeTo("fast", 0.5);
}
/*
function endAJAXDivUpdate(indicatorid, containerid, req) {
  if (req != null) {
    var theDiv = document.getElementById(containerid);
    theDiv.innerHTML = req.responseText;
  }
  $("#" + indicatorid).hide();
}
*/