//this script is called from a form submit button and gathers up all the information in that form and passes it to the site comments script using AJAX magicks
//then writes the results back out to the div
function sc_ajax() {

	
	//grab info from form
    var form = document.forms['sc_form'];
    var blog_id = form.blog_id.value;
    var entry_id = form.entry_id.value;
    var name = form.name.value;
    var email = form.email.value;
    var url = form.url.value;
    var comment = form.comment.value;
    var recaptcha_challenge_field = form.recaptcha_challenge_field.value; 
    var recaptcha_response_field = form.recaptcha_response_field.value;
    
    //construct query
    query = 'blog_id=' + blog_id + '&entry_id=' + entry_id + '&name=' + name + '&email=' + email + '&url=' + url + '&comment=' + escape(comment);
    query = query + '&recaptcha_challenge_field=' + recaptcha_challenge_field + '&recaptcha_response_field=' + recaptcha_response_field;
	
	
	//DEBUG
	//document.getElementById('sc_wrapper').innerHTML = "QUERY:"+query;		


	//the url to the script that handles the posted request	
	var request_url='/interactive/sc/sc.php';

	//create new xmlHttpRequest object, depending on whether compliant browser or crappy IE
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	//call a function to process the response sent back by the comment script
	request.onreadystatechange = sc_process;
	
	//send actual request to script using POST method
	request.open("POST", request_url, true);
	
	//Send the proper header information along with the request
	request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request.setRequestHeader("Content-length", query.length);
	request.setRequestHeader("Connection", "close");
	
	//Send the parameters required for the POST request 
	request.send(query);

}//end sc_ajax()





//this processes the AJAX states and stuff returned from server
function sc_process() {

	//catch AJAX response states
	if(request.readyState == 0)
		document.getElementById('sc_wrapper').innerHTML = '';

	if(request.readyState == 1)
		document.getElementById('sc_wrapper').innerHTML = "<p align=\"center\"><img src=\"/interactive/sc/images/ajax_loading.gif\"/></p>"; 

	if(request.readyState == 4) {
		
		// if everything is okay
 		if (request.status==200) {
 		
			//write the results from the comment script to the page
		 	var response = request.responseText;
			document.getElementById('sc_wrapper').innerHTML = response;			
			 } else {
			 // if status != 200 there was an error.
			 var error_message = 'Problem retrieving the data: ' + xmlhttp.statusText
		}
	}
}// end sc_process()





//requires recaptcha_ajax.js - SEE NOTES!!  
//this function dynamically creates a recaptcha in the the html element with the designated name.  (element should be inside of a submit form)
function showRecaptcha(element) {
	Recaptcha.create("6LepDwYAAAAAAPQ7etPW8QxPjvK3PyWl9-P79Dgw",
	element, {
	   theme: "clean",
//	   callback: Recaptcha.focus_response_field			//uncomment this to automatically put focus back on recaptcha field after load
	});
}

