
//***********************************************	
//***************** FUNCTIONS *******************
//***********************************************

//*******************START UP FUNCTIONS *********

// start up
function startUp() {
	setTool(document.frmMain.tool.value);
	showPanel(document.frmMain.panel.value);
	setZoomBoxSettings();
}


// *********** SWAP IMAGE FUNCTIONS *************

//swap image
function swapImage(sImg, sSrc){
	theImg = eval("document."+sImg);
	theImg.src = sSrc;
}


//*******************TOOL FUNCTIONS *************

//set tool
function setTool(sTool){
	sOldTool = document.frmMain.tool.value;
	if((sOldTool == "EDIT_POINT") || (sOldTool == "EDIT_LINE") || (sOldTool == "EDIT_POLY")){
		resetEditTool();
	}
	setToolImage(sTool);
	document.frmMain.tool.value = sTool;
}


//set tool image
function setToolImage(sTool){
	sOldTool = document.frmMain.tool.value;
	theOldImg = eval("document.B_"+sOldTool);
	if(theOldImg != null){
		theOldImg.src = "images/b_"+sOldTool+"_off.gif";
	}
	theNewImg = eval("document.B_"+sTool);
	if(theNewImg != null){
		theNewImg.src = "images/b_"+sTool+"_on.gif";
	}
}

//set cmd
function setCmd(sCmd){
	document.frmMain.cmd.value = sCmd;
	document.frmMain.submit();
	showLayer("Splash");
}

//*******************PANEL FUNCTIONS *********
//hide panels
function hidePanel(){
	sOldPanel = document.frmMain.panel.value;
	if(sOldPanel != "EDIT_PROJECT"){
		theOldImg = eval("document.T_"+sOldPanel);
		theOldImg.src = "images/t_"+sOldPanel+"_off.gif";
	}
	hideLayer(sOldPanel);
}

//show panel
function showPanel(sPanel){
	hidePanel();
	if(sPanel != "EDIT_PROJECT"){
		theImg = eval("document.T_"+sPanel);
		theImg.src = "images/t_"+sPanel+"_on.gif";
	}
	showLayer(sPanel);
	document.frmMain.panel.value = sPanel;
	thePanel = document.getElementById("PanelBG");
	thePanel.scrollTop = 0;	
}


//******************* SEARCH FUNCTIONS *********

//search location
function searchLocation(sMode){

	theForm = document.frmMain;
	
	switch(sMode){
	
		case "ADDRESS":
			
			stnum = theForm.txtNumAddr.value;
			stdir = theForm.ddlDirAddr.options[theForm.ddlDirAddr.options.selectedIndex].value;
			fdate = theForm.ddladdrfrom.options[theForm.ddladdrfrom.options.selectedIndex].value;
			tdate = theForm.ddladdrto.options[theForm.ddladdrto.options.selectedIndex].value;
			stname = theForm.txtNameAddr.value;
			sttype = theForm.ddlTypeAddr.options[theForm.ddlTypeAddr.options.selectedIndex].value;
			if((stnum == "") || (stname == "")){
				alert("Please enter a valid street address.");
			} else {
			if((fdate == "") || (tdate == "")){
			alert("Please choose a date range for your search.");
			} else {
				setCmd("SEARCHADDRESS");
			}
			}
			break;
			
		case "NEIGHBORHOOD":
			fdate = theForm.ddlneighfrom.options[theForm.ddlneighfrom.options.selectedIndex].value;
			tdate = theForm.ddlneighto.options[theForm.ddlneighto.options.selectedIndex].value;
			nbhd = theForm.ddlNeighborhoodLoc.options[theForm.ddlNeighborhoodLoc.options.selectedIndex].value;
			if(nbhd == "") {
				alert("Please select a neighborhood.");
			}else{
			if((fdate == "") || (tdate == "")){
			alert("Please choose a date range for your search.");
			} else {
				setCmd("SEARCHNEIGHBORHOOD");
			}
			}	
			break;
	}
}
//search project
function searchCase(sMode){

	theForm = document.frmMain;
	
	switch(sMode){
	
		case "idsearch":
			id = theForm.txtCaseID.value;
			if(id == ""){
				alert("Please enter a valid Case ID.");
			} else {
				setCmd("SEARCHCASEID");
			}
			break;
		}

}
// clear search
function clearSearch(){
	theForm = document.frmMain;
	theForm.txtNumAddr.value = "";
	theForm.ddlDirAddr.options.selectedIndex = -1;
	theForm.txtNameAddr.value = "";
	theForm.ddlTypeAddr.options.selectedIndex = -1;
	theForm.ddlDirStreet.options.selectedIndex = -1;
	theForm.txtNameStreet.value = "";
	theForm.ddlTypeStreet.options.selectedIndex = -1;
	theForm.ddlDirInt.options.selectedIndex = -1;
	theForm.txtNameInt.value = "";
	theForm.ddlTypeInt.options.selectedIndex = -1;
	theForm.ddlNeighborhoodLoc.options.selectedIndex = -1;
	theForm.ddlCommissionDistrictLoc.options.selectedIndex = -1;
	theForm.chkTrafficLoc.checked = false;
		
	theForm.txtProjectName.value = "";
	theForm.ddlProjectType.options.selectedIndex = -1;
	theForm.ddlProgramCategory.options.selectedIndex = -1;
	theForm.ddlProjectStatus.options.selectedIndex = -1;
	theForm.ddlNeighborhoodPrj.options.selectedIndex = -1;
	theForm.ddlCommissionDistrictPrj.options.selectedIndex = -1;
	theForm.ddlStartMM.options.selectedIndex = -1;
	theForm.ddlStartYYYY.options.selectedIndex = -1;
	theForm.ddlEndMM.options.selectedIndex = -1;
	theForm.ddlEndYYYY.options.selectedIndex = -1;
	theForm.chkTrafficPrj.checked = false;
	
	if(theForm.chkMyProjectsLoc != null){
		theForm.chkMyProjectsLoc.checked = false;
		theForm.chkOrphanProjectsLoc.checked = false;
		theForm.chkMyProjectsPrj.checked = false;
		theForm.chkOrphanProjectsPrj.checked = false;
	}
	
	
	setCmd("CLEAR");
	
}

//zoom all
function zoomAll(){
	var proceed = confirm("Zooming to all selected projects may take you to a large map extent where the projects are not visible. Do you wish to proceed?");
	if(proceed){
		setCmd("ZOOMALL");
	}
}

//Export To Excel
function exportexcel(){
	var proceed = confirm("Are you sure you want to export your data to and excel workbook?");
	if(proceed){
		setCmd("EXPORTEXCEL");
	}
}

//LAUNCH TABLE VIEW
function tableview(){
	setCmd("TABLEVIEW");
}

//show more
function showMore(sNum){
	theForm = document.frmMain;
	theForm.startnum.value = sNum;
	setCmd("SEARCHSHOWMORE");
}

//****************** KEY DOWN FUNCTIONS ********

//search key down
function searchKeyDown(event, searchType){
	if(event.keyCode == 13){
		window.event.keyCode = 0;
		if(searchType == "ADDRESS"){
			searchLocation("ADDRESS");
		}
		if(searchType == "STREET"){
			searchLocation("STREET");
		}
		if(searchType == "PROJECTNAME"){
			searchProject("PROJECTNAME");
		}
	}
}

//edit key down
function editKeyDown(event){
	if(event.keyCode == 13){
		window.event.keyCode = 0;
	}
}


//layout key down
function layoutKeyDown(event){
	if(event.keyCode == 13){
		window.event.keyCode = 0;
		setCmd("LAYOUT");
	}
}

//****************** EDIT FUNCTIONS ************

//edit project
function editProject(){
	if(document.frmMain.pid.value != ""){
		setCmd("EDITPROJECT");
	}
}

//add project
function addProject(){
	setCmd("ADDPROJECT");
}

//update project
function updateProject(){
	var ok = validateEditForm();
	if(ok == true){
		setCmd("UPDATEPROJECT");
	}
}

//cancel project
function cancelProject(){
	if(document.frmMain.txtEditProjectID.value == ""){
		showPanel("SEARCH_LOCATION");
	}else{
		showPanel("RESULTS");
	}
}

//delete project
function deleteProject(){
	var ok = false;
	ok = confirm("Are you sure you want to delete all project data and project graphics for this project?");
	if(ok == true){
		if(document.frmMain.txtEditProjectID.value == ""){
			cancelProject();
		}else{
			setCmd("DELETEPROJECT");
		}
	}
}

//delete all project graphics
function deleteAllProjectGraphics(){
	var ok = false;
	ok = confirm("All graphics for this project will be permanently deleted. Are you sure you want to delete all graphics associated with this project?");
	if(ok == true){
		if(document.frmMain.txtEditProjectID.value == ""){
			cancelProject();
		}else{
			setCmd("DELETEALLPROJECTGRAPHICS");
		}
	}
}

//accept graphics
function acceptGraphics(){
	switch(document.frmMain.tool.value){
	
		case "EDIT_POINT":
			if(clickCount > 0){
				setCmd("ACCEPTPROJECTGRAPHICS");
			}else{
				alert("Please add at least one ICP point to the map before using the Accept Graphics button.");
			}
			break;
		
		case "EDIT_LINE":
			if(clickCount > 1){
				setCmd("ACCEPTPROJECTGRAPHICS");
			}else{
				alert("Please add at least two points to create an ICP line before using the Accept Graphics button.");
			}
			break;
		
		case "EDIT_POLY":
			if(clickCount > 2){
				setCmd("ACCEPTPROJECTGRAPHICS");
			}else{
				alert("Please add at least three points to create an ICP polygon before using the Accept Graphics button.");
			}
			break;
	}
}

//****************** RESULT FUNCTIONS ************

//toggle record
function toggleRecord(sID){
	theImg = eval("document.IMG_REC_"+sID);
	theDiv = document.getElementById("REC_"+sID);
	//closed rec
	if(theImg.src.indexOf("i_plus.gif") > 0){
		theImg.src = "images/i_minus.gif";
		theDiv.className = "RecordOpen";
	//open rec
	}else{
		theImg.src = "images/i_plus.gif";
		theDiv.className = "RecordClosed";
	}

}


//****************** LAYOUT FUNCTIONS ************

// show layout
function showLayout(sLink){
	//alert(sLink);
	layoutWin = window.open(sLink, "Layout", "width=500,height=400,toolbar=0,menubar=1,scrollbars=1,resizable=1,left=20,top=0");
}

//****************** UPLOAD FUNCTIONS ************

//upload data
function uploadData(){
	theForm = document.frmMain;
	if(theForm.uploadFile.value != ""){
		if(confirm("Uploading data can be a time consuming process.  Please make sure the data file is in the correct format.")){
			setCmd("UPLOADDATA");
		}else{
			theForm.uploadFile.value = "";
		}
		
	}else{
		alert("No data file selected.");
	}
}


//****************** VAILDATION FUNCTIONS ******

// validate phone
function validatePhone(controlname){
	txtbox = document.getElementById(controlname);
	if(txtbox != null){
		sCheck = "0123456789-";
		sVal = txtbox.value;
		sValOut = sVal;
		for(i=0;i<sVal.length;i++){
			sChar = sVal.charAt(i);
			if(sCheck.indexOf(sChar) < 0){
				sValOut = sValOut.replace(sChar, "");
			}
		}
		if(sValOut.length >= 4){
			if(sValOut.charAt(3) != "-"){
				sValOut = sValOut.substr(0,3) + "-" + sValOut.substr(3);
			}
		}
		if(sValOut.length >= 8){
			if(sValOut.charAt(7) != "-"){
				sValOut = sValOut.substr(0,7) + "-" + sValOut.substr(7);
			}
		}
		txtbox.value = sValOut;
	}
}

// validate numeric
function validateNumeric(controlname){
	txtbox = document.getElementById(controlname);
	if(txtbox != null){
		sCheck = "0123456789";
		sVal = txtbox.value;
		sValOut = sVal;
		for(i=0;i<sVal.length;i++){
			sChar = sVal.charAt(i);
			if(sCheck.indexOf(sChar) < 0){
				sValOut = sValOut.replace(sChar, "");
			}
		}
		txtbox.value = sValOut;
	}
}

// validate date
function validateDate(controlname){
	txtbox = document.getElementById(controlname);
	if(txtbox != null){
		sCheck = "0123456789/";
		sVal = txtbox.value;
		sValOut = sVal;
		for(i=0;i<sVal.length;i++){
			sChar = sVal.charAt(i);
			if(sCheck.indexOf(sChar) < 0){
				sValOut = sValOut.replace(sChar, "");
			}
		}
		txtbox.value = sValOut;
	}
}

// validate email
function validateEmail(controlname){
	txtbox = document.getElementById(controlname);
	if(txtbox != null){
		sCheck = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@-_.";
		sVal = txtbox.value;
		sValOut = sVal;
		for(i=0;i<sVal.length;i++){
			sChar = sVal.charAt(i);
			if(sCheck.indexOf(sChar) < 0){
				sValOut = sValOut.replace(sChar, "");
			}
		}
		txtbox.value = sValOut;
	}
}


//validate edit form
function validateEditForm(){
	var ok = true;
	theForm = document.frmMain;
	
	name = theForm.txtEditProjectName.value;
	if(name == ""){
		alert("Please enter a project name.");
		ok = false;
	}
	
	sdate = theForm.txtEditStartDate.value;
	if(validateDateEntry(sdate, "project start date") == false){
		ok = false;
	}
	
	edate = theForm.txtEditEndDate.value;
	if(validateDateEntry(edate, "project end date") == false){
		ok = false;
	}
	
	email = theForm.txtEditCityProjectManagerEmail.value;
	if(validateEmailAddress(email) == false){
		ok = false;
	}
		
	return ok;
}


//validate date entry
function validateDateEntry(thedate, thefield){
	var ok = true;
	if(thedate.length >= 6){
		dateArray = thedate.split("/")
		if(dateArray.length != 3){
			ok = false;
		}else{
			m = dateArray[0];
			d = dateArray[1];
			y = dateArray[2];
			if(m.length < 2){
				m = "0" + m;
			}
			if(d.length < 2){
				d = "0" + d;
			}
			if(y.length < 4){
				y = "20" + y;
			}
			month = parseInt(m);
			day = parseInt(d);
			year = parseInt(y);
			var leap = 0;
			if((day < 1) || (month < 1) || (year < 1)){
				ok = false;
			}
			if(month > 12){
				ok = false;
			}
			if((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)){
				leap = 1;
			}
			if((month == 2) && (leap == 1) && (day > 29)){
				ok = false;
			}
			if((month == 2) && (leap != 1) && (day > 28)){
				ok = false;
			}
			if((day > 31) && ((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))){
				ok = false;
			}
			if ((day > 30) && ((month == 4) || (month == 6) || (month == 9) || (month == 11))){
				ok = false;
			}
		}
	}else{
		ok = false;
	}
	if(ok == false){
		alert("Please enter a valid " + thefield + ".");
	}
	return ok;
}


//validate email address
function validateEmailAddress(email){
	var ok = true;
	if(email.length > 0){
		if(email.indexOf("@") == -1){ 
			ok = false; 
		}
		if(ok){
			emailArray = email.split("@");
			if(emailArray.length != 2){
				ok = false;
			}else{
				if(emailArray[1].indexOf(".") == -1){ 
					ok = false; 
				}else{
					domainArray = emailArray[1].split(".");
					if(domainArray.length < 2){
						ok = false;
					}else{
						if((domainArray[0].length == 0) || (domainArray[1].length == 0)){
							ok = false;
						}
					}
				}
			}
		}
		if(ok == false){
			alert("Please enter a valid City Project Manager Email Address.");
		}
	}
	return ok;
}

//****************** HELP FUNCTIONS ************

// show help
function showHelp(sTopic){
	helpURL = "help.htm"+"#"+sTopic;
	if (typeof helpWin == 'undefined'){
		helpWin = window.open(helpURL, "Help", "width=500,height=400,toolbar=0,menubar=0,scrollbars=1,resizable=1,left=300,top=0");
		helpWin.opener = self;
	} else {
		if (helpWin.closed){
			helpWin = window.open(helpURL, "Help", "width=500,height=400,toolbar=0,menubar=0,scrollbars=1,resizable=1,left=300,top=0");
			helpWin.opener = self;
		} else {
			helpWin = window.open(helpURL, "Help", "width=500,height=400,toolbar=0,menubar=0,scrollbars=1,resizable=1,left=300,top=0");			
			helpWin.focus();
		}
	}
}






