var baseurl = "http://www.dosomethingnow.ca/";

function makeHttpRequest(url, callback_function, return_xml){
    var http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
    }

    if (!http_request) {
        alert('Unfortunatelly you browser doesn\'t support this feature.');
        return false;
    }

    http_request.onreadystatechange = function() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
                if (return_xml) {
                    eval(callback_function + '(http_request.responseXML)');
                } else {
                    eval(callback_function + '(http_request.responseText)');
                }
            } else {
                alert('There was a problem with the request.(Code: ' + http_request.status + ')');
            }
        }
    }
    http_request.open('GET', url, true);
    http_request.send(null);
}

function feedback(element, classString, theMessage, theImage) {
	
	if (theMessage != null){

		var messageDiv = document.createElement("div");

		messageDiv.appendChild(document.createTextNode(theMessage));

		messageDiv.className = classString;

		var image = document.createElement('img');
		image.src = theImage;
		image.className ="animation";
		
		
		if(element.parentNode){

			if(element.childNodes.length >=1){

		   		while (element.hasChildNodes()){

		  			element.removeChild(element.firstChild);

			   	}

		    }

			if(element.childNodes.length >=1){

				element.insertBefore(messageDiv, element.firstChild);
				element.insertBefore(image, element.firstChild);

			}else{

				element.appendChild(messageDiv);
				element.appendChild(image);

			}

		}

	}

	return true;

}

function onchangeReceipt(){

	/* Check for running connections */

	if (requester != null && requester.readyState != 0 && requester.readyState != 4){

		requester.abort();

	}

} 

function xmlhttp_question(topic){
	result_div = document.getElementById('expertbody');
	var email = document.getElementById('qemail').value;
	var question = document.getElementById('qexpert').value;
	if (email && question) {
		result_div.onchange = onchangeReceipt;
		feedback(result_div, "", "Submitting Question","");
		var the_url = baseurl+'inc/scripts/xml-expert.php?topic='+escape(topic)+'&email='+escape(email)+'&question='+escape(question);
		makeHttpRequest(the_url, 'updateQuestion', true);
	}else{
		alert('Please ensure you have entered your email address and a question');	
	}
}

function updateQuestion(xmldoc){
	var success = xmldoc.getElementsByTagName('success').item(0).firstChild.nodeValue;
	var element = result_div;
	
	if(element.childNodes.length >=2){
		while (element.hasChildNodes()){
			element.removeChild(element.firstChild);
		}
	}
		
	if (success == "true"){
		
		mycurrent_cell = document.createElement('h2');
		mycurrent_cell.appendChild(document.createTextNode(xmldoc.getElementsByTagName('command').item(0).firstChild.nodeValue));
		element.appendChild(mycurrent_cell);
	
	}else{
		feedback(result_div, "", 'An error occured',"");	
	}	
}

function xmlhttp_postcard(div){
	var from  = document.getElementById('from').value;
	var greeting  = document.getElementById('greeting').value;
	var str ="";
	if(document.getElementById('to_1')){
		var to_1  = document.getElementById('to_1').value;
		str += '&to1='+escape(to_1);
	}
	if(document.getElementById('to_2')){
		var to_2  = document.getElementById('to_2').value;
		str += '&to2='+escape(to_2);
	}
	if(document.getElementById('to_3')){
		var to_3  = document.getElementById('to_3').value;
		str += '&to3='+escape(to_3);
	}
	if(document.getElementById('to_4')){
		var to_4  = document.getElementById('to_4').value;
		str += '&to4='+escape(to_4);
	}
	if(document.getElementById('to_5')){
		var to_5  = document.getElementById('to_5').value;
		str += '&to5='+escape(to_5);
	}
	
	result_div = document.getElementById(div);
	var choice = document.getElementById('sendtopic').value;
	if (choice) {
		result_div.onchange = onchangeReceipt;
		feedback(result_div, "", "",baseurl+"images/layout/card/sending.gif");
		var the_url = baseurl+'inc/scripts/xml-email.php?topic='+escape(choice)+'&from='+escape(from)+'&message='+escape(greeting) + str;
		makeHttpRequest(the_url, 'updateEmail', true);
	}
}

function updateEmail(xmldoc){
	var success = xmldoc.getElementsByTagName('success').item(0).firstChild.nodeValue;
	var element = result_div;
	
	
	if(element.childNodes.length >=2){
		while (element.hasChildNodes()){
			element.removeChild(element.firstChild);
		}
	}
		
	if (success == "true"){
		
		var mycontainer = document.createElement('div');
		mycontainer.className ='emailfeedback';
		
		mycurrent_cell = document.createElement('h3');
		mycurrent_cell.appendChild(document.createTextNode(xmldoc.getElementsByTagName('command').item(0).firstChild.nodeValue));
		
		mycontainer.appendChild(mycurrent_cell);
		
		my_link = document.createElement('a');
		my_link.setAttribute("href",baseurl+'contest/individual-contest.php');
		my_link.setAttribute("alt", "Enter the Contest");
		my_link.appendChild(document.createTextNode('Go to Contest Entry Form'));
		
		mycontainer.appendChild(my_link);
		element.appendChild(mycontainer);
				
	
	}else{
		feedback(result_div, "", xmldoc.getElementsByTagName('command').item(0).firstChild.nodeValue,"");	
	}	
}

function xmlhttp_poll(div, getValue, pollid){
	result_div = document.getElementById(div);
	var choice = getSelect(getValue);
	if (choice) {
		result_div.onchange = onchangeReceipt;
		feedback(result_div, "", "adding","");
		var the_url = baseurl+'inc/scripts/xml-poll.php?option='+escape(choice)+'&pollid='+escape(pollid);
		makeHttpRequest(the_url, 'updatePoll', true);
	}
}

function updatePoll(xmldoc){
	
	var success = xmldoc.getElementsByTagName('success').item(0).firstChild.nodeValue;
	
	if(xmldoc.getElementsByTagName('command').length >=1){
		var word = xmldoc.getElementsByTagName('command').item(0).firstChild.nodeValue;
	}
	
	
	var element = result_div;
	if(element.childNodes.length >=2){
		while (element.hasChildNodes()){
			element.removeChild(element.firstChild);
		}
	}
		
	if (success == "true"){
		
		if(word){
			var container = document.createElement('p');
			container.className = "question";
			container.style.width = "310px";
			container.appendChild(document.createTextNode(word));
			element.appendChild(container);
		}
		
		var result_collection = xmldoc.getElementsByTagName('line');	
		var number_of_items = result_collection.length;
		for (var i = 0; i < number_of_items; i++) {
			if (result_collection.item(i).hasChildNodes()) {
				var elements_collection = result_collection.item(i).getElementsByTagName('el');
				
				var container = document.createElement('div');
				container.className = 'clearBoth';
				
				mycurrent_cell = document.createElement('div');
				mycurrent_cell.className = 'float_left';	 
				mycurrent_cell.style.width = '40px';
				mycurrent_cell.appendChild(document.createTextNode(elements_collection.item(2).firstChild.nodeValue));
				container.appendChild(mycurrent_cell);
				
				mycurrent_cell = document.createElement('div');
				mycurrent_cell.className = 'float_left';
				mycurrent_cell.style.width = '260px';
				mycurrent_cell.appendChild(document.createTextNode(elements_collection.item(0).firstChild.nodeValue));
				container.appendChild(mycurrent_cell);
							
				element.appendChild(container);
			}
		}
	}else{
		feedback(result_div, "", word,"");	
	}	
}

function xmlhttp_facts(div, topic, rowid){
	resetknav(topic);
	result_div = document.getElementById(div);
	globaltopic = topic;
	if (result_div) {
		result_div.onchange = onchangeReceipt;
		feedback(result_div, "", "GETTING DATA","");
		var url = baseurl +'inc/scripts/xml-facts.php?topic='+escape(topic);
		if(rowid){	url = url+'&rowID='+escape(rowid); }
		var the_url = url;
		makeHttpRequest(the_url, 'updateFacts', true);
	}
}

function updateFacts(xmldoc){
	var success = xmldoc.getElementsByTagName('success').item(0).firstChild.nodeValue;
	var element = result_div;
	var topic =  globaltopic;
	
	if(element.childNodes.length >=2){
		while (element.hasChildNodes()){
			element.removeChild(element.firstChild);
		}
	}
		
	if (success == "true"){
		
		var result_collection = xmldoc.getElementsByTagName('line');	
		var number_of_items = result_collection.length;
		for (var i = 0; i < number_of_items; i++) {
			if (result_collection.item(i).hasChildNodes()) {
				
				var elements_collection = result_collection.item(i).getElementsByTagName('el');
				
				if(elements_collection.item(2).hasChildNodes() && elements_collection.item(2).firstChild.nodeValue !="n/a"){
					mycurrent_cell = document.createElement('h2');
					mycurrent_cell.appendChild(document.createTextNode(elements_collection.item(2).firstChild.nodeValue));
					element.appendChild(mycurrent_cell);				
				}
				
				
				mycurrent_cell = document.createElement('div');
				mycurrent_cell.className = 'facts';	 
				mycurrent_cell.appendChild(document.createTextNode(elements_collection.item(3).firstChild.nodeValue));
				element.appendChild(mycurrent_cell);
				
				mycurrent_cell = document.createElement('div');
				mycurrent_cell.className = 'float_right';	 
				
				my_link = document.createElement('a');
				my_link.setAttribute("href",elements_collection.item(4).firstChild.nodeValue);
				my_link.setAttribute("target","_blank");
				my_link.setAttribute("alt", "Source Web page");
				my_link.appendChild(document.createTextNode('Source Website'));
				
				mycurrent_cell.className ="facts";
				mycurrent_cell.appendChild(my_link);
				element.appendChild(mycurrent_cell);
																			
				document.getElementById('k-previous').onclick= function(){ xmlhttp_facts('fcopy',topic,elements_collection.item(0).firstChild.nodeValue); return false};																						
				document.getElementById('k-next').onclick= function(){ xmlhttp_facts('fcopy',topic,elements_collection.item(1).firstChild.nodeValue); return false};																						
			
				var getImage = document.getElementById('fheading');
				getImage.src = baseurl+'images/layout/know-'+ elements_collection.item(5).firstChild.nodeValue +'.jpg';
			
				resetknav(elements_collection.item(5).firstChild.nodeValue);
			}
		}
	}else{
		feedback(result_div, "", word,"");	
	}	
}
