///////////////////////////////////////////////////////////////////////////////////
// Email Window Functions
////////////////////////////////////////////////////////////////////////////////////

//opens a small window for sending email; the params hold the content id and 
//any other querystring vars
function open_ewin(id, queryString) {
	window.open('/email/?id=' + id + queryString, 'email_window', 'width=450,height=280,scrollbars=1');
}

//validates the email form (which must be named 'eform')
function submit_eform() {
	var err;
	if (document.eform.from_name.value == "") err = "blankNField";
	else if (document.eform.from_email.value == "") err = "blankEField";
	else if (document.eform.to_email.value == "") err = "blankFEField";
	if (err) doErr(err);
	else document.eform.submit();
}


//adds an email address, or list of email addresses, to the toEmail field
function add_email_addr(toField, fromField, fieldIndex) {
	var semicolon = '; ', thisValue = ''
	if (fromField[fieldIndex].value == 'all') { //add all addresses in the menu
		thisValue = fromField[1].value;
		for (var x = 2; x < fromField.length; x++) {
			thisValue += (semicolon + fromField[x].value); 
		}
	}
	else {
		thisValue = fromField[fieldIndex].value;
	}
	if (toField.value == '') semicolon = '';
	toField.value += (semicolon + thisValue);
	toField.select();
}

////////////////////////////////////////////////////////////////////////////////////
// Login/Signup Functions
////////////////////////////////////////////////////////////////////////////////////

//validates the login form (which must be name 'loginform')
function submit_login() {
	var err;
	if (document.loginform.email.value == '') err = "blankEField";
	else if (document.loginform.pass.value == '') err = "blankPField";
	if (err) doErr(err);
	else document.loginform.submit();
}

//validates the enticing login form (which must be name 'eloginform')
function submit_elogin() {
	var err;
	if (document.eloginform.email.value == '') err = "blankEField";
	else if (document.eloginform.pass.value == '') err = "blankPField";
	if (err) doErr(err);
	else document.eloginform.submit();
}

//validates the sign up form (which must be name 'suform')
function submit_suform() {
	var err;
	if (document.suform.name.value == '') err = "blankNField";
	else if (document.suform.email.value == '') err = "blankEField";
	else if (document.suform.pass.value == '') err = "blankPField";
	else if (document.suform.pass2.value == '') err = "blankCPField";
	else if (!(document.suform.gender[0].checked||document.suform.gender[1].checked))  err = "noGender";
	if (err) doErr(err);
	else document.suform.submit();
}

////////////////////////////////////////////////////////////////////////////////////
// User Profile Functions
////////////////////////////////////////////////////////////////////////////////////

//validates the forgotten password  form (which must be name 'fpform')
function submit_fpform() {
	var err;
	if (document.fpform.email.value == '') err = "blankEField";
	if (err) doErr(err);
	else document.fpform.submit();
}

//validates the edit user info form (which must be name 'euform')
function submit_euform() {
	var err;
	if (document.euform.name.value == '') err = "blankNField";
	else if (document.euform.pass.value == '') err = "blankPField";
	else if (document.euform.pass2.value == '') err = "blankCPField";
	if (err) doErr(err);
	else document.euform.submit();
}

////////////////////////////////////////////////////////////////////////////////////
// Question/Comment Functions
////////////////////////////////////////////////////////////////////////////////////

//validates the question form (which must be named 'qform')
function submit_qform() {
	var thisQuestion, checked, hasOptions;
	// check each question
	for (var x = 0; x <= document.qform.question_count.value; x++) {
		thisQuestion = "q[" + x + "]";
		checked = false;
		if (document.qform[thisQuestion]) {
			hasOptions = true
			for (var y = 0; y < document.qform[thisQuestion].length; y++) {
				if (document.qform[thisQuestion][y].checked) {
					checked = true;
					break;	
				}
			}
			if (checked == false) { 
				alert("Please answer question " + (x+1) + ".");
				break;
			}
		}
	}
	// if there are no questions, they must enter a comment
	if (!hasOptions) {
		if (document.qform.comment.value == "") {
			alert("Please enter your comment.");
			checked = false;
		}
		else checked = true;
	}
	if (checked&&document.qform.gender) {
		if (!(document.qform.gender[0].checked||document.qform.gender[1].checked)) {
			alert("Please enter your gender.");
			checked = false;
		}
	}
	if  (checked) { 
		document.qform.submit();
	}
}

//validates the question form (which must be named 'smallqform')
function submit_smallqform() {
	var thisQuestion, checked, hasOptions;
	// check each question
	for (var x = 0; x <= document.smallqform.question_count.value; x++) {
		thisQuestion = "q[" + x + "]";
		checked = false;
		if (document.smallqform[thisQuestion]) {
			hasOptions = true
			for (var y = 0; y < document.smallqform[thisQuestion].length; y++) {
				if (document.smallqform[thisQuestion][y].checked) {
					checked = true;
					break;	
				}
			}
			if (checked == false) { 
				alert("Please answer question " + (x+1) + ".");
				break;
			}
		}
	}
	if  (checked) { 
		document.smallqform.submit();
	}
}

////////////////////////////////////////////////////////////////////////////////////
// Error Functions
////////////////////////////////////////////////////////////////////////////////////

//function checks for errors and does anything else necessary on load
function doErr(err) {
	if (err) {
		var errMsg;
		switch(err) {
			case "eNotFound": 
				errMsg = "I'm sorry, that email address wasn't found in our database.";
				break;
			case "eFound": 
				errMsg = "I'm sorry, that email address already exists in our database.";
				break;
			case "nFound": 
				errMsg = "I'm sorry, that username already exists in our database.";
				break;
			case "pWrong": 
				errMsg = "I'm sorry, that password is incorrect.";
				break;
			case "blankField": 
				errMsg = "You forgot to fill out one of the fields.";
				break;
			case "blankEField": 
				errMsg = "You didn't enter your email address.";
				break;
			case "blankFEField": 
				errMsg = "You didn't enter your friend's email address.";
				break;
			case "blankNField": 
				errMsg = "You didn't enter a name.";
				break;
			case "blankPField": 
				errMsg = "You didn't enter your password.";
				break;
			case "blankCPField": 
				errMsg = "You didn't confirm your password.";
				break;
			case "noGender": 
				errMsg = "You didn't enter your gender.";
				break;
			case "pNotConfirmed": 
				errMsg = "Your confirmation didn't match your password. Please enter them again.";
				break;
			case "qSuccess": 
				errMsg = "Your question has been successfully submitted.";
				break;
			case "eSent": 
				errMsg = "We sent an email to you with your username and password. You should get it any minute now, just wait.";
				break;
			default: 
				errMsg = "Um... something went wrong. Try again.";
				break;
		}
		alert(errMsg);
	}
}

////////////////////////////////////////////////////////////////////////////////////
// Utility Functions
////////////////////////////////////////////////////////////////////////////////////

//confirms a deletion
function confirm_delete(url, what) {
	if (confirm("Are you sure you want to delete "+what+"?")) document.location.href = url;
}

//confirms a deletion on a form
function confirm_form_delete(thisform, what) {
	if (confirm("Are you sure you want to delete "+what+"?")) {
		if (document[thisform].delete_confirmed) document[thisform].delete_confirmed.value = "1";
		document[thisform].submit();
	}
}

////////////////////////////////////////////////////////////////////////////////////
// Image Functions
////////////////////////////////////////////////////////////////////////////////////

// Functions that swaps images.
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

// Functions that handle preload.
function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
   var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
   if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
