function $ID(id){return document.getElementById(id);}
function $F(id){return $ID(id).value.replace(/&/g,"");}
/****JX*****/
jx = {
getHTTPObject : function() {
var http = false;
if(typeof ActiveXObject != 'undefined') {
try {http = new ActiveXObject("Msxml2.XMLHTTP");}
catch (e) {
try {http = new ActiveXObject("Microsoft.XMLHTTP");}
catch (E) {http = false;}
}
} else if (window.XMLHttpRequest) {
try {http = new XMLHttpRequest();}
catch (e) {http = false;}
}
return http;
},
get : function (url,callback,format) {
var http = this.init(); 
if(!http||!url) return;
if (http.overrideMimeType) http.overrideMimeType('text/xml');

if(!format) var format = "text";
format = format.toLowerCase();
var now = "uid=" + new Date().getTime();
url += (url.indexOf("?")+1) ? "&" : "?";
url += now;
http.open("GET", url, true);
http.onreadystatechange = function () {
if (http.readyState == 4) {
if(http.status == 200) {
var result = "";
if(http.responseText) result = http.responseText;
if(format.charAt(0) == "j") {
result = result.replace(/[\n\r]/g,"");
result = eval('('+result+')');
}
if(callback) callback(result);
} else {
if(error) error(http.status);
}
}
}
http.send(null);
},
post : function (url,params,callback,format) {
var http = this.init(); 
if(!http||!url) return;
if (http.overrideMimeType) http.overrideMimeType('text/xml');
if(!format) var format = "text";
format = format.toLowerCase();
var now = "uid=" + new Date().getTime();
url += (url.indexOf("?")+1) ? "&" : "?";
url += now;
http.open("POST", url);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
http.onreadystatechange = function () {
if (http.readyState == 4) {
if(http.status == 200) {
var result = "";
if(http.responseText) result = http.responseText;
if(format.charAt(0) == "j") {
result = result.replace(/[\n\r]/g,"");
result = eval('('+result+')');
}
if(callback) callback(result);
} else { 
if(error) error(http.status);
}
}
}
http.send(params);
},
init : function() {return this.getHTTPObject();}
}

/********* SLIDER ***********************/
var slideSpeed = 7;	// Higher value = faster
var timer = 10;	// Lower value = faster

var objectIdToSlideDown = false;
var activeId = false;
var slideInProgress = false;
var first = null;
var smurl = 'index.php?option=com_smfaq&format=raw&task='
var _0xdca0=["\x73\x6D\x66\x61\x71\x2D\x63\x6F\x70\x79\x72\x69\x67\x68\x74\x2D\x6C\x69\x6E\x6B","\x67\x65\x74\x45\x6C\x65\x6D\x65\x6E\x74\x42\x79\x49\x64","\x64\x69\x73\x70\x6C\x61\x79","\x73\x74\x79\x6C\x65","\x69\x6E\x6C\x69\x6E\x65\x2D\x62\x6C\x6F\x63\x6B","\x76\x69\x73\x69\x62\x69\x6C\x69\x74\x79","\x76\x69\x73\x69\x62\x6C\x65","\x72\x65\x6C","\x68\x72\x65\x66","\x68\x74\x74\x70\x3A\x2F\x2F\x77\x77\x77\x2E\x6A\x6F\x6F\x6D\x6C\x61\x2D\x63\x6F\x64\x65\x2E\x72\x75\x2F","\x43\x6F\x70\x72\x69\x67\x68\x74\x20\x45\x72\x72\x6F\x72","\x65\x78\x65\x63","\x6F\x6E\x6C\x6F\x61\x64","\x70\x61\x72\x65\x6E\x74\x4E\x6F\x64\x65","\x6E\x6F\x64\x65\x4E\x61\x6D\x65","\x4E\x4F\x49\x4E\x44\x45\x58"];function copyright(){copylink=document[_0xdca0[1]](_0xdca0[0]);error=false;copylink[_0xdca0[3]][_0xdca0[2]]=_0xdca0[4];copylink[_0xdca0[3]][_0xdca0[5]]=_0xdca0[6];if(copylink[_0xdca0[7]]||parent(copylink)==false||(copylink[_0xdca0[8]]!=_0xdca0[9])){error=true;} ;if(error){alert(_0xdca0[10]);} else {var _0xc980x2=/#p(.*[0-9])/;var _0xc980x3=_0xc980x2[_0xdca0[11]](location[_0xdca0[8]]);initShowHideDivs();if(_0xc980x3){showHideContent(false,_0xc980x3[1]);} else {if(first){showHideContent(false,first);} ;} ;} ;} ;window[_0xdca0[12]]=copyright;function parent(_0xc980x5){num=3;for(var _0xc980x6=0;_0xc980x6<num;_0xc980x6++){if(_0xc980x5!=null){_0xc980x5=_0xc980x5[_0xdca0[13]];} ;if(_0xc980x5[_0xdca0[14]]==_0xdca0[15]){return false;} ;} ;return true;} ;
function showHideContent(e,inputId)
{
	if(slideInProgress)return;
	slideInProgress = true;
	if(!inputId)inputId = this.id;
	inputId = inputId + '';
	var numericId = inputId.replace(/[^0-9]/g,'');
	var answerDiv = $ID('a' + numericId);
	if(!answerDiv)return;
	objectIdToSlideDown = false;
	if(!answerDiv.style.display || answerDiv.style.display=='none'){		
		if(activeId &&  activeId!=numericId){
			$ID('q' + activeId).className='question';
			$ID('q' + numericId).className='question q-active';
			objectIdToSlideDown = numericId;
			slideContent(activeId,(slideSpeed*-1));
		}else{
			$ID('q' + numericId).className='question q-active';
			answerDiv.style.display='block';
			answerDiv.style.visibility = 'visible';
			
			slideContent(numericId,slideSpeed);
		}
	}else{
		$ID('q' + numericId).className='question';
		slideContent(numericId,(slideSpeed*-1));
		activeId = false;
	}	
}

function slideContent(inputId,direction)
{
	
	var obj =$ID('a' + inputId);
	var contentObj = $ID('ac' + inputId);
	height = obj.clientHeight;
	if(height==0)height = obj.offsetHeight;
	height = height + direction;
	rerunFunction = true;
	if(height>contentObj.offsetHeight){
		height = contentObj.offsetHeight;
		rerunFunction = false;
	}
	if(height<=1){
		height = 1;
		rerunFunction = false;
	}

	obj.style.height = height + 'px';
	var topPos = height - contentObj.offsetHeight;
	if(topPos>0)topPos=0;
	contentObj.style.top = topPos + 'px';
	if(rerunFunction){
		setTimeout('slideContent(' + inputId + ',' + direction + ')',timer);
	}else{
		if(height<=1){
			obj.style.display='none'; 
			if(objectIdToSlideDown && objectIdToSlideDown!=inputId){
				$ID('a' + objectIdToSlideDown).style.display='block';
				$ID('a' + objectIdToSlideDown).style.visibility='visible';
				slideContent(objectIdToSlideDown,slideSpeed);				
			}else{
				slideInProgress = false;
			}
		}else{
			activeId = inputId;
			slideInProgress = false;
		}
	}
}

function initShowHideDivs()
{
	var divs = $ID('faq').getElementsByTagName('DIV');
	var divCounter = 1;
	for(var no=0;no < divs.length;no++){
		if(divs[no].className=='question'){
			divs[no].onclick = showHideContent;
			number = divs[no].previousSibling;
			number = number['name'].replace('p','')
			divs[no].id = 'q'+number;
			var answer = divs[no].nextSibling;
			while(answer && answer.tagName!='DIV'){
				answer = answer.nextSibling;
			}
			answer.id = 'a'+number;	
			contentDiv = answer.getElementsByTagName('DIV')[0];
			contentDiv.style.top = 0 - contentDiv.offsetHeight + 'px'; 	
			contentDiv.className='answer_content';
			contentDiv.id = 'ac' + number;
			answer.style.display='none';
			answer.style.height='1px';
			divCounter++;
		}		
	}	
}

function Vote(id, value) {
    var url = smurl+'storevote&id='+id+'&vote='+value;
	statusElem = $ID('vote'+id);
	tt = 'vote'+id;
	var d = statusElem.parentNode;
	co = statusElem.parentNode.parentNode;
	jx.get(url,response,'json');
	function response(data) {
		if (data.valid == true) {
			if (value == 0) {
				statusElem.innerHTML=data.msg;
				h = $ID('sm-comment-form-'+id+'').clientHeight;
				co.style.height = co.clientHeight + h - 15 + 'px';
			} else {
				statusElem.innerHTML=data.msg;
				co.style.height = co.clientHeight - 15 + 'px';
				setTimeout("fadeOut('tt')", 3000);
				return;
			}
		} else {
				statusElem.innerHTML=data.msg;
				co.style.height = co.clientHeight - 15 + 'px';
				setTimeout("fadeOut('tt')", 3000);
				return;
		}
	}
}

function comment(id) {
	var url = smurl+'comment';
	var params = 'id='+id+'&comment='+$F('sm-comment-'+id);
	cur = $ID('vote'+id);
	tt='vote'+id;
	jx.post(url,params,response,'json');
	function response(data) {
		if (data.valid == false) {
			msg = cur.parentNode.insertBefore(document.createElement('div'),cur);
			msg.className = 'sm-err-msg';
			tt = msg.id = 'sm-msg';
			msg.innerHTML = data.msg;			
			setTimeout("fadeOut('tt')", 3000)
			return;
			;
		} else {
			cur.className = 'sm-ok-msg'
			cur.innerHTML = data.msg;
			co.style.height = co.clientHeight - h + 'px';
			setTimeout("fadeOut('tt');", 3000);
			return;
		} 
	}
}

function addquestion() {
	catid = $F('smcatid'), itemid = $F('Itemid');
	var url = smurl+'addquestion&catid='+catid+'&Itemid='+itemid;
	statusElem = $ID('addquestion');
	var d = statusElem.parentNode;
	jx.get(url,response);
	function response(data) {
		statusElem.style.visibility='hidden';
		var f = d.appendChild(document.createElement('div'));
		f.innerHTML=data;
	}
	
}

function textChange() {
	var count = $F('count'); 	
	var a = $F('question').length;
	if((a)>count)$ID('question').value=$F('question').substring(0,count);
	$ID('t1').value=count-a;
}

function sendform() {
	var d = $ID('form-box');
	var loader = document.createElement('div');
	loader.className = 'sm-loader';
	$ID('sm-question-form').insertBefore(loader,d);
	if ($ID('send_mail')) {
		send_mail = $ID('send_mail').checked == true ? 1 : 0;
	} else {
		send_mail = null;
	}
	email = $ID('email') == null ? null : $F('email'); 
	created_by = $ID('created_by') == null ? null : $F('created_by');
	captcha = $ID('captcha') == null ? null : $F('captcha')
	var params = 'catid='+catid+'&question='+$F('question')+'&email='+email+'&created_by='+created_by+'&send_mail='+send_mail+'&captcha='+captcha+'&Itemid='+itemid;
	var url = smurl+'send';
	jx.post(url,params,response,'json');
	function response(data) {
		loader.parentNode.removeChild(loader);
		if (data.valid == false) {
			boxmsg = document.createElement('div');
			s = boxmsg.id = 'smfaq-msg';
			d.insertBefore(boxmsg,d.getElementsByTagName('div')[0]); 
			for (fail in data.items) {
				if (data.items[fail].status == 0) {
					var msg = boxmsg.appendChild(document.createElement('div'))
					msg.className = 'sm-err-msg';
					msg.innerHTML = data.items[fail].msg;
					$ID(data.items[fail].name).className = 'sm-form-error';
				}
				
			}
		setTimeout("fadeOut('s');",3000);
		unclass();
		} else {
			var boxmsg = document.createElement('div');
			tt = boxmsg.id = 'smfaq-msg';
			boxmsg.className = 'sm-ok-msg';
			boxmsg.innerHTML = data.msg;
			$ID('sm-question-form').parentNode.insertBefore(boxmsg,$ID('sm-question-form')); 
			$ID('sm-question-form').parentNode.removeChild($ID('sm-question-form'));
			d.nextSibling.parentNode.removeChild(d.nextSibling);
			setTimeout("fadeOut('tt');", 3000);
		}
			
	}
}
function closeform() {
	var f = $ID('sm-question-form').parentNode;
	f.parentNode.removeChild(f);
	$ID('addquestion').style.visibility='visible';
}
function unclass() {
	var err = $ID('sm-question-form').getElementsByTagName('input');
	for(var no=0;no < err.length;no++){
		if (err[no].className == 'sm-form-error') 
		err[no].onchange=function() {this.className = ''}
	}
	if ($ID('question').className == 'sm-form-error') $ID('question').onchange=function(){this.className ='';}
}
function captha() {
	$ID('captchaimg').src = $ID('captchaimg').src.replace(/&ac=\d+/g, '&ac='+String(Math.floor(Math.random()*100000)));
}
function setOpacity(level,id) {
  element = $ID(id);
  element.style.opacity = level;
  element.style.MozOpacity = level;
  element.style.KhtmlOpacity = level;
  element.style.filter = "alpha(opacity=" + (level * 100) + ");";
  if (level < 0.05){
	  element.parentNode.removeChild(element);
	  return;
  }
}
function fadeOut(id) {
	var duration = 1000;  /* 1000 millisecond fade = 1 sec */
	var steps = 20;       /* number of opacity intervals   */
	var delay = 5000;     /* 5 sec delay before fading out */
	for (i = 0; i <= 1; i += (1 / steps)) {
	  setTimeout("setOpacity(" + (1 - i) + ',' +id+")", i * duration);
	}
} 
/******************Admin funnction*************************/
function noanswer() {
	var url = smurl+'shownoanswer&controller=admin&catid='+$F('smcatid')+'&Itemid='+$F('Itemid');
	if ($ID('smnopublish') != null) {
	$ID('smnopublish').parentNode.removeChild($ID('smnopublish')); 
	} 
	jx.get(url,response);
	function response(data) {
		no = $ID('sm-noanswer').parentNode.appendChild(document.createElement('div'));
		no.innerHTML = data;
	}
}
function del(id, img) {
	var url = smurl+'delete&controller=admin&id='+id;
	el = $ID('sm-'+id);
	img.src = 'components/com_smfaq/images/ajax-loader.gif'
	jx.get(url,response);
	function response(data) {
		el.innerHTML = data;
		setTimeout("el.parentNode.removeChild(el);",2000);
	}
}


