function ifnn(sInput) {if (sInput != null) return sInput; else return '';}; //"if not null" function
function checked(sInput,sValue) {if (sInput != null) if (sInput==sValue) return 'CHECKED'; else return '';};
function selected(sInput,sValue) {if (sInput != null) if (sInput==sValue) return 'SELECTED'; else return '';};
function checkText(oTextbox,nMinLength,nMaxLength) {if (oTextbox != null) if (isValidLength(oTextbox.value,nMinLength,nMaxLength)) oTextbox.style.color='Black'; else oTextbox.style.color='red';}
function checkNumber(oTextbox,nMinValue,nMaxValue,bWholeNumber) {if (oTextbox != null) if (isNumberInRange(oTextbox.value,nMinValue,nMaxValue,bWholeNumber)) oTextbox.style.color='Black'; else oTextbox.style.color='red';}
function checkDate(oTextbox, sMask, bFuture) {
  if (arguments.length < 2) sMask = null;
  if (arguments.length < 3) bFuture = false;
  var oToday = new Date(); 
  if (oTextbox != null) 
    if (isDate(oTextbox.value) && (!bFuture || getDate(oTextbox.value) >= oToday))
      oTextbox.style.color='Black';
    else
      oTextbox.style.color='red';
}
function checkDateTime(oTextbox, bFuture) {
  if (arguments.length < 2) bFuture = false;
  var oToday = new Date(); 
  if (oTextbox != null) 
    if (isDateTime(oTextbox.value) && (!bFuture || getDate(oTextbox.value) >= oToday))
      oTextbox.style.color='Black';
    else
      oTextbox.style.color='red';
}
function checkTime(oTextbox) {if (oTextbox != null) if (isTime(oTextbox.value)) oTextbox.style.color='Black'; else oTextbox.style.color='red';}
function checkEmail(oTextbox) {if (oTextbox != null) if (isValidEmail(oTextbox.value)) oTextbox.style.color='Black'; else oTextbox.style.color='red';}
function checkEmailList(oTextbox) {if (oTextbox != null) if (isValidEmailList(oTextbox.value)) oTextbox.style.color='Black'; else oTextbox.style.color='red';}
function checkPhone(oTextbox, sCountryCode) {if (oTextbox != null) if (isValidPhone(oTextbox.value, sCountryCode)) oTextbox.style.color='Black'; else oTextbox.style.color='red';}
function checkURL(oTextbox) {if (oTextbox != null) if (isValidURL(oTextbox.value)) oTextbox.style.color='Black'; else oTextbox.style.color='red';}
function checkState(oTextbox, sCountryCode) {if (oTextbox != null) if (isValidState(oTextbox.value, sCountryCode)) oTextbox.style.color='Black'; else oTextbox.style.color='red';}
function checkZip(oTextbox, sCountryCode) {if (oTextbox != null) if (isValidZip(oTextbox.value, sCountryCode)) oTextbox.style.color='Black'; else oTextbox.style.color='red';}
function reformatDate(oTextbox, sFormatMask, sCheckMask, bAssumeFuture) {
	if (arguments.length < 2 || sFormatMask == null) sFormatMask = 'MM/DD/YYYY';
	if (arguments.length < 3) sCheckMask = null;
	if (arguments.length < 4) bAssumeFuture = true;
  if (sCheckMask != null)
    if (!isDate(oTextbox.value, sCheckMask)) return;
  else
    if (!isDate(oTextbox.value)) return;
  var oDate = getDate(oTextbox.value);
  if (bAssumeFuture) {
  	if (!oTextbox.value.match(/\d{4}/)) {
      var oToday = new Date();
      if (oDate) if (oDate.getFullYear) if (oDate.getFullYear() < (oToday.getFullYear()-50)) oDate.setFullYear(oDate.getFullYear()+100);
    }
  }
  oTextbox.value = formatDate(oDate, sFormatMask);
}
function reformatTime(oTextbox, sFormatMask) {
	if (arguments.length < 2) sFormatMask = 'HH:MI AM';
  if (isTime(oTextbox.value)) oTextbox.value = formatTime(oTextbox.value, sFormatMask);
}
function reformatPhone(oTextbox, sCountryCode, sMask) {
	if (arguments.length < 2) sCountryCode = 'US';
	if (arguments.length < 3) sMask = '(###) ###-####';
  if (isValidPhone(oTextbox.value, sCountryCode)) oTextbox.value = formatPhone(oTextbox.value, sMask);
}
function reformatNumber(oTextbox, nDecimals) {
	if (arguments.length < 2) nDecimals = 0;
	if (isNumeric(oTextbox.value)) oTextbox.value = formatNumber(oTextbox.value, nDecimals);
}
function reformatZip(oTextbox, sCountryCode) {
  if (arguments.length < 2) sCountryCode = 'US';
  var sValue = oTextbox.value;
  if (!isValidZip(sValue, sCountryCode)) return;
  switch (sCountryCode) {
    case 'US':
      if (sValue.length > 5 && sValue.substring(5,6) != '-') oTextbox.value = sValue.substring(0, 5)+'-'+sValue.substring(5);
      break;
    case 'CA':
      if (sValue.substring(3, 4) != ' ') sValue = sValue.substring(0, 3)+' '+sValue.substring(3);
      oTextbox.value = sValue.toUpperCase();
      break;
    default:
      oTextbox.value = sValue.toUpperCase();
  }
}
if (document.getElementById) {
  var hidden = "hidden";
  var visible = "visible";
}
else if (document.layers) {
  var hidden = "hide";
  var visible = "show";
} else {
  var hidden = "hidden";
  var visible = "visible";
}
var toggle = "toggle";
function changeVisibility() {
  if (!document.getElementById && !document.layers && !document.all) return;
  var inc, endInc=arguments.length;
  for (inc=0; inc<endInc; inc+=2) {
    var daObj = checkIn(arguments[inc]);
    if (document.getElementById || document.all)
      daObj = daObj.style;
    if (arguments[inc+1] == hidden) {
      daObj.visibility = hidden;
    } else if (arguments[inc+1] == visible) {
      daObj.visibility = visible;
    } else if (arguments[inc+1] == toggle) {
      if (daObj.visibility == visible) {
        daObj.visibility = hidden;
      } else if (daObj.visibility == hidden) {
        daObj.visibility = visible;
      }
    }
  }
}
function checkIn(WM_id) { 
  var theObj,ss,sr,i,j,WM_layers=new Array();
  if (document.getElementById) {
    theObj = document.getElementById(WM_id);
    if (theObj == null) {
      var objName = 'document.' + WM_id;
      theObj = eval(objName);
    }
  }
  if (document.all) {
    if((document.all[WM_id].style.position != 'absolute') && (document.all[WM_id].style.position != 'relative')) {
      for (ss=0 ; ss < document.styleSheets.length; ss++) {
        for (sr=0 ; sr < document.styleSheets(ss).rules.length; sr++) { 
          if (document.styleSheets(ss).rules(sr).selectorText == '#' + WM_id) {
            theObj = document.styleSheets(ss).rules(sr).style;
            break;
          }
        }
      }
    } 
    else {
      theObj = document.all[WM_id];
    }
  } 
  else if(document.layers) {
    WM_layers = new Array();
    with (document) {
      for (i=0; i<layers.length; i++) WM_layers[i]=layers[i]; {
        for (i=0; i<WM_layers.length; i++) {
          if (WM_layers[i].document && WM_layers[i].document.layers) {
            for (j=0; j<WM_layers[i].document.layers.length; j++) {
              WM_layers[WM_layers.length] = WM_layers[i].document.layers[j];
            }
            if(WM_layers[i].name == WM_id) {
              theObj = WM_layers[i];
            }
          }
        }
      }
    }
  }
  return theObj;
}
function formCode(adder) {
  var objToday = new Date();
  var objCode = randomNumber(1,9)+padLeft(objToday.getUTCMinutes(), 2, '0')+padLeft(objToday.getUTCDate(), 2, '0')+padLeft(objToday.getUTCMonth()+1, 2, '0')+padLeft(objToday.getUTCHours(), 2, '0')+padLeft(objToday.getUTCSeconds(), 2, '0');
  objCode = Number(objCode) * 2;
  objCode += adder;
  objCode = randomCharacters(4)+String(objCode)+randomCharacters(4);
  return objCode;
}
function randomNumber(minimum, maximum) {
  var intDifference = maximum - minimum + 1;
  return (Math.floor(Math.random() * 1000) % intDifference) + minimum;
}
function randomCharacters(characters) {
  var strOut = '';
  for (var c=0; c<characters; c++) {
    strOut += String.fromCharCode(randomNumber(65, 90));
  }
  return strOut;
}
function padLeft(input, digits, character) {
  var strOut = String(input);
  while (strOut.length < digits) {
    strOut = character+''+strOut;
  }
  return strOut;
}

document.observe("dom:loaded", function() {
  // Add Widget Button
  function addWidgetCallback(sender, args) {
    if (sender) args.editor.PasteHtml(sender);
  }
  try {
    if (RadEditorCommandList) {
      RadEditorCommandList["Widget"] = function(commandName, editor, oTool) { 
        if (editor)
          editor.ShowDialog(
            '/Admin/AddWidget.aspx',
            'test',
            600,
            450,
            addWidgetCallback,
            null,
            'Add Widget'
          );
      }
    }
    if (WidgetFilter) WidgetFilter.registerClass('WidgetFilter', Telerik.Web.UI.Editor.Filter);
  }
  catch (e) {
  }
});

  function RadEditorCustomFilter()
  {         
   this.GetHtmlContent = function (content)
   {    
      var newContent = content;
      newContent = newContent.replace(/(?=<img[^>]*class="widget_placeholder"[^>]*>)<img[^>]*code="([^"]*)"[^>]*>/gi, function ($0, $1) {
        return $1.replace(/&quot;/gi, '"');
      });
      return newContent;
   };       
   this.GetDesignContent = function (content)
   {    
      var newContent = content;
      newContent = newContent.replace(/(?!<code="\[\$(\w*).*\$\]")(\[\$(\w*).*\$\])/gi, function ($0, $1, $2, $3) {
        return '<img src="widget_placeholder_'+$3.toLowerCase()+'.png" class="widget_placeholder" code="'+$2.replace(/"/g, '&quot;')+'" alt="WIDGET - '+$3+'" />';
      });
      //console.log(newContent);
      return newContent;
   };       
  }
  /* Use the editor onload function to register the filter with the FiltersManager */
  function RadEditorClientLoad (editor)
  {
   var customFilter = new RadEditorCustomFilter();
   editor.FiltersManager.Add(customFilter);
   //console.log(editor.ContentArea.innerHTML);
   //var content = editor.ContentArea.innerHTML;
   //editor.ContentArea.innerHTML = customFilter.GetDesignContent(content);
  }