<!--
//
//  Esoteric javascript coding for Accounting Issue home page.
//
//  Call relevent function for each area of home page - Dependent on Tab selected - On initial load the paramenters passed to this function will be set to 1.


//  Example of code calling this script file:

//  <script type="text/javascript" src="http://www.accountingissue.info/hpLoadControl.js#obj1/ABC/obj2/XYZ/obj3/abc"></script>


//  1) The ID attribute has been removed. Even without the ID attribute there is still a simple way to reference the script element. The trick is to take advantage 
//     of the fact that as the browser is rendering the page, it executes JS code immediately when encountered. As a result, if the code in your JS file queries the DOM, 
//     the last script element in the DOM is a self-reference (i.e. a reference to the script element that loaded the code being executed). Thus, using 
//     document.getElementsByTagName("script") and referencing the last element in the array, provides an easy way to get the current script reference.

//  2) The standard querystring separator "?" has been replaced by "#". This is not necessary, but something I did for semantic reasons. To overcome the "&" entity issue, 
//     I used a slash ("/") character for separating not only key-value pairs, but keys and values too. The reason for this will become evident in a bit, but since I 
//     modified the querystring to something non-standard, I felt it was important to also remove the "?" separator which is a prefix for querystring. Instead, I used a 
//     hash ("#") character which is a pointer to a document fragment and does not have any semantic value in the context of a JS script URL. (In fact the JS URL hash 
//     technique is a common way to pass data in cross-domain XMLHttp requests for this reason and also because it does not cause a page re-load.)

//  3) Key-Value pairs. The last change I made is to use a path-based approach to key-value pairs instead of Key=Value format. I did this because it is a common technique 
//     in RESTful URL's, is easier to read and much simpler to parse. In fact, using a single "for..." loop, it becomes a trivial task to create an associative array of 
//     all the parameters for ready reference.


var myParams = getScriptUrlParams();
str  = (myParams["obj1"]);
str2 = (myParams["obj2"]);
str3 = (myParams["obj3"]);

function getScriptUrlParams()
{	
	var scriptTags = document.getElementsByTagName("script");
	// This code is assumed to be in a file so the "src" attribute	
	// is guaranteed to be present...no error-checking is needed	
	var urlFrags = scriptTags[scriptTags.length-1].src.split("#");
	var urlParams=[];
	var urlParamRaw = [];

	if (urlFrags.length > 1)
	{	    
	    urlParamRaw = urlFrags[1].split("/");
   	    if (urlParamRaw.length >= 2)
	    {	    	
		for(var param=0;param<urlParamRaw.length;param+=2)
	            urlParams[urlParamRaw[param]] = (urlParamRaw.length >= param + 1 ? unescape(urlParamRaw[param+1]) : null);
    	    }
	}
	return(urlParams);
}



//
// This code controls the functions that are called to home build page - (str = The tab selected 1 thru 4) 
//									 (str2 = Color Style No associate with tab) 
//									 (str3 = Color Style No associate with previous tab selection)  
// This function controls the main processing
// ** M A I N    P R O C E S S I N G    C O N T R O L
  
Tab(str,str2,str3);

function Tab(str,str2,str3) {

  //Update style sheet

  var newfilename = "site-styles-colors"+str2+".css";
  var oldfilename = "site-styles-colors"+str3+".css";
  var filetype = "css";
  replacejscssfile(oldfilename, newfilename, filetype);

//
//***** Note: Now replaced str3 is the new color scheme so set str3 = str2 ******  
  str3 = str2;


  var xmlHttp;
  str = String(str);

  //Update logo image at top of page
  imgid = "logo";
  imgname = "images/accounting-issue"+str2+".gif";
  updateImage(imgid, imgname);

  //Update contents of feature box
  FeatureBox(str,str2,str3);
}


// *************************************************************************
// Fuctions below are called from main function above or directly from page
// *************************************************************************


// 1 . This code updates the style sheet in the <head> part of the page

// ****** NB: These lines are for parameter example only, and NOT coding commented out!  ******* 
// replacejscssfile("oldscript.js", "newscript.js", "js") //Replace all occurences of "oldscript.js" with "newscript.js"
// replacejscssfile("oldstyle.css", "newstyle", "css") //Replace all occurences "oldstyle.css" with "newstyle.css"


function replacejscssfile(oldfilename, newfilename, filetype) {
  var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist using
  var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
  var allsuspects=document.getElementsByTagName(targetelement)
  for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
    if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
       var newelement=createjscssfile(newfilename, filetype)
       allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
    }
  }
}

// Note: This fuction is called by one directly above! 
function createjscssfile(newfilename, filetype) {
  if (filetype=="js"){ //if filename is a external JavaScript file
     var fileref=document.createElement('script')
     fileref.setAttribute("type","text/javascript")
     fileref.setAttribute("src", newfilename)
  }
  else if (filetype=="css"){ //if filename is an external CSS file
     var fileref=document.createElement("link")
     fileref.setAttribute("rel", "stylesheet")
     fileref.setAttribute("type", "text/css")
     fileref.setAttribute("href", newfilename)
  }
  return fileref
}




// 2. This code updates the Accounting Issue logo in the first row of the home page  
function updateImage(imgid, imgname) {
  var image = document.getElementById(imgid);
  image.src = imgname;
}


// 3. This code updates/builds the feature box window.
function FeatureBox(str,str2,str3) { 
   xmlHttp=GetXmlHttpObject();
   if (xmlHttp==null) {
      alert ("Browser does not support HTTP Request");
      return;
   }
   switch (str)
      {
      case "1":
	 var url="php/tab1-color-menu-hp.php";
	 break;

      case "2":
	 var url="php/tab2-color-menu-hp.php";
	 break;

      case "3":
	 var url="php/tab3-color-menu-hp.php";
	 break;

      case "4":
	 var url="php/tab4-color-menu-hp.php";
	 break;

      default:
	 var url="php/tab1-color-menu-hp.php";
 	 document.write("invalid str = ");
 	 document.write(str);
   }
   url=url+"?q="+str+"/"+str2+"/"+str3;
   url=url+"&sid="+Math.random();
   xmlHttp.onreadystatechange=stateChangedFeatureBox; 
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
}


function stateChangedFeatureBox() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 { 
 document.getElementById("hpFeatureBox").innerHTML=xmlHttp.responseText 
 } 
}




//
// 90. This code is for a general function used by all the above functions except Tab()  
//
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
// -->

