﻿// JScript File



var activeRequests=new Array()

function Request(ReqName){
	this.active=false;
	this.url='';
	this.response='';
	this.debug=false;
	this.req=null;
	this.name=ReqName;
	this.oncomplete=null;
	this.XMLErrorCount=0;
	this.MaxError=10;
	this.XMLEnabled=CheckXML()
	//this.XMLEnabled=false
	this.requestindex=activeRequests.length 
	activeRequests[activeRequests.length]=this
}


Request.prototype.send=SendRequest
Request.prototype.writeLog=writeLog
Request.prototype.isActive=function (){return this.active}
Request.prototype.resetErrCount=function (){XMLErrorCount=0}
Request.prototype.sendUsingIframe=sendUsingIframe

function writeLog(Msg){
	if(!this.debug)
		return;
	if(!document.getElementById("ReqDebug"))
		CreateDebug()
	
	document.getElementById("ReqDebug").value+='\n'+this.name+":"+Msg
	
	

}
function CreateDebug(){
	var DebugText=document.createElement("textarea")
	DebugText.id="ReqDebug"
	DebugText.style.width="100%"
	DebugText.style.height="300px"
	document.body.appendChild(DebugText)

}


function SendRequest() 
{
	if (arguments.length>0)
		this.url=arguments[0]
	
	if (this.url=='')
		return false;
	
	if (this.active)
		return false;
		
	if (!this.XMLEnabled){
		this.sendUsingIframe(this.url)
		return true
		
	}
		
    
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        this.req = new XMLHttpRequest();
        this.writeLog("getting url:"+this.url);
        this.req.onreadystatechange = ProcessRequests;       
        this.req.open('GET', this.url, true);
        this.active=true
        this.req.send(null);
        //branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
		this.req = new ActiveXObject("Microsoft.XMLHTTP");
		this.req.onreadystatechange = ProcessRequests;        
        if (this.req) {
			this.writeLog("getting url:"+this.url);
            this.req.open("GET", this.url, true);
            this.active=true
            this.req.send();
        }
    }
    return true;
}

function ProcessRequests(){
	for (j=0;j<activeRequests.length;j++){
		if(activeRequests[j].req!=null){
			if (activeRequests[j].req.readyState){
				if (activeRequests[j].req.readyState == 4) {
					activeRequests[j].writeLog("Document Processed");
				    // only if "OK"
				    if (activeRequests[j].req.status == 200) {
						activeRequests[j].writeLog("Document OK");
						activeRequests[j].XMLErrorCount=0
						
				        activeRequests[j].response=activeRequests[j].req.responseText;
				        re = /<(.*)INFO\"/;
				        activeRequests[j].response=trim(activeRequests[j].response.replace(re,''))
				        re = /-->/;
				        activeRequests[j].response=activeRequests[j].response.replace(re,'')
				        activeRequests[j].response=RemoveViewState(activeRequests[j].response)
				        activeRequests[j].writeLog("Response:\n"+activeRequests[j].response);
							
				    } else {
						activeRequests[j].XMLErrorCount++
				        if (activeRequests[j].XMLErrorCount>activeRequests[j].MaxError){
							alert("There was a problem retrieving the data");
				        }
				    }

						
						activeRequests[j].req=null
						activeRequests[j].active=false
						if (activeRequests[j].oncomplete!=null)
							//activeRequests[j].oncomplete
							window.setTimeout(activeRequests[j].oncomplete,50)

				}			
			}
		}
	}
}
function RemoveViewState(x){
    if (x.indexOf('<input type="hidden" name="__VIEWSTATE"')>0){
        var spos=x.indexOf('<input type="hidden" name="__VIEWSTATE"')
        var epos=x.indexOf(">",spos)+1
        x=x.substring(0,spos)+x.substring(epos,x.length)
    }
    return x
}

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}
function CheckXML(){
	if (window.XMLHttpRequest) {
		return true; 
     //branch for IE/Windows ActiveX version
    }
    else if (window.ActiveXObject) {
		return true;
    }
    
    return false;

}

function sendUsingIframe(url){
	this.writeLog("Send Using Iframe")
	if (url.indexOf("?")>0)
	url+="&CallBack=requestCallback("+this.requestindex+",document.body.innerHTML)"
	this.writeLog("Send using URL:" +url)
	
	this.active=true
	CreateIFrame("iframeRequest_"+ this.name,url,200,200)
	this.writeLog("Iframe Created Waiting for a Response")
}
function requestCallback(requestIdx,Response){

	if (activeRequests[requestIdx]){
		activeRequests[requestIdx].writeLog("Iframe Response")
		activeRequests[requestIdx].response=Response
		activeRequests[requestIdx].writeLog("Response:\n"+Response)
		activeRequests[requestIdx].active=false
		if (activeRequests[requestIdx].oncomplete!=null)
			//activeRequests[requestIdx].oncomplete
			window.setTimeout(activeRequests[requestIdx].oncomplete,50)
		
	}

}



function CreateIFrame(id,url){

	var Framewidth=0
	var Frameheight=0
	
	if (arguments.length>2){
		Framewidth=arguments[2]
	}
	if (arguments.length>3){
		Frameheight=arguments[3]
	}
	
	if(!window.frames[id]){
	try{
			var iFrame = document.createElement("iframe");
			iFrame.setAttribute('id',id);
			iFrame.setAttribute('name',id);
			iFrame.style.width = Framewidth+"px";
			iFrame.style.height = Frameheight+"px";
			iFrame.src = url;
			createdframe = document.body.appendChild(iFrame);
			
			if (document.frames) {
				createdframe = document.frames[id];
			}
			
		}catch(e){
			var iframeHTML = '\<iframe id="' + id + '"';
			iframeHTML += ' style="border:0px; width:'+Framewidth+'px; height:'+Frameheight+'px;';
			iframeHTML += '"><\/iframe>';
			document.body.innerHTML += iframeHTML;
			iframe = new Object();
			iframe.document = new Object();
			iframe.document.location = new Object();
			iframe.document.location.createdframe = 
			document.getElementById(id);
			iframe.document.location.replace = 
			function(location) {
				this.iframe.src = location;
			}
		}	
	
	}
	else
	{
		window.frames[id].location.href=url
	}
	
	
}



// Not Part of the class needs to be moved to another file
var Question = new Request()

function DisplayQuestion(TargetDivId, id){
var uid=new Date()

if (arguments.length==3){
    if (arguments[2]==1)
        Question.url='/QuestionView.aspx?Full=1&size=sm&ID='+id+'&uid='+uid
    else
        Question.url='/QuestionView.aspx?size=sm&ID='+id+'&uid='+uid
    
}
else
    Question.url='/QuestionView.aspx?size=sm&ID='+id+'&uid='+uid
    
      
    
    Question.oncomplete="UpdateQuestion('" + TargetDivId + "')"
    Question.send()


}
function removeForm(responseText){
var x =new String()
x=responseText
if (x.indexOf("<form")>0){
    var spos=x.indexOf(">",x.indexOf("<form"))+1
    var epos=x.indexOf("</form",spos)
    x=x.substring(spos,epos)
    

}
return x

}

function HideQuestion(){
var tdiv
    if (arguments.length>0){
        tdiv=arguments[0]
    }else{
        tdiv="TargetDivId"
        }

    document.getElementById(tdiv).style.display="none"

}


function UpdateQuestion(TargetDivId){

var Qtext=Question.response
Qtext=removeForm(Qtext)
Qtext=RemoveViewState(Qtext)

    if (document.getElementById(TargetDivId)){
        document.getElementById(TargetDivId).innerHTML=Qtext
        document.getElementById(TargetDivId).style.display="block"
        //document.getElementById(TargetDivId).classname="questiontarget"
    }
    else
    {
        var x=document.createElement('div')
        x.id="TargetDivId"
        x.className="questiontarget"
        x.innerHTML=Qtext
        document.body.appendChild(x)
    }


}
var req =new Request()

function AddRes(id){
    req.url='/AddDownload.aspx?FileId='+id
    req.oncomplete="UpdateLink('" + id + "')"
    req.send()
}
function RemoveRes(id){
    req.url='/AddDownload.aspx?Remove=1&FileId='+id
    req.oncomplete="UpdateLink('" + id + "')"
    req.send()
}
function AddQ(id){
    req.url='/AddDownload.aspx?QuestionId='+id
    req.oncomplete="UpdateLink('" + id + "')"
    req.send()
}
function RemoveQ(id){
    req.url='/AddDownload.aspx?Remove=1&QuestionId='+id
    req.oncomplete="UpdateLink('" + id + "')"
    req.send()
}

function AddtoDownloads(id){
    if (!loggedin){
        window.top.location.href='/signin.aspx?referer='+window.location.href
        return
    }

    linkObj=document.getElementById('downloadlnk'+id)
    if (linkObj.innerHTML=="Remove from Downloads"){
        RemoveRes(id)
    }
    else
    {
        AddRes(id)
    }
}
function AddQtoDownloads(id){
    if (!loggedin){
        window.top.location.href='/signin.aspx?referer='+window.location.href
        return
    }
    linkObj=document.getElementById('downloadlnk'+id)
    if (linkObj.innerHTML=="Remove from Downloads"){
        RemoveQ(id)
    }
    else
    {
        AddQ(id)
    }
}

function UpdateLink(id){

var resp=req.response
if (resp.indexOf('cess')>0){
    linkObj=document.getElementById('downloadlnk'+id)
    if (linkObj.innerHTML=="Remove from Downloads"){
        linkObj.innerHTML="Add to Downloads"
    }
    else
    {
        linkObj.innerHTML="Remove from Downloads"
    
    }

}

}

function htmlEncode(source, display, tabs){	function special(source)	{		var result = '';		for (var i = 0; i < source.length; i++)		{			var c = source.charAt(i);			if (c < ' ' || c > '~')			{				c = '&#' + c.charCodeAt() + ';';			}			result += c;		}		return result;	}		function format(source)	{		// Use only integer part of tabs, and default to 4		tabs = (tabs >= 0) ? Math.floor(tabs) : 4;				// split along line breaks		var lines = source.split(/\r\n|\r|\n/);				// expand tabs		for (var i = 0; i < lines.length; i++)		{			var line = lines[i];			var newLine = '';			for (var p = 0; p < line.length; p++)			{				var c = line.charAt(p);				if (c === '\t')				{					var spaces = tabs - (newLine.length % tabs);					for (var s = 0; s < spaces; s++)					{						newLine += ' ';					}				}				else				{					newLine += c;				}			}			// If a line starts or ends with a space, it evaporates in html			// unless it's an nbsp.			newLine = newLine.replace(/(^ )|( $)/g, '&nbsp;');			lines[i] = newLine;		}				// re-join lines		var result = lines.join('<br />');				// break up contiguous blocks of spaces with non-breaking spaces		result = result.replace(/  /g, ' &nbsp;');				// tada!		return result;	}	var result = source;		// ampersands (&)	result = result.replace(/\&/g,'&amp;');	// less-thans (<)	result = result.replace(/\</g,'&lt;');	// greater-thans (>)	result = result.replace(/\>/g,'&gt;');		if (display)	{		// format for display		result = format(result);	}	else	{		// Replace quotes if it isn't for display,		// since it's probably going in an html attribute.		result = result.replace(new RegExp('"','g'), '&quot;');	}	// special characters	result = special(result);		// tada!	return result;}// VOting and comment stuff
var req =new Request()
SavedVote=0

function ShowStars(nStars){

    i=1;
    while (i<=nStars){

        document.getElementById("Star"+i).src = "/Images/rating/singlestar.gif"
        i++;

    }

}

function ClearStars(){

    i=5;
    while (i>SavedVote){
        document.getElementById("Star"+i).src = "/Images/rating/singlestargrey.gif"
        i--;

    }
}

function DoneVote(QID){

    //unhide Voted Div
    document.getElementById('VoteSaved').style.visibility = 'visible';
    ClearStars()
    if (document.getElementById('TargetDivId'))
    {
    var uid=new Date()
        Question.url='/QuestionView.aspx?full=1&size=sm&ID='+QID+'&uid='+uid
        Question.send()
    }    
}

function SaveStars(nStars,QID){

    SavedVote=nStars;
    var req =new Request()
   
    req.url='/DoVote.aspx?QID='+QID+'&Vote='+nStars
    req.oncomplete="DoneVote("+QID+")"
    req.send()
   
}


function GetComment(CommentID,QID){

   
    var uid = new Date()
    
    req.url='/GetComment.aspx?CommentId='+CommentID + "&uid="+uid
    req.oncomplete="LayoutComment()"
    req.send()
    
   
}
function LayoutComment(){

resp = req.response
retval = resp.split("|")
document.getElementById("User").innerText = retval[0]
document.getElementById("Comment").innerText = retval[1]
document.getElementById("DateAdded").innerText = retval[3]



}


function AddComment(QID){
    
    
    var req =new Request()
    var Comment=document.forms[0].commenttext.value
    
   
   var t='/DoComment.aspx?QID='+QID+'&CommentText='+escape(Comment.replace("/\n/g","<br/>"))

    req.url=t
    req.oncomplete="DoneComment("+QID+")"
    req.send()
   
}

function DoneComment(QID){
   document.getElementById('CommentSaved').style.visibility = 'visible';
    if (document.getElementById('TargetDivId'))
    {
    var uid=new Date()
        Question.url='/QuestionView.aspx?full=1&size=sm&ID='+QID+'&uid='+uid
        Question.send()
    }
}

function PopPage(Url,pName,pWidth,pHeight,modal){
	var pTop=(screen.availHeight -pHeight)/2;
	var pLeft=(screen.availWidth  -pWidth)/2;
	if (modal){
		if (window.showModalDialog){
		    window.showModalDialog(Url,"","dialogWidth:"+pWidth/16+" ; dialogHeight:"+pHeight/13);
		    return;
		}
		else {
		    window.open(Url,pName,"top="+pTop+",left="+pLeft+",toolbar=0,location=0,status=0,menubar=0,width="+pWidth+",height="+pHeight+",scrollbars=0,resizable=0");
		    return ;
		}
	}
	else
	{
	window.open(Url,pName,"top="+pTop+",left="+pLeft+",toolbar=0,location=0,status=0,menubar=0,width="+pWidth+",height="+pHeight+",scrollbars=0,resizable=1");
	return ;
	}
}

function ShowHide(DivID){
 
    if (document.getElementById('row'+DivID).style.display == 'none'){
        document.getElementById('row'+DivID).style.display= 'block';
    }else{
        if (document.getElementById('row'+DivID).style.display = 'block'){
            document.getElementById('row'+DivID).style.display= 'none';
        }
    }
    

}

    function AddYourComment(){
    if (document.getElementById("commentfrm").style.display=="none"){
    document.getElementById("commentfrm").style.display="block"
    }
    else
    {
    document.getElementById("commentfrm").style.display="none"
    }
    
}
