/*
File name: functions.js
Author: J.Ludden - the Hill Group
Created: 11 April, 2009
Updated: 11 April, 2009
	1. 
Purpose: javascript functions for form navigation and validation
Note: 

*/
function exitSubmission() {
	if (confirm('Are you sure you want to exit this submission? If you leave now, you will not be able to access the content that you have entered. \n\nAre you sure you want to leave?')) {
		document.location = 'index.cfm?module=home';
	}
}

/* javascript functions for submission navigation */
function checkForm1(){
	var  frm = document.mainForm;
	var  name1 = frm.name1.value;
    var  slocation1 = frm.slocation1.value;
	var  addressa1 = frm.addressa1.value;
	//var  addressa2 = frm.addressa2.value;
	var  city1 = frm.city1.value;
	var  state1 = frm.state1.value;
	var  zip1 = frm.zip1.value;
	var  country1 = frm.country1.value;
	var  email1 = frm.email1.value;
	var  phone1 = frm.phone1.value;
	var  contactType = frm.contactType.value;
	var strFields = '';
	var strMessage = '';
	
	if (name1 == "") {
		strFields = strFields + 'Name\n';
		frm.name1.focus();
	}
	if (slocation1 == "") {
		strFields = strFields + 'Site or location name\n';
		frm.slocation1.focus();
	}
	if (addressa1 == "") {
		strFields = strFields + 'Address\n';
		frm.addressa1.focus();
	}
	if (city1 == "") {
		strFields = strFields + 'City\n';
		frm.city1.focus();
	}
	if (state1 == "") {
		strFields = strFields + 'State\n';
		frm.state1.focus();
	}
	if (zip1 == "") {
		strFields = strFields + 'Zip\n';
		frm.zip1.focus();
	}
	//if (country1 == "") {
	//	strFields = strFields + 'Country\n';
	//	frm.country1.focus();
	//}
	if (email1 == "") {
		strFields = strFields + 'Email\n';
		frm.email1.focus();
	}
	if (phone1 == "") {
		strFields = strFields + 'Phone\n';
		frm.phone1.focus();
	}
	
	if (strFields != '') {
		strMessage = 'Required fields are empty on the form. The following fields are required:\n\n' + strFields + '\nPlease enter the information to continue.';
	} else if((frm.contactType[0].checked == false ) && (frm.contactType[1].checked == false )) {
		strMessage = '2. The information is required';
		frm.contactType[0].focus();
	} else if (email1.length < 6 || email1.indexOf("@") < 1 || email1.lastIndexOf(".") < email1.indexOf("@") || email1.indexOf("@") != email1.lastIndexOf("@")) {
		strMessage = 'Invalid e-mail address entered (' + email1 + '). \n\nPlease enter a valid email address to continue.';
		frm.email1.focus();
	} else {
		return true;
	}    
	if (strMessage != '') {
		alert(strMessage);
		return false;
	}
}

function checkForm2_i(){
	
	var  frm = document.mainForm;
	var  name2 = frm.name2.value;
    var  email2 = frm.email2.value;
	var  phone2 = frm.phone2.value;
	//var  contact1 = frm.contact1.value;
	var strFields = '';
	var strMessage = '';
	
	if (name2 == "") {
		strFields = strFields + 'Name\n';
		frm.name2.focus();
	}
	if (email2 == "") {
		strFields = strFields + 'Email\n';
		frm.email2.focus();
	}
	if (phone2 == "") {
		strFields = strFields + 'Phone\n';
		frm.phone2.focus();
	}
	
	if (strFields != '') {
		strMessage = 'Required fields are empty on the form. The following fields are required:\n\n' + strFields + '\nPlease enter the information to continue.';
	} else if (email2.length < 6 || email2.indexOf("@") < 1 || email2.lastIndexOf(".") < email2.indexOf("@") ||           email2.indexOf("@") != email2.lastIndexOf("@")) {
		strMessage = 'Invalid e-mail address entered (" + email2 + ").';
		frm.email2.focus();
	} else {
		return true;
	}
	if (strMessage != '') {
		alert(strMessage);
		return false;
	}
}
//-----
function checkForm2_e(){

	var  frm = document.mainForm;
	var  bppple = frm.bppple.value;
	var strFields = '';
	var strMessage = '';
	
	if((frm.bppple[0].checked == false ) && (frm.bppple[1].checked == false) && (frm.bppple[2].checked == false) && (frm.bppple[3].checked == false) && (frm.bppple[4].checked == false )) {
		strFields = '3. Please select the type of program you are submitting.';
		frm.bppple[0].focus();
	}
	if (strFields != '') {
		strMessage = 'Required fields are empty on the form. The following fields are required:\n\n' + strFields + '\nPlease enter the information to continue.';
	} else {
		return true;
	}
	if (strMessage != '') {
		alert(strMessage);
		return false;
	}

}

function goback2(){
	document.mainForm.action ="index.cfm?module=formpg1";
	document.mainForm.submit();
}

function checkForm3(){
	//alert('hello');
	var  frm = document.mainForm;
	var  ihssvc  = frm.ihssvc.value;
    var  title   = frm.title.value;
	var  agegrp1 = frm.agegrp1.value;
	var  agegrp2 = frm.agegrp2.value;
	var  agegrp3 = frm.agegrp3.value;
	var  agegrp4 = frm.agegrp4.value;
	var  agegrp5 = frm.agegrp5.value;
	var  agegrp6 = frm.agegrp6.value;
	var sitetyp1  = frm.sitetyp1.value;
	var sitetyp2  = frm.sitetyp2.value;
	var sitetyp3  = frm.sitetyp3.value;
	var sitetyp4  = frm.sitetyp4.value;
	var sitetyp5  = frm.sitetyp5.value;
	var sitetyp6  = frm.sitetyp6.value;
	var sitetyp7  = frm.sitetyp7.value;
	var  descrip = frm.descrip.value;
	var  link1 = frm.link1.value;
	var  cost = frm.cost.value;
	
	var strFields = '';
	var strMessage = '';
	
	var blnIndicaFound = false;
	var blnKeywordFound = false;
	
	if(ihssvc == "") {
		if (frm.contactType.value == 'N') {
			strFields = strFields + '4. Service Area\n';
		} else {
			strFields = strFields + '5. Service Area\n';
		}
		frm.ihssvc.focus();
	}
	if(title == "") {
	   if (frm.contactType.value == 'N') {
			strFields = strFields + '5. Program\/Information Title\n';
		} else {
			strFields = strFields + '6. Program\/Information Title\n';
		}
		frm.title.focus();
	}
	
	if((frm.agegrp1.checked == false ) 
		   && (frm.agegrp2.checked == false) 
		   && (frm.agegrp3.checked == false)  
		   && (frm.agegrp4.checked == false) 
           && (frm.agegrp5.checked == false ) 
           && (frm.agegrp6.checked == false )) {
	   	if (frm.contactType.value == 'N') {
			strFields = strFields + '6. Target Population\n';
		} else {
			strFields = strFields + '7. Target Population\n';
		}
		frm.agegrp1.focus();
	}
	
	if((frm.sitetyp1.checked == false ) 
		   && (frm.sitetyp2.checked == false) 
		   && (frm.sitetyp3.checked == false)  
		   && (frm.sitetyp4.checked == false) 
           && (frm.sitetyp5.checked == false ) 
           && (frm.sitetyp6.checked == false )
		   && (frm.sitetyp7.checked == false )) {
		if (frm.contactType.value == 'N') {
			strFields = strFields + '7. Type of Location\n';
		} else {
			strFields = strFields + '8. Type of Location\n';
		}
		frm.sitetyp1.focus();
	}
	
	for (var i=0; i < frm.indica.length; i++) {
		if (frm.indica[i].checked) {
			blnIndicaFound = true;
			break;
		}
	}
	if (!blnIndicaFound) {
		if (frm.contactType.value == 'N') {
			strFields = strFields + '8. Health Indicators\n';
		} else {
			strFields = strFields + '9. Health Indicators\n';
		}
		frm.indica[0].focus();
	}
/*
	if (descrip == "") {
		if (frm.contactType.value == 'N') {
			strFields = strFields + '9. Description\n';
		} else {
			strFields = strFields + '10. Description\n';
		}
		frm.descrip.focus();
	}	
*/	
	for (var j=0; j < frm.keywrd.length; j++) {
		if (frm.keywrd[j].checked) {
			blnKeywordFound = true;
			break;
		}
	}
	if (!blnKeywordFound) {
		if (frm.contactType.value == 'N') {
			strFields = strFields + '11. Keywords\n';
		} else {
			strFields = strFields + '12. Keywords\n';
		}
		frm.keywrd[0].focus();
	}
/*	
	if ((strMessage == '') && (frm.evaluation_summary.value == '')) {
		strMessage = '13. The information is required\n ';
		frm.evaluation_summary.focus();
	}
*/
	if ((cost == '') && (frm.cost_type[frm.cost_type.selectedIndex].value != 'N/A')) {
		if ((frm.contactType.value == 'N') && (frm.bppple.value != 'EB')) {
			strFields = strFields + '12. Cost\n';
		} else if ((frm.contactType.value == 'Y') && (frm.bppple.value != 'EB')) {
			strFields = strFields + '13. Cost\n';
		} else if ((frm.contactType.value == 'N') && (frm.bppple.value == 'EB')) {
			strFields = strFields + '15. Cost\n';
		} else if ((frm.contactType.value == 'Y') && (frm.bppple.value == 'EB')) {
			strFields = strFields + '16. Cost\n';
		} else {
			strFields = strFields + 'Cost\n';
		}
		frm.cost.focus();
	}
	if (strFields != '') {
		strMessage = 'Required fields are empty on the form. The following fields are required:\n\n' + strFields + '\nPlease enter the information to continue.';
	} else {
		return true;
	}
	if (strMessage != '') {
		alert(strMessage);
		return false;
	}
}

function goback3(){
	document.mainForm.action ="index.cfm?module=formpg2";
	document.mainForm.submit();
}
function goback4(){
	document.mainForm.action ="index.cfm?module=formpg3";
	document.mainForm.submit();
}
function checkForm4(){
   document.mainForm.submit();
}
function approverCancel() {
	if (confirm('WARNING: Any changes will be lost.\n\nAre you sure you want to continue?')) {
		document.location='index.cfm?module=approver';
	}
}
function adminCancel() {
	if (confirm('WARNING: Any changes will be lost.\n\nAre you sure you want to continue?')) {
		document.location='index.cfm?module=admin';
	}
}
