/**
* File: Common.js
* Defines some common javascript functions
* for use in javascript applications.
*
* @author Ian Yates
*
*/

/**
* Programatically attaches an event handler to a Node
*
* @param string eventName The name of the event that the handler should respond to
* @param Node eventNode The DOM Node to attach the handler to
* @param function eventHandler The event handling function
* @return void
*
*/
function attachEventHandler(eventName, eventNode, eventHandler)
{
    if(eventNode.attachEvent)
    {
        eventNode.attachEvent("on" + eventName, eventHandler);
    }
    else if(eventNode.addEventListener)
    {
        eventNode.addEventListener(eventName, eventHandler, false);
    }
    else
    {
        eval("eventNode.on" + eventName + " = eventHandler;");
    }
}

/**
* Returns the DOM Node that was the source of a given event
*
* @param Event e The event to get the source element of
* @return Node
*
*/
function getEventSource(e)
{
    var eventSrc = null;
    if(e.target)
    {
        eventSrc = e.target;
    }
    else if(e.srcElement)
    {
        eventSrc = e.srcElement;
    }
    else if(e.currentTarget)
    {
        eventSrc = e.currentTarget;
    }
    return eventSrc;
}

function appendSelectOption(selectNode, optionNode)
{
    if(document.all)
    {
        selectNode.add(optionNode);
    }
    else
    {
        selectNode.add(optionNode, null);
    }
}

function insertSelectOption(selectNode, optionNode, index)
{
    if(document.all)
    {
        selectNode.add(optionNode, index);
    }
    else
    {
        selectNode.add(optionNode, selectNode.options[index]);
    }
}

function getIFrameDocument(iframeNode)
{
    var doc = null;
    if(iframeNode.contentWindow.document.body)
    {
        doc = iframeNode.contentWindow.document.body;
    }
    else if(iframeNode.contentDocument.body)
    {
        doc = iframeNode.contentWindow.document.body;
    }
    return doc;
}

function inArray(arr, val)
{
    var inArr = false;
    
    for(var i = 0; i < arr.length; i++)
    {
        if(val == arr[i])
        {
            inArr = true;
            break;
        }
    }
    
    return inArr;
}

function emptySelectList(selectNode, offset)
{
    if(!offset)
    {
        offset = 0;
    }
    
    while(selectNode.options.length > offset)
    {
        selectNode.remove(selectNode.options.length - 1);
    }
}

function cancelEvent(e)
{
    if(e.preventDefault)
    {
        e.preventDefault();
    }
    else if(typeof(e.cancelBubble) != "undefined")
    {
        e.cancelBubble = true;
    }
    else if(e.stopPropagation)
    {
        e.stopPropagation();
    }
}

function searchErrorMsg(querystring)
{
    //querystring = window.location.search.substring(1);

    if(querystring.search("e=1") > -1 || querystring.search("&e=1") > -1)
    {
        //document.write('<p class="redbox">There is an error with your search.</p>');
    }

    if(querystring.search("e=2") > -1 || querystring.search("&e=2") > -1)
    {
        document.write('<div class="redbox clearfix">Return date must be after depart.</div>');
    }

    if(querystring.search("e=3") > -1 || querystring.search("&e=3") > -1)
    {
        document.write('<div class="redbox clearfix">Depart and Return can\'t be the same.</div>');
    }

    if(querystring.search("e=4") > -1 || querystring.search("&e=4") > -1)
    {
        document.write('<div class="redbox clearfix">Child ages are missing.</div>');
    }

    if(querystring.search("e=5") > -1 || querystring.search("&e=5") > -1)
    {
        document.write('<div class="redbox clearfix">Please call for bookings within the next 48 hours.</div>');
    }

    if(querystring.search("e=6") > -1 || querystring.search("&e=6") > -1)
    {
        document.write('<div class="redbox clearfix">You must select a destination.</div>');
    }

    if(querystring.search("e=7") > -1 || querystring.search("&e=7") > -1)
    {
        document.write('<div class="redbox clearfix">You must select a departure.</div>');
    }

    if(querystring.search("e=8") > -1 || querystring.search("&e=8") > -1)
    {
        document.write('<div class="redbox clearfix">No departures are allowed before today.</div>');
    }

    if(querystring.search("e=9") > -1 || querystring.search("&e=9") > -1)
    {
        document.write('<div class="redbox clearfix">The session you tried to use has now expired and cannot be continued. Go back and try again.</div>');
    }

    if(querystring.search("e=10") > -1 || querystring.search("&e=10") > -1)
    {
        document.write('<div class="redbox clearfix">The date that you have entered is invalid.</div>');
    }

    if(querystring.search("e=13") > -1 || querystring.search("&e=13") > -1)
    {
        document.write('<div class="redbox clearfix">The date range specified is too large, maximum is 31 days.</div>');
    }
}