﻿// Javascript file containing common functions used in copybazar.com
//** Note: minify before finall upload

function getIEVersion()
{
 var vUA = window.navigator.userAgent;
 
 var vMSIE = vUA.indexOf ("MSIE ");

 if (vMSIE > 0)      // If Internet Explorer, return version number
  return parseInt (vUA.substring(vMSIE + 5, vUA.indexOf (".", vMSIE )));
 else                 // If another browser, return 0
  return 0;
}


function findAbsolutePos(obj) 
{
 var top = 0;
 var left = 0;
 if (obj.offsetParent) 
 {
  do 
  {
   top += obj.offsetTop;
   left += obj.offsetLeft;
   } while (obj = obj.offsetParent);
  } 

 return [top, left];
}


// Cookie management functions
// Adapted from http://www.quirksmode.org/js/cookies.html

function createCookie(name, value, days) 
{
 if (days) 
 {
  var date = new Date();
  date.setTime(date.getTime() + (days*24*60*60*1000));
  var expires = "; expires=" + date.toGMTString();
  }
 else 
  var expires = "";
 
 document.cookie = name + "=" + encodeURIComponent(value) + expires + "; path=/";
}

function readCookie(name) 
{
 var nameEQ = name + "=";
 var ca = document.cookie.split(';');
 for(var i=0; i < ca.length; i++) 
 {
  var c = ca[i];
  while (c.charAt(0) == ' ') 
   c = c.substring(1, c.length);
  
  if (c.indexOf(nameEQ) == 0) 
   return decodeURIComponent(c.substring(nameEQ.length,c.length));
  }
 
 return null;
}

function deleteCookie(name) 
{
 createCookie(name, "", -1);
}


// regular expression-based functions

function validateDatePattern(pDate)
{
 var rgxDate = /^([1-9]?[0-9]{1,3})\/([0-9]{1,2})\/([0-9]{1,2})$/;
 return rgxDate.test(pDate);
}


// Adapted from http://www.mredkj.com/javascript/numberFormat.html#addcommas

function addThousandSeparator(pInput)
{
 pInput += '';
 vSplit = pInput.split('.');
 vSplit1 = vSplit[0];
 vSplit2 = vSplit.length > 1 ? '.' + vSplit[1] : '';
 var vRegx = /(\d+)(\d{3})/;
 while (vRegx.test(vSplit1)) 
  vSplit1 = vSplit1.replace(vRegx, '$1' + ',' + '$2');
  
 return vSplit1 + vSplit2;
}


// verifies whether all characters of pValue are numeric

function isNumeric(pValue)
{
 var vNumericExpression = /^[0-9]+$/;
 if (pValue.match(vNumericExpression))
  return true;
 else
  return false;
}

