function open_win(URL,ats){
		window.open(URL,"newwindow",ats,"false");}

function open_win2(URL,winname,ats){
		window.open(URL,winname,ats);}
			
function GoURL(s) {
	var d = s.options[s.selectedIndex].value
	if (d != '') {
		window.location.href = d
		s.selectedindex = 0
	}
}

function GoURLpop(s) {
	var d = s.options[s.selectedIndex].value;

	if (d.substr(0,4) == 'www.') {
		d = 'http://' + d;
	}
	
	if (d != '') {
		window.open(d,"_blank","width=800,height=600,scrollbars=yes,resizable=yes,toolbar=yes,menubar=yes","false");
		s.selectedindex = 0
	}
}

function GoURLpop2(s) {

	if (s.substr(0,4) == 'www.') {
		s = 'http://' + s;
	}
	
	if (s != '') {
		window.open(s,"_blank","width=800,height=600,scrollbars=yes,resizable=yes,toolbar=yes,menubar=yes","false");
	}
	
	return false;
}

// AUTHENTICATION //
function valid(signinform) {
	var username = signinform.elements['cn'];
	var pwd = signinform.elements['userpassword'];
  if (username.value == "") {
    alert("Please enter a username");
		username.focus();
		return false;
  }
	if (username.value.split(" ").length < 2) {
		alert("Please check your username (usernames are full names)");
		username.focus();
		return false;
	}
  if (pwd.value == "") {
    alert("Please enter a password");
		pwd.focus();
		return false;
  }
}

function confirmGoPopup(URL,ats,msg) {
	if (confirm(msg))
		open_win(URL,ats);
}

function confirmGoURL(URL,msg) {
	if (confirm(msg))
		location.replace(URL);
}

function confirmGoForm(msg) {
	if (confirm(msg)) {
		return true;
	} else {
		return false;
	}
}

// FORM VALIDATION //

// sets the maxlength on a text area
// open /goals/dsp_goal.cfm to see this function put to use
function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

// FORM FORMATTING //

// insertcode is used for bold, italic, underline and quote and just
// wraps the tags around a selection or prompts the user for some
// text to apply the tag to
function wrapcode(tag, desc, formElement)
{
    // our textfield
    var textarea = document.getElementById(formElement);

    // our open tag
    var open = "<" + tag + ">";

    // our close tag
    var close = "</" + tag + ">";

		var selected = document.selection.createRange().text; 
		if(selected.length <= 0)
		{ 
				// no text was selected so prompt the user for some text
				textarea.value += open + prompt("Please enter the text you'd like to " + desc, "") + close;
		}
		else
		{
				// put the code around the selected text
				document.selection.createRange().text = open + selected + close; 
		}

    // set the focus on the text field
    textarea.focus();
}

function insertcode(tag, formElement) {

    // our textfield
    var textarea = document.getElementById(formElement);

		textarea.focus();
		
		sel = document.selection.createRange();

    sel.text = tag;

}