//##############################################################################
// GENERAL FUNCTIONS ###########################################################
//##############################################################################

//preloading images and files
window.onload = function() {
	setTimeout(function() {
		// XHR to request a JS and a CSS
		//var xhr = new XMLHttpRequest();
		//xhr.open('GET', 'http://domain.tld/preload.js');
		//xhr.send('');
		//xhr = new XMLHttpRequest();
		//xhr.open('GET', 'http://domain.tld/preload.css');
		//xhr.send('');
		// preload image
		new Image().src = "http://franscape.com/images/ajax-loading.gif";
		new Image().src = "http://franscape.com/images/ajax-loading2.gif";
	}, 1000);
};

function confirmHide()
{	
	var agree=confirm("Are you sure you wish to HIDE this item?");
	if (agree)
	return true ;
	else
	return false ;
}	

function confirmExecute()
{	
	var agree=confirm("Erm, are you sure you wanna do that?");
	if (agree)
	return true ;
	else
	return false ;
}	

function confirmShow()
{	
	var agree=confirm("Are you sure you wish to SHOW this item?");
	if (agree)
	return true;
	else
	return false ;
}

function confirmDelete()
{	
	var agree=confirm("Are you sure you wish to DELETE this item?");
	if (agree)
	return true ;
	else
	return false ;
}

function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}

var xmlhttp = new getXMLObject();
var xmlhttpUpdate = new getXMLObject();

function handleServerResponse() {
   
	if (xmlhttp.readyState == 4) 
	{
		if(xmlhttp.status == 200) 
		{
			document.getElementById("txtResult").innerHTML=xmlhttp.responseText; //Update the HTML Form element 
		}
		else 
		{
			alert("Error during AJAX call. Please try again");
		}
	}
}

function hndlServResp(location) 
{
	if (xmlhttp.readyState == 4) 
	{
		alert(location.innerHTML);
		if(xmlhttp.status == 200) 
		{
			location.innerHTML=xmlhttp.responseText; //Update the HTML Form element 
		}
		else 
		{
			alert("Error during AJAX call. Please try again");
		}
	}
}


//##############################################################################
// APP FUNCTIONS  ##############################################################
//##############################################################################

function updateCounts(type)
{
	var getdate = new Date();
	if(xmlhttpUpdate) 
	{	
		xmlhttpUpdate.open("GET","mngapps.php?directive=count&location="+type,true); //calling PHP handler using POST method
		xmlhttpUpdate.onreadystatechange = function(){
			if(xmlhttpUpdate.readyState==4) {
				if(xmlhttpUpdate.status==200){
					document.getElementById(type+"Count").innerHTML = xmlhttpUpdate.responseText;
				}else{alert("ajax error in updateCounts");}
			}
		};
		
		xmlhttpUpdate.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttpUpdate.send(); //Posting to PHP File
	}
}

function adminShowApp(aid)
{
	var getdate = new Date();
	if(xmlhttp)
	{
		
		var ajaxLocation = document.getElementById("appRecordResult");
		
		//Show the window
		document.getElementById("appRecord").style.display = '';
		
		//Fetch and display the record
		
		// here is where you can show pending image
		window.scrollTo(0,0);
		ajaxLocation.innerHTML="<div style=\"overflow-y:scroll; height:330px; padding:20px; border-bottom:solid 4px #171c29;\"><br><br><br><br><strong>Please wait!</strong><br><image src=http://franscape.com/images/ajax-loading.gif>&nbsp;</image><br><br><br><br></div>";
		
		xmlhttp.open("GET","mngapps.php?directive=viewapps&aid="+aid,true); //calling PHP handler using GET method
		//document.getElementById("appRecordResult").style.display = 'none';
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState==4) {
				if(xmlhttp.status==200){
					ajaxLocation.innerHTML = xmlhttp.responseText;
				}else{alert("ajax error");}
			}
		};
		
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(); //Posting to PHP File
	}
}

function adminShowFb(fbid)
{
	var getdate = new Date();
	if(xmlhttp)
	{
		
		var ajaxLocation = document.getElementById("feedbackTxtResult");
		
		//Show the window
		document.getElementById("fbRecord").style.display = '';
		
		//Fetch and display the record
		
		// here is where you can show pending image
		window.scrollTo(0,0);
		ajaxLocation.innerHTML="<div style=\"overflow-y:scroll; height:330px; padding:20px; border-bottom:solid 4px #171c29;\"><br><br><br><br><strong>Please wait!</strong><br><image src=http://franscape.com/images/ajax-loading.gif>&nbsp;</image><br><br><br><br></div>";
		
		xmlhttp.open("GET","mngapps.php?directive=viewfbs&fbid="+fbid,true); //calling PHP handler using GET method
		//document.getElementById("appRecordResult").style.display = 'none';
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState==4) {
				if(xmlhttp.status==200){
					ajaxLocation.innerHTML = xmlhttp.responseText;
				}else{alert("ajax error");}
			}
		};
		
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(); //Posting to PHP File
	}
}


function submitApp()
{
	var getdate = new Date();
	if(xmlhttp)
	{
		var formSubmitted 	= document.getElementById("formSubmit").value;
		var formAppType 	= document.getElementById("app_type").value;
		var formUFName		= document.getElementById("fname").value;
		var formUserAge		= document.getElementById("uage").value;
		var formUserGender 	= document.getElementById("ugender").value;
		var formAttReq 		= document.getElementById("attendance_requirement").value;
		var formUserPers	= document.getElementById("upersonality").value;
		var formJoinReason 	= document.getElementById("ujoin_reason").value;
		var formLeaveReason = document.getElementById("uguild_leave_reason").value;
		var formCharName 	= document.getElementById("charname").value;
		var formClassSpec 	= document.getElementById("class_and_spec").value;
		var formCurrGuild 	= document.getElementById("current_guild").value;
		var formSurrServ 	= document.getElementById("current_server").value;
		var formDurPlay 	= document.getElementById("duration_playing").value;
		var formRaidExp 	= document.getElementById("raid_experience").value;
		var formRGuildHis 	= document.getElementById("raid_guild_history").value;
		var formOffGuild 	= document.getElementById("offer_to_guild").value;
		var formCompStats 	= document.getElementById("comp_stats").value;
		var formNetCon 		= document.getElementById("net_connection").value;
		var formVentCheck 	= document.getElementById("ventrillo_check").value;
		var formMicCheck 	= document.getElementById("microphone_check").value;
		var formAddonSkill 	= document.getElementById("skill_with_addons").value;
		var formInfoCheck 	= document.getElementById("read_info_check").value;
		var formSrecCheck 	= document.getElementById("read_srec_check").value;
		var formRrecCheck 	= document.getElementById("read_rrec_check").value;
		
		// here is where you can show pending image
		window.scrollTo(0,400);
		document.getElementById("txtResult").innerHTML="<br><br><br><br><strong>Please wait!</strong><br><image src=http://franscape.com/images/ajax-loading.gif>&nbsp;</image><br><br><br><br>";
		
		xmlhttp.open("POST","http://souloftheempire.com/mngapps.php?directive=submitapp",true); //calling PHP handler using POST method
		xmlhttp.onreadystatechange = handleServerResponse;
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		
		xmlhttp.send("formSubmit=" + formSubmitted + 
		"&app_type=" + formAppType +
		"&fname=" + formUFName +
		"&uage=" + formUserAge +
		"&ugender=" + formUserGender 	+
		"&attendance_requirement=" + formAttReq 		+
		"&upersonality=" + formUserPers	+
		"&ujoin_reason=" + formJoinReason 	+
		"&uguild_leave_reason=" + formLeaveReason +
		"&charname=" + formCharName 	+
		"&class_and_spec=" + formClassSpec 	+
		"&current_guild=" + formCurrGuild 	+
		"&current_server=" + formSurrServ 	+
		"&duration_playing=" + formDurPlay 	+
		"&raid_experience=" + formRaidExp 	+
		"&raid_guild_history=" + formRGuildHis 	+
		"&offer_to_guild=" + formOffGuild 	+
		"&comp_stats=" + formCompStats 	+
		"&net_connection=" + formNetCon 		+
		"&ventrillo_check=" + formVentCheck 	+
		"&microphone_check=" + formMicCheck 	+
		"&addon_skill=" + formAddonSkill 	+
		"&read_info_check=" + formInfoCheck 	+
		"&read_srec_check=" + formSrecCheck 	+
		"&read_rrec_check=" + formRrecCheck
		); 	
	}
}

function addComment(aid)
{
	var getdate = new Date();
	if(xmlhttp)
	{
		var cformComment = document.getElementById("commentField").value;
		var cformSubmit = document.getElementById("commSubmitted").value;
		var ajaxLocation = document.getElementById("appRecordResult");
		
		ajaxLocation.innerHTML="<div style=\"overflow-y:scroll; height:330px; padding:20px; border-bottom:solid 4px #171c29;\"><br><br><br><br><strong>Please wait!</strong><br><image src=http://franscape.com/images/ajax-loading.gif>&nbsp;</image><br><br><br><br></div>";
		xmlhttp.open("POST","http://souloftheempire.com/mngapps.php?directive=addcomment&aid="+aid,true); //calling PHP handler using POST method
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState==4) {
				if(xmlhttp.status==200){
					ajaxLocation.innerHTML = xmlhttp.responseText;
				}else{alert("ajax error");}
			}
		};
		
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send("commSubmitted=" + cformSubmit + "&commentbody=" + cformComment);
		
	}
}

function submitFeedback()
{
	var getdate = new Date();
	if(xmlhttp)
	{
		var fbFormContent = document.getElementById("fbContent").value;
		var fbFormSubmit = document.getElementById("fbSubmit").value;
		var ajaxLocation = document.getElementById("fdbckFormArea");
		
		ajaxLocation.innerHTML="<div style=\"overflow-y:scroll; height:330px; padding:20px; border-bottom:solid 4px #171c29;\"><br><br><br><br><strong>Please wait!</strong><br><image src=http://franscape.com/images/ajax-loading.gif>&nbsp;</image><br><br><br><br></div>";
		
		xmlhttp.open("POST","http://souloftheempire.com/mngapps.php?directive=sendfb",true); //calling PHP handler using POST method
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState==4) {
				if(xmlhttp.status==200){
					ajaxLocation.innerHTML = xmlhttp.responseText;
				}else{alert("ajax error");}
			}
		};
		
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send("fbfsubmit=" + fbFormSubmit + "&fbformbody=" + fbFormContent);
		
	}
}
function adminDeleteItem(origin, itemId, ajaxLocation)
{
	var getdate = new Date();  //Used to prevent caching during ajax call
	var check = confirmDelete();
	var itemDiv;
	var cntLoc;
	
	switch(origin)
	{
		case "35ce1d4eb0": //feedback
			itemDiv = "fbrecorditem"+itemId;
			cntLoc = "feedback";
			break;
		case "d2a57dc1d8": //apps
			itemDiv = "apprecorditem"+itemId;
			cntLoc = "apps";
			break;
		default:
			itemDiv = null;
			cntLoc = null;
			break;
	}
	
	if(xmlhttp && check) 
	{	
		//document.getElementsByName(itemDiv)[0].style.display = 'none';
		xmlhttp.open("GET","mngapps.php?directive=delete&s="+check+"&origin="+origin+"&id="+itemId,true); //calling PHP handler using POST method
		xmlhttp.onreadystatechange = function(){
			if(xmlhttp.readyState==4) {
				if(xmlhttp.status==200){
					ajaxLocation.innerHTML = xmlhttp.responseText;
				}else{alert("ajax error in Delete; status: "+ xmlhttp.status);}
			}
		};
		
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlhttp.send(); //Posting to PHP File
	}
}
