if(typeof(jQuery)!="undefined"){
var $=jQuery.noConflict();
}

var resubmit = 0;
var resubmit1=0;
var fetchedAnswer=0;
var answersSet;
var alias;
var unameflag = 0;
var greetFlag=0;
var unsatisfiedFlag=0;
var freshPage=1;
var firstload="";
var cnxt="";
var prevres=0;
var countMainBusyInterval=0;
var startNoResponseCounter=0;
var qMRT;

var widgetName=null;
var widgetEmail=null;

var isExplainMoreClicked=0;


var isCloseClicked=0;
var nnfInterval=500;

var infoNameEmailSent=0;

var acceptedOnce=0;


//alert(document.getElementById('server_url').value);
var domain = document.referrer;
if(domain=="")
	domain = window.location.host;

if(domain==document.domain){
	domain=document.location.href;
}	


var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		//alert(data[10].string.indexOf(data[10].subString));
		
		for (var i=0;i<data.length;i++)	{
			
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				
				var txt=new RegExp(data[i].subString,"g");
				
				var str = dataString.match(txt);
				//alert(i + " :: " + data[i].subString + " :: " + str);
			
				
				if (str!=null)				
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		}	
		
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();



	
//alert("dff"+domain);
//alert(document.location.host);
//alert(document.location.hostname);
//alert(document.location.href);
//alert(document.location.pathname);
//var busymsge = "Sorry, all representatives are currently busy assisting other customers.";
//busymsge+=" Please enter your email address below and we will answer your question as soon as possible.";
var dont_unstd_msge="Sorry, please rephrase your question.";
var unsatisfied_msg = "Please provide us with your name and email address and we will send you a detailed answer. Thank you!";
//$(window).load(function() {
if(typeof(jQuery)!="undefined"){						  
$(document).ready(function() {
						   
						   
	if($.fn.rating){
		if($(".rating").length>0){
			$(".rating").each(function(){
									   
				showrating1($(this).attr("id"));						   
			});
			
		}
		
	}
						   
	if($.fn.zclip){				   
	$("#widgetcode").zclip({
    path: "../../includes/js/ZeroClipboard.swf",
     copy: function(){
		 $(this).focus();
		 return $(this).val();
	 }


});
	
	$(".faqToClipboard").zclip({	
    path: "../../includes/js/ZeroClipboard.swf",
	 copy:function(){return $("input#" + $(this).attr("title")).val();},
	 afterCopy:function(){alert("The Dynamic FAQ\'s script for this category has been copied to your clipboard. Simply paste this script into the source code of any page of your website to generate your Dynamic FAQ\'s.");}

     				

});
	
	
	}
	
	
	$("input#widgetReqID").click(function(){
		if($(this).attr("checked")==true){
			 $("input#checkReqID[type=hidden]").val(1);
		}else{
			$("input#checkReqID[type=hidden]").val(0);
		}
		var bigStringToWidget='<script type="text/javascript">';
			bigStringToWidget +='var btnType=' + $("input#checkBtnType[type=hidden]").val() + ';';
			bigStringToWidget +='var btnMedia="' + $("input#checkBtnMedia[type=hidden]").val() + '";';
			bigStringToWidget +='var btnClass="' + $("input#checkBtnClass[type=hidden]").val() + '";';
			bigStringToWidget +='var reqID=' + $("input#checkReqID[type=hidden]").val() + ';';
			bigStringToWidget +='var quesMaxChar="' + $("input#checkquesMaxChar[type=hidden]").val() + '";';
			bigStringToWidget +='var deployPath="' + $("input#server_url").val() + '";';
			bigStringToWidget +='</script><script type="text/javascript" src="' + $("input#server_url").val() + $("#checkJSNewerVersion").val() + '"></script>';
			$("textarea#widgetcode").val();			
			$("textarea#widgetcode").val(bigStringToWidget);
	});
	
	
	$("input#11[name=quesmaxchar]").keyup(function(){
			$("input#checkquesMaxChar[type=hidden]").val($(this).val());
			var bigStringToWidget='<script type="text/javascript">';
			bigStringToWidget +='var btnType=' + $("input#checkBtnType[type=hidden]").val() + ';';
			bigStringToWidget +='var btnMedia="' + $("input#checkBtnMedia[type=hidden]").val() + '";';
			bigStringToWidget +='var btnClass="' + $("input#checkBtnClass[type=hidden]").val() + '";';
			bigStringToWidget +='var reqID=' + $("input#checkReqID[type=hidden]").val() + ';';
			bigStringToWidget +='var quesMaxChar="' + $("input#checkquesMaxChar[type=hidden]").val() + '";';
			bigStringToWidget +='var deployPath="' + $("input#server_url").val() + '";';
			bigStringToWidget +='</script><script type="text/javascript" src="' + $("input#server_url").val() + $("#checkJSNewerVersion").val() + '"></script>';
			$("textarea#widgetcode").val();			
			$("textarea#widgetcode").val(bigStringToWidget);
	});
	
	$("div.widgetDH :button").click(function(){
		switch($(this).attr("title")){
			case "widgetDH1":
			$("input#checkBtnType[type=hidden]").val(1);
			$("input#checkBtnMedia[type=hidden]").val($("input#widgetText").val());
			$("input#checkBtnClass[type=hidden]").val(" class=\'widgetText\'");
			$("input[name=wBT2]").attr("checked", false);
			$("input[name=wBT3]").attr("checked", false);
			$("input[name=wBT4]").attr("checked", false);
			break;
			case "widgetDH2":
			if($("input[name=wBT2]:checked").length==0){
				if($("input#wBT2-4").val()=="1"){
					alert("Click on the Upload button to upload the image");
					return false;
				}
			}
			$("input#checkBtnType[type=hidden]").val(2);
			if($("input[name=wBT2]:checked").length!=0){
							$("input#checkBtnMedia[type=hidden]").val("<img border='0' src='" + $("input[name=wBT2]:checked").next("img").attr("src") + "' />");
			}
			$("input#checkBtnClass[type=hidden]").val(" class=\'widgetImg\'");
			$("input[name=wBT3]").attr("checked", false);
			$("input[name=wBT4]").attr("checked", false);
			
			break;
			case "widgetDH3":
			if($("input[name=wBT3]:checked").length==0){
				if($("input#wBT3-4").val()=="1"){
					alert("Click on the Upload button to upload the image");
					return false;
				}
			}
			$("input#checkBtnType[type=hidden]").val(3);
			if($("input[name=wBT3]:checked").length!=0){
			$("input#checkBtnMedia[type=hidden]").val("<img border='0' src='" + $("input[name=wBT3]:checked").next("img").attr("src") + "' />");
			}
			$("input#checkBtnClass[type=hidden]").val(" class=\'widgetFL\'");
			$("input[name=wBT2]").attr("checked", false);
			$("input[name=wBT4]").attr("checked", false);
			break;
			case "widgetDH4":
			if($("input[name=wBT4]:checked").length==0){
				if($("input#wBT4-4").val()=="1"){
					alert("Click on the Upload button to upload the image");
					return false;
				}
			}
			$("input#checkBtnType[type=hidden]").val(4);
			if($("input[name=wBT4]:checked").length!=0){
			$("input#checkBtnMedia[type=hidden]").val("<img border='0' src='" + $("input[name=wBT4]:checked").next("img").attr("src") + "' />");
			}
			$("input#checkBtnClass[type=hidden]").val(" class=\'widgetFR\'");
			$("input[name=wBT2]").attr("checked", false);
			$("input[name=wBT3]").attr("checked", false);
			break;
		}
		$("div#" + $(this).attr("title")).fadeOut("fast");	
		if(BrowserDetect.browser=="Explorer" && BrowserDetect.version==7)
			{	
				$("table.tablebstyle div.widgetCode").css("position", "relative");
			}
			var bigStringToWidget='<script type="text/javascript">';
			bigStringToWidget +='var btnType=' + $("input#checkBtnType[type=hidden]").val() + ';';
			bigStringToWidget +='var btnMedia="' + $("input#checkBtnMedia[type=hidden]").val() + '";';
			bigStringToWidget +='var btnClass="' + $("input#checkBtnClass[type=hidden]").val() + '";';
			bigStringToWidget +='var reqID=' + $("input#checkReqID[type=hidden]").val() + ';';
			bigStringToWidget +='var quesMaxChar="' + $("input#checkquesMaxChar[type=hidden]").val() + '";';
			bigStringToWidget +='var deployPath="' + $("input#server_url").val() + '";';
			bigStringToWidget +='</script><script type="text/javascript" src="' + $("input#server_url").val() + $("#checkJSNewerVersion").val() + '"></script>';
			
			$("textarea#widgetcode").val();			
			$("textarea#widgetcode").val(bigStringToWidget);
			
	});
						  
	$("input#widgetBtnType1").click(function(){	
			if(BrowserDetect.browser=="Explorer" && BrowserDetect.version==7)
			{	
				$("table.tablebstyle div.widgetCode").css("position", "static");
			}
			$("div#widgetDH2").fadeOut("fast");
			$("div#widgetDH3").fadeOut("fast");
			$("div#widgetDH4").fadeOut("fast");
			$("div#widgetDH1").fadeIn("fast");
	});
	$("input#widgetBtnType2").click(function(){
			if(BrowserDetect.browser=="Explorer" && BrowserDetect.version==7)
			{	
				$("table.tablebstyle div.widgetCode").css("position", "static");
			}								 
			$("div#widgetDH1").fadeOut("fast");	
			$("div#widgetDH3").fadeOut("fast");
			$("div#widgetDH4").fadeOut("fast");
			$("div#widgetDH2").fadeIn("fast");
			$("div#widgetDH2 input#wBT2_1").attr("checked", true);
	});
	$("input#widgetBtnType3").click(function(){
			if(BrowserDetect.browser=="Explorer" && BrowserDetect.version==7)
			{	
				$("table.tablebstyle div.widgetCode").css("position", "static");
			}								 
			$("div#widgetDH1").fadeOut("fast");								 
			$("div#widgetDH2").fadeOut("fast");			
			$("div#widgetDH4").fadeOut("fast");
			$("div#widgetDH3").fadeIn("fast");
			$("div#widgetDH3 input#wBT3_1").attr("checked", true);
	});
	$("input#widgetBtnType4").click(function(){
			if(BrowserDetect.browser=="Explorer" && BrowserDetect.version==7)
			{	
				$("table.tablebstyle div.widgetCode").css("position", "static");
			}								 
			$("div#widgetDH1").fadeOut("fast");								 
			$("div#widgetDH2").fadeOut("fast");
			$("div#widgetDH3").fadeOut("fast");
			$("div#widgetDH4").fadeIn("fast");
			$("div#widgetDH4 input#wBT4_1").attr("checked", true);
	});
						   
						   
	$("div.questiontd span.questionDelete").live("click", function(){
		if($("input#storageLimitExceeded").length>0){
			show_overlay("It appears that you have reached your data storage limit, to continue to using ResponseQue click here to upgrade your account.");
			return false;
		}
		if(deleteCurRecord()){
			queue_ajaxRemoveQueue($(this).attr("title"));
		}
	});
	
	$("div.autoSuggestions").live("click", function(){
											 
		var fillTextIn=$(this).find("span.cOAS").html();
		
		$('#flatbox').val(fillTextIn);
		setTimeout("$('#suggestions').hide();", 200);
											   });		
			   
   queue_starttimer();
  

});
}
function getHttpObj()
{
	var xmlHttp = null;
	try
	{
		xmlHttp=new XMLHttpRequest();
		
	}
	catch (e)
	{
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			
		}
		catch (ea)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				
			}
			catch (eaa)
			{
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

function notAllowedForUserMessage(){
	alert("This is being answered by another agent at this time."); return false;
}


function uncheckCheckedFields(a){
	$("input[name="+a+"]").attr("checked", false);
	$("input#"+a+"-4").val(1);
	$("div#"+a+"-5").html("Image ready for upload.");
}

function updateFileField(txt, a){
	$("input#"+a+"-4").val(txt);
	$("div#"+a+"-5").html("Image is uploaded succcessfully. <a href='" + txt + "' target='_blank'>See it</a>");
	$("input#checkBtnMedia[type=hidden]").val("<img border='0' src='" + txt + "' />");
}

function queueMonitorResponseTime(a){
	//$("span#monitorqMRTFunc").html("when it calls " + countMainBusyInterval + " :: " + a);
	startNoResponseCounter=1;
	qMRT=window.setInterval("qMRTFunc('" + a + "')", 1000);
}

function qMRTFunc(a){
	countMainBusyInterval++;
	//$("span#monitorqMRTFunc").html(countMainBusyInterval);
	if(countMainBusyInterval>a){
		window.clearInterval(qMRT);
	}
}


function dispachNNFFields(){
	resubmit1++;
	var ques="";
	if(document.getElementById('question')){		
		ques=document.getElementById('question').value;
		document.getElementById('hiddenques').value=ques;
	}if(!document.getElementById('question')){
		ques=document.getElementById('hiddenques').value;
	}
	
	
	var postData={
		"question": ques,
		"domain": domain,
		"resubmit": resubmit1,
		"expl": cnxt,
		"name": widgetName,
		"email": widgetEmail
	}
	
	$.ajax({
						   type: "POST",
						   url: document.getElementById('server_url').value+"/includes/cActionDispatcher.php?action=getNFFields",
						   data: postData,
						   dataType: "html",
						   error: function(){
							 alert("There is a problem with this request.");  
						   },
						   success: function(responseText){
							   if(responseText==1){
								if(isCloseClicked==0){													
									setFormFieldMessage();
								}
							}else{
									var nnfStart=window.setTimeout(function(){dispachNNFFields();}, nnfInterval);				
							}
	
						   }
	});
	
	
}


function dispachQuestion(){
	if(isCloseClicked==1) return false;
	var nnfStart=window.setTimeout(function(){dispachNNFFields();}, nnfInterval);
	
	
	var mainSubmitInterval=parseInt($("#resubmitinterval").val());
	
	
	
	
	if(document.getElementById('question'))
		if($.trim(document.getElementById('question').value)=="" || $.trim(document.getElementById('question').value).length<2)
			return false;
	resubmit++;
	
	
	var ques="";
	if(resubmit==1){		
		ques=document.getElementById('question').value;
		document.getElementById('hiddenques').value=ques;
	}else if(resubmit>=2){
		ques=document.getElementById('hiddenques').value;
	}
	
	afterQuestionAsked(ques);
	
	freshPage=0;	
	
	var postData={
		"question": ques,
		"domain": domain,
		"resubmit": resubmit,
		"expl": cnxt,
		"name": widgetName,
		"email": widgetEmail
	}
	
	$.ajax({
						   type: "POST",
						   url: document.getElementById('server_url').value+"/includes/cActionDispatcher.php?action=getans",
						   data: postData,
						   dataType: "xml",
						   error: function(){
							 alert("There is a problem with this request.");  
						   },
						   success: function(xml){
							   
			//document.getElementById('test').innerHTML=objques.responseText;
			//alert(objques.responseText);
			try{
				//$("#monitorqMRTFunc").html(countMainBusyInterval + " : " + mainSubmitInterval);
				//alert(xmltext.getElementsByTagName("responsecode")[0].childNodes[0].nodeValue);
				if($("responsecode", xml).text()=="01"){
					
					
					//alert(xmltext.getElementsByTagName("responsecode")[0].childNodes[0].nodeValue);
					answersSet=xml;
					var q_id = $("q_id", xml).text();
					var question = $("question", xml).text();
					var answer = $("answer", xml).text();
					var type = $("type", xml).text();
					var alias = $("alias", xml).text();
					if(type=='greeting' || type=='queryback'){
						greetFlag=1;
						resubmit=0;
						showResponse(q_id,question,answer,alias,0);
						countMainBusyInterval=0;
					}
					else{
						greetFlag=0;
						resubmit=0;
						//showResponse(q_id,question,answer,alias,1);					
						
						showNextAns('first');
					}
					
				}
				else if($("responsecode", xml).text()=="11"){
					//alert('after office hours');
					setBusyMessage('afterhour');
				}				
				/*
				else if(xmltext.getElementsByTagName("responsecode")[0].childNodes[0].nodeValue=='10'){
					if(resubmit<23){
						//alert(resubmit);
						document.getElementById('dynamic_content').innerHTML="<table><tr><td width='15px'><div class='loading'></div></td><td class='waitmsg'><font size='2'>"+waitmsg+"</font></td></tr></table>";
						setTimeout('dispachQuestion()',5000);
					}
					else {
						setBusyMessage('timeout');
					}
				}
				*/
				
				else if($("responsecode", xml).text()=='10'){	
				acceptedOnce=1;				
						document.getElementById('dynamic_content').innerHTML="<table><tr><td width='15px'><div class='loading'></div></td><td class='waitmsg'><font size='2'>"+waitmsg+"</font></td></tr></table>";
						setTimeout('dispachQuestion()',5000);
					
				}
				else if($("responsecode", xml).text()=='00'){					
					acceptedOnce=0;
				if(startNoResponseCounter==0){
					$('input#checkQueryID').val($("queue", xml).text());
					queueMonitorResponseTime(mainSubmitInterval);
				}	
					if(countMainBusyInterval-mainSubmitInterval<0){						
						setTimeout('dispachQuestion()',($("#refreshinterval").val())*1000);
					}
					else{ 
						isCloseClicked=1;
						setBusyMessage('timeout');						
					}
				}
			}
			catch(e){
				alert("problem here");
				setBusyMessage('timeout');
			}
		return false;
						   }
					});	
	

}

function direct_queue(a){
	var mainSubmitInterval=parseInt($("#resubmitinterval").val());
	
	var parentquesId= answersSet.getElementsByTagName("q_id")[0].childNodes[0].nodeValue;
	ques=document.getElementById('hiddenques').value;
	
	var postData={
		"question": ques,
		"domain": domain,
		"parentqid": parentquesId,
		"name": widgetName,
		"email": widgetEmail
	}
	
	$.ajax({
						   type: "POST",
						   url: document.getElementById('server_url').value+"/includes/cActionDispatcher.php?action=puttoq",
						   data: postData,
						   dataType: "html",
						   error: function(){
							 alert("There is a problem with this request.");  
						   },
						   success: function(responseText){
							    isExplainMoreClicked=1;
								cnxt="Explain";
								typ="explainmore";
								resubmit=1;
								//alert("a = " + a);
								if(a==1){
									countMainBusyInterval=0;
									startNoResponseCounter=0;
								}
								dispachQuestion();
						   }
	});
	
	
	
}

function save_rating(rate){
	
	var objrate = getHttpObj();
	if(document.getElementById('q_id')){
	var qid=document.getElementById('q_id').value;
	}
	objrate.onreadystatechange=function()
	{
		if(objrate.readyState==4)
		{	
			document.getElementById('rate1').innerHTML='Thank You';
			setTimeout('resetAll()',2000);
		}
	}
	var url = $('#server_url').val()+"/includes/cActionDispatcher.php?qid="+qid+"&rate="+rate+"&action=saverate";
	objrate.open("get",url,true);
	objrate.send(null);	
}

function save_rating1(rate, id){
	var objrate = getHttpObj();
	
	var qid=$('#'+id).attr("title");
	
	objrate.onreadystatechange=function()
	{
		if(objrate.readyState==4)
		{	
			$('#'+id).html('Thank You');
		}
	}
	var url = document.getElementById('server_url').value+"/includes/cActionDispatcher.php?qid="+qid+"&rate="+rate+"&action=saverate";
	objrate.open("get",url,true);
	objrate.send(null);	
}

function saveEmailId(){
	var objemail = getHttpObj();
	if(infoNameEmailSent==0){
	var email=document.getElementById('emailid').value;
	}else{
		email=widgetEmail;
	}
	if(!validateEmail(email)){
		if($("input#isRequiredID").val()==1){
			var errNoMailMessage=$("<div />").attr("class", "errMessageInput").html("Please enter in a valid email").css({"width": $("input#emailid").width()-5 + "px", "height": $("input#emailid").height()+5 + "px"});
			if($("div.errMessageInput").length==0){
				errNoMailMessage.insertBefore($("input#emailid"));
				$("div.errMessageInput").fadeIn("fast").click(function(){$(this).remove();$("input#emailid").focus();});
			}
		}else{
			$('#serverresponse').html('<p class="srQuestAns"><font class="serverres"><strong>Please enter in a valid email address below...</strong></font></p>');
		}
		return false;		
	}
	if(unsatisfiedFlag){
		//direct_queue();
	}
	if(infoNameEmailSent==0){
	var name=document.getElementById('name').value;
	}else{
		name=widgetName;
	}
	var ques=document.getElementById('hiddenques').value;
	if($("input#isRequiredID").val()==1){
		widgetName=name;
		widgetEmail=email;
		$('input#checkName').val(name);
		$('input#checkEmail').val(email);
		resetAll(1);
		dispachQuestion();
		//resetAll(1);
		return false;
	}
	var queryID=$('input#checkQueryID').val();
	//alert(email+name+ques+domain);
	objemail.onreadystatechange=function()
	{
		if(objemail.readyState==4)
		{	
			//alert(objemail.responseText);
			if($("input#isRequiredID").val()==0){
			widgetName=name;
			widgetEmail=email;	
			document.getElementById('dynamic_content').innerHTML="<span class='widget_replymsg'>"+thnkmsg+"</span>";
			infoNameEmailSent=1;
			setTimeout('resetAll()',2000);
			}else if($("input#isRequiredID").val()==1){
				$('input#checkName').val(name);
				$('input#checkEmail').val(email);
				$('input#checkQueryID').val(objemail.responseText);
				$('#dynamic_content').html("");
				resetAll(1);
			}
		}
	}
	var url = document.getElementById('server_url').value+"/includes/cActionDispatcher.php?email="+email+"&name="+name+"&ques="+escape(ques)+"&domain="+domain+"&queryID="+queryID+"&action=saveemail";
	objemail.open("get",url,true);
	objemail.send(null);	
}

function dispachQuestion1(){
	saveEmailId();
}


function queue_ajaxReadQus(){
	var url;
	var objread = getHttpObj();
	objread.onreadystatechange=function(){

		
		if(objread.readyState==4){
			document.getElementById("insidecontents_leftdiv").innerHTML = objread.responseText;
			
			var sng="1.8em";
			var dbl="1.52em"
			
			if($.browser.msie){
				var sng="1.98em";
				var dbl="1.70em"
			}
			
			if($.browser.safari){
				var sng="1.95em";
				var dbl="1.68em"
			}
			
			$('.timer').each(function(){
				var ln=$.trim($(this).val()).length;
				if(ln==2)
					$(this).css({'padding-left':dbl});
				else 
					$(this).css({'padding-left':sng});	
			});
			
			
			
			if (navigator.appVersion.indexOf("Win")!=-1){
				if($.browser.mozilla){
					$('.timer').each(function(){
						$(this).css({'padding-bottom':'2px'});
						
					});
				}else if($.browser.safari){
					$('.timer').each(function(){
						$(this).css({'padding-top':'4px'});
					});
				}else if($.browser.msie){
					$('.timer').each(function(){
						$(this).css({'padding-top':'2px'});
					});
				}
			}
			
			var leftdiv=document.getElementById("insidecontents_leftdiv");
			//var rightdiv=document.getElementById("questions_rightdiv");

			
			//rightdiv.style.height=leftdiv.offsetHeight+"px";
			
			
		}
	}
	url = "../view/ajaxqueue.php";
	objread.open("get",url,true);
	objread.send(null);
}
function queue_ajaxReadQusEmail(){
	var url;
	var objread = getHttpObj();
	objread.onreadystatechange=function(){
		if(objread.readyState==4){
			document.getElementById("insidecontents_leftdiv").innerHTML = objread.responseText;
			queue_stoptimer(0);
			
			var sng="1.8em";
			var dbl="1.52em"
			
			if($.browser.msie){
				var sng="1.98em";
				var dbl="1.70em"
			}
			
			if($.browser.safari){
				var sng="1.95em";
				var dbl="1.68em"
			}
			
			$('.timer').each(function(){
				var ln=$.trim($(this).val()).length;
				if(ln==2)
					$(this).css({'padding-left':dbl});
				else 
					$(this).css({'padding-left':sng});	
			});
			
			
			
			if (navigator.appVersion.indexOf("Win")!=-1){
				if($.browser.mozilla){
					$('.timer').each(function(){
						$(this).css({'padding-bottom':'2px'});
						
					});
				}else if($.browser.safari){
					$('.timer').each(function(){
						$(this).css({'padding-top':'4px'});
					});
				}else if($.browser.msie){
					$('.timer').each(function(){
						$(this).css({'padding-top':'2px'});
					});
				}
			}
			
			var leftdiv=document.getElementById("insidecontents_leftdiv");
			//var rightdiv=document.getElementById("questions_rightdiv");

			
			//rightdiv.style.height=leftdiv.offsetHeight+"px";
			
			
		}
	}
	url = "../view/ajaxqueueemail.php";
	objread.open("get",url,true);
	objread.send(null);
}

function queue_ajaxSetQusFlag(queue_id,flag,sTO){
	if(typeof(sTO)=="undefined"){
		sTO=0;
	}
	var url;
	var objflag = getHttpObj();
	objflag.onreadystatechange=function(){
		if(objflag.readyState==4){
			var resp = objflag.responseText;			
			if(resp.replace('\n','')=="no"){
					queue_starttimer();
				}
			}
		}
	
	url = document.getElementById('server_url').value+"/includes/cActionDispatcher.php?context=queue_setflag&queue_id="+queue_id+"&flag="+flag+"&stoppedTimerOn="+sTO;
	objflag.open("get",url,true);
	objflag.send(null);

	
}

function queue_ajaxRemoveQueue(queue_id){
	var url;
	var objflag = getHttpObj();
	objflag.onreadystatechange=function(){
		if(objflag.readyState==4){
			var resp = objflag.responseText;
			if(resp==1){
				queue_starttimer();
			}
			if(resp.replace('\n','')=="no"){
					queue_starttimer();
				}
			}
		}
	
	url = document.getElementById('server_url').value+"/includes/cActionDispatcher.php?context=queue_remove&queue_id="+queue_id;
	objflag.open("get",url,true);
	objflag.send(null);

	
}

function queue_ajaxSendAns(queue_id){
	var url;
	var qtype=cnttype.substring(0,cnttype.indexOf('_'));
	var rans=document.getElementsByName("anstext")[0].value;
	$("input.send_ico1").parent("div.sendIcon").prepend("<img src='" + $("input#server_url").val() + "/includes/images/ajax-loader.gif' class='ajaxLoaderImg' />");
	var isglobal=0;
	if(document.getElementById("catList")){	
	var isdynform=0;
	var cat=document.getElementById("catList").value;
	}else{
		cat=0;
		isdynform=1;
	}
	if(document.getElementById("globalans")){
		if(document.getElementById("globalans").checked===true){
	isglobal=document.getElementById("globalans").value;
	}
	}
	
	var expirydate;
	if(document.getElementById("expdate")){
	var expy=document.getElementById("expdate").value;       
	var dateexp=/^\d{4}-(0[0-9]|1[0,1,2])-([0,1,2][0-9]|3[0,1])$/;
	if(expy.match(dateexp)){
		var srpres=document.getElementById("pres").value;
		var dt=expy.split("-");
		var nw=srpres.split("-");
		var now=new Date(nw[0],(nw[1]-1),nw[2]);
		var stdt=new Date(dt[0],(dt[1]-1),dt[2]);
		if(now>stdt)
			expy=document.getElementById("expy").value;
			expirydate=expy+" "+document.getElementById("exphrs").value+":00:00";			
	}else{
		expirydate=""
	}
	}else{
		expirydate="";
	}
	var postData={
		"queue_id": queue_id,
		"rans": rans,
		"expirydate": expirydate,
		"qtype": qtype,
		"category": cat,
		"isglobal": isglobal,
		"isdynform": isdynform
	}
	
	$.ajax({
						   type: "POST",
						   url: document.getElementById('server_url').value+"/includes/cActionDispatcher.php?context=queue_sendanswer",
						   data: postData,
						   dataType: "html",
						   error: function(){
							 alert("There is a problem with this request.");  
						   },
						   success: function(responseText){
							   var isActionSuccessfull=responseText;
								if(parseInt(isActionSuccessfull)==0){
									alert("This question has answered once.\nDelete this Q&A record from the queue.");
									return false;
								}else{
									queue_starttimer();	
								}
						   }
	});
	
}

function queue_ajaxPages(term){
	var objresp=getHttpObj();
	objresp.onreadystatechange=function(){
		if(objresp.readyState==4){
			document.getElementById("pages").innerHTML = objresp.responseText;
		}
	}
	url = document.getElementById('server_url').value+"/modQA/view/sitexmlparser.php?searchterm="+term;
	objresp.open("get",url,true);
	objresp.send(null);
}

function clear() {
  var divs = document.getElementById('dynamic_content').getElementsByTagName('div');
  for (var i=0; i<divs.length; i++) {
    var div = divs[i];
    while (div.hasChildNodes()) {
      div.removeChild(div.firstChild);
    }
  }
}

function afterQuestionAsked(question){	
	clear();
	document.getElementById('dynamic_content').style.height=168 + "px";
	if(freshPage==1)
		document.getElementById('quesasked').innerHTML="<font class='quesasked'><strong></strong>"+question+"</font>";
	else
		if(resubmit==1)	
			document.getElementById('serverresponse').innerHTML=document.getElementById('serverresponse').innerHTML+"<p class='srQuestAns'><font class='quesasked'>"+question+"</font></p>";	
			if(acceptedOnce==0){		
	document.getElementById('dynamic_content').innerHTML="<table><tr><td width='15px'><div class='loading'></div></td><td class='widget_replymsg'><font size='2'>"+connectmsg+"</font></td></tr></table>";
			}
		
}
function showResponse(q_id,question,answer,alias,showlink){
	//alert(q_id + "\n" + question + "\n" + answer + "\n" + alias + "\n" + showlink);
	document.getElementById('q_id').value = q_id;
	//document.getElementById('quesasked').innerHTML="<font class='quesasked'><strong>>&nbsp; </strong>"+question+"</font>";
	if(showlink){
		setHtml(answer,alias,widget_link_form);
	}
	else{
		setHtml(answer,alias,widget_askform);
	}
	window.clearInterval(qMRT);	
	countMainBusyInterval=0;
	startNoResponseCounter=0;
	
}

function showrating(){
	document.getElementById('serverresponse').innerHTML=document.getElementById('serverresponse').innerHTML+"<p class='srQuestAns'><font class='quesasked'> Thanks, that's what I needed to know!</font></p>";
	document.getElementById('dynamic_content').innerHTML=ratings;
	$('#rate1').rating({maxvalue:5, increment:.5});
	
}

function setFormFieldMessage(){
	countMainBusyInterval=0;
	isCloseClicked=1;
	if($("input#isRequiredID").val()==1){
			widget_emailform="";
		}else{
			widget_emailform=widget_emailform1;
		}
	markQueueForm();
	busymsge="Please leave us your contact information below...";
	setHtml(busymsge,alias,widget_emailform);
}


function setBusyMessage(spec){	
	if(spec=='timeout'){		
		document.getElementById('quesasked').innerHTML="<font class='quesasked'><strong></strong>"+document.getElementById('hiddenques').value+"</font>";
		if($("input#isRequiredID").val()==1){
			widget_emailform="";
			
			busymsge=busymsge1;
		}else{
			widget_emailform=widget_emailform1;
			if(infoNameEmailSent==1){
				widget_emailform="";
			
				busymsge=busymsge1;
			}
		}
		markQueueForm();
		if(infoNameEmailSent==1){
			saveEmailId();
		}
		setHtml(busymsge,alias,widget_emailform);
	}
	else if(spec=='afterhour'){
		if($("input#isRequiredID").val()==1){
			widget_emailform="";
			afterhrs=afterhrs1;
		}else{
			widget_emailform=widget_emailform1;
		}
		setHtml(afterhrs,alias,widget_emailform);
	}
	if($("input#isRequiredID").val()==1){
		updateSavedEmailId();
	}
	   
	   var objemail = getHttpObj();
	//document.getElementById('serverresponse').innerHTML="<font color='red' size='2'><strong>ZVendor:</strong><br/>"+busymsge+"</font>";
	//document.getElementById('dynamic_content').innerHTML=widget_emailform;

}

function markQueueForm(quest){
	var objques = getHttpObj();
	var id=$('input#checkQueryID').val();	
	var ques=$('input#hiddenques').val();
					objques.onreadystatechange=function()
					{
						if(objques.readyState==4)
						{
							
						}
					}
					
					
					if(isCloseClicked==1){
					var url = document.getElementById('server_url').value+"/includes/cActionDispatcher.php?queryID="+id + "&action=setNFFields";
	
					//alert(url);
					//document.getElementById('test').innerHTML=url;
					objques.open("get",url,true);
					objques.send(null);
					}
}

function updateSavedEmailId(){
	var objemail = getHttpObj();
	var id=$('input#checkQueryID').val();	
	var ques=$('input#hiddenques').val();	
	//alert(email+name+ques+domain);
	objemail.onreadystatechange=function()
	{
		if(objemail.readyState==4)
		{	
			
		}
	}
	var url = document.getElementById('server_url').value+"/includes/cActionDispatcher.php?email="+widgetEmail+"&name="+widgetName+"&ques="+escape(ques)+"&domain="+domain+"&queryID="+id+"&action=updatesavedemail";
	objemail.open("get",url,true);
	objemail.send(null);	
}





function showNextAns(typ){
	
	var mainSubmitInterval=$("#resubmitinterval").val();
	if(typ!='first')
		document.getElementById('serverresponse').innerHTML=document.getElementById('serverresponse').innerHTML+"<p class='srQuestAns'><font class='quesasked'> Please explain more</font></p>";
	
	var q_tot = answersSet.getElementsByTagName("q_id");
	if(cnxt=="Explain"){
		cnxt="";
		if($(q_tot).length>prevres)
			fetchedAnswer=$(q_tot).length-1;	
		else{
			if(countMainBusyInterval-mainSubmitInterval<0){						
						
				
			}else{
			 setBusyMessage('timeout');
			 return;
			}
		}
		  
		
	}else{
		prevres=$(q_tot).length;
	}
		
	try {
		//alert("it is showNextAns " + typ);		
		var q_id = answersSet.getElementsByTagName("q_id")[fetchedAnswer].childNodes[0].nodeValue;
		var question = answersSet.getElementsByTagName("question")[fetchedAnswer].childNodes[0].nodeValue;
		var answer = answersSet.getElementsByTagName("answer")[fetchedAnswer].childNodes[0].nodeValue;
		var alias = answersSet.getElementsByTagName("alias")[fetchedAnswer].childNodes[0].nodeValue;
		showResponse(q_id,question,answer,alias,1);
	} catch (e) {		
		document.getElementById('quesasked').innerHTML="<font class='quesasked'><strong></strong>"+document.getElementById('hiddenques').value+"</font>";
		firstload="";
		//setHtml(unsatisfied_msg,alias,widget_emailform);
		
		if(startNoResponseCounter==0 && typ==1){
			queueMonitorResponseTime(mainSubmitInterval);
		}
		//if(typ!='first'){	
		//alert(startNoResponseCounter + " : " + $("#refreshinterval").val()*1000 + " / " + mainSubmitInterval);
			window.setTimeout(function(){direct_queue(typ);}, ($("#refreshinterval").val()*1000)); 
		//}
		afterQuestionAsked(document.getElementById('hiddenques').value);
		unsatisfiedFlag=1;
		
		// TODO: handle exception
		
	}
	
	scrolloutput();
	fetchedAnswer++;	
}
function resetAll(a){
	if($("input#isRequiredID").val()==0){
	resubmit = 0;
	resubmit1=0;
	fetchedAnswer=0;
	unsatisfiedFlag=0;
	freshPage=1;
	isCloseClicked=0;
	acceptedOnce=0;
	
	firstload="";
	document.getElementById('quesasked').innerHTML="";
	document.getElementById('serverresponse').innerHTML="";
	document.getElementById('dynamic_content').innerHTML="&nbsp;";
	document.getElementById('dynamic_content').innerHTML=widget_askform;
	$("#quesasked").html("<font class='quesasked'><strong><center>"+welcomemsg+"</center></strong></font>");
	}else if($("input#isRequiredID").val()==1 && a==1){
		resubmit = 0;
		fetchedAnswer=0;
		unsatisfiedFlag=0;
		freshPage=1;
		firstload="";
		$("div#output").show();		
	}else if($("input#isRequiredID").val()==1 && a==0){
		Shadowbox.close();
	}
	countMainBusyInterval=0;
	isWaitAlertMessage=0;
	startNoResponseCounter=0;
}

function setHtml(response,alias,option){
	if(alias==undefined) alias='Customer Service';
	document.getElementById('serverresponse').innerHTML=document.getElementById('serverresponse').innerHTML+'<p class="srQuestAns">'+"<font class='serverres'><strong>"+alias+":&nbsp;</strong>"+response+"</font></p>";
	document.getElementById('dynamic_content').innerHTML=option;
	//document.getElementById('serverresponse').foucs();
	scrolloutput();
	firstload="<br/>";
}
function rephraseQuestion(){
	document.getElementById('serverresponse').innerHTML=document.getElementById('serverresponse').innerHTML+'<p class="srQuestAns">' +"<font class='quesasked'> You don't seem to understand my question!</font></p>";
	resubmit = 0;
	fetchedAnswer=0;
	unsatisfiedFlag=0;
	setHtml(dont_unstd_msge,alias,widget_askform);
	scrolloutput();
	firstload="<br/>";
	//document.getElementById('serverresponse').innerHTML="<font color='red'><strong>ZVendor:</strong><br/>"+dont_unstd_msge+"</font>";
	//document.getElementById('dynamic_content').innerHTML=widget_askform;
	
}
function userVal(name){
	var url;
	var xmlHttp = getHttpObj();
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{	
			if(xmlHttp.responseText == 1){
				unameflag = 1;
				document.getElementById("user_name").innerHTML = "Username already exist";
				if(document.getElementById("context").value=='newuser'){
					document.getElementById("save").disabled = true;
				}
				unameflag=0;
			}else{
				document.getElementById("user_name").innerHTML = "New user";
				if(document.getElementById("context").value=='newuser'){
					document.getElementById("save").disabled = false;
				}
			}
			
		}
	}
	
	url = document.getElementById('server_url').value+"/includes/cActionDispatcher.php?context=userVal&uname="+name;
	
	xmlHttp.open("get",url,true);
	xmlHttp.send(null);
	
}

function sendAjaxEmail(from,to,subject,message,host,username,pass,mailurl,mailpath){
	var url;
	var xmlHttp = getHttpObj();
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			if(xmlHttp.responseText==0){
				alert("Could not connect to the SMTP server. Please check your SMTP configuration.");
			}
			else
			if(xmlHttp.responseText==1){
				alert("Email sent successfully.");
			}
			else
			if(xmlHttp.responseText==2){
				alert("Could not send email. Please try again later or check your SMTP configuration.");
			}
			else{
				alert("Could not connect to the SMTP server. Please check your SMTP configuration.");
			}
			document.getElementById('sendtest').value="Send Test";
		}
	}
	url = mailurl+"/includes/cActionDispatcher.php?action=sendtestemail&from="+from+"&to="+to+"&subject="+subject+"&message="+message+"&host="+host+"&username="+username+"&pass="+pass+"&mailpath="+mailpath;
	xmlHttp.open("get",url,true);
	xmlHttp.send(null);
}

function scrolloutput(){
	var obj = document.getElementById('output');
	obj.scrollTop = obj.scrollHeight;
}


function deleteCurRecord()
{
	if(confirm('You are about to delete this question permanently without answering it.\n\nDo you want to continue?'))
    {
		return true;
	}
	else
	{	
		return false;
	}
}

function showrating1(id){
	$("#"+id).rating({maxvalue:5, increment:.5});
	
}


function dispachQuestionFromForm(){
	var a=/^[\w]/;
	var reMail = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	var countFields=3;
	var countFilled=0;
	var errString="Missing fields:";
	if(!a.test($("input[type=text]#name").val())){
		errString +="\nName,";
	}else{
		countFilled++;
	}
	if(!reMail.test($("input[type=text]#emailid").val())){
		errString +="\nEmail,";
	}else{
		countFilled++;
	}
	if(!a.test($("textarea#question").val())){
		errString +="\nQuestion";
	}else{
		countFilled++;
	}
	if(countFilled>=countFields){
		sendQuestionFromForm();
	}else{
		alert(errString);
		return false;
	}
}

function sendQuestionFromForm(){
	var objemail = getHttpObj();
	var name=$("input[type=text]#name").val();
	var email=$("input[type=text]#emailid").val();
	var ques=$("textarea#question").val();
	
	objemail.onreadystatechange=function()
	{
		if(objemail.readyState==4)
		{
			if(objemail.responseText==1){
				$("div#askAnotherQuestion div.aAQInside").html("<div class='aAQThanks'>" + "Thank you, your message has been received." + "</div>");
				return false;
			}else{
				alert("You have alredy asked the same question");
				return false;
			}
		}
	}
		
		
	
	var url = $("input#server_url").val()+"/includes/cActionDispatcher.php?email="+email+"&name="+name+"&ques="+escape(ques)+"&domain="+domain+"&action=savequestionfromform";
	objemail.open("get",url,true);
	objemail.send(null);
	$("div#askAnotherQuestion div.aAQInside").html("<div class='aAQThanks'>" + connectmsg + "</div>");
}
