/************************************************************
Javascript - Function Page
Page: functions.js
Developer: Jeffrey M. Johns
Support: jeffrey.johns@mcall.com
Created: 10/15/2004
Modified: N/A
************************************************************
Notes/Comments: This is the JS page to hold all JS functions for the off-site
Merge rendition.
************************************************************
Updates:
04/04/2005 - Added a new function erase_default_text() and added onto the
comment validation function to ask the user question before submitting their
comment.
************************************************************/
function find_DOM(object_id,incl_style) { 
    /*************************** 
    IE 5+ || Netscape 6+ 
    ***************************/ 
    if (document.getElementById) { 
        found_object = document.getElementById(object_id); 
    } 
    /*************************** 
    IE 4+ 
    ***************************/ 
    else if (document.all) { 
        found_object = document.all[object_id]; 
    } 
    /*************************** 
    Netscape 4 
    ***************************/ 
    else { 
        browser_version = parseInt(navigator.appVersion); 
        if ((navigator.appName.indexOf('Netscape') != 1 && browser_version == 4)) { 
            found_object = document.layers[object_id]; 
            var nav4 = true; 
        }      
    } 

    /*************************** 
    Return object 
    ***************************/ 
    if (incl_style == 1 && !nav4) { 
        return found_object.style; 
    } else { 
        return found_object; 
    }          
} 

function input_focus(field,field_style) {
	field.className = field_style;
}

function input_blur(field,field_style) {
	field.className = field_style;
}

function erase_default_text(field) {
	var default_text = "This space is our place for you to express your opinions in a civil and respectful manner. Insensitive, inflammatory and inappropriate posts are not allowed and will be removed at our discretion.";

	if (field.value == default_text) {
		field.value = '';
	}

}

function validate_mcall() {
	var winl = (screen.width - 350) / 2; 
	var wint = (screen.height - 125) / 2; 
	var default_text = "This space is our place for you to express your opinions in a civil and respectful manner.";
	default_text += " Insensitive, inflammatory and inappropriate posts are not allowed and will be removed at our discretion.";

	with (document.the_form) {
		if (comments.value.length < 2) {
			comments.focus();
			alert("The following errors were found:\n--------------------------\nYou must submit a comment.\n");
		} 
		else if (comments.value == default_text) {
			comments.value = "";
			comments.focus();
			alert("The following errors were found:\n--------------------------\nYou must submit a comment.\n");
		} 
		else {
			window.open('question.php','questionBox','width=350,height=125,left=' + winl + ',top=' + wint + ',scrollbars=no');
		}
	}
}

function validate_warning() {
	var errors = 0;
	var error_list = "";
	var focus_found = "";

	with (document.the_form) {
		if (email.value.length < 1) {
			errors = errors + 1;
			error_list += "You must choose a username.\n";
			email.focus();
			focus_found = "true";
		}
		if (message.value.length < 2) {
			errors = errors + 1;
			error_list += "You must submit a message .\n";
			if (!focus_found) {
				message.focus();
				focus_found = "true";
			}
		}
	}

	if (errors >= 1) {
		alert("The following errors were found:\n--------------------------\n" + error_list);
		return false;
	} else {
		return true;
	}
}

function validate_email_form() {
	var errors = 0;
	var error_list = "";
	var focus_found = "";

	with (document.the_form) {
		if (email.value.length < 1) {
			errors = errors + 1;
			error_list += "You must choose a username.\n";
			email.focus();
			focus_found = "true";
		}
		if (subject.value.length < 1) {
			errors = errors + 1;
			error_list += "You must submit a subject.\n";
			if (!focus_found) {
				subject.focus();
				focus_found = "true";
			}
		}
		if (from_name.value.length < 1) {
			errors = errors + 1;
			error_list += "You must submit your name.\n";
			if (!focus_found) {
				from_name.focus();
				focus_found = "true";
			}
		}
		if (from_address.value.length < 1) {
			errors = errors + 1;
			error_list += "You must submit your email address.\n";
			if (!focus_found) {
				from_address.focus();
				focus_found = "true";
			}
		}
		if (message.value.length < 2) {
			errors = errors + 1;
			error_list += "You must submit a message.\n";
			if (!focus_found) {
				message.focus();
				focus_found = "true";
			}
		}
	}

	if (errors >= 1) {
		alert("The following errors were found:\n--------------------------\n" + error_list);
		return false;
	} else {
		return true;
	}
}

function validate_curse() {
	var errors = 0;
	var error_list = "";
	var focus_found = "";

	with (document.the_form) {
		if (word.value.length < 2) {
			errors = errors + 1;
			error_list += "You must submit a word with at least 2 characters.\n";
			word.focus();
			focus_found = "true";
		}
	}

	if (errors >= 1) {
		alert("The following errors were found:\n--------------------------\n" + error_list);
		return false;
	} else {
		return true;
	}
}

function validate_edit() {
	var errors = 0;
	var error_list = "";
	var focus_found = "";

	with (document.the_form) {
		if (user_id.value == "") {
			errors = errors + 1;
			error_list += "You must choose a username.\n";
			user_id.focus();
			focus_found = "true";
		}
		if (slug_id.value == "") {
			errors = errors + 1;
			error_list += "You must choose a headline.\n";
			if (!focus_found) {
				slug_id.focus();
				focus_found = "true";
			}
		}
	}

	if (errors >= 1) {
		alert("The following errors were found:\n--------------------------\n" + error_list);
		return false;
	} else {
		return true;
	}
}

function check_max(field,max) {
	var message = 'This field is at is maximum allowable characters: ' + max;
	if (field.value.length >= max) {
		alert(message);
		document.the_form.submit_button.focus();
		if (field.value.length > max) {
			field.value = field.value.substring(0,max);
		}
	}
}

function new_window(winURL,winNamer,winWidth,winHeight,scrollBars) { 
	var winl = (screen.width - winWidth) / 2; 
	var wint = (screen.height - winHeight) / 2; 
	var features = 'width=' + winWidth + ',height=' + winHeight + ',left=' + winl + ',top=' + wint + ',scrollbars=' + scrollBars;
	var NewWindow = window.open(winURL,winNamer,features);
}

function deselect_all(form,field) { 
    begin = document[form][field]; 
    field_length = begin.options.length; 

    for( i=0 ; i<field_length ; i++) { 
        begin.options[i].selected = false; 
    } 
} 

function select_all(form,field) { 
    begin = document[form][field]; 
    field_length = begin.options.length; 

    for( i=0 ; i<field_length ; i++) { 
        begin.options[i].selected = true; 
    } 
} 

function uncheck_radios() {
	for (var i = 0; i < document.the_form.space.length; i++) {
		document.the_form.space[i].checked = false;
	}
}

function delete_curse(id) {
	if (confirm('Are you sure you want to delete this curse word?')) {
		window.location = "delete_cw.php?id=" + id;
	}
}

function edit_curse(id) {
	opener.location = "edit_cw.php?id=" + id + "&submitted=1";
	self.close();
}

function comments(user_id) {
	var spans = document.getElementsByTagName("span");
	var thisCookie = document.cookie.split("; ");
	var ssCookie = null;
	var expireDate = new Date();
	expireDate.setMonth(expireDate.getMonth()+120);
	var kill_date = expireDate.setMonth(expireDate.getMonth()-1)
	var span_id = null;
	var span_dom = null;
	var span_found = 0;
	var cookie_user_array = new Array();
	var pos1 = null;
	var pos2 = null;
	var pos3 = null;
	var all_vars = null;
	var cookie_list = null;
	var cookie_written = null;
	var re = null;

	//IF NO USER ID IS FOUND, TRY TO EXTRACT IT FROM THE COOKIE
	for (i = 0; i < thisCookie.length; i++) {
		if (thisCookie[i].split("=")[0] == "ignoreUsers") {
			ssCookie = thisCookie[i].split("=")[1];
			cookie_user_array = ssCookie.split(",");
		}
	}

	//IF NO USER ID IS FOUND, ATTEMPT TO EXTRACT ID LIST FROM COOKIE
	//IF A LIST IS FOUND, ATTEMPT HIDE ALL IGNORED POSTS
	//BASICALLY THIS SECTION IS ONLY CALLED WHEN THE PAGE LOADS
	//THIS SECTION WILL PARSE EACH SPAN ID INTO THREE PARTS
	//THEN SEE IF THE FIRST PART EQUALS THE USER'S ID NUMBER
	//IF SO, FIND OUT IF THE SPAN DISPLAY IS SET TO INLINE OR NONE
	//THEN SIMPLY CHANGE IT
	if (user_id == '' || user_id == null) {
		if (ssCookie != '' && ssCookie != null) {
			for (i = 0; i < cookie_user_array.length; i++) {
				if (cookie_user_array[i] != "") {
					for (x = 0; x < spans.length; x++) {
						span_id = spans[x].id;
						pos1 = span_id.split("|")[0];
						pos2 = span_id.split("|")[1];
						pos3 = span_id.split("|")[2];

						if (cookie_user_array[i] == pos1) {
							span_dom = find_DOM(span_id,1);
							if (span_dom.display == "inline") {
								span_dom.display = "none";
							} else {
								span_dom.display = "inline";
							}
						}
					}
				}
			}
		}
	} else {
		//THIS SECTION WILL EXECUTE WHEN A USER CLICKS AN IGNORE/SHOW LINK
		//THIS SECTION WILL PARSE EACH SPAN ID INTO THREE PARTS
		//THEN SEE IF THE FIRST PART EQUALS THE USER'S ID NUMBER
		//IF SO, FIND OUT IF THE SPAN DISPLAY IS SET TO INLINE OR NONE
		//THEN SIMPLY CHANGE IT AND SET YOUR COOKIE IF IT HAS NOT BEEN SET YET
		for (x = 0; x < spans.length; x++) {
			span_id = spans[x].id;
			pos1 = span_id.split("|")[0];
			pos2 = span_id.split("|")[1];
			pos3 = span_id.split("|")[2];

			if (user_id == pos1) {
				span_dom = find_DOM(span_id,1);
				if (span_dom.display == "inline") {
					span_dom.display = "none";
					if (cookie_written != 1) {
						cookie_written = 1;
						cookie_list = ssCookie + user_id + ",";
						document.cookie = "ignoreUsers=" + cookie_list + "; expires=" + expireDate.toGMTString() + "; path=/comments/";
					}
				} else {
					span_dom.display = "inline";
					if (cookie_written != 1) {
						cookie_written = 1;
						re = user_id + ",";
						cookie_list = ssCookie.replace(re,"");
						if (cookie_list != "" && cookie_list != null) {
							document.cookie = "ignoreUsers=" + cookie_list + "; expires=" + expireDate.toGMTString() + "; path=/comments/";
						} else {
							document.cookie = "ignoreUsers=" + cookie_list + "; expires=-10; path=/comments/";
						}
					}
				}
			}
		}
	}
}
