// general interface functions
function divDisplaySwap(showDivID, hideDivID) {
	$('#' + hideDivID).hide();
	$('#' + showDivID).show();
	return false;
}

function multiDivDisplaySwap(showDivID, idPrefix) {
	$('div[id^=' + idPrefix + ']').hide();
	$('#' + showDivID).show();
	return false;
}

function multiDivSlideSwap(showID, idPrefix) {
	$('div[id^=' + idPrefix + ']').each( function(i) {
		if ($('#' + this.id).css('display') == 'block' && this.id != showID) {
			$('#' + this.id).slideUp(500);
		}
	});
	if ($('#' + showID).css('display') == 'block') {
		$('#' + showID).slideUp(500);
	} else {
		$('#' + showID).slideDown(500);
	}
	return false;
}

function imageSwap(imageID, mainImg, altImg) {
	if ($('#' + imageID).attr('src') == mainImg) {
		$('#' + imageID).attr('src', altImg);
	} else {
		$('#' + imageID).attr('src', mainImg);
	}
}

function pagingDivDisplay(idPrefix, tabIdPrefix, numberToShow, containerID) {
	if (!$('div#' + tabIdPrefix + numberToShow).hasClass('active')) {
		var tempHeight = $('#' + containerID).height();

		// $('#'+containerID).css('height',tempHeight);
		// var tempHeightTwo = 0;

		$('#' + containerID).css('min-height', tempHeight);

		if ($.browser.msie && $.browser.version.substr(0, 1) == '6') {
			// set height for ie6
			$('#' + containerID).css('height', tempHeight);
		}

		$('div[id^=' + idPrefix + ']').hide();
		$('div[id^=' + tabIdPrefix + ']').removeClass('active');

		$('div#' + tabIdPrefix + numberToShow).addClass('active');
		$('div#' + idPrefix + numberToShow).fadeIn(500);

		// tempHeightTwo = $('#'+containerID).height();

		// if ( Number(tempHeightTwo) < Number(tempHeight) ) {
		// $('#'+containerID).css('height',tempHeight);
		// }
	}
}

function accordianDivDisplay(idPrefix, numberToShow) {
	if (!$('div#' + idPrefix + numberToShow).hasClass('active')) {
		$('div[id^=' + idPrefix + ']').each( function() {
			if ($('#' + this.id).hasClass('active')) {
				$('#' + this.id).slideUp(500);
			}
		});
		$('div[id^=' + idPrefix + ']').removeClass('active');

		$('div#' + idPrefix + numberToShow).addClass('active');
		$('div#' + idPrefix + numberToShow).slideDown(500);
	}
}

function addHeaderRollovers() {
	// main policy holders nav
	$('div#navContainer div.headerNavItem a img').mouseover( function() {
		this.src = this.src.replace(/.gif/ig, '_rollover.gif');
	});
	$('div#navContainer div.headerNavItem a img').mouseout( function() {
		this.src = this.src.replace(/_rollover.gif/ig, '.gif');
	});

	// policy holders home nav
	$('div#homeNavContainer div.headerNavItem a img').mouseover( function() {
		this.src = this.src.replace(/.gif/ig, '_rollover.gif');
	});
	$('div#homeNavContainer div.headerNavItem a img').mouseout( function() {
		this.src = this.src.replace(/_rollover.gif/ig, '.gif');
	});
}

function loadURL(newUrl) {
	if (newUrl.length > 0) {
		location.href = newUrl;
	}
}
// end general interface functions

// jqGrid functions
function claimsGrid_showPast3Yrs(nowYear) {
	var tempYearOne = Number(nowYear);
	var tempYearTwo = Number(tempYearOne) - 1;
	var tempYearThree = Number(tempYearTwo) - 1;
	jqGrid_hideAll("claimsList");
	$("table#claimsList tr").each(
			function(i) {
				var found = false;
				$("tr#" + this.id + " td[title^=" + tempYearOne + "]").each(
						function(itr) {
							found = true;
						});
				$("tr#" + this.id + " td[title^=" + tempYearTwo + "]").each(
						function(itr) {
							found = true;
						});
				$("tr#" + this.id + " td[title^=" + tempYearThree + "]").each(
						function(itr) {
							found = true;
						});
				if (found) {
					$("#" + this.id).show();
				}
			});
}

function claimsGrid_showPast5Yrs(nowYear) {
	var tempYearOne = Number(nowYear);
	var tempYearTwo = Number(tempYearOne) - 1;
	var tempYearThree = Number(tempYearTwo) - 1;
	var tempYearFour = Number(tempYearThree) - 1;
	var tempYearFive = Number(tempYearFour) - 1;
	jqGrid_hideAll("claimsList");
	$("table#claimsList tr").each(
			function(i) {
				var found = false;
				$("tr#" + this.id + " td[title^=" + tempYearOne + "]").each(
						function(itr) {
							found = true;
						});
				$("tr#" + this.id + " td[title^=" + tempYearTwo + "]").each(
						function(itr) {
							found = true;
						});
				$("tr#" + this.id + " td[title^=" + tempYearThree + "]").each(
						function(itr) {
							found = true;
						});
				$("tr#" + this.id + " td[title^=" + tempYearFour + "]").each(
						function(itr) {
							found = true;
						});
				$("tr#" + this.id + " td[title^=" + tempYearFive + "]").each(
						function(itr) {
							found = true;
						});
				if (found) {
					$("#" + this.id).show();
				}
			});
}

function jqGrid_showAll(gridID) {
	$("table#" + gridID + " tr").each( function(i) {
		if (this.id != "_empty") {
			$("#" + this.id).show();
		}
	});
}
function jqGrid_hideAll(gridID) {
	$("table#" + gridID + " tr").each( function(i) {
		if (this.id != "_empty") {
			$("#" + this.id).hide();
		}
	});
}
// end jqGrid functions

// data container functions
function activateDivTab(tabIdPrefix, numberToActivate) {
	$("div[id^=" + tabIdPrefix + "]").removeClass('active');
	$("div#" + tabIdPrefix + numberToActivate).addClass('active');
}

function dataContainerShowHideRows(rowIdPrefix, setToShow, range, pagingRowId) {
	var endNum = Number(setToShow) * Number(range);
	var startNum = Number(endNum) - (Number(range) - 1);
	var i = 1;

	$("tr[id^=" + rowIdPrefix + "]").hide();
	for (i = startNum; i <= endNum; i++) {
		$("tr#" + rowIdPrefix + i).show();
	}

	// reset paging row display
	$("#" + pagingRowId).show();
	$("#" + pagingRowId + "TabContainer").show();
	$("#" + pagingRowId + "TabContainer_years").hide();

	activateDivTab(rowIdPrefix + "tab_", setToShow);
}

function dataContainerShowHideRows_years(rowIdPrefix, setToShow, range,
		pagingRowId, yearsBack, nowYear) {
	var endNum = Number(setToShow) * Number(range);
	var startNum = Number(endNum) - (Number(range) - 1);
	var arrIter = 1;
	var j = 1;
	var found = false;
	var arrYears = new Array();
	var tempNowDate = new Date();
	var foundCount = 0;

	if (Number(yearsBack) < 1) {
		yearsBack = 1;
	}
	if (Number(nowYear) < 1900) {
		nowYear = tempNowDate.getUTCFullYear();
	}

	// build an array of years to check
	for (arrIter = 1; arrIter <= yearsBack; arrIter++) {
		arrYears[arrIter - 1] = Number(nowYear) - (arrIter - 1);
	}

	$("tr[id^=" + rowIdPrefix + "]").hide();

	// for (i=startNum; i<=endNum; i++) {
	// $("tr#"+rowIdPrefix+i).show();
	// }

	/*
	 * $("tr[id^="+rowIdPrefix+"]").each(function(i){ found = false;
	 * 
	 * if ( i >= startNum && i <= endNum ) { for (j = 0; j<arrYears.length;
	 * j++) { $("tr#"+this.id+"
	 * td[title^=date_"+arrYears[j]+"]").each(function(itr){found=true;}); } }
	 * 
	 * if (found) { $("#"+this.id).show(); }
	 * 
	 * });
	 */

	for (j = 0; j < arrYears.length; j++) {
		$("tr[id^=" + rowIdPrefix + "] td[title^=date_" + arrYears[j] + "]")
				.each( function(itr) {

					// found=true;
						foundCount++;

						if (foundCount >= startNum && foundCount <= endNum) {
							$(this).parent().show();
						}

					});
	}

	// set paging row display
	$("#" + pagingRowId).show();
	$("#" + pagingRowId + "TabContainer").hide();
	$("#" + pagingRowId + "TabContainer_years").show();

	activateDivTab(rowIdPrefix + "tabset2_", setToShow);
}

function dataContainer_showPast3Yrs(rowIdPrefix, nowYear, maxShow, pagingRowId) {
	var tempYearOne = Number(nowYear);
	var tempYearTwo = Number(tempYearOne) - 1;
	var tempYearThree = Number(tempYearTwo) - 1;
	var shown = 0;
	var foundcount = 0;
	var found = false;
	var tabCount = 0;
	var tempNum = 0;

	// show hide rows
	$("tr[id^=" + rowIdPrefix + "]").hide();
	$("tr[id^=" + rowIdPrefix + "]").each(
			function(i) {
				found = false;
				$("tr#" + this.id + " td[title^=date_" + tempYearOne + "]")
						.each( function(itr) {
							found = true;
						});
				$("tr#" + this.id + " td[title^=date_" + tempYearTwo + "]")
						.each( function(itr) {
							found = true;
						});
				$("tr#" + this.id + " td[title^=date_" + tempYearThree + "]")
						.each( function(itr) {
							found = true;
						});
				if (Number(maxShow) > 0) {
					if (found && shown < Number(maxShow)) {
						$("#" + this.id).show();
						shown++;
					}
				} else {
					if (found) {
						$("#" + this.id).show();
						shown++;
					}
				}
				if (found) {
					foundcount++;
				}
			});

	// show hide paging div
	if (foundcount > shown) {

		dataContainer_setYearPagingTabDisplay_years(pagingRowId, rowIdPrefix,
				tabCount, maxShow, 3, nowYear);
	} else {
		$("#" + pagingRowId).hide();
	}
}

function dataContainer_showPast5Yrs(rowIdPrefix, nowYear, maxShow, pagingRowId) {
	var tempYearOne = Number(nowYear);
	var tempYearTwo = Number(tempYearOne) - 1;
	var tempYearThree = Number(tempYearTwo) - 1;
	var tempYearFour = Number(tempYearThree) - 1;
	var tempYearFive = Number(tempYearFour) - 1;
	var shown = 0;
	var foundcount = 0;
	var found = false;
	var tabCount = 0;
	var tempNum = 0;

	// show hide rows
	$("tr[id^=" + rowIdPrefix + "]").hide();
	$("tr[id^=" + rowIdPrefix + "]").each(
			function(i) {
				found = false;
				$("tr#" + this.id + " td[title^=date_" + tempYearOne + "]")
						.each( function(itr) {
							found = true;
						});
				$("tr#" + this.id + " td[title^=date_" + tempYearTwo + "]")
						.each( function(itr) {
							found = true;
						});
				$("tr#" + this.id + " td[title^=date_" + tempYearThree + "]")
						.each( function(itr) {
							found = true;
						});
				$("tr#" + this.id + " td[title^=date_" + tempYearFour + "]")
						.each( function(itr) {
							found = true;
						});
				$("tr#" + this.id + " td[title^=date_" + tempYearFive + "]")
						.each( function(itr) {
							found = true;
						});
				if (Number(maxShow) > 0) {
					if (found && shown < Number(maxShow)) {
						$("#" + this.id).show();
						shown++;
					}
				} else {
					if (found) {
						$("#" + this.id).show();
						shown++;
					}
				}
				if (found) {
					foundcount++;
				}
			});

	// show hide paging div
	if (foundcount > shown) {
		tabCount = parseInt(foundcount / shown);
		if (foundcount % shown > 0) {
			tabCount += 1;
		}
		dataContainer_setYearPagingTabDisplay_years(pagingRowId, rowIdPrefix,
				tabCount, Number(maxShow), 5, nowYear);
	} else {
		$("#" + pagingRowId).hide();
	}
}

function dataContainer_setYearPagingTabDisplay_years(pagingRowId, rowIdPrefix,
		tabCount, maxShow, yearsBack, nowYear) {
	var newHtml = '<div class="left">page:</div>';
	var tempClass = '';
	var i = 0;

	$("#" + pagingRowId).show();
	$("#" + pagingRowId + "TabContainer").hide();
	$("#" + pagingRowId + "TabContainer_years").show();

	// dataContainerShowHideRows_years
	// tabCount should always be greater than 1 if this function is called
	for (i = 1; i <= Number(tabCount); i++) {
		if (i == 1) {
			tempclass = 'tab active';
		} else {
			tempclass = 'tab';
		}
		newHtml += '<div id="' + rowIdPrefix + 'tabset2_' + i + '" class="'
				+ tempclass + '" onclick="dataContainerShowHideRows_years(\''
				+ rowIdPrefix + '\',Number(' + i + '),Number(' + maxShow
				+ '),\'' + pagingRowId + '\',Number(' + yearsBack + '),Number('
				+ nowYear + ')); return false;">' + i + "</div>";
	}
	newHtml += '<div class="clear"></div>';

	$("#" + pagingRowId + "TabContainer_years").attr('innerHTML', newHtml);

}
// end data container functions

// form functions
function formField_slideDiv(inputElem, divID, toShowInputVal,callBackFn) {
	if (inputElem.type.toLowerCase() == 'radio' || inputElem.type.toLowerCase() == 'checkbox') {
		if (String(inputElem.value).toLowerCase() == String(toShowInputVal).toLowerCase() && inputElem.checked) {
			if ( callBackFn != null && callBackFn.length > 0 ) {
				$('#' + divID).slideDown(500,function(){ eval(callBackFn); });
			}
			else {
				$('#' + divID).slideDown(500);
			}
		} 
		else {
			if ( callBackFn != null && callBackFn.length > 0 ) {
				$('#' + divID).slideUp(500,function(){ eval(callBackFn); });
			}
			else {
				$('#' + divID).slideUp(500);
			}
		}
	} 
	else if (inputElem.type.toLowerCase() == 'text') {
		// TODO
	} 
	else {
		// TODO 
	}
}

function setVehicleFieldValidation() { // toggle vehicle data form validation
	if ($("div#vehicleInfoDiv").css("display") != "block") {
		$("input[name=PH_VehicleYear]").rules("remove");
		$("input[name=PH_VehicleMake]").rules("remove");
		$("input[name=PH_VehicleModel]").rules("remove");
		$("input[name=WhoWasDriving]").rules("remove");
		$("input[name=ph_vehicledrivable]").rules("remove");
		$("textarea[name=PA_Description]").rules("remove");
	} 
	else {
		$("input[name=PH_VehicleYear]").rules("add",{ required: true, digits: true, minlength: 2 });
		$("input[name=PH_VehicleMake]").rules("add",{ required: true });
		$("input[name=PH_VehicleModel]").rules("add",{ required: true });
		$("input[name=WhoWasDriving]").rules("add",{ required: true });
		$("input[name=ph_vehicledrivable]").rules("add",{ required: true });
		$("textarea[name=PA_Description]").rules("add",{ required: true });
	}
}

function setInjuryFieldValidation() { // toggle injury field validation
	if ($("div#whoWasInjuredDiv").css("display") != "block") {
		$("input#WhoInjured").rules("remove");
	} 
	else {
		$("input#WhoInjured").rules("add",{ required: true });
	}
}

function recheckErrorDivs () {
	$("div.error").each(function(i){
		hasErrorChild = false;
		$(this).children().each(function(k){
			$(this).children().each(function(j){
				if ($(this).hasClass("error")){
					hasErrorChild = true;
				}
			});
		});
		if (!hasErrorChild){
			$(this).removeClass("error"); // take away error class for rows with no errors in them
		}
	});
	$("div.row div .error").each(function(i){
		$tempRow = $(this).parent().parent();
		if (!$tempRow.hasClass("error")){
			$tempRow.addClass("error"); // add error class to rows with errors in them
		}
	});
}

function validateSubmitAClaimForm() {
	// not using this validation any more
	return true;
}
// end form functions

// Date Validation and Format Javascript
// copyright 11th June 2007 by Stephen Chapman
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (including these
// comments) is used without any alteration
// you may swap the 12 and 31 around if you want mm/dd instead of dd/mm

function dtval(d,e) {
var pK = e ? e.which : window.event.keyCode;
if (pK == 8) {d.value = substr(0,d.value.length-1); return;}
var dt = d.value;
var da = dt.split('/');
for (var a = 0; a < da.length; a++) {if (da[a] != +da[a]) da[a] = da[a].substr(0,da[a].length-1);}
if (da[0] > 31) {da[1] = da[0].substr(da[0].length-1,1);da[0] = '0'+da[0].substr(0,da[0].length-1);}
if (da[1] > 12) {da[2] = da[1].substr(da[1].length-1,1);da[1] = '0'+da[1].substr(0,da[1].length-1);}
if (da[2] > 9999) da[1] = da[2].substr(0,da[2].length-1);
dt = da.join('/');
if (dt.length == 2 || dt.length == 5) dt += '/';
d.value = dt;
}



// ****** on document ready functionality ******
$(document).ready( function() {
	addHeaderRollovers();
	
});
