﻿//    for logging add:
//    <textarea rows="8" cols="70" id="log"></textarea>
//    <input type="button" onclick="ClearLog()" class="ButtonSmallGrey" value="clear log" />
//    to the page.
var Logging = false;
var logger;
function ClearLog()
{
   if (logger != null)
      document.getElementById('log').value = '';
}
function Log(message)
{
    if (logger != null)
    {
        if (Logging)
            logger.value += message + '\n';
    }
    else
        logger = document.getElementById('log'); 
}




// create new item properties
NewItemCounter = 0;





function ParseNumber(value)
{
    var n = parseInt(value);
    return n == null || isNaN(n) ? 0 : n;
}



function ArrayToString(arrArray, delimiter)
{
    // if its null just return an empty string:
    if (arrArray == null)
        return '';

    var str = '';
    for (i=0; i<arrArray.length; i++)
    {
        if (i > 0)
        str += delimiter;
        str += arrArray[i]
    }
    return str;
}



function AddToArray(arrArray, intItem1, intItem2, delimiter)
{
    // probably the arrArray has the form:
    //
    //    2-1
    //    3-2

    if (arrArray == null)
    {
        // we need to create a new array and add our details to it
        arrArray = new Array(1);
        arrArray[0] = intItem1 + delimiter + intItem2;
    }
    else
    {
        // we are appending to an existing array and may have to overwrite an entry
        var indexToUse = arrArray.length; // default to the end of the array
        for (i=0; i<arrArray.length; i++)
        {
            // if intItem1 is the same as the start index of an item in the array it means we need to overwrite
            if (arrArray[i].charAt(0) == intItem1) 
            indexToUse = i;
        }
        // indexToUse will tell us to use the end of the array or overwrite existing entry
        arrArray[indexToUse] = intItem1 + delimiter + intItem2; 
    }		      
    return arrArray;
}


function AppendToArray(arrArray, intItem1, intItem2, delimiter)
{
    if (arrArray == null)
    {
        arrArray = new Array(1);
        arrArray[0] = intItem1 + delimiter + intItem2;
    }
    else
    {
        arrArray.push(intItem1 + delimiter + intItem2);
    }
    
    return arrArray;
}
    
    
    
    
    
function RemoveArrayItemStartingWithChar(arrArray, charStartsWith)
{
    // find the index of the item we are discarding (if any):
    var indexToUse = -1; // set to -1 so we know if its changed
    for (i=0; i<arrArray.length; i++)
    {
        // if intItem1 is the same as the start index of an item in the array it means we need to overwrite
        if (arrArray[i].charAt(0) == charStartsWith) 
        indexToUse = i;
    }

    // if index is -1 it hasnt been found so dont do anything:
    if (indexToUse != -1)
    {
        // we want to remove a specific item from the array:
        var arrNewArray = new Array();
        var count = 0;
        for (i=0; i<arrArray.length; i++)
        {
            if (i == indexToUse)
            continue; // dont add this item to the new array
            
            arrNewArray[count] = arrArray[i];
            count += 1;
        }

        return arrNewArray;
    }

    return arrArray; // return the old array as the new one was not returned above
}

    
    
    
    
    
    
    
function GetElementHeight(divElement)
{
    // for the benefit of FireFox which has probably forgotten whats going on...
    divElement = document.getElementById(divElement.id);

    if (divElement.naturalHeight && divElement.naturalHeight != 0)
    {
        //Log('GetElementHeight->NaturalHeight');
        return divElement.naturalHeight;
    }
    
    if (divElement.offsetHeight && divElement.offsetHeight != 0)
    {
        //Log('GetElementHeight->OffsetHeight');
        return divElement.offsetHeight;
    }
        
    if (divElement.height && divElement.height != 0)
    {
        //Log('GetElementHeight->Height');
        return divElement.height;
    }
        
    if (divElement.clientHeight && divElement.clientHeight != 0)
    {
        //Log('GetElementHeight->ClientHeight');
        return divElement.clientHeight;
    }
}
    
function GetElementWidth(divElement)
{
    // for the benefit of FireFox which has probably forgotten whats going on...
    divElement = document.getElementById(divElement.id);

    if (divElement.naturalWidth && divElement.naturalWidth != 0)
    {
        //Log('GetElementWidth->NaturalWidth');
        return divElement.naturalWidth;
    }
    
    if (divElement.offsetWidth && divElement.offsetWidth != 0)
    {
        //Log('GetElementWidth->OffsetWidth');
        return divElement.offsetWidth;
    }
        
    if (divElement.width && divElement.width != 0)
    {
        //Log('GetElementWidth->Width');
        return divElement.width;
    }
        
    if (divElement.clientWidth && divElement.clientWidth != 0)
    {
        //Log('GetElementWidth->ClientWidth');
        return divElement.clientWidth;
    }
}
    
    



	
function AddWindowOnLoadEvent(func) 
{   
    var oldonload = window.onload;   
    if (typeof window.onload != 'function') 
    {   
        window.onload = func;   
    } 
    else 
    {   
        window.onload = function() 
        {   
            if (oldonload) 
            {   
                oldonload();   
            }   
            func();   
        }   
    }   
}
		
		

function AddFormOnSubmitEvent(func) 
{   
    var oldSubmit = document.forms[0].onsubmit;   
    if (typeof document.forms[0].onsubmit != 'function') 
    {   
        document.forms[0].onsubmit = func;   
    } 
    else 
    {   
        document.forms[0].onsubmit = function() 
        {   
            if (oldSubmit) 
            {   
                oldSubmit();   
            }   
            func();   
        }   
    }   
}



function OpenWindow(url)
{
   var newWindow = window.open(url);
}


function StringRemove(string, searchString)
{
   if (string.indexOf(searchString) != -1)
   {
      var index = string.indexOf(searchString);
      var length = searchString.length;
      
      var strStart = string.substr(0, index);
      var strEnd = string.substr(index + length);
      
      string = strStart + strEnd;
   }
   return string;
}


function swapStyle(item, newClassName)
{
    item.className = newClassName;
}






function getY( oElement )
{
    var iReturnValue = 0;
    while( oElement != null ) 
    {
        iReturnValue += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function getX( oElement )
{
    var iReturnValue = 0;
    while( oElement != null ) 
    {
        iReturnValue += oElement.offsetLeft;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}
