// Busts the current out of a parent frameset if present
function FrameBust()
{
    if (top.location != document.location)
    {
        top.location.href = document.location.href;
    }
}

// Global event handler for the body.onLoad event
function global_body_onLoad(sender)
{
    //alert("global_body_onLoad() called");
    FrameBust();
    if (this.SetupMap)
    {
        //alert("global_body_onLoad() setting up Google Maps");
        SetupMap(false); // Setup Google Maps
    }
    else
    {
        //alert("global_body_onLoad() Google Maps setup method not found!");
    }
}

function global_body_onUnLoad(sender)
{
    if (this.GUnload)
    {
        GUnload(); // Unload Google Maps
    }
}

function SetCookie(name, value, expires)
{
    document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString());
}

function GetCookie (name) { 
    var dc = document.cookie; 
    var cname = name + "="; 

    if (dc.length > 0)
    { 
        begin = dc.indexOf(cname); 
        if (begin != -1)
        { 
            begin += cname.length; 
            end = dc.indexOf("", begin);
            if (end == -1) end = dc.length;
            return unescape(dc.substring(begin, end));
        } 
    }
    return null; 
}

function blank()
{
    var result = '';
}

function CalendarExtender_DateSelectionChanged(oCalendar)
{
    oCalendar.hide();
    oCalendar.get_element().blur();    
}

function CustomDateValidator_ClientValidate(source, arguments)
{
    // dd-MMM-yyyy or dd-MMM-yy
    
    //alert('CustomDateValidator_ClientValidate() entered');
    var isValid = false;
    
    var dateString = arguments.Value;
    var dayS = dateString.substr(0, 2);
    var day = parseInt(dateString.substr(0, 2), 10);
    var month = dateString.substr(3, 3);
    var year = parseInt(dateString.substr(7), 10);
    
    // Change date to 4 digits if 2, use 50 as threshold year
    //alert('CustomDateValidator_ClientValidate() year is ' + year);
    if (year < 100)
    {
        //alert('CustomDateValidator_ClientValidate() year is < 100');
        year = (year > 50 ? year += 1900 : year += 2000);
        dateString = day + ' ' + month + ' ' + year;
        //alert('CustomDateValidator_ClientValidate() dateString changed to ' + dateString);
    }
    
    // Remove hypens
    var re = /-/g; // RegEx pattern to match the string '-'
    dateString = dateString.replace(re, ' ');
    
    // Attempt to parse date
    var i = Date.parse(dateString);
    isValid = !isNaN(i);
    if (!isValid)
    {
        //alert('CustomDateValidator_ClientValidate() failed Date.parse: ' + dateString);
        arguments.IsValid = isValid;
        return;
    }
    
    // Check for leap-year (javascript Date.parse() doesn't check for this)
    // Check for valid last day of month (javascript Date.parse() doesn't check for this)    
    isValid = IsValidDayOfMonth(day, month, year);
    
    arguments.IsValid = isValid;
    //alert('CustomDateValidator_ClientValidate() complete');
}

function IsValidDayOfMonth(day, month, year)
{
    var result = false;
    var m = month.toLowerCase();
    
    switch (day)
    {
        case 29:
            result = (month.toLowerCase() == 'feb' ? IsLeapYear(year) : true);
            break;
        
        case 30:
            result = (month.toLowerCase() != 'feb');
            break;
        
        case 31:
            result = (m == 'jan' || m == 'mar' || m == 'may' || m == 'jul' || m == 'aug' || m == 'oct' || m == 'dec');
            break;
            
        default:
            result = (day >= 1 && day <= 28);
    }
    
    return result;
}

function IsLeapYear(year)
{    
	if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
		return true;
	return false;
}

function SwitchArrow(direction)
{
    var arrow = document.getElementById('imgArrow');
    if(direction == "Left")
    {
        arrow.className='imgArrowLeft';
    }
    else
    {
        arrow.className='imgArrowRight';
    }
}

function FindAdjacentElement(start, tagName, id)
{
    //alert("FindAdjacentElement() start.parentNode.tagName = " + start.parentNode.tagName);

    var result;
    var els = start.parentNode.childNodes;
    var x;
    var i;
    for (i = 0;i < els.length;i++)
    {
        x = els[i];
        if (x.id && x.id.indexOf(id) >= 0 && x.tagName == tagName)
        {
            //alert("FindAdjacentElement() x.id = " + x.id);
            result = x;
            break;
        }
    }
    return result;
}

function FindFirstElementWithClassName(direction, start, tagName, className)
{
    var result;
    var found = false;
    var lastRoot = start;
    
    if (direction == "up")
    {
        while (!found)
        {
            if (lastRoot.parentNode.className &&
                lastRoot.parentNode.className.indexOf(className) >= 0 &&
                lastRoot.parentNode.tagName.toLowerCase() == tagName.toLowerCase())
            {
                found = true;
                result = lastRoot.parentNode;
            }
            lastRoot = lastRoot.parentNode;
        }
    }
    else if (direction == "down")
    {
        var els = start.getElementsByTagName(tagName);
        var x;
        var i;
        for (i = 0;i < els.length;i++)
        {
            x = els[i];
            if (x.className && x.className.indexOf(id) >= 0)
            {
                result = x;
                break;
            }
            if (x.hasChildNodes())
            {
                // Recurse
                result = FindFirstElementWithClassName(direction, x, tagName, className);
            }
            if (result != null)
            {
                break;
            }
        }
    }
    
    return result;
}

function FindElement(direction, start, tagName, id)
{
    var result;
    var found = false;
    var lastRoot = start;
    
    if (direction == "up")
    {
        while (!found)
        {
            if (lastRoot.parentNode.id && lastRoot.parentNode.id.indexOf(id) >= 0 && lastRoot.parentNode.tagName.toLowerCase() == tagName.toLowerCase())
            {
                found = true;
                result = lastRoot.parentNode;
            }
            lastRoot = lastRoot.parentNode;
        }
    }
    else if (direction == "down")
    {
        var els = start.getElementsByTagName(tagName);
        var x;
        var i;
        for (i = 0;i < els.length;i++)
        {
            x = els[i];
            if (x.id && x.id.indexOf(id) >= 0)
            {
                result = x;
                break;
            }
            if (x.hasChildNodes())
            {
                // Recurse
                result = FindElement(direction, x, tagName, id);
            }
            if (result != null)
            {
                break;
            }
        }
    }
    
    return result;
}
    
function collapsiblePanel_onExpandComplete(sender, args)
{
    //alert(sender.get_id() + ' expanded');
    SaveCollapsePanelState(sender.get_id(), 'expanded');
}

function collapsiblePanel_onCollapseComplete(sender, args)
{
    //alert(sender.get_id() + ' collapsed');
    SaveCollapsePanelState(sender.get_id(), 'collapsed');
}

function SaveCollapsePanelState(name, state)
{
    var cookieExpires = new Date();
    cookieExpires.setTime(cookieExpires.getTime() + (1000 * 60 * 60 * 24 * 365 * 5)); //set it 5 years ahead 
    SetCookie(name + '_collapseState', state, cookieExpires);
}

function WireUpCollapsePanelEventHandlers(value)
{
    $find(value).add_expandComplete(Function.createDelegate(null, collapsiblePanel_onExpandComplete));
    $find(value).add_collapseComplete(Function.createDelegate(null, collapsiblePanel_onCollapseComplete));
}
