
function getHttpObject()
{
	var httpRequest = null;
	try
	{
		httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	}	
	catch (e)
	{
		try 
		{	
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)	
		{
			httpRequest = null;
		}
	}
	if (!httpRequest && typeof XMLHttpRequest != "not defined")
	{
		httpRequest = new XMLHttpRequest();
	}
	return httpRequest;
}

function loadSchedule(id, onLoadCallBack, w)
{
	// code for IE
	var xmlURL = "http://"+ getHost() +"/scheduleXML/"+ id +".xml";	
	if( window.ActiveXObject )
   	{
  		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
 		xmlDoc.async=false;
		xmlDoc.load(xmlURL);
        onLoadCallBack(xmlDoc, w);
		return true;
  	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation &&
			document.implementation.createDocument)
	{
		xmlDoc=document.implementation.createDocument("","",null);
		xmlDoc.load(xmlURL);
		xmlDoc.onload = function()
      	{
        	onLoadCallBack(xmlDoc, w);
      	}
		return true;
	}
	else
   	{
      	return false;
   	}
}

function showSchedule(xmlDoc, width)
{
	var programs = xmlDoc.getElementsByTagName("program");
	var innerText = "<table cellspacing='0' cellpadding='0' border='0' class='schedule_box'><tbody>";
	var check = true;
	for (var x = 0; x < programs.length; x++)
	{	
		var now = new Date();
		var GMT_start_date =  xmlDoc.getElementsByTagName("start_date")[x].firstChild.nodeValue;
		var GMT_start_time =  xmlDoc.getElementsByTagName("start_time")[x].firstChild.nodeValue;
		var GMT_end_date =  xmlDoc.getElementsByTagName("end_date")[x].firstChild.nodeValue;
		var GMT_end_time =  xmlDoc.getElementsByTagName("end_time")[x].firstChild.nodeValue;
		var program_name =  xmlDoc.getElementsByTagName("name")[x].firstChild.nodeValue;
	
		var show_start_date = new Date(
		GMT_start_date.substring(0,4), 
		GMT_start_date.substring(5,7)-1,
		GMT_start_date.substring(8,10), 
		GMT_start_time.substring(0,2), 
		GMT_start_time.substring(3,5)  
		);
		
		var show_end_date = new Date(
		GMT_end_date.substring(0,4), 
		GMT_end_date.substring(5,7)-1,
		GMT_end_date.substring(8,10), 
		GMT_end_time.substring(0,2), 
		GMT_end_time.substring(3,5)  
		);
		
		
		var timeoffset = show_start_date.getTime() - (show_start_date.getTimezoneOffset() * 60000);
		var local_date_start = new Date(timeoffset);
		
		var timeoffset = show_end_date.getTime() - (show_end_date.getTimezoneOffset() * 60000);
		var local_date_end = new Date(timeoffset);
		
		var program_time_start = ((local_date_start.getHours()+"").length == 2 ? local_date_start.getHours() : "0"+local_date_start.getHours()) +":"+ ((local_date_start.getMinutes()+"").length == 2 ? local_date_start.getMinutes() : "0"+local_date_start.getMinutes());
		var program_time_end = ((local_date_end.getHours()+"").length == 2 ? local_date_end.getHours() : "0"+local_date_end.getHours()) +":"+ ((local_date_end.getMinutes()+"").length == 2 ? local_date_end.getMinutes() : "0"+local_date_end.getMinutes());
		
		if(check &&  local_date_start < now && now < local_date_end ){
			innerText += "<tr><td height='3'></td></tr><tr class='row_h'><td width='5'></td><td width='15'><img src='image/arrow.png' width='13' height='13'></td>"+
					"<td width='100' height='25'>"+program_time_start+" - " + program_time_end + "</td><td width='"+width+"'><div class='prog_chop'>"+program_name+"</div></td></tr><tr><td height='3'></td></tr>";
			check = false;
		}
		else
		innerText += "<tr class='row"+(x%2+1)+"'><td width='10'></td><td width='20'></td>"+
					"<td width='100' height='25'>"+program_time_start+" - " + program_time_end + "</td><td width='"+width+"'><div class='prog_chop'>"+program_name+"</div></td></tr>";
	 
	}
	if(programs.length == 0){
	   
		var bwidth = (width+100); 
		innerText += "<tr class='row2'><td width='"+bwidth+"' height='204' align='center'>Program Guide Not Available</td></tr>";
		}
		
	innerText += "</tbody></table>";
	document.getElementById('schedule_table').innerHTML = innerText;	
}

function getUrl(url, async, onStatusChangeExe) 
{ 
	var HttpObj = getHttpObject();	
	if (!HttpObj) 
		return; 
	if (onStatusChangeExe)
	{ 
		HttpObj.onreadystatechange = function() 
		{	
		onStatusChangeExe(HttpObj);	
		}; 
	} 
	else 
	{	
		HttpObj.onreadystatechange = function() 
		{;}	
	} 
	//avoiding IE cache 
	var fakeURL = url  + "&dummy=" + new Date().getTime();
	
	HttpObj.open("GET", fakeURL, async); 
	HttpObj.send(null); 
}
function displayHttpResponseToHTML(HttpObj, elementId) 
{ 
	initHttpResponse(HttpObj); 
	document.getElementById(elementId).innerHTML=HttpObj.responseText; 
}

function getContentAndDisplay(url, elementId) 
{	
	var x = new Object();
	x.requestCallBack = displayHttpResponseToHTML; 
	x.elementId = elementId; 	
	getUrl(url, true, execute(x));
}

function execute(obj)
{ 
	return function(HttpObj)	
	{ 
		if (HttpObj.readyState == 4 && HttpObj.status == 200) 
			obj.requestCallBack(HttpObj, obj.elementId ); 
	}; 
}

function initHttpResponse(HttpObj) 
{ 
	if(HttpObj.responseXML == null) 
	{ 
		return; 
	} 
} 
function postUrl(url, data, async, stateChangeCallback)
{
	var xmlHttpReq = getHttpObject();

 	if (!xmlHttpReq)
		return;

	xmlHttpReq.open("POST", url, async);
	xmlHttpReq.onreadystatechange = function()
	{
		stateChangeCallback;
	};
	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttpReq.send(data);
	
}
function urlEncodeDict(dict)
{
 var result = "";
 for (var i=0; i<dict.length; i++) {
 result += "&" + encodeURIComponent(dict[i].name) + "=" + encodeURIComponent(dict[i].value);
 }
 return result;
}
function postFormByForm(form, async, successCallback) {
    var formVars = new Array;
    for (var i = 0; i < form.elements.length; i++) {
        var formElement = form.elements[i];
        if (formElement.type == "checkbox" && !formElement.checked) {
            continue;
        }
        var v = new Object;
        v.name = formElement.name;
        v.value = formElement.value;
        formVars.push(v);
    }
    postUrl(form.action, urlEncodeDict(formVars), async, successCallback);
}

function blankF(req)
{}
