function toggleSection(id){
	var content = document.getElementById(id+"_content");
	var toggle = document.getElementById(id+"_toggle");
	var restore = document.getElementById(id+"_restore");
	var section = document.getElementById(id);
	
	
	
	if (content && toggle && section){
	
		var content_type = (section.parentNode.className == "image_content") ? "image" : "section";

		if (toggle.className == "close"){
			toggle.className = "restore";
			content.style.display = "none";
			
			
			if (!restore){
				var section_name = id;
				restore = document.createElement("DIV");
				restore.id = id+"_restore";
				restore.className = "restore_placeholder";
				for (var i=0; i<content.childNodes.length; i++){
					if (content.childNodes[i].tagName == "H3"){
						section_name = content.childNodes[i].innerHTML;
						continue;
					}
				}
				
				if (content_type == "section"){
					restore.innerHTML = "<h3><a href=\"#\" onclick=\"toggleSection('"+id+"');return false;\">Restore " + section_name + "</a> <span>(this line will not print)</span></h3>";
				}else{
					restore.innerHTML = "<h3><a href=\"#\" onclick=\"toggleSection('"+id+"');return false;\">Restore Image</a>";
				}
				
				content.parentNode.appendChild(restore);
			}else{
				restore.style.display = "";
			}
			section.className = content_type + " " + content_type + "_restore";
		}else{
			toggle.className = "close";
			content.style.display = "block";
			if (restore){
				restore.style.display = "none";
			}
			section.className = content_type;
		}
	}
	checkReport();
}


function deleteImage(link){

	var id = null;
	var parent_node = link;
		while (parent_node.className != 'image' && parent_node.tagName != "BODY"){
			parent_node = parent_node.parentNode
		}

		if (parent_node.className == 'image'){
			id = parent_node.id;
		}
	if (id){
		
	var section = document.getElementById(id);
	if (section){
		section.parentNode.removeChild(section);
	}
	checkReport();
	}else{
		alert("error deleting image --- " + id);
	}
}

function change_image(target_id,propkey,link,id){

if (document.getElementById("img_"+id)){
	alert("sorry, but you cannot add that image, it is already there");
	return;
}

	var div = document.getElementById("img_"+target_id);
	var images = div.getElementsByTagName("IMG");
	image = images[0];
	
	if (image){
		var link_images = link.getElementsByTagName("IMG");
		image.src = link_images[0].src;
		//image.height = '';
	}
	general_popuphide(link);
	if (div){
		div.id = "img_"+id
		div.style.display = "";
	}
	checkReport();
}

function get_available_images(link,propkey,action,id){
	var url = '/mason/OnePageReports/components/images.html?action=' + action + '&propkey=' + propkey + '&images=' + get_images_str();
	if (action == 'change_image'){
		var parent_node = link;
		while (parent_node.className != 'image' && parent_node.tagName != "BODY"){
			parent_node = parent_node.parentNode
		}

		if (parent_node.className == 'image'){
			url += "&id=" + parent_node.id.replace('img_','');
		}
	}
	
	popupshow(link, url, 'type=ajaxLoader', 'width=640', 'align=center', 'valign=middle', 'call_event=click', 'stayopen', 'popup_drag=1', 'title=Images');
}

function prepare_email(link,propkey,title){
	var url = '/mason/OnePageReports/components/mail.html?propkey=' + propkey + '&title=' + title;
	popupshow(link, url,'type=ajaxLoader','call_event=click','align=center','valign=middle','title=Email This Report to a Friend', 'width=480');

}

function send_mail(link,propkey){

	
	var url = '/mason/OnePageReports/report.html?format=email&propkey=' + propkey + "&sections=" + get_sections_str() + "&images=" + get_images_str() + get_more_less_hide_str();

	var fields = ["sender","from","to","message"];
	for (var i=0; i<fields.length; i++){
		if (document.getElementById("send_mail_" + fields[i]) && document.getElementById("send_mail_" + fields[i]).value){
			url += "&" + fields[i] + "=" + document.getElementById("send_mail_" + fields[i]).value;
		}
	}
	
	var s = new AjaxService();
	s.setURL(url);
	//window.open(url);
	s.setDelegate(function(r){
		var send_div = document.getElementById("mail_form_send");
		send_div.innerHTML = r;
	});
	s.callService();
	var send_div = document.getElementById("mail_form_send");
	var mail_form = document.getElementById("mail_form");
	if (!send_div){
		send_div = document.createElement("DIV");
		var height = (mail_form.height || mail_form.offsetHeight)
		send_div.id = "mail_form_send";
		send_div.style.height = height - 60 + "px";
		mail_form.parentNode.appendChild(send_div);
	}
	send_div.innerHTML = "Sending mail";
	send_div.style.display = "block";
	mail_form.style.display = "none";
}

function close_email(){
		var send_div = document.getElementById("mail_form_send");
		var mail_form = document.getElementById("mail_form");
		general_popuphide(document.getElementById("mail_form"));
		mail_form.style.display = "block";
		send_div.style.display = "none";
}

function toggle_check(id){
	var c = document.getElementById(id);
	c.checked =! c.checked;
}

function set_checked(){
	var inputs = document.getElementsByTagName("INPUT");
	var checked = [];
	
	for (var i=0; i<inputs.length; i++){
		if (inputs[i].className == "include_img"){
			if (inputs[i].checked){
				document.getElementById(inputs[i].id.replace('include_','select_')).className="checked";
			}else{
				document.getElementById(inputs[i].id.replace('include_','select_')).className="";
			}
		}
	}
}


function set_selected_images(link,propkey){
	// get all checkboxes
	var inputs = document.getElementsByTagName("INPUT");
	var checked = [];
	
	for (var i=0; i<inputs.length; i++){
		if (inputs[i].className == "include_img"){
			if (inputs[i].checked){
				checked.push(inputs[i].id.replace('include_',''));
			}
		}
	}
	
	var image_tools = document.getElementById('image_tools');
	var imgs = document.body.getElementsByTagName("DIV");
	
	
	for (var i=imgs.length-1; i>=0; i--){
		if (imgs[i].className == "image" && imgs[i].id != "image_tools"){
			var image = imgs[i];
			image.parentNode.removeChild(image);
		}
	}
	

	
	
	
	for (var i=0; i<checked.length; i++){
	
		var container = document.getElementById("select_" + checked[i]);
		var src = container.getElementsByTagName("IMG")[0].src;
	
		var div = document.createElement("DIV");
		
		div.className = "image";
		
		div.id = checked[i];

		
		div.innerHTML = "<a href=\"#\" onclick=\"get_available_images(this, " + propkey + ", 'update_images');return false;\"><img src=\"" + src + "\" width=\"198\" /></a>";
		
	
		image_tools.parentNode.appendChild(div);
	
	}
	
	general_popuphide(link);
	

	checkReport();
	


}

function add_image(id,propkey,link){

var div = document.getElementById("img_"+id);
if (div){
	alert("sorry, but you cannot add that image again");
	return;
}
	var image_tools = document.getElementById('image_tools');
	var link_images = link.getElementsByTagName("IMG");
	var src = link_images[0].src;
	
	if (image_tools){
	
		var n = id;
		
		var div = document.createElement("DIV");
		
		div.className = "image";
		
		div.id = "img_"+n;

		
		div.innerHTML = "<a href=\"#\" onclick=\"get_available_images(this, " + propkey + ", 'change_image','" + id + "'); return false;\"><img id=\"img_"+n+"_content\" src=\"" + src + "\" width=\"198\" /></a>";
		
		var tools = document.createElement("DIV");
		tools.className = "image_tools";
		tools.innerHTML = "<a href=\"#\" onclick=\"get_available_images(this, " + propkey + ", 'change_image','" + id + "'); return false;\">Change</a>";
		tools.innerHTML += " ";					
		tools.innerHTML += "<a href=\"#\" onclick=\"deleteImage(this); return false;\">Delete</a>";
		div.appendChild(tools);
		


		
		image_tools.parentNode.insertBefore(div,image_tools);

							
		general_popuphide(link);
	
		checkReport();
		
	}
}


function set_sections(link){
	location.href = link.href + "&sections=" + get_sections_str() + "&images=" + get_images_str() + get_more_less_hide_str();
}

function get_sections_str(){
	var divs = document.body.getElementsByTagName("DIV");
	var open_sections = [];
	for (var i=0; i<divs.length; i++){
		if (divs[i].className == "section"){
			open_sections.push(divs[i].id);
		}
	}
	
	return open_sections.join("|");
}

function get_more_less_hide_str(){
	var divs = document.body.getElementsByTagName("DIV");
	var str = "";
	var exp_status_more = [];
	var exp_status_less = [];
	var exp_status_hide = [];
	for (var i=0; i<divs.length; i++){
		if (divs[i].className == "exp_status_more"){
			exp_status_more.push(divs[i].id);
		}
		if (divs[i].className == "exp_status_less"){
			exp_status_less.push(divs[i].id);
		}
		if (divs[i].className == "exp_status_hide"){
			exp_status_hide.push(divs[i].id);
		}
	}
	
	if (exp_status_more.length){
		str += "&more=" + exp_status_more.join("|");
	}
	if (exp_status_less.length){
		str += "&less=" + exp_status_less.join("|");
	}
	if (exp_status_more.length){
		str += "&hide=" + exp_status_hide.join("|");
	}
	
	return str;
}

function get_images_str(){
	var divs = document.body.getElementsByTagName("DIV");

	var open_images =[];
	for (var i=0; i<divs.length; i++){

		if (divs[i].className == "image" && divs[i].id != "image_tools"){
			open_images.push(divs[i].id);
		}
	}
	
	return open_images.join("|");
}

window.onload = checkReport;

function checkReport(){



var max_height = 900;


	var tools = document.getElementById("tools");


	var header = document.getElementById("header");
	var header_height = (header.height || header.offsetHeight);
	
	// check the height of the divs
	
	var arr_main_content_sections = [];
	arr_main_content_sections[0] = [];
	
	var divs = document.body.getElementsByTagName("DIV");
	var divs_height = 0
	
	for (var i=0; i<divs.length; i++){
		if (divs[i].className == "section" || divs[i].className == "section section_restore"){
			var section = divs[i];
			var section_height = 0;
			if ( divs[i].className == "section" ) {
				section_height = (section.height || section.offsetHeight)
				section_height += parseInt(getStyle(section,"marginTop")) + parseInt(getStyle(section,"marginBottom"));
				divs_height += section_height;
				//status.innerHTML += divs[i].id + " is " + section_height + " and the total is now " + divs_height + "<br>";
				if ( (i==0 && divs_height>max_height-header_height) || divs_height>max_height){
					arr_main_content_sections[arr_main_content_sections.length]=[];
					divs_height = section_height;
					//status.innerHTML += "<hr>";
				}
			}
			//var old_section = section.parentNode.removeChild(section);
			arr_main_content_sections[arr_main_content_sections.length-1].push(section);
		}
	}
	
	// check the height of the images
	
	var arr_images_content_sections = [];
	arr_images_content_sections[0] = [];
	
	var imgs = document.body.getElementsByTagName("DIV");
	var imgs_height = 0
	
	for (var i=0; i<imgs.length; i++){
		if (imgs[i].className == "image" || imgs[i].className == "image image_restore"){
			var image = imgs[i];
			var image_height = 0;
			if ( imgs[i].className == "image" && imgs[i].id != "image_tools" ) {
				image_height = (image.height || image.offsetHeight)
				image_height += parseInt(getStyle(image,"marginTop")) + parseInt(getStyle(image,"marginBottom"));
				imgs_height += image_height;
			//	status.innerHTML += imgs[i].src + " is " + image_height + " and the total is now " + imgs_height + "<br>";
				if ( (i==0 && imgs_height>max_height-header_height) || imgs_height>max_height){
					arr_images_content_sections[arr_images_content_sections.length]=[];
					imgs_height = image_height;
					//status.innerHTML += "<hr>";
				}
			}
		//	alert(image.id)
			arr_images_content_sections[arr_images_content_sections.length-1].push(image);
		}
	}
	
	var pages = document.getElementById("pages");
	
	if (arr_main_content_sections.length>1 || arr_images_content_sections.length>1){
		pages.innerHTML = "Report will print on " + Math.max(arr_main_content_sections.length,arr_images_content_sections.length) + " pages";
		pages.style.display = "block";
	}else{
		pages.innerHTML = "";
		pages.style.display = "none";
	}
//	status.innerHTML += "<hr>based on divs we need to split this into " + arr_main_content_sections.length + " pages<br>";
//	status.innerHTML += "<hr>based on images we need to split this into " + arr_images_content_sections.length + " pages<br>";
	
	var current_tables = 0;
	
	// Distribute content
	for (var m=0; m<divs.length; m++){
		if (divs[m].className == "content"){
			var content = divs[m];
			for (var i=0; i<content.childNodes.length; i++){
				if (content.childNodes[i].tagName == "TABLE"){
					var tds = content.childNodes[i].getElementsByTagName("TD");
					var delete_page = true;
					for (var j=0; j<tds.length; j++){
						if (tds[j].className == "main_content"){
							if (current_tables < arr_main_content_sections.length){
								for (var k=0; k<arr_main_content_sections[current_tables].length;k++){
									tds[j].appendChild(arr_main_content_sections[current_tables][k])
								}
								delete_page = false;
							}
						}
						
						if (tds[j].className == "image_content"){
							if (current_tables < arr_images_content_sections.length){
								for (var k=0; k<arr_images_content_sections[current_tables].length;k++){
									tds[j].appendChild(arr_images_content_sections[current_tables][k])
								}
								delete_page = false;
							}
						}
					
					}
					if (delete_page){
						content.parentNode.parentNode.removeChild(content.parentNode);
					}
					current_tables++;
				}
			}
		}
	}
	
	for (var i=current_tables; i<Math.max(arr_main_content_sections.length,arr_images_content_sections.length);i++){
		var page = document.createElement("DIV");
		page.className = "page extra_page";
		var content = document.createElement("DIV");
		content.className = "content";
		var table = document.createElement("TABLE");
		var tbody = document.createElement("TBODY");
		var tr = document.createElement("TR");
		var main_content = document.createElement("TD");
		main_content.className = "main_content";
		if (arr_main_content_sections[i]){
			for (var k=0; k<arr_main_content_sections[i].length;k++){
				main_content.appendChild(arr_main_content_sections[i][k])
			}
		}
		tr.appendChild(main_content);
		
		var image_content = document.createElement("TD");
		if (arr_images_content_sections[i]){
			for (var k=0; k<arr_images_content_sections[i].length;k++){
				image_content.appendChild(arr_images_content_sections[i][k])
			}
		}
		image_content.className = "image_content";
		tr.appendChild(image_content);
		
		tbody.appendChild(tr)
		table.appendChild(tbody)

		table.style.pageBreakBefore="always";

		content.appendChild(table);
		page.appendChild(content);
		document.body.appendChild(page);
	}
	
	for (var i=0; i<divs.length; i++){
		if (divs[i].className == "popup_outer"){
			divs[i].parentNode.removeChild(divs[i]);
		}
	}
	
//	document.body.appendChild(status);
}


function getStyle(el, cssprop){
 if (el.currentStyle) //IE
  return el.currentStyle[cssprop]
 else if (document.defaultView && document.defaultView.getComputedStyle) //Firefox
  return document.defaultView.getComputedStyle(el, "")[cssprop]
 else //try and get inline style
  return el.style[cssprop]
}

function checkDuplicates(){
	var divs = document.body.getElementsByTagName("DIV");
	var c = 0;
	for (var i=0; i<divs.length; i++){
		if (divs[i].id == 'image_tools'){
			c++;
		}
	}
	return c;
}
