/* RMD Drawing page tools */

function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
	field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
else 
	countfield.value = maxlimit - field.value.length;
}

function Del(Word) {
	a = Word.indexOf("<");
	b = Word.indexOf(">");
	len = Word.length;
	c = Word.substring(0, a);
	if(b == -1)
	b = a;
	d = Word.substring((b + 1), len);
	Word = c + d;
	tagCheck = Word.indexOf("<");
	if(tagCheck != -1)
	Word = Del(Word);
	return Word;
}
function Check() {
	ToCheck = document.commentForm.comment.value;
	Checked = Del(ToCheck);
	document.commentForm.comment.value = Checked;
}

function submitcomment(){
	if(document.commentForm.comment.value!=""){
		Check();
		var opt = {
			method:'post', 
			postBody: Form.serialize('commentForm'),
			onLoading: function() { $('submit').disable().value = 'Sending...'; },
			onSuccess: function(ajxt) {
				document.getElementById("commentresponse").innerHTML += ajxt.responseText;
				$('submit').enable().value = 'Submit';
				//window.location.hash="commentTop";
				scroll(0,880);
			}
		}
		new Ajax.Request('/includes/ajax_drawcomments.php', opt);
		document.commentForm.comment.value = "";

	} else {
		alert('Please enter some text to leave a comment!');
	}
}
function morecomcrits(param1,param2,param3,param4,param5){
	var opt = {
			method:'get', 
			parameters: {id: param1, uid: param2, pg: param3, allowcrits: param4, filter: param5},
			onSuccess: function(ajxt) {
				document.getElementById("commentList").innerHTML = ajxt.responseText;
				//window.location.hash="commentTop";
				scroll(0,880);
			}
	}
	document.getElementById("commentList").innerHTML = '';
	new Ajax.Request('/includes/ajax_showdcomcrits.php', opt);
}
function submitcritique(){
	if(document.critiqueForm.award.value!="" && document.critiqueForm.f_like.value!="" && document.critiqueForm.f_tip.value!="" && document.critiqueForm.f_like.value.length>15 && document.critiqueForm.f_tip.value.length>15){
		Check();
		var opt = {
			method:'post', 
			postBody: Form.serialize('critiqueForm'),
			onLoading: function() { $('submit').disable().value = 'Sending...'; },
			onSuccess: function(ajxt) {
				var responseArray = ajxt.responseText.split('|');
				document.getElementById("critiqueDiv").innerHTML = responseArray[0];
				if(responseArray[1]){
					document.getElementById("commentresponse").innerHTML += responseArray[1];
					//window.location.hash="commentTop";
					scroll(0,880);
				}
				$('submit').enable().value = 'Submit';
			}
		}
		new Ajax.Request('/includes/critique_inc.php', opt);
		//document.commentForm.comment.value = "";

	} else if(document.critiqueForm.f_like.value.length<=16 || document.critiqueForm.f_tip.value.length<=16){
		alert('Step 1 and 2 require more text than that! Give the user more feedback.');
	} else {
		alert('All 3 Steps are required to submit a critique!');
	}
}

function morerandraws(param1,param2){
	var opt = {
			method:'get', 
			parameters: {cat: param1, randID: param2},
			onSuccess: function(ajxt) {
				document.getElementById("morecatdraws").innerHTML = ajxt.responseText;
				var r = document.getElementById('refresh');
				r.style.display="none";
			}
	}
	new Ajax.Request('/includes/ajax_morecatdrawings_v2.php', opt);
	var r = document.getElementById('refresh');
	r.style.display="block";
}

var slideCount = 0;
var doClick=true;
var catslideCount = 0;
var catdoClick=true;

function moreuserdraws(param1,param2,param3){
	var opt = {
			method:'get', 
			parameters: {id: param1, uid: param2, row: param3},
			onSuccess: function(ajxt) {
				document.getElementById("userDrawsHolder").innerHTML = ajxt.responseText;
				document.getElementById('userThumbScroll').style.marginLeft = '405px';
				var t1B = new Tween(document.getElementById('userThumbScroll').style,'marginLeft',Tween.strongEaseInOut,405,0,1,'px');
				t1B.onMotionFinished = function(){ doClick=true; };
				t1B.start();
			}
	}
	if(doClick){
		slideCount+=1;
		if(slideCount%2!=0){
			var newStartX = (slideCount-1)*405;
			var newFinishX = -(slideCount)*405;
			var t1A = new Tween(document.getElementById('userThumbScroll').style,'marginLeft',Tween.strongEaseInOut,newStartX,newFinishX,1,'px');
			t1A.onMotionFinished = function(){ doClick=true; };
			t1A.start();
		} else {
			var newStartX = (slideCount-3)*405;
			var newFinishX = -(slideCount+2)*405;
			slideCount=0;
			var t1A = new Tween(document.getElementById('userThumbScroll').style,'marginLeft',Tween.strongEaseInOut,newStartX,newFinishX,1,'px');
			t1A.start();
			new Ajax.Request('/includes/ajax_moreuserdrawings_v2.php', opt);
		}
		doClick=false;
	}
}

function lessuserdraws(param1,param2,param3){
	var opt = {
			method:'get', 
			parameters: {id: param1, uid: param2, row: param3},
			onSuccess: function(ajxt) {
				document.getElementById("userDrawsHolder").innerHTML = ajxt.responseText;
				document.getElementById('userThumbScroll').style.marginLeft = '-810px';
				t1B = new Tween(document.getElementById('userThumbScroll').style,'marginLeft',Tween.strongEaseInOut,-810,-405,1,'px');
				t1B.onMotionFinished = function(){ doClick=true; };
				t1B.start();
			}
	}
	if(doClick && (param3>=0 || param3<=0 && slideCount==1)){
		slideCount-=1;
		if(slideCount%2==0){
			var newStartX = (slideCount-1)*405;
			var newFinishX = (slideCount)*405;
			var t1A = new Tween(document.getElementById('userThumbScroll').style,'marginLeft',Tween.strongEaseInOut,newStartX,newFinishX,1,'px');
			t1A.onMotionFinished = function(){ doClick=true; };
			t1A.start();
		} else {
			var newStartX = (slideCount+1)*405;
			var newFinishX = (slideCount+2)*405;
			slideCount=1;
			t1A = new Tween(document.getElementById('userThumbScroll').style,'marginLeft',Tween.strongEaseInOut,newStartX,newFinishX,1,'px');
			t1A.start();
			new Ajax.Request('/includes/ajax_moreuserdrawings_v2.php', opt);
		}
		doClick=false;
	}


}

function morecatdraws(param1,param2){
	//alert("morecatdraws");
	var opt = {
			method:'get', 
			parameters: {cat: param1, randID: param2},
			onSuccess: function(ajxt) {
				document.getElementById("catDrawsHolder").innerHTML = ajxt.responseText;
				document.getElementById('catThumbScroll').style.marginLeft = '270px';
				var t1B = new Tween(document.getElementById('catThumbScroll').style,'marginLeft',Tween.strongEaseInOut,270,0,1,'px');
				t1B.onMotionFinished = function(){ catdoClick=true; };
				t1B.start();
				//alert('onSuccess');
			}
	}
	if(catdoClick){
		catslideCount+=1;
		if(catslideCount%2!=0){
			//alert('A');
			var newStartX = (catslideCount-1)*270;
			var newFinishX = -(catslideCount)*270;
			var t1A = new Tween(document.getElementById('catThumbScroll').style,'marginLeft',Tween.strongEaseInOut,newStartX,newFinishX,1,'px');
			t1A.onMotionFinished = function(){ catdoClick=true; };
			t1A.start();
		} else {
			//alert('B');
			var newStartX = (catslideCount-3)*270;
			var newFinishX = -(catslideCount+2)*270;
			catslideCount=0;
			var t1A = new Tween(document.getElementById('catThumbScroll').style,'marginLeft',Tween.strongEaseInOut,newStartX,newFinishX,1,'px');
			t1A.start();
			new Ajax.Request('/includes/ajax_morecatdrawings_v4.php', opt);
		}
		catdoClick=false;
		//alert("catdoClick");
	}
}

function lesscatdraws(param1,param2){
	//alert("morecatdraws");
	var opt = {
			method:'get', 
			parameters: {cat: param1, randID: param2},
			onSuccess: function(ajxt) {
				document.getElementById("catDrawsHolder").innerHTML = ajxt.responseText;
				document.getElementById('catThumbScroll').style.marginLeft = '-340px';
				t1B = new Tween(document.getElementById('catThumbScroll').style,'marginLeft',Tween.strongEaseInOut,-340,-270,1,'px');
				t1B.onMotionFinished = function(){ catdoClick=true; };
				t1B.start();
				//alert('onSuccess');
			}
	}
	//if(catdoClick){
	if(catdoClick){
		catslideCount-=1;
		if(catslideCount%2==0){
			//alert('A');
			var newStartX = (catslideCount-1)*270;
			var newFinishX = (catslideCount)*270;
			var t1A = new Tween(document.getElementById('catThumbScroll').style,'marginLeft',Tween.strongEaseInOut,newStartX,newFinishX,1,'px');
			t1A.onMotionFinished = function(){ catdoClick=true; };
			t1A.start();
		} else {
			//alert('B');
			var newStartX = (catslideCount+1)*270;
			var newFinishX = (catslideCount+2)*270;
			catslideCount=1;
			var t1A = new Tween(document.getElementById('catThumbScroll').style,'marginLeft',Tween.strongEaseInOut,newStartX,newFinishX,1,'px');
			t1A.start();
			new Ajax.Request('/includes/ajax_morecatdrawings_v4.php', opt);
		}
		catdoClick=false;
		//alert("catdoClick");
	}
}
function jumpCritique(){
	show('drawtabdiv1','drawtabdiv2');
	setTab('drawtab1','drawtab2');
	window.scroll(0,500);
	var t1A = new ColorTween(document.getElementById('drawtabdiv1').style, 'backgroundColor', Tween.bounceEaseOut,'cccccc','F5F5F5',2);
	t1A.start();
	//document.getElementById('critiqueDiv').style.border='2px solid #f50000';
}
function jumpComment(){
	show('drawtabdiv2','drawtabdiv1');
	setTab('drawtab2','drawtab1');
	window.scroll(0,500);
	var t1A = new ColorTween(document.getElementById('drawtabdiv2').style, 'backgroundColor', Tween.bounceEaseOut,'cccccc','F5F5F5',2);
	t1A.start();
	//document.getElementById('comment').style.border='2px solid #f50000';	
}
function likeCritique(id){
	var opt = {
			method:'get', 
			parameters: {id: id},
			onSuccess: function(ajxt) {
				document.getElementById("crit"+id).innerHTML = ajxt.responseText;
				//alert('onSuccess');
			}
	}
	new Ajax.Request('/includes/ajax_critiquerates.php', opt);
        //alert('clicked');
}