
var txtField = document.createElement("textarea");
//BBcode btns
var B = document.createElement("input");
var U = document.createElement("input");
var QUOTE = document.createElement("input");
var URL = document.createElement("input");
var IMG = document.createElement("input");
var tagcount = document.createElement("input");
var helpbox = document.createElement("input");
var emoticonShow ;
var bPremium;
var bSubDir;
var strSubDir;


var fombj;		
if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
	fombj = document.Form1;
}
else {
	fombj = document.forms["Form1"];
}	

function postComment(postType,parentID){
	ajaxpack.postAjaxRequest(strSubDir+'ajaxPost.aspx', 'function=update&postType='+postType+'&parentID='+parentID+'&txtPost='+escape(txtField.value), updatePagePost, 'txt');
}

function getCommentForm(postType,parentID,premium,bSubDirectory) { 

	bPremium = premium;
	bSubDir = bSubDirectory;
	strSubDir='';
	if (bSubDir) {strSubDir = '../';}
	ajaxpack.getAjaxRequest(strSubDir+'ajaxPost.aspx', 'function=render&parentID='+parentID+'&postType='+postType, updatePage, 'txt');
	
	//hide post comment link
	var div = document.getElementById("divPostCommentLink");
	div.className= 'hidden'; 
	var div2 = document.getElementById("divPostCommentLink2");
	div2.className= 'hidden'; 
	
}
	

function postPreview(){		
		//ajaxpack.getAjaxRequest('/matchdoctor/ajaxComment.aspx', 'post='+escape(txtField.value), updatePreview, 'txt');	
		ajaxpack.getAjaxRequest(strSubDir+'ajaxPost.aspx', 'function=preview&txtPost='+escape(txtField.value), updatePreview, 'txt');	
		
		
		//var txtTagCount = document.createElement("span");
		//txtTagCount.innerHTML = "<b>Open Tags:</b>";
		//divBB.appendChild(txtTagCount);
		//divBB.appendChild(tagcount);
}


function updatePage_old() {
   var myajax = ajaxpack.ajaxobj;
   var myfiletype = ajaxpack.filetype;	
   if (myajax.readyState == 4){
      //alert("Server is done!");
		var div = document.getElementById("divPostComment");
		div.innerHTML =  myajax.responseText;
		
		var divTxt = document.getElementById("divTxtArea");
		txtField.setAttribute("name","txtPost");
		txtField.setAttribute("rows","17");
		txtField.setAttribute("cols","100");
		divTxt.appendChild(txtField);

		//add BBcode btn bar.. 
		var divBB = document.getElementById("divBBcode");
		B = document.getElementById("B");  
		I = document.getElementById("I"); 
		U = document.getElementById("U"); 
		QUOTE = document.getElementById("QUOTE"); 
		URL = document.getElementById("URL"); 
		IMG = document.getElementById("IMG"); 
		tagcount = document.getElementById("tagcount");
		helpbox = document.getElementById("helpbox");
		
		B.onclick = function(){simpletag('B')};
		I.onclick = function(){simpletag('I')};
		U.onclick = function(){simpletag('U')};
		QUOTE.onclick = function(){simpletag('QUOTE')};
		URL.onclick = function(){tag_url()};
		IMG.onclick = function(){tag_image(1)};
		
		B.onmouseover = function(){hstat('bold')};
		I.onmouseover = function(){hstat('italic')};
		U.onmouseover = function(){hstat('under')};
		QUOTE.onmouseover = function(){hstat('quote')};
		URL.onmouseover = function(){hstat('url')};
		IMG.onmouseover = function(){hstat('image')};
		
		
		loadEmoticon('emoticons');
		
		scrollBottom();
      }
}

function updatePage() {
   var myajax = ajaxpack.ajaxobj;
   var myfiletype = ajaxpack.filetype;	
   if (myajax.readyState == 4){
      //alert("Server is done!");
		var div = document.getElementById("divPostComment");
		div.innerHTML =  myajax.responseText;
		
		var divTxt = document.getElementById("divTxtArea");
		txtField.setAttribute("name","txtPost");
		txtField.setAttribute("rows","17");
		txtField.setAttribute("cols","100");
		divTxt.appendChild(txtField);

		//add BBcode btn bar.. 
		var divBB = document.getElementById("divBBcode");
		B = buildBtn("B","b"," B "); //,"simpletag('B')","hstat('bold');");
		I = buildBtn("I","i"," I "); //,"simpletag('I');","hstat('italic');");
		U = buildBtn("U","u"," U "); //,"simpletag('U');","hstat('under');");
		
		QUOTE = buildBtn("QUOTE","q"," QUOTE "); //,"simpletag('QUOTE');","hstat('quote')");
		URL = buildBtn("URL","l"," URL "); //,"tag_url()","hstat('url');");
		IMG = buildBtn("IMG","M"," IMG ");//,"simpletag('tag_image1');","hstat('image')");
		
		B.style.fontWeight="bold";
		I.style.fontStyle="italic";
		U.style.textDecoration="underline";
		
		
		//B.setAttribute("onclick",simpletag('B'));
		B.onclick = function(){simpletag('B')};
		I.onclick = function(){simpletag('I')};
		U.onclick = function(){simpletag('U')};
		QUOTE.onclick = function(){simpletag('QUOTE')};
		URL.onclick = function(){tag_url()};
		IMG.onclick = function(){tag_image(bPremium,bSubDir)};
		
		B.onmouseover = function(){hstat('bold')};
		I.onmouseover = function(){hstat('italic')};
		U.onmouseover = function(){hstat('under')};
		QUOTE.onmouseover = function(){hstat('quote')};
		URL.onmouseover = function(){hstat('url')};
		IMG.onmouseover = function(){hstat('image')};
		
		
		tagcount.setAttribute("type","text");
		tagcount.setAttribute("name","tagcount");
		tagcount.setAttribute("size","3");
		tagcount.setAttribute("maxlength","3");
		tagcount.setAttribute("readonly","readonly");
		tagcount.setAttribute("class","helpbox");
		tagcount.setAttribute("value","0");
		
		helpbox.setAttribute("type","text");
		helpbox.setAttribute("name","helpbox");
		helpbox.setAttribute("size","50");
		helpbox.setAttribute("maxlength","120");
		helpbox.setAttribute("readonly","readonly");
		helpbox.setAttribute("class","helpbox");
		helpbox.setAttribute("value","Help Status");
		
		divBB.appendChild(B);
		divBB.appendChild(I);
		divBB.appendChild(U);
		divBB.appendChild(QUOTE);
		divBB.appendChild(URL);
		divBB.appendChild(IMG);
		var txtTagCount = document.createElement("span");
		txtTagCount.innerHTML = "<b>Open Tags:</b>";
		divBB.appendChild(txtTagCount);
		divBB.appendChild(tagcount);
		
		var divHelpBox = document.getElementById("divHelpBox");
		divHelpBox.appendChild(helpbox);
		
		//var txtField = fombj.txtPost;
		//loadEmoticon('emoticons');
		//alert("Server is done!");
		//B.onclick = simpletag('B');
		loadEmoticon('emoticons');
		scrollBottom();
      }
}

function buildBtn(name,accesskey,value){
	var btn = document.createElement("input");
	btn.setAttribute("type","button");
	btn.setAttribute("name",name);
	btn.setAttribute("accesskey",accesskey);
	btn.setAttribute("value",value);
	//btn.onclick = function(){onclick};
	return btn;
}
function emoticonsPopup() {
	w = window.open("forum/emoticons.aspx", "smilies", "width=250, height=300, toolbars=no, scrollbars=yes");
	w.focus();
}
function updatePreview(){
   var myajax = ajaxpack.ajaxobj;
   //var myfiletype = ajaxpack.filetype;	
   if (myajax.readyState == 4){
		
		var span5 = document.createElement("span");
		span5.innerHTML = "<br><h3>Preview:</h3>";
		
		var divPreview = document.getElementById("divPreview");
		divPreview.innerHTML = myajax.responseText;
		var node = divPreview.firstChild;
		divPreview.insertBefore(span5,node);
	}
}
function updatePagePost(){
   var myajax = ajaxpack.ajaxobj;
   //var myfiletype = ajaxpack.filetype;	
   if (myajax.readyState == 4){
		var divPostComment = document.getElementById("divPostComment"); 
		var divPostResult = document.getElementById("divPostResult"); 
		removeAllChildNodes(divPostComment);
		divPostResult.innerHTML = myajax.responseText;
	}
}

function cancelPost(){
		var div = document.getElementById("divPostComment");
		removeAllChildNodes(div);		
		
		var div2 = document.getElementById("divPostCommentLink");
		//removeAllChildNodes(div);
		div2.className= 'visible'; 
		var div3 = document.getElementById("divPostCommentLink2");
		//removeAllChildNodes(div);
		div3.className= 'visible'; 
}



function removeAllChildNodes(node) {
	if (node && node.hasChildNodes && node.removeChild) {
		while (node.hasChildNodes()) {
			node.removeChild(node.firstChild);
		}
	}
} // removeAllChildNodes()



function scrollBottom() {
   	window.scroll(0,100000000000); // horizontal and vertical scroll targets
   	//parent.window.scrollTo(0,100000000000); 
}

// ********************* Emoticon code

function loadEmoticon(divName) {
	var divSetting = ReadCookie(divName);
	if (divSetting==null || divSetting==3) {
		doEmoticon(divName,3,0);
	} else {
		doEmoticon(divName,1,0);
	}				
	//if (divSetting==2){
	//	doEmoticon(divName,2,0);
	//}
}
			
function doEmoticon(divName, show, bSave) {
	//default to save if bSave not passed in
	if (bSave==null) bSave = 1;
	emoticonShow = show;
	ajaxpack.getAjaxRequest(strSubDir+'ajaxEmoticon.aspx', 'show='+show, updateEmoticon, 'txt');
		
	if (bSave = 1) SetCookie(divName,show);
}
			
function updateEmoticon(){
   var myajax = ajaxpack.ajaxobj;
   if (myajax.readyState == 4){
		if (emoticonShow==1 || emoticonShow==3) {
			var divEmoticon = document.getElementById("divEmoticon");
			removeAllChildNodes(divEmoticon);
			divEmoticon.innerHTML = myajax.responseText;}
		else {
			var divEmoticonMore = document.getElementById("divEmoticonMore");
			removeAllChildNodes(divEmoticonMore);
			divEmoticonMore.innerHTML = myajax.responseText;}
			
	}
}

function ReadCookie(cookieName) {
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length;
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}
			
function SetCookie(cookieName,cookieValue,nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=365;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}
function emoticonHideMore(){
	var div = document.getElementById("divEmoticonMore");
	var link = document.createElement("a");
	link.href="javascript:doEmoticon('emoticons',2);";
	link.appendChild( document.createTextNode('More Emoticons'));
	link.style.fontWeight = 'bold';
	removeAllChildNodes(div);
	div.appendChild(link);
	
}

