/*
'----------------------------------------------------------------------------
'  (C)opyright 2003
'
'                           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 validateRequiredField( sValue, sName, sErrMsg )
{
    if( trimString(sValue) == "" )
    {
      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 + ' needs a valid number with area code for a total of 10 digits.';
	}
	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 = trimString(sValue);
	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 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 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;
}