﻿// JScript File


var txtField = document.createElement("textarea");
//BBcode btns
var tagcount = document.createElement("input");
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(parentID){
	ajaxpack.postAjaxRequest(strSubDir+'ajaxPost.aspx', 'function=update&parentID='+parentID+'&txtPost='+escape(txtField.value), updatePagePost, 'txt');
}

function getCommentForm(parentID,premium,bSubDirectory) { 

	bPremium = premium;
	bSubDir = bSubDirectory;
	strSubDir='';
	if (bSubDir) {strSubDir = '../';}
	ajaxpack.getAjaxRequest(strSubDir+'ajaxPost.aspx', 'function=render&parentID='+parentID, 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() {
   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");
		txtField.setAttribute("style","width:500px;height:200px;");
		txtField.setAttribute("onkeydown","textCounter(this,'progressbar1',8000)");
		txtField.setAttribute("onkeyup","textCounter(this,'progressbar1',8000)");
		txtField.setAttribute("onfocus","textCounter(this,'progressbar1',8000)");
		divTxt.appendChild(txtField);
		
		//loadEmoticon('emoticons');
		scrollBottom();
      }
}

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 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();
}





//************************************************************
//progress bar/text counter
//needs following style sheet
//<style type="text/css">
//.progress{
//	    width: 1px;
//	    height: 14px;
//	    color: white;
//	    font-size: 12px;
//  overflow: hidden;
//  	background-color: navy;
//  	padding-left: 5px;
//}
//</style>

function textCounter(field,counter,maxlimit,linecounter) {
	// text width//
	var fieldWidth =  parseInt(field.offsetWidth);
	var charcnt = field.value.length;        

	// trim the extra text
	if (charcnt > maxlimit) { 
		field.value = field.value.substring(0, maxlimit);
	}

	else { 
	// progress bar percentage
	var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
	document.getElementById(counter).style.width =  parseInt((fieldWidth*percentage)/100)+"px";
	document.getElementById(counter).innerHTML="Limit: "+percentage+"%"
	// color correction on style from CCFFF -> CC0000
	setcolor(document.getElementById(counter),percentage,"background-color");
	}
}
function setcolor(obj,percentage,prop){
	obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}