var global_response_getUrl = '';

function __createObj() {
			var httpRequest;

        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // override mime type because some versions of mozilla browsers won't continue unless what is returned is XML
            }
        }
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               }
                             catch (e) {}
                          }
                                       }

        if (!httpRequest) {
            alert('You must have Javascript turned on to use this feature');
            return false;
        } else {
				return httpRequest;
        }
}

function __getUrl(url,show) {
	if(show == '') {
		show = true;
	}

	var httpRequest = __createObj();

	if(url.indexOf("index") == -1) { /* If we have navigated away from the index page clear the "reload home page interval" */
		try {
			clearInterval(reload_home_page_interval);
		} catch (er) {

		}
	}

	httpRequest.open("GET", url, true); /* Third option is asynchronous or not. If set to true, this function will return immediately. Damn man. */
	httpRequest.send(null);

	httpRequest.onreadystatechange = function() {
		if (httpRequest.readyState == 4 || httpRequest.readyState == "done")   {
				global_response_getUrl = httpRequest.responseText;
				if(show == true) {
					__showData();
				}
				return true;
		}
	}
}

function reloadHomePage() {
	__getUrl('/index?xmlrpc=yar', true);
}

function addEmailAddress() {
	var email_owner;
	var email_address;

	email_owner 	= document.getElementById('email_owner').value;
	email_address 	= document.getElementById('email_address').value;

	__getUrl("my_account?a=eadd&submit=y&email_owner=" + email_owner + "&email_address=" + email_address, true);
	return false;
}


function addPOPAccount(host,username,password){
		__getUrl("my_account?a=epopadd&submit=y&host="+host+"&username="+username+"&password="+password)
	return false;
}

function addTransaction() {
	var trans_amount;
	var trans_spent_on;
	var trans_spent_at;
	var trans_type;

	trans_amount 	= document.getElementById('amount').value;
	trans_spent_at 	= document.getElementById('spent_at').value;
	trans_spent_on 	= document.getElementById('spent_on').value;
	trans_type 	= document.getElementById('type').value;

	__getUrl("a?act=add&submit=y&amount=" + trans_amount +  "&spent_at=" + trans_spent_at + "&spent_on=" + trans_spent_on + "&type=" + trans_type, true);
	return false;
}

function raddTransaction() {
	var trans_amount;
	var trans_spent_on;
	var trans_type;
	var trans_freq;
	var trans_date;


	trans_amount 	= document.getElementById('amount').value;
	trans_spent_on 	= document.getElementById('spent_on').value;
	trans_type 	= document.getElementById('type').value;
	trans_freq 	= document.getElementById('freq').value;
	trans_date 	= document.getElementById('date').value;

	__getUrl("a?act=radd&submit=y&amount=" + trans_amount +  "&spent_on=" + trans_spent_on + "&type=" + trans_type + "&freq="+trans_freq+"&date="+trans_date, true);
	return false;
}

function __showData() {
	if(global_response_getUrl == '') {
		return false;
	} else {
		document.getElementById('data').innerHTML = global_response_getUrl;
		return true;
	}
}

/*
var doc = xhr.responseXML;   // Assign the XML file to a var
var element = doc.getElementsByTagName('root').item(0);   // Read the first element
document.ajax.dyn.value= element.firstChild.data;    // Assign the content to the form
*/

