// JavaScript Document

var http = false;
var req;
var which;
var spanId1;


if(navigator.appName == "Microsoft Internet Explorer") 
{
	http = new ActiveXObject("Microsoft.XMLHTTP");
} 
else 
{
  	http = new XMLHttpRequest();
}


function choosereply(TouserId,id,mode) {

var params="TouserId="+TouserId+"&mode="+mode+"&i="+id;
  http.open("POST", "replyindex.php", true);
  
	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) {
	    
		// alert(http.responseText);
		var section="divreply"+id;
		 
	    document.getElementById(section).innerHTML = http.responseText;
    }
  }
  http.send(params);
}



function retrieveURL(url,nameOfFormToPost) {
   //alert("hello");
  // spanid = id;
    //get the (form based) params to push up as part of the get request
	var posturl="";
   if(nameOfFormToPost != 'inputForm')
    posturl=getFormAsString(nameOfFormToPost);

    //Do the Ajax call
    if (window.XMLHttpRequest) { // Non-IE browsers
      req = new XMLHttpRequest();
      req.onreadystatechange = processStateChange;
      try {
		req.open("POST", url, true); //was get
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		req.send(posturl);
		
		} catch (e) {
        alert("Problem Communicating with Server\n"+e);
      }
//      req.send(null);
    } else if (window.ActiveXObject) { // IE
      
      req = new ActiveXObject("Microsoft.XMLHTTP");
      if (req) {
		  
         req.open("POST", url, true);
    	req.onreadystatechange = processStateChange;
	    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(posturl);

		  
      }
    }
  }
  
 function processStateChange() 
 { 
  	if (req.readyState == 4) 
	{ // Complete
      	if (req.status == 200) 
		{ // OK response
			//Split the text response into Span elements
			
	        spanElements = splitTextIntoSpan(req.responseText);        
	        //Use these span elements to update the page

    	    replaceExistingWithNewHtml(spanElements);
      	}
		else 
		{
        	alert("Problem with server response:\n " + req.statusText);
      	}
    }
}
  
   function replaceExistingWithNewHtml(newTextElements){
       
 	//loop through newTextElements
 	for ( var i=newTextElements.length-1; i>=0; --i ){
 		//check that this begins with <span
		 
 		if(newTextElements[i].indexOf("<span")>-1){
 			//alert('hello')
 			//get the name - between the 1st and 2nd quote mark
 			startNamePos=newTextElements[i].indexOf('"')+1;
 			endNamePos=newTextElements[i].indexOf('"',startNamePos);
 			name=newTextElements[i].substring(startNamePos,endNamePos);
 			
 			//get the content - everything after the first > mark
 			startContentPos=newTextElements[i].indexOf('>')+1;
 			content=newTextElements[i].substring(startContentPos);
 			
 			//Now update the existing Document with this element
			//alert(name);
			//alert(content);
	 			//check that this element exists in the document
				
	 			if(document.getElementById(name)){
	 			
                                   
	 				//alert("Replacing Element:"+name);
	 				document.getElementById(name).innerHTML = content;
	 			} else {
	 			//	alert("Element:"+name+"not found in existing document");
	 			}
 		}
 	}
 }

function retrieveURL1(url,nameOfFormToPost,spanid) 
{
	//alert("hello");
	spanId1 = spanid;
	//get the (form based) params to push up as part of the get request
	if(nameOfFormToPost != 'inputForm')
		url=url+getFormAsString(nameOfFormToPost);
	//Do the Ajax call
	
	if (window.XMLHttpRequest) 
	{ // Non-IE browsers
		req = new XMLHttpRequest();
		req.onreadystatechange = processStateChangenew;
		try {
			req.open("GET", url, true); //was get
		} catch (e) {
			alert("Problem Communicating with Server\n"+e);
		}
		req.send(null);
	} 
	else if (window.ActiveXObject) 
	{ // IE	
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) 
		{
			req.onreadystatechange = processStateChangenew;
			req.open("GET", url, true);
			req.send();
		}
	}
}

function getFormAsString(formName)
{ 	
 	//Setup the return String
 	returnString ="";
 	
  	//Get the form values
	//alert("ddd="+formName);
 	formElements=document.forms[formName].elements;
 	
 	//loop through the array , building up the url
 	//in the form /strutsaction.do&name=value
 	
 	for ( var i=formElements.length-1; i>=0; --i )
	{
 		//we escape (encode) each value
		//alert(formElements[i].name+" "+formElements[i].value);
		if (formElements[i].type=="radio")
		{
			if (formElements[i].checked==true)
			{
			returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);
			}
		}
		else
		{
		returnString=returnString+"&"+escape(formElements[i].name)+"="+escape(formElements[i].value);	
		}
 	} 	
 	//return the values
 	return returnString; 
}

function processStateChangenew() 
{  
	if (req.readyState == 4)
	{ // Complete
		if (req.status == 200)
		{ // OK response	
			var a = req.responseText;
			var arryspan = a.split("~~~");
			
			if(arryspan.length == 4)
			{	
				if(arryspan[2] == 'error')
				{
					eval(arryspan[1]);
				}
			}
			//Split the text response into Span elements
			
			spanElements = splitTextIntoSpan(req.responseText);
			//Use these span elements to update the page
			
			replaceExistingWithNewHtmlnew(spanElements,spanId1);			
		}
		else
		{
			alert("Problem with server response:\n " + req.statusText);
		}
	}
}

function splitTextIntoSpan(textToSplit)
{ 
  	//Split the document
 	returnElements=textToSplit.split("</span>")
 	//Process each of the elements 	
 	for ( var i=returnElements.length-1; i>=0; --i )
	{ 		
 		//Remove everything before the 1st span
 		spanPos = returnElements[i].indexOf("<span");
 		//if we find a match , take out everything before the span
 		if(spanPos>0)
		{
 			subString=returnElements[i].substring(spanPos);
 			returnElements[i]=subString;
			//alert(subString); 		
 		} 
 	}
 	return returnElements;
}


subject_id = '';
	function handleHttpResponse() {
		if (http.readyState == 4) {
			if (subject_id != '') {
				//alert(http.responseText);
				document.getElementById(subject_id).innerHTML =  http.responseText;
			}
		}
		else
		{
			document.getElementById(subject_id).innerHTML =  "<div align='center'><img src='images/bigrotation2.gif' align='center'></div>";
		}
	}
	

function ajaxfetch(arraoeid,imageid,post_div_id,postid,topicid,url)
	{
		//alert(arraoeid); alert(imageid); alert(post_div_id); alert(topicid); alert(url);
		
		if(document.images[imageid].src == url + "/images/close.gif")
		{
			document.images[imageid].src = url + "/images/open.gif";
			subject_id = post_div_id;
			http.open("GET", "fetchtitle.php?type=1&postid=" + postid +"&topicid=" + topicid, true);
			http.onreadystatechange = handleHttpResponse;
			http.send(null);
		}
		else
		{
			document.images[imageid].src = url + "/images/close.gif";
			subject_id = post_div_id;
			http.open("GET", "fetchtitle.php?type=2&postid=" + postid +"&topicid=" + topicid, true);
			http.onreadystatechange = handleHttpResponse;
			http.send(null);
		}	
	}


function replaceExistingWithNewHtmlnew(newTextElements,spanid2)
{
 	//loop through newTextElements
 	for ( var i=newTextElements.length-1; i>=0; --i )
	{
 		//check that this begins with <span
 		if(newTextElements[i].indexOf("<span")>-1)
		{ 			
 			//get the name - between the 1st and 2nd quote mark
 			startNamePos=newTextElements[i].indexOf('"')+1;
 			endNamePos=newTextElements[i].indexOf('"',startNamePos);
 			name=newTextElements[i].substring(startNamePos,endNamePos);
 			
 			//get the content - everything after the first > mark
 			startContentPos=newTextElements[i].indexOf('>')+1;
 			content=newTextElements[i].substring(startContentPos);
 			
 			//Now update the existing Document with this element
			//alert(name);
			//alert(content);
	 			//check that this element exists in the document
			if(name == spanid2)	
			{
	 			if(document.getElementById(name))
				{
                   // alert(document.getElementById(name).innerHTML);               
	 				//alert("Replacing Element:"+name);
	 				document.getElementById(name).innerHTML = content;
	 			} 
				else 
				{
	 			//	alert("Element:"+name+"not found in existing document");
	 			}
			}
 		}
 	}
}
