// JavaScript Document


/************ BEGIN MODULE CFMS ***************/
/**************************************************/

function DisableCFHeader() // when no council file is found erase the council file header info
{
	window.document.getElementById('CouncilFileHeader').innerHTML = '';
}


function PrintReport(rpt) // Prints Search Results 
	{
	
	alert('Attention: Some reports can take a few minutes to generate depending on the amount of data they contain. \nPlease be patient. ');
	
	if (rpt == 1) // summary report by council file number (PDF)
		{
			window.document.PrintRpt.action = 'index.cfm?fa=vcfi.dsp_CFMS_Report&rptid=1';
		}
	if (rpt == 2) // summary report by last changed date (PDF)
		{
			window.document.PrintRpt.action = 'index.cfm?fa=vcfi.dsp_CFMS_Report&rptid=2';
		}
	if (rpt == 3) // detailed report by council file number (PDF)
		{
			window.document.PrintRpt.action = 'index.cfm?fa=vcfi.dsp_CFMS_Report&rptid=3';
		}
	if (rpt == 4) // detailed report by last change date (PDF)
		{
			window.document.PrintRpt.action = 'index.cfm?fa=vcfi.dsp_CFMS_Report&rptid=4';
		}
	if (rpt == 5) // detailed report (Excel)
		{
			window.document.PrintRpt.action = 'index.cfm?fa=vcfi.dsp_CFMS_Report&rptid=5';
		}

	window.document.PrintRpt.submit(); // submit the form
	}
	
function searchfocus() // set the focus to the simple search box
	{
	document.getElementById('basicsearch').focus();
	}

function resetBasic() // resets the basic form
{
	document.getElementById('basicsearch').value = '' ;
	document.getElementById('secondterm').value = '' ;
	document.getElementById('searchtype').value = 1 ;

}

function resetAdvanced() // resets the advanced form
{
	var frm = document.getElementById('AdvancedSearch1') ;
	
	for (i = 0; i < frm.elements.length; i++)
	{
		if (frm[i].type == 'checkbox')
			{
				frm[i].checked = false;
			}
		else if ((frm[i].type == 'text') || (frm[i].type == 'select-one'))
			{
				frm[i].value = '' ;
			}
	}
}

function resetSearchForm() //clear all search form values
{
	//reset basic form
	resetBasic();
	
	// reset advanced form
	resetAdvanced();
	
	//hide reward section
	document.getElementById('rewardsection').style.display = 'none';
	
	//clear results
	 document.getElementById('ContentRight').innerHTML = '';

	//clear highlighted sections
	var frm = window.document.AdvancedSearch1 ;
		for (i = 0; i < AdvancedSearch1.elements.length; i++)
			{
				if (frm[i].type == 'text')
					{
						frm[i].style.backgroundColor = '';
					}
			}
}

function doSearchSimple(){ // Basic Search
	
	if (document.getElementById('basicsearch').value == '')
		{
		alert('You must enter a search criteria to perform a search.');
		return false;
		}
		
	var callback = function(text)
		{
		
		}
		
	var failure = function(text)
		{
				
		}
	
	ColdFusion.navigate('index.cfm?fa=vcfi.doSearch','ContentRight', callback, failure ,'POST','SimpleSearch1');

}

function doSearchAdv(){ //Advanced Search

//date validations

var thedate = document.getElementById('DateRecStart') ;
if (checkDate(thedate.value) == false)
	{
	thedate.focus();
	thedate.style.backgroundColor = 'yellow';
	return false;
	}

var thedate = document.getElementById('DateRecEnd') ;
if (checkDate(thedate.value) == false)
	{
	thedate.focus();
	thedate.style.backgroundColor = 'yellow';
	return false;
	}

var thedate = document.getElementById('DateExpStart') ;
if (checkDate(thedate.value) == false)
	{
	thedate.focus();
	thedate.style.backgroundColor = 'yellow';
	return false;
	}

var thedate = document.getElementById('DateExpEnd') ;
if (checkDate(thedate.value) == false)
	{
	thedate.focus();
	thedate.style.backgroundColor = 'yellow';
	return false;
	}
var thedate = document.getElementById('RewPosStart') ;
if (checkDate(thedate.value) == false)
	{
	thedate.focus();
	thedate.style.backgroundColor = 'yellow';
	return false;
	}
var thedate = document.getElementById('RewPosEnd') ;
if (checkDate(thedate.value) == false)
	{
	thedate.focus();
	thedate.style.backgroundColor = 'yellow';
	return false;
	}
var thedate = document.getElementById('RewExpStart') ;
if (checkDate(thedate.value) == false)
	{
	thedate.focus();
	thedate.style.backgroundColor = 'yellow';
	return false;
	}
var thedate = document.getElementById('RewExpEnd') ;
if (checkDate(thedate.value) == false)
	{
	thedate.focus();
	thedate.style.backgroundColor = 'yellow';
	return false;
	}
var thedate = document.getElementById('ActDateStart') ;
if (checkDate(thedate.value) == false)
	{
	thedate.focus();
	thedate.style.backgroundColor = 'yellow';
	return false;
	}
var thedate = document.getElementById('ActDateEnd') ;
if (checkDate(thedate.value) == false)
	{
	thedate.focus();
	thedate.style.backgroundColor = 'yellow';
	return false;
	}

var thedate = document.getElementById('SchDateStart') ;
if (checkDate(thedate.value) == false)
	{
	thedate.focus();
	thedate.style.backgroundColor = 'yellow';
	return false;
	}

var thedate = document.getElementById('SchDateEnd') ;
if (checkDate(thedate.value) == false)
	{
	thedate.focus();
	thedate.style.backgroundColor = 'yellow';
	return false;
	}

//date  range validations
var startdate = document.getElementById('DateRecStart') ;
var enddate = document.getElementById('DateRecEnd') ;

if (enddate.value != '' && startdate.value == '')
	{
	alert('You entered and end date with no start date for council file received.');
	startdate.focus();
	startdate.style.backgroundColor = 'yellow'; 
	enddate.style.backgroundColor = 'yellow';
	return false;		
	}
	
if (doDateCheck(startdate.value,enddate.value) > 0)
	{
	alert('The start date must be earlier than the end date for council file received.');
	startdate.focus();
	startdate.style.backgroundColor = 'yellow'; 
	enddate.style.backgroundColor = 'yellow';
	return false;
	}

var startdate = document.getElementById('DateExpStart') ;
var enddate = document.getElementById('DateExpEnd') ;

if (enddate.value != '' && startdate.value == '')
	{
	alert('You entered and end date with no start date for council file expiration.');
	startdate.focus();
	startdate.style.backgroundColor = 'yellow'; 
	enddate.style.backgroundColor = 'yellow';
	return false;		
	}

if (doDateCheck(startdate.value,enddate.value) > 0)
	{
	alert('The start date must be earlier than the end date for council file expiration.');
	startdate.focus();
	startdate.style.backgroundColor = 'yellow'; 
	enddate.style.backgroundColor = 'yellow';
	return false;
	}

var startdate = document.getElementById('RewPosStart') ;
var enddate = document.getElementById('RewPosEnd') ;

if (enddate.value != '' && startdate.value == '')
	{
	alert('You entered and end date with no start date for reward posted date.');
	startdate.focus();
	startdate.style.backgroundColor = 'yellow'; 
	enddate.style.backgroundColor = 'yellow';
	return false;		
	}

if (doDateCheck(startdate.value,enddate.value) > 0)
	{
	alert('The start date must be earlier than the end date for reward posted date.');
	startdate.focus();
	startdate.style.backgroundColor = 'yellow'; 
	enddate.style.backgroundColor = 'yellow';
	return false;
	}
	
var startdate = document.getElementById('RewExpStart') ;
var enddate = document.getElementById('RewExpEnd') ;

if (enddate.value != '' && startdate.value == '')
	{
	alert('You entered and end date with no start date for reward expiration date.');
	startdate.focus();
	startdate.style.backgroundColor = 'yellow'; 
	enddate.style.backgroundColor = 'yellow';
	return false;		
	}

if (doDateCheck(startdate.value,enddate.value) > 0)
	{
	alert('The start date must be earlier than the end date for reward expiration date.');
	startdate.focus();
	startdate.style.backgroundColor = 'yellow'; 
	enddate.style.backgroundColor = 'yellow';
	return false;
	}

var startdate = document.getElementById('ActDateStart') ;
var enddate = document.getElementById('ActDateEnd') ;

if (enddate.value != '' && startdate.value == '')
	{
	alert('You entered and end date with no start date for file history action date.');
	startdate.focus();
	startdate.style.backgroundColor = 'yellow'; 
	enddate.style.backgroundColor = 'yellow';
	return false;		
	}

if (doDateCheck(startdate.value,enddate.value) > 0)
	{
	alert('The start date must be earlier than the end date for file history action date.');
	startdate.focus();
	startdate.style.backgroundColor = 'yellow'; 
	enddate.style.backgroundColor = 'yellow';
	return false;
	}

var startdate = document.getElementById('SchDateStart') ;
var enddate = document.getElementById('SchDateEnd') ;

if (enddate.value != '' && startdate.value == '')
	{
	alert('You entered and end date with no start date for file history scheduled date.');
	startdate.focus();
	startdate.style.backgroundColor = 'yellow'; 
	enddate.style.backgroundColor = 'yellow';
	return false;		
	}

if (doDateCheck(startdate.value,enddate.value) > 0)
	{
	alert('The start date must be earlier than the end date for file history scheduled date.');
	startdate.focus();
	startdate.style.backgroundColor = 'yellow'; 
	enddate.style.backgroundColor = 'yellow';
	return false;
	}

// Other validations

if (_isInteger(document.getElementById('RewardAmount').value) == false)
	{
	alert('Please enter only numbers for reward amount. \nDo not use dollar signs, commas, decimals or other special characters.');
	document.getElementById('RewardAmount').focus();
	document.getElementById('RewardAmount').style.backgroundColor = 'yellow';
	return false;
	}

if (document.getElementById('RewardAmount').value != '')
	{
	if ((document.getElementById('RewardAmount').value < 1) || (document.getElementById('RewardAmount').value > 200000))
		{
		alert('Please enter an amount between 1 and 200000.');
		document.getElementById('RewardAmount').focus();
		document.getElementById('RewardAmount').style.backgroundColor = 'yellow';
		return false;
		}
	}
	
if (_isInteger(document.getElementById('RewardDuration').value) == false)
{
alert('Please enter only numbers for reward duration');
document.getElementById('RewardDuration').focus();
document.getElementById('RewardDuration').style.backgroundColor = 'yellow';
return false;
}


	var callback = function(text)
		{
		
		}
		
	var failure = function(text)
		{
				
		}
	
	ColdFusion.navigate('index.cfm?fa=vcfi.doSearch','ContentRight', callback, failure ,'POST','AdvancedSearch1');

}

function ToggleSection(sec) //expands selected section and collapses all others
{
	
	//Disable the search button for the other search
	if (sec == 'SimpleSearch')
	{
		document.getElementById('btnAdvanced').disabled = true ;
		document.getElementById('btnBasicSearch').disabled = false ;
		resetAdvanced(); // reset the advanced form
	}
	else if (sec = 'AdvSearch')
	{
		document.getElementById('btnBasicSearch').disabled = true ;
		document.getElementById('btnAdvanced').disabled = false ;
		resetBasic(); //clear the basic form
	}
	
	//First Collapse any div that is open
	if (document.getElementById('SimpleSearch').style.display == "block")
		{
		toggleSlide('SimpleSearch');	
		}
	if (document.getElementById('AdvSearch').style.display == "block")
		{
		toggleSlide('AdvSearch');	
		}
		
	// Then we expand the selected div
	toggleSlide(sec);

}

var currentvote = 1 ; //set the default global variable for the current vote
function scrollvotes(totvotes, method) // scrolls through cvvs votes
	{
		if (method == 'right') // if user is going to next vote
			{
			currentvote = currentvote + 1 ;	
			}
		else if (method == 'left') // if user is going to previous vote
			{
			currentvote = currentvote - 1 ;	
			}
			
		if (currentvote > totvotes) // if the current vote is greater than the total number of votes 
			{
				currentvote = 1 ; // start over at the 1st vote
			}
			
		if (currentvote < 1) // if the current vote is less than 1 
			{
				currentvote = totvotes ; // go to the last vote
			}
	
		for (i = 1; i <= totvotes; i++) // loop through all the vote divs
			{
				document.getElementById('vote_' + i).style.display = "none";
			}
		
		document.getElementById('vote_' + currentvote).style.display = "block"; // now show the current vote
		
	}

function SubscribeEmail() // submits and processes a users request to subscribe via email
{

if (document.getElementById('email').value == '')
		{
		alert('Please enter your email address');
		return false;
		}
		
var success = function(text){
	document.getElementById('EmailForm').style.display = "none" ;
	document.getElementById('EmailResult').style.display = "block" ;
	}
	
var failure = function(errorCode, errorMessage){
	var msg = 'An error occurred: ' + errorCode + '\nError Message: ' + errorMessage ;
	alert(msg);
	return false;
	}

	ColdFusion.navigate('index.cfm?fa=mcfi.act_SubscribeEmail', 'EmailResult', success, failure, 'POST', 'EmailSubcribefrm');
		
}

function SwitchMoverTab(tab) //switches between tabs for mover
	{
		if (tab == 'current')
		{
			document.getElementById('currentmover').style.display = "block";
			document.getElementById('formermover').style.display = "none";
			document.getElementById('MoverCurrentTab').className = "current";
			document.getElementById('MoverFormerTab').className = "";
		}
		else if (tab == 'former')
		{
			document.getElementById('currentmover').style.display = "none";
			document.getElementById('formermover').style.display = "block";
			document.getElementById('MoverFormerTab').className = "current";
			document.getElementById('MoverCurrentTab').className = "";
		}
	}
	
function SwitchSeconderTab(tab) //switches between tabs for seconder
	{
		if (tab == 'current')
		{
			document.getElementById('currentseconder').style.display = "block";
			document.getElementById('formerseconder').style.display = "none";
			document.getElementById('SeconderCurrentTab').className = "current";
			document.getElementById('SeconderFormerTab').className = "";
		}
		else if (tab == 'former')
		{
			document.getElementById('currentseconder').style.display = "none";
			document.getElementById('formerseconder').style.display = "block";
			document.getElementById('SeconderFormerTab').className = "current";
			document.getElementById('SeconderCurrentTab').className = "";
		}
	}	


function showhidereward() //shows or hides the reward section as needed
{
	if (document.getElementById('IsReward').checked)
		{
			document.getElementById('rewardsection').style.display = 'block';
		}
	else{
			document.getElementById('RewardAmount').value = '';
			document.getElementById('RewardDuration').value = '';
			document.getElementById('RewPosStart').value = '';
			document.getElementById('RewExpStart').value = '';
			document.getElementById('RewPosEnd').value = '';
			document.getElementById('RewExpEnd').value = '';
			document.getElementById('rewardsection').style.display = 'none';
		}
}
/************ END MODULE CFMS ***************/
/**************************************************/


/************ BEGIN MODULE HELP ***************/
/**************************************************/

function ToggleSectionHelp(sec) //expands selected section and collapses all others
{
	
	//First Collapse any div that is open
	if (document.getElementById('Help_CFMS').style.display == "block")
		{
		toggleSlide('Help_CFMS');	
		}
	if (document.getElementById('Help_Contract').style.display == "block")
		{
		toggleSlide('Help_Contract');	
		}
	if (document.getElementById('Help_Disclaimer').style.display == "block")
		{
		toggleSlide('Help_Disclaimer');	
		}
	// Then we expand the selected div
	toggleSlide(sec);

}

/************ END MODULE HELP ***************/
/**************************************************/


/************ BEGIN MODULE CONTRACTS ***************/
/**************************************************/

function resetContractsSearchForm(){ // reset the contracts search form

window.document.ContractSearchFrm.contractsummary.value = '';
window.document.ContractSearchFrm.contractnumber.value = '';
}

function DoContractSearch() // Performs a contract search
{
		
	if ((document.getElementById('contractnumber').value == '') && (document.getElementById('contractsummary').value == ''))
		{
		alert('You must enter a search criteria to perform a search.');
		return false;
		}
		
	var callback = function(text)
		{
		
		}
		
	var failure = function(text)
		{
				
		}
	
	ColdFusion.navigate('index.cfm?fa=vcon.doSearch','ContentRight', callback, failure ,'POST','ContractSearchFrm');
	
}




/************ BEGIN MODULE COMMON FUNCTIONS ***************/
/**************************************************/


function filterNum(str) { //utility function used in the Trim functions, removes $ and , from string
re = /^\$|,/g;
// remove "$" and ","
return str.replace(re, "");
}

function filterInvalidChars(str) //utility function used in the Trim functions, removes | from string
{
re = /|"/g;

return str.replace(re, "");
}

function filterampersands(str) //utility function used in the Trim functions, removes & from string
{
re = /&/g;
return str.replace(re, "and");
	
}

function Trim(TRIM_VALUE, obj){ //utility function used to remove invalid characters and whitespace before/after text string
		if(TRIM_VALUE.length < 1){
		obj.value = "";
		}
		
		TRIM_VALUE = filterInvalidChars(TRIM_VALUE); //removes restricted characters
		//TRIM_VALUE = filterampersands(TRIM_VALUE); //replaces ampersand with 'and'
		
		TRIM_VALUE = RTrim(TRIM_VALUE);
		TRIM_VALUE = LTrim(TRIM_VALUE);
		
		if(TRIM_VALUE==""){
		obj.value = "";
		}
		else{
		
		obj.value = TRIM_VALUE;
		return TRIM_VALUE;
		}
		} //End Function

function RTrim(VALUE){ //used in Trim function
		var w_space = String.fromCharCode(32);
		var v_length = VALUE.length;
		var strTemp = "";
		if(v_length < 0){
		return"";
		}
		var iTemp = v_length -1;
		
		while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
		strTemp = VALUE.substring(0,iTemp +1);
		break;
		}
		iTemp = iTemp-1;
		
		} //End While
		return strTemp;
		
		} //End Function

function LTrim(VALUE){ //used in Trim function
		var w_space = String.fromCharCode(32);
		if(v_length < 1){
		return"";
		}
		var v_length = VALUE.length;
		var strTemp = "";
		
		var iTemp = 0;
		
		while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
		strTemp = VALUE.substring(iTemp,v_length);
		break;
		}
		iTemp = iTemp + 1;
		} //End While
		return strTemp;
		} //End Function


function doDateCheck(from, to) {
if (Date.parse(from) <= Date.parse(to))
	{
	return -1;
	}
else
	{
	if (from == "" || to == "")
		{
		return 0;
		}
	else
		{ 
		return 1;
		}
   }
}
	
function checkDate(dateStr)
{
if (dateStr == '')
	{
	return true;	
	}
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat);
if (matchArray == null) {
	alert("The date does not appear to be valid. Please enter the date as either mm/dd/yyyy or mm-dd-yyyy.");
	return false;
}
month = matchArray[1]; // p@rse date into variables
day = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (year < 1900 || year > 2070) { // check year range
alert("Year must be between 1900 and 2070.");
return false;
}

if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn`t have 31 days!")
return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
alert("February " + year + " doesn`t have " + day + " days!");
return false;
}
}
}

function _isInteger(val) {
	var digits="1234567890";
	for (var i=0; i < val.length; i++) {
		if (digits.indexOf(val.charAt(i))==-1) { return false; }
		}
	return true;
	}

// script for sliding divs
var timerlen = 5;
var slideAniLen = 250;

var timerID = new Array();
var startTime = new Array();
var obj = new Array();
var endHeight = new Array();
var moving = new Array();
var dir = new Array();

function slidedown(objname){
        if(moving[objname])
                return;

        if(document.getElementById(objname).style.display != "none")
                return; // cannot slide down something that is already visible

        moving[objname] = true;
        dir[objname] = "down";
        startslide(objname);
}

function slideup(objname){
        if(moving[objname])
                return;

        if(document.getElementById(objname).style.display == "none")
                return; // cannot slide up something that is already hidden

        moving[objname] = true;
        dir[objname] = "up";
        startslide(objname);
}

function startslide(objname){
        obj[objname] = document.getElementById(objname);

        endHeight[objname] = parseInt(obj[objname].style.height);
        startTime[objname] = (new Date()).getTime();

        if(dir[objname] == "down"){
                obj[objname].style.height = "1px";
        }

        obj[objname].style.display = "block";

        timerID[objname] = setInterval('slidetick(\'' + objname + '\');',timerlen);
}

function slidetick(objname){
        var elapsed = (new Date()).getTime() - startTime[objname];

        if (elapsed > slideAniLen)
                endSlide(objname)
        else {
                var d =Math.round(elapsed / slideAniLen * endHeight[objname]);
                if(dir[objname] == "up")
                        d = endHeight[objname] - d;

                obj[objname].style.height = d + "px";
        }

        return;
}

function endSlide(objname){
        clearInterval(timerID[objname]);

        if(dir[objname] == "up")
                obj[objname].style.display = "none";

        obj[objname].style.height = endHeight[objname] + "px";

        delete(moving[objname]);
        delete(timerID[objname]);
        delete(startTime[objname]);
        delete(endHeight[objname]);
        delete(obj[objname]);
        delete(dir[objname]);

        return;
}

function toggleSlide(objname){
  if(document.getElementById(objname).style.display == "none"){
    // div is hidden, so let's slide down
   slidedown(objname);
  }else{
    // div is not hidden, so slide up
   slideup(objname);
  }
 //alert ('testing');
}

/************ END MODULE COMMON FUNCTIONS ***************/
/**************************************************/