// teamTRACKER Core Javascript Library
// NOTE: convention for indexing arrays 
// is AA where A is the next consecutive
// unused character, not including L and O

// browser check
var IsNS4 = (document.layers) ? 1 : 0;
var IsIE4 = (document.all) ? 1 : 0;
var IsNS6 = (document.getElementById && !document.all) ? 1 : 0;
//alert (IsNS4 + ' ' + IsIE4 + ' ' + IsNS6);
//if (!IsNS4 && !IsIE4 && !IsNS6) alert ('unsufficient browser');

// value checking functions
function IsNull(Val) {
	return (Trim(Val) == "");
}

function IsNumeric(Val) {
	return (Val-0 == Val);
}

function IsInt(Val) {
	return (Val == parseInt(Val));
}

function IsAllDigits(Val) {
	for (aa=0; aa<Val.length; aa++)
		if (!IsInt(Val.charAt(aa)))
			return false;
	return true;
}

function IsBetween(Val,Num1,Num2) {
	return (Val > Num1 && Val < Num2);
}

function IsInRange(Val,Num1,Num2) {
	return (Val >= Num1 && Val <= Num2);
}

function IsGTZero (ThisValue) {
	return (ThisValue > 0);
}

function IsGTEZero (ThisValue) {
	return (ThisValue >= 0);
}

function Trim(Str) {
	if (Str) {
		if (Str.length > 0)
			while (Str.substring(0,1) == " ")
				Str = Str.substring(1);
		if (Str.length > 0)
			while (Str.substring(Str.length-1,Str.length) == " ")
				Str = Str.substring(0,Str.length-1);
		return Str;
	}
	return "";
}

function ContainsSpaces(Str) {
	return (Str.indexOf(" ") != -1);
}

function RemoveSpaces(Str) {
	var NewStr = "";
	for (bb=0; bb<Str.length; bb++) 
		if (Str.charAt(bb) != " ")
			NewStr += Str.charAt(bb);
	return NewStr;
}

// element masking functions
function MakeCaps(Element) {
	var Val = Element.value.toLowerCase();
	var NewVal = "";
	var ThisChar = "";
	var LastChar = "";
	var SecondToLast = "";
	var Cap = true;
	for (cc=0; cc<Val.length; cc++) {
		ThisChar = Val.substring(cc,cc+1);
		if (ThisChar == " " || (LastChar == "." && ThisChar == " ")) {
			Cap = true;
			NewVal += ThisChar;
		} else if (LastChar == "M" && ThisChar == "c") {
			Cap = true;
			NewVal += ThisChar;
		} else if (SecondToLast == "M" && LastChar == "a" && ThisChar == "c") {
			Cap = true;
			NewVal += ThisChar;
		} else if (Cap){
			Cap = false;
			NewVal += ThisChar.toUpperCase();
		} else {
			NewVal += ThisChar;
		}
		SecondToLast = LastChar;
		LastChar = ThisChar;
	}
	Element.value = NewVal;
}

function IsPhone(Element) {
	var Val = Element.value;
	var NewNumber = "";
	for (dd=1, ee=0; dd<=Val.length; dd++) {
		if (IsInt(Val.substring(dd-1,dd))) {
			NewNumber += Val.substring(dd-1,dd);
			ee++;
			if (ee == 3 || ee == 7) {
				NewNumber += "-";
				ee++;
			}
		}
	}
	if (ee != 12) NewNumber = "";
	if (!IsNull(NewNumber)) {
		Element.value = NewNumber;
		return true;
	}
	return false;
}

function IsPostalCode(Element) {
	var Val = Element.value;
	if (Val.length == 5 && IsAllDigits(Val))
		return true;
	if (Val.length == 10 && IsAllDigits(Val.substring(0,4)) && IsAllDigits(Val.substring(6,9)) && Val.indexOf('-') == 5)
		return true;
	return false;
}

function IsEmail(Element) {
	Element.value = RemoveSpaces(Element.value);
	var Val = Element.value;
	if (Val.indexOf('@') >= 1 &&
		Val.lastIndexOf('.') < Val.length - 2 &&
		Val.lastIndexOf('.') > Val.indexOf('@') + 1)
		return true;
	return false;
}

// date validation functions
var ThisYear = 0;
var ThisMonth = 0;
var ThisDay = 0;
var ThisDayOfYear = 0;

function IsDate(Val) {
	if (ThisYear == 0) return false;
	while (Val.indexOf("-") != -1)
		Val = Val.substring(0,Val.indexOf("-")) + "/" + Val.substring(Val.indexOf("-")+1,Val.length-1);
	var DateParts = Val.split("/");
	if (DateParts.length != 3) return false;
	for (ff=0; ff<DateParts.length; ff++)
		if (!IsAllDigits(DateParts[ff]))
			return false;
	if (DateParts[2] <= ThisYear-(-10) && DateParts[2] >= ThisYear-100) {
		switch (DateParts[0]-0) {
			// Febraury
			case 2: 
				if (DateParts[1] > 0 && DateParts[1] < 29) return true;
				else if (DateParts[1] == 29 && DateParts[2] % 4 == 0) return true;
				break;
			// January, March, May, July, August, October, December
			case 1: case 3: case 5: case 7: case 8: case 10: case 12: 
				if (DateParts[1] > 0 && DateParts[1] <= 31) return true;
				break;
			// April, June, September, November
			case 4: case 6: case 9: case 11: 
				if (DateParts[1] > 0 && DateParts[1] <= 30) return true;
				break;
		}
	}
	return false;
}

// format date like YYYYMMDD for comparison functions
function FormatDate(Val) {
	var DateParts = Val.split("/");
	return DateParts[2]*10000 - -DateParts[0]*100 - -DateParts[1];
}

// compare two dates 
function CompareDates(ValOne,Op,ValTwo) {
	if (!IsDate(ValOne) || !IsDate(ValTwo)) return false;
	var Date1 = parseInt(FormatDate(ValOne));
	var Date2 = parseInt(FormatDate(ValTwo));
	switch (Op) {
		case "EQ":
			return (Date1 == Date2);
			break;
		case "GT":
			return (Date1 > Date2);
			break;
		case "GTE":
			return (Date1 >= Date2);
			break;
		case "LT":
			return (Date1 < Date2);
			break;
		case "LTE":
			return (Date1 <= Date2);
			break;
	}
	return false;
}

function DaysInMonth(mm,yy) {
	switch (mm) {
		case 2:
			if (yy%4 == 0) return 29;
			return 28;
			break;
		case 1: case 3: case 5: case 7: case 8: case 10: case 12:
			return 31;
			break;
		case 4: case 6: case 9: case 11:
			return 30;
			break;
	}
	return 0;
}

function DateDiff(ThisDatePart, ThisDate, ThisDate2) {
	if (ThisDate > ThisDate2) {
		var TempDate = ThisDate2;
		ThisDate2 = ThisDate;
		ThisDate = TempDate;
	}
	if (ThisDatePart.toLowerCase() == "d") {
		Total1 = DaysInYearSoFar(ThisDate.getMonth(),ThisDate.getYear()) + ThisDate.getDate();
		Total2 = DaysInYearSoFar(ThisDate2.getMonth(),ThisDate2.getYear()) + ThisDate2.getDate();
		Total3 = DaysInYearSpan(ThisDate.getYear(),ThisDate2.getYear());
		return Total3 + Total2 - Total1;
	} else if (ThisDatePart.toLowerCase() == "w") {
		Total1 = DaysInYearSoFar(ThisDate.getMonth(),ThisDate.getYear()) + ThisDate.getDate();
		Total2 = DaysInYearSoFar(ThisDate2.getMonth(),ThisDate2.getYear()) + ThisDate2.getDate();
		Total3 = DaysInYearSpan(ThisDate.getYear(),ThisDate2.getYear());
		return (Total3 + Total2 - Total1) / 7;
	} else if (ThisDatePart.toLowerCase() == "m") {
		return ThisDate2.getMonth() - ThisDate.getMonth() + (ThisDate2.getYear() - ThisDate.getYear()) / 12;
	}
	return 0;
}

function DaysInYearSoFar(mmm,yyy) {
	for (ThisTotal = 0, ii=0; ii<mmm; ii++)
		ThisTotal += DaysInMonth(ii+1, yyy);
	return ThisTotal;
}

function DaysInYearSpan(y1,y2) {
	var TotalYears = 0;
	for (ii=y1; ii<y2; ii++) {
		if (ii % 4 == 0)
			TotalYears += 366;
		else TotalYears += 365;
	}
	return TotalYears;
}

// event-driven functions
var ClickedOnce = false;
var ClickPause;
var LastSI = -1;
function FakeDoubleClick (ThisList) {
	ThisList.blur();
	if (ThisList.selectedIndex == LastSI && ClickedOnce) {
		ClickedOnce = false;
		clearTimeout(ClickPause);
		return true;
	} 
	ClickedOnce = true;
	clearTimeout(ClickPause);
	ClickPause = setTimeout("ClickedOnce=false",300);
	LastSI = ThisList.selectedIndex;
	return false;	
}

function CheckMultiSelects (ThisElement) {
	if (ThisElement[0].selected) 
		for(i=1;i<ThisElement.length;i++) 
			ThisElement[i].selected = false;
}

// window handling functions
var HelpContent = "";

function Help() {
	BCHelpWin = window.open("", "BCHelpWin", "height=475,width=525,scrollbars=yes,resizable=yes");
	BCHelpWin.focus();
	BCHelpWin.location.href = "/Help/Help.cfm?HelpContent=" + HelpContent;
} 

function ShowCopyright() {
	BCCopyrightWin = window.open("", "BCCopyrightWin", "height=475,width=500,scrollbars=yes,resizable=yes");
	BCCopyrightWin.focus();
	BCCopyrightWin.location.href = "/Copyright.cfm";
}

function ShowPrivacy() {
	BCCopyrightWin = window.open("", "BCCopyrightWin", "height=475,width=500,scrollbars=yes,resizable=yes");
	BCCopyrightWin.focus();
	BCCopyrightWin.location.href = "/Privacy.cfm";
}
