// Get the HTTP Object (this is for implementation of AJAX)
function getHTTPObject(){
	if (window.ActiveXObject)        
		return new ActiveXObject("Microsoft.XMLHTTP");   
	else if (window.XMLHttpRequest)        
		return new XMLHttpRequest();   
	else {      
		alert("Your browser does not support AJAX.");      
		return null;   
	}
}

String.prototype.reverse = function(){
	splitext = this.split("");
	revertext = splitext.reverse();
	reversed = revertext.join("");
	return reversed;
}

function trim(val) {
	a = val.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};

function validate(type,txt,id) {
	if(type=="name") {
		
		return txt.substring(0,1).toUpperCase() + txt.substring(1).toLowerCase();
		
	} else if(type=="email") {
		
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(txt)){
			return (true);
		}
			alert("Invalid E-mail Address! Please re-enter.")
			return (false);
			
	} else if(type=="password") {
		
		if (txt != $('#'+id).val()) {
			alert("Please Verify that Passwords Match!");
			return (false);
		}
		return (true);
		
	}

}

function ajax(url,callback) {
	alert("switch to ajax please");
	$.ajax({type: "GET",
		url: url,
		success: function(result) {
					callback();
				}
	   });

	
}

//showChart(id, daterange, credittype) - generate the chart
function showChart(whichdiv, daterange, credittype, userid, val) {
	$('#stats_chart_' + whichdiv).html("<div style='padding-top:30px;'>CALCULATING STATS...</div>");
	url = "php/GenerateChart.php?daterange=" + daterange + "&credittype=" + credittype + "&id=" + userid + "&val=" + val;
	$.ajax({type: "GET",
		url: url,
		success: function(result) {
			chartOutput(whichdiv,result);
		}
   });
} 

//chartOutput(id) - show the chart in the <div id="id">
function chartOutput(id,result) {
	$('#stats_chart_' + id).html(result);
}


function deleteMessage(mid) {
	
	if(confirm("Are You Sure you Want to Permenantly Delete this Message?")) {
		msgDelete(mid);
	}
}

function msgDelete(mid) {
	
	url = "php/msgDelete.php?mid=" + mid;
	$.get(url,null,function(){refreshPage();});
}

//confirmupdate() - when db is updated, show confirmation + fade out after 5s
function confirmUpdate() {
	$('#confirmation').html("Account Updated...");
	$('#confirmation').fadeIn('slow');
	setTimeout(function () {
						 	$('#confirmation').fadeOut('slow');
							//location.reload(true);
							}
							,5000);
}

function refreshPage() {
	window.location.href = unescape(window.location.pathname);
}
