//test function to make sure it can see functions in this file
function srslyWTF(){
	alert ('Seriously, WTF?');
}

//takes in the value of a text area/field and trims the whitespace at the start and end
function jsTrim(fld) {
	var result = "";
	var c = 0;
	
	for (i=0; i<fld.length; i++) {
		if (fld.charAt(i) != " " || c > 0) {
			result += fld.charAt(i);
			if (fld.charAt(i) != " ") c = result.length;
		}
	}
	
	return result.substr(0,c);
}

//takes in a form object and checks value for a valid email address
function checkEmail(fld){
	with(fld){
		apos = value.indexOf("@");
		dotpos = value.lastIndexOf(".");
		
		if((apos < 1) || ((dotpos-apos) < 2)){
			return false;
		}
		else{
			return true;
		}
	}
}

//function is called to set the hidden input value to determine which page to be forwarded to
function setSub(form, which){
	form.sub.value = which;
	validateA(page_form);
}

//form vaildation
//match the cases to the possible id value of the answer format in the Survey_F table
function validateA(form) {
	//setup flags to default values
	var optionChosen = false;
	var allAnswered = true;
	
	//check for a FName value
	if(jsTrim(form.FName.value) == ''){
		alert ('Please type in your First Name.');
		allAnswered = false;
	}
	
	//check for a LName value
	if(jsTrim(form.LName.value) == ''){
		alert ('Please type in your Last Name.');
		allAnswered = false;
	}
	
	//check for a Email value
	if(jsTrim(form.Email.value) == ''){
		alert ('Please type in your Email address.');
		allAnswered = false;
	}
	else{
		if(checkEmail(form.Email) == false){
			//check for a valid Email value - checking for a valid email addres, since this value will be imported into usend by client at some point
			alert('Please enter a valid Email address.');
			allAnswered = false;
		}
	}
	
	//loop through the number of questions (which on the page is filled into the 'qs' hidden field)
	//NOTE: IF ANY MORE ANSWER FORMATS WERE ADDED TO THE Survey_F TABLE, MORE CONDITIONS WILL HAVE TO BE ADDED HERE TO ACCOMIDATE
	for(k=0;k<form.qs.value;k++){
		
		//yes/no and strongly agree, etc formatted answers
		//check for an answer in the radio button formatted answers
		if(document.getElementById('afid_'+k).value == 1 || document.getElementById('afid_'+k).value == 2){
			
			for (i=0;i<eval("form.a"+k+".length");i++){
				if(eval("form.a"+k+"["+i+"].checked")){
					optionChosen = true;
				}
			}
			if(!optionChosen){
				alert ('Please choose an answer for Question '+(k+1)+'.');
				allAnswered = false;
			}
			
		}
		//text area formatted answers
		//check for an answer in the text area formatted answers
		else if(document.getElementById('afid_'+k).value == 3){
			if(jsTrim(eval("form.a"+k+".value")) == ''){
				alert ('Please type in your answer for Question '+(k+1)+'.');
				allAnswered = false;
			}
		}
		
		//reset optionChosen value to check next question
		optionChosen = false;
	}
	
	//if all's clear, then lets blow this form like Chernobyl
	if(allAnswered){
		form.submit();
	}
}





/*
=========================================================================================================================
=========================================================================================================================

~<< SURVEY FEATURE/APPLICATION NOTES AND ERRATA >>~


-Database Tables-

Survey_S
|-> Links With: 	Survey_Q, Survey_U
|-> Fields: 		id, Visible, Title, LastModified, LastModifiedBy, (Candidate_id used by Jeb/Clegg Duo)
|-> Notes: 		Stores the title of the survey as well as its visibility. One to Many relationship of this
			table to both linked tables.

Survey_Q
|-> Links With: 	Survey_S, Survey_A, Survey_F
|-> Fields: 		id, Survey_id, Visible, Question, AFormat_id, Sort_Order,  LastModified, 
			LastModifiedBy, (Candidate_id used by Jeb/Clegg Duo)
|-> Notes: 		Stores the Question, Which Survey it belongs to, and which format the answer will be in.
			Sort_Order field is used to determine the order in which the questions will appear on the
			page, with 0 denoting the top-most question.
			Survey_id field's entry should match the Survey_S id entry if the survey it belongs to.
			One to Many relationship of this table to Survey_A.
			AFormat_id should match the Survey_F id number of the format the answer should be put in.

Survey_A
|-> Links With:		Survey_Q
|-> Fields:		id, q_id, Answer, DateTime
|-> Notes:		This table stores each individual answer given by a submitter for a survey question.
			q_id references the Survey_Q id of the question this answer was given for.
			DateTime is a timestamp given upon insertion via the now() function.

Survey_F
|-> Links With:		Survey_Q
|-> Fields:		id, Title, Form_Html
|-> Notes:		This table houses each possible answer format a question could have and its html.
			Form_Html contents are enclosed in one or more <tr> tags, so it should be included as part of a
			table. 
			The name attribute for each form element (radio button grp or text area) is set to "a". When
			the form element is brought onto the page, an unique identifier (i use the index of a for loop)
			is appended to the end of name. (ie. "a" becomes "a1", "a2", etc)
			The id number is used to determine what kind of answer it 


Survey_U
|-> Links With:		Survey_Q, Survey_A
|-> Fields:		id, FName, LName, Email, Surveys, Answers, (Candidate_id used by Jeb/Clegg Duo)
|-> Notes:		This table stores the name, email, and answer information of those people that 
			fill out the survey.
			Surveys field contains a comma-delimited list of ids to each survey that the user has taken.
			Answers field contains a comma-delimited list of ids to each answer that the user has given.  





-Pertinent Files- (using JebForCongress.com as an example)

/inc/survey.js
|-> Used For:				Form validation of the survey.

/survey.php
|-> Used For:				Survey form display.

/survey_ty.php
|-> Used For:				Information processing, insertion, and feedback to the user.

/admin/SingleSubmitterExport.php
|-> Used For:				Exporting all recorded answers by user that's taken the survey into a .csv

/admin/SubmitUsendExport.php
|-> Used For:				Exporting all email addresses of survey submitters into a .csv that'll be 
					imported into Usend

/admin/SurveyQExport.php
|-> Used For: 				Exporting survey results into a .csv file

/admin/SurveyEditor.php, /admin/SurveyGroupEditor
|-> Used For:				Adding Questions and Surveys, respectively, to the database



-Other Notes-

::Uses the dbexec function of the inc_header.php file to do the db queries

::There is javascript that should be included in the head tag of the pages. For JebForCongress.com, you can find this
  in the /templates/temp_top.php file, where there should be script tags enclosed by php if statements based on php_self

::If more Answer Formats are added to the Survey_F table aside from the three standard ones


=========================================================================================================================
=========================================================================================================================
*/