String.prototype.trim=function(){
    return this.replace(/^\s*|\s*$/g,'');
}

function isEmpty(val) {
	tval = val.trim();
	if (tval == null || tval == "") return true;
	return false;
}

function validateSubmitWord (form) {
	if (isEmpty(form.wordlit.value)) {
		alert ("You forgot to give us an unword for your definition.");
		return false;
	}
	if (isEmpty(form.define.value)) {
		alert ("You must give us a definition for your unword.");
		return false;
	}
	if (isEmpty(form.security_text.value)) {
		alert ("Please enter the security code from the image on this page.");
		return false;
	}
	if (!form.agree.checked) {
		alert ("You must agree to our policies to submit unwords.\nPlease check the agreement checkbox before submitting.");
		return false;
	}
	
	return true;
}

function insertAtCursor(myField, myValue) {
	//IE support
	if (document.selection) {
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	} else if (myField.selectionStart || myField.selectionStart == '0') {
	// MOZILLA/NETSCAPE support
		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;
	}
	myField.focus();
}

function jiggleSecurity (seckey) {
	if (!document.getElementsByTagName) return true;
	var images = document.getElementsByTagName("img");
	var repImg = document.createElement('img');
	var x = Math.floor(Math.random()*5000);
	for (var i=0; i<images.length; i++) {
   		var image = images[i];
   		if (image.getAttribute("id") == "security") {
   			if (seckey == "loading") image.src = '/images/loading.png';
   			else image.src = '/images/security.png?rand='+x+'&key=' + seckey;
		}
	}
	return true;
}

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "external" || anchor.getAttribute("rel") == "help"))
     anchor.target = "_blank";
 }
}

window.onload = externalLinks;