﻿/*<![CDATA[*/
////////////////////////////////////////////////////////////
// START OF SIMPLE AJAX ////////////////////////////////////
////////////////////////////////////////////////////////////
// NOTICE:
// Copyright 1995-current, David Keane ooChaCha.com
// You're free to use this code but no free support given.
// Please leave this notice, Thanks
//
// If I get around to it, new code and tutorials may be
// found at http://www.ooChaCha.com/tutorials
// Enjoy
//
///////////////////////////////////////////////////////////
// CODE:
// Version 20091117a
// simpleAJAX code uses the single call, GET method
// Code not cross-browser, but can use with proxy method.
// Remember, AJAX code works in http://blaBla not c:\file
// EXTERNAL DEPENDENCIES
// vars:
// functions: 
// objects:
 
var gDom=document.domain;
var gPutInThisDiv;
var xmlHttp=null;
var gURL="";
var gStopCache = true;

function fnRunAjax(){ // usually only need fnRunAjax(fileToGet,putInDisDiv)
        // Pass data as follows and in correct order, fnRunAjax(fileToGet,putInDisDiv,strStuff)
		// eg fnRunAjax("myPage.htm","dMiddle","b=fred&c=this%20text")
        // strStuff must be encodeURI() ie "&bla01=" + encodeURI(value01) + "&bla02=" + encodeURI(value02)
        var arrFrn = fnRunAjax.arguments; // use arguments method so new versions of simpleAjax can replace old without the need to change existing code in html
        if(!arrFrn[0] || arrFrn[0].length==0 ){alert("(Err 35ssa) No page to get? ");return;}else{}
        if(!arrFrn[1] || arrFrn[1].length==0 ){alert("(Err 36ssa) No target id, Which div id? ");return;}else{}
		if(!arrFrn[2] || arrFrn[2].length==0){arrFrn[2]="";}else{} // allowed not to declare strStuff
		if(!arrFrn[3] || arrFrn[3].length==0){gStopCache=true;}else{gStopCache=false;} // allowed not to declare strStuff
        if(!document.getElementById(arrFrn[1])){alert("(Err 37ssa) Oops, the target object (usually a div) does not exist?");return;}
		if(fnAjaxFileExtension(arrFrn[0])=="FAIL"){return;}
        gPutInThisDiv = arrFrn[1]; // makes putInDisDiv global variable so it can be used by other functions 
        xmlHttp=fnGetAjaxObject();
        if (xmlHttp==null){
          alert ("OOPs, your browser is very old and does not support AJAX");
          return;
          } 
        var vURL=arrFrn[0] + "?fr=" + gDom + "" + arrFrn[2]; //javascript use encodeURI(,)Note strStuff should already be correct eg x=1&b=fred&c=this%20text 
        if(gStopCache){vURL=vURL + "&nc=" + Math.random();} //stops cache
        xmlHttp.onreadystatechange=stateChanged; // checks to see if page loaded
        xmlHttp.open("GET",vURL,true);
        xmlHttp.send(null);
    } 

function stateChanged(){ 
        if (xmlHttp.readyState==4){ 
		   if (xmlHttp.status == 200) {
    		    var daReturnOrg = xmlHttp.responseText;
				var daReturnNew = "";
				 // We don't want to bring in the full HTML document only the body content or one part of the page between the AJAX CONTENT comments.
                 // Remember javascript brought in via simpleAJAX will not work.
				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];
                        document.getElementById(gPutInThisDiv).innerHTML=daReturnNew;
						sw=1; // found!
                    }
 				}
				fnMatch(0,2);
				if(sw==0){fnMatch(1,3);}
                if(sw==0){ //all arrRegExp failed :(
                    alert("The AJAX page being called is not standard\nPages should be standard HTML, ASP or PHP websites");
                    document.getElementById(gPutInThisDiv).innerHTML=daReturnOrg;
                }
           }else{
			    var vMsg="";
				var daCase=(xmlHttp.status).toString();
			    switch(daCase){
					case "404":vMsg="Oops, Page not found :(";break;
					case "403":vMsg="oops, Page access denied";break;
					default: vMsg="AJAX Page error " + xmlHttp.status + "\nUnable to get information";break;
					}
                alert(vMsg); // turned on if you are having trouble understanding the xmlHttp.status
           }
        }
}

function fnGetAjaxObject(){
       var disAjax=null; //set ajax object to nothing to start
       try{
          disAjax=new XMLHttpRequest(); //ff,o,s
		     // to stop ff and safari bugs
		     if (disAjax.overrideMimeType){disAjax.overrideMimeType('text/html');} //for firefox error// set to anticipated content type
         }catch(e){  // IE
             try{disAjax=new ActiveXObject("Msxml2.XMLHTTP");
           }catch(e){
             try{disAjax=new ActiveXObject("Microsoft.XMLHTTP");
           }catch(e){
    	     alert("Sorry, your browser seems to old to run modern Websites - please upgrade")
           }
         }
       }
       return disAjax;
}
function fnAjaxFileExtension(){
	var disAns="FAIL";
	var arrAtrGFE=fnAjaxFileExtension.arguments;
	//alert("83 " + arrAtrGFE.length);
	if(arrAtrGFE.length>=1){
    	var disSplitGFE=arrAtrGFE[0].split("?")[0];
		if(disSplitGFE.length>=5){
    	    disSplitGFE="." + (disSplitGFE.substring(disSplitGFE.length-6,disSplitGFE.length)).split(".")[1];
            if(disSplitGFE.match(/(\.jpg)|(\.gif)|(\.png)|(\.swf)|(\.flv)|(\.mov)|(\.wmv)|(\.mp3)/)!=null){
               alert("(Err 119ssa)You should always have media embedded in a coded page, never raw!!)");
            }else{
               if(disSplitGFE.match(/(\.htm)|(\.html)|(\.asp)|(\.php)|(\.txt)|(\.xml)|(\.aspx)|(\.inc)|(\.oocc)/)!=null){
			      disAns=disSplitGFE;
			   }else{alert("(Err 123ssa) File type is not a valid AJAX extension");}
			}
		}else{alert("(Err 125saa) File Name length does not seem correct");}
	}else{alert("(Err 126ssa) No file named");}
	return disAns;
}
////////////////////////////////////////////////////////////
// END OF SIMPLE AJAX //////////////////////////////////////
////////////////////////////////////////////////////////////
/*]]>*/
