
/*Remove extra spaces from left and right sides of string*/
function Trim(strValue){
	if(strValue.length <= 0){return '';}
	strValue = RTrim(strValue);
	strValue = LTrim(strValue);
	if(strValue == ''){return '';} else {return strValue;}
} 

/*Remove extra spaces from right side of string*/
function RTrim(strValue){	
	var strSpace	   = String.fromCharCode(32);
	var strValueLength = strValue.length;
	var tmpValue 	   = '';
	if(strValueLength <= 0 ){return '';}
	var intCharCtr = strValueLength - 1;
	while(intCharCtr > -1){
		if(strValue.charAt(intCharCtr) == strSpace){}
		else{
			tmpValue = strValue.substring(0, intCharCtr + 1);
			break;
		}
		intCharCtr = intCharCtr - 1;
	} 
	return tmpValue;
}

/*Remove extra spaces from left side of string*/
function LTrim(strValue){	
	var strSpace 		= String.fromCharCode(32);
	var strValueLength 	= strValue.length;
	var tmpValue 		= '';
	var intCharCtr 		= 0;	
	if(strValueLength <= 0 ){return '';}
	while (intCharCtr < strValueLength){
		if(strValue.charAt(intCharCtr) == strSpace){}
		else{
			tmpValue = strValue.substring(intCharCtr,strValueLength);
			break;
		}
		intCharCtr = intCharCtr + 1;
	}
	return tmpValue;
} 

/*Boolean check if string starts with vowel to determine if it should be preceeded with a 'an' or 'a'*/
function isVowel(strValue){
	var blnSuccess     = false;
	var intCtr 		   = 0;
	
	var arrEscapeWords = new Array(1);
	arrEscapeWords[0]  = "username";
	
	var strValidChars  = 'aeiou';
	var strFirstLetter = strValue.substring(0,1).toLowerCase();
	
	blnSuccess = checkValidChars(strFirstLetter,strValidChars)
		
	for(intCtr = 0; intCtr < arrEscapeWords.length; intCtr++){
		if (Trim(strValue.toLowerCase()) == Trim(arrEscapeWords[intCtr].toLowerCase())){blnSuccess = false;}
	}
	return blnSuccess;
}


function jsReplace(str1, strLookFor, strReplace) { 
 while(strValue.indexOf(strLookFor) != -1) {
   strValue = strValue.replace(strLookFor, strReplace);
 } 
 return strValue;     
} 

/*Remove All Name/Value Pair Parameters from Querystring Except One*/
function RemoveQRYStringNameValuePairExcept(strQueryString, csvKeepParamName, blnEncodeValues){	
	var strNewQueryString = '';
	var strParamValue	  = '';
	var pairs 			  = strQueryString.split("&"); // Split query at the amperstand
	var blnMatchFound	
	if (csvKeepParamName.indexOf(',') == -1){		
		var arrKeepParams = new Array(csvKeepParamName);
	}else{
		var arrKeepParams = csvKeepParamName.split(",");
	}	
	// Begin loop through the querystring
	for(var i = 0; i < pairs.length; i++) {		
		var pos = pairs[i].indexOf('='); 	// Look for "name=value"		
		if (pos == -1) continue; 			// if not found, skip to next
		// Extract the name/value pair, Don't repeat target data And build new querystring
		//	target data will be appended to location upon second popup form
		
		for(var j = 0; j < arrKeepParams.length; j++) {
			if( pairs[i].substring(0,pos).toLowerCase() == arrKeepParams[j].toLowerCase() ) {blnMatchFound=true;break;} else {blnMatchFound=false;}
		}
		
		if (blnMatchFound==true)
		{
			if (blnEncodeValues == true) {strParamValue = escape(pairs[i].substring(pos+1));} else {strParamValue = pairs[i].substring(pos+1);}
			
			if (strNewQueryString == ''){
				strNewQueryString += pairs[i].substring(0,pos)+'='+strParamValue;
			}
			else{		
				strNewQueryString += '&'+pairs[i].substring(0,pos)+'='+strParamValue;
			}
		}
	}
	return strNewQueryString;	
}

/*Remove Name/Value Pair Parameters from Querystring*/
function RemoveQRYStringNameValuePair(strQueryString, strRemoveParamName, blnEncodeValues){	
	var strNewQueryString = '';
	var strParamValue	  = '';
	var pairs 			  = strQueryString.split("&"); // Split query at the amperstand	
	// Begin loop through the querystring
	for(var i = 0; i < pairs.length; i++) {		
		var pos = pairs[i].indexOf('='); 	// Look for "name=value"		
		if (pos == -1) continue; 			// if not found, skip to next
		// Extract the name/value pair, Don't repeat target data And build new querystring
		//	target data will be appended to location upon second popup form
		if (pairs[i].substring(0,pos) != strRemoveParamName)
		{
			if (blnEncodeValues == true) {strParamValue = escape(pairs[i].substring(pos+1));} else {strParamValue = pairs[i].substring(pos+1);}
			if (i==0){
				strNewQueryString += pairs[i].substring(0,pos)+'='+strParamValue;
			}
			else{		
				strNewQueryString += '&'+pairs[i].substring(0,pos)+'='+strParamValue;
			}
		}
	}
	return strNewQueryString;	
}

/*Return Parent.Opener Querystring*/
function getOpenerQueryString(strWindowOpener){
	if (strWindowOpener != ''){
		var objWindowOpener = eval(strWindowOpener);
		if(objWindowOpener){													//Parent URL And QueryString
			var strQueryString = objWindowOpener.location.search.substring(1);	// Get Query String
			if (strQueryString != '') {return strQueryString;} else {return '';}
		}else{return '';}
	}else{return '';}
}

/*Return Parent.Opener URL*/
function getOpenerURL(strWindowOpener){
	if (strWindowOpener != ''){
		var objWindowOpener = eval(strWindowOpener);
		if(objWindowOpener){
			var strOldURL = objWindowOpener.location.href;			
			if (objWindowOpener.location.href.indexOf('?') == -1){
				return objWindowOpener.location = objWindowOpener.location.href
			}else{
				return strOldURL.substring(0,strOldURL.indexOf('?'));
			}
		}else{return '';}
	}else{return '';}
}


function FormatNumber(Number,Decimals,Separator)
{
 // *

 // This function accepts a number to format and number
 // specifying the number of decimal places to format to. May
 // optionally use a separator other than '.' if specified.
 //
 // If no decimals are specified, the function defaults to
 // two decimal places. If no number is passed, the function
 // defaults to 0. Decimal separator defaults to '.' .
 //
 // If the number passed is too large to format as a decimal
 // number (e.g.: 1.23e+25), or if the conversion process
 // results in such a number, the original number is returned
 // unchanged.
 // *
 Number += ""          // Force argument to string.
 Decimals += ""        // Force argument to string.
 Separator += ""       // Force argument to string.
 if((Separator == "") || (Separator.length > 1))
  Separator = "."
 if(Number.length == 0)
  Number = "0"
 var OriginalNumber = Number  // Save for number too large.
 var Sign = 1
 var Pad = ""
 var Count = 0
 // If no number passed, force number to 0.
 if(parseFloat(Number)){
  Number = parseFloat(Number)} else {
  Number = 0}
 // If no decimals passed, default decimals to 2.
 if((parseInt(Decimals,10)) || (parseInt(Decimals,10) == 0)){
  Decimals = parseInt(Decimals,10)} else {
  Decimals = 2}
 if(Number < 0)
 {
  Sign = -1         // Remember sign of Number.
  Number *= Sign    // Force absolute value of Number.
 }
 if(Decimals < 0)
  Decimals *= -1    // Force absolute value of Decimals.
 // Next, convert number to rounded integer and force to string value.
 // (Number contains 1 extra digit used to force rounding)
 Number = "" + Math.floor(Number * Math.pow(10,Decimals + 1) + 5)
 if((Number.substring(1,2) == '.')||((Number + '')=='NaN'))
  return(OriginalNumber) // Number too large to format as specified.
 // If length of Number is less than number of decimals requested +1,
 // pad with zeros to requested length.
 if(Number.length < Decimals +1) // Construct pad string.
 {
  for(Count = Number.length; Count <= Decimals; Count++)
   Pad += "0"
 }
 Number = Pad + Number // Pad number as needed.
 if(Decimals == 0){
  // Drop extra digit -- Decimal portion is formatted.
  Number = Number.substring(0, Number.length -1)} else {
  // Or, format number with decimal point and drop extra decimal digit.
 Number = Number.substring(0,Number.length - Decimals -1) +
          Separator +
          Number.substring(Number.length - Decimals -1,
          Number.length -1)}
 if((Number == "") || (parseFloat(Number) < 1))
  Number="0"+Number // Force leading 0 for |Number| less than 1.
 if(Sign == -1)
  Number = "-" + Number  // Set sign of number.
 return(Number)
}
