var xmlhttp, timeout_ID = null, UpCl = null, Update_Content_ID = null

window.onload = function(){
 var StartID = setTimeout("Startup()", 5000)
}

function Startup(){
 Update_Content()
 Update_Content_ID = setInterval("Update_Content()", 30000)
}

function Update_Content(){
 var webHandle = null
 var webRequestTimeOutID = null
 if(window.XMLHttpRequest){
   webHandle = new XMLHttpRequest()
 }else if(window.ActiveXObject){
   webHandle = new ActiveXObject("Microsoft.XMLHTTP")
 }
 
 if(webHandle != null){
   webRequestTimeOutID = setTimeout(function(){
     webHandle.abort()
   }, 5000)
   webHandle.onreadystatechange = function(){
     if(webHandle.readyState == 4){
       clearTimeout(webRequestTimeOutID)
       if(webHandle.status == 200){
         //Successful Read
         if(webHandle.responseText != ""){
           Activate_Weather_Alert(webHandle.responseText.slice(4,18))
         }else{
           //window.parent.Deactivate_Weather_Alert()
           Deactivate_Weather_Alert()
         }
         document.getElementById("Weather_Content_OBJECT").innerHTML = webHandle.responseText
       }else{
         //Error situation
         //
       }
     }
   }//End ReadyStateChange function
   webHandle.open("GET","weather_scraper.asp?s=" + Math.random(), true)
   webHandle.send(null)
 }//End If webHandle not null
}//End Update_Content

function Activate_Weather_Alert(Time_Index){
 if(getCookie("Active_Weather_Alert") != Time_Index){
   var Parent = window.parent
   if(Parent.document.title != "Coppin State University Homepage"){
      var newnode = Parent.document.createElement("A"), newtext = Parent.document.createTextNode("Weather Alert")
      newnode.setAttribute("href", "javascript:Open_Weather_Alert_Window(\"" + Time_Index + "\")")
      newnode.setAttribute("className", "Active_Weather_Alert_CLASS")
      newnode.setAttribute("class", "Active_Weather_Alert_CLASS")
      newnode.appendChild(newtext)
      //Replace Child 1 of Alert Container Object (empty DIV) with Weather Alert Link
      var oldnode = Parent.document.getElementById("Active_Alert_Container_OBJECT").childNodes[0]
      Parent.document.getElementById("Active_Alert_Container_OBJECT").replaceChild(newnode, oldnode)
   }else{
      //If there is an active Security Alert, do not show a competing Weather Message on the main homepge
      var Weather_Delayed = setTimeout(function(){
        if(Parent.document.getElementById("Security_Alert_Notification_OBJECT").style.display != "block"){
          Parent.document.getElementById("Weather_Alert_Notification_OBJECT").style.display = "block"
          var Homepage_Message = setTimeout("Set_Homepage_Message()", 250)
          var expiration = new Date(), today = new Date()
          expiration.setTime(today.getTime() + 1000*60*60*24)
          setCookie("Active_Weather_Alert", Time_Index, expiration)
        }
      }, 500)
   }
 }
}

function Cancel_Alert(Time_Index){
 var Parent = window.parent
 var newnode = Parent.document.createElement("DIV")
 //Replace Child 1 of Alert Container Object (Weather Alert Link) with empty DIV
 var oldnode = Parent.document.getElementById("Active_Alert_Container_OBJECT").childNodes[0]
 Parent.document.getElementById("Active_Alert_Container_OBJECT").replaceChild(newnode, oldnode)
 Parent.document.getElementById("Weather_Alert_Content_OBJECT").innerHTML = document.getElementById("Weather_Content_OBJECT").innerHTML
 Parent.Set_Alert_Message(document.getElementById("Weather_Content_OBJECT").innerText)
 var expiration = new Date(), today = new Date()
 expiration.setTime(today.getTime() + 1000*60*60*24)
 setCookie("Active_Weather_Alert", Time_Index, expiration)
}

function Deactivate_Weather_Alert(){
 var Parent = window.parent
 if(Parent.document.title != "Coppin State University Homepage"){
    Parent.Weather_Alert_Message = ""
    if(Parent.document.getElementById("Active_Alert_Container_OBJECT").childNodes[1].className == "Active_Weather_Alert_CLASS"){
      var newnode = Parent.document.createElement("DIV")
      //Replace Child 1 of Alert Container Object (Weather Alert Link) with empty DIV
      var oldnode = Parent.document.getElementById("Active_Alert_Container_OBJECT").childNodes[0]
      Parent.document.getElementById("Active_Alert_Container_OBJECT").replaceChild(newnode, oldnode)
      Parent.document.getElementById("Weather_Alert_Content_OBJECT").innerHTML = ""
    }
 }else{
    window.parent.document.getElementById("Weather_Alert_Content_OBJECT").innerHTML = ""
    window.parent.document.getElementById("Weather_Alert_Notification_OBJECT").style.display = "none"
 }
}

function Set_Homepage_Message(){
 window.parent.document.getElementById("Weather_Alert_Content_OBJECT").innerHTML = document.getElementById("Weather_Content_OBJECT").innerHTML
}
