// Hide accessibility submits from javascript browsers 
$(document).ready(function() {
    $('.no-script').css('position', 'absolute');
    $('.no-script').css('margin-left', '-900000px');

    $('#input-search').focus(clearDefault2).blur(showDefault2);
    $('#newsletter input#email').focus(clearDefault2).blur(showDefault2);
    $('input.default-text').focus(clearDefault2).blur(showDefault2);
});

function clearDefault2(ev)
{
    //clearDefault(ev.originalTarget)
    clearDefault(ev.target)
}

function showDefault2(ev)
{
    //showDefault(ev.originalTarget);
    showDefault(ev.target);
}


// Quick navigation autosubmit function for javascript enabled browsers
function goTo(Selected)
{
    var newURL = Selected.options[Selected.selectedIndex].value ;

    if (newURL!=" ")
    {
        document.location.href = newURL;
    }        
}


/*-------------------------
Promotion Tracker Functions - wonder what actually uses these ...
-------------------------*/
// Validate reference
function PtValidateReference(reference) {
  var result = /X|Y|Z/.test(reference);
  return result;
}

// Create cookie
function PtCreateCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

// Read cookie
function PtReadCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(";");
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

// Destroy cookie
function PtDestroyCookie(name) {
    PtCreateCookie(name, "", -1);
}

// Set promotion reference if promotion_tracker field exists
function PtSetPromotionReference(reference) {
  if (document.getElementById("orderref")) {
    var sourcecode = document.getElementById("orderref").value;
  var first_half = sourcecode.substring(0, 7);
  var second_half = sourcecode.substring(8);
  document.getElementById("orderref").value = first_half + reference + second_half;
  }
}
 
/*---------------------
Promotion Tracker Logic
---------------------*/
function promotion_tracker() {
  // Set Variables
  var pt_qs = new Querystring();
  var pt_reference = pt_qs.get('ptcode');
  var pt_valid = PtValidateReference(pt_reference);
  var pt_cookie = PtReadCookie('promotion_tracker');
  // If a valid reference code is found
  if (pt_valid == true) {
    // Look for a existing cookie
    if (pt_cookie) {
      // If a cookie exists, set the hidden form field to the value held in the existing cookie
      PtSetPromotionReference(pt_cookie);
    }
    // If no cookie exists
    else {
      // Create a new cookie that expires in 6 months
      PtCreateCookie('promotion_tracker', pt_reference, 183);
      PtSetPromotionReference(pt_reference);
    }
  }
  // If no valid reference code is found
  else {
    // Look for a existing cookie
    if (pt_cookie) {
      // If a cookie exists, set the promotion tracker reference
      PtSetPromotionReference(pt_cookie);
    }
  }
}
 
/*-----------
Miscellaneous
-----------*/
/* Client-side access to querystring name=value pairs
    Version 1.2.3
    22 Jun 2005
    Adam Vandenberg
*/
function Querystring(qs) { // optionally pass a querystring to parse
    this.params = new Object()
    this.get=Querystring_get
    
    if (qs == null)
        qs=location.search.substring(1,location.search.length)
    if (qs.length == 0) return
// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ')
    var args = qs.split('&') // parse out name/value pairs separated via &
    
// split out each name=value pair
    for (var i=0;i<args.length;i++) {
        var value;
        var pair = args[i].split('=')
        var name = unescape(pair[0])
        if (pair.length == 2)
            value = unescape(pair[1])
        else
            value = name
        
        this.params[name] = value
    }
}
function Querystring_get(key, default_) {
    // This silly looking line changes UNDEFINED to NULL
    if (default_ == null) default_ = null;
    
    var value=this.params[key]
    if (value==null) value=default_;
    
    return value
}



//story of the future code
function openStory(url){width = screen.width - 100;height = screen.height - 100;var Win=window.open(url,"displayWindow",'top=20,left=20,width=' + width + ',height=' + height + ',resizeable=0,scrollbars=no,menubar=no,status=no');}


// Clear input text - should be called via onfocus()
function clearDefault(el) 
{
    if (el.defaultValue == el.value)
    {
        el.value = "";
    }
}
 
// Reinstate the default value - should be called via onblur()
function showDefault(el) 
{
    if (el.value == "")
    {
        el.value = el.defaultValue;
    }
}

//rewrite the future blackboard
//STC  - SET THE CONSTANTS HERE
        SAVE_THE_CHILDREN_URL="http://rewritefuture.stc-resources.org/";
        SATELLITE_SITE_NUMBER=36; //Your satellite site number, refer to the list below.
        LANGUAGE="English"
function openWindow(url,width,height) {
            var Win=window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',resizeable=0,scrollbars=no,menubar=no,status=no');
        }

        function launchrtf()
        {
            openWindow(SAVE_THE_CHILDREN_URL + "index.php?mp=" + SATELLITE_SITE_NUMBER,800,600);
        }
       


    
// Check all checkboxes on the advanced search
function selectAll(box)
{
    // Get the element's name
    var id = box.id;
    var name = /^([a-z]+)(-select-all)$/i.exec(id);
    name = name[1];
    re = new RegExp('^'+name+'-[0-9]+$');

    // Get the elements status
    var state = box.checked;

    // Get the form element, search "up"
    var form = box.parentNode;

    while (form.nodeName.toLowerCase() != "form")
    {
        if (!form.parentNode)
        {
            break;
        }

        form = form.parentNode;
    }

    // Search through forms children for email_ checkboxes
    for (i = 0; i < form.elements.length; i++)
    {
        if (form.elements[i].type == 'checkbox')
        {
            // Find checkbox for each member of the group:
            if (re.test(form.elements[i].name))
            {
                form.elements[i].checked = state;
            }
        }
    }
}


      // Onloads
      window.onload = function() {
        
       // promotion_tracker();
        var quickNav         = document.getElementById('select-navigation');
        var sections         = document.getElementById('sections-select-all');
        var contentTypes     = document.getElementById('content-select-all');
 
       // RedDot Additions
        var primary        = document.getElementById('primary');        
        var enquiry        = document.getElementById('enquiry');
        var keyword       = document.getElementById('keyword');
        var bymonth       = document.getElementById('by-month');
        var region       = document.getElementById('by-region');
        var topic       = document.getElementById('by-topic');
        
        // Modified by Bobby Jack: check each object is non-null before
        // assigning to onchange.
        if (quickNav) quickNav.onchange = function() { goTo(this); };

        if (primary) primary.onchange = function() { goTo(this); };
        if (enquiry) enquiry.onchange = function() { goTo(this); };
        if (keyword) keyword.onchange = function() { goTo(this); };
        if (bymonth) bymonth.onchange = function() { goTo(this); };
        if (region)  region.onchange  = function() { goTo(this); };
        if (topic)   topic.onchange   = function() { goTo(this); };
        
        if (sections != null) {
            sections.onclick = function() {
                  return selectAll(sections);
              };
        }
        
        if (contentTypes !=null) {    
            contentTypes.onclick = function() {
                  return selectAll(contentTypes);
              };
        }        
        
      }