var currentReplyTo = false;

// Short name of function
function $(divName) {
	return document.getElementById(divName);
}

function stripSlashes(content) {
		// Strip Slashes
		re = /\\\"/gi;
		content = content.replace(re, '"');
		re = /\\\'/gi;
		content = content.replace(re, "'");
		return content;
}
/** 
* Browser indentification
*/ 				
if(document.implementation && document.implementation.createDocument) var isMozilla=true;
else var isMozilla=false;

// Vote comment
function voteComment(comment_id, poster_id, vote_action) {
	showProcessMessage("Please wait, data is transfering");
	YAHOO.util.Connect.asyncRequest('POST', HTTP_CTRLPATH+"comment_control/index.ctrl.php", justalert_callback, "ctrl_action="+vote_action+"&comment_id="+comment_id+"&poster_id="+poster_id);
}
// Delete comment
function deleteComment(comment_id, media_id) {
	showProcessMessage("Please wait, data is transfering");
	YAHOO.util.Connect.asyncRequest('POST', HTTP_CTRLPATH+"comment_control/index.ctrl.php", justalert_callback, "ctrl_action=deletecomment&comment_id="+comment_id+"&media_id="+media_id);
}

function addComment(content, media_id, replyto) {
	if(replyto) $('comment_panel'+replyto).style.display="none"; 
	showProcessMessage("Please wait, data is transfering");
	YAHOO.util.Connect.asyncRequest('POST', HTTP_CTRLPATH+"comment_control/index.ctrl.php", justalert_callback, "ctrl_action=addcomment&content="+content+"&media_id="+media_id+"&replyto="+replyto);
	return false;
}

function goSingup(){
	$("login").style.display = "none";
	$("singup").style.display = "block";
}
function goLogin() {
	$("login").style.display = "block";
	$("singup").style.display = "none";
}
function calcVisitor(ad_id, url)  {
	YAHOO.util.Connect.asyncRequest('POST', HTTP_CTRLPATH+"ad/index.ctrl.php", justalert_callback, "ctrl_action=calcVisitor&ad_id="+ad_id+"&url="+url); 
	return false;
}
function check(field, value)  {
	showProcessMessage("Please wait");
	YAHOO.util.Connect.asyncRequest('POST', HTTP_CTRLPATH+"user_panel/user_panel.ctrl.php", justalert_callback, "ctrl_action=checkField&field="+field+"&value="+value); 
	return false;
}
function loginUser(login, password)  {
	showProcessMessage("Please wait");
	YAHOO.util.Connect.asyncRequest('POST', HTTP_CTRLPATH+"user_panel/user_panel.ctrl.php", justalert_callback, "ctrl_action=loginUser&login="+login+"&password="+password); 
	return false;
}
function createUser()  {
	showProcessMessage("Please wait");
	var formObject = document.getElementById('signup_form'); 
	YAHOO.util.Connect.setForm(formObject, true); 	
	YAHOO.util.Connect.asyncRequest('POST', HTTP_CTRLPATH+"user_panel/user_panel.ctrl.php", justalert_callback); 
	return false;
}

var justAlertRespond = function(obj){
	if($('entering')!=undefined) $('entering').style.display='none'; 
	hideProcessMessage();
	if(obj.responseText == undefined) return false;
	if(obj.responseText.substr(0,1)=="{") {
		var respondStructure = eval( '(' + obj.responseText + ')' ); 
		if( respondStructure.ErrorMsg ) return showSystemMessage(respondStructure.ErrorMsg); 
		// Go to the authorization form
		if( respondStructure.ActionCode==1 ) goLogin(); 
		// Refresh page
		if( respondStructure.ActionCode==2 ) { location.href=''; return false; }
		// Vote comment and show results
		if( respondStructure.ActionCode==3 ) { $('commentPP_'+respondStructure.Extra).innerHTML = stripSlashes(respondStructure.Body); return true; }
		// User clicked a banner
		if( respondStructure.ActionCode==5 ) { if(respondStructure.Extra) location.href=respondStructure.Extra; return false; }
		// Apply Thesaurus Text Parsing
		if( respondStructure.ActionCode==6 ) { applyTextParsing(stripSlashes(respondStructure.Body)); return false; }
		// Show Thesaurus Term
		if( respondStructure.ActionCode==7 ) { showTooltip(respondStructure.Extra, stripSlashes(respondStructure.Body)); return false; }
		// Click Thesaurus Term
		if( respondStructure.ActionCode==8 ) return false;
		// Add comment
		if( respondStructure.ActionCode==4 ) {
			$('addcomment_content').value=''; 
			$('hiddenaddcomment_content').value=''; 
			$('CommentListBody').innerHTML = stripSlashes(respondStructure.Body); 
			return true; 
		}
		
		if( respondStructure.Body ) showSystemMessage(respondStructure.Body); 
	} else alert(obj.responseText);
}; 

var justalert_callback =
{
  success:justAlertRespond,
  failure:commonHandleFailure,
  upload: justAlertRespond, 
  argument:['foo','bar']
};

var handleSuccess = function(o){
	if(o.responseText !== undefined){
		alert("here: "+o.responseText);
	}
};

var commonHandleFailure = function(o){ 
	if(o.responseText !== undefined){
		showSystemMessage("Connection Error");
	}
};