﻿//<![CDATA[
//#################################################### START AJAX SETTINGS 
//#################################################### START AJAX SETTINGS
// Simple Asynchronous Threadsafe AJAX Code
// Version 20100527a This simple version is only used to demonstrate AJAX.
// This notice must stay with this code
// Updates of this code and other code may be available from http://www.ooChaCha.com/code/
 
//DEPENDENCIES:
// fnGetFileExtention, fnReset (dTorso,dLeft,dMiddle and dRight) for incoming information
// gAltPage for none AJAX browsers

var vDefaultMimeType="text/html";
var vDefaultDivTarget="dMiddle"; //make global
var vDefaultMethod="GET";
var vDefaultHomePage = "/index.html";
var vSendData="";
var vAllowExt=".jpg#.htm#.html#.asp#.php#.txt#.xml#.aspx#";

function fnAjax(url,daDiv,daStr,cacheNum){ //args 0=url,1=daDiv,2=data,cacheNum
	//// onreadystatechange return codes // 0 (uninitialized)  // 1 (loading)  // 2 (loaded)  // 3 (interactive)  // 4 (complete) 
    // START Test arguments
  var isPass=true;
  if(cacheNum<=-1){
	//do nothing, ie turn off ajax for this call
	isPass=false;
  }else{
	if(url=="" || url==null){alert("(28) ERROR : No URL Declared");isPass=false;}
	if(isPass){
		var vDisExt=fnGetFileExtension(url);
		//var vDisExt_example=fnGetFileExtension("http://www.asp.fred.com/ddd.asp?id=fred&pg=whatever");
		//alert("32 : " + vDisExt_example);
        switch(vDisExt){
    		case ".htm":case ".html":case ".txt": case ".xml":vMethod="GET";break;
    		case "asp":case "php": case ".aspx":vMethod="POST";break;
    		default:vMethod=vDefaultMethod;
    		}
        // set daDiv to default div if not declared
        if(daDiv=="" || daDiv==null){daDiv=vDefaultDivTarget;}
		// test if div exists on this page
		if(!document.getElementById(daDiv)){isPass=false;alert("46 ERROR : " + daDiv + " not found on page")}
    }
  }
    // END Test arguments
    function fnAjaxBindCallback(){
        if (ajaxRequest.readyState == 4) {
			if (ajaxRequest.status == 200) {
                     if (ajaxCallback) { // if ajaxCallback exists
						 var daReturnOrg = ajaxRequest.responseText;
						 var daReturnNew = "";
						 var arrRegExp = new Array;
						 arrRegExp[0] = /(<\!-- AJAX CONTENT START -->)([\S\s]*?)(<\!-- AJAX CONTENT END -->)/i; //daGroup = 2
						 arrRegExp[1] = /(<body([\S\s]*?)>)([\S\s]*?)(<\/body>)/i; //daGroup=3
						 var sw=0;
						 function fnMatch(disReg,disGroup){
							var match = arrRegExp[disReg].exec(daReturnOrg);
							if (match != null) {
								daReturnNew = match[disGroup];
								sw=1; // found!
							}
						 }
						 fnMatch(0,2);
						 if(sw==0){fnMatch(1,3);}
						 if(sw==0){daReturnNew=daReturnOrg}
                         document.getElementById(daDiv).innerHTML=daReturnNew;
                     }else{
                         document.getElementById(daDiv).innerHTML="<br><br>NOTICE 1: " + request.status + "<br>Sorry, undefined callback";
                     }
					 fnReset();
                } else if (ajaxRequest.status == 404) {
                     document.getElementById(daDiv).innerHTML="<br><br>NOTICE 2: " + ajaxRequest.status + "<br>Sorry, the requested page was not found";
                } else if (ajaxRequest.status == 403) {
                     document.getElementById(daDiv).innerHTML="<br><br>NOTICE 3: " + ajaxRequest.status + "<br>Sorry, Page access denied";
                } else {
                     document.getElementById(daDiv).innerHTML="<br><br>NOTICE 4: " + ajaxRequest.status + "<br>Sorry, we&acute;re unable to retrieve the information for you";
            } // END if (ajaxRequest.status == 200) {
        } // END if (ajaxRequest.readyState == 4) {
    }
	// END function fnAjaxBindCallback(){

  if(isPass){
    // use a local variable to hold our request and callback until the inner function is called
    var ajaxRequest = null; 
    var ajaxCallback = daDiv; // assign name as div target
    // bind our callback then hit the server...
	if(vSendData==""){vSendData="v01=" + encodeURI("empty");} //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    // 
    if (window.ActiveXObject == null) { // Mozilla, Safari,...
          ajaxRequest = new XMLHttpRequest();
          if (ajaxRequest.overrideMimeType) { //overcomes firefox browser issue
             ajaxRequest.overrideMimeType(vDefaultMimeType);// set type accordingly to anticipated content type: if used for innerHTML then text/plain
          }
      } else if (window.ActiveXObject != null) { // IE
	      var vSuccess = false;
          var reqXArr = new Array("Msxml2.XMLHTTP.6.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP");
          for (var i = 0;i < reqXArr.length-1 && !vSuccess;i++){
               try{ajaxRequest = new ActiveXObject(reqXArr[i]);vSuccess=true;}
               catch (e){isPass=false;}
          }
		  if(!vSuccess){isPass=false;alert("(88) ERROR: No IE AJAX object created")} //force
      }
      //##### SEND REQUEST
      if (!ajaxRequest || !isPass){
          alert('(92) ERROR: Sorry, you may have an old browser that is unable to use AJAX standards');
          location.href=gAltPage;
      }else{
	      ajaxRequest.onreadystatechange = fnAjaxBindCallback;
          ajaxRequest.open(vMethod, url, true); // POST is default
	      ajaxRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=UTF-8;');
	      ajaxRequest.setRequestHeader("Connection", "close");
          ajaxRequest.send(vSendData); // vSendData is formated string sent to called page for processing, usually in asp or php page.
      }
  }// END if(isPass){
}

//#################################################### END AJAX SETTINGS 
//#################################################### END AJAX SETTINGS 
//]]
