 // THIS FILE REQUIRES THE USE OF 'tpajax.js' WHICH MUST BE INCLUDED BEFORE THIS FILE IS.

 // THE FOLLOWING CODE IS USED FOR THE TAB FUNCTIONS FOUND ON THE BREED SPECIFIC PAGES.
 // IT DOES NOT APPLY TO ANYTHING ELSE WITHIN THE TERRIFICPETS.COM WEBSITE.*/
function AddSmilie(num,eleID){
	 if(eleID==""){
		eleID = 'TopicMessage'; 
	 }
 	var f = $(eleID);
	var selection = new Selection(f);
		selection.setText('[#'+num+'#]');
    	f.focus();
 }
 	
 function toggleSmileRow(ele){
	 if($('smileRow').style.display == 'none'){
	  ele.value = 'Hide Smilies';	 
	  $('smileRow').style.display = '';
	 }else{
	  ele.value = 'Show Smilies';
	  $('smileRow').style.display = 'none';
	 }
 }
 
  function toggleFontColorRow(ele){
	 if($('FontColorRow').style.display == 'none'){ 
	  $('FontColorRow').style.display = '';
	 }else{
	  $('FontColorRow').style.display = 'none';
	 }
 }
 
 function fontColor(color){
 	var f = $('TopicMessage');
	var selection = new Selection(f);
		if(selection.getText() != false){
		selection.setText('[color='+color+'*]'+selection.getText()+'[/color]');
    	f.focus();
		}
 }
 
 function messagePreview(ele){
	 if(ele.value=="Show Preview"){
	 $('preview_row').style.display = 'block';
	 ele.value="Hide Preview";
	new Ajax('post_preview.asp',{
		parameters:Form.serialize('new_topic_form'),
		method:'post',
		update:'message_preview',
		onComplete: function(req){
		}
	}); 		 
	 }else{
	$('message_preview').innerHTML = "";	 
	$('preview_row').style.display = 'none';
	ele.value="Show Preview";
	 }
 }
	
 function addTag(tag,eleID) { 
 	var f = $(eleID);
	var selection = new Selection(f);
		if(selection.getText() != false){
		selection.setText('['+tag+']'+selection.getText()+'[/'+tag+']');
    	f.focus();
		}
 }	

 var smilesadded=false;
 function addMoreSmilies(eleID){
	 if(eleID==""){
		eleID = 'TopicMessage'; 
	 }
	 if(!smilesadded){
	var smileyBox = $('smileyBox');
		smileyBox.style.width = '556px';
		
	for(i=26;i<89;i++){
	 var newSmile 	      = new Image();
		 newSmile.src     = '/images/smilies/'+i+'.gif';
		 newSmile.style.cssText = 'border:none; cursor:pointer;';

		 newSmile.onclick = function(){
			 num = this.src.replace('http://www.terrificpets.com/images/smilies/','');
			 num = num.replace('.gif','');
			 AddSmilie(num,eleID);
		 };
		 
		 smileyBox.appendChild(newSmile);
	}
	smilesadded=true;
	 }
 }


 function changeTabs(ele){
  var tabs = $('tpb_tabrow').getElementsByTagName("DIV");
  var inc  = 10;
  for(i=0;i<tabs.length;i++){
   	tabs[i].style.zIndex = inc;
	inc = inc + 10;
	tabs[i].className = "tpb_unselected_tab";
  }
  ele.className = "tpb_selected_tab";
  ele.style.zIndex = 100;
 }
 
 function fontSize(size){
	var messages = document.getElementsByClassName('post_message'); 
	for(i=0;i<messages.length;i++){
		messages[i].style.cssText = "font-size:"+size+"px;";
	}
	setCookie('tpviewsettings',size,365,'/','','');
 }
 
 function showRSS(ele){
	var ele = $(ele);
	if(ele.style.display=='none'){
	 ele.style.display = 'block';	
	}else{
	 ele.style.display = 'none';	
	}
 }
 

 // SWITCHES THE BREED PHOTO BETWEEN THE ADULT AND THE PUPPY PHOTOS
 function showPhoto(src,ele){
	 var span = $(ele);
	 var pPic = $('pupPic');
	 var aPic = $('aduPic');
	 
	 	 aPic.style.cssText = "background: url('/images/iswap_on.gif') no-repeat;";
	 	 pPic.style.cssText = "background: url('/images/iswap_on.gif') no-repeat;";
		 span.style.cssText = "background: url('/images/iswap_off.gif') no-repeat;";
	 
 	$('breed_image').src = src;
 }
 
 // TOGGLES BETWEEN THE RATINGS AND ATTRIBUTES TAB PANES
 function showSpecs(num){
 	var tab1 = $('miniTab01');
	var tab2 = $('miniTab02');
  if(num==1){
  	$('ptb_ratings').style.display = 'none';
	$('ptb_attributes').style.display = 'block';
	tab1.className = 'miniTabOn';
	tab2.className = 'miniTabOff';
  }else{
  	$('ptb_attributes').style.display = 'none';
	$('ptb_ratings').style.display = 'block';
	tab1.className = 'miniTabOff';
	tab2.className = 'miniTabOn';
  }
 }
 
 // EXPANDS/COLLAPSES THE BREED ATTRIBUTES AND RATINS PANE FOUND ON THE MAIN BREED PAGE. "Information" TAB
  function collapseAttrs(ele){
 	var img = ele.firstChild;
 	if($('ptb_coll_attrs').style.display=='none'){
	  $('ptb_coll_attrs').style.display='block';
	  img.src = "/images/icon_expand.gif";
	  ele.style.cssText = "cursor:pointer;";
	}else{
	  $('ptb_coll_attrs').style.display='none';
	  img.src = "/images/icon_collapse.gif";
	  ele.style.cssText = "margin-bottom:3px;cursor:pointer;";
	}
 }
 
 // INITIALLY SETS THE CURRENT ARTICLE RATING.  IT REQUIRES THE VARIABLE "currentRating" WHICH IS SET ON THE PAGE REQUIRING THIS FUNCTION.
 	var isRatingSet 	= false;
	function ratingSet(num){
		var num = parseFloat(num);
		var imgs = $('tpa_rate_icons').childNodes;
		for(i=0;i<imgs.length;i++){
			if(i < num){
			imgs[i].src = 'images/rate_green.gif';
			}else{
			imgs[i].src = 'images/rate_grey.gif';
			}
		}
	}

	// SET THE RATING IF A USER CLICKS ON THE RATING ICONS.
	function setRating(id,num){
		isRatingSet = true;
		var userID = "<%=pgTPUser%>";
		
		alert(id+', '+num+', '+userID)
		
		if (userID==""){
			alert('You must be logged in to rate articles.');
		}else{
		$('tpa_rate_icons').innerHTML = 'Rating Article...';
		new Ajax('rating.asp',{
			parameters:'Id='+id+'&Rating='+num+'&Type=article&UserID=<%=userID%>',
			method:'post',
			update:'tpa_rate_icons',
			onComplete: function(req){
			alert(req.responseText);
				if(req.indexOf("[ERROR]")>-1){
				 alert(req.responseText);
				}else{
				 $('tpa_rate_icons').innerHTML = req.responseText;
				}
			}
		});
		}
	}
	
	// CHECKS IF THE RATING WAS SUCCESSFULLY SET
	function checkIfRatingSet(){
	 	if(!isRatingSet){
			$('tpa_rate_icons').innerHTML = $('tpa_rate_icons_ghost').innerHTML;
		}
	}
	
		 function quickRegister(func){
		 	if(func=="on"){
				$('tpa_post_register').style.display = "";
				$('tpa_post_needlog').style.display = "none";
			}else{
				$('tpa_post_register').style.display = "none";
				$('tpa_post_needlog').style.display = "";
			}
		 }




  // HANDLES AND PROCESSES THE LOGIN/REGISTER FORM FOUND ON THE ARTICLES PAGE > COMMENT SECTION
  		function formHandler(type){
			conLoader('tpa_post_form','on');		
					if(type=='register'){
					if(!$('regAgreement').checked){
						alert("You must agree to our terms and conditions before you can register.");
					}else{	
					var values = Form.serialize('registerUser')+"&ScreenName="+$('regScreenName').value+"&Password="+$('regPasswordA').value+"&Password2="+$('regPasswordB').value;	
					new Ajax('/secure/article-newuser.asp',{
						parameters:values,
						method: 'post',
						onComplete: function(req){
							if(req.responseText.indexOf("[ERROR]")>-1){
							 alert(req.responseText);
							}else{
							 $('tpa_post_form').innerHTML = req.responseText;
							}
							conLoader('tpa_post_form','off');
						}
					});	
					}
					}else if(type=='login'){
					var values = Form.serialize('loginUser');
					new Ajax("/secure/article-login.asp",{
						parameters:values,
						method: 'post',
						onComplete: function(req){
							if(req.responseText.indexOf("[ERROR]")>-1){
							 alert(req.responseText);
							}else{
							 $('tpa_post_form').innerHTML = req.responseText;
							}
							conLoader('tpa_post_form','off');
						}
					});	
					}else{
					alert("An error occured while processing your request.  Please try again.");
					conLoader('tpa_post_form','on');
					}
					

		}
		
  // SHOWS THE UPLOAD FORM FOR USERS TO SUBMIT THEIR OWN CUSTOM PHOTOS OF A SPECIFIC BREED.
   function showUploadForm(){
 	if($('image_uploader').style.display=='none'){
		$('image_uploader').style.display='block';
	}else{
		$('image_uploader').style.display='none';
	}
 }		
 
 	function clearCheck(ele){
		if(ele.value==""){
			ele.value = 'Type your message here...';	
		}else if(ele.value=="Type your message here..."){
			ele.value = '';	
		}
	}
 
	function swapText(ele,func){
	var ele = $(ele);
	$('display_alpha').style.display = "none";
	$('display_size').style.display = "none";
	$('display_group').style.display = "none";
	$('display_rating').style.display = "none";
	
	var tabs = $('breed_tabs').childNodes;
	 for(i=0;i<tabs.length;i++){
	  tabs[i].className = 'tabOff';
	 }
	 
	 ele.className = 'tabOn';
	
	 if(func=="showAlpha"){
	  $('display_alpha').style.display = "block";
	 }else if(func=="showSize"){
	  $('display_size').style.display = "block";
	 }else if(func=="showGroup"){
	  $('display_group').style.display = "block";
	 }else if(func=="showRating"){
	  $('display_rating').style.display = "block";
	 }
	}
 
	function swapDirectory(ele){
	var ele = $(ele);
	var tabs = $('breed_tabs').childNodes;
	 for(i=0;i<tabs.length;i++){
	  tabs[i].className = 'tabOff';
	 }
	 ele.className = 'tabOn';
	}
 
 // GET PAGE SIZES FOR POSITIONING
 function getPageSize(){
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function getPageScroll(){
	var yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
	}
	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


function postop(ele){
	var curtop = 0;
	if (ele.offsetParent) {
		curtop = ele.offsetTop;
		while (ele = ele.offsetParent) {
		curtop += ele.offsetTop;
		}
	}
	return curtop;
}
function posleft(ele){
	var curleft = 0;
	if (ele.offsetParent) {
		curleft = ele.offsetLeft;
		while (ele = ele.offsetParent) {
		curleft += ele.offsetLeft;
		}
	}
	return curleft;
}

 function CheckRules(form){
	var agree = $('agreeRules');
	var error="";
	if(form.id=="new_topic_form"){
		if($('TopicTitle').value=="") error = error+"Title is Blank\n";
		if($('TopicMessage').value=="") error = error+"Message is Blank\n";
		if($('TopicDescription').value=="") error = error+"Description is Blank\n";
		if(!agree.checked) error = error+"You must agree to the forum rules to post.\n";
	}else if(form.id=="submit_post_form"){
		$('submitPost').disabled = true;
		if($('message').value=="") error = error+"Message is Blank\n";
		if(!agree.checked) error = error+"You must agree to the forum rules to post.\n";
	}
	
	if(error!=""){ 
	alert(error);
	$('submitPost').disabled = false;
	}else{
		form.submit();	
	}
 }
 
 
 
 function ShowForumRules(){
	var rules = $('reply_rules');
	if(rules.style.display=='none'){
		rules.style.display='block';
	}else{
		rules.style.display='none';
	}
 }



var loaderState = false;
function conLoader(ele,action){
 var parent = $(ele);
	if(action=="off"){
	 if(parent.firstChild.id=="conLoader" || parent.firstChild.id=="conLoaderBox"){
	 parent.removeChild(parent.firstChild);
	 parent.removeChild(parent.firstChild);
	 }
	 loaderState = false;
	}else{
	 if(!loaderState){
	 loaderState = true;
 var parH	= parent.offsetHeight;
 var parW 	= parent.offsetWidth;
 var ldrH	= 28;
 var ldrW 	= 96;
 var ldrTop = Math.round((parH-ldrH)/2);
 var ldrLft = Math.round((parW-ldrW)/2);
 var styles2 = "background-color:#FFFFFF; filter:alpha(opacity=50);-moz-opacity:0.5;opacity: 0.5;width:"+parW+"px; height:"+parH+"px; position:absolute; z-index:150;margin-top:0px;margin-left:0px;";
 var styles = "background:url(/images/loader.gif);width:96px; height:28px; position:absolute; z-index:200;margin-top:"+ldrTop+"px;margin-left:"+ldrLft+"px;";
 var ldrBox = document.createElement("DIV");
     ldrBox.style.cssText = styles2;
 	 ldrBox.id = "conLoaderBox";
 var loader = document.createElement("DIV");
     loader.style.cssText = styles;
 	 loader.id = "conLoader";
	 loader.innerHTML = '<img src="/images/indicator.gif" style="float:left;margin-top:5px;margin-left:9px;">';
	 parent.insertBefore(ldrBox, parent.firstChild);
	 parent.insertBefore(loader, parent.firstChild);
	} 
}
}