/**
 * GetXmlHttpObject
 * Request a new XML HTTP request object that can be used to send synchronous
 * or asynchronous HTTP requests to pages and scripts on the current domain.
 * This works in standard-compliant browsers and IE.
 */
function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari, standard-compliant browsers
        xmlHttp = new XMLHttpRequest();
    }
   catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (ex) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    if (!xmlHttp) {
        alert("Could not create an HTTP Request.\nPlease upgrade your web browser" +
            " and/or adjust JavaScript security settings.");
        throw "Error - could not create an HTTP Request";
    }
    return xmlHttp;
}

/** 
* @Desc This function returns a valid XML HTTP object according to the browser that is being used
*       by the user. The XML HTTP request object can be used to send synchronous or asynchronous HTTP 
*       requests to pages and scripts on the current domain.
*
**/
function getXmlHttpObject() {
    var xml_http = null;
        
    try {
        // Firefox, Opera 8.0+, Safari, standard-compliant browsers
        xml_http = new XMLHttpRequest();
    }
        catch (e) {
        // Internet Explorer
        try {
            xml_http = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (ex) {
            xml_http = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    if (!xml_http) {
        alert("Could not create an HTTP Request.\nPlease upgrade your web browser" +
            " and/or adjust JavaScript security settings.");
        throw "Error - could not create an HTTP Request";
    }
    return xml_http;
}



/**
* @Desc This function opens and sends a POST request to the specified URL with the parameters in the 
*       body of the POST.  The function uses sychronous requests.
*
* @params paramNames comma seperated list of param names to send via HTTP request
* @params paramVals comma seperated list of param values to send via HTTP request
* @params url the URL to send the request to
*
* @return  the response from the HTTP post request
*
**/
function postRequest(param_names, param_vals, url){

        var request = getXmlHttpObject();
        
        //Encode the parameters to be sent
        var temp_names = param_names.split(",");
        var temp_vals  = param_vals.split(",");
        var params;
        
        //Set the first paramater in the param variable
        params = temp_names[0] + "=" + encodeURIComponent(temp_vals[0]);
        //Loop to add the rest of the params (in neccessary)
        for (var i = 1; i < temp_names.length; i++){
                params += "&" + temp_names[i] + "=" + encodeURIComponent(temp_vals[i]);                                                                    
        }
        
        //Open a sychronous POST request
        request.open("POST", url, false);
        
        //Set the content-type request header for a POST request
        request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        
        //Send the request with the params in the body
        request.send(params);
        
        //Return the replied status
        return request.responseText;
}



/**
*
*/
function joinNewletter() {
	var email = document.getElementById("news_email").value;
	
	//Send email to database to be added
	var paramNames = "type,email";
	var paramVals  = "addNewsletter," + email;
	var url        = "request.php";
	
	//Wait for a response from the login.php file
	var response = postRequest(paramNames, paramVals, url);
	
	document.getElementById("news_email").value = "";
}

/**
*
*/
function addBlogEntry() {
	var title  = document.getElementById("blog_subject").value;
	var author = document.getElementById("blog_author").value;
	var entry  = document.getElementById("blog_text").value;
	
	var url        = "request.php";
	//Send email to database to be added
	var params = "type=addBlogEntry";
	params += "&title="  + encodeURIComponent(title);
	params += "&author=" + encodeURIComponent(author);
	params += "&entry="  + encodeURIComponent(entry);
	
	var request = getXmlHttpObject();
        
	//Open a sychronous POST request
	request.open("POST", url, false);
        
	//Set the content-type request header for a POST request
	request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        
	//Send the request with the params in the body
	request.send(params);	
	
	//Wait for a response from the login.php file
	var response = request.responseText;
	
	document.getElementById("blog_subject").value = "";
	document.getElementById("blog_text").value = "";
	document.getElementById("blog_author").value = "Wesley Eggebrecht";	
}

function clearName(elem) {
	if (elem.value == "Name")
		elem.value = "";
}

function resetName(elem) {
	if (elem.value == "")
		elem.value = "Name";
}

function resetEmail(elem) {
	if(elem.value == "")
		elem.value = "Email (not published)";
}

function clearEmail(elem) {
	if (elem.value == "Email (not published)")
		elem.value = "";
}

function resetWebsite(elem) {
	if(elem.value == "" || elem.value == "http://")
		elem.value = "Website";
}

function clearWebsite(elem) {
	if (elem.value == "Website")
		elem.value = "http://";
}

function addBlogComment(blog_id) {
	var name    = document.getElementById("c_name_" + blog_id).value;
	var text    = document.getElementById("c_text_" + blog_id).value;
	var email   = document.getElementById("c_email_" + blog_id).value;	
	var website = document.getElementById("c_website_" + blog_id).value;
	
	var error = 0;
	
	if (name == "Name") {
		document.getElementById('name_req_' + blog_id).style.display = "inline";
		error = 1;
	}
	else 
		document.getElementById('name_req_' + blog_id).style.display = "none";
	
	if (email == "Email (not published)") {
		document.getElementById('email_req_' + blog_id).style.display = "inline";
		error = 1;		
	}
	else 
		document.getElementById('email_req_' + blog_id).style.display = "none";	
		
	if (text == "") {
		document.getElementById('comment_req_' + blog_id).style.display = "inline";
		error = 1;		
	}
	else 
		document.getElementById('comment_req_' + blog_id).style.display = "none";	
	
	if (error)
		return false;
	
	var url        = "manage/request.php";
	//Send email to database to be added
	var params = "type=addBlogComment";
	params += "&name="     + encodeURIComponent(name);
	params += "&text="     + encodeURIComponent(text);
	params += "&blog_id="  + encodeURIComponent(blog_id);
	params += "&email="    + encodeURIComponent(email);	
	
	if (website == "Website" || website == "http://")
		website = "none";
	
	params += "&website="  + encodeURIComponent(website);	
	
	var request = getXmlHttpObject();
        
	//Open a sychronous POST request
	request.open("POST", url, false);
        
	//Set the content-type request header for a POST request
	request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        
	//Send the request with the params in the body
	request.send(params);	
	
	//Wait for a response from the login.php file
	var response = request.responseText;
	
	var elem = document.getElementById("comment_write_" + blog_id);
	var lastElement = document.getElementById("comment_write_" + blog_id).previousSibling;
	var newElem = document.createElement("div");  	
	
	if (lastElement == null)
		newElem.className = "comments_gray";	
	else if (lastElement.className == "comments_white")
		newElem.className = "comments_gray";
	else if (lastElement.className == "comments_gray")
		newElem.className = "comments_white";
	else
		newElem.className = "comments_gray";
		
		
	newElem.innerHTML = "<p><b><a href=http://" + website + ">" + name + "</a></b><br/><i>" + response + "</i></p><p>" + text + "</p>";
	elem.parentNode.insertBefore(newElem, elem);
	
	var count = document.getElementById('count_' + blog_id).innerHTML;
	count = parseInt(count,10);
	count++;
	document.getElementById('count_' + blog_id).innerHTML = count;
	
	document.getElementById("c_name_" + blog_id).value = "Name";
	document.getElementById("c_email_" + blog_id).value = "Email (not published)";	
	document.getElementById("c_website_" + blog_id).value = "Website";	
	document.getElementById("c_text_" + blog_id).value = "";	
	
}