var locationUpdater;
var ageGroupUpdater;
var groupTypeUpdater;

function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	
	return true;

}

function initPage() {
	locationUpdater = new locationUpdater();
	ageGroupUpdater = new ageGroupUpdater();
	groupTypeUpdater = new groupTypeUpdater();
	assimilatorUpdater = new assimilatorUpdater();
	ajaxEngine.registerRequest( 'getLocations', '/assimilation/getLocation.php' );
	ajaxEngine.registerRequest( 'getAgeGroups', '/assimilation/getAgeGroups.php' );
	ajaxEngine.registerRequest( 'getGroupTypes', '/assimilation/getGroupTypes.php' );
	ajaxEngine.registerRequest( 'getAssimilator', '/assimilation/getAssimilator.php' );
	ajaxEngine.registerAjaxObject( 'locationUpdater', locationUpdater );
	ajaxEngine.registerAjaxObject( 'ageGroupUpdater', ageGroupUpdater );
	ajaxEngine.registerAjaxObject( 'groupTypeUpdater', groupTypeUpdater );
	ajaxEngine.registerAjaxObject( 'assimilatorUpdater', assimilatorUpdater );
	locationUpdater.reset();
	ageGroupUpdater.reset();
	groupTypeUpdater.reset();
	assimilatorUpdater.reset();
	
	document.form1["fhscol_FirstName"].value = "";
	document.form1["fhscol_SpouseName"].value = "";
	document.form1["fhscol_srchgrouptype"].value = "";
	document.form1["fhscol_GroupType"].value = "";
	document.form1["HusbandName"].value = "";
	document.form1["WifeName"].value = "";
	document.form1["SingleName"].value = "";
}

function updateCampus(selectBox) {
   hideElement("bb_map");
   if ( selectBox.value == "" ){
      locationUpdater.reset();
      ageGroupUpdater.reset();
   }
   else {
      ajaxEngine.sendRequest( 'getLocations',
                              "campusId=" + selectBox.value );
      setTimeout("ajaxEngine.sendRequest( 'getAgeGroups','campusId=" + selectBox.value + "' )",300);
   }
   
   if (selectBox.value == "3")
   		showElement("bb_map");
}

function updateGroupTypes(campusId, gender, maritalstatus) {
   if ( campusId == "" || gender == "" || maritalstatus == "" ){
	  groupTypeUpdater.reset();
   }
   else {
	  setTimeout("ajaxEngine.sendRequest( 'getGroupTypes','campusId=" + campusId + "','gender=" + gender + "','maritalstatus=" + maritalstatus + "')",400);
	  /*ajaxEngine.sendRequest( 'getGroupTypes',
                              "campusId=" + campusId,
                              "gender=" + gender,
                              "maritalstatus=" + maritalstatus );*/
	  
	  
   }
}

function getAssimilator(campusId, groupType, gender, region, ageGroup, lastName) {
   if ( campusId == "" || groupType == "" || gender == "" || region == "" || ageGroup == "" || lastName == "" ){
	  assimilatorUpdater.reset();
   }
   else {
	  //alert("Campus: " + campusId + "\nGroupType: " + groupType + "\nGender: " + gender + "\nRegion: " + region + "\nAgeGroup: " + ageGroup);
      
	  setTimeout("ajaxEngine.sendRequest( 'getAssimilator','campusId=" + campusId + "','groupType=" + groupType + "','gender=" + gender + "','region=" + region + "','ageGroup=" + ageGroup + "','lastName=" + lastName + "')",900 );
	  /*ajaxEngine.sendRequest( 'getAssimilator',
                              "campusId=" + campusId,
							  "groupType=" + groupType,
                              "gender=" + gender,
							  "region=" + region,
                              "ageGroup=" + ageGroup );*/
   }
}

function addGroupType(id, name, description, selected) {
	
  	var d = document.getElementById('grouptypetest');
	var lastRow = d.rows.length;
	var newRow = d.insertRow(lastRow);
	newRow.setAttribute('id','gt' + id);
	
	var newCell = newRow.insertCell(0);
	newCell.setAttribute('align','right');
	newCell.setAttribute('width','4%');
	newCell.setAttribute('valign','top');
	var clickCode = 'updateGroupType("' + escape(name) + '","' + id + '");';
	if (selected == 1) {
		newCell.innerHTML = "<input id='gtRadio' name='gtRadio' type='radio' value='" + id + "' onclick='" + clickCode + "' checked>";
	} else {
		newCell.innerHTML = "<input id='gtRadio' name='gtRadio' type='radio' value='" + id + "' onclick='" + clickCode + "'>";
	}
	//alert("<input id='GroupType' name='GroupType' type='radio' value='" + id + "' onclick='" + clickCode + "'>");
	
	
	newCell = newRow.insertCell(1);
	newCell.setAttribute('width','10%');
	newCell.setAttribute('valign','top');
	newCell.setAttribute('nowrap','true');
	newCell.innerHTML = "<strong>" + name + "</strong> - ";
	
    newCell = newRow.insertCell(2);
	newCell.setAttribute('valign','top');
	newCell.innerHTML = description;
}

function removeGroupType(id) {
	var d = document.getElementById('grouptypetest');
	if (d.rows.length > 1) {
		for ( var i = 0 ; i < d.rows.length ; i++ ) {
			if (d.rows[i].id == "gt" + id)
				d.deleteRow(i);
		}
	} else {
		d.deleteRow(0);
	}
}

function updateGroupType(name,id) {
	document.form1["fhscol_srchgrouptype"].value = unescape(name);
	document.form1["fhscol_GroupType"].value = id;
	setFields();
	//alert(name);
	//alert(unescape(name));
}


function addOption(selectbox,text,value){
	selectbox[selectbox.length] = new Option(text,value);
}

function clearList(selectbox){
	for (i=selectbox.length-1; i > 0; i--){
		selectbox[i] = null;
	}
}

function hideElement (elementId) {
	  var element;
	  if (document.all)
			element = document.all[elementId];
	  else if (document.getElementById)
			element = document.getElementById(elementId);
	  if (element && element.style)
			element.style.display = 'none';
 }
 function showElement (elementId) {
	  var element;
	  if (document.all)
			element = document.all[elementId];
	  else if (document.getElementById)
			element = document.getElementById(elementId);
	  if (element && element.style)
			element.style.display = '';
 }

 

function updateForm(){
	var gender = "";
	var maritalstatus = "";
	
	hideElement("marriedNames");
	hideElement("marriedEmail");
	hideElement("marriedPhone");
	hideElement("marriedYears");
	hideElement("singleName");
	hideElement("singleEmail");
	hideElement("singlePhone");
	
	if (document.form1["fhscol_Gender"].selectedIndex != 0 && document.form1["fhscol_marital_status"].selectedIndex != 0 && document.form1["fhscol_CampusID"].selectedIndex != 0){
		showElement("theform");
		showElement("grouptype");
		showElement("submit");
	} else {
		hideElement("theform");
		hideElement("grouptype");
		hideElement("submit");
	}
	
	if (document.form1["fhscol_Gender"].selectedIndex == 2) {
		gender = "f";
	} else {
		gender = "m";
	}
	
	if (document.form1["fhscol_marital_status"].selectedIndex == 2) {
		maritalstatus = "m";
		showElement("marriedNames");
		showElement("marriedEmail");
		showElement("marriedPhone");
		showElement("marriedYears");
		hideElement("singleName");
		hideElement("singleEmail");
		hideElement("singlePhone");
	} 
	if (document.form1["fhscol_marital_status"].selectedIndex == 1) {
		maritalstatus = "s";
		hideElement("marriedNames");
		hideElement("marriedEmail");
		hideElement("marriedPhone");
		hideElement("marriedYears");
		showElement("singleName");
		showElement("singleEmail");
		showElement("singlePhone");
	}
	
	
	if (document.form1["fhscol_Gender"].selectedIndex != 0 && document.form1["fhscol_marital_status"].selectedIndex != 0 && document.form1["fhscol_CampusID"].selectedIndex != 0){
		updateGroupTypes(document.form1["fhscol_CampusID"].value, gender, maritalstatus);
		setFields();
	}
}

function setFields(){
	var gender = "";
	
	if (document.form1["fhscol_Gender"].selectedIndex == 2) {
		gender = "f";
	} else {
		gender = "m";
	}
	
	if (document.form1["fhscol_marital_status"].selectedIndex == 2) {
		if (document.form1["fhscol_Gender"].selectedIndex == 1) {
			document.form1["fhscol_FirstName"].value = document.form1["HusbandName"].value;
			document.form1["fhscol_SpouseName"].value = document.form1["WifeName"].value;
		} else {
			document.form1["fhscol_SpouseName"].value = document.form1["HusbandName"].value;
			document.form1["fhscol_FirstName"].value = document.form1["WifeName"].value;
		}
	} else {
		document.form1["fhscol_FirstName"].value = document.form1["SingleName"].value;
	}
	
	document.form1["fhscol_HomePhone"].value = document.form1["phone1"].value + '-' + document.form1["phone2"].value + '-' + document.form1["phone3"].value;
	
	document.form1["fhscol_srchagegroup"].value = document.form1.fhscol_AgeGroup[document.form1.fhscol_AgeGroup.selectedIndex].text;
	document.form1["fhscol_srchregion"].value = document.form1.fhscol_Region[document.form1.fhscol_Region.selectedIndex].text;
	
	switch(document.form1["fhscol_CampusID"].selectedIndex)
	 {
		case 1 : //NPCC
			document.form1["fhscol_Campus"].value = "NPCC";
			document.form1["fhs_return_url"].value = "http://www.northpoint.org/site/page/grouplink_form_done";
			break;
	   
		case 2 : //BC
			document.form1["fhscol_Campus"].value = "Buckhead";
			document.form1["fhs_return_url"].value = "http://www.northpoint.org/site/page/grouplink_form_done";
			break;
	   
		case 3 : //BB
			document.form1["fhscol_Campus"].value = "Browns Bridge";
			document.form1["fhs_return_url"].value = "http://www.northpoint.org/site/page/grouplink_form_done";
			break;
	 }
	 
	 if (document.form1.gtRadio != null) {
		 var gtID = 0;
		 if (document.form1.gtRadio.length != null)
			 for (i = 0; i<document.form1.gtRadio.length; i++){
				if (document.form1.gtRadio[i].checked)
					gtID = document.form1.gtRadio[i].value;
			 }
		else
			gtID = document.form1.gtRadio.value;
		
		//Early in Marriage Code
		if (document.form1.fhscol_YearsMarried.value == "2 Years or less" && (document.form1["fhscol_Campus"].value == "NPCC" || document.form1["fhscol_Campus"].value == "Browns Bridge") && (gtID == "12" || gtID == "13" || gtID == "14")){
			switch(gtID)
			  {
			  	case "12" : 
			  		gtID = "19";
					document.form1["fhscol_GroupType"].value = "19";
					break;
			  	case "13" : 
			  		gtID = "20";
					document.form1["fhscol_GroupType"].value = "20";
					break;
			  	case "14" : 
			  		gtID = "21";
					document.form1["fhscol_GroupType"].value = "21";
					break;
			  }
			
			
			//alert(gtID);
			//alert(document.form1.fhscol_GroupType.value);
		}
			
		//getAssimilator(document.form1["fhscol_CampusID"].value, gtID, gender, document.form1["fhscol_Region"].value, document.form1["fhscol_AgeGroup"].value);
		document.form1["fhs_return_url"].value = "http://www.northpoint.org/adults/grouplink_success/c" + document.form1["fhscol_CampusID"].value + "/gt" + gtID + "/g" + gender + "/r" + document.form1["fhscol_Region"].value + "/ag" + document.form1["fhscol_AgeGroup"].value + "/ln" + document.form1["fhscol_LastName"].value;
	}
}

function formCheck(){
	message = "";
	flag = true;
	
	
	if (document.form1["fhscol_Gender"].selectedIndex == 0){message += "  -- Gender\n";flag = false;}
	if (document.form1["fhscol_marital_status"].selectedIndex == 0){message += "  -- Marital Status\n"; flag = false;}
	
	if (flag){
		if (document.form1["fhscol_marital_status"].selectedIndex == 2) {
			if (document.form1["HusbandName"].value == ""){message += "  -- Husband's First Name\n";}
			if (document.form1["WifeName"].value == ""){message += "  -- Wife's First Name\n";}
		} else {
			if (document.form1["SingleName"].value == ""){message += "  -- First Name\n";}
		}
		
		if (document.form1["fhscol_LastName"].value == ""){message += "  -- Last Name\n";}
		//if (document.form1["fhscol_StreetAddress"].value == ""){message += "  -- Address\n";}
		//if (document.form1["fhscol_City"].value == ""){message += "  -- City\n";}
		//if (document.form1["fhscol_State"].value == ""){message += "  -- State\n";}
		//if (document.form1["fhscol_Zip"].value == ""){message += "  -- Zip\n";}
		if (document.form1["fhscol_Email"].value == ""){message += "  -- Email\n";}
		if (document.form1["phone1"].value == "" || document.form1["phone3"].value == "" || document.form1["phone3"].value == ""){message += "  -- Phone\n";}
		
		if (document.form1["fhscol_AgeGroup"].value == ""){message += "  -- Age Group\n";}
		if (document.form1["fhscol_Region"].value == ""){message += "  -- Location\n";}
		
		//if (document.form1.fhscol_GroupType.length != null) {
			//for (i = 0, res = false; i<document.form1.fhscol_GroupType.length; i++)
				//res = res || document.form1.fhscol_GroupType[i].checked;
			//if (res == false){message += "  -- Type of group you would like join\n";}
		//}else if (!document.form1.fhscol_GroupType.checked){message += "  -- Type of group you would like join\n";}
		
		if (document.form1["fhscol_GroupType"].value == ""){
			message += "  -- Type of group you would like to join\n";
			if (document.form1.GroupType == null) {
				updateForm();
			}
		}
		
		
		if (document.form1["fhscol_EnteredBy"].value == ""){message += "  -- Entered By\n";}
		
	}
			
	if (message != ""){
		message = "You must enter the following information:\n\n" + message + "\nPlease click OK, enter the required information,\nand re-submit your request.";
		alert(message);
		return false;
	}
	
	
	if (document.form1["fhscol_marital_status"].selectedIndex == 2) {
		if (document.form1["fhscol_Gender"].selectedIndex == 1) {
			document.form1["fhscol_FirstName"].value = document.form1["HusbandName"].value;
			document.form1["fhscol_SpouseName"].value = document.form1["WifeName"].value;
		} else {
			document.form1["fhscol_SpouseName"].value = document.form1["HusbandName"].value;
			document.form1["fhscol_FirstName"].value = document.form1["WifeName"].value;
		}
	} else {
		document.form1["fhscol_FirstName"].value = document.form1["SingleName"].value;
	}
	
	//setFields();
			//alert(document.form1["fhs_recipients"].value);
			//return false;
	
	//submitForm(document.form1);
	//document.form1.submit();
	//return false;
	
}

function resetLeader(){
	
	document.form1["fhscol_InterestLeading"].selectedIndex = 2;
		
}

// Auto-Tab Functions
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}


	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
			if(arr[index] == ele)
				found = true;
			else
				index++;
		return found;
	}
	
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
			if (input.form[i] == input)
				index = i;
			else 
				i++;
		return index;
	}
return true;
}

