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 print_preview() {
    // Switch the stylesheet
    setActiveStyleSheet('Print Preview');

    // Create preview message
    add_preview_message();

    // Print the page
    window.print();
}

function add_preview_message(){
var main_content = document.getElementById('body-wrapper');
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 preview of how this page will look when printed.');
        preview_header.appendChild(preview_header_text);

        // Create paragraph
        var preview_para = document.createElement('p');
//        var preview_para_text = document.createTextNode ('Without this message of course. ');

        var cancel_function_link = document.createElement('a');
            cancel_function_link.onclick = function() { cancel_print_preview(); return false; };
            cancel_function_link.setAttribute('href', '#');    
        var cancel_function_link_text = document.createTextNode('Close preview and return to page.');
        cancel_function_link.appendChild (cancel_function_link_text);
//        preview_para.appendChild(preview_para_text); //
        preview_para.appendChild(cancel_function_link);

		var print_logo = document.createElement('img');
		print_logo.id = 'print-logo';
		print_logo.setAttribute('src', '/images/layout/header-logo-print.png');

        // Put it all together
        preview_message.appendChild(preview_header); 
        preview_message.appendChild(preview_para);
        main_body.insertBefore(preview_message, main_content);
        main_body.insertBefore(print_logo, main_content);
    }
}

function cancel_print_preview() {
    // Destroy the preview message
    var print_preview = document.getElementById('preview-message');
    var print_logo = document.getElementById('print-logo');
    var main_body = print_preview.parentNode;
    main_body.removeChild(print_preview);
    main_body.removeChild(print_logo);

    // Switch back stylesheet
    setActiveStyleSheet('default');
}

function copy_to_clipboard(id) {
  text2copy = document.getElementById(id).innerHTML
  if (window.clipboardData) {
    window.clipboardData.setData("Text",text2copy);
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="flash/_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
    }
}

function copy_to_email(id) {
    text2copy = document.getElementById(id).innerHTML
	text2copy = text2copy.replace(/\&amp\;/g,'&')
	target = window.opener.document.main.history_body
	insertAtCursor(target, text2copy + "\n")
//	if(target.value == '') { target.value = text2copy }
//	else { target.value = target.value + "\n\n" + text2copy }
//	target.scrollTop = target.scrollHeight
}

//function insertAtCursor(myField, myValue) {
//  //IE support
//  if (document.selection) {
//    myField.focus();
//    sel = document.selection.createRange();
//    sel.text = myValue;
//  }
//
//  //MOZILLA/NETSCAPE support
//  else if (myField.selectionStart || myField.selectionStart == '0') {
//    var startPos = myField.selectionStart;
//    var endPos = myField.selectionEnd;
//    myField.value = myField.value.substring(0, startPos)
//                  + myValue
//                  + myField.value.substring(endPos, myField.value.length);
//  } else {
//    myField.value += myValue;
//  }
//}


// thanks to http://parentnode.org/javascript/working-with-the-cursor-position/
function insertAtCursor(obj, text) { 
    if(document.selection) { 
        obj.focus(); 
        var orig = obj.value.replace(/\r\n/g, "\n"); 
        var range = document.selection.createRange(); 
 
        if(range.parentElement() != obj) { 
            return false; 
        } 
 
        range.text = text; 
         
        var actual = tmp = obj.value.replace(/\r\n/g, "\n"); 
 
        for(var diff = 0; diff < orig.length; diff++) { 
            if(orig.charAt(diff) != actual.charAt(diff)) break; 
        } 
 
        for(var index = 0, start = 0;  
            tmp.match(text)  
                && (tmp = tmp.replace(text, ""))  
                && index <= diff;  
            index = start + text.length 
        ) { 
            start = actual.indexOf(text, index); 
        } 
    } else if(obj.selectionStart) { 
        var start = obj.selectionStart; 
        var end   = obj.selectionEnd; 
 
        obj.value = obj.value.substr(0, start)  
            + text  
            + obj.value.substr(end, obj.value.length); 
    } 
     
    if(start != null) { 
        setCursorTo(obj, start + text.length); 
    } else { 
        obj.value += text; 
    } 
} 
 
function setCursorTo(obj, pos) { 
    if(obj.createTextRange) { 
        var range = obj.createTextRange(); 
        range.move('character', pos); 
        range.select(); 
    } else if(obj.selectionStart) { 
        obj.focus(); 
        obj.setSelectionRange(pos, pos); 
    } 
} 

function showHideForgotPassword() {
	text = document.getElementById('forgotPassword')
	if(text.style.display == 'none') {
		text.style.display = 'block'
	} else {
		text.style.display = 'none'
	}
}

function password_strength()
{
	$("#password_strength").load("ajax/password_strength.php", { 'password' : document.getElementById('password').value } );
}

function showhide(id)
{
	obj = document.getElementById(id)
	if(obj.style.display == 'none') {
		obj.style.display = 'block'
	} else {
		obj.style.display = 'none'
	}
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
// from http://www.somacon.com/p143.php
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function checkSecurity()
{
	// if the response type has already been filled out, go ahead and submit the form
	if(getCheckedValue(document.forms['main'].elements['contact'])) {
		return true;
	} else {
		// get the user's answers to the security questions & sum them as a score
		score = 0;
		for(i=1; i<=6; i++) {
			score += Number(getCheckedValue(document.forms['main'].elements['q'+i]));
		}
					
		if(score > 0) {
			msg = "You checked at least one <strong>Yes</strong> to the security questions, so your email may not be safe. We suggest that you not receive any emails at this address; choose the first option and log back in to the website to check the responses to your question.";
		} else {
			msg = "You answered all <strong>No's</strong> to the security questions, so your email appears to be safe.&nbsp; You may still prefer to limit the email that you receive and read your response on a private and secure webpage instead.";
		}
		document.getElementById('contact-method-msg').innerHTML = msg;
		document.getElementById('contact-method-box').style.display = 'block';
		//document.getElementById('security-questions').style.display = 'none';
		return false;
	}
}