// JavaScript Document

var request;
var queryString;   //will hold the POSTed data

function sendData(url,section){
    setQueryString(section);
    //var url="";
    httpRequest("POST",url+"?sec="+section,true,section);
}

//event handler for XMLHttpRequest
function shareresponse(){
	var now = new Date();
    if(request.readyState == 4){
        if(request.status == 200){
			if (request.responseText != "1") {
				
				eval(request.responseText);
				
				// "theErrors" is the variable generated by the 'eval' command above.
				
				for (i=0; i<theErrors.length; i++) {
					if (theErrors[i].status == "0") {
						document.getElementById(theErrors[i].div).innerHTML = theErrors[i].msg;
						document.getElementById(theErrors[i].div).style.display = "block";
					}
					else {
						document.getElementById(theErrors[i].div).style.display = "none";
						document.getElementById(theErrors[i].div).innerHTML = theErrors[i].msg;
					}
				}
				
				if (theErrors[theErrors.length-1].status == "2") {
					document.forms[1].namefrom.value = "";
					document.forms[1].nameto.value = "";
					document.forms[1].emailfrom.value = "";
					document.forms[1].emailto.value = "";
					document.forms[1].message.value = "";
					document.forms[1].code.value = "";
					document.getElementById(theErrors[theErrors.length-1].div).innerHTML = theErrors[theErrors.length-1].msg;
					document.getElementById(theErrors[theErrors.length-1].div).style.display = "block";
				}
				
				document.sending = false;
				document.registering = false;
			}
        } else {
            alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
        }
    }//end outer if
}

//event handler for XMLHttpRequest
function loginresponse(){
	var now = new Date();
    if(request.readyState == 4){
        if(request.status == 200){
			if (request.responseText != "1") {
				
				eval(request.responseText);
				//alert(request.responseText);
				
				// "theErrors" is the variable generated by the 'eval' command above.
				
				for (i=0; i<theErrors.length; i++) {
					if (theErrors[i].status == "0") {
						document.getElementById(theErrors[i].div).innerHTML = theErrors[i].msg;
						//hideErrorMsg(theErrors[i].div);
						setTimeout("document.getElementById(\""+theErrors[i].div+"\").innerHTML = ''",2500);
					}
					else {
						var temp = document.loggedInHTML;
						document.getElementById(theErrors[i].div).innerHTML = temp.replace(/\[THEIKRUSER\]/,theErrors[i].msg);
						if (document.commenting == true) {
							document.location.href = "#addcommentanchor";
							document.getElementById("commentloginwarning").style.display = "none";
							document.getElementById("addacomment").style.display = "block";
							document.getElementById("holdcommentiframe").style.display = "block";
						}
						if (!document.atindex) {
							document.getElementById("commentiframe").src = "http://www.iveknownrivers.org/comments/add-comment.php";
						}
						document.loggedIn = true;
					}
				}
			}
        } else {
            alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
        }
    }//end outer if
}


//event handler for XMLHttpRequest
function logoutresponse(){
	var now = new Date();
    if(request.readyState == 4){
        if(request.status == 200){
			
			eval(request.responseText);
			for (i=0; i<theErrors.length; i++) {
					if (theErrors[i].status == "0") {
						document.getElementById(theErrors[i].div).innerHTML = theErrors[i].msg;
						//hideErrorMsg(theErrors[i].div);
						setTimeout("document.getElementById(\""+theErrors[i].div+"\").innerHTML = ''",2500);
					}
					else {
						document.location.replace("http://www.iveknownrivers.org/");
						/*
						document.getElementById(theErrors[i].div).innerHTML = document.loggedOutHTML;
					
						// reset the events for the login and logout links
						document.getElementById("showlogin").onclick = showlogin;
						document.getElementById("hidelogin").onclick = hidelogin;
						document.getElementById("joinikrlink").onclick = joinIKR;
						document.loggedIn = false;
						
						// hide the comment panel
						if (!document.atIndex) {
							document.commenting = false;
							document.getElementById("commentloginwarning").style.display = "block";
							document.getElementById("commentiframe").src = "";
							document.getElementById("holdcommentiframe").style.display = "none";
							document.getElementById("addacomment").style.display = "none";
						}
						*/
						
					}
				}	
				
        } else {
            alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
        }
    }//end outer if
}


function joinresponse() {
	var now = new Date();
    if(request.readyState == 4){
        if(request.status == 200){
			eval(request.responseText);
			for (i=0; i<theErrors.length; i++) {
					if (theErrors[i].status == "0") {
						document.getElementById(theErrors[i].div).innerHTML = theErrors[i].msg;
					}
					else {
						//document.getElementById(theErrors[i].div).style.display = "none";
						document.getElementById(theErrors[i].div).innerHTML = theErrors[i].msg;
					}
				}	
				document.registering = false;
        } else {
            alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
        }
    }//end outer if
}


function commentresponse() {
	var now = new Date();
    if(request.readyState == 4){
        if(request.status == 200){
			//alert(request.responseText);
			
			eval(request.responseText);
			for (i=0; i<theErrors.length; i++) {
				if (theErrors[i].status == "0") {
					document.getElementById(theErrors[i].div).innerHTML = theErrors[i].msg;
					document.getElementById(theErrors[i].div).style.display = "block";
					document.getElementById("e3").innerHTML = "";
					document.getElementById("e3").style.display = "none";
				}
				else {
					//document.getElementById(theErrors[i].div).style.display = "none";
					document.getElementById(theErrors[i].div).innerHTML = theErrors[i].msg;
					setTimeout("parent.commentReset()",2500);
				}
			}
			
				
        } else {
            alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
        }
    }//end outer if
}


function getauthorresponse(){
	var now = new Date();
    if(request.readyState == 4){
        if(request.status == 200){
			
			
			eval(request.responseText);
			
			document.getElementById(authorData[0].authorcontentdiv).innerHTML = authorData[0].html;
		    document.getElementById(authorData[0].authordiv).style.display = authorData[0].display;
			document.location.href = "#authoranchor"+authorData[0].index;
			
			
        } else {
            alert("A problem occurred with communicating between the XMLHttpRequest object and the server program.");
        }
    }//end outer if
}



/* Initialize a Request object that is already constructed */

function initReq(reqType,url,bool,section){
    /* Specify the function that will handle the HTTP response */
	if (section == 0) { request.onreadystatechange=loginresponse; }
	if (section == 1) { request.onreadystatechange=logoutresponse; }
	if (section == 2) { request.onreadystatechange=shareresponse; }
	if (section == 3) { request.onreadystatechange=joinresponse; }
	if (section == 4) { request.onreadystatechange=commentresponse; }
	if (section == 5) { request.onreadystatechange=getauthorresponse; }
	
    request.open(reqType,url,bool);
    request.setRequestHeader("Content-Type",
            "application/x-www-form-urlencoded; charset=UTF-8");
    request.send(queryString);
}

/* Wrapper function for constructing a Request object.
 Parameters:
  reqType: The HTTP request type such as GET or POST.
  url: The URL of the server program.
  asynch: Whether to send the request asynchronously or not. */
  
function httpRequest(reqType,url,asynch,section){
    //Mozilla-based browsers
    if(window.XMLHttpRequest){
        request = new XMLHttpRequest();
    } else if (window.ActiveXObject){
        request=new ActiveXObject("Msxml2.XMLHTTP");
        if (! request){
            request=new ActiveXObject("Microsoft.XMLHTTP");
        }
     }
    //the request could still be null if neither ActiveXObject
    //initializations succeeded
    if(request){
       initReq(reqType,url,asynch,section);
    }  else {
        alert("Your browser does not permit the use of all "+
        "of this application's features!");}
}

function setQueryString(section){
    queryString="";
	if ((section == 5) || (section == 2)) { var frm = document.forms[1]; }
	else {
		var frm = document.forms[0];
	}
    var numberElements =  frm.elements.length;
    for(var i = 0; i < numberElements; i++)  {
            if(i < numberElements-1)  {
                queryString += frm.elements[i].name+"="+
                               encodeURIComponent(frm.elements[i].value)+"&";
            } else {
                queryString += frm.elements[i].name+"="+
                               encodeURIComponent(frm.elements[i].value);
            }

    }
}


function hideErrorMsg(div) {
	setTimeout("document.getElementById("+div+").innerHTML = ''",2000);
}
