//go cincinnati javascript funcs
var rolesArr = [];
var availRoleLevel = 0;
var seasonedLeader = [];
var seasonedJourney = [];

function getNextStep(x) 
{
	var d = document.frm_leadersignup;
	$('member_prompt').style.display = 'none';
	$('member_details').style.display = 'none';
	$('partner_church_user_info').style.display = 'none';
	$('static_details').style.display = 'none';
	$('project_info').style.display = 'none';
	if(x == '1'){
		if(d.actMember.value == '1')
		{
			$('member_details').style.display = '';
			$('static_details').style.display = '';
		} else {
			$('member_prompt').style.display = '';
		}
	} else {
		$('partner_church_user_info').style.display = '';
		$('static_details').style.display = '';
	}
	getAvailableRoles(0);
}

function getAvailableRoles(x)
{	
	var msgs = "";
	var allowSubmit = true;
	var d = document.frm_leadersignup;
	var c = d.homechurch.value;
	var s = d.spiritualjourney.value;
	var p = d.pastparticipant.value;
	
	switch(x){
		case 0:
			if(d.homechurch.value.length < 1 || d.homechurch.value == 'Please Select') {
			  allowSubmit = false;
				msgs += "- Please select a home church.\n";
			} else {
				(d.homechurch.value == "1") ? d.churchVal.value = 1 : d.churchVal.value = 0;
			}
			break;
		case 1:
			if(d.spiritualjourney.value.length < 1 || d.spiritualjourney.value == 'Please Select') {
			  allowSubmit = false;
				msgs += "- Please select where you are on your spiritual journey.\n";
			} else {
				(d.spiritualjourney.value == "4") ? d.journeyVal.value = 1 : d.journeyVal.value = 0;
			}
			break;
		case 2:
			if(d.pastparticipant.value.length < 1 || d.pastparticipant.value == 'Please Select') {
			  allowSubmit = false;
				msgs += "- Please select how you\'ve participated with GO Cincinnati in the past.\n";
			} else {
				(d.pastparticipant.value == "3" || d.pastparticipant.value == "4" || d.pastparticipant.value == "5") ? d.participantVal.value = 1 : d.participantVal.value = 0;
				$('project_info').style.display = '';
			}
			break;
	}
	
	if(allowSubmit) { 
		var result;
		availRoleLevel = 0;
		
		if(c == "1") //crossroads is home church
		{
			if(s == "4") //been following Christ for more than a year
			{
				showHide(1,1);				
				if(p == "4" || p == "5") //neighborhood lead or Resource in past
				{
					availRoleLevel = 2;
					showHide(1,0);
				} else if(p == "3"){ //team captain in past
					availRoleLevel = 1;
					showHide(1,0);
				} else {
					showHide(0,0);
				}
			} else {
				showHide(0,1);
			}
		} else {
			if(s == "4") //been following Christ for more than a year
			{
				showHide(1,1);
			} else {
				showHide(0,1);
			}
			if(p == "3" || p == "4" || p == "5") //neighborhood lead or Resource in past
			{
				showHide(1,0);
			} else {
				showHide(0,0);
			}
		}
		d.resultVal.value = availRoleLevel;
		if(availRoleLevel == 2)
		{
			$('nl').style.display = '';
			$('rt').style.display = '';
		} else if(availRoleLevel == 1) {
			$('nl').style.display = '';
			$('rt').style.display = 'none';
		} else {
			$('nl').style.display = 'none';
			$('rt').style.display = 'none';
		}
	} else {
		msgs += "\nPlease correct the item(s) listed above before continuing.";
		d.pastparticipant.selectedIndex=0;
		alert(msgs);
	}
}

function showHide(flag,type)
{
	if(flag == 0) //hide
	{
		if(type == 0) //type is prior leader
		{
			for(i=0;i<seasonedLeader.length;i++)
			{
				$(seasonedLeader[i]).checked = false;
				$(seasonedLeader[i]).style.display = 'none';				
			}
		} else { //type is journey
			for(i=0;i<seasonedJourney.length;i++)
			{
				$(seasonedJourney[i]).checked = false;
				$(seasonedJourney[i]).style.display = 'none';
			}
		}
	} else { //show
		if(type == 0) //type is prior leader
		{
			for(i=0;i<seasonedLeader.length;i++)
			{
				$(seasonedLeader[i]).style.display = '';
			}
		} else { //type is journey
			for(i=0;i<seasonedJourney.length;i++)
			{
				$(seasonedJourney[i]).style.display = '';
			}
		}
	}
}

function checkAll()
{
	var d = document.frm_leadersignup;
	for (i=0;i<d.interest.length;i++)
	{
		if(d.interest0.checked == true)
		{
			d.interest[i].checked = true ;
			if($('frm_buttons').style.display == 'none'){$('frm_buttons').style.display = '';};
		} else {
			d.interest[i].checked = false ;	
			if($('frm_buttons').style.display == ''){$('frm_buttons').style.display = 'none';};
		}
	}	
}

function checkAllStatus()
{
	var d = document.frm_leadersignup;
	var result = true;
	var pipe = '';
	for (i=0;i<d.interest.length;i++)
	{
		if(d.interest[i].checked == false)
		{
			result = false;
		} else {
			pipe += d.interest[i].value + '|';
			if($('frm_buttons').style.display == 'none'){$('frm_buttons').style.display = '';};
		}
	}
	(result == true) ? d.interest0.checked = true : d.interest0.checked = false;
	d.interestsVal.value = pipe;
}

function getSkills()
{
	var d = document.frm_leadersignup;
	var result = "";
	for(i=0;i<d.skill.length;i++)
	{
		if(d.skill[i].checked==true)
		{ 
			result += d.skill[i].value + '|';		
		}
	}
	d.skillsVal.value = result;
}

function validateLeaderSignupForm(x)
{
	var d = document.frm_leadersignup;
	var msgs = "";
	var allowSubmit = true;
	
	checkAllStatus();
	getSkills();
	//alert("Value of X: " + x + "\n\rInterests: " + d.interestsVal.value + "\n\rSkills: " + d.skillsVal.value + "\n\r");
	
	if(d.homechurch.value == 'Please Select') {
	  allowSubmit = false;
		msgs += "- Please provide a home church.\n";
	} else {
		if(d.homechurch.value > 1) {
			if(d.first.value.length < 1) {
			  allowSubmit = false;
				msgs += "- Please provide your first name.\n";
			}
			if(d.last.value.length < 1) {
			  allowSubmit = false;
				msgs += "- Please provide your last name.\n";
			}
			if(d.email.value.length < 1) {
			  allowSubmit = false;
				msgs += "- Please provide your email address.\n";
			} else { //we have an entry for email let's try an validate the address
				if(emailCheck(d.email.value) == 0)
				{
					msgs += "- The email address entered doesn't appear to be valid.\n";
				}
			}
			if(d.phone_cell.value.length < 1) {
					allowSubmit = false;
					msgs += "- Please provide a cell phone number.\n";
			}
			if(d.phone_primary.value.length < 1) {
					allowSubmit = false;
					msgs += "- Please provide a primary phone number.\n";
			}			
		} else { //crossroads was selected let's double check that we have member id
			if(d.personid.value < 1) {
				allowSubmit = false;
				msgs += "- Please login using your Crossroads account \n\ror register a new account and return to complete this form.\n";	
			}
		}
	}
	
	if(d.shirtsize.value == 'Please Select') {
	  allowSubmit = false;
		msgs += "- Please provide a T-Shirt size.\n";
	}
	
	if(d.spiritualjourney.value == 'Please Select') {
	  allowSubmit = false;
		msgs += "- Please select where you are on your spiritual journey.\n";
	}
	
	if(d.pastparticipant.value == 'Please Select') {
	  allowSubmit = false;
		msgs += "- Please select your prior GO Cincinnati experience.\n";
	}
		
	var val = 0;
	for( i = 0; i < d.avRoles.length; i++ )
	{
		if( d.avRoles[i].checked == true )
		val = d.avRoles[i].value;
	}
	if(val == 0) {
	  allowSubmit = false;
		msgs += "- Please select the GO Cincinnati Role you would like to volunteer for.\n";
	}

	if(d.interestsVal.value.length < 1) {
	  allowSubmit = false;
		msgs += "- Please select at least one project type that you would be interested in leading.\n";
	}
	
	if(allowSubmit) {
		d.action = 'https://' + window.location.hostname + '/gocincinnati/actions/signup/leadership/actLeaderSignup.php';
		d.submit();
		  
	} else {
		msgs += "\nPlease correct the item(s) listed above to submit the form.";
		alert(msgs);
	}
}

function emailCheck(emailStr) 
{
	if(emailStr.length < 1) {
	return true;
	}
	/* The following variable tells the rest of the function whether or not
	to verify that the address ends in a two-letter country or well-known
	TLD.  1 means check it, 0 means don't. */
	
	var checkTLD=1;
	
	/* The following is the list of known TLDs that an e-mail address must end with. */
	
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	
	/* The following pattern is used to check if the entered e-mail address
	fits the user@domain format.  It also is used to separate the username
	from the domain. */
	
	var emailPat=/^(.+)@(.+)$/;
	
	/* The following string represents the pattern for matching all special
	characters.  We don't want to allow special characters in the address. 
	These characters include ( ) < > @ , ; : \ " . [ ] */
	
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	
	/* The following string represents the range of characters allowed in a 
	username or domainname.  It really states which chars aren't allowed.*/
	
	var validChars="\[^\\s" + specialChars + "\]";
	
	/* The following pattern applies if the "user" is a quoted string (in
	which case, there are no rules about which characters are allowed
	and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	is a legal e-mail address. */
	
	var quotedUser="(\"[^\"]*\")";
	
	/* The following pattern applies for domains that are IP addresses,
	rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	e-mail address. NOTE: The square brackets are required. */
	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	
	/* The following string represents an atom (basically a series of non-special characters.) */
	
	var atom=validChars + '+';
	
	/* The following string represents one word in the typical username.
	For example, in john.doe@somewhere.com, john and doe are words.
	Basically, a word is either an atom or quoted string. */
	
	var word="(" + atom + "|" + quotedUser + ")";
	
	// The following pattern describes the structure of the user
	
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	
	/* The following pattern describes the structure of a normal symbolic
	domain, as opposed to ipDomainPat, shown above. */
	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	
	/* Finally, let's start trying to figure out if the supplied address is valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	different pieces that are easy to analyze. */
	
	var matchArray=emailStr.match(emailPat);
	
	if (matchArray==null) {
	
	/* Too many/few @'s or something; basically, this address doesn't
	even fit the general mould of a valid e-mail address. */
	
	return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	
	// Start by checking that only basic ASCII characters are in the strings (0-127).
	
	for (i=0; i<user.length; i++) {
	if (user.charCodeAt(i)>127) {
	return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
	if (domain.charCodeAt(i)>127) {
	return false;
	   }
	}
	
	// See if "user" is valid 
	
	if (user.match(userPat)==null) {
	// user is not valid
	return false;
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	host name) make sure the IP address is valid. */
	
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
	
	// this is an IP address
	
	for (var i=1;i<=4;i++) {
	if (IPArray[i]>255) {
	return false;
	   }
	}
	return true;
	}
	
	// Domain is symbolic name.  Check if it's valid.
	 
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
	if (domArr[i].search(atomPat)==-1) {
	return false;
	}
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	known top-level domain (like com, edu, gov) or a two-letter word,
	representing country (uk, nl), and that there's a hostname preceding 
	the domain or country. */
	
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
	return false;
	}
	
	// Make sure there's a host name preceding the domain.
	
	if (len<2) {
	return false;
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}
