// ------------------------------------------ Target2Top
// Make the form's target the top window.
function Target2Top() {
  window.open("", document.forms[0].target).focus();
  return true;
} // Target2Top

// ------------------------------------------ Link2Top
// Make the links's target the top window.
function Link2Top(target) {
//  window.open("", target).focus();
  return true;
} // Link2Top

// ------------------------------------------- getArgs
// return the page arguments (arg=value).
function getArgs() {
//alert("getArgs");
  var args = new Object();
  var query = location.search.substring(1);
  var pairs = query.split("+");
  for (var i=0; i < pairs.length; i++) {
    var pos = pairs[i].indexOf('=');
    if (pos == -1) continue;
    var argname = pairs[i].substring(0, pos);
    var value = pairs[i].substring(pos+1);
    args[argname] = unescape(value);
  }
  if (!args.state) args[state]='GEN';
  return args;
}

// -------------------------------------------- heading
// Output html for page heading.
function heading(state, bgcolor, sample) {
  var d = document;
  d.writeln('<html><head>');
  d.writeln('<title>Essential Skills Online(TM)</title>');
	d.writeln('<meta name="description" content="Test drive RALLY!Education\'s Essential Skills for Reading Online. Reading skill lessons correlated to your state test.">');
	d.write('<meta name="keywords" content="Reading,Reading Standards,TAKS,TEKS,Texas Reading,NY ELA,New York Reading,NJ ASK,New Jersey Reading,Connecticut Reading,CMT 4 ELA,FCAT,Florida Reading,GPS ELA,Georgia Reading,ISTEP+,');
  d.writeln('Indiana Reading,ISAT,Illinois Reading">');
	d.writeln('<meta name="robots" content="index,follow">');

  d.writeln('<style type="text/css">');
  d.writeln('<!--');
  d.writeln('@import url(../rallycss/rally1.css);');
  d.writeln('-->');
  d.writeln('</style>');
  d.writeln('</head>');
  d.writeln('<body background="../states/'+
            state+'/ReadingBG3.gif" bgcolor="'+
            bgcolor+'"');
  d.writeln('style="background-repeat: repeat-x; '+
            'background-position: right top; '+
            'margin: 0px; padding: 0px;">');

  d.writeln('<img style="position: absolute; top: 0px; left: 0px;" border="0"');
  if (state == 'UFT') {
    d.writeln('alt="UFT" src="../states/UFT/UFTlogo.gif"></a>');
  }
  else {
    d.writeln('alt="RALLY! Education" src="../images/RALLYlogo3.gif"></a>');
    d.writeln('<span class="SubTitle">We\'re all about student success!<sup>&reg;</sup></span>');
  }
  d.writeln('<span class="NavBarLogin"></span>');
  if (sample != false) {
    d.writeln('<img style="position: absolute; top: 79px; left: 240px;"');
    d.writeln('alt="Sample" src="../images/sample.gif">');
  }
}

// ------------------------------------------------ StateInfo
// set the state and stateAbbr values
function StateInfo() {
  stateAbbr = state;
  activeStates = 'TX,NY,CT,FL,UFT,';
  
  if (activeStates.indexOf(state+',') == -1)
    state = 'GEN';
}

// ----------------------------------------------------- link
function link(url, state, stateAbbr) {
  d.write('<a href="'+url+'.html?state='+state+'+stateAbbr='+stateAbbr+'"');
}

// ------------------------------------------------- testName
function testName(state) {
  if (state == 'TX') return 'TAKS';
  if (state == 'NY') return 'NY ELA';
  if (state == 'FL') return 'FCAT';
  if (state == 'CT') return 'CMT4 ELA';

  return '';
}

// ------------------------------------------------ writeDate
function writeDate() {
  var now=new Date()

  var month=new Array(12)
  month[0]="January";
  month[1]="February";
  month[2]="March";
  month[3]="April";
  month[4]="May";
  month[5]="June";
  month[6]="July";
  month[7]="August";
  month[8]="September";
  month[9]="October";
  month[10]="November";
  month[11]="December";

  document.write(month[now.getMonth()]+' '+now.getDate()+', '+now.getFullYear());
}

var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadstatustext="<img src='images/loading.gif'> Loading content..."

////NO NEED TO EDIT BELOW////////////////////////
var loadedobjects=""
var defaultcontentarray=new Object()
var bustcacheparameter=""

function ajaxpage(url, containerid, targetobj){
//alert(url);
  var page_request = false
  if (window.XMLHttpRequest) // if Mozilla, Safari etc
    page_request = new XMLHttpRequest()
  else if (window.ActiveXObject){ // if IE
    try {
      page_request = new ActiveXObject("Msxml2.XMLHTTP")
    }
    catch (e){
      try{
      page_request = new ActiveXObject("Microsoft.XMLHTTP")
      }
      catch (e){ /*alert(e)*/}
      }
  }
  else
    return false

  var ullist=targetobj.parentNode.parentNode.getElementsByTagName("li")
  for (var i=0; i<ullist.length; i++)
    ullist[i].className=""  //deselect all tabs
  targetobj.parentNode.className="selected"  //highlight currently clicked on tab

  if (url.indexOf("#default")!=-1){ //if simply show default content within container (verus fetch it via ajax)
    document.getElementById(containerid).innerHTML=defaultcontentarray[containerid]
    return
  }
  document.getElementById(containerid).innerHTML=loadstatustext
  page_request.onreadystatechange=
    function(){
      loadpage(page_request, containerid)
    }
  if (bustcachevar) //if bust caching of external page
    bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
//alert(url+bustcacheparameter);
  page_request.open('GET', url+bustcacheparameter, true)
  page_request.send(null)
}

function loadpage(page_request, containerid){
  if (page_request.readyState == 4 &&
      (page_request.status==200 ||
       window.location.href.indexOf("http")==-1)) {
    var text = page_request.responseText;
    var k = text.indexOf("<!--");
    if (k != -1) {
      var selText = text.substring(k+4, text.length-3);
      text = text.substring(0,k);
      //alert(selText);
      document.getElementById("selects").innerHTML = selText;
    }
    document.getElementById(containerid).innerHTML = text;
  }
}

function loadobjs(revattribute){
  if (revattribute!=null && revattribute!=""){ //if "rev" attribute is defined (load external .js or .css files)
    var objectlist=revattribute.split(/\s*,\s*/) //split the files and store as array
    for (var i=0; i<objectlist.length; i++){
      var file=objectlist[i]
      var fileref=""
      if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
        if (file.indexOf(".js")!=-1){ //If object is a js file
          fileref=document.createElement('script')
          fileref.setAttribute("type","text/javascript");
          fileref.setAttribute("src", file);
        }
        else if (file.indexOf(".css")!=-1){ //If object is a css file
          fileref=document.createElement("link")
          fileref.setAttribute("rel", "stylesheet");
          fileref.setAttribute("type", "text/css");
          fileref.setAttribute("href", file);
        }
      }
      if (fileref!=""){
        document.getElementsByTagName("head").item(0).appendChild(fileref)
        loadedobjects+=file+" " //Remember this object as being already added to page
      }
    }
  }
}

function expandtab(tabcontentid, tabnumber){ //interface for selecting a tab (plus expand corresponding content)
  var thetab=document.getElementById(tabcontentid).getElementsByTagName("a")[tabnumber]
  if (thetab.getAttribute("rel")){
    ajaxpage(thetab.getAttribute("href"), thetab.getAttribute("rel"), thetab)
    loadobjs(thetab.getAttribute("rev"))
  }
}

function savedefaultcontent(contentid){// save default ajax tab content
  if (typeof defaultcontentarray[contentid]=="undefined") //if default content hasn't already been saved
    defaultcontentarray[contentid]=document.getElementById(contentid).innerHTML
}

function startajaxtabs(){
  for (var i=0; i<arguments.length; i++){ //loop through passed UL ids
    var ulobj=document.getElementById(arguments[i])
    var ulist=ulobj.getElementsByTagName("li") //array containing the LI elements within UL
    for (var x=0; x<ulist.length; x++){ //loop through each LI element
    var ulistlink=ulist[x].getElementsByTagName("a")[0]
    if (ulistlink.getAttribute("rel")){
      var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
      ulistlink.setAttribute("href", modifiedurl) //replace URL's root domain with dynamic root domain, for ajax security sake
      savedefaultcontent(ulistlink.getAttribute("rel")) //save default ajax tab content
      ulistlink.onclick=
        function(){
          ajaxpage(this.getAttribute("href"), this.getAttribute("rel"), this)
          loadobjs(this.getAttribute("rev"))
          return false
        }
      if (ulist[x].className=="selected"){
          ajaxpage(ulistlink.getAttribute("href"), ulistlink.getAttribute("rel"), ulistlink) //auto load currenly selected tab content
          loadobjs(ulistlink.getAttribute("rev")) //auto load any accompanying .js and .css files
        }
      }
    }
  }
}

function startajaxtabs2(){
  for (var i=0; i<arguments.length; i++){ //loop through passed UL ids
    var ulobj=document.getElementById(arguments[i])
    var ulist=ulobj.getElementsByTagName("li") //array containing the LI elements within UL
    for (var x=0; x<ulist.length; x++){ //loop through each LI element
    var ulistlink=ulist[x].getElementsByTagName("a")[0]
    if (ulistlink.getAttribute("rel")){
      var modifiedurl=ulistlink.getAttribute("href").replace(/^http:\/\/[^\/]+\//i, "http://"+window.location.hostname+"/")
      ulistlink.setAttribute("href", modifiedurl) //replace URL's root domain with dynamic root domain, for ajax security sake
      savedefaultcontent(ulistlink.getAttribute("rel")) //save default ajax tab content
      ulistlink.onclick=
        function(){
          var theForm = findObject(formID)
          var theData = ""
          for (var i=0; i<theForm.elements.length; i++) {
            name = theForm.elements[i].name
            if (name == 'report') theForm.elements[i].value = this.name
            value = theForm.elements[i].value
            if (name != "")
              theData += "+" + name + "=" + value
          }
          theData += '+';
          ajaxpage(this.getAttribute("href")+theData, this.getAttribute("rel"), this)
          loadobjs(this.getAttribute("rev"))
          return false
        }
      if (ulist[x].className=="selected"){
          ajaxpage(ulistlink.getAttribute("href"), ulistlink.getAttribute("rel"), ulistlink) //auto load currenly selected tab content
          loadobjs(ulistlink.getAttribute("rev")) //auto load any accompanying .js and .css files
        }
      }
    }
  }
}

/*
function ajaxUpdate() {
  var report = findObject('report').value;
alert(report);
  var obj = findObject(report);
alert(obj.name);
  obj.onClick();
}
*/

function ajaxSort(key, report) {
  obj = findObject('sortBy');
  obj.value = key;
  obj = findObject(report);
  obj.onclick();
}

// ------------------------------------------------------------------------ AJAX
// ---------------------------------------------------------- makeXMLHttpRequest

function makeXMLHttpRequest() {
 var ua;
 if(window.XMLHttpRequest) {
 try {
  ua = new XMLHttpRequest();
 } catch(e) {
  ua = false;
 }
 } else if(window.ActiveXObject) {
  try {
	ua = new ActiveXObject("Microsoft.XMLHTTP");
  } catch(e) {
	ua = false;
  }
 }
return ua;
}

// ------------------------------------------------------------------ findObject
function findObject(id) {
  var elm = null;
  if (document.getElementById) elm = document.getElementById(id);
  else if (document.layers) elm = document.layers[id];
  else if (document.all) elm = document.all[id];
  return elm;
} // getObject



