
	//=========================================================================
	// Check if an input field is empty.
	//=========================================================================
	function IsEmpty(sValue) {
	var hReturnInfo = new ReturnInfo(0, '', 0, 1);
	
		if (StripWhitespace(sValue) == '')
			hReturnInfo.ReturnCode = 1;
			
		return hReturnInfo;
	}



	//=========================================================================
	// Check if the value of an input field is a valid number.
	//=========================================================================
	function IsValidNumber(sValue, iMinValue, iMaxValue, iDecimalPlaces, bCanBeEmpty) {
	var expNumber = /^[0-9]+$|^\-[0-9]+$|^[0-9]+\.?[0-9]+$|^\-[0-9]+\.?[0-9]+$/
	var hReturnInfo = new ReturnInfo(0, '', 0, 1);

		
		//Checking if a value was entered.
		if (IsEmpty(sValue).ReturnCode == 1) {
			hReturnInfo.ReturnCode = 1;
			hReturnInfo.ErrorDescription = 'Please enter a number';
		}

		//Checking that the value in numeric.
		if (hReturnInfo.ReturnCode == 0)
			if (!expNumber.test(sValue)) {
				hReturnInfo.ReturnCode = 2;
				hReturnInfo.ErrorDescription = 'Please enter a valid number';
			}
	
		//Checking that the number is greater or equal to the minimum allowed.
		if (hReturnInfo.ReturnCode == 0)
			if ((iMinValue != null) & (sValue!=''))
				if (sValue < iMinValue) {
					hReturnInfo.ReturnCode = 3;
					hReturnInfo.ErrorDescription = 'Please enter a number grater or equal to ' + iMinValue;
				}
				
	
		//Checking that the number is less or equal to the maximum allowed.
		if (hReturnInfo.ReturnCode == 0)
			if ((iMaxValue != null) & (sValue!=''))
				if (sValue > iMaxValue) {
					hReturnInfo.ReturnCode = 4;
					hReturnInfo.ErrorDescription = 'Please enter a number less than or equal to ' + iMaxValue;
				}
		
				
		//Checking that the number contain the allowed decimal places or less.
		if (hReturnInfo.ReturnCode == 0)
			if (iDecimalPlaces > 0) {
				if (sValue.indexOf('.') != -1)
					if (sValue.substring(sValue.indexOf('.')+1, sValue.length).length > iDecimalPlaces) {
						hReturnInfo.ReturnCode = 5;
						hReturnInfo.ErrorDescription = 'Please enter a number with ' + iDecimalPlaces + ' decimal places';
					}
			}
			else
				if (sValue.indexOf('.') != -1) {
					hReturnInfo.ReturnCode = 5;
					hReturnInfo.ErrorDescription = 'Please enter a whole number without any decimal places';
				}


		//Canceling the error information if the field can be empty.
		if (bCanBeEmpty && hReturnInfo.ReturnCode == 1) {
			hReturnInfo.ReturnCode = 0;
			hReturnInfo.ErrorDescription = '';
		}
				
		return hReturnInfo;
	}



	//=========================================================================
	// Check if a given string contain a valid phone number.
	//=========================================================================
	function IsValidPhoneNumber(sPhoneNumber, bCanBeEmpty) {
	var expPhone = /^1?\d{10}$/
	var hReturnInfo = new ReturnInfo(0, '', 0);


		if (!bCanBeEmpty && IsEmpty(sPhoneNumber).ReturnCode == 1) {
			hReturnInfo.ReturnCode = 1;
			hReturnInfo.ErrorDescription = 'Please enter your phone number.';
		}

		if (hReturnInfo.ReturnCode == 0)
			if (IsEmpty(sPhoneNumber).ReturnCode == 0) {
				sPhoneNumber = StripCharsNotInBag(sPhoneNumber, '0123456789');
				if (!expPhone.test(sPhoneNumber)) {
					hReturnInfo.ReturnCode = 2;
					hReturnInfo.ErrorDescription = 'The phone number you enter is invalid.';
				}
			}                   		     
		
		return hReturnInfo;
	}

	//=========================================================================
	// Format a given string in a phone number form.
	//=========================================================================
	function FormatPhoneNumber(sPhoneNumber) {
	var sReturnValue = '';
	
		sPhoneNumber = StripCharsNotInBag(sPhoneNumber, '0123456789');
		if (sPhoneNumber.length == 10)
			sReturnValue = sPhoneNumber.substring(0,3) + '-' + sPhoneNumber.substring(3,6) + '-' + sPhoneNumber.substring(6,10);
			
		if (sPhoneNumber.length == 11)
			sReturnValue = '1-' + sPhoneNumber.substring(1,4) + '-' + sPhoneNumber.substring(4,7) + '-' + sPhoneNumber.substring(7,11);
	
		return sReturnValue;
	}


	//=========================================================================
	// Check if a given string contain a valid ZIP code.
	//=========================================================================
	function IsValidZipCode(sZipCode, bCanBeEmpty) {
	var expZipCode = /^\d{5}$|^\d{5}[\-\s]?\d{4}$|^[A-Z0-9]{3}[\-\s][A-Z0-9]{3}$/
	var hReturnInfo = new ReturnInfo(0, '', 0);
	
		if (!bCanBeEmpty && IsEmpty(sZipCode).ReturnCode == 1) {
			hReturnInfo.ReturnCode = 1;
			hReturnInfo.ErrorDescription = 'Please enter your zip code.';
		}

		if (hReturnInfo.ReturnCode == 0) {
			if (!expZipCode.test(sZipCode) && IsEmpty(sZipCode).ReturnCode == 0) {
				hReturnInfo.ReturnCode = 2;
				hReturnInfo.ErrorDescription = 'The zip code you entered is invalid.';
			}
		}                   
		     
		return hReturnInfo;
	}



	//=========================================================================
	// Check if a given string contain a valid e-mail address.
	//=========================================================================
	function IsValidEMailAddress(sEMailAddress, bCanBeEmpty) {
	var expEMail = /^[a-z]{1}[a-z_0-9\.\-]+@[a-z_0-9\.\-]+\.(([a-z]{2,3})|([a-z]{2,3}\.[a-z]{2}))$/i


	var hReturnInfo = new ReturnInfo(0, '', 0);
	
		if (!bCanBeEmpty && IsEmpty(sEMailAddress).ReturnCode == 1) {
			hReturnInfo.ReturnCode = 1;
			hReturnInfo.ErrorDescription = 'Please enter your e-mail address.';
		}

		if (hReturnInfo.ReturnCode == 0) {
			if (!expEMail.test(sEMailAddress) && IsEmpty(sEMailAddress).ReturnCode == 0) {
				hReturnInfo.ReturnCode = 2;
				hReturnInfo.ErrorDescription = 'The e-mail address you entered is invalid.';
			}
		}                   
		     
		return hReturnInfo;
	}



	//=========================================================================
	// Check if a given string contain a valid password.
	//=========================================================================
	function IsValidPassword(sPassword) {
	var expPassword = /[A-Za-z_0-9]{6,10}$/
	var hReturnInfo = new ReturnInfo(0, '', 0);
	
		if (!expPassword.test(sPassword)) {
			hReturnInfo.ReturnCode = 1;
			hReturnInfo.ErrorDescription = 'Password should be 6 - 10 charcters long and should contain only letters and digits';
		}
		     
		return hReturnInfo;
	}



	//=========================================================================
	// Check if a given string contain a valid product SKU.
	//=========================================================================
//	function IsValidSKU(sSKU, bCanBeEmpty) {
//	var expSKU = /^[A-Z0-9]+[/-]?[A-Z0-9]+$/i;
//	var hReturnInfo = new ReturnInfo(0, '', 0, 1);
//	var iIsEmpty;
//		
//		iIsEmpty = IsEmpty(sSKU).ReturnCode;
//		if (!bCanBeEmpty && iIsEmpty == 1) {
//			hReturnInfo.ReturnCode = 1;
//			hReturnInfo.ErrorSeverity = 3;
//			hReturnInfo.ErrorDescription = 'Please enter a product SKU.';
//		}
//
//		if (hReturnInfo.ReturnCode == 0) {
//			if (!expSKU.test(sSKU) && iIsEmpty == 0) {
//				hReturnInfo.ReturnCode = 2;
//				hReturnInfo.ErrorDescription = 'Please enter a valid product SKU. (A..Z, 0..9, -, no spaces)';
//			}
//		} 
//		    
//		return hReturnInfo;
//	}



	//=========================================================================
	// Check if a given number is a valid credit card number.
	//=========================================================================
	function IsValidCreditCard(sCardNumber, iCatdType) {
	var iDigitSum, iDigitMultiple, iCardNumberLength, iDigitIndex, iDigitProduct, iCurrentDigit;
	var sFirstDigit, sSecoundDigit;
	var hReturnInfo = new ReturnInfo(0, '', 0, 1);
	var bReturnValue = false;
	var NumericExp = /^\d{14,16}$/;
	
		//Removing white spaces from the credit card number.
		sCardNumber = StripWhitespace(sCardNumber);
	
		//Encoding only works on card number with 14 - 16 digits.
		bReturnValue = NumericExp.test(sCardNumber);
	
		//Performing Luhn Mod-10 Test on the credit card number.
		if (bReturnValue) {
			iDigitSum = 0;
			iDigitMultiple = 1;
			iCardNumberLength = sCardNumber.length;
			for (iDigitIndex = 0; iDigitIndex < iCardNumberLength; iDigitIndex++) {
				iCurrentDigit = sCardNumber.substring(iCardNumberLength - iDigitIndex - 1, iCardNumberLength - iDigitIndex);
				iDigitProduct = parseInt(iCurrentDigit ,10) * iDigitMultiple;
				if (iDigitProduct >= 10)
					iDigitSum += (iDigitProduct % 10) + 1;
				else
					iDigitSum += iDigitProduct;

				iDigitMultiple++
				if (iDigitMultiple > 2)
					iDigitMultiple = 1;
			}

			bReturnValue = ((iDigitSum % 10) == 0);
		}
		
	
		//Checking if the card number matches a specific credit card type.
		if (bReturnValue) {
			sFirstDigit = sCardNumber.substring(0,1);
			sSecoundDigit = sCardNumber.substring(1,2);
			
			switch(parseInt(iCatdType,10)) {
				//Visa: Sample number: 4111 1111 1111 1111 (16 digits)	
				case 1: 
					bReturnValue = (((sCardNumber.length == 16) || (sCardNumber.length == 13)) && (sFirstDigit == '4'));
					break;
					
				//MasterCard: Sample number: 5500 0000 0000 0004 (16 digits)
				case 2:	
					bReturnValue = ((sCardNumber.length == 16) && (sFirstDigit == '5') && ((sSecoundDigit >= '1') && (sSecoundDigit <= '5')));
					break;

				//American Express: Sample number: 340000000000009 (15 digits)
				case 3:	
					bReturnValue = ((sCardNumber.length == 15) && (sFirstDigit == '3') && ((sSecoundDigit == '4') || (sSecoundDigit == '7')));
					break;
				//Diners Club: Sample number: 30000000000004 (14 digits)
				case 4:	
					bReturnValue = ((sCardNumber.length == 14) && (sFirstDigit == '3') && ((sSecoundDigit == '0') || (sSecoundDigit == '6') || (sSecoundDigit == '8')))
					break;
				//Discover: Sample number: 6011000000000004 (16 digits)
				case 5:	
					bReturnValue = ((sCardNumber.length == 16) && (sCardNumber.substring(0,4) == "6011"));
			}
		}
				  
		
		if (bReturnValue == false) {
			hReturnInfo.ReturnCode = 1;
			hReturnInfo.ErrorSeverity = 3;
			hReturnInfo.ErrorDescription = 'The credit card number you entered is invalid.';
		}	
			  
		return hReturnInfo;
	}



	//=========================================================================
	// Removes all the characters in sBag from sString.
	//=========================================================================
	function StripCharsInBag (sString, sBag) {
	var iIndex, sCurrentChar;
	var sReturnString = '';


	    // Search through string's characters one by one.
	    // If character is not in bag, append to returnString.
	    for (iIndex = 0; iIndex < sString.length; iIndex++) {   
	        // Check that current character isn't whitespace.
	        sCurrentChar = sString.charAt(iIndex);
	        if (sBag.indexOf(sCurrentChar) == -1) 
				sReturnString += sCurrentChar;
	    }

	    return sReturnString;
	}



	//=========================================================================
	// Removes all the characters that are not in sBag from sString.
	//=========================================================================
	function StripCharsNotInBag(sString, sBag) {
	var iIndex, sCurrentChar;
	var sReturnString = '';


	    // Search through string's characters one by one.
	    // If character is in the bag, append to returnString.
	    for (iIndex = 0; iIndex < sString.length; iIndex++) {   
	        sCurrentChar = sString.charAt(iIndex);
	        if (sBag.indexOf(sCurrentChar) != -1) 
				sReturnString += sCurrentChar;
	    }

	    return sReturnString;
	}

	
	
	//=========================================================================
	// Removes all whitespace characters from sString.
	//=========================================================================
	function StripWhitespace (sString) {
		return StripCharsInBag (sString, ' \t\n\r');
	}
