//
//	K2.Tec, Inc.
//	Author: Kevin Yacoben
//	Date:   21/Sep/1999
//	File name : Strings.js
//
//	Purpose:	This file contains routines which test Strings or
//				converts items to strings.
//

//
//	This function checks to see if the given string 
//	contains whitespace
//
function isblank(theString)
{
	var c;
	for (var i = 0; i < theString.length; i++)
	{
		c = theString.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	} 
	return true;
}

//
//	This function checks to see if the given string is NULL length,
//	EMPTY or WhiteSpace
//
function checkBlankText(theString)
{
	if ((theString == null) || (theString == "") || isblank(theString))
		return true;
	return false;
}

//
// This function will check if a string is empty and if not if is the correct length
//
function checkStringLength(theString, theLen)
{
	if (!checkBlankText(theString))
	{
		if (theString.length != theLen)
			return false;
		else
			return true;
	}
	else
		return false;
}
//
// This function will add wild card characters to a string
//
function addWildCard(theString, theLen, chWild)
{
	if (!checkBlankText(theString))
		startCnt = theString.length;
	else
		startCnt = 0;
	retStr = theString;	
	for(cnt=startCnt; cnt < theLen; cnt++)
		retStr += chWild;
	return retStr;
}

//
//	This function determines if the given character is a digit
//
function isDigit(dg)
{
	if ((dg=='0') || (dg=='1') || (dg=='2') || (dg=='3') ||
		(dg=='4') || (dg=='5') || (dg=='6') || (dg=='7') || (dg=='8') || (dg=='9'))
		return true;
	else
		return false;
}

//
//	This function checks if the given value is a number
//
function isNumber(theString)
{
	var c;
	if (theString.length > 0)
	{
		for (var i = 0; i < theString.length; i++)
		{
			c = theString.charAt(i);
			if (!isDigit(c)) return false;
		} 
		return true;
	}
	else
		return false;
}
//
//	This function takes the input string and attempts to convert it
//	to a dollar string
//
function formatCurrency(numStr)
{
	var num;
	var formatStr;
	var strArry;
	
	if (numStr.length == 0) return "0.00";
	strArry = numStr.split(".");
	if (strArry.length > 2) return "0.00"
	// Find first non-blank character
	for(var fChar=0; fChar < strArry[0].length; fChar++)
	{
		if (strArry[0].charAt(fChar) != ' ') break;
	}
	if (strArry[0].charAt(fChar) == '-') fChar++;
	if ((fChar > strArry[0].length) || 
		(! isDigit(strArry[0].charAt(fChar)))) return "0.00";
	//Format head
	var cCnt = 0;
	var c;
	formatStr = "";
	for(var ii=strArry[0].length-1; ii >= fChar; ii--)
	{
		c = strArry[0].charAt(ii);
		if ((c != ',') && (! isDigit(c))) return "0.00";
		if ((c != ',') && (c != '.'))
		{
			if (cCnt==3)
			{
				cCnt = 1;
				formatStr = c + "," + formatStr;
			}
			else
			{
				cCnt ++;
				formatStr = c +formatStr;
			}
		} 
	}
	if (parseInt(formatStr) == 0) formatStr = "0";
	// Check for negative sign and add if needed
	if (fChar > 0)
	{
		if (strArry[0].charAt(fChar-1) == '-')
			formatStr = "-" + formatStr;
	}
	//Format mantisa
	if (strArry.length == 1) return formatStr + ".00";
	if (strArry[1].length == 1)
	{
		if (isDigit(strArry[1].charAt(0)))
			return formatStr +"."+ strArry[1] + "0";
		else
			return "0.00"
	}
	if ((isDigit(strArry[1].charAt(0))) &&
		(isDigit(strArry[1].charAt(1))))
		return formatStr + "." +strArry[1].charAt(0) + strArry[1].charAt(1);
	else
		return "0.00"
}

//
// This function will create a date string
//
function getDateStr(theDay,theMonth,theDayNum,theYear)
{
	objToday	= new Date();
	intDay		= objToday.getDay();
	intMonth	= objToday.getMonth();
	intYear		= objToday.getFullYear();
	intDayNum	= objToday.getDate();
	
	strRetVal	= '';

	aryDayName		= new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
	aryMonthName	= new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

	if (theDay) 
	{
		strRetVal = aryAayName[intDay];
	}
	if (theMonth)
	{
		if (strRetVal.Length > 0) strRetVal += ',';
		strRetVal += aryMonthName[intMonth];
	}
	if (theDayNum)
	{
		if (strRetVal.Length > 0) strRetVal += ' ';
		strRetVal += intDayNum;
	}
	if (theYear)
	{
		if (strRetVal.Length > 0) strRetVal += ',';
		strRetVal += intYear
	}
	return strRetVal
}
//
// This function will return the current time string
//
function getTimeStr()
{
	objToday	= new Date();
	hour		= objToday.getHours();
	minute		= objToday.getMinutes();
	strRetVal	= '';
	
	if (hour < 12)
	{
		if (hour == 0)
			strRetVal = '12:';
		else
			strRetVal = hour + ':';
		strRetVal += minute + ' am';
	}
	else
	{
		if (hour == 12)
			strRetVal = hour + ':';
		else
			strRetVal = hour-12 + ':';
		strRetVal += minute + ' pm';
	}
	return strRetVal;
}

function replaceChars(entry,findStr,replaceStr) 
{
	strTemp = "" + entry;

	while (strTemp.indexOf(findStr)>-1)
	{
		pos= strTemp.indexOf(findStr);
		strTemp = "" + (strTemp.substring(0, pos) + replaceStr + 
		strTemp.substring((pos + findStr.length), strTemp.length));
	}
	return strTemp;
}
function formatTimeString(hrs,mins,ampm)
{
	var timeStr;

	if (hrs == 0)
		timeStr = "12:";
	else
		if (hrs < 10)
			timeStr = "0" + hrs + ":";
		else
			timeStr = hrs + ":";
	if (mins == 0)
		timeStr += "00 ";
	else
		timeStr += mins + " ";
	if (ampm == 0)
		timeStr += "AM";
	else
		timeStr += "PM";
		
	return timeStr;
}

function formatNumber(numStr)
{
	retNum = parseInt(numStr);
	if (isNaN(retNum))
		return 0;
	else
		return retNum;
}

function removeZero(inStr)
{
    theStr = inStr;
    if (theStr.length > 1)
    {
        if (theStr.charAt(0) == '0')
        {
            return theStr.substring(1);
        }
        else
            return theStr;
    }
    else
        return theStr;
}

function verifyDateStr(dateStr,intMaxYr,intMinYr)
{
	var tmpAry = dateStr.split("/");
	if (tmpAry.length != 3)
		return false;
	var mth,dy,yr;

	mth = formatNumber(removeZero(tmpAry[0]));
	dy	= formatNumber(removeZero(tmpAry[1]));
	yr	= formatNumber(removeZero(tmpAry[2]));
	if ((mth < 1) || (mth > 12))
		return false;
	if ((dy < 1) || (dy > 31))
		return false;
	if ((yr>intMaxYr) || (yr<intMinYr))
		return false;

	return true;
}
function checkEmail(strEmailAddr)
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strEmailAddr))
	{
		return (true);
	}
	return (false);
}

