﻿/*
'----------------------------------------------------------------------------
'  (C)opyright 2007
'
'                           Quest TechnoLogy Group, Inc.
'                           Quest Dynamix, LLC
'                           3751 Maguire Blvd, Suite 150
'                           Orlando, FL 32803
'
'  All rights reserved.
'
'  Proprietary and Confidential.
'  May not be reproduced, transmitted or reverse engineered in any form or
'  by any means, electronic, mechanical, photocopying, recording, or
'  otherwise, without the prior written consent of Quest TechnoLogy Group Inc.
'  and Quest Dynamix, LLC.  Access to this software, including but not limited
'  to runtime applications, source code, binaries and all documentation is
'  restricted to authorized personnel of Quest TechnoLogy Group, Inc and Quest
'  Dynamix, LLC.
'  Information contained herein is confidential and proprietary to Quest
'  TechnoLogy Group and Quest Dynamix, LLC.  Violation of these restrictions
'  may be punishable by law.
'
'  No co-ownership provisions apply.
'----------------------------------------------------------------------------
*/


//----------------------------------------------------------------------------------------------------
//					Script for trimming any Text Area Value
//----------------------------------------------------------------------------------------------------
function trimString (str) {
	str = this != window? this : str;
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

//----------------------------------------------------------------------------------------------------
/*=================================================================================
Objective			: Function to calculate the text area count
Inputs				: sValue value, maximum length	
Output				: returns string with max length
===================================================================================*/

function textCounter(sValue,maxlimit) 
{
	var strFldVal =sValue;
	var intCtr;				//Counter
	var intBRLen=0;			//Length for break tags
	var intMaxLen;			//Maximum length of sValue
	
	for(intCtr=0;intCtr<strFldVal.length;intCtr++)
	{
		if (strFldVal.charCodeAt(intCtr)==10)
			intBRLen=parseInt(intBRLen)+parseInt(3);
	}
	intMaxLen=(maxlimit-intBRLen);
	if (sValue.length >intMaxLen) 
		{
		alert("You can't input more than "+ maxlimit +" characters");
		sValue = sValue.substring(0,intMaxLen);
		sValue.focus();
		}
}

function validateWVInput(aElements, sErrMsg) {
	//Loop through input tags as aElements with specified WV parameters:
	for(var i = 0; i < aElements.length; i++) { 
		sErrMsg = validateWVNode(aElements.item(i), sErrMsg);
	}
		 
	return sErrMsg;
}

function validateWVNode(aNode, sErrMsg) {
	// following attributes are used to validate the node
	// WV_name="Display Name"
	// WV_required="true" 
	// WV_edit="0" - No Edit
	//         ="1" - Numeric Edit - no commas allowed
	//         ="2" - Phone # Edit 
	//         ="3" - Zip Code Edit
	//         ="4" - Date Edit
	//         ="5" - EMail Edit
	//         ="6" - Formatted Number Edit - allows commas, prefixed:$
	
	// check if element is mandatory; ie has a pattern  
	var attrName = aNode.getAttribute('WV_name'); 
	if (attrName != null) {
		if (aNode.getAttribute('WV_required') == "true") { 
			sErrMsg = validateRequiredField(aNode.value, attrName, sErrMsg);
		}
		
		var attrEdit = aNode.getAttribute('WV_edit');
		if ( attrEdit != null && trimString(aNode.value) != "" ) {
			switch (parseInt(attrEdit)) {
			case 1: 
				sErrMsg = validateNumberField(aNode.value, attrName, sErrMsg);
				break;
			case 2: 
				sErrMsg = validatePhoneField(aNode.value, attrName, sErrMsg);
				break;
			case 3: 
				sErrMsg = validateZipCodeField(aNode.value, attrName, sErrMsg);
				break;
			case 4: 
				sErrMsg = validateDateField(aNode.value, attrName, sErrMsg);
				break;
			case 5:
				sErrMsg = validateEmailField(aNode.value, attrName, sErrMsg);
				break;	
			case 6:
				sErrMsg = validateFormattedNumber(aNode.value, attrName, sErrMsg);
				break;	
			}
		}		
	} 
			 
	return sErrMsg;
}

function validateFormattedNumber( sValue, sName, sErrMsg )
{
	while ( sValue.substring(0,1) == ' ' || sValue.substring(0,1) == '$' ) {
		sValue = sValue.substring(1, sValue.length);
	}
		
	var i = 0;
	var iSub = 0;
	var bComma = false;
	while ( i < sValue.length && sValue.substring(i,i+1) != '.' ) {
		if ( sValue.substring( i, i + 1 ) == ',' ) {
			if ( iSub > 4 || iSub == 0 ) {
				if ( sErrMsg != "" ) {
		  			sErrMsg += '<br />\n';
				}
				sErrMsg += sName + ' has misplaced comma';
				return sErrMsg;
			}
			iSub = 0
			bComma = true;
		}
		++i
		++iSub
	}

	if ( bComma && iSub != 4 ) {
		if ( sErrMsg != "" )	{
			sErrMsg += '<br />\n';
		}
		sErrMsg += sName + ' has misplaced comma';
		return sErrMsg;
	}	
	
	var re = /,/g; 
  	return validateNumberField( sValue.replace(re,''), sName, sErrMsg, false );
}

function validateRequiredField( sValue, sName, sErrMsg )
{
    if( trimString(sValue) == "" )
    {
      if( sErrMsg != "" )
      {
        sErrMsg += '<br />\n';
      }
      sErrMsg += sName + ' is required.';
    }
	return sErrMsg;
}

function validateRequiredFieldCbo( sValue, sName, sErrMsg )
{
    if( trimString(sValue) <= 0 )
    {
      if( sErrMsg != "" )
      {
        sErrMsg += '<br />\n';
      }
      sErrMsg += sName + ' is required.';
    }
	return sErrMsg;
}

function validateVerificationCodeField( sValue, sCardType, sName, sErrMsg )
{
	if( ! isVerificationCode(sValue, sCardType) )
	{
		if( sErrMsg != "" )
		{
		  sErrMsg += '<br />\n';
		}

		switch(sCardType.toLowerCase())
		{
			case "visa":
			case "v":	  
			case "mastercard":
			case "mc":
			case "m":
			case "diners club":
			case "carte blanche":
			case "diners":
			case "carte":
			case "dc":
			case "cb":
			case "discover":
			case "disc":
			case "d":
				sErrMsg += sName + ' must be a 3-digit code.';
				break;

			case "american express":
			case "amex":
			case "a":
				sErrMsg += sName + ' must be a 4-digit code.';
				break;

			default:
				sErrMsg += sName + ' must be a 3 or 4-digit code.';
		}
	}
	return sErrMsg;
}

function isVerificationCode(sValue, sCardType)
{
	var re = new RegExp(/^\d{3}\b/);
	var ra = new RegExp(/^\d{4}\b/);
	var ph = new String( sValue ).replace(/[^0-9]/g, '');
	var iValueLength = sValue.length;
	var lengthIsValid = false;

	switch(sCardType.toLowerCase())
	{
		case "visa":
		case "v":	  
		case "mastercard":
		case "mc":
		case "m":
		case "diners club":
		case "carte blanche":
		case "diners":
		case "carte":
		case "dc":
		case "cb":
		case "discover":
		case "disc":
		case "d":
			return re.test(ph);
			break;
	
		case "american express":
		case "amex":
		case "a":
			return ra.test(ph);
			break;

		default:
			alert("Card type not found");
	}
}

function validatePhoneField( sValue, sName, sErrMsg )
{
	if( ! isPhoneNumber(sValue) )
	{
		if( sErrMsg != "" )
		{
		  sErrMsg += '<br />\n';
		}
		sErrMsg += sName + ' requires a valid number including area code.';
	}
	return sErrMsg;
}

function isPhoneNumber( sValue )
{
	var re = new RegExp(/^\d{10}\b/);
	var ph = new String( sValue ).replace(/[^0-9]/g, '');
	return re.test(ph);
}

function validateCreditCardNumberField( sCardNumber, sCardType, sName, sErrMsg )
{
	if( ! isCreditCardNumber(sCardNumber, sCardType) )
	{
		if( sErrMsg != "" )
		{
		  sErrMsg += '<br />\n';
		}
		sErrMsg += sName + ' is an invalid number.';
	}
	return sErrMsg;
}

function isCreditCardNumber(sCardNumber, sCardType)
{
	// This is an edited version of http://www.evolt.org/article/Validating_a_Credit_Card_Number_with_JavaScript/17/24700/
	var isValid = false;
	var reCardNumberPattern = /[\d ]+/;
	isValid = reCardNumberPattern.test(sCardNumber);
	
	if (isValid)
	{
		sCardNumber = sCardNumber.replace(/ /g,"");
		var iCardNumberLength = sCardNumber.length;
		var lengthIsValid = false;
		var prefixIsValid = false;
		var rePrefixPattern;

		// from http://www.beachnet.com/~hstiles/cardtype.html
		// Card 			Prefix  		Length  	Check digit algorithm  
		// Visa				4 				13, 16  	mod 10 
		// MC				51-55 			16  		mod 10 
		// AmEx 			34,37			15  		mod 10 
		// Diners Club/
		// Carte Blanche 	300-305,36,38	14			mod 10 
		// Discover 		6011 			16  		mod 10 
		// enRoute 			2014, 2149 		15 			any 

		switch(sCardType.toLowerCase())
		{
			case "visa":
			case "v":	  
				lengthIsValid = (iCardNumberLength == 16 || iCardNumberLength == 13);
				rePrefixPattern = /^4/;
				break;
	
			case "mastercard":
			case "mc":
			case "m":
				lengthIsValid = (iCardNumberLength == 16);
				rePrefixPattern = /^5[1-5]/;
				break;
	
			case "american express":
			case "amex":
			case "a":
				lengthIsValid = (iCardNumberLength == 15);
				rePrefixPattern = /^3(4|7)/;
				break;
	
			case "diners club":
			case "carte blanche":
			case "diners":
			case "carte":
			case "dc":
			case "cb":
				lengthIsValid = (iCardNumberLength == 14);
				rePrefixPattern = /^3(0[0-5]|6|8)/;
				break;
			
			case "discover":
			case "disc":
			case "d":
				lengthIsValid = (iCardNumberLength == 16);
				rePrefixPattern = /^6011/;
				break;

			//case "enRoute": NOT IMPLEMENTED

			default:
				rePrefixPattern = /^$/;
				alert("Card type not found");
		}
	
		prefixIsValid = rePrefixPattern.test(sCardNumber);
		isValid = prefixIsValid && lengthIsValid;
	}

	if (isValid)
	{
		// This is a weird little thing called the Luhn formula
		// 
		// 	1.	Start with the second digit from last in the card number. 
		// 	Moving backwards towards the first digit in the number, 
		// 	double each alternate digit. In our example, we would double 
		// 	the bold numbers in 4221 3456 1243 1237 to give us: 
		// 	(4x2), (2x2), (3x2), (5x2), (1x2), (4x2), (1x2), (3x2) 
		// 	which is: 8, 4, 6, 10, 2, 8, 2, 6
		// 
		// 2.	Take the results of the doubling, add each of the individual 
		// 	digits in each doubled number together, and then add to the 
		// 	running total.
		// 	In our example we have:
		// 	8 + 4 + 6 + (1 + 0) + 2 + 8 + 2 + 6 = 37
		// 
		// 3.	Add all the non-doubled digits from the credit card number together.
		// 	In our example we have:
		// 	2 + 1 + 4 + 6 + 2 + 3 + 2 + 7 = 27
		// 
		// 4.	Add the values calculated in step 2 and step 3 together.
		// 	In our example:
		// 	37 + 27 = 64
		// 
		// 5.	Take the value calculated in step 4 and calculate the remainder when 
		// 	it is divided by 10. If the remainder is zero, then it's a valid number, 
		// 	otherwise it's invalid.
		// 	In our example:
		// 	64 / 10 = 6 with remainder 4.
		// 	So, our example number is not a valid card number, as the remainder is not 0. 
		
		var iDigitProduct;
		var iCheckSumTotal = 0;
		
		for( i = iCardNumberLength - 1; i >= 0; i-- )
		{
			iCheckSumTotal += parseInt(sCardNumber.charAt(i));
			i--;
			iDigitProduct = String((sCardNumber.charAt(i) * 2));
			for( j = 0; j < iDigitProduct.length; j++ )
			{
				iCheckSumTotal += parseInt(iDigitProduct.charAt(j));
			}
		}

		isValid = (iCheckSumTotal % 10 == 0);
	}

	return isValid;
}

function validateCreditCardExpirationDateField( sValue, sName, sErrMsg )
{
	var datePat = /^(\d{1,2})(\/|-|\.)(\d{2})$/;
	var matchArray = sValue.match(datePat); // it must first match the formats
	
	if (matchArray == null) 
	{
		//didn't match mm/yy or mm-yy or mm.yy
		if( sErrMsg != "" )
		{
		  sErrMsg += '<br />\n';
		}
		sErrMsg += sName + ' is not a valid format. Use MM/YY.';
	}
	else
	{
		var iExpMonth = parseInt(matchArray[1], 10); // array locations may look wrong, but they're correct
		var iExpYear = parseInt(matchArray[3], 10);

	
		var now = new Date();
		var iCurMonth = now.getMonth() + 1;
		var iCurYear = now.getYear();
	
		if( iExpYear < 60 )
		{
			iExpYear += 2000;
		}
		else
		{
			iExpYear += 1900;
		}
	
		if( ( iExpYear < iCurYear ) || ( (iExpYear == iCurYear) && (iExpMonth < iCurMonth) ) )
		{
			if( sErrMsg != "" )
			{
			  sErrMsg += '<br />\n';
			}
			sErrMsg += sName + ' shows that your credit card has expired.';
		}
	}

	return sErrMsg;
}

function validateZipCodeField( sValue, sName, sErrMsg )
{
	if( ! isZipCode(sValue) )
	{
		if( sErrMsg != "" )
		{
		  sErrMsg += '<br />\n';
		}
		sErrMsg += sName + ' is an invalid ZIP code.';
	}
	return sErrMsg;
}
function isZipCode( sValue )
{
	var re = new RegExp(/^\d{5}(\d{4})?$/);
	var zip = new String( sValue ).replace(/[^0-9]/g, '');
	return re.test(zip);
}

function validateStateField( sValue, sName, sErrMsg )
{
	if( ! isStateAbbreviation(sValue) )
	{
		if( sErrMsg != "" )
		{
		  sErrMsg += '<br />\n';
		}
		sErrMsg += sName + ' is not a valid state.';
	}
	return sErrMsg;
}
function isStateAbbreviation( sValue )
{
	var re = new RegExp(/^(AL|AK|AZ|AR|CA|CO|CT|DE|FL|GA|HI|ID|IL|IN|IA|KS|KY|LA|ME|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|OH|OK|OR|PA|RI|SC|SD|TN|TX|UT|VT|VA|WA|DC|WV|WI|WY)/);
	var state = trimString(sValue);
	return re.test(state.toUpperCase());
}

function validateEmailField( sValue, sName, sErrMsg)
{
	if( ! isEmailAddress(sValue) )
	{
		if( sErrMsg != "" )
		{
		  sErrMsg += '<br />\n';
		}
		sErrMsg += sName + ' is not a valid email address.';
	}
	return sErrMsg;
}
function isEmailAddress( sValue )
{
	var re = new RegExp(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/);
	var email = trimString(sValue);
	return re.test(email);
}

function validateURLField( sValue, sName, sErrMsg )
{
	if( ! isURL(sValue) )
	{
		if( sErrMsg != "" )
		{
		  sErrMsg += '<br />\n';
		}
		sErrMsg += sName + ' is not a valid URL.';
	}
	return sErrMsg;
}
function isURL( sValue )
{
	var re = new RegExp(/^\w+([-+.]\w+)*\.\w+([-.]\w+)*\.\w+([-.]\w+)*/);
	var url = trimString(sValue);
	return re.test(url);
}

function validateNumberField( sValue, sName, sErrMsg )
{
	return validateNumberField( sValue, sName, sErrMsg, false )
}

function validateNumberField( sValue, sName, sErrMsg, bPositive )
{
	if( isNaN(sValue) )
	{
		if( sErrMsg != "" )
		{
		  sErrMsg += '<br />\n';
		}
		sErrMsg += sName + ' is not a number';
	}
	else if( bPositive && sValue < 0 )
	{
		if( sErrMsg != "" )
		{
		  sErrMsg += '<br />\n';
		}
		sErrMsg += sName + ' must be a positive number.';
	}
	return sErrMsg;
}

function validateTimeField( sValue, sName, sErrMsg )
{
	if( ! isTime(sValue) )
	{
		if( sErrMsg != "" )
		{
		  sErrMsg += '<br/>\n';
		}
		sErrMsg += sName + ' is not a valid time.';
	}
	return sErrMsg
}
function isTime( sValue )
{
	var re = new RegExp(/^\d{1,2}:\d{2}( )?[AP\/ap](\.)?(\.)?/);
	var re2 = /^\d{1,2}( )?[AP\/ap](\.)?(\.)?/;
	var matchArray = sValue.match(re2);
	var sTime = trimString(sValue).toUpperCase();
	
	if( ! re.test(sTime) && matchArray == null)
	{
		return false;
	}
	else
	{
		var h = new Number( sTime.substring( 0, 2 ) );
		var m = new Number( sTime.substring( sTime.indexOf(":") + 1, sTime.indexOf(":") + 3 ) );
		if( h > 12 || m > 59 )
		{
			return false;
		}
	}
	return true;
}

function validateDateField( sValue, sName, sErrMsg )
{
	if( ! isDate(sValue) )
	{
		if( sErrMsg != "" )
		{
		  sErrMsg += '<br />\n';
		}
		sErrMsg += sName + ' is not a valid date.';
	}
	return sErrMsg;
}
function isDate( sDateString )
{
	// This function accepts a string variable and verifies if it is a
	// proper date or not. It validates format matching either
	// mm-dd-yy or mm-dd-yyyy or mm/dd/yy or mm/dd/yyyy or mm.dd.yy or mm.dd.yyyy .
	// Then it checks to make sure the month has the proper number of days, based 
	// on which month it is.

	var datePat = /^(\d{1,2})(\/|-|\.)(\d{1,2})(\/|-|\.)(\d{2}|\d{4})$/;
	var matchArray = sDateString.match(datePat); // it must first match the formats
	
	if (matchArray == null) 
	{
		//didn't match mm-dd-yy or mm-dd-yyyy or mm/dd/yy or mm/dd/yyyy or mm.dd.yy or mm.dd.yyyy
		return false;
	}

	month = matchArray[1]; // array locations may look wrong, but they're correct
	day = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) 
	{	
		// invalid month
		return false;
	}

	if (day < 1 || day > 31) 
	{
		//invalid day
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	{
		//invalid day for these months
		return false;
	}

	if (month == 2) 
	{ 
		// check for february 29th when not a leap year
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) 
		{
			//invalid day for this particular february
			return false;
		}
	}
	
	return true; // date is valid
}

function validateConfirmPassword( sValue, sValue2, sName, sErrMsg )
{
	if( ! isConfirmPassword(sValue, sValue2) )
	{
		if( sErrMsg != "" )
		{
		  sErrMsg += '<br />\n';
		}
		sErrMsg += sName + ' does not match';
	}
	return sErrMsg;
}
function isConfirmPassword( sPwdString, sPwdString2 )
{
	if ( sPwdString != sPwdString2 )
	{
		return false;
	}
	else
	{
		return true; 
	}
}

// The bottown two stringToDate functions does not work
// This one works fine 8/2/2004
function stringToDate( sDateString )
{
	return new Date(sDateString);
}

//
//function stringToDate( sDateString )
//{
//	return stringToDate( sDateString, "/" );
//}

//function stringToDate( sDateString, sDelimiter )
//{
//	arrDate = sDateString.split(sDelimiter);
//	return new Date(arrDate[2], new Number(arrDate[1]) - 1, arrDate[0] );
//}

function nowDateString()
{	
	var now = new Date();
	return (now.getMonth() + 1) + '/' + now.getDate() + '/' + now.getFullYear();
}

function stringToTodaysTime( sTimeString )
{
	if( isTime( sTimeString ) )
	{
		h = new Number( sTimeString.substring( 0, sTimeString.indexOf(":") ) );
		s = sTimeString.substring( sTimeString.indexOf(":") + 1, sTimeString.indexOf(":") + 3 );
		if( sTimeString.toUpperCase().indexOf("P") > 0 ) {
			if( h != 12 )
			{
				h += 12;
			}
		}
		else
		{
			if( h == 12 ) 
			{
				h = 0;
			}
		}

		dNow = new Date();
		return new Date(dNow.getYear(), dNow.getMonth(), dNow.getDate(), h, s);
	}
	return null;
}
	
function parseToDate( sDateIn )                                              
{
	var sDate = trimString(sDateIn);
	
	if ( ! isDate(sDate) ) {
		if ( ! isNaN(sDate) ) {
			if ( sDate.length == 4 ) {
				sDate = sDate.substring(0,1) + "/" + sDate.substring(1,2) + "/" + sDate.substring(2,4);
			}
			else if ( sDate.length == 5 ) {
				sDate = sDate.substring(0,1) + "/" + sDate.substring(1,3) + "/" + sDate.substring(3,5);
			}
			else if ( sDate.length == 6 ) {
				sDate = sDate.substring(0,2) + "/" + sDate.substring(2,4) + "/" + sDate.substring(4,6);
			}
			else if ( sDate.length == 7 ) {
				sDate = sDate.substring(0,1) + "/" + sDate.substring(1,3) + "/" + sDate.substring(3,7);
			}
			else if ( sDate.length == 8 ) {
				sDate = sDate.substring(0,2) + "/" + sDate.substring(2,4) + "/" + sDate.substring(4,8);
			}
		}	
	}
	
	return sDate;
}

function compareTimes( sTimeOne, sTimeTwo )
{
	if( isTime(sTimeOne) && isTime(sTimeTwo) )
	{
		dTimeOne = stringToTodaysTime(sTimeOne);
		dTimeTwo = stringToTodaysTime(sTimeTwo);
		if( dTimeOne == dTimeTwo ) 
			return 0;
		else if( dTimeOne > dTimeTwo ) 
			return 1;
		else
			return 2;
	}
	else
		return -1;
}

function formatCurrency( num ) 
{
	var isNegative = false;
	num = num.toString().replace(/\\$|\\,/g,'');
	if( isNaN( num ) ) 
	{
	  num = "0";
	}
	if ( num < 0 ) 
	{
	  num = Math.abs( num );
	  isNegative = true;
	}
	cents = Math.floor( ( num * 100 + 0.5 ) % 100 );
	num = Math.floor( ( num * 100 + 0.5 ) / 100 ).toString();
	if ( cents < 10 ) 
	{
	  cents = "0" + cents;
	}
	for ( 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 ) );
	}

	var result = '$' + num + '.' + cents;
	if ( isNegative ) 
	{
	  result = "-" + result;
	}
	return result;
}

function getNewHTTPObject()
{
        var xmlhttp;

        /** Special IE only code ... */
        /*@cc_on
          @if (@_jscript_version >= 5)
              try
              {
                  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
              }
              catch (e)
              {
                  try
                  {
                      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                  }
                  catch (E)
                  {
                      xmlhttp = false;
                  }
             }
          @else
             xmlhttp = false;
        @end @*/

        /** Every other browser on the planet */
        if (!xmlhttp && typeof XMLHttpRequest != 'undefined')
        {
            try
            {
                xmlhttp = new XMLHttpRequest();
            }
            catch (e)
            {
                xmlhttp = false;
            }
        }

        return xmlhttp;
}

//----------------------------- V B   L I K E   F U N C T I O N S ------------------------------------------

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);
    }
}

function inStr(strSearch, charSearchFor)
{
	for (i=0; i < String(strSearch).length; i++)
	{
	    if (charSearchFor == mid(strSearch, i, String(charSearchFor).length))
	    {
			return i;
	    }
	}
	return -1;
}

function mid(str, start, len)
{
        if (start < 0 || len < 0) return "";
        var iEnd, iLen = String(str).length;
        
        if (start + len > iLen)
              iEnd = iLen;
        else
              iEnd = start + len;

        return String(str).substring(start,iEnd);
}

function iif( evalIt, doTrue, doFalse )
{
	if (  evalIt ) {
		return doTrue;
	}
	else {
		return doFalse;
	}
}				

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

//-----------------------------------------------------------------------------------------------------------

function parseFloatCurrency( num ) 
{
	num = num.toString().replace(/\$|\,|\ /g,'');
	if( num.length == 0 ) {
		return 0;
	}	
	else if( isNaN( num ) )	{
		return 0;
	}
	else {
		return parseFloat(num);
	}	
}

function parseIntCurrency( num ) 
{
	num = num.toString().replace(/\$|\,|\ /g,'');
	if( num.length == 0 ) {
		return 0;
	}	
	else if( isNaN( num ) )	{
		return 0;
	}
	else {
		return parseInt(num);
	}	
}

// calculate the ASCII code of the given character
function calcKeyCode(aChar) {
  var character = aChar.substring(0,1);
  var code = aChar.charCodeAt(0);
  return code;
}

function numericOnly(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = calcKeyCode(lchar);

 /* Check if the keyed in character is a number */
   if (cCode < 48 || cCode > 57 ) 
  {
  	if (cCode == 46 )  // 46 is a period so allow for decimal
	{
		return true;		
	}
	else if (cCode == 45 ) // 45 is a negative sign so allow for increase in price
	{ 
		return true;
	}
	else 
	{
	    var myNumber = val.value.substring(0, (strLength) - 1);
    	val.value = myNumber;
	}
  }
  return false;
}

function numericDateOnly(val) {
  var strPass = val.value;
  var strLength = strPass.length;
  var lchar = val.value.charAt((strLength) - 1);
  var cCode = calcKeyCode(lchar);

 /* Check if the keyed in character is a number */
   if (cCode < 48 || cCode > 57 ) 
  {
  	if (cCode == 47 )  // 47 is a slash / so allow for date
	{
		return true;		
	}
	else if (cCode == 45 ) // 45 is a dash sign so allow for increase in price
	{ 
		return true;
	}
	else 
	{
	    var myNumber = val.value.substring(0, (strLength) - 1);
    	val.value = myNumber;
	}
  }
  return false;
}


function getXMLHTTP()
{
	var oXMLHTTP = null;
	
	if (window.ActiveXObject) { // IE
        try {
            oXMLHTTP = new ActiveXObject("Msxml2.XMLHTTP");
        } 
		catch (e) {
            try {
                oXMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
            } 
			catch (e) {
			}
        }
    } 
	else if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        oXMLHTTP = new XMLHttpRequest();
		if ( oXMLHTTP ) {
			if (oXMLHTTP.overrideMimeType) {
				oXMLHTTP.overrideMimeType('text/xml');
			}
		}	
    } 
	else {	
        alert("Browser doesn't support XMLHTTP");
        return null;
    }
    
	return oXMLHTTP;	
}
	

