 /* ------------------------------------------------ © DPR Consulting Ltd. 1998-1999 -- */
/* -- Helper function(s) ---------------------------------------------------- Start -- */

/* -----------------------------------------------------------------------------------
	parseDate()
		Parameters
			day				-	Integer, Returns the day number.
			month			-	Integer, Returns the month number.
			year			-	Integer, Returns the year number.
		Returns
			0	-	parameters are OK and meets requirements.
			1	-	the year contains less or more digits required.
			2	-	the date, month and year contain letters and/or symbols.
			3	-	the date contain letters and/or symbols.
			4	-	the month contain letters and/or symbols.
			5	-	the year contain letters and/or symbols.
			6	-	the month contains less or more days than there actually is.
   ----------------------------------------------------------------------------------- */
function parseDate(day, month, year) {
var blnIsSafe = true;
var blnLeapYear = false;
var ErrorMsg = "";

	blnLeapYear = isleapYear(year);

	// Check for alphabetic and symbollic character(s) within variable
	if (hasAlpha(year) == true || hasAlpha(day) == true || hasSymbol(year) == true || hasSymbol(day) == true) {
		ErrorMsg += "  -  either the date or the year contain letters and/or symbols, these must contain numbers only.\n";
		blnIsSafe = false;
	}
	// Check that the year is consistent to 4 characters in length
	if (year.length != 4) {
		ErrorMsg += "  -  you entered a year that is not 4 digits in length (\'YYYY\').\n";
		blnIsSafe = false;
	}

	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 ||month == 10 || month == 12) {
		if ((day > 0 && day < 32) == false) {
			ErrorMsg += "  -  there are only 31 days in " + cMonthStr(month) + ".\n";
			blnIsSafe = false;
		}
	} else if (month == 4 || month == 6 || month == 9 || month == 11) {
		if ((day > 0 && day < 31) == false) {
			ErrorMsg += "  -  there are only 30 days in " + cMonthStr(month) + ".\n";
			blnIsSafe = false;
		}
	} else if (month == 2) {
		if (blnLeapYear && day > 29) {
			ErrorMsg += "  -  February, " + year + " has only 29 days.\n";
			blnIsSafe = false;
		} else if (!blnLeapYear && day > 28) {
			ErrorMsg += "  -  February, " + year + " has only 28 days.\n";
			blnIsSafe = false;
		}
	}

	if (!blnIsSafe) {
		return(ErrorMsg);
	} else {
		return(blnIsSafe);
	}
}

function parseMonthYear(strMonth, strYear) {
var blnIsSafe = true;

	if (strMonth == "0" && strYear.length == 0) {
			blnIsSafe = false;
	} else {
		if (strMonth == "0") {
			blnIsSafe = false;
		}

		if (strYear.length != 4) {
			blnIsSafe = false;
		} else {
			if (hasAlpha(strYear)) {
				blnIsSafe = false;
			}
		}
	}

	return(blnIsSafe);
}

/* -----------------------------------------------------------------------------------
	parseCurrency(value, params)
		Parameters
			value		-	String, Returns the object containing the array of options.
			params		-	String, Returns defined parameters on how textbox is 
							interrogated.
							Parameter options...
								symbol:a		-	Sets the currency symbol to to be used.
								dad:[n]			-	Digits After Decimal, Sets the number 
													of digits after the decimal point.
								dig:[n]			-	Digits In Group, Determines the number of  
													digits in currency to group.
								default:[n]		-	Sets the default currency value to return.
		Returns
			Value formatted to its respective currency else false.
   ----------------------------------------------------------------------------------- */
function parseCurrency(value, params) {
var acurAmount = new Array();
var aParams = new Array();
var aElement = new Array();
var blnRounded = false;
var curAmount = 0;
var curSymbol = "";
var curDef = 0;
var digitsAftDeci = 2;
var digitsInGroup = 0;
var lngCount;

	// If any, breakdown parameter string into an array.
	aParams = splitt(params, ";");

	if (typeof(params) != "undefined") {
		for (lngCount = aParams.length; lngCount > 0; lngCount--) {

			aElement = splitt(aParams[(lngCount - 1)], ":");

			if (aElement[0] == "symbol") {
				if (aElement[1] != "") curSymbol = aElement[1];
			} else if (aElement[0] == "rounded") {
				blnRounded = true;
			} else if (aElement[0] == "dad") {
				if (aElement[1] != "" || !isNaN(aElement[1])) digitsAftDeci = parseInt(aElement[1]);
			} else if (aElement[0] == "dig") {
				if (aElement[1] != "" || !isNaN(aElement[1])) digitsInGroup = parseInt(aElement[1]);
			} else if (aElement[0] == "default") {
				if (aElement[1] != "" || !isNaN(aElement[1])) curDef = parseInt(aElement[1]);
			}
		}
	}

	// Currency field is not needed but value has been added anyway.
	if (value.length > 0) {
		// Determine if the currency symbol is being used.
		if (value.substring(0, curSymbol.length) == curSymbol) {
			// Remove currency sybmbol.
			value = strip(value, curSymbol);
		} else if (curSymbol == "") {
			if (isNaN(value.substring(0, curSymbol.length))) return(curSymbol + curDef);
		} else {
		/* Check for any alphabetic and/or symbollic characters 
		   (excluding £, commas and decimal). */
			if (hasSymbol(value, curSymbol + ".") || hasAlpha(value)) return(curSymbol + curDef);
		}


		/*
			Checks currency format, making sure commas and decimal points are in the
			right posistions.
		*/
		acurAmount = splitt(value, ",");

		// Check the format of value.
		if (acurAmount.length > 1) {
			for (lngCount = 0; lngCount < acurAmount.length; lngCount++) {
				if (lngCount != 0 && lngCount != (acurAmount.length - 1)) {
					if (digitsInGroup != 0) {
						if (acurAmount[lngCount].length < digitsInGroup) {
							return(curSymbol + curDef);
							break
						}
					}
				} else if (lngCount == (acurAmount.length - 1)) {
					if (acurAmount[lngCount].length == digitsInGroup) {
						if (acurAmount[lngCount].substring(0, 1) == ".") {
							if (acurAmount[lngCount].substring(1, acurAmount[lngCount].length) == digitsAftDeci) {
								if (isNaN(acurAmount[lngCount].substring(1, 3))) {
									return(curSymbol + curDef);
									break
								}
							} else {
								return(curSymbol + curDef);
								break
							}
						} else if (isNaN(acurAmount[lngCount])) {
							return(curSymbol + curDef);
							break
						}
					}
				}
			}
		}

		if (blnRounded) {
			curAmount = parseInt(value);
		} else {
			curAmount = parseFloat(value);
		}
	}

	return(curSymbol + curAmount);
}

function isleapYear(lngYear)
{
	return (lngYear%4==0)&&(lngYear%100 != 0 || lngYear%400 == 0);
}

function cMonthStr(strMonth) {

	if (strMonth == "1") {
		return("January");
	} else if (strMonth == "2") {
		return("February");
	} else if (strMonth == "3") {
		return("March");
	} else if (strMonth == "4") {
		return("April");
	} else if (strMonth == "5") {
		return("May");
	} else if (strMonth == "6") {
		return("June");
	} else if (strMonth == "7") {
		return("July");
	} else if (strMonth == "8") {
		return("August");
	} else if (strMonth == "9") {
		return("September");
	} else if (strMonth == "10") {
		return("October");
	} else if (strMonth == "11") {
		return("November");
	} else if (strMonth == "12") {
		return("December");
	}
}

function hasAlpha(vChar, except) {
var astrException;
var blnIsSafe = false;
var Count, Count2;
var intLength = vChar.length;

	for (Count = 0; Count < intLength; Count++) {
		// Character code ranges between 65 to 90 (A - Z) and 97 to 122 (a - z)
		if (vChar.substring(Count, Count + 1) >= "A" && vChar.substring(Count, Count + 1) <= "Z" || 
			vChar.substring(Count, Count + 1) >= "a" && vChar.substring(Count, Count + 1) <= "z") 
		{
			if (typeof(except) != "undefined") {
				// If the exception string doesn't contain the current character,
				// we've found an alpha
				if (except.indexOf(vChar.substring(Count,Count+1)) == -1)
					blnIsSafe = true;
			} else {
				blnIsSafe = true;
			}
		}
	}

	return(blnIsSafe);
}



function hasNumeric(vChar) {
var blnIsSafe = false;
var Count;
var intLength = vChar.length;

	for (Count = 0; Count < intLength; Count++) {
		// Character code range 48 to 57 (0 - 9)
		if (vChar.substring(Count, Count + 1) >= "0" && vChar.substring(Count, Count + 1) <= "9") {
			return(true);
		}
	}

	return(blnIsSafe);
}

function hasSpaces(value) {
var blnIsSafe = false;
var Count;

	for (Count = 0; Count < value.length; Count++) {
		if (value.substring(Count, Count + 1) == " ") {
			blnIsSafe = true;
			break
		}
	}

	return(blnIsSafe);
}

function hasOnlySpaces(value) {
var blnIsSafe = true;

	for (var Count = 0; Count < value.length; Count++) {
		if (value.substring(Count, Count + 1) != " ") {
			blnIsSafe = false;
			break
		}
	}

	return(blnIsSafe);
}

function hasSymbol(vChar, except) {
var astrException;
var blnIsSafe = false;
var Count, Count2;
var intLength = vChar.length;

	for (Count = 0; Count < intLength; Count++) {
		// Character code ranges outside of 65 to 90 (A - Z) and 97 to 122 (a - z)
		if (!hasAlpha(vChar.substring(Count, Count + 1)) && !hasNumeric(vChar.substring(Count, Count + 1))) {
			if (typeof(except) != "undefined") {
				// If the exception string doesn't contain the current character,
				// we've found a symbol
				if (except.indexOf(vChar.substring(Count,Count+1)) == -1)
					blnIsSafe = true;
			} else {
				blnIsSafe = true;
			}
		}
	}

	return(blnIsSafe);
}

function hasAtSymbol(vChar) {
var blnIsSafe;
var intLength = vChar.length;
var Count;

	for (Count = 0; Count < intLength; Count++) {
		if (vChar.substring(Count, Count + 1) == "@") {
			return(true);
		} else {
			blnIsSafe = false;
		}
	}

	return(blnIsSafe);
}

function getOrder(lngNumber) {
var strPosition = "";

	if (lngNumber == 1) {
		strPosition += "1st";
	} else if (lngNumber == 2) {
		strPosition += "2nd";
	} else if (lngNumber == 3) {
		strPosition += "3rd";
	} else {
		strPosition += lngNumber + "th";
	}

	return(strPosition);
}

function parseNumber(numString) {
var lngCount;
var strNumber = "";

	if (numString.length > 0) {
		for (lngCount = numString.length; lngCount > 0; lngCount--) {
			if (hasNumeric(numString.substring(lngCount, lngCount + 1))) {
				strNumber += numString.substring(lngCount, substring + 1);
			} else {
				break
			}
		}
	}

	if (strNumber == "") {
		strNumber = "1";
	}

	return(Number(strNumber));
}

function parseCurrency(strCurrency) {
var Count;
var strAmount;
var dblAmount;

	if (strCurrency.substring(0, 1) == "£") {
		strAmount = strCurrency.substring(1, strCurrency.length);
		dblAmount = strAmount.parsefloat(strAmount.length);
	} else {
		dblAmount = strCurrency.parsefloat(strCurrency.length);
	}
					
	if (isNaN(Price)) {
		return(False);
	} else {
		return(Price);
	}
}

function lTrim(source, target) {
var Count = 0;
var strTrim;
var retVal;

	if (source.length < 2) {
		return(source);
	} else if (target.length > 1) {
		return(target);
	} else if (left(source, 1) != target) {
		return(source);
	}

	// Trim the left side of string
	while (source.substring(Count, Count + 1) == target || Count == source.length) {
		Count = Count + 1
	}

	return(source.substring(Count, source.length));
}

function rTrim(source, target) {
var Count;
var clone;

	if (source.length < 2) {
		return(source);
	} else if (target.length > 1) {
		return(target);
	} else if (right(source, 1) != target) {
		return(source);
	}

	// Trim the right side of string
	for (Count = source.length; Count > 0; Count--) {
		if (source.substring(Count, Count - 1) == target) {
			clone = source.substring(0, Count - 1);
		} else {
			break
		}
	}

	return(clone);
}

function oTrim(source, target) {
var Count1, Count2;
var CharCount = 0;
var clone;
var imageStr;

	if (source.length < 2) {
		return(source);
	} else if (target.length > 1) {
		return(target);
	}

	if (left(source, 1) == target) {
		// Trim the left side of string
		for (Count1 = 0; Count1 < source.length; Count1++) {
			if (source.substring(Count1, Count1 + 1) == target) {
				clone = right(source, source.length - (Count1 + 1));
			} else {
				break
			}
		}
	} else {
		clone = source;
	}

	if (right(clone, 1) == target) {
		// Trim the right side of string
		for (Count2 = clone.length; Count2 > 0; Count2--) {
			if (clone.substring(Count2, Count2 - 1) == target) {
				clone = left(clone, Count2 - 1);
			} else {
				break
			}
		}
	}

	return(clone);
}

function splitt(source, delimeter) {
var astrSplitt = new Array();
var blnIsEnd;
var lngCount, lngArrayCount = 0;
var lngMarker = 0;

	for (lngCount = 0; lngCount < source.length + 1; lngCount++) {
		if (delimeter == null) {
			astrSplitt[lngCount - 1] = source.substring(lngCount, lngCount - 1);
		} else if (source.substring(lngCount, lngCount + 1) == delimeter || lngCount == source.length) {
			astrSplitt[lngArrayCount] = lTrim(source.substring(lngMarker, lngCount), " ");
			lngMarker = lngCount + 1;
			lngArrayCount++;
		}
	}

	return(astrSplitt);
}

function strip(source, delimeter) {
var Count;
var startMark = 0, endMark = source.length;
var imageStr = source;

	if (delimeter == null || delimeter.length == 0) {
		return(source);
	} else {
		Count = startMark;

		while(Count < endMark) {
			if (imageStr.substring(Count, delimeter.length) == delimeter) {
				imageStr += source.substring(startMark, Count);
				Count = Count + delimeter.length;
				startMark = Count;
			} else {
				Count++;
			}
		}

		if (startMark < endMark) {
			imageStr = source.substring(startMark, endMark);
		}

		return(imageStr);
	}
}

function lastChar(value) {
var Count;
var charCount = 0;
	alert("in last char");
	if (hasAlpha(value)) {
		for (Count = 0; Count < value.length + 1; Count++) {
			alert(value.substring(Count, Count + 1));
			// Character code ranges between 65 to 90 (A - Z) and 97 to 122 (a - z)
			if (value.substring(Count, Count + 1) >= "A" && value.substring(Count, Count + 1) <= "Z" || 
				value.substring(Count, Count + 1) >= "a" && value.substring(Count, Count + 1) <= "z") {
				charCount++
			} else {
				break 
			}
		}
	}

	return (charCount);
}

function LastCharPos(value)
{	
	var Count;
	var charCount = 0;
	var tmpChar;
	if (hasAlpha(value))
	{
		for (Count = 0; Count < value.length; Count++) 
		{	
			tmpChar = value.substr(Count, 1);
			// Character code ranges between 65 to 90 (A - Z) and 97 to 122 (a - z)
			if ( (tmpChar >= "A" && tmpChar <= "Z") || 
				 (tmpChar >= "a" && tmpChar <= "z") ) 
			{	
				charCount = Count;
			} 
		}
	}
	return (charCount);
}


function LastNumber(value) {
var Count;
var charCount = 0;

	if (hasNumeric(value)) {
		for (Count = 0; Count < value.length + 1; Count++) {
			// Character code range 48 to 57 (0 - 9)
			if (value.substring(Count, Count + 1) >= "0" && value.substring(Count, Count + 1) <= "9") {
				charCount++
			} else {
				break 
			}
		}
	}

	return (charCount);
}

function left(source, len) {
var strImage;

	if (isNaN(len)) {
		strImage = source;
	} else {
		strImage = source.substring(0, len);
	}

	return(strImage);
}

function right(source, len) {
var strImage;

	if (isNaN(len)) {
		strImage = strImage = source;
	} else {
		strImage = source.substring(source.length - len, source.length);
	}

	return(strImage);
}

function Join(source, delimeter) {
var Count;
var Joined = "";

	if (typeof(source) == "object") {
		for (Count = 0; Count < source.length; Count++) {
			if (Count == (source.length - 1)) {
				Joined += source[Count];
			} else {
				Joined += source[Count] + delimeter;
			}
		}

		return(Joined);
	} else {
		return("");
	}
}

function cBoolLong(varDataType) {
var strDataType = typeof(varDataType);

	if (strDataType == 'boolean') {
		if (varDataType == true) {
			return(-1);
		} else if (varDataType == false) {
			return(0);
		}
	} else {
		alert('Type mismatch.');
		return(0);
	}
}

function cLongBool(varDataType) {
var strDataType = typeof(varDataType);

	if (strDataType == 'long' || strDataType == 'integer') {
		if (varDataType == -1) {
			return(true);
		} else if (varDataType == 0) {
			return(false);
		}
	} else {
		alert('Type mismatch.');
		return(false);
	}
}

/* -- Helper function(s) ------------------------------------------------------ End -- */
/* ------------------------------------------------ © DPR Consulting Ltd. 1998-1999 -- */