function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  }
  else {
    window.onload = function() {
      if (oldonload) oldonload();
      func();
  	  }
  }
}

function setFirstFocus() {
	var ff;
	try {ff = document.getElementById('firstfocus');} catch(e) {}
	if (ff!=null) ff.focus();
}
addLoadEvent(setFirstFocus);

function setActiveStyleSheet(title){
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style")!=-1 && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

function addPrintButton() {
	if (document.getElementById){
		var print_page = document.getElementById('printLink');
		if (print_page==null) return;
		var print_function = document.createElement('input');
		print_function.onclick = function() {print_preview(); return false; }
		print_function.setAttribute('type','button');
		print_function.setAttribute('value','Print this page');
		print_page.appendChild(print_function);            
	}
}
addLoadEvent(addPrintButton);

function print_preview() {
    setActiveStyleSheet('Print Preview');
    add_preview_message();
    window.print();
}

function add_preview_message(){
	var main_content = document.getElementById('content');
	var main_body = main_content.parentNode;

    if (document.getElementById){

        var preview_message = document.createElement('div');
        preview_message.id = 'preview-message';

        // Create Heading
        var preview_header = document.createElement('h3');
        var preview_header_text = document.createTextNode('This is a print preview of the page');
        preview_header.appendChild(preview_header_text);

        // Create paragraph
        var preview_para = document.createElement('p');
        var preview_para_text = document.createTextNode('This box will not appear on the printout. ');
		preview_para.appendChild(preview_para_text);
		
		// Create print button
		var print_function_btn = document.createElement('input');
		print_function_btn.onclick = function() { window.print(); return false; };
		print_function_btn.setAttribute('type','button');
		print_function_btn.setAttribute('value','Print this page');

		// Create close button
		var cancel_function_btn = document.createElement('input');
		cancel_function_btn.onclick = function() { cancel_print_preview(); return false; };
		cancel_function_btn.setAttribute('type','button');
		cancel_function_btn.setAttribute('value','Close print preview');
		
        // Put it all together
        preview_message.appendChild(preview_header); 
        preview_message.appendChild(preview_para);
		preview_message.appendChild(print_function_btn);
		preview_message.appendChild(cancel_function_btn);
        main_body.insertBefore(preview_message, main_content);

    }
}

function cancel_print_preview() {
    // Destroy the preview message
    var print_preview = document.getElementById('preview-message');
    var main_body = print_preview.parentNode;
    main_body.removeChild(print_preview);

    // Switch back stylesheet
    setActiveStyleSheet('default');
}

function addManager() {
	var btnDiv;
	try {btnDiv = document.getElementById('addmanager');} catch(e) {}
	if (btnDiv==null) return;
	var btn = document.createElement('input');
	btn.onclick = function() {addManagerData();};
	btn.setAttribute('type','button');
	btn.setAttribute('value','Add the manager to this list');
	btnDiv.appendChild(btn);
	btnDiv.style.paddingBottom='30px';
	btnDiv.style.display='block';
}

function addManagerData() {
	var f = document.regform.elements;
	for (var a=1; a<=5; a++) {
		if (f['adult_0'+a+'_name'].value=='') {
			f['adult_0'+a+'_name'].value = f['first'].value + ' ' + f['last'].value;
			f['adult_0'+a+'_addr'].value = f['address1'].value + ' ' + f['address2'].value;
			f['adult_0'+a+'_city'].value = f['city'].value;
			f['adult_0'+a+'_state'].value = f['state'].value;
			f['adult_0'+a+'_zip'].value = f['zip'].value;
			f['adult_0'+a+'_email'].value = f['email'].value;
			return;
		}
	}
}

addLoadEvent(addManager);

function makeTheButtonsPretty() {
// An entirely frivolous script that finds form submit buttons of a certain class and rewrites
// them as beautiful butterflies. Changing the button to a link requires that the link uses
// JavaScript to submit the form, so it must be written that way using JavaScript so the default
// markup doesn't rely on JavaScript.

	var frms = document.forms;
	for (var f=0; f<frms.length; f++) {
		elms = frms[f].elements;
		for (e=0; e<elms.length; e++) {
			if (elms[e].className.toLowerCase()=='dear-god-make-me-a-bird' && 
				elms[e].type.toLowerCase()=='submit') {
				
				newDiv = document.createElement('p');
				newDiv.className='pretty-button';
				
				linkText = document.createTextNode(elms[e].value);
				descText = document.createTextNode(elms[e].title);

				newLink = document.createElement('a');
				newLink.setAttribute('href','javascript:document.'+frms[f].name+'.submit();');
				newLink.onclick=elms[e].onclick;
				newLinkB = document.createElement('strong');
				newLinkB.appendChild(linkText);
				newLink.appendChild(newLinkB);
				newDiv.appendChild(newLink);
				newDiv.appendChild(document.createElement('br'));
				newDiv.appendChild(descText);
		  		
				
				newFld = document.createElement('input');
				newFld.setAttribute('type','hidden');
				newFld.setAttribute('name',elms[e].name);
				newFld.setAttribute('value',elms[e].value);
				
				newDiv.appendChild(newFld);
				
				frms[f].insertBefore(newDiv,elms[e]);
				frms[f].removeChild(elms[e+1]);
				
			}
		}
	}
	

}
addLoadEvent(makeTheButtonsPretty);
