var data,UserName,CompanyName,SourceFileDir;
var loaded_files = new Array();
var included_files = new Array();
var count_includes_files = 0;
var count_included_files = 0;
var granularity = 500; //ms

// Check Browser to make sure the browser is compatible with AJAX

function ajaxRequest()
{
	var ajaxRequest;
	var theURL;
	var sendString;
	var callbackFunction;
	var ajaxInnerMsg;
	//ajaxRequest = initiateRequest();
	ajaxRequest = checkBrowser();
	ajaxRequest.onreadystatechange = processRequest;

	this.ResetAjaxMsg = function()
		{
		ajaxInnerMsg = 0;
		}
	this.GetAjaxMsg = function()
		{
		return ajaxInnerMsg;
		}
	this.SetAjaxMsg = function(Msg)
		{
		ajaxInnerMsg = Msg;
		}
	function checkBrowser()
		{
		var ajaxRequest;  
			try{
			// Opera 8.0+, Firefox, Safari
			ajaxRequest = new XMLHttpRequest();
			} catch (e){
			// Internet Explorer Browsers
				try{
				ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try{
					ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e){
					// Something went wrong
					alert("Huh! Your browser is not compatible with AJAX!");
					return false;
					}
				}
			}
		return ajaxRequest;
		}
	function processRequest()
		{
		AjaxRequestState = ajaxRequest.readyState;	
		if (ajaxRequest.readyState == 4)
			{
			AjaxMsg = "Done!";
			if (ajaxRequest.status == 200)
				{
				if (callbackFunction)
				callbackFunction(ajaxRequest, sendString);
				ajaxInnerMsg = 1;	
				}
				else
				alert("There was an error: (" + ajaxRequest.status + ") " + ajaxRequest.statusText);
			}
		AjaxMsg = "Busy!";
		}
	this.sendURL = function(GetUrl)
		{
		theURL = GetUrl;
		}
	this.sendString = function(GetString)
		{
		sendString = GetString;
		}
	this.callbackFunction = function(GetcallbackFunction)
		{
		callbackFunction = GetcallbackFunction;
		} 
	this.sendGetData = function()
		{
		if (theURL)
			{
			ajaxRequest.open("GET", theURL, true);
			ajaxRequest.send(sendString);
			}
		}

	this.sendPostData = function()
		{
		if (theURL)
			{
			ajaxRequest.open("POST", theURL, true);
			ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			ajaxRequest.send(sendString);
			}
		}
}
function checkBrowser()
{
	var ajaxRequest;  
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Huh! Your browser is not compatible with AJAX!");
				return false;
			}
		}
	}
	return ajaxRequest;
}
function Initalize()
{
	SendAccessToWebsiteReport();
	LoadDataForInitalization();
	InitalizeDivs();
	InitalizeBodies();

	var StartInitalizePageIdAndPass = function()
		{
		InitalizePageIdAndPass();
		}

	loadScript("js/init.js",StartInitalizePageIdAndPass);

	DisplayFooter();
}
function InitalizeDivs()
{
	var innerHTML;

	// Page Header
	
	innerHTML = "<div id=\"page_header_top\">";
	innerHTML += "	<div id=\"LiveStatusFeed\">";
	innerHTML += "		<div id=\"LiveStatusTitle\">";
	innerHTML += "		</div>";
	innerHTML += "		<div id=\"LiveStatusData\">";
	innerHTML += "		</div>";
	innerHTML += "	</div>";
	innerHTML += "</div>";

	// Iframe for download

	innerHTML += "<iframe id=\"df\" name=\"df\">";
	innerHTML += "</iframe>";

	// Overlay

	innerHTML += "<div id=\"fade\"";
	innerHTML += "<div id=\"overlay\"";
	innerHTML += "</div>";
	innerHTML += "</div>";

	// Bodies

	innerHTML += "<div id=\"Bodies\">";
	innerHTML += "</div>";
	
	// Footer Line
	
	innerHTML += "<div id=\"page_footer_line\">";
	innerHTML += "</div>";

	document.getElementById("container").innerHTML = innerHTML;
}
function InitalizeBodies()
{
	innerHTML = "<div id=\"IDandPassBody\">";
	innerHTML += "</div>";

	document.getElementById("Bodies").innerHTML = innerHTML;
}
function SendAccessToWebsiteReport()
{
	var url = "includes/websiteaccesslog.php?function=AccessToWebsite";
	var objSalesData = new ajaxRequest();
	objSalesData.sendURL(url);
	objSalesData.sendString("null");
	objSalesData.sendPostData();
}
function LoadDataForInitalization()
{
	var url = "includes/load.php?function=LoadDataForInitalization";
	var objSalesData = new ajaxRequest();
	objSalesData.sendURL(url);
	objSalesData.sendString("null");
	objSalesData.callbackFunction(RetrieveDataForInitalization);
	objSalesData.sendPostData();
}
function RetrieveDataForInitalization(returnData)
{
	var DataSplit = returnData.responseText.replace(/^\s+/,"").replace(/\s+$/,"").split("|");
	CompanyName = DataSplit[0];
	SourceFileDir = DataSplit[1];
}
function loadScript(sScriptSrc,callbackfunction)   
{  
	//gets document head element  
	var oHead = document.getElementsByTagName('head')[0];  

	if(oHead)  
	{  
        //creates a new script tag        
        var oScript = document.createElement('script');  
                  
        //adds src and type attribute to script tag  
        oScript.setAttribute('src',sScriptSrc);  
        oScript.setAttribute('type','text/javascript');  

	if(navigator.appName!="Microsoft Internet Explorer")
		{
		//calling a function after the js is loaded (Firefox)  
		oScript.onload = callbackfunction;  
		}
		else
		{
		//calling a function after the js is loaded (IE)  
		var loadFunction = function()  
			{  
			if (this.readyState == 'complete' || this.readyState == 'loaded')  
				{  
				callbackfunction();   
				}  
			};  

		oScript.onreadystatechange = loadFunction;  
		}
  
	//append the script tag to document head element          
	oHead.appendChild(oScript);  
	}  
}  
function include_js(sScriptSrc)   
{  
	//gets document head element  
	var oHead = document.getElementsByTagName('head')[0];  

	if(oHead)  
		{  
		//creates a new script tag        
		var oScript = document.createElement('script');  
		//adds src and type attribute to script tag  
		oScript.setAttribute('src',sScriptSrc);  
		oScript.setAttribute('type','text/javascript');  

		if(navigator.appName!="Microsoft Internet Explorer")
			{
			//calling a function after the js is loaded (Firefox)  
			oScript.onload = function ()
				{
				loaded_files[count_includes_files] = sScriptSrc;
				count_includes_files++;
				}
			}
			else
			{
			//calling a function after the js is loaded (IE)  
			oScript.onreadystatechange = function ()
				{  
				if (this.readyState == 'complete' || this.readyState == 'loaded')  
					{  
					loaded_files[count_includes_files] = sScriptSrc;
					count_includes_files++;
					}  
				}  

			}
		//append the script tag to document head element          
		oHead.appendChild(oScript);  
		}  
}
function include_once(file,type)
{
	var found = false;
	if (count_included_files>0)
		{
		for(a=0;a<count_included_files;a++)
			{
			if(included_files[a]==file)
				{
				found=true;
				a=count_included_files+1;
				}
			}
		found=false;
		}
		
	if(found==false)
		{
		included_files[count_included_files] = file;
		count_included_files++;
		switch(type)
			{
			case 'js':
				include_js(file);
				break;
			case 'css':
				include_css(file);
				break;
			default:
				include_js(file);
				break;
			}     
		}
}
function waitToLoadFiles(postLoadFunc)
{
	if(areAllFilesLoaded())
		setTimeout(postLoadFunc, 1);
		else
		setTimeout("waitToLoadFiles('"+postLoadFunc+"')", granularity);
}
function areAllFilesLoaded()
{
	if(included_files.length == loaded_files.length)
		return true;
		else
		return false;
}
function DisplayFooter()
{
	document.getElementById("page_footer_line").innerHTML = "Web application development - steven@724care.net || Sales - sales@724care.net Copyright (C) 724 Care, Inc. Philippines All rights reserved."; 
}
