/**
 * Some frequently used patterns
 */
var PATTERN_PNG = new RegExp("^(.*)(\\.png)(\\?(.*))?$", "i");
var PATTERN_EMAIL = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?)$/i);

/**
 * Some frequently used constants
 */
var URL_IMG_1x1_TRANSPARENT = "/v2/img/spacer.gif";

/**
 * selects text in elemenr by its given id
 * @param id
 */
function selectText(id) {
   jQuery(document).ready(function() {
      jQuery("#" + id).select();
   });
}

/**
 * validates email
 * @param emailAddress
 */
function isValidEmailAddress(emailAddress) {
   return PATTERN_EMAIL.test(emailAddress);
}

function setBgColor(e, color) {
   if (e != null) {
      e.style.backgroundColor = color;
   }
}

function checkMaxLength(filedId, indicatorId, maxLength) {
   jQuery(document).ready(function() {
      var field = jQuery("#" + filedId);
      var indicator = jQuery("#" + indicatorId);
      var inputText = field.val();
      var inputLength = inputText.length;

      if (inputLength > maxLength) {
         field.val(inputText.substring(0, maxLength));
         indicator.html(0);
      } else {
         indicator.html(maxLength - inputLength);
      }
   });
}

// string trim method implementations
String.prototype.trim = function() {
   return this.replace(/(^\s*)|(\s*$)/g, "");
}

String.prototype.ltrim = function() {
   return this.replace(/(^\s*)/g, "");
}

String.prototype.rtrim = function() {
   return this.replace(/(\s*$)/g, "");
}

// get instance of XMLHttpRequest
function getHttpRequest() {
   var xmlHttp = null;

   try {
      xmlHttp = new XMLHttpRequest();
   } catch (e) {
      var progIds = ["Microsoft.XMLHTTP", "MSXML2.XMLHTTP", "MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0"]
      var success = false;

      for (var iterator = 0; (iterator < progIds.length) && ( ! success); iterator ++) {
         try {
            xmlHttp = new ActiveXObject(progIds[iterator]);
            success = true;
         } catch (e) {
         }
      }
      if (!success) {
         return null;
      }
   }

   return xmlHttp;
}


// remove child nodes in given DOM element
function removeChildNodes(e) {
   if (e != null) {
      while (e.hasChildNodes()) {
         e.removeChild(e.firstChild);
      }
   }
}

function isArray(o) {
   if (o instanceof Array) {
      return true;
   } else {
      return typeof(o.length) == "number";
   }
}

// returns top position of provided element
function findPosX(o) {
   var curleft = 0;

   if (o.offsetParent) {
      while (o.offsetParent) {
         curleft += o.offsetLeft
         o = o.offsetParent;
      }
   } else if (o.x) {
      curleft += o.x;
   }

   return curleft;
}

// returns left position of provided element
function findPosY(o) {
   var curtop = 0;

   if (o.offsetParent) {
      while (o.offsetParent) {
         curtop += o.offsetTop
         o = o.offsetParent;
      }
   } else if (o.y) {
      curtop += o.y;
   }

   return curtop;
}

/* StringBuffer class */
function StringBuffer() {
   this.buffer = [];
   this.bufferLength = 0;
}

StringBuffer.prototype.append = function(string) {
   this.buffer.push(string);

   if (string != null) {
      this.bufferLength += string.length;
   }

   return this;
}

StringBuffer.prototype.toString = function() {
   return this.buffer.join("");
}

StringBuffer.prototype.length = function() {
   return this.bufferLength;
}

function insertAfter(newNode, afterNode) {
   if (newNode == null || afterNode == null) {
      return null;
   }

   var parent = afterNode.parentNode;
   var offset = 0;
   var insert = false;

   var nodes = [];

   for (var i = 0; i < parent.childNodes.length; i++) {
      if (parent.childNodes[i].nodeType == 1) {
         nodes.push(parent.childNodes[i]);

         if (parent.childNodes[i] == afterNode) {
            offset = nodes.length - 1;
         }
      }
   }

   if (nodes.length > offset + 1) {
      insert = true;
   }

   if (insert) {
      return parent.insertBefore(newNode, nodes[offset + 1]);
   } else {
      return parent.appendChild(newNode);
   }
}

function fs(url) {
   jQuery(document).ready(function() {
      if (jQuery.trim(url) == "") {
         return;
      }

      var f = jQuery("#videoAcknowledgementForm");
      f.attr("action", url);
      f.submit();
   });
}


// redirects to login page (depricated?)
function doLogin(url) {
   if (url != null && url.trim().length > 0) {
      window.parent.location.href = url;
   }
}


