// JScript File
var user =
{
	exists: false,
	confirmed: true,
	userId: 0
}

function CheckSignin()
{
    var lblLoginMessage = document.getElementById("ctl00_lblLoginMessage");
    if(lblLoginMessage != null && !user.exists) {
        lblLoginMessage.innerHTML = "Please Sign up or Login &nbsp;&nbsp;  ";
        var txtSignin = document.getElementById("ctl00_txtUserName");
        txtSignin.focus();
        return false;
    } else {
        return true;
    }

}
function ShowWelcome(userName)
{

    var lbSignin = document.getElementById("ctl00_lbSignin");
        lbSignin.style.display = "none";
    var lbSignup = document.getElementById("ctl00_lbSignup");
        lbSignup.style.display = "none";
    var lblWelcome = document.getElementById("ctl00_lblWelcome");
        lblWelcome.style.display = 'inline';
        lblWelcome.innerHTML = "Welcome " + userName;
    var lbSignout = document.getElementById("ctl00_lbSignout");
        lbSignout.style.display = "";
    
}

function ajaxFunction()
{
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	    // Internet Explorer
	    try
	    {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	    catch (e)
	    {
		    try
		    {
		        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		    }
		    catch (e)
		    {
		        alert("Your browser does not support AJAX!");
		        return false;
		    }
	    }
	}
}
  
var xmlHttp;
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
	    // Firefox, Opera 8.0+, Safari
	    xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
	    // Internet Explorer
	    try
	    {
	      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	    catch (e)
	    {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	}
	return xmlHttp;
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
	}
}

function showHint(str)
{
	if (str.length==0)
	{ 
		document.getElementById("txtHint").innerHTML="";
		return;
	}
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="gethint.asp";
	url=url+"?q="+str;
	url=url+"&sid="+Math.random();
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

var is_being_rated = false;
var ratings_max = "5";
	
// When User Mouse Over Ratings
function current_rating(id, rating, rating_text) {
	if(!is_being_rated) 
	{
		post_id = id;
		post_rating = rating;
		for(i = 1; i <= rating; i++) {
			var star = document.getElementById('rating_' + post_id + '_' + i); //].src ="Images/rating_over.gif";
			star.className = "star";
		}
	}
}

// When User Mouse Out Ratings
function ratings_off(id, everage, ratings_max) {

	if(!is_being_rated) 
	{
        var updateRating =  document.getElementById("ctl00_cpMain_txtRating" + id);;
       //5.0(1)
       var oldRating = new String(updateRating.value);
       var av = 0;
       var ceil=0;
       var floor = 0;
       if(everage !="")
       {
           av = everage;//oldRating.substring(0, oldRating.indexOf("("));
           floor = Math.floor(av);
           ceil = Math.ceil(av);
       }

    	post_id = id;
		for(i = 1; i <= ratings_max; i++) {
			var star = document.getElementById('rating_' + post_id + '_' + i); //].src ="Images/rating_over.gif";
			if(i <= floor) {
				star.className = "lightstar";
			} else if(i == ceil) {
				star.className = "lighthalf";
			} else {
				star.className = "graystar";
			}
		}
		if(document.getElementById('ratings_' + post_id + '_text')) 
		{
			document.getElementById('ratings_' + post_id + '_text').style.display ="none";// 'inline';
			document.getElementById('ratings_' + post_id + '_text').innerHTML = '&nbsp;';
		}

	}
}

function showinput(id) {
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		if (document.getElementById('answerinput').style.display == 'block')
			document.getElementById('answerinput').style.display = 'none';
		else
			document.getElementById('answerinput').style.display = 'block';
	}
}

function DoRating(id, rating, rating_text, ratingType)
{
    //if(!CheckSignin()) return;
	if(is_being_rated) return;
    if(id==0) return;
    
    post_id=id;   
 
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null) {
        alert ("Your browser does not support AJAX.  It's time to upgrade."); 
        return;
    }
    var url="Rating.aspx";
    url=url+"?id="+id;
    if(ratingType == "answertext"){
        var saveanswer =  document.getElementById("ctl00_cpMain_txtSaveAnswer");
        url=url+"&rating="+saveanswer.value;
    } else {
        url=url+"&rating="+rating;
    }
    url=url+"&ratingType="+ratingType;
    xmlHttp.onreadystatechange=ShowRating;//('ratings_' + post_id + '_text');
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    
    for(i = 1; i <= ratings_max; i++) {
		var star = document.getElementById('rating_' + post_id + '_' + i); 
        if(i <= rating) {		
			star.className = "star";	        
        } else {
	        star.className = "graystar";	    
        }
		var star = Object;
		star = document.getElementById("rating_" + id+"_" +i);
		if(star != null){ 
			star.onmouseout = function() {};
			star.onmouseup = function() {};
			star.onmouseover = function() {}; 
		}
    }

    var updateRating = Object;
    if( ratingType ==  "answer" ) updateRating = document.getElementById("ctl00_cpMain_txtRating" + id);
    if( ratingType == "question") updateRating = document.getElementById("ctl00_cpMain_txtRating" + id);

    var oldRating = new String(updateRating.value);
    updateRating.value = oldRating*1 + 1;

    //udpate score
    var updateScore = Object;
    if(ratingType =="answer")updateScore = document.getElementById("ctl00_cpMain_txtScore" + id);;
    if (ratingType == "question") updateScore = document.getElementById("ctl00_cpMain_txtScore" + id);;
    //5.0(1)
    var oldScore = new String(updateScore.value);

    updateScore.value = Math.round( (updateScore.value*1 + (rating-3) * 10 * (Math.exp((0.0-oldRating)/8) - Math.exp((0.0-updateRating.value)/8)))*10)/10;

    if(rating == 0)//abuse
    { //hide this question section
        var trTitle = document.getElementById("ctl00_cpMain_trTitle" + id);;
        var trDetail = document.getElementById("ctl00_cpMain_trDetail" + id);;
        var trAsked = document.getElementById("ctl00_cpMain_trAsked" + id);;
        var trAnswer = document.getElementById("ctl00_cpMain_trAnswer" + id);;
        if(trTitle != null)trTitle.style.display ="none";
        if(trDetail != null)trDetail.style.display ="none";
        if(trAsked != null)trAsked.style.display ="none";
        if(trAnswer != null)trAnswer.style.display ="none";

        window.open("popupemail.aspx?f=abuse&id="+id + "&rt=" + ratingType, "Helpglobe","menubar=0,toolbar=0,resizable=1,width=850,height=600"); 
    }
    var cookie = "ratedQs";
    var cookValue = getCookie(cookie) + "q" + id + ",";
    if(ratingType == "answer")
    {
        cookie = "ratedAs";
        cookValue = getCookie(cookie) + "a" + id + ",";
    }
	if( user.userId != 0) 
	{
		cookValue = "";
	    setCookie("ratedAs", cookValue, 1);
	    setCookie("ratedQs", cookValue, 1);
	}
    setCookie(cookie, cookValue, 365);
}

function ShowDialog(url)
{                    
    var oWnd = window.radopen(null, "DialogWindow");
    oWnd.SetSize (510, 550);
    oWnd.SetUrl(url);
}

function ShowRating() 
{ 
    if (xmlHttp.readyState==4)
    { 
    //alert("ajax:" + xmlHttp.responseText);
    }
}

function imposeMaxLength(Object, MaxLen)
{
	if (Object.value.length > MaxLen)
		Object.value = Object.value.substring(0, MaxLen);
}

//for question page search
function AskQuestion_OnKeyUp(e, oThis)
{
	TriggerAjax(e, oThis, 'ctl00_cpMain_tblSearch');
    var key=e.keyCode || e.which;
	if (key!=32 && key!=13) return;
	var btn = document.getElementById("ctl00_cpMain_btnSearch");
	btn.click();
}
function Question_OnKeyUp(e, oThis)
{
	TriggerAjax(e, oThis, 'ctl00_tblSearch');
}
function TriggerAjax(e, oThis, htmlTable)
{
    var key=e.keyCode || e.which;
	if (oThis.value.length < 3) {
		var tbl = document.getElementById(htmlTable);
		if (tbl.rows.length > 0) tbl.deleteRow(0);
	}
	if (key!=32 && key!=13) return;
    var objPasses = document.getElementById("ctl00_txtQuestionFiltered");
	objPasses.value = objPasses.value *1.0 + 1;
	if (true || objPasses.value == "2") {
		CallServer(oThis.value);
		objPasses.value = 1;
		
		var tbl = document.getElementById(htmlTable);
		for(var i = tbl.rows.length - 1; i >= 0; i--) tbl.deleteRow(i);  // delete all
		var row = tbl.insertRow(0);
		var cellLeft = row.insertCell(0);
		var textNode = document.createTextNode(0);
		cellLeft.appendChild(textNode);  // one row only.. keeps it clean.
		cellLeft.innerHTML = "<img src=images/loadgraphic.gif />";
		//tbl.innerHTML = "Searching for similar questions...";
	}
}

function ReceiveAskData(rValue)
{   
	FillTable(rValue, 'ctl00_cpMain_tblSearch');
}
function ReceiveServerData(rValue)
{   
	FillTable(rValue, 'ctl00_tblSearch');
}
function FillTable(rValue, htmlTable)
{   
	var tbl = document.getElementById(htmlTable);
	if (rValue.length > 0) 
	{
		for(var i = tbl.rows.length - 1; i > 0; i--) tbl.deleteRow(i);
		tbl.rows[0].cells[0].innerHTML = rValue;
	}
	else
		tbl.rows[0].cells[0].innerHTML = "";
}

function Question_OnKeyDown()
{
    var objQuestion = document.getElementById("ctl00_txtQuestion");
    if(objQuestion.value=="Search or Ask your question here.")
    {
        objQuestion.value = "";
        objQuestion.style.color = "black";
    }
}

function ValidateQuestion()
{
    var objQuestion = document.getElementById("ctl00_txtQuestion");
    if(objQuestion.value.length>0) {
        return CheckSignin();
    } else {
        objQuestion.value = "Ask your question here."; //"Please type your question here.";

        objQuestion.focus();
        objQuestion.style.color = "gray";
        return false;
    }
}

function Password_KeyPress(e, oThis)
{
    var key=e.keyCode || e.which;
    // alert(key);
    if (key==13){
        Signin_Onlcik();
    }
}

function Signin_Onlcik()
{
    var txtSubmit = document.getElementById("ctl00_cpMain_txtSubmit");
    txtSubmit.value = "signin";
    return true;
}

function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
    if (document.cookie.length>0)
    {
	    c_start=document.cookie.indexOf(c_name + "=")
	    if (c_start!=-1)
        { 
	        c_start=c_start + c_name.length+1 
	        c_end=document.cookie.indexOf(";",c_start)
	        if (c_end==-1) c_end=document.cookie.length
	        return unescape(document.cookie.substring(c_start,c_end))
        } 
    }
    return ""
}

function OpenWin(url)
{
    window.open(url);
    return false;
}

    function ShowDetail()
    {
        
        var tbltblAnswer = document.getElementById("tblMoreDetail");
        if(tbltblAnswer.style.display == "")
            tbltblAnswer.style.display = "none";
        else
            tbltblAnswer.style.display = "";
    }
    
    function SetSubmitText(pn)
    {
            var subm =  document.getElementById("ctl00_cpMain_txtSubmit");
            var currPage =  document.getElementById("ctl00_cpMain_txtCurrentPage");
            currPage.value = pn;
            subm.value = pn;
            return true;
	}

    function MoreAnswers(qId)
    {
        document.location.href="answers.aspx?qId=" + qId;
    }
    
    function SubmitAnswer(answerTextBoxId, qId)
    {
		//alert(qId);
		var answer =  document.getElementById(answerTextBoxId);
		//alert(answer);
		if(answer.value == "") 
		{
		   alert("Please enter your answer");
		   answer.focus();
		   return false;
		}
		var saveanswer =  document.getElementById("ctl00_cpMain_txtSaveAnswer");
		saveanswer.value =answer.value;
		var saveanswerqid =  document.getElementById("ctl00_cpMain_txtSaveAnswerqId");
		saveanswerqid.value = qId;
		var subm =  document.getElementById("ctl00_cpMain_txtSubmit");  //SS- not needed anymore...
		subm.value = answerTextBoxId;
		return true;
    }    
    
	function SubmitComments(commentId)
    {
        if(!CheckSignin()) return false;
        var answer =  document.getElementById(commentId);
        if(answer.value.replace(/^\s+|\s+$/, '') == "") 
        {
           alert("Please enter your comments");
           answer.focus();
           return false;
        }
		var aid = document.getElementById("ctl00_cpMain_txtAnswerId");
		aid.value = commentId;  // C# is ToolTip = contains parent answer ID
		var cmt = document.getElementById("ctl00_cpMain_txtSaveComment");
        cmt.value = answer.value;
        return true;
    }
	
	function SubmitMyAnswer()
    {
		var answer =  document.getElementById("ctl00_cpMain_txtAnswer");
		//alert(answer);
		if(answer.value == "") 
		{
		   alert("Please enter your answer");
		   answer.focus();
		   return false;
		}
		var subm =  document.getElementById("ctl00_cpMain_txtSubmit");
		subm.value = "SubmitMyAnswer";
		return true;
    }    
    
