﻿/************************************************
DESCRIPTION: Trims trailing whitespace chars.

PARAMETERS:
   strValue - String to be trimmed.

RETURNS:
   Source string with right whitespaces removed.
*************************************************/
function rightTrim( strValue ) {
	var objRegExp = /^([\w\W]*)(\b\s*)$/;

	if(objRegExp.test(strValue)) {
       //remove trailing a whitespace characters
       strValue = strValue.replace(objRegExp, '$1');
    }
  return strValue;
}

/************************************************
DESCRIPTION: Trims leading whitespace chars.

PARAMETERS:
   strValue - String to be trimmed

RETURNS:
   Source string with left whitespaces removed.
*************************************************/
function leftTrim( strValue ) {
	var objRegExp = /^(\s*)(\b[\w\W]*)$/;

	if(objRegExp.test(strValue)) {
       //remove leading a whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
function trimAll( strValue ) {
	 var objRegExp = /^(\s*)$/;

    //check for all spaces
	if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

/************************************************
DESCRIPTION: Removes currency formatting from
  source string.

PARAMETERS:
  strValue - Source string from which currency formatting
     will be removed;

RETURNS: Source string with commas removed.
*************************************************/
function removeCurrency( strValue ) {
  var objRegExp = /\(/;
  var strMinus = '';

  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

/************************************************
DESCRIPTION: Formats a number as currency.

PARAMETERS:
  strValue - Source string to be formatted

REMARKS: Assumes number passed is a valid
  numeric value in the rounded to 2 decimal
  places.  If not, returns original value.
*************************************************/
function addCurrency( strValue ) {
	var objRegExp = /-?[0-9]+\.[0-9]{2}$/;

	if( objRegExp.test(strValue)) {
      objRegExp.compile('^-');
      strValue = addCommas(strValue);
      if (objRegExp.test(strValue)){
        strValue = '(' + strValue.replace(objRegExp,'') + ')';
      }
      return '$' + strValue;
    }
    else{
      return strValue;
	}
}

/************************************************
DESCRIPTION: Removes commas from source string.

PARAMETERS:
  strValue - Source string from which commas will
    be removed;

RETURNS: Source string with commas removed.
*************************************************/
function removeCommas( strValue ) {
	var objRegExp = /,/g; //search for commas globally

	//replace all matches with empty strings
	return strValue.replace(objRegExp,'');
}

/************************************************
DESCRIPTION: Inserts commas into numeric string.

PARAMETERS:
  strValue - source string containing commas.

RETURNS: String modified with comma grouping if
  source was all numeric, otherwise source is
  returned.

REMARKS: Used with integers or numbers with
  2 or less decimal places.
*************************************************/
function addCommas( strValue ) {
	var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');

	//check for match to search criteria
	while(objRegExp.test(strValue)) {
	   //replace original string with first group match,
	   //a comma, then second group match
	   strValue = strValue.replace(objRegExp, '$1,$2');
	}
	return strValue;
}

/************************************************
DESCRIPTION: Removes characters from a source string
  based upon matches of the supplied pattern.

PARAMETERS:
  strValue - source string containing number.

RETURNS: String modified with characters
  matching search pattern removed

USAGE:  strNoSpaces = removeCharacters( ' sfdf  dfd',
                                '\s*')
*************************************************/
function removeCharacters( strValue, strMatchPattern ) {
	var objRegExp =  new RegExp( strMatchPattern, 'gi' );

	//replace passed pattern matches with blanks
	return strValue.replace(objRegExp,'');
}

function getFileExt(filename){
	var dot = filename.lastIndexOf(".");
	if( dot == -1 ) return "";
	return filename.substr(dot, filename.length);
}

function getFileName(str){
	if( str.length == 0 ){
		return false;
	}
	var string = str;
	var num_of_last_slash = string.lastIndexOf("\\");
	if(num_of_last_slash < 1){ 
		num_of_last_slash = string.lastIndexOf("/"); 
	}
	var file_name = string.slice(num_of_last_slash + 1, string.length);
	return file_name;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return {"left":curleft, "top":curtop};
}

//----------------------------------------
// load popup window
//----------------------------------------

var popupWin;
function loadPage(url,height,width,focus){
	if( typeof(focus) == "undefined" ){ focus = false }
	
	var w = 480, h = 340;
	var browserName=navigator.appName;
	if (browserName=="Netscape") {
		w = window.innerWidth;
		h = window.innerHeight;
	} else {
		if (browserName=="Microsoft Internet Explorer") {
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}

	var popW = width, popH = height;
	//var leftPos = (w-popW)/2;
	//var topPos = (h-popH)/2;
	
	var leftPos = (screen.availWidth/2)-(width/2);
	var topPos = (screen.availHeight/2)-(height/2);
	
	//causing issues with AOL browser
	//if(popupWin){
	//	popupWin.close();
	//}

	
	popupWin = window.open(url, '_blank',
		'toolbars=0, '+
		'scrollbars=0, '+
		'location=0, status=0, menubars=0,'+
		'resizable=1, width='+width+', height='+height+
		', left='+leftPos +', top='+topPos);

	if( focus && popupWin ){
		popupWin.focus();
	}
	//detect popup block
	if(!popupWin){
  		alert('We have detected that you are using popup blocking software.\nPlease "allow popups from this website" and try your selection again.');
	}
	/*
	else{
		var NewX = (screen.availWidth/2)-(width/2);
		var NewY = (screen.availHeight/2)-(height/2);
		//popupWin.moveTo(NewX, NewY);
		NewX = null;
		NewY = null;
	}
	*/	
}

function makeUrlAjaxReady(url){
	var ret="";
	var arrUrl = url.split("?");
	var qString = "";
	if( arrUrl[1] ){
		var arrQ = arrUrl[1].split("&");
		for( var i=0; i<arrQ.length; i++ ){
			var keyValue = arrQ[i].split("=");
			qString += keyValue[0]+"="+encodeURIComponent(keyValue[1])+"&";
		}
	}
	ret = arrUrl[0]+"?"+qString;
	return ret;
}

function get_qs(url){
	var ret = new Object();
	var arrUrl = url.split("?");
	if( arrUrl[1] ){
		var arrQ = arrUrl[1].split("&");
		for( var i=0; i<arrQ.length; i++ ){
			var keyValue = arrQ[i].split("=");
			ret[ keyValue[0] ] = keyValue[1];
		}
	}
	return ret;
}

function qs(arrParams){
	var qString = "";
	for( var key in arrParams ){
		qString += "&"+key+"="+encodeURIComponent(arrParams[key]);
	}
	return qString;
}

function Point(x, y) {
    this.x = x;
    this.y = y;
}

function getObjCoords(o) {
    var oX = 0;
    var oY = 0;
    if (o.offsetParent) {
        while (1) {
            oX+=o.offsetLeft;
            oY+=o.offsetTop;
			if (!o.offsetParent) { break; }
            o=o.offsetParent;
        }
    } else if (o.x) {
        oX+=o.x;
        oY+=o.y;
    }
    //alert(oX + ":" + oY);
    return new Point(oX, oY);
}

function getScrollPosition(){
	if (navigator.appName == "Microsoft Internet Explorer"){
		return document.body.scrollTop;
	} else{
		return window.pageYOffset;
	} 
}
