var CurrentMessageBoxId="";
var CurrentReplyBoxId="0";

function submitform(FormName)
{
  FormName.submit();
}


function CharCount(divID)
{
	var limit;
	//Current character count
	var current = document.getElementById(divID).value.length;
	//How many left?
	switch (divID){
		case 'desc':
			limit=500;
			break; 
		case 'title':
			limit=75;
			break; 
		case 'about':
			limit=750;
			break; 
		}

	var chars_left = limit - current;
	//Over the limit or not?
	if(chars_left < 0)
	{
		//Time for a warning
		document.getElementById(divID+"_char_count").innerHTML = "<div class='errors'>Your post will not be submitted unless you remove some characters!</div>";
	}
	else 
	{
		//Display the number of chars left.
		document.getElementById(divID+"_char_count").innerHTML = "<div class='submit-post-info'><strong>"+chars_left+"</strong> characters left</div>";
	}
}

function popup(pageURL,x,y)
{

 var newwin = window.open( pageURL, "_blank", "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width=" + x + ",height=" + y + ",resizable=yes" );

}

function clearText(thefield) { 
	if (thefield.defaultValue == thefield.value) thefield.value = "";
	} 


function ShowLoginBox(){
	document.getElementById('LoginBox').style.display="block";
	document.LoginForm.username.focus();

}
function HideLoginBox(){
	document.getElementById('LoginBox').style.display="none";
}

function EnterTest(event,FormName) {

         if (event && event.which == 13){
			 //LoginNow('login');
            FormName.submit();
			}
         else if (window.event && window.event.keyCode == 13)
            {  
			//LoginNow('login');
			FormName.submit(); 
			}
         else
         return true;		
 }
 function ProfileEdit(){
	document.getElementById('profile-view-box').style.display="none";
	document.getElementById('profile-edit-box').style.display="block";	
	}
	
function ProfileEditCancel(){
	document.getElementById('profile-view-box').style.display="block";
	document.getElementById('profile-edit-box').style.display="none";	
	
	}	
function ShowMessage(id){
	var url=location.href;
	divID="message_"+id;
	document.getElementById(divID).style.display="block";
//	message="<span>Please Login | <a href=\"javascript:HideMessage('"+id+"')\">close</a></span>";
	message=''+
	'<div class="loginBox2" id="LoginBox2" name="LoginBox2">'
	+'<div class="loginBox2Content">'
	+'<form name="LoginForm2" action="'+url+'" method="post">'	
	+'<table cellspacing="1" cellpadding="0" border="0">'
	+'<tr><td colspan="3"><span class="highlight"><b>Login to make your vote count. </b> Not a member yet? <a href="/signup">Sign Up</a> for FREE</span></td></tr>'
	+'<tr>'
	+'  <td>Username:<br><input name="username" type="text" id="username" size="10" style="width:100px;" onKeyPress="return EnterTest(event,this.form)"/></td>'
	+'  <td>Password:<br><input name="password" type="password" id="password" size="10" style="width:100px;"   onKeyPress="return EnterTest(event,this.form)"/></td>'
	+'  <td style="padding-top:17px;"><input type="image" src="/images/b_login.gif" width="48" height="17" border="0" /> <a href="javascript:HideMessage(\''+ id+'\')"><img src="/images/b_cancel.gif" width="58" height="17" border="0" /></a></td>'
	+'</tr>'
	+'<tr>'
	+'  <td colspan="3"><input type="checkbox" name="remember_check" value="remember" /><input type="hidden" name="doThis" value="login"/>Remember Me </td>'
	+'</tr>'
	+'</table>'
	+'</form>'
	+'</div>'
	+'</div>';
	
	document.getElementById(divID).innerHTML=message;		
	
	if(CurrentMessageBoxId!="" && CurrentMessageBoxId!=id ){
		HideMessage(CurrentMessageBoxId);
		}
	CurrentMessageBoxId=id;
	
	}	
function HideMessage(id){
	divID="message_"+id;
	document.getElementById(divID).style.display="none";	
	}	
function containsDOM(container,containee){var isParent=false;do{if((isParent=container==containee))
break;containee=containee.parentNode;}
while(containee!=null);return isParent;}
function checkMouseLeave(element,evt){evt=(evt)?evt:((window.event)?window.event:"");window.status=evt;if(evt.relatedTarget){return!containsDOM(element,evt.relatedTarget);}else{if(element.contains(evt.toElement)){return(false);}else{return(true);}}
}

function ShowHideFlag(i){
	nav=document.getElementById('flag_dropdown_'+i).style;
	con=document.getElementById('flag_dropdown_'+i);
	if(nav.display=='none'){
		nav.display='block';
		con.onmouseout=function(evt){
			if(checkMouseLeave(this,evt)){

				nav=document.getElementById('flag_dropdown_'+i).style;
				nav.display='none';
			}
		}
	}else{
		nav.display='none';
		con.onmouseout=function(evt){
			if(checkMouseLeave(this,evt)){

				nav=document.getElementById('flag_dropdown_'+i).style;
				nav.display='none';
			}
		}
	}	
	
}
function ShowHideDiv(divID){
	divStyle=document.getElementById(divID).style;
	if(divStyle.display=='none')
	{
		divStyle.display="block";
		if(divID=='login'){ document.LoginForm.username.focus();}
	}else{
		divStyle.display="none";
		}	
}

function ShowAnotherCaptcha(divID){
	document.getElementById('captcha_img_'+divID).innerHTML='<img src="/captcha.php?'+Math.random(10)+'" width="200" height="35">';	
	}
	
function ChangeClass(divID,cssClassName){
	document.getElementById(divID).className=cssClassName;
	}

function ShowAddComment(){
	CommentReplyHide(CurrentReplyBoxId);
	}


function CommentReplyShow(commentID,userName,userAvatar){	
	var url=location.href;
	var replyBox=""
	+"<form name='CommentForm_"+commentID+"' action='"+url+"' method='post' onsubmit='return false'>"
	+"<div>"
	+"<table width='100%'  border='0' cellspacing='0' cellpadding='0'>"
	+"<tr>"
	+"	<td width='65' valign='top'>"
	+"			<div class='comment-avatar'><a href='/profile/"+userName+"'><img src='" + userAvatar +"' width='50' height='50' border='0'></a></div>"
	+"	</td>"
	+"	<td valign='top'>"
	+'          <div class="comment-header">'
	+"			<div class='comment-owner'><a href='/profile/"+userName+"'>"+userName+"</a> <span class='comment-date'><b>Add Comment</b></span> *** HTML tags are not  allowed </div></div>"
    +"			<div class='comment-text'>"
	+"			<div id=comment_submit_error_"+commentID+"></div>"
	+"			<textarea cols='50' rows='4' style='width:99%' name='comment_text' class='bizheatInput'></textarea>"
	+"			<br>"
	+"			<div class='goLeft'>"
	+"				<div>Text in the image (<a href='javascript:ShowAnotherCaptcha("+commentID+");'>show me another</a>)</div>"
	+"				<div><input type='text' name='security_text' size='15' style='width:170px;' class='bizheatInput'></div>"
	+"			</div>"
	+"			<div class='goLeft addPadding' id='captcha_img_"+commentID+"'><img src='http://www.bizheat.com/captcha.php' width='200' height='35'></div>"
	+"			<br>"
	+"          <input type='hidden' name='parentid' value='"+commentID+"'>"
	+"			<input type='hidden' name='doThis' value='submit'>"
	+"          <div style='clear:both;'>"
	+"			<input type='submit' value='Add Comments' class='bizheatButton' onClick='CommentSubmitCheck("+commentID+",CommentForm_"+commentID+");'>"
	+"			<input type='button' name='Cancel' value='Cancel' class='bizheatButton2' onclick='CommentReplyHide("+commentID+")'>"	
	+"			</div>"
	+"			</div>"
	+"	</td>"
	+" </tr>"
	+"</table>"
	+"</div>"
	+"</form>";
	
	document.getElementById('comment_'+commentID+'_reply').style.display="block";	
	document.getElementById('comment_'+commentID+'_reply').innerHTML=replyBox;
	
	if(CurrentReplyBoxId!="0" && CurrentReplyBoxId!=commentID){
		CommentReplyHide(CurrentReplyBoxId);
		}

	if(CurrentReplyBoxId=="0"){
		ShowHideDiv('add-comment-button');
		ShowHideDiv('comment_0_reply');
	}
		
	CurrentReplyBoxId=commentID;	
}

function CommentReplyHide(commentID){
	document.getElementById('comment_'+commentID+'_reply').style.display="none";		
	document.getElementById('comment_'+commentID+'_reply').innerHTML='';	

	if(CurrentReplyBoxId==commentID  ){
		ShowHideDiv('add-comment-button');
		ShowHideDiv('comment_0_reply');
		ShowAnotherCaptcha('0');
		CurrentReplyBoxId="0";
		}
}


//-------------------------- AJAX FUNCTIONS ---------------------------------

function checkavailability(str){
	httpRequest=GethttpRequestObject();
	
	var url="/checkavailability.php";
	url=url+"?username="+str;
	url=url+"&sid="+Math.random();
	
	httpRequest.onreadystatechange = function() { check_username(httpRequest,"Feedback"); };
	httpRequest.open('GET', url, true);
	httpRequest.send('');
	}

function check_username(httpRequest,ElementID){ 
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			document.getElementById(ElementID).innerHTML=httpRequest.responseText;
			}
		}
	}

function CheckUrl(str){
	httpRequest=GethttpRequestObject();
	
	var url="/check_url.php";
	url=url+"?url="+str;
	url=url+"&sid="+Math.random();
	
	httpRequest.onreadystatechange = function() { check_url(httpRequest,"url_message"); };
	httpRequest.open('GET', url, true);
	httpRequest.send('');
	}

function check_url(httpRequest,ElementID){ 
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			document.getElementById(ElementID).innerHTML=httpRequest.responseText;
			}
		}
	}


function Vote(postID,userID,ballot){
	httpRequest=GethttpRequestObject();
	
	var url="/vote.php";
	url=url+"?postid="+postID+"&userid="+userID+"&vote="+ballot;
	url=url+"&sid="+Math.random();
	
	httpRequest.onreadystatechange = function() { check_vote(httpRequest,postID,userID,ballot); };
	httpRequest.open('GET', url, true);
	httpRequest.send('');
	}
	
function check_vote(httpRequest,postID,userID,ballot){ 
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			document.getElementById('vote_count_'+postID).innerHTML=httpRequest.responseText;

			message=''
			+'<div id="vote_retract_'+postID+'">'
			+'<div class="post-vote-info">voted <span class="color2">'+ballot+'</span></div>'
			+'<div class="post-vote-retract"><a href="javascript:Vote('+postID+','+userID+',\'retract\')" title="retract"></a></div>'
			+'</div>'

			switch (ballot){
				case 'hot':
					document.getElementById('vote_'+postID).innerHTML=message;	
					document.getElementById('flag_'+postID).innerHTML='Flag';	
					break;
					
				case 'not':
					document.getElementById('vote_'+postID).innerHTML=message;	
					document.getElementById('post_'+postID).className+=' post-flagged';
					document.getElementById('flag_'+postID).innerHTML='Flag';						
					break;
					
				case 'retract':
					message=''
					+'<div id="vote_submit_'+postID+'">'
					+'<span class="post-vote-hot"><a href="javascript:Vote('+postID+','+userID+',\'hot\')" title="hot"></a></span>'
					+'<span class="post-vote-not"><a href="javascript:Vote('+postID+','+userID+',\'not\')" title="not"></a></span>'
					+'</div>';					
					document.getElementById('vote_'+postID).innerHTML=message;						
					document.getElementById('post_'+postID).className='post';				
					
					message=''					
					+'<div id="flag_dropdown_'+postID+'" class="flag-dropdown" style="display:none;">'
					+'<a href="javascript:FlagPost('+postID+','+userID+',1);">Duplicate Story</a>'
					+'<a href="javascript:FlagPost('+postID+','+userID+',2);">Inaccurate</a>'
					+'<a href="javascript:FlagPost('+postID+','+userID+',3);">Inappropriate</a>'
					+'<a href="javascript:FlagPost('+postID+','+userID+',4);" class="final">SPAM</a>'
					+'</div>'
					+'<a href="javascript:ShowHideFlag('+postID+')">Flag</a>'
				
					document.getElementById('flag_'+postID).innerHTML=message;						
					break;
			}
		} else {
				alert('There was a problem with the request.');
		}
	}
}

function CommentVote(commentID,userID,ballot){
	httpRequest=GethttpRequestObject();
	
	var url="/vote_comment.php";
	url=url+"?comment_id="+commentID+"&userid="+userID+"&vote="+ballot;
	url=url+"&sid="+Math.random();
	
	httpRequest.onreadystatechange = function() { check_comment_vote(httpRequest,commentID,userID,ballot); };
	httpRequest.open('GET', url, true);
	httpRequest.send('');
	}
	
function check_comment_vote(httpRequest,commentID,userID,ballot){ 
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			document.getElementById('comment_vote_count_'+commentID).innerHTML=httpRequest.responseText;

			switch (ballot){
				case 'hot':
					message='<div class="comment-info-content">You Voted &nbsp;<img src="/images/comment_vote_hot.gif" align="absmiddle" border="0" width="16" height="16" title="Hot"></div>';
					document.getElementById('comment_info_'+commentID).innerHTML=message;
					document.getElementById('comment_vote_'+commentID).innerHTML='';
					document.getElementById('comment_flag_'+commentID).innerHTML='';					
					break;
					
				case 'not':
					message='<div class="comment-info-content">You Voted &nbsp;<img src="/images/comment_vote_not.gif" align="absmiddle" border="0" width="16" height="16" title="Not"></div>';
					document.getElementById('comment_info_'+commentID).innerHTML=message;					
					document.getElementById('comment_vote_'+commentID).innerHTML='';	
					document.getElementById('comment_flag_'+commentID).innerHTML='';						
					message='<div class="comment-flagged">This comment has been voted out. <span id="comment_flagged_action_'+commentID+'"><a href="javascript:ShowComment('+commentID+')">Show Comment</a></span></div>';
					document.getElementById('comment_flagged_'+commentID).innerHTML=message;		
					document.getElementById('comment_text_'+commentID).innerHTML='';						
					break;					
			}
		} else {
				alert('There was a problem with the request.');
		}
	}
}

function FlagPost(postID,userID,flagID){
	ShowHideFlag(postID);
	document.getElementById('post_'+postID).className+=' post-flagged';
	document.getElementById('flag_'+postID).innerHTML='Flagged';	
	
	message=''			
			+'<div id="flag_retract_'+postID+'">'					
			+'<div class="post-flag-info">flagged</div>'
			+'<div class="post-flag-retract"></div>'
			+'</div>';
		
	document.getElementById('vote_'+postID).innerHTML=message;	
	
	httpRequest=GethttpRequestObject();
	
	var url="/flag.php";
	url=url+"?postid="+postID+"&userid="+userID+"&flagid="+flagID;
	url=url+"&sid="+Math.random();
	
	httpRequest.open('GET', url, true);
	httpRequest.send('');
}	


function BookmarkPost(postID,userID){
	document.getElementById('bookmark_'+postID).innerHTML='Bookmarked';	
	
	httpRequest=GethttpRequestObject();
	
	var url="/bookmark.php";
	url=url+"?postid="+postID+"&userid="+userID;
	url=url+"&sid="+Math.random();
	
	httpRequest.open('GET', url, true);
	httpRequest.send('');
}	

function RemoveBookmark(deleteID,userID){
	document.getElementById('remove_bookmark_'+deleteID).innerHTML='';	
	
	httpRequest=GethttpRequestObject();
	
	var url="/remove_bookmark.php";
	url=url+"?delete_id="+deleteID+"&userid="+userID;
	url=url+"&sid="+Math.random();
	
	httpRequest.open('GET', url, true);
	httpRequest.send('');
}	

function RemoveFriend(deleteID,userID){
	document.getElementById('remove_friend_'+deleteID).innerHTML='';	
	
	httpRequest=GethttpRequestObject();
	
	var url="/remove_friend.php";
	url=url+"?delete_id="+deleteID+"&userid="+userID;
	url=url+"&sid="+Math.random();
	
	httpRequest.open('GET', url, true);
	httpRequest.send('');
}	

function AddFriend(requesterID,requestedID){
	httpRequest=GethttpRequestObject();
	
	var url="/add_friend.php";
	url=url+"?requester_id="+requesterID+"&requested_id="+requestedID;
	url=url+"&sid="+Math.random();
	
	httpRequest.open('GET', url, true);
	httpRequest.send('');
}	

function ProcessRequest(requesterID,requestedID,action){
	document.getElementById('process_request_'+requesterID).innerHTML='';	
	
	httpRequest=GethttpRequestObject();
	
	var url="/process_request.php";
	url=url+"?requester_id="+requesterID+"&requested_id="+requestedID+"&action="+action;
	url=url+"&sid="+Math.random();
	
	httpRequest.open('GET', url, true);
	httpRequest.send('');
}	


function FlagComment(commentID,userID,flagID){
	ShowHideFlag('c_'+commentID);
	message='<div class="comment-info-content">You Flagged <img src="/images/icon_flag.gif" width="11" height="11" align="absmiddle"></div>';
	document.getElementById('comment_info_'+commentID).innerHTML=message;	
	document.getElementById('comment_vote_'+commentID).innerHTML='';
	document.getElementById('comment_flag_'+commentID).innerHTML='';		

	message='<div class="comment-flagged">This comment has been flagged. <span id="comment_flagged_action_'+commentID+'"><a href="javascript:ShowComment('+commentID+')">Show Comment</a></span></div>';
	document.getElementById('comment_flagged_'+commentID).innerHTML=message;		
	document.getElementById('comment_text_'+commentID).innerHTML='';		

	httpRequest=GethttpRequestObject();
	
	var url="/flag_comment.php";
	url=url+"?commentid="+commentID+"&userid="+userID+"&flagid="+flagID;
	url=url+"&sid="+Math.random();
	
	httpRequest.open('GET', url, true);
	httpRequest.send('');
}	

function ShowComment(commentID){
	document.getElementById('comment_text_'+commentID).innerHTML="<div class='comment-text'>Loading...</div>";
	
	httpRequest=GethttpRequestObject();
	
	var url="/show_comment.php";
	url=url+"?comment_id="+commentID;
	url=url+"&sid="+Math.random();
	
	httpRequest.onreadystatechange = function() { check_show_comment(httpRequest,commentID); };
	httpRequest.open('GET', url, true);
	httpRequest.send('');
	
}

function check_show_comment(httpRequest,commentID){ 
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			document.getElementById('comment_text_'+commentID).innerHTML=httpRequest.responseText;	
			
			message='<a href="javascript:HideComment('+commentID+')">Hide Comment</a>';	
			document.getElementById('comment_flagged_action_'+commentID).innerHTML=message;	
		}
	}
}

function HideComment(commentID){
	document.getElementById('comment_text_'+commentID).innerHTML='';	
	message='<a href="javascript:ShowComment('+commentID+')">Show Comment</a>';	
	document.getElementById('comment_flagged_action_'+commentID).innerHTML=message;			
	}

function CommentSubmitCheck(commentID,formName){
	
	var	SecurityText=formName.security_text.value;
	
	httpRequest=GethttpRequestObject();
	
	var url="/comment_submit_check.php";
	url=url+"?security_text="+SecurityText;
	url=url+"&sid="+Math.random();
	
	httpRequest.onreadystatechange = function() { check_comment_submit(httpRequest,commentID,formName); };
	httpRequest.open('GET', url, true);
	httpRequest.send('');	
	
}

function check_comment_submit(httpRequest,commentID,formName){ 
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			CaptchaCheck=httpRequest.responseText;
			switch (CaptchaCheck){
				case "false":
					document.getElementById('comment_submit_error_'+commentID).innerHTML='<div class="error-box">The text you have entered does not match the image.</div>';	
					break;
				case "true":
					formName.submit();
					break;
				} 
			}
		}
}

function ShowTags(interval){

	switch (interval){
	case 'week':
		ChangeClass('b_week','tags-week week-on');
		ChangeClass('b_month','tags-month');
		ChangeClass('b_year','tags-year');
		break; 
	case 'month':
		ChangeClass('b_week','tags-week');
		ChangeClass('b_month','tags-month month-on');
		ChangeClass('b_year','tags-year');
		break;
	case 'year':
		ChangeClass('b_week','tags-week');
		ChangeClass('b_month','tags-month');
		ChangeClass('b_year','tags-year year-on');
		break;
	}
	
	httpRequest=GethttpRequestObject();
	
	var url="/tags_ajax.php";
	url=url+"?time_interval="+interval;
	url=url+"&sid="+Math.random();
	
	httpRequest.onreadystatechange = function() { check_tags(httpRequest); };
	httpRequest.open('GET', url, true);
	httpRequest.send('');	

}	

function check_tags(httpRequest){ 
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			document.getElementById('tags-cloud').innerHTML=httpRequest.responseText;	
		}
	}
}


function GethttpRequestObject(){
	var httpRequest;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
			}
		}else if (window.ActiveXObject) { // IE
			try {
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
				}catch (e){
					try {
						httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
						}catch (e) {}
					}
			}
	if (!httpRequest){
	  alert ("Your browser does not support AJAX!");
	  return false;
	  }
	return httpRequest;
	}

//------------------------------------------------------------------------------

function openwin(form_name,field_name)
{
window.open("/upload_avatar.php?form_name="+form_name+"&field_name="+ field_name,"upload","location=no,status=no,height=400,width=650");
}
