var other = 0;
var req;
var reqt;
var what;
var reqb;
var reqn;
var page2;

function dateChanged(calendar) {
   if (calendar.dateClicked) {
		 var y = calendar.date.getFullYear();
		 var m = calendar.date.getMonth(); // integer, 0..11
		 var d = calendar.date.getDate(); // integer, 1..31
		 m = m + 1;
		 reqScheduleNews("schedule2", "ssd="+d+"&ssm="+m+"&ssy="+y, "");
   }
}

function fillDivs(schx) {
         var sx = document.getElementById("scheduleq");
         sx.innerHTML = schx;
         if(what == 'schedule' || what == 'schedule2') {
                  calSetup();

         }
}

function calSetup() {
    if (document.all) {
        Calendar.setup({
            inputField     :    document.all.startdate,
            ifFormat       :    "%Y-%m-%d",
            showsTime      :    false,
            firstDay       : 1,
            button         :    document.all.chgd,
            singleClick    :    true,
            step           :    2,
            onUpdate       :    dateChanged,
	        x	           :     -200
        });
    }
    else {
        Calendar.setup({
            inputField     :    document.getElementById("startdate"),
            ifFormat       :    "%Y-%m-%d",
            showsTime      :    false,
            firstDay    : 1,
            button         :    document.getElementById("chgd"),
            singleClick    :    true,
            step           :    2,
            onUpdate       :    dateChanged,
            x              :    -200
        });
    }
}
function processReqChange() {
    if (req.readyState == 4) {
        if (req.status == 200) {
           fillDivs(req.responseText);
           if(req.responseText == 0) {
           }
        } else {
            //alert("There was a problem retrieving the XML data:\n" +
            //    req.statusText);
        }
    }
}
function reqScheduleNews(w, args, channel) {

    if(args != 'short') {
		if(w == 'schedule') {
		    reqTimeout();
		        other = 0;
	        } else {
		        other = 1;
		}
    }

    if(args == 'short' && other == 1) {
	return;
    }

    req = false;
    if(window.XMLHttpRequest) {
	    try {
	        req = new XMLHttpRequest();
	    } catch(e) {
	        req = false;
	    }
    } else if(window.ActiveXObject) {
       try {
        req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
           try {
              req = new ActiveXObject("Microsoft.XMLHTTP");
           } catch(e) {
               req = false;
           }
        }
    }
    if(req) {
        req.onreadystatechange = processReqChange;
        url = '/' + channelName + '/' + 'index.php?module=schedule&filter=latest&channel2=&category=&keyword=&mode=' + w + '&rand=' + Math.random() + '&' + args + '&channel='+channel;
        //alert(url);
        what = w;
        req.open("GET", url, true);
        req.send("");
    }
}

function processTimeout() {
    if (reqt.readyState == 4) {
        if (reqt.status == 200) {
	 eval(reqt.responseText);
         x = tmpTimeout;
 	 if(x == 0) x = 60;
	 if(other == 0) {
	     window.setTimeout('reqTimeout()', (x*1000) + 1000 );
	 }
	 if(other == 0) {
	    window.setTimeout('reqScheduleNews("schedule", "short", "")', (x*1000) + 1000);
	 }
           if(reqt.responseText == 0) {
           }
        } else {
            //alert("There was a problem retrieving the XML data:\n" +
             //   reqt.statusText);
        }
    }
}
function reqTimeout() {

    reqt = false;
    if(window.XMLHttpRequest) {
	    try {
	        reqt = new XMLHttpRequest();
	    } catch(e) {
	        reqt = false;
	    }
    } else if(window.ActiveXObject) {
       try {
        reqt = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
           try {
              reqt = new ActiveXObject("Microsoft.XMLHTTP");
           } catch(e) {
               reqt = false;
           }
        }
    }
    if(reqt) {
        reqt.onreadystatechange = processTimeout;
        url = 'index.php?module=timeout'
        reqt.open("GET", url, true);
        reqt.send("");
    }
}


function sup(URL) {
	id = 'sup';
	try {
	    if(channelName) {
		URL = "/" + channelName + "/" + URL;
	    }
	} catch(e) {}
        eval("page =" + "window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=0,width=900,height=690,left = 107,top = 164');");
	page.focus();
}

function processBroadcastBox() {
    if(reqb.readyState == 4) {
        if(reqb.status == 200) {
	        eval(reqb.responseText);
	        x = tmpTimeout;
	        if(x == 0) x = 60;
	        window.setTimeout('reqBroadcastBox()', (x*1000) + 1000);
            if(reqb.responseText == 0) {
            }
        } else {
        }
    }
}
function reqBroadcastBox() {
    reqb = false;
    if(window.XMLHttpRequest) {
    try {
        reqb = new XMLHttpRequest();
    } catch(e) {
        reqb = false;
    }
    // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
       try {
        reqb = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
           try {
              reqb = new ActiveXObject("Microsoft.XMLHTTP");
           } catch(e) {
               reqb = false;
           }
        }
    }
    if(reqb) {
        reqb.onreadystatechange = processBroadcastBox;
        url = 'index.php?module=timeout';
        reqb.open("GET", url, true);
        reqb.send("");
    }
}
