﻿// Change the Size of the Document text
function changeFontSize(size){
  // Set the document size
  document.body.style.fontSize = size;
  
  // Set the cookie
  setCookie('style', new Array({name: 'fontSize', value: size}),
    90, null, '/');

  return false;
}

// Format a Number
function formatNumber(value, decimals){
  value = value.toString();
  var re = /([\d]+)([\d]{3})/;
  while (re.test(value)){
    value = value.replace(re, '$1,$2');
  }
  return value;
}

// Return the Querystring
function getQueryString(){
  var qs = new Array();

  // Get the start of the Querystring
  var start = document.location.href.indexOf('?');
  if (start == -1) return qs;
  start++;

  // Get the querystring part of the url
  var str = document.location.href
  str = str.substring(start, str.length);
  
  // Break the querystring into it's parts
  var parts = str.split('&');
  for (var i = 0; i < parts.length; i++){
    var comp = parts[i].split('=');
    var name = comp[0];
    var value = (comp.length == 1) ? '' : comp[1];
    
    if (qs[name] == null){
      // Add the name, value
      qs[name] = value;
    }else{
      // Append the value to the name
      qs[name] += ',' + value;
    }
  }
  
  return qs;
}

// Test if a Values is numeric
function isNumeric(value){
  // First test if it's null
  if (value == null) return false;
  
  var re = /^([\d]+([\.][\d]*)?|[\.][\d]+)$/;
  return re.test(value);
}

// Print a HTML Object
function printObject(obj, title){
  // Create the new windows
  var win = window.open();
  win.document.open();
  
  // Duplicate the CSS
  for (var i = 0; i < document.styleSheets.length; i++){
    // Check if the current Stylesheet is inline
    var origSS = document.styleSheets[i];

    // We're going to use invalid HTML to create this
    // Styles will be put in the body
    win.document.write('<style type=\'text/css\'>');
    if (origSS.cssText){
      win.document.write(origSS.cssText);
    }else{
      for (var j = 0; j < origSS.cssRules.length; j++){
        var rule = origSS.cssRules[i];
        win.document.write(origSS.cssRules[j].cssText);
      }
    }
    win.document.write('</style>');
  }
  
  // Duplicate the Object
  win.document.write('<' + obj.tagName);
  for (var i = 0; i < obj.attributes.length; i++){
    win.document.write(' ' + obj.attributes[i].name + '=\'' +
      obj.attributes[i].nodeValue + '\'');
  }
  win.document.write('>' + obj.innerHTML + '</' + obj.tagName + '>');
  win.document.close();

  // Set the title
  win.document.title = title;

  win.print();
}

// Set a cookie
function setCookie(name, values, days, domain, path, secure){
  // Set the expiry
  var d;
  if (days){
    d = new Date();
    d.setDate(d.getDate() + days);
  }
  
  // Write the cookie
  var cookie = name + '=';
  for (var i = 0; i < values.length; i++){
    if (i > 0){
      cookie += '&';
    }
    cookie += values[i].name + '=' + values[i].value;
  }
  cookie += (d ? '; expires=' + d.toGMTString() : '') +
    (path ? '; path=' + path : '') +
    (domain ? '; domain=' + domain : '') +
    (secure ? '; secure=' + secure : '');
    
  document.cookie = cookie;
}

// Set the value of a select object
function setSelectValue(sel, value){
  for (var i = 0; i < sel.options.length; i++){
    if (sel.options[i].value == value){
      sel.selectedIndex = i;
      return;
    }
  }
  // If there was not match, set the first item
  sel.selectedIndex = 0;
}

// Trim a string
function trim(value){
  // Return a null object if null was passed
  if (value == null) return null;
  
  return value.replace(/^[\s]*(.*?)[\s]*$/, '$1');
}
