// Copyright (c) 2002-2007 Axoft Group BV. All rights reserved.
// No reproduction or republication without written permission.
// http://www.axoft.nl/

//redirects to url when not IE 5.5 or higher
function RedirectWhenNotIE55(newurl) {
  var ver=0
  if (navigator.appVersion.indexOf("MSIE")!=-1){
    temp=navigator.appVersion.split("MSIE");
    ver=parseFloat(temp[1]);
  }
  if (ver>=5.5); else {
    location.href = newurl;
  }
}

//redirects to url when not IEx or higher
function RedirectWhenLowerThenIE(version, newurl) {
  var ver=0
  if (navigator.appVersion.indexOf("MSIE")!=-1){
    temp=navigator.appVersion.split("MSIE");
    ver=parseFloat(temp[1]);
  }
  if (ver>=version); else {
    location.href = newurl;
  }
}

//mod function (math)
function Mod(a, b) { 
  return a-Math.floor(a/b)*b 
}

//checks if email is valid
function IsValidEmail(emailAddress) {
  var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  return filter.test(emailAddress);
}

//creates new xml object for IE or native object
function NewXMLObj() {
  var MyXMLObj;
  if (window.XMLHttpRequest) {
    MyXMLObj = new XMLHttpRequest();
  } 
  else if (window.ActiveXObject) {
    MyXMLObj = new ActiveXObject("Microsoft.XMLHTTP");
  }
  return MyXMLObj;
}

//object that manages cookies
//example call:
//var CookieObj = new CookieObject();
//CookieObj.ExpireDays = 365; //0 for current browswersession only
//CookieObj.SetCookie('name', 'olav');
function CookieObject() {
  //private functions
  function SetCookie(name, value) {
    var today = new Date();
    var expires = new Date();
    if (this.ExpireDays==null) 
      this.ExpireDays=1;
    if (this.ExpireDays!=0)
      expires.setTime(today.getTime() + 3600000*24*this.ExpireDays);
    else
      expires = null;
    document.cookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((this.Path) ? "; path=" + this.Path : "") +
      ((this.Domain) ? "; domain=" + this.Domain : "") +
      ((this.Secure) ? "; secure" : "");
  }

  function GetCookie(cookieName) { 
    var theCookie=""+document.cookie; 
    var ind=theCookie.indexOf(cookieName); 
    if (ind==-1 || cookieName=="") return ""; 
    var ind1=theCookie.indexOf(';',ind); 
    if (ind1==-1) 
      ind1=theCookie.length; 
    if (theCookie.substring(ind+cookieName.length+1,ind1) == ';') 
      return '';
    else
      return unescape(theCookie.substring(ind+cookieName.length+1,ind1)); 
  }

  function DeleteCookie (name) {
    var exp = new Date();
    var today
    exp.setTime (exp.getTime() - 1);
    var cval = "";
    document.cookie = name + "=" +
      ((this.Path) ? "; path=" + this.Path : "") +
      ((this.Domain) ? "; domain=" + this.Domain : "");
  }
  
  //published properties
  this.ExpireDays = 0;
  this.Domain = window.location.hostname;
  this.Path = '/';
  this.SetCookie = SetCookie;
  this.GetCookie = GetCookie;
  this.DeleteCookie = DeleteCookie;
}

//show current DOM in new window
function ShowCurrentDOM(){
  remote = window.open('','HTML','');
  remote.document.writeln('<html>' + document.body.outerHTML + '</html>');
}

//checks if given value is numeric
function IsNumeric(strString) {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;
   if (strString.length == 0) return false;
   for (i = 0; i < strString.length && blnResult == true; i++) {
     strChar = strString.charAt(i);
     if (strValidChars.indexOf(strChar) == -1) {
       blnResult = false;
     }
   }
   return blnResult;
 }

//removes brackets in guids
function RemoveGUIDBrackets(guid) {
  var tempGUID = guid.replace('{', '');
  return tempGUID.replace('}', '');
}

//formats string (separated) into an Array
function stringToArray(string, separator) {
  var tempString = new String();
  var stringArray = new Array();
  var I = 0;
  tempString = string;
  while (tempString.indexOf(separator)>-1) {
    stringArray[I] = trim(tempString.substr(0, tempString.indexOf(separator)));
    tempString = tempString.substr(tempString.indexOf(separator)+1, tempString.length);
    I++;
  }
  stringArray[I] = (tempString);
  return stringArray;
}

// Removes leading whitespaces
function lTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function rTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim( value ) {
	return lTrim(rTrim(value));
}

