// JavaScript Document

function addEvent(obj, evType, fn)
{
	if (obj.addEventListener)
	{
		obj.addEventListener(evType, fn, false);
		return true;
	}
	else if (obj.attachEvent)
	{
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	}
	else
	{
		return false;
	}
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function getWindowSize()
{
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' )
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) )
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) )
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}		  
	return myWidth;
}

function CheckIfPrintRequested()
{
	invocations++;
	if (invocations > 1)
		return;
	switch (dialogArguments.__IE_PrintType)
	{
		case "Prompt":
			if (printer.showPrintDialog())
			PrintPrep();
		break;
		case "NoPrompt":
			PrintPrep();
		break;
		case "Preview":
		default:
		break;
	}
}

function LimitUploadFileTypes(uploadField, allowedList)
{
	var uploadExt = uploadField.value.substring(uploadField.value.lastIndexOf('.') + 1);
  	allowedList = typeof(allowedList) != 'undefined' ? allowedList : 'pdf';
	if (ListFindNoCase(allowedList, uploadExt) == -1)
	{
		uploadField.value = '';
		alert('Only the following file types are allowed: ' + allowedList);
	}
}

function ListFindNoCase(list, value)
{
  var i = 0;
  var delimiter = ',';
  var returnValue = -1;
  var _tempArray = new Array();
  if(ListFindNoCase.arguments.length == 3)
    delimiter = ListFindNoCase.arguments[2].toLowerCase();
  list = list.toLowerCase();
  value = value.toLowerCase();
  _tempArray = list.split(delimiter);
  for(i = 0; i < _tempArray.length; i++)
  {
    if(_tempArray[i] == value)
    {
      returnValue = i;
      break;
    }
  }
  return returnValue;
}


function Left(str, n)
{
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else
       return String(str).substring(0, n);
}

function Right(str, n)
{
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else
    {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

// use it like this: s.trim();
String.prototype.trim = function () {
    return this.replace(/^\s*/, '').replace(/\s*$/, '');
}

function formatCurrency(num, dSign)
{
	if (!dSign)
		var dSign = false;
	if (dSign == true)
		dSign = '$';
	else if (dSign == false)
		dSign = ''; 
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + dSign + num + '.' + cents);
}

//strips out $ and , from currency fields
function getCurrencyValue(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	return parseFloat(num)	
}

// this function is used to "nullify" select boxes. use it on the onChange event of a checkbox, like so: onChange="nullSwitch(this.checked, 'selectBoxID')"
function nullSwitch(toggle, target)
{
	if (document.getElementById(target))
	{
		if (toggle)		
		{
			document.getElementById(target).disabled = false;
		}
		else
		{
			document.getElementById(target).disabled = true;		
		}
	}
}
