var changes_made = false;
var flash_clicked = false;
var template1over_img = "../images/template1_off.jpg";
var template1out_img = "../images/template1.jpg";
var template2over_img = "../images/template2_off.jpg";
var template2out_img = "../images/template2.jpg";
var selected_template = "";

var mouseover_color = "red";
var mousedown_color = "green";
var mouseout_color = "white";
var preview_window = "";
var global_str = "";
var updating_fields = false;
var splitting_fields = false;

var template_definition = new Array();
template_definition[1] = 1;
template_definition[2] = 2;
template_definition[3] = 3;

//var highlightuser_tracking = new Array();
var open_image = "images/down_grey.gif";
var closed_image = "images/right_grey.gif";
var sort_ascending_on = "../images/down_white.gif";
var sort_ascending_off = "../images/down_grey.gif";
var sort_descending_on = "../images/up_white.gif";
var sort_descending_off = "../images/up_grey.gif";

var tree_highlight_bgcolor = "#000033";
var tree_texthighlight_color = "#FFFFFF";
var tree_textunhighlight_color = "#000000";
var expanduser_tracking = new Array();


//This function archives the given article
function archive_current(articles_id, archived) {
	url = get_page();
	if (archived == 0) {
		selection = confirm("Are you sure you want to archive this document"); //Your question.
		if (selection) {
			window.location.href = url+"?articles_id="+articles_id+"&archive=yes";
		}
	}
	else {
		selection = confirm("Are you sure you want to unarchive this document"); //Your question.
		if (selection) {
			window.location.href = url+"?articles_id="+articles_id+"&archive=no";
		}
	}
}

//Gets element by id
function byid(id) {
	if (document.getElementById(id)){
	  return document.getElementById(id);
	}
}

//produces an alert if have made edits to the flash documentand not cliced on submit
function change_alert() {
	if (changes_made) {
		selection = confirm("If you leave this edit mode before clicking submit you will loose your changes\nClick Cancel to go back and save\nClick ok to loose changes and continue"); //Your question.
		if (selection) {
			return true;
		}
		else {
			return false;
		}

	}
}

//changes the background color for a table cell
function change_bg(id) {
	if (byid(id)) {
		byid(id).style.backgroundColor=mousedown_color;
	}
}

//This function set all colors of node array to none
function clear_nodes() {
	for ( feature_id in expanduser_tracking ) {
		feature_id = feature_id.replace(/[^0-9:]+/, '');
		if (byid("table"+feature_id)) {
			 byid("table"+feature_id).style.backgroundColor="";
			 byid("href"+feature_id).style.color= "";
		}
	}
}

//This function closes a node and children below it
function close_parent(id) {
	regex = "/^"+id+"/";
	myregexp = new RegExp("^"+id);
	/*
	the hidden value expanduer_tracking is what tracks the nodes that are open or just highlighted. 
	here we totally redo that value
	*/
	temp_tracking = new Array();
	for (feature_id in expanduser_tracking) {
		feature_id = feature_id.replace(/[^0-9:]+/g, "");
		//If this node is not under the current parent keep tracking it
		if (feature_id.match(myregexp)) {
			byid("table"+feature_id).style.backgroundColor="";	
			if (byid("li"+feature_id)) {
				byid(feature_id).style.display="none";
				byid("img"+feature_id).src=closed_image;
			}
		}
		else {
			temp_tracking[feature_id] = "";
		}
	}
	expanduser_tracking = temp_tracking;
}


//This function delete the current article
function delete_current(articles_id) {
	url = get_page();
	selection = confirm("Are you sure you want to delete this article"); //Your question.
	if (selection) {
		window.location.href = url+"?articles_id="+articles_id+"&delete=yes";
	}
}

function editor_loaded() {
	//alert("editor loaded");
}

function edit_button(command) {
	if ((changes_made == true) && (flash_clicked == true)){
		change = change_alert();
		if (change) {
			byid('command').value=command;
			document.form.submit();
		}
	}
	else {
		byid('command').value=command;
		document.form.submit();
	}
}

//This function will open or close a branch on the tree
function expand_contract(id, node) {
	id_list = id.split(":");
  if (node == "child") {
		if (byid("table"+id)) {
				clear_nodes();
				byid("table"+id).style.backgroundColor=tree_highlight_bgcolor;
				byid("href"+id).style.color= tree_texthighlight_color;
				highlight_parents(id);
		}
    first_only = "";

  }
  //if parent 
	else {
    if (byid(id).style.display == "none") {
			if (byid("table"+id)) {
				clear_nodes();
				byid("table"+id).style.backgroundColor=tree_highlight_bgcolor;
				byid("href"+id).style.color= tree_texthighlight_color;
				highlight_parents(id);
			}
			id_string = "";
  	}
	  else {
      if (first_only == "yes") {
				clear_nodes();
				close_parent(id);
        byid(id).style.display = "none";
 	      byid("img"+id).src=closed_image;


			}
    }
	}
 first_only = "";
}

//This function expands a node and parents above it
function expand_parents(id) {
	id_list = id.split(":");
	id_string = "";
	for (x=0; x<id_list.length; x++) {
		if (id_string == "") {
			id_string = id_string + id_list[x];
		}
		else {
			id_string = id_string + ":" + id_list[x];
		}
    if (byid(id_string)) {
			byid(id_string).style.display = "block";
			if (byid("img"+id_string)) {
	      byid("img"+id_string).src=open_image;
			}
      expanduser_tracking[id_string] = "";
		}
		else if (byid("li"+id_string)) {
      expanduser_tracking[id_string] = "";
		}

    if (byid("table"+id_string)) {
  		byid("table"+id_string).style.backgroundColor = tree_highlight_bgcolor;
  		byid("href"+id_string).style.color = tree_texthighlight_color;			
		}
	}
}


//Parses the text coming from flash
function flash_parse(id) {
	flash_string = "";
	if (byid(id)) {
		flash_string = byid(id).value;


		//flash_string = flash_string.replace(/\>TM\</gi, '>&trade;<');
		/*flash_string = flash_string.replace(/<font[^>]+>/ig, '');
		flash_string = flash_string.replace(/<.font>/ig, '');
		flash_string = flash_string.replace(/<p[^>]+>/i, '');
		flash_string = flash_string.replace(/<p[^>]+>/ig, '<br>');
		flash_string = flash_string.replace(/<.p>/ig, '');
		flash_string = flash_string.replace(/<textformat[^>]+>/ig, '');
		flash_string = flash_string.replace(/<.textformat>/ig, '');
		flash_string = flash_string.replace(/(<br>)+$/ig, '');
		flash_string = flash_string.replace(/&apos;/ig, "'");
		flash_string = flash_string.replace(/&quot;/ig, "&#34;");
		flash_string = flash_string.replace(/THIS_IS_A_LESS_THAN_SYMBOL/ig, '&lt;');
		flash_string = flash_string.replace(/THIS_IS_A_GREATER_THAN_SYMBOL/ig, '&gt;');
		flash_string = flash_string.replace(/THIS_IS_A_PERCENT_SYMBOL/ig, '%');
		flash_string = flash_string.replace(/THIS_IS_A_AMPERSAND_SYMBOL/ig, '&amp;');
		flash_string = flash_string.replace(/THIS_IS_A_QUOTE_SYMBOL/ig, '&#43;');
		flash_string = flash_string.replace(/THIS_IS_A_SINGLE_QUOTE_SYMBOL/ig, '&apos;');

		flash_string = flash_string.replace(/THIS_IS_A_THREE_DOT_SYMBOL/ig, '&#133;');
		flash_string = flash_string.replace(/THIS_IS_A_SINGLE_OPEN_SMART_QUOTE/ig, '&#145;');
		flash_string = flash_string.replace(/THIS_IS_A_SINGLE_CLOSED_SMART_QUOTE/ig, '&#146;');
		flash_string = flash_string.replace(/THIS_IS_A_DOUBLE_OPEN_SMART_QUOTE/ig, '&#147;');
		flash_string = flash_string.replace(/THIS_IS_A_DOUBLE_CLOSED_SMART_QUOTE/ig, '&#148;');
		flash_string = flash_string.replace(/THIS_IS_A_BULLET_SYMBOL/ig, '&#149;');
		flash_string = flash_string.replace(/THIS_IS_A_SHORT_MDASH/ig, '&#150;');
		flash_string = flash_string.replace(/THIS_IS_A_LONG_MDASH/ig, '&#151;');
		flash_string = flash_string.replace(/THIS_IS_A_TRADEMARK_SYMBOL/ig, '&#153;');*/

		//alert(flash_string);


//flash_string = flash_string.replace(/\u0092/ig, '\u0092');
		//if (flash_string.match(/(\u0092|f|&#146;)/ig)) {
		//}

		/*flash_string = flash_string.replace(/?/, "\\u00AE");
		flash_string = flash_string.replace(/?/, "\\u00A9");
		flash_string = flash_string.replace(/?/, "\\u0099");
		flash_string = flash_string.replace(/?/, "\\u0092");
		flash_string = flash_string.replace(/?/, "\\u0097");
		flash_string = flash_string.replace(/?/, "\\u0091");
		flash_string = flash_string.replace(/?/, "\\u0093");
		flash_string = flash_string.replace(/?/, "\\u0094");
		flash_string = flash_string.replace(/?/, "\\u0085");
		flash_string = flash_string.replace(/?/, "\\u0096");	
*/

	
		//flash_string = flash_string.replace(/&lt;/gi, 'THIS_IS_A_LESS_THAN_SYMBOL');
		//flash_string = flash_string.replace(/&gt;/gi, 'THIS_IS_A_GREATER_THAN_SYMBOL');
		//flash_string = flash_string.replace(/%/gi, 'THIS_IS_A_PERCENT_SYMBOL');
		//flash_string = flash_string.replace(/&amp;+;/gi, 'THIS_IS_A_AMPERSAND_SYMBOL');
	}
	return flash_string
	/*
		//<a HREF="http://www.bendettoguitars.com" TARGET=""> </a>
		//$flash_string = preg_replace('/<a [^>]+> <.a>/i','\\u00A0', $flash_string);
		$flash_string = preg_replace('/<a[^>]+>(<[^>]+>)*.(<[^>]+>)*<.a>/i', '\\u00A0', $flash_string);
		$flash_string = preg_replace('/<br>$/i', '', $flash_string);
	*/
}

function flash_output(str) {
	alert(str);
}

function flash_updating(status) {
	//alert("status = "+status);
	if (status == "send_yes") {
		updating_fields = true;
	}
	else if (status == "send_no") {
		updating_fields = false;
	}
	else if (status == "split_yes") {
		splitting_fields = true;
	}
	else if (status == "split_no") {
		splitting_fields = false;
	}

	if (status == "yes") {
		byid("submit_top").disabled = true;
		byid("submit_bottom").disabled = true;
		byid("processing").style.display = "";
		
	}
	else {
		byid("submit_top").disabled = false;
		byid("submit_bottom").disabled = false;
		byid("processing").style.display = "none";
	}

}

function flash_text_populate(name, value, status) {
	changes_made = true;
	//alert("name = "+name+"\nvalue = "+value);
	if (byid("output")) {
		byid("output").value = "name = "+name+"\nvalue = "+value;
	}
	if (byid(name)) {
		if (status == "first") {
			byid(name).value = value;
		}
		else if (status == "save") {
			byid(name).value += value;
			//alert('global = '+global_str);
			global_str = "";
		}
		else {
			global_str = "";
			byid(name).value = value;
		}
	}
}


//This function formats the text according to the template
function format_text(id, location) {
	article_id = "article"+id;
	date_id = "datef"+id;
	summary_id = "summary"+id;
	main_title_id = "main_title"+id;
	article_text = "<html><head><title>Preview</title>"
	article_text += '<link href="../util/news.css" rel="stylesheet" type="text/css">';
	article_text +="</head><body onUnload='window.close()'>";
	//article_text += '<div style="text-align: justify">';
  article_text += '<img class="pfloat_left" src="../'+location+'/'+article_id+'_pic1.jpg" />';
	parsed_text = flash_parse(article_id);
	input_point = 300;
	if (parsed_text.length < input_point) {
		input_point = parsed_text.length;
	}
	else {
		input_point = parsed_text.length - input_point;
	}
	if (parsed_text.indexOf(">", input_point) != -1) {
		input_point = parsed_text.indexOf(">", input_point)+1;
	}
	
	article_text += parsed_text.substring(0, input_point);
	if (byid("template").value > 1) {
	  article_text += '<img class="pfloat_left" src="../'+location+'/'+article_id+'_pic2.jpg" />';
	}
	article_text += parsed_text.substring(input_point, parsed_text.length);
	//article_text += '</div>	';
	article_text += '</body></html>';
	//byid("output").value = article_text;
	return article_text;
}

//This function returns the current page name
function get_page() {
	url=location.href;
	url=url.split("/");
	url = url[url.length-1];	
	url = url.split("?");
	url = url[0];
	return url;
}

//This function expands a node and parents above it
function highlight_parents(id) {
	id_list = id.split(":");
	id_string = "";
  for (x=0; x<id_list.length; x++) {
		if (id_string == "") {
			id_string = id_string + id_list[x];
		}
		else {
			id_string = id_string + ":" + id_list[x];
		}
    if (byid(id_string)) {
  		byid(id_string).style.display = "block";
			if (byid("img"+id_string)) {
	      byid("img"+id_string).src=open_image;
			}
      expanduser_tracking[id_string] = "";
		}
		else if (byid("li"+id_string)) {
      expanduser_tracking[id_string] = "";
		}
    if (byid("table"+id_string)) {
  		byid("table"+id_string).style.backgroundColor = tree_highlight_bgcolor;
  		byid("href"+id_string).style.color = tree_texthighlight_color;			
		}
	}
}


//This function records that the mouse has been clicked in the flash window
function mouse_clicked() {
	flash_clicked = true;
	byid("editor_loaded").value = "yes"
	
}

//This function previews the text in a popup window
function preview(text) {
	w_close = '<center><a href="javascript:window.close()">Click here to close window<br>';
	preview_window = window.open("", "Preview","width=800,height=600,toolbar=1,scrollbars=1,resizable=1");
	text = w_close + text + "</a></center>";
	preview_window.document.write(text);
}


function template_mousedown(id) {
	for (x=1; x<=3; x++) {
		if (x == id) {
			byid("t"+x).style.backgroundColor=mousedown_color;
			selected_template = id;
		}
		else {
			if (byid("t"+x)) {
				byid("t"+x).style.backgroundColor=mouseout_color;
			}
		}
	}
}

//This function manages onmouseout for the templates
function template_mouseout(id) {
	byid("t"+id).style.backgroundColor=mouseout_color;
	if (byid("t"+selected_template)) {
		byid("t"+selected_template).style.backgroundColor=mousedown_color;
	}
}

//This function manages onmouseover for the templates
function template_mouseover(id) {
	byid("t"+id).style.backgroundColor = mouseover_color;
}

function template_switch(template_id) {
	template_small = "template_small_uploaded"+template_id;
	template_medium = "template_medium_uploaded"+template_id;
	template_large = "template_large_uploaded"+template_id;
	for (x=1; x<=3; x++) {
		temp_template_small = "template_small_uploaded"+x;
		temp_template_medium = "template_medium_uploaded"+x;
		temp_template_large = "template_large_uploaded"+x;
		if (byid(temp_template_medium)) {
			if (temp_template_small == template_small) {
				byid(temp_template_medium).style.display = "block";
				byid(temp_template_large).style.display = "block";
				byid("template").value = x;
			}
			else {
				byid(temp_template_medium).style.display = "none";
				byid(temp_template_large).style.display = "none";
			}
		}
	}
}

//this function parses the text field correctly when submitting to the database
function update_text(id) {
	article_id = "article"+id;
	summary_id = "summary"+id;
	main_title_id = "main_title"+id;
	title_id = "titlef"+id;
	author_id = "author"+id;
	location_id = "location"+id;
	date_id = "datef"+id;
	feature_title_id = "feature_title"+id;
	
	if (byid(feature_title_id)) {
		feature_title_text = flash_parse(feature_title_id);
		byid(feature_title_id).value = feature_title_text;
	}
	
	date_text = byid(date_id).value;
	date_text = date_text.replace(/<[^>]+>/ig, '');
	byid(date_id).value = date_text;
	

	article_text = flash_parse(article_id);
	byid(article_id).value = article_text;
	//alert("article text = "+article_text);

	summary_text = flash_parse(summary_id);
	byid(summary_id).value = summary_text;

	main_title_text = flash_parse(main_title_id);
	byid(main_title_id).value = main_title_text;

	title_text = flash_parse(title_id);
	byid(title_id).value = title_text;

	author_text = flash_parse(author_id);
	byid(author_id).value = author_text;

	location_text = flash_parse(location_id);
	byid(location_id).value = location_text;

	if ((updating_fields) || (splitting_fields)){
		return false;
	}
	else {
		return true;
	}
}




