// ***************************************************************************
// * Common.js - Only add WORKING routines to this file. Don't debug from it
// ***************************************************************************
// * In theory, all these routines work on IE 6, IE 7, FF 1.5 and 2.x
// * Opera, Konquor and most likely Safari.
// * If you have something useful to add, ensure it works on all these
// * browsers! Kplztnx.
// *
// *     void       addEvent(source, eventType, callBack)
// *     httpObj    getXmlHttp()
// *     elmObj     getFirstChild(elmObj)
// *     elmObj     getPreviousSibling(elmObj)
// *     elmObj     getNextSibling(elmObj)
// *     elmObj     getAspNetForm()
// *     void       simulateImageButtonClick(elmObj)
// *     void       simulateSubmitButtonClick(elmObj)
// *     int        scrollY()
// *     int        scrollX()
// *
// ***************************************************************************

var aspNetForm;

// Adds an eventhandler to an event, whithout overwriting existing handlers
addEvent = function(source, eventType, callBack)
{
    if (window.addEventListener)
        source.addEventListener(eventType, callBack, false);
    else if (window.attachEvent)
        source.attachEvent('on' + eventType, callBack);
}

// Returns an instance to the browsers XmlHttp implementation.
// For a single connection only! Don't attempt to recycle, or
// IE will become ziek, zwak en misselijk.
getXmlHttp = function()
{
    var result;

    try
    {
        result = new XMLHttpRequest();
    }
    catch (e)
    {
        try
        {
            result = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (e)
        {
            try
            {
                result = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (e)
            {
                return false;
            }
        }
    }
    return result;
}

// Browser independent getFirstChild
getFirstChild = function(elm)
{
    var result = elm.firstChild;
    
    if (result == null)
        return null;
    
    if (result.nodeName == '#text')
        return result.nextSibling;
        
    return result;
}

// Browser independent getPreviousSibling
getPreviousSibling = function(elm)
{
    var result = elm.previousSibling;
    
    if (result == null)
        return null;
        
    if (result.nodeName.toLowerCase() == '#text')
        return result.previousSibling;
    
    return result;
}

// Browser independent getNextSibling
getNextSibling = function(elm)
{
    var result = elm.nextSibling;
    
    if (result == null)
        return null;
    
    if (result.nodeName == '#text')
        return result.nextSibling;
        
    return result;
}

// Set the global aspNetForm and returns the value as well
getAspNetForm = function()
{
    aspNetForm = document.forms[0];
    return aspNetForm;
}

simulateImageButtonClick = function(imgButtonElm)
{
    var hidInputX = document.createElement('input');
    var hidInputY = document.createElement('input');
    
    hidInputX.name = imgButtonElm.name + '.x'; hidInputX.value = '3';
    hidInputY.name = imgButtonElm.name + '.y'; hidInputY.value = '3';
    
    hidInputX.type = 'hidden';
    hidInputY.type = 'hidden';
    
    imgButtonElm.name += '.old';
    
    if (getAspNetForm === undefined)
        getAspNetForm();
    
    aspNetForm.appendChild(hidInputX);
    aspNetForm.appendChild(hidInputY);
    
    aspNetForm.submit();
}

simulateSubmitButtonClick = function(submitButtonElm)
{
    var hidInput = document.createElement('input');
    
    hidInput.name = submitButtonElm.name; hidInput.value = '3';
    hidInput.type = 'hidden';
        
    submitButtonElm.name += '.old';
    
    if (getAspNetForm === undefined)
        getAspNetForm();

    aspNetForm.appendChild(hidInput);
    aspNetForm.submit();
}

getAbsolutePosition = function(elm)
{
  var resultX = 0;
  var resultY = 0;
  
  do
  {
    resultX += elm.offsetLeft;
    resultY += elm.offsetTop;
  }
  while (elm = elm.offsetParent);
  
  return { x : resultX, y : resultY };
}

// Browser independent scroll vertical offset in pixels
scrollY = function()
{
    try
    {
        if (typeof(window.pageYOffset) == 'undefined')
            throw sigh;
        else
            var o = window.pageYOffset;
    }
    catch(sigh)
    {
        var o = document.body.scrollTop | document.documentElement.scrollTop;
    }
    
    return o;
}

// Browser independent scroll horizontal offset in pixels
scrollX = function()
{
    try
    {
        if (typeof(window.pageXOffset) == 'undefined')
            throw sigh;
        else
            var o = window.pageXOffset;
    }
    catch(sigh)
    {
        var o = document.body.scrollLeft | document.documentElement.scrollLeft;
    }
    
    return o;
}

// Trims a string from spaces, tabs and enter keys
trimString = function(val)
{
    return val.replace(/^[\s\t\r\n]+|[\s\t\r\n]+$/, '');
}

// Good luck :)
function validDate(theDate)
{
    return /^((((0?[1-9]|[12]\d|3[01])[\.\-\/](0?[13578]|1[02])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|[12]\d|30)[\.\-\/](0?[13456789]|1[012])[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|((0?[1-9]|1\d|2[0-8])[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?\d{2}))|(29[\.\-\/]0?2[\.\-\/]((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00)))|(((0[1-9]|[12]\d|3[01])(0[13578]|1[02])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|[12]\d|30)(0[13456789]|1[012])((1[6-9]|[2-9]\d)?\d{2}))|((0[1-9]|1\d|2[0-8])02((1[6-9]|[2-9]\d)?\d{2}))|(2902((1[6-9]|[2-9]\d)?(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)|00))))$/.test(theDate);
}

