﻿/* Prototypes to extend the standard string object */
String.prototype.startsWith = function(str) {
   return (this.match("^" + str) == str);
};

String.prototype.trim = function() {
   return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
};

String.prototype.endsWith = function(str) {
   return (this.match(str + "$") == str);
};

/* Master page functions */
var timerHideCustomerName = null;
var timerShowProgress = null;
function startWorking() {
   document.body.style.cursor = 'wait';
   if (document.getElementById('divCustomerName') && document.getElementById('divProgress')) {
      timerHideCustomerName = window.setTimeout("document.getElementById('divCustomerName').style.display = 'none'", 500);
      timerShowProgress = window.setTimeout("document.getElementById('divProgress').style.display = 'block'", 500);
   }
}
function startWorkingNoDelay() {
   document.body.style.cursor = 'wait';
   if (document.getElementById('divCustomerName') && document.getElementById('divProgress')) {
      document.getElementById('divCustomerName').style.display = 'none';
      document.getElementById('divProgress').style.display = 'block';
   }
}
function stopWorking() {
   document.body.style.cursor = 'auto';
   if (timerHideCustomerName != null) {
      window.clearTimeout(timerHideCustomerName);
      timerHideCustomerName = null;
   }
   if (timerShowProgress != null) {
      window.clearTimeout(timerShowProgress);
      timerShowProgress = null;
   }

   if (document.getElementById('divCustomerName') && document.getElementById('divProgress')) {
      document.getElementById("divCustomerName").style.display = "block";
      document.getElementById("divProgress").style.display = "none";
   }
}
/* General utility methods */
function addHandler(target, eventName, handlerName) { 
   if (target.addEventListener)
      target.addEventListener(eventName, handlerName, false);
   else if (target.attachEvent)
      target.attachEvent("on" + eventName, handlerName);
    else
      target["on" + eventName] = handlerName;
}
function removeHandler(target, eventName, handlerName){
   if (target.removeEventListener)
      target.removeEventListener(eventName, handlerName, false);
   else if (target.detachEvent)
      target.detachEvent("on" + eventName, handlerName);
   else
      target["on" + eventName] = "";
} 
function getClass(objID) {
   var obj = document.getElementById(objID);
   if (obj)
      return obj.className;
   else
     return '';
}

function setClass(objID, className) {
   var obj = document.getElementById(objID);
   if (obj)
      obj.className = className;
}
function elementStringAdd(elementString, element) {
   if (element == null)
      return elementString;
   if (elementString == null)
      return element;

   var elements = elementString.split(' ');
   var first = true;
   var exists = false;
   var newElementString = '';

   for (var x = 0; x < elements.length; x++) {
      var e = elements[x];

      if (!first)
         newElementString += ' ';
      else
         first = false;

      newElementString += e;
      if (e == element)
         exists = true;
   }
   if (!exists) {
      if (!first)
         newElementString += ' ';
      newElementString += element ;
   }

   return newElementString;
}
function elementStringRemove(elementString, element) {
   if (elementString == null || element == null)
      return elementString;

   var elements = elementString.split(' ');
   var first = true;
   var newElementString = '';

   for (var x = 0; x < elements.length; x++) {
      var e = elements[x];

      if (e != element) {
         if (!first)
            newElementString += ' ';
         else
            first = false;

         newElementString += e;
      }
   }

   return newElementString;
}

function viewport() {
   return {
      height: -1,
      width: -1
   };
}

function getViewPortSize() {
   var vp = new viewport();

   try {
      // The more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
      if (typeof window.innerWidth != 'undefined') {
         vp.width = window.innerWidth;
         vp.height = window.innerHeight;
      }
      // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
      else if (typeof document.documentElement != 'undefined'
      && typeof document.documentElement.clientWidth != 'undefined'
      && document.documentElement.clientWidth != 0) {
         vp.width = document.documentElement.clientWidth;
         vp.height = document.documentElement.clientHeight;
      }
      // Older versions of IE
      else {
         vp.width = document.getElementsByTagName('body')[0].clientWidth;
         vp.height = document.getElementsByTagName('body')[0].clientHeight;
      }
   }
   catch(e) {
      vp = new viewport();
   }

   return vp;
}

/* RAD Window methods */
window.__radOpenWindowFunction = undefined;
function openModalWndAfterPostback(url, width, height, title) {
   window.__radOpenWindowFunction = function() {
      openModalWnd(url, width, height, title);
      Sys.Application.remove_load(__radOpenWindowFunction);
   };
   Sys.Application.add_load(__radOpenWindowFunction);
}
function openModalWnd(url, width, height, title, windowName) {
   if (!windowName || windowName == "")
      windowName = "wndModal"; 
   var oWnd = GetRadWindowManager().getWindowByName(windowName);
   oWnd.setSize(width, height);
   oWnd.setUrl(url);
   if (title)
      oWnd.set_title(title);
      
   oWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Resize);
   oWnd.show();
   return oWnd;
}
function openModalWnd2(url, width, height, title) {
   openModalWnd(url, width, height, title, "wndModal2");
}
function openModalWndWithoutClose(url, width, height, title) {
   var oWnd = openModalWnd(url, width, height, title, "wndModal");
   oWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Move);
}
function openModalWndShowLoading(url, width, height, title) {
   openModalWnd(url, width, height, title, "wndModalShowLoading");
}
function openModalWndShowLoadingWithMaximize(url, width, height, title) {
   var oWnd = openModalWnd(url, width, height, title, "wndModalShowLoading");
   oWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Maximize);
}
function openModalWndNoOverlay(url, width, height, title) {
   openModalWnd(url, width, height, title, "wndModalNoOverlay");
}
function openModalWndPdf(url, width, height, title) {
   openModalWnd(url, width, height, title, "wndModalPdf");
}
function openModalWndEditor(url) {
   // Open the window to nearly fill the viewport (viewportsize - 40)
   var vp = getViewPortSize();
   if (vp.height < 0) {
      vp.height = 640;
      vp.width = 840;
   }
   var oWnd = openModalWnd(url, vp.width - 40, vp.height - 40, "", "wndModalEditor");
   oWnd.set_behaviors(Telerik.Web.UI.WindowBehaviors.Move + Telerik.Web.UI.WindowBehaviors.Resize);
}
window.__radAlertFunction = undefined;
function openRadAlertAfterPostback(message, title, callbackfunction) {
   window.__radAlertFunction = function() {
      var oWnd = radalert(message, 300, 75, title);
      if (callbackfunction)
         oWnd.add_close(callbackfunction);
      Sys.Application.remove_load(__radAlertFunction);
   };
   Sys.Application.add_load(__radAlertFunction);
}
function openRadAlert(message, title) {
   radalert(message, 300, 75, title);
}
function openRadConfirm(message, title, callbackfunction) {
   return radconfirm(message, callbackfunction, 300, 75, '', title);
}
function getRadWindow() {
   var oWindow = null;
   if (window.radWindow) oWindow = window.radWindow;
   else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
   return oWindow;
}

/* Radwindows with overlay have white corners in Firefox - color them the same as the edge of the window,
 * making the rounded corners appear square.
 * This is annoying, but it's a Telerik workaround for a Firefox bug, where the only html element that
 * will be displayed over a swf (f.eks. a pdf viewer for letter activities) is an iframe.
 */
function onClientWindowShow(sender, args) {
   var overlay = sender.get_popupElement()._hideWindowedElementsIFrame;
   if (!overlay) return;
   Sys.UI.DomElement.addCssClass(overlay, "radWindowOverlayBackgroundColor");
} 
