﻿// JScript File

/////////////////////////////////////////
// GLOBAL VARIABLES                    //
/////////////////////////////////////////
var blinkTimer = new Object();
var selectboxes = new Object();

/////////////////////////////////////////
// getID(id)                           //
// 2006-12-20 Andy Male                //
//                                     //
// Used to get document object by ID   //
// name.                               //
//                                     //
// Depends on nothing                  //
//                                     //
// id = the id of the object from the  //
// document                            //
/////////////////////////////////////////
function getID(id)
{
    var obj = document.getElementById ? document.getElementById(id) :
    document.all ? document.all[id] :
    document.layers ? document.layers[id] : null;
    var obj = document.getElementById(id)
    return obj;
}

/////////////////////////////////////////
// findPos(id)                         //
// QuirksMode.org - Peter-Paul Koch    //
//                                     //
// Used to find the real position of   //
// an element                          //
//                                     //
// Depends on nothing                  //
/////////////////////////////////////////
function findPos(obj) 
{
	var curleft = obj.offsetLeft;
	var curtop = obj.offsetTop;
	if (obj.offsetParent) 
	{
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

/////////////////////////////////////////
// inArray()                           //
/////////////////////////////////////////
function inArray(arrayList, val)
{
    var returnVal_ = false;
    for (var idx = 0; idx < arrayList.length; idx++)
    {
        if(arrayList[idx] == val) 
        {
            returnVal_ = true;
        }
    }
    return returnVal_;
}

//////////////////////////////////
// trim()                       //
// 2006-12-20 Andy Male         //
// Regular expression to trim   //
// leading and trailing spaces  //
//////////////////////////////////
//Left Trim
function ltrim(str)
{
    return str.replace(/^\s*/, "");
}
//Right Trim
function rtrim(str)
{
    return str.replace(/\s*$/, "");
}
//Trim
function trim(str)
{
    return rtrim(ltrim(str));
}

/////////////////////////////////////////
// pageOverlay()                       //
// 2006-12-20 Andy Male                //
//                                     //
// Used to overlay an image across the //
// whole page by layering a DIV and    //
// stretching it to the size of the    //
// next relative object which is a     //
// marker on the page. Has property    //
// true/false to turn on or off.       //
//                                     //
// Depends on getID()                  //
//            hideInputs(state)        //
/////////////////////////////////////////
function pageOverlay(state)
{
    if (state)
    {
        //getID('pageOverlay').style.height=document.documentElement.clientHeight+'px';
        getID('pageOverlay').style.height=getID('pageWrapper').offsetHeight+'px';
        //getID('pageOverlay').style.width=document.documentElement.clientWidth+'px';
        //getID('pageOverlay').style.width=getID('pageWrapper').offsetWidth+'px';
        // aletered so it us always the maximum width of the window
        getID('pageOverlay').style.width='100%';
        hideInputs(true);
        getID('pageOverlay').style.visibility = 'visible';
    }
    else
    {
        getID('pageOverlay').style.height='0px';
        getID('pageOverlay').style.width=='0px';
        hideInputs(false);
        getID('pageOverlay').style.visibility = 'hidden';
    }
}


/////////////////////////////////////////
// hideInputs()                        //
// 2007-02-14 Andy Male                //
//                                     //
// IE 5.5 and 6.0 bug fix              //
// Hide all the inputs so they dont    //
// appear over the top of floating     //
// items                               //
//                                     //
// Depends on getID()                  //
/////////////////////////////////////////
function hideInputs(state)
{
    // USING GLOBAL VARIABLE 'selectboxes'
    if(state)
    // turn them off
    {
        selectboxes = document.getElementsByTagName('SELECT');
        {
            for (var x=0; x < selectboxes.length; x++)
            {
                selectboxes[x].style.visibility = 'hidden';
            }
        }
    }
    else
    // selectboxes should be set already and equal to the 
    {
        if(selectboxes != document.getElementsByTagName('SELECT'))
        {
            selectboxes == document.getElementsByTagName('SELECT')    
        }
        for (var x=0; x < selectboxes.length; x++)
        {
            selectboxes[x].style.visibility = 'visible';
        }
    }
}

/////////////////////////////////////////
// hideIt(id)                          //
// 2007-01-09 Andy Male                //
//                                     //
// Used to hide and element by         //
// positioning it off the visisble     //
// screen                              //
//                                     //
// Depends on getID()                  //
//                                     //
// id = the id of the object from the  //
// document                            //
/////////////////////////////////////////
function hideIt(id)
{
    getID(id).style.position = 'absolute';
    getID(id).style.left='-5000px';
    getID(id).style.top='-5000px';
}

/////////////////////////////////////////
// showIt(id)                          //
// 2007-01-09 Andy Male                //
//                                     //
// Used to show an element which is    //
// positioned off the visisble screen  //
// assumes the correct position is at  //
// zero left,top i.e. top=0px left=0px //
//                                     //
// Depends on getID()                  //
//                                     //
// id = the id of the object from the  //
// document                            //
/////////////////////////////////////////
function showIt(id)
{
    getID(id).style.position = 'relative';
    getID(id).style.left='0px';
    getID(id).style.top='0px';
}

/////////////////////////////////////////
// showHide(show_id, hide_id)          //
// 2007-01-09 Andy Male                //
//                                     //
// Combines showIt() and hideIt()      //
//                                     //
// Depends on                          //
// getID(), showIt(),hideIt()          //
//                                     //
// show_id = object to show            //
// show_id = object to hide            //
/////////////////////////////////////////
function showHide(show_id, hide_id)
{
    showIt(show_id);
    hideIt(hide_id);
}

/////////////////////////////////////////
// showFloatBox(id)                    //
// 2007-01-09 Andy Male                //
//                                     //
// Sets the position of an object and  //
// its z-index, implements             //
// pageOverlay() to mask out the page  //
//                                     //
// Depends on                          //
// getID(), pageOverlay()              //
//                                     //
// id = object to show                 //
/////////////////////////////////////////
function showFloatBox(id)
{
    pageOverlay(true);
    getID(id).style.position='absolute';
    getID(id).style.left='150px';
    getID(id).style.top='300px';
    getID(id).style.zIndex ='2000';
}

/////////////////////////////////////////
// hideFloatBox(id)                    //
// 2007-01-09 Andy Male                //
//                                     //
// Sets the position of an object      //
// to -5000,-5000 off the visible page //
// turns off pageOverlay()             //
//                                     //
// Depends on                          //
// getID(), pageOverlay()              //
//                                     //
// id = object to show                 //
/////////////////////////////////////////
function hideFloatBox(id)
{
    pageOverlay(false);
    getID(id).style.left='-5000px';
    getID(id).style.top='-5000px';
    getID(id).style.zIndex ='-1';
}

/////////////////////////////////////////
// checkPageTopEdge(id, topPos)        //
// 2007-02-02 Andy Male                //
//                                     //
// Checks the new object will fit on   //
// screen.                             //
//                                     //
// Depends on                          //
// getID()                             //
//                                     //
// id = object to show                 //
// topPos = the proposed position      //
/////////////////////////////////////////
function checkPageTopEdge (id, topPos)
{
    var checkTop = parseInt(topPos) - parseInt(getID(id).offsetTop)- parseInt(document.documentElement.offsetTop);
    if (checkTop < 0)
    {
        topPos -= checkTop;
        topPos += 50;  // THIS COMPENSATES FOR SCROLLIES AND PADDING
    }
    return topPos;
}

/////////////////////////////////////////
// fixPosition(id)                     //
// 2007-02-02 Andy Male                //
//                                     //
// Checks the new object will fit on   //
// screen without extending past the   //
// footer                              //
//                                     //
// Depends on                          //
// getID()                             //
//                                     //
// id = object to show                 //
/////////////////////////////////////////
function fixPosition(id)
{
    var newTop = 0;
    var objPos = findPos(getID(id));
    var objTop = parseInt(objPos[1]);
    var objHeight = parseInt(getID(id).offsetHeight);
    var pageHeight = parseInt(document.documentElement.offsetHeight);
    var windowHeight = parseInt(document.documentElement.clientHeight);
    if (windowHeight < pageHeight)
    {
        pageHeight = windowHeight;
    }
    if ((objTop + objHeight) > pageHeight)
    {
        if ((objHeight+50) > pageHeight)
        {
            newTop = 10;  // 10 pixels below top of client viewable screen
        }
        else
        {
            newTop = pageHeight - objHeight - 50; // 50 pixels above bottom viewable portion of screen
        }
        getID(id).style.top = newTop + 'px';
    }
}

/////////////////////////////////////////
// checkPageRightEdge(id, leftPos)     //
// 2007-02-02 Andy Male                //
//                                     //
// Sets the position of an object and  //
//                                     //
// Depends on                          //
// getID()                             //
//                                     //
// id = object to show                 //
/////////////////////////////////////////
function checkPageRightEdge (id, leftPos)
{
    var checkRight = parseInt(document.documentElement.offsetWidth) - parseInt(leftPos) - parseInt(getID(id).offsetWidth);
    if (checkRight < 30)
    {
        leftPos += checkRight;
        leftPos = leftPos - 50;  // THIS COMPENSATES FOR SCROLLIES AND PADDING
    }
    return leftPos;
}

/////////////////////////////////////////
// showMenu(thisid, id, menuPos)       //
// 2007-02-02 Andy Male                //
//                                     //
// Sets the position of an object and  //
// its z-index                         //
//                                     //
// Depends on:                         //
// getID(), checkPageRightEdge()       //
//                                     //
// thisid = calling object position    //
// id = object to show                 //
// menuPos = position to display       //   
// the object which can be one of      //
//      - topLeft                      //
//      - topRight                     //
//      - bottomLeft                   //
//      - bottomRight                  //
//      - aboveLeft                    //
//      - aboveRight                   //
/////////////////////////////////////////
function showMenu(thisid, id, menuPos)
{
    var pos = new Array();
    pos = findPos(getID(thisid));
    
    switch (menuPos)
    {
        case 'topLeft':
        //pos[0]  // LEFT DOESNT CHANGE
        //pos[1]  // TOP DOESNT CHANGE
        break;
        case 'topRight':
        pos[0] += getID(thisid).offsetWidth;
        //pos[1] // TOP DOESNT CHANGE
        break;
        case 'bottomLeft':
        //pos[0]  // LEFT DOESNT CHANGE
        pos[1] += getID(thisid).offsetHeight;
        break;
        case 'bottomRight':
        pos[0] += getID(thisid).offsetWidth;
        pos[1] += getID(thisid).offsetHeight;
        break;
        case 'aboveLeft':
        //pos[0]  // LEFT DOESNT CHANGE
        pos[1] += getID(id).offsetHeight;
        break;        
        case 'aboveRight':
        pos[0] += getID(thisid).offsetWidth;
        pos[1] -= getID(id).offsetHeight;
        break;                
    }
    // make sure it doesn't go off the edge of the page
    pos[0] = checkPageRightEdge (id, pos[0]);
    pos[1] = checkPageTopEdge (id, pos[1]);
    //alert('top=' + pos[1] + ' left=' + pos[0]);
    getID(id).style.left = pos[0] + 'px';
    getID(id).style.top = pos[1] + 'px';
    getID(id).style.zIndex ='2000';
}

/////////////////////////////////////////
// hideFloatBox(id)                    //
// 2007-01-09 Andy Male                //
//                                     //
// Sets the position of an object      //
// to -5000,-5000 off the visible page //
// turns off pageOverlay()             //
//                                     //
// Depends on                          //
// getID(), pageOverlay()              //
//                                     //
// id = object to show                 //
/////////////////////////////////////////
function hideMenu(id)
{
    getID(id).style.left='-5000px';
    getID(id).style.top='-5000px';
    getID(id).style.zIndex ='-1';
}

// Call Back Error alert
function CallbackError (result,context)
{
        alert('Call back Error' + result);
}

/////////////////////////////////////////
// setExternalLinkNewWindow(state)     //
// 2007-01-12 Andy Male                //
//                                     //
// Sets the anchors in the page to     //
// open in new window or open in       //
// current window depending on bool    //
//                                     //
// state = bool                        //
/////////////////////////////////////////
function setExternalLinkNewWindow(state)
{
    var nodes = document.getElementsByTagName('A');
    {
        for (var x=0; x < nodes.length; x++)
        {
            if (nodes[x].rel == 'external')
            {
                if (state)
                {
                    nodes[x].onclick = function() {window.open(this.href); return false;};
                }
                else
                {
                    nodes[x].onclick = '';
                }
            }
        }
    }
}

/////////////////////////////////////////
// setValue(id, newVal)                //
// 2007-02-01 Andy Male                //
//                                     //
// Sets value on object such as INPUT  //
// type 'hidden'                       //
//                                     //
// Depends on                          //
// getID()                             //
//                                     //
// id = ID of object                   //
// newVal = value to set object        //
/////////////////////////////////////////
function setValue(id, newVal)
{
    if (id != '' && id != null)
    {
        if (getID(id))
        {
            getID(id).value = newVal;
        }
    }
}

/////////////////////////////////////////
// setInnerHTML(id, newVal)            //
// 2007-02-01 Andy Male                //
//                                     //
// Sets innerHTML on document element  //
//                                     //
// Depends on                          //
// getID()                             //
//                                     //
// id = ID of object                   //
// newVal = value to set object        //
/////////////////////////////////////////
function setInnerHTML(id, newVal)
{
    if (id != '' && id != null)
    {
        if (getID(id))
        {
            getID(id).innerHTML = newVal;
        }
    }
}

/////////////////////////////////////////
// objectBlink(id, onColour, offColour)//
// 2007-02-06 Andy Male                //
//                                     //
// JS/CSS blinks border                //
//                                     //
// Depends on                          //
// getID()                             //
// blinkBorderID GLOBAL VAR            //
//                                     //
// id = ID of object                   //
// onColour = positive colour          //
// offColour = neutral colour          //
// blinkBorderTimerID = 0 (0=not set)  //
// blinkSpeed = millisecs 5000 = 5 sec //
// state = bool  (on or off)           //
/////////////////////////////////////////
function objectBlink(id, onColour, offColour, blinkBorderTimerID, blinkSpeed, state)
{ 
    // changed the index of the array blinkBorderTimer
    // to be the ID of the object that is blinking so
    // we can find it elsewhere and store other data 
    // in a multidimensional array
    
    // store the original colours
    if (!blinkTimer[id])
    {
        blinkTimer[id] = new Object();
        blinkTimer[id]['borderColor'] = getID(id).style.borderColor;
        blinkTimer[id]['color'] = getID(id).style.color;
    }

    if (id != '' && id != null)
    {
        if (getID(id))
        {
            if (state)
            {
                getID(id).style.borderWidth = '1px';
                getID(id).style.borderColor = onColour;
                getID(id).style.color = onColour;
                blinkTimer[id]['timer'] = setTimeout("objectBlink('"+id+"', '"+onColour+"', '"+offColour+"', '"+blinkBorderTimerID+"','"+blinkSpeed+"', false)", blinkSpeed);
            }
            else
            {
                getID(id).style.borderWidth = '1px';
                getID(id).style.borderColor = offColour;
                getID(id).style.color = offColour;
                blinkTimer[id]['timer'] = setTimeout("objectBlink('"+id+"', '"+onColour+"', '"+offColour+"', '"+blinkBorderTimerID+"','"+blinkSpeed+"', true)", blinkSpeed);
            }
        }
    }
}

/////////////////////////////////////////
// objectBlinkOff(id)                  //
// 2007-02-08 Andy Male                //
//                                     //
// Depends on                          //
// getID()                             //
// blinkBorderID GLOBAL VAR            //
//                                     //
// id = ID of object                   //
/////////////////////////////////////////
function objectBlinkOff(id)
{
    if (blinkTimer[id])
    {
        clearTimeout(blinkTimer[id]['timer']);
        getID(id).style.borderColor = blinkTimer[id]['borderColor'];
        getID(id).style.color = blinkTimer[id]['color'];
        delete blinkTimer[id];
    }
}

/////////////////////////////////////////
// setTextAreaWidth(idToSet,idGetWidth)//
// 2007-02-21 Andy Male                //
//                                     //
// Depends on: getID()                 //
/////////////////////////////////////////
function setTextAreaWidth(idToSet,idGetWidth)
{
    var pixelWidthToSet_ = getID(idGetWidth).offsetWidth;
    var txtAreaWidth_ = getID(idToSet).offsetWidth;
    while (txtAreaWidth_ < pixelWidthToSet_)
    {
        getID(idToSet).cols++;
        txtAreaWidth_ = getID(idToSet).offsetWidth;
    }
}


/////////////////////////////////////////
// makeTableRow(tableName)             //
// 2006-04-05 Andy Male                //
//                                     //
// Add a new row in a table            //
//                                     //
// Depends on getID(object)            //
/////////////////////////////////////////
function makeTableRow(tableName)
{
    var tableID = getID(tableName);
    var newTR = tableID.insertRow(tableID.rows.length);
    return newTR;
}

///////////////////////////////////////////
// makeRowCells(newTR, cellsCount)       //
// 2006-04-05 Andy Male                  //
//                                       //
// Add a new cells to a row in a table   //
//                                       //
// Depends on getID(object)              //
///////////////////////////////////////////
function makeRowCells(newTR, cellsCount)
{
    for (var i=0; i < cellsCount; i++)
    {
        newTR.insertCell(-1);
    }
}

//////////////////////////////////////////////////////////////
// addRowData(newTR, dataHeader, dataSet)                   //
// 2006-04-05 Andy Male                                     //
//                                                          //
// Add data the a new row in a table using the dataHeader   //
// the index for dataSet                                    //
//                                                          //
// Depends on getID(object)                                 //
//////////////////////////////////////////////////////////////
function addRowData(tableName, rowDataArray)
{
    var tableID = getID(tableName);
    var indexArray = tableID.rows[0].cells;  // the first row
    var cellsCount = indexArray.length;
    var newTR = makeTableRow(tableName);
    makeRowCells(newTR, cellsCount);
    for (var cellID=0; cellID < cellsCount; cellID++)
    {
        newTR.cells[cellID].innerHTML = rowDataArray[cellID];
    }
}

//////////////////////////////////////////////////////////////
// deleteRows(tableName, startInt, endInt)                  //
// 2007-02-22 Andy Male                                     //
//                                                          //
// Delete rows from startInt to endInt if they are set      //
// otherwise delete all the rows                            //
//                                                          //
// Depends on getID(object)                                 //
//////////////////////////////////////////////////////////////
function deleteRows(tableName)
{
    var tableID_ = getID(tableName);
    var args_ = deleteRows.arguments;
    var startInt_;
    var endInt_ = 0;
    var rowsToDelete_;
    if (args_.length >= 2)
    {
        isNaN(args_[2]) ? endInt = 0 : endInt_ = parseInt(args_[2]);
    }
    if (args_.length >= 1)
    {
        isNaN(args_[1]) ? startInt_ = 0 : startInt_ = parseInt(args_[1]);
    }
    endInt_ == 0 ? endInt_ = tableID_.rows.length : endInt_;
    rowsToDelete_ = startInt_;
    //alert('startInt_=' + startInt_ + " endInt_=" + endInt_ + ' rowsToDelete_=' + rowsToDelete_);
    for (rowsToDelete_; rowsToDelete_ < endInt_; rowsToDelete_++)
    {
        tableID_.deleteRow(startInt_);
    }
}

////////////////////////////////////////////
// getTableData(tableName)                //
// 2007-02-27 Andy Male                   //
//                                        //
// Depends on getID(object)               //
////////////////////////////////////////////
function getTableData(tableName, startRow)
{
    var tblObj = getID(tableName);
    var rows_ = tblObj.rows.length;
    var cols_ = tblObj.rows[0].cells.length;
    var dataArray_ = new Array();
    // restart numbering for new array
    var arrayIndex_ = 0;
    var x = startRow;
    for (x; x < (rows_-1); x++)
    {       
        dataArray_[arrayIndex_] = new Array();
        for (var y=0; y < (cols_-1); y++)
        {
            if ((tblObj.rows[x].cells[y]) != undefined)
            {
                dataArray_[arrayIndex_][y] = tblObj.rows[x].cells[y].innerHTML;
            }
            else
            {
                dataArray_[arrayIndex_][y] = '';
            }
        }
        arrayIndex_++;
    }
    return dataArray_;
}


////////////////////////////////////////////
// maxLength(objectID, length)            //
// 2007-03-08 Andy Male                   //
//                                        //
// Depends on getID(objectID)             //
// id = name of object to check           //
// length = the max length allowed        //
////////////////////////////////////////////
function maxLength(id, length, alertState, messageData)
{
    var obj = getID(id);
    if (obj.value != null)
    {
        var txtLen = obj.value.length;
        //alert('value=' + txtLen + ' length=' + length);
        if (txtLen > length)
        {
            if (alertState) {ShowMessageBoxDialogue('Alert', messageData, 'Ok', 'Ok');}      
//            alert(messageData);}
            obj.value = obj.value.substr(0, length);
        }
    }
}


////////////////////////////////////////////
// tagLength(objectID, length)            //
// 2007-03-08 Andy Male                   //
//                                        //
// Depends on getID(objectID)             //
// id = name of object to check           //
// length = the max length allowed        //
////////////////////////////////////////////
function tagLength(id, length, messageData)
{
    var obj = getID(id);
    if (obj.value != null)
    {
        var strTags_ = obj.value;
        var strErrTags_ = '<ul>\r\n';
        var strNewTags_ = '';
        var strShortTag_; 
        var tagsArray_ = strTags_.split(',');
        var errState_ = false;
        for (var i=0; i < tagsArray_.length; i++)
        {
            tagsArray_[i] = trim(tagsArray_[i]);
            if (tagsArray_[i] != '')
            {
                if (tagsArray_[i].length > length)
                {
                    strShortTag_ = tagsArray_[i].substr(0, 20);
                    strErrTags_ += "<li>\"" + tagsArray_[i] + "\" is \"" + strShortTag_ + "\"</li>\r\n";
                    errState_ = true;
                    strNewTags_ += strShortTag_ + ', ';
                }
                else
                {
                    strNewTags_ += tagsArray_[i] + ', ';
                }
            }
        }
        strErrTags_ += '</ul>\r\n';
        if (errState_)
        {
            obj.value = strNewTags_;
            ShowMessageBoxDialogue('Alert', messageData + "<br/><br/>\r\n" + strErrTags_, 'Ok', 'Ok');
        }
    }
}

function pause(ms)
{
    var date = new Date();
    var curDate = null;
    do 
    {
        curDate = new Date();
    }
    while(curDate-date < ms);
}

function setClass(cName)
{
    this.className = cName;
}

function unsetClass()
{
    this.className = '';
}

function unescapeChars(textToUnescape)
{
    return unescape(textToUnescape).replace(/\|/g,',').replace(/\+/g,' ');
}

function escapeCharsLeaveComma(texttoescape)
{
    return escape(texttoescape);
}


function escapeChars(texttoescape)
{

    return escape(texttoescape.replace(/,/g,'|'));
}

function GoTerms (resourceLocation)
{
    var resourceView_ = new popWindow();
    resourceView_.setWindowName('Terms and Conditions');
    resourceView_.setURL(resourceLocation);
    resourceView_.setTop(100);
    resourceView_.setLeft(100);
    resourceView_.setHeight(parseInt(document.documentElement.clientHeight)-200);    
    resourceView_.setWidth(800);
    resourceView_.setScrollbars('yes');
    resourceView_.setResizable('yes');
    resourceView_.setMenubar('yes');
    resourceView_.setLocation('yes');
    resourceView_.setToolbar('yes');
    resourceView_.setStatus('yes');
    resourceView_.newWindow();
} 

/// IMPORTANT: THIS MUST BE AT THE END OF FILE !
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();