function countChars(textarea) {
	var max_chars = 10000;
	var num_chars = max_chars - textarea.value.length;
	textarea.style.color = (num_chars < 0 ) ? "red" : "";
	document.getElementById('textarea-status').innerHTML = Math.abs(num_chars) + " character" + ((Math.abs(num_chars) == 1) ? "" : "s") + " " + ((num_chars < 0) ? "over" : "left");
	document.getElementById('textarea-status').style.color = (num_chars < 0 ) ? "red" : "";
	textarea.form.onsubmit = (num_chars < 0 ) ? function(){ alert('You have entered more than the maximum number of characters allowed. Please fix.'); return false; } : function(){ } ;
}

function showLinkForm( linkurl, htmlid ) {
      htmlcode ='';
//    htmlcode += '<br/><span id="linkurl">link to this here:<br/><a href="'+linkurl+'>'+linkurl+'</a><br/></span><br/>';
      htmlcode += '<br/><span id="linkurl">link to this here:<br /><br />';
      htmlcode += '<input type=text size=40 value="'+linkurl+'" /></span><br /><br />';
      htmlcode += '<a href="" onClick="document.getElementById( \'formholder\' ).style.display=\'none\'; return false">close</a>';
        //alert( 'before:'+document.getElementById(htmlid).innerHTML);
      document.getElementById(htmlid).innerHTML = htmlcode;
        //alert( 'after:'+document.getElementById(htmlid).innerHTML);
      document.getElementById(htmlid).style.display = 'inline';
}

String.prototype.toUnicode = function() {
	return this.replace(/(.)/gi,
		function (matched_substring, paren1, match_offset, orig_string) {
			return ( (paren1.charCodeAt(0) >= 160) ? "&#x" + paren1.charCodeAt(0).toString(16) + ";" : paren1);
		}
	);
}


function convertFormToUnicode(f) {
	for (var i=0; i < f.elements.length; i++) {
		if ( (f.elements[i].type == "text") || (f.elements[i].type == "textarea")) {
			f.elements[i].value = f.elements[i].value.toUnicode();
		}
	}
}

function resizeLargeimage(img) {

 if (img.offsetWidth == 0 || img.offsetHeight == 0) {
// returns image without resizing, if there is a problem getting size - as with bug on IE when first loading
     return false;

 } else {

        var img_width = img.offsetWidth;
        var img_height = img.offsetHeight;
        var img_aspect_ratio = Math.round((img_width / img_height) * 100) / 100;

        var max_width = 300;
        var max_height = 225;
        var max_aspect_ratio = Math.round((max_width / max_height) * 100) / 100;

      // alert("image is " + img.src + " orig image size is " + img_width + "x" + img_height + "\n" + "aspect ratio is " + img_aspect_ratio + "\n\n" + "max image size is " + max_width + "x" + max_height + "\n" + "max aspect ratio is " + max_aspect_ratio);

        var new_img_width = 0;
        var new_img_height = 0;
        var new_aspect_ratio = 0;

        // if no resize needed
        if (img_width < 300 && img_height < 225) {
                new_img_width = img_width;
                new_img_height = img_height;

        // if wider
        } else if (img_aspect_ratio > max_aspect_ratio) {
                new_img_width = max_width;
                new_img_height = Math.round(new_img_width / img_aspect_ratio);

        // if taller
        } else if (img_aspect_ratio < max_aspect_ratio) {
                new_img_height = max_height;
                new_img_width = Math.round(new_img_height * img_aspect_ratio);

        // equal
        } else {
                new_img_width = max_width;
                new_img_height = max_height;
        }

        img.style.width = new_img_width + "px";
        img.style.height = new_img_height + "px";
        new_aspect_ratio = Math.round((new_img_width / new_img_height) * 100) / 100;

//      alert("new image size is " + new_img_width + "x" + new_img_height + "\n" + "new aspect ratio is " + new_aspect_ratio);
}
}
