//
// Insurance Company Management
// Bret Sheeley
// Moisture Mapper
//
// This document contains scripts that control the
// insurance company listings
//

var insuranceManagementToolsMenu = new Array();
insuranceManagementToolsMenu[0] = "<a href='/site/insurance/adjusters/adjusterManagement.php'>Adjuster Mgmt.</a>";
insuranceManagementToolsMenu[1] = "<a href='/site/insurance/regions/insuranceCompanyRegions.php'>Region Mgmt.</a>";

var insuranceAnalysisToolsMenu = new Array();
//insuranceAnalysisToolsMenu[0] = "<a href='/site/graphs/analysisRegionalGraphs.php?id1=-1'>Regional Summary</a>";
insuranceAnalysisToolsMenu[0] = "<a href='/site/insurance/analysisGraphs/regionalGraphs.php'>Regional Analysis</a>";
insuranceAnalysisToolsMenu[1] = "<a href='/site/insurance/analysisGraphs/contractorRegionalGraphs.php'>Contractor Analysis</a>";
insuranceAnalysisToolsMenu[2] = "<a href='/site/insuranceReports/summaryReports/summaryReportSelector.php'>Summary Report</a>";
insuranceAnalysisToolsMenu[3] = "<a href='/site/insurance/network/networkList.php'>Network Analysis</a>";

//
// Insurance Company List
//

// add insurance company
function addInsuranceCompanyToList() {
	var companyName = window.document.insData.newCompanyName.value;
	if(companyName != "") {
		window.document.insData.action = "/site/insuranceCompanyManagement/insuranceCompanyListAdd.php";
		window.document.insData.submit();
	} else {
		alert("A name is required for a new insurance company.");
	}
}


// remove insurance company
function removeInsuranceCompanyFromList(id) {
	var answer = confirm("Are you certain you wish to delete this entry?");
	if (answer) {
		window.location = "/site/insuranceCompanyManagement/insuranceCompanyListDelete.php?id=" + id;
	}
}

// readd insurance company
function readdInsuranceCompanyToList(companyName) {
	window.document.insData.newCompanyName.value = companyName;
	if(companyName != "") {
		window.document.insData.action = "/site/insuranceCompanyManagement/insuranceCompanyListAdd.php";
		window.document.insData.submit();
	} else {
		alert("A name is required for a new insurance company.");
	}
}

// edit insurance company
function editInsuranceCompanyListEntry() {
	var cid = window.document.insData.editCompanyID.value;
	var companyName = window.document.insData.editNewCompanyName.value;
	if(cid != 0) {
		if(companyName != "") {
			window.document.insData.action = "/site/insuranceCompanyManagement/insuranceCompanyListEdit.php";
			window.document.insData.submit();
		} else {
			alert("A name is required for the insurance company.");	
		}
	} else {
		alert("Select a company before editing.");	
	}
}

// choose insurance company to edit
function chooseInsuranceCompanyToEdit() {
	var selectedIndex = window.document.insData.editCompanyID.selectedIndex;
	if(selectedIndex != 0) {
		var stringValue = window.document.insData.editCompanyID.options[selectedIndex].text;
		window.document.insData.editNewCompanyName.value = stringValue;
		
		var value = window.document.insData.editCompanyID.options[selectedIndex].value;
		var result = window.document.getElementsByName(value)[0].value;
		
		for(var i=0; i<window.document.insData.editSubscription.length;i++) {
			if(window.document.insData.editSubscription[i].value == result) {
				window.document.insData.editSubscription[i].selected = true;
			}
		}
	}
}


//
// Region Management
//

// add region
function addRegionToList() {
	var regionName = window.document.insData.newRegionName.value;
	if(regionName != "") {
		window.document.insData.action = "/site/insurance/regions/insuranceCompanyRegionsAdd.php";
		window.document.insData.submit();
	} else {
		alert("A name is required for a new region.");
	}
}

// remove region
function removeRegionFromList(id) {
	var answer = confirm("Are you certain you wish to delete this entry?");
	if (answer) {
		window.location = "/site/insurance/regions/insuranceCompanyRegionsDelete.php?id=" + id;
	}
}


// readd region
function readdRegionToList(regionName) {
	window.document.insData.newRegionName.value = regionName;
	if(regionName != "") {
		window.document.insData.action = "/site/insurance/regions/insuranceCompanyRegionsAdd.php";
		window.document.insData.submit();
	} else {
		alert("A name is required for a new region.");
	}
}



// edit region
function editRegionList() {
	var selectedIndex = window.document.insData.editRegion.selectedIndex;
	if(selectedIndex != 0) {
		var id = window.document.insData.editRegion.options[selectedIndex].value;
		var regionName = window.document.insData.editNewRegionName.value;
		if(regionName != "") {
			window.document.insData.action = "/site/insurance/regions/insuranceCompanyRegionsEdit.php?id=" + id;
			window.document.insData.submit();
		} else {
			alert("A name is required for the region.");	
		}
	} else {
		alert("Select a region before editing.");	
	}
}

// choose region to edit
function chooseRegionToEdit() {
	var selectedIndex = window.document.insData.editRegion.selectedIndex;
	if(selectedIndex != 0) {
		var stringValue = window.document.insData.editRegion.options[selectedIndex].text;
		window.document.insData.editNewRegionName.value = stringValue;
	}
}

//
// Region Manager Management
//

// add manager
function addRegionManagerToList() {
	var selectedIndex = window.document.insData.newUserID.selectedIndex;
	if(selectedIndex != 0) {
		window.document.insData.action = "/site/insurance/regions/insuranceCompanyRegionManagersAdd.php";
		window.document.insData.submit();
	} else {
		alert("A region manager must be selected in order to be added.");	
	}
}



// remove manager
function removeRegionManangerFromList(id) {
	var answer = confirm("Are you certain you wish to delete this entry?");
	if (answer) {
		window.location = "/site/insurance/regions/insuranceCompanyRegionManagersDelete.php?id=" + id;
	}
}



//
// Adjuster Management
//

// add adjuster
function addAdjusterToList() {
	var selectedIndex = window.document.insData.newUserID.selectedIndex;
	if(selectedIndex != 0) {
		window.document.insData.action = "/site/adjusters/adjustersAdd.php";
		window.document.insData.submit();
	} else {
		alert("An adjuster must be selected in order to be added.");		
	}
}

// remove adjuster
function removeAdjusterFromList(id) {
	if(id > 0) {
		var answer = confirm("Are you certain you wish to delete this entry?");
		if (answer) {
			window.location = "/site/adjusters/adjustersDelete.php?id=" + id;
		}
	}
}



//=======================================
//
// Summary Report Generator
//
function summaryReportGenerate(timeSpanType,endDate) {
	
	// Get the form
	var myForms = window.document.getElementsByName('summaryReportGeneratorForm');
	var myForm = myForms[0];
	
	
	// Get Media
	var mediaObj = window.document.getElementsByName('mediaType');
	var mediaObjLength = mediaObj.length;
	for(var i = 0; i < mediaObjLength; i++) {
		if(mediaObj[i].checked) {
			var mediaValue	= mediaObj[i].value;
		}
	}
	
	
		
	// Month
	var mInput = window.document.createElement('input');
	mInput.type = 'hidden';
	mInput.id = 'month';
	mInput.name = 'month';
	if(timeSpanType == 'month' || timeSpanType == 'all') {
		mInput.value = 'on';
	} else {
		mInput.value = 'off';	
	}
	myForm.appendChild(mInput);
	
	// Quarter
	var qInput = window.document.createElement('input');
	qInput.type = 'hidden';
	qInput.id = 'quarter';
	qInput.name = 'quarter';
	if(timeSpanType == 'quarter' || timeSpanType == 'all') {
		qInput.value = 'on';
	} else {
		qInput.value = 'off';
	}
	myForm.appendChild(qInput);
	
	// Year
	var yInput = window.document.createElement('input');
	yInput.type = 'hidden';
	yInput.id = 'year';
	yInput.name = 'year';
	if(timeSpanType == 'year' || timeSpanType == 'all') {
		yInput.value = 'on';
	} else {
		yInput.value = 'off';	
	}
	myForm.appendChild(yInput);
	
	// endDate
	var endDateInput = window.document.createElement('input');
	endDateInput.type = 'hidden';
	endDateInput.id = 'currentDateOverride';
	endDateInput.name = 'currentDateOverride';
	endDateInput.value = endDate;
	myForm.appendChild(endDateInput);
	
	
	
	// Go to the Code to Update the Database
	if(mediaValue == "pdf") {
		var location = '/site/insuranceReports/summaryReports/printReport.php';
		myForm.target = '_top';
	} else {
		var location = '/site/insuranceReports/summaryReports/printReport.php?var=print';
		myForm.target = '_blank';
	}
	myForm.method = 'POST';
	myForm.action = location;
	myForm.submit();
	
}



//================================================
//
// Insurance Adjuster Management Change Region
//
function insuranceAdjusterManagementChangeRegion(id) {

	// get the new region
	var regionID = window.document.getElementsByName('adjusterRegionSelect_' + id)[0].value;

	// build form
	var myForm = addFormToWindow(window,'myForm');
	createFrame(window,'hiddenFrame');
	addPostToForm(myForm,'id',id);
	addPostToForm(myForm,'regionID',regionID);
	
	myForm.method = 'post';
	myForm.action = '/site/insurance/adjusters/adjusterManagementChangeRegion.php';
	myForm.target = 'hiddenFrame';
	myForm.submit();

}



//================================================
//
// Insurance Adjuster Management Change Region
//
function insuranceRegionalManagerManagementChangeRegion(id) {

	// get the new region
	var regionID = window.document.getElementsByName('managerRegionSelect_' + id)[0].value;


	// build form
	var myForm = addFormToWindow(window,'myForm');
	createFrame(window,'hiddenFrame');
	addPostToForm(myForm,'id',id);
	addPostToForm(myForm,'regionID',regionID);
	
	myForm.method = 'post';
	myForm.action = '/site/insurance/regionManagers/regionManagerManagementChangeRegion.php';
	myForm.target = 'hiddenFrame';
	myForm.submit();

}


//================================================
//
// Insurance Project Region Update
//
function insuranceProjectRegionUpdate(projectID) {

	var regionID = window.document.getElementsByName('regionSelect_' + projectID)[0].value;
	
	
	createFrame(window,'hiddenFrame');
	var myForm = addFormToWindow(window,'myForm');
	addPostToForm(myForm,'projectID',projectID);
	addPostToForm(myForm,'regionID',regionID);
	myForm.target = 'hiddenFrame';
	myForm.method = 'post';
	myForm.action = '/site/insurance/updateProjectRegion.php';
	myForm.submit();

}

//==============================================
//
// Insurance Project Region Lock Update
//
function insuranceProjectRegionLockUpdate(projectID) {
	
	
	var regionLock = window.document.getElementsByName('regionLock_' + projectID)[0].checked;
	
	
	createFrame(window,'hiddenFrame');
	var myForm = addFormToWindow(window,'myForm');
	addPostToForm(myForm,'projectID',projectID);
	addPostToForm(myForm,'regionLock',regionLock);
	myForm.target = 'hiddenFrame';
	myForm.method = 'post';
	myForm.action = '/site/insurance/updateProjectRegion.php';
	myForm.submit();
	
}
