

function spellcheckpopup(textAreaId, quote_type, hide_errors) {
		var texttocheck = document.getElementById(textAreaId).value;
	texttocheck = texttocheck.replace(/"/g, "&quot;");

		var spellcheck_window = window.open('','spellcheckwindow', 'width=750,height=550,left=50,top=50,fullscreen=no,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,directories=no,location=no');

		spellcheck_window.document.write('<HTML><HEAD><\/HEAD><BODY>');
	spellcheck_window.document.write('<FORM NAME="spellcheckform" ID="spellcheckform" ACTION="spellcheck.php" METHOD="POST">');
	spellcheck_window.document.write('<INPUT TYPE="hidden" name="spellcheckthistext" VALUE="' + texttocheck +'">');
	spellcheck_window.document.write('<INPUT TYPE="hidden" name="quote_type" VALUE="' + quote_type + '">');
	spellcheck_window.document.write('<INPUT TYPE="hidden" name="hide_errors" VALUE="' + hide_errors + '">');
	spellcheck_window.document.write('<INPUT TYPE="submit" name="submitthis" VALUE="Please click this button, if necessary">');
	spellcheck_window.document.write('<\/FORM>');
	spellcheck_window.document.write('<\/BODY><\/HTML>');
	spellcheck_window.document.forms[0].submit();
}



function insertTags(textAreaId, openTag, closeTag, exampleText){ 	if (!textAreaId){					 		return false;
	}
	textArea=document.getElementById(textAreaId);		 
	var isExampleText=false;

	if (textArea.selectionStart || textArea.selectionStart == '0') { // Mozilla
		var scrollPos = textArea.scrollTop;	
		var startPos = textArea.selectionStart; 		var endPos = textArea.selectionEnd;			selectedText = textArea.value.substring(startPos, endPos);

		if (!selectedText) {						selectedText = exampleText;
			isExampleText = true;
		}else if (selectedText.charAt(selectedText.length - 1) == ' ') { 			selectedText = selectedText.substring(0, selectedText.length - 1);
			closeTag += ' '
		}

		textArea.value = textArea.value.substring(0, startPos)  			+ openTag + selectedText + closeTag
			+ textArea.value.substring(endPos, textArea.value.length);

		if ((isExampleText) && (exampleText)) {								textArea.selectionStart = startPos + openTag.length;					textArea.selectionEnd = startPos + openTag.length + selectedText.length;
		} else {											textArea.selectionStart = startPos + openTag.length + selectedText.length + closeTag.length;
			textArea.selectionEnd = textArea.selectionStart;
		}

		textArea.scrollTop = scrollPos;		
	}else if (document.selection && document.selection.createRange) { 
		if (document.documentElement && document.documentElement.scrollTop){				var winScroll = document.documentElement.scrollTop;
		}else if (document.body){
			var winScroll = document.body.scrollTop;
		}

		textArea.focus();		
		var range = document.selection.createRange();			selectedText = range.text;	
		if (!selectedText) {					selectedText = exampleText;
			isExampleText = true;
		}else if (selectedText.charAt(selectedText.length - 1) == ' ') { 			selectedText = selectedText.substring(0, selectedText.length - 1);
			closeTag += ' '
		}

		range.text = openTag + selectedText + closeTag;	 
		if (isExampleText && (exampleText) && range.moveStart) {				if (window.opera){
				closeTag = closeTag.replace(/\n/g,'');
			}
			range.moveStart('character', - closeTag.length - selectedText.length);
			range.moveEnd('character', - closeTag.length);
		}
		range.select();
		if (document.documentElement && document.documentElement.scrollTop){				document.documentElement.scrollTop = winScroll;
		}else if (document.body){
			document.body.scrollTop = winScroll;
		}
	}
}



function ttpos(event,tttb,tip){
	var tttb = document.getElementById(tttb);		var tt = document.getElementById(tip);			var ie = document.all ? true : false;			tttbTop = tttb.offsetTop;            			tttbLeft = tttb.offsetLeft;
	var i=0;
	while(tttb.offsetParent!=null) {
		tttbParent = tttb.offsetParent;
		tttbTop += tttbParent.offsetTop;
		tttbLeft += tttbParent.offsetLeft;
		tttb = tttbParent;
		i++;
		if (i > 1000){	//sanity-check			
			break;
		}
	}
	if (!event){
		var event = window.event;			}
	var u = ie ? event.clientY + document.documentElement.scrollTop : event.pageY;   	var l = ie ? event.clientX + document.documentElement.scrollLeft : event.pageX;

	tt.style.top = ((u - tttbTop) + 30) + 'px';
	tt.style.left = ((l - tttbLeft) + 30) + 'px';
}



function vertScrollToAnchor(anchorname){		obj = document.anchors[anchorname];		objTop = obj.offsetTop;            		while(obj.offsetParent!=null) { 			objParent = obj.offsetParent;  			objTop += objParent.offsetTop; 			obj = objParent;
	}
	if (objTop > 15){					objTop-=15;				}
	scroll (0,objTop);			}



function resizeTextAreaHeight (textArea, style, minLines, maxLines){
	MINLINES = 7;					
	MAXLINES = 40;  
	if (style == 'mobile'){					return (false);
	}
	if (minLines == undefined ){			
		minLines=MINLINES;			}
	if (maxLines == undefined ){
		maxLines=MAXLINES;
	}
	var cols=textArea.cols;
	var text=textArea.value;
	var x=0; var lines=1;		   		for (i=0; i < text.length; i++){   			x++;
		if (text.charAt(i)=="\n"){ 				lines++;
			x=0;
		}else if ( (x%(cols-1) == 0) && (x!=0) ){ 			lines++;
			/* This used to exclude long-lines (i.e. unless there's a space within COLS prev, or COLS next chars.) - why? 
			if ( (text.substring((i-cols),i).indexOf(' ') != -1) || (text.substring(i,(i+cols)).indexOf(' ') != -1) ){   
				lines++;
			}*/
		}
	}
	if (lines < minLines){
		lines=minLines;
	}else if (lines > maxLines){
		lines=maxLines;
	}
	textArea.rows=lines;     		}



function shrinkFontEliminateScrollbars(){
	NUM_LOOPS=3;				SCALE_FACTOR=0.85;			MAX_HOR_OVERSIZE=4;			MAX_VERT_OVERSIZE=1;								MIN_WINDOW_WIDTH=640;			MIN_WINDOW_HEIGHT=480;
	MIN_NEWFONT_PX=8;			MIN_NEWFONT_PT=6;		
	for (i=0; i< NUM_LOOPS; i++){												var body = document.getElementById('body');				var maintable = document.getElementById('maintable');	
		windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;     		windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight; 
		contentWidth =  maintable.offsetWidth || maintable.style.pixelWidth;     		contentHeight =  maintable.offsetHeight || maintable.style.pixelHeight;  
		if (window.getComputedStyle){							currentFontSize = document.defaultView.getComputedStyle(body,null).getPropertyValue("font-size");
								}else if (body.currentStyle){							currentFontSize =body.currentStyle["fontSize"];	 								}

		if ( (windowWidth == undefined) || (windowWidth == 0) || (windowHeight == undefined) || (windowHeight == 0) || (contentWidth == undefined) || (contentWidth == 0) || (contentHeight == undefined) || (contentHeight == 0) || (currentFontSize == undefined) || (currentFontSize == 0) ){
			return (false);							}

		if ( typeof (currentFontSize) == "number"){					fontSizeUnits="";
		}else{
			fontSizeUnits = currentFontSize.substr(-2);
			currentFontSize = currentFontSize.substr(0,currentFontSize.length -2);
		}

		newFontSize = (currentFontSize * SCALE_FACTOR)  	
		if (  ( contentWidth > windowWidth ) && ( contentWidth < MAX_HOR_OVERSIZE * windowWidth ) &&  ( windowWidth >= MIN_WINDOW_WIDTH )  ){   			shrink = true;
		}else if (  ( contentHeight > windowHeight ) &&  (contentHeight < MAX_VERT_OVERSIZE * windowHeight ) && ( windowHeight >= MIN_WINDOW_HEIGHT )  ){   			shrink = true;
		}else{
			shrink = false;
		}

		if ( ( ( fontSizeUnits == "px" ) || ( fontSizeUnits == "" ) ) &&  ( newFontSize < MIN_NEWFONT_PX )  ){   			shrink = false;
		}else if (  ( fontSizeUnits == "pt" )  &&  ( newFontSize  < MIN_NEWFONT_PT )  ){							shrink = false;
		}

		if (shrink == true){								newFontSize = newFontSize + fontSizeUnits;							maintable.style.fontSize = newFontSize;				}else{
			return (false);	  						}
	}
}

