/*
*****
Progam-ID : OnLoadTridion.js
Author : Katia De Smet.
Company : Touring (Since 01/02/2005).
Date written : 10/08/2007
Last modified : 10/08/2007 (By : Katia De Smet)

Functions :
    -   LoadTridion()
    -   OnLoadPrintPage()
Following function is copied from the internet :
    -   qsObject()
*****
*/
window.onload = LoadTridion;
function LoadTridion()
{
    /*
        Calls the functions that need to be executed after the onload of the body.
    */
    try 
    {
        OnLoadPrintPage();
    } 
    catch (e)
    {   
        //alert (e.description);
    }         
} // end LoadTridion()

//this is to store the key value pairs of the querystring
function qsObject()
{
    this.objects = new Array();
    this.add = _add;
    this.remove =_remove;
    this.item = _item;
    this.populateCollection =_populateCollection;
    this.setRawString = _setRawString;
    this.rawString ="";

    this.setRawString();
    this.populateCollection();

    // to add items to the array
    function _add(obj)
    {
        this.objects[this.objects.length] = obj;
    } // end _add()

    //to remove items from the array
    function _remove(index)
    {
        this.objects.splice(index, 1);
    } // end _remove()
    
    //gets a reference to an item object
    function _item(searchKey)
    {
        searchKey = new String(searchKey);
        searchKey = searchKey.toLowerCase();
        for(i=0;i<this.objects.length;i++)
        {
            //the current key in the coll
            var key = this.objects[i].key;
            key = new String(key);
            key = key.toLowerCase();
            if(key == searchKey)
            {
                return this.objects[i];
            } // end if
        } // end for
    } // end _item()

    function _setRawString()
    {
        //Create regular expression object to retrieve the qs part
        // Used regex 'cause the search property on the location object includes the bookmark stuff
        var qsReg = new RegExp("[?][^#]*","i");  
        hRef = unescape( window.location.href);
        var qsMatch = hRef.match(qsReg);  
        //removes the question mark from the url 
        qsMatch = new String(qsMatch);
        qsMatch = qsMatch.substr(1, qsMatch.length -1);        
        this.rawString = qsMatch;        
    } // end _setRawString()

    //takes a string and populates the array with the key/value pairs
    function _populateCollection(rawString)
    {
        this.rawString = new String(this.rawString);
        var rootArr = this.rawString.split("&");
        for(i=0;i<rootArr.length;i++)
        {
            var tempArr =  rootArr[i].split("=");
            if(tempArr.length ==2)
            {
                tempArr[0] = unescape(tempArr[0]);
                tempArr[1] = unescape(tempArr[1]);
                this.add(new qsValue(tempArr[0], tempArr[1]));
            } // end if
        } // end for
    } // end _populateCollection()

    function qsValue(key, value)
    {
        this.key = key;
        this.value=value;    
    } // end qsValue()
} // end qsObject()

function OnLoadPrintPage()
{
    try
    {
        var qs= new qsObject(); 
        var idObj= qs.item("print"); 
        if (idObj.value + "" == "1")
        {
            // Print the window - in case there is an iframe on the window, the iframe will be printed
	        if (window.print) 
	        {
		        var bContinue = true;
		        for (iCount = 0; bContinue; iCount++)
		        {
			        if (typeof(top.frames[iCount]) != "undefined")
			        {
				        if (typeof(top.frames[iCount].ifrDocument) != "undefined")
				        {
					        top.frames[iCount].ifrDocument.focus();
					        bContinue = false;
				        } // end if
			        }
			        else
				        bContinue = false;
		        } // end for
		        window.print();
	        } // end if
        } // end if
    } 
    catch (e)
    {
        // dummy
    } // end try .. catch
} // end OnLoadPrintPage()
