﻿// JScript File

/////////////////////////////////////////
// getID(id)                           //
// Depends on nothing                  //
// id = the id of the document object  //
/////////////////////////////////////////
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(obj)                        //
// Used to find the real obj position  //
// Depends on nothing                  //
// obj = the object NOT the ID         //
/////////////////////////////////////////
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()                       //
// RegEx to trim whitespace     //
//////////////////////////////////
//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));
}

/////////////////////////////////////////
// hide(id)                            //
// Depends on getID()                  //
// id = the id of the document object  //
/////////////////////////////////////////
function hide(id)
{
    var obj = getID(id);
    obj.style.position = 'absolute';
    obj.style.left='-5000px';
    obj.style.top='-5000px';
}

/////////////////////////////////////////
// show(id)                            //
// Depends on getID()                  //
// id = the id of the document object  //
/////////////////////////////////////////
function show(id)
{
    var obj = getID(id);
    obj.style.position = 'relative';
    obj.style.left='0px';
    obj.style.top='0px';
}

/////////////////////////////////////////
// showNhide(show_id, hide_id)         //
// Combines show() and hide()          //
// Depends on:                         //
//   getID(), show(),hide()            //
// show_id = id doc object to show     //
// show_id = id doc object to hide     //
/////////////////////////////////////////
function showNhide(show_id, hide_id)
{
    showIt(show_id);
    hideIt(hide_id);
}

//////////////////////////////////////////////
// fixTopEdge(id, topPos)                   //
// Checks the new object will fit on screen //
// Depends on:                              //
//    getID()                               //
// id = id of document object to show       //
// topPos = the proposed top position       //
//////////////////////////////////////////////
function fixTopEdge (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;
}

///////////////////////////////////////////
// fixBottomEdge(id)                     //
// Fixes the new object position without //
// extending past the footer             //
// Depends on:                           //
//    getID()                            //
// id = id of document object to show    //
///////////////////////////////////////////
function fixBottomEdge(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';
    }
}

///////////////////////////////////////////
// fixRightEdge(id, leftPos)             //
// Fixes the new object position without //
// extending past the right edge of page //
// Depends on:                           //
//     getID()                           //
// id = id document object to show       //
///////////////////////////////////////////
function fixRightEdge (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;
}

//////////////////////////////////////////
// showHere(parentID, id, menuPos)      //
// Depends on:                          //
//   getID(), fixRightEdge()            //
// parentID = id doc obj to position by //
// showID = object to show              //
// menuPos = position to display        //   
// the object which can be one of:      //
//      - topLeft                       //
//      - topRight                      //
//      - bottomLeft                    //
//      - bottomRight                   //
//      - aboveLeft                     //
//      - aboveRight                    //
//////////////////////////////////////////
function showMenu(parentID, showID, menuPos)
{
    var pos = new Array();
    pos = findPos(getID(parentID));
    var pObj = getID(parentID);
    var sObj = getID(showID);
    
    switch (menuPos)
    {
        case 'topLeft':
        //pos[0]  // LEFT DOESNT CHANGE
        //pos[1]  // TOP DOESNT CHANGE
        break;
        case 'topRight':
        pos[0] += pObj.offsetWidth;
        //pos[1] // TOP DOESNT CHANGE
        break;
        case 'bottomLeft':
        //pos[0]  // LEFT DOESNT CHANGE
        pos[1] += pObj.offsetHeight;
        break;
        case 'bottomRight':
        pos[0] += pObj.offsetWidth;
        pos[1] += pObj.offsetHeight;
        break;
        case 'aboveLeft':
        //pos[0]  // LEFT DOESNT CHANGE
        pos[1] += sObj.offsetHeight;
        break;        
        case 'aboveRight':
        pos[0] += pObj.offsetWidth;
        pos[1] -= sObj.offsetHeight;
        break;                
    }
    pos[0] = fixRightEdge (showID, pos[0]);
    pos[1] = fixTopEdge (showID, pos[1]);
    sObj.style.left = pos[0] + 'px';
    sObj.style.top = pos[1] + 'px';
    sObj.style.zIndex ='2000';
}

//////////////////////////////////////////////
// Call Back Error alert                    //
// Generic function used to display ASP.Net //
// errors                                   //
//////////////////////////////////////////////
function CallbackError (result,context)
{
        alert('Call back Error' + result);
}

/////////////////////////////////////////
// setExternalLinkNewWindow(state)     //
// 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 = '';
                }
            }
        }
    }
}

/////////////////////////////////////////
// setTextAreaWidth(idToSet,idGetWidth)//
// 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;
    }
}

////////////////////////////////////////////
// maxLength(objectID, length)            //
// 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;
        if (txtLen > length)
        {
            if (alertState) {ShowMessageBoxDialogue('Alert', messageData, 'Ok', 'Ok');}      
            obj.value = obj.value.substr(0, length);
        }
    }
}

/////////////////////////////////////////////////
// getBaseID(ID)                               //
// for ASP.Net gets the string after the last  //
// underscore                                  //
// Depends on getID(objectID)                  //
// ID = id of doc object selected              //
/////////////////////////////////////////////////
function getBaseID(id)
{
    var strArray = id.split('_');
    var item = strArray.length - 1;
    var baseID = strArray[item];
    //alert('id=' + id + ' arrayLength=' + strArray.length + ' baseID='+ baseID);
    if (baseID.length == 0) baseID = id;
    return baseID;
}


/////////////////////////////////////////////////
// showActiveLIContent(selectedID, postFix)    //
// show child/realted LI, hide everything else //
// Depends on:                                 //
//      getID(objectID), show(id), hide(id)    //
// selectedID = id of doc object selected      //
// postFix = post name for the content ie      //
// div = 'shop' then content = 'shop'+postFix  //
/////////////////////////////////////////////////
function showActiveLIContent(selectedID, postFix, contentULname)
{
    var sObj = getID(selectedID);
    var pObj = getID(sObj.parentNode.id);
    var targetPrefix = getLIprefix(contentULname);
    var lstItem;
    
    if ((sObj!=null) && (pObj!=null))
    {
        for (var n=0; n < pObj.childNodes.length; n++)
        {
            if (pObj.childNodes[n].nodeName == 'LI')
            {
                
                if (pObj.childNodes[n].id == selectedID)
                {
                    lstItem = getID(targetPrefix + getBaseID(selectedID) + postFix);
                    if (lstItem!=null)
                    {
                        show(lstItem.id);
                    }
                    else
                    {
                        return true;
                    }
                }
                else
                {
                    lstItem = getID(targetPrefix + getBaseID(pObj.childNodes[n].id) + postFix);
                    if (lstItem!=null)
                    {
                        hide(lstItem.id);
                    }
                    else
                    {
                        return true;
                    }                    
                }
                //if (lstItem!=null) alert('Item: ' + lstItem.id);
            }
        }
        return false;
    }
    //else
    //{
    //   alert('sObj:' + sObj + ' pObj:' + pObj);
    //   return true;    
    //}
}

// finds a UL based on the NON ASP prefixed name
function findUL(ULname)
{
    var arrayULs = document.getElementsByTagName('UL');
    var tmpMsg = "";
    for (var i=0; i<arrayULs.length; i++)
    {
        tmpMsg += arrayULs[i].id + '\r\n';
        if (getBaseID(arrayULs[i].id) == ULname)
        {
            return arrayULs[i].id;
        }
    }
    return "";
}


// find the LI ASP prefix name from a parent UL
function getLIprefix(ULname)
{
    var targetUL = findUL(ULname);
    if (targetUL != null)
    {
        var objUL = getID(targetUL);
        for (var i=0; i < objUL.childNodes.length; i++)
        {
            if (objUL.childNodes[i].nodeName == 'LI')
            {
                var strVal = objUL.childNodes[i].id;
                strVal = strVal.replace(getBaseID(strVal), '');
                if (strVal != '')
                {
                    return strVal;
                }
            }
        }
    }
    return '';
}

function switchCSSClass(strInitial, strToFind, strToReplaceWith)
{
    strToReplaceWithregex = new RegExp("\\b" + strToReplaceWith + "\\b");
    strToFindregex = new RegExp("\\b" + strToFind + "\\b");
    if (!strInitial.match(strToReplaceWithregex))
    {
         if (strInitial.match(strToFindregex))
         {
             strInitial = strInitial.replace(strToFindregex,strToReplaceWith);
         }
         else
         {
             if (strInitial.length>0)
             {
               strInitial += ' ' + strToReplaceWith;
             }
             else
             {
               strInitial = strToReplaceWith;
             }
          }
    }
    return strInitial;
}