﻿// JScript File

// From XSchedule.JS
Date.prototype.toDotNetString = function()
{
	return this.toShortDateString() + " " + this.toShortTimeString();
};

// From ScheduleSports.JS
Date.prototype.toSportsString = function()
{
	//ddd M/d/yyyy HHmm
	var day = "";
	switch (this.getDay())
	{
		case 0:
			day = "Sun";break;
		case 1:
			day = "Mon";break;
		case 2:
			day = "Tue";break;
		case 3:
			day = "Wed";break;
		case 4:
			day = "Thu";break;
		case 5:
			day = "Fri";break;
		case 6:
			day = "Sat";break;
	}
	
	var hour = this.getHours();
	var minute = this.getMinutes();
	if (hour < 10) { hour = "0" + hour; }
	if (minute < 10) { minute = "0" + minute; }
	var timeString = hour.toString() + minute.toString();
	
	return day + " " + this.toShortDateString() + " " + timeString;
};

Date.prototype.toShortDateString = function()
{
	return this.getMonth() + 1 + "/" + this.getDate() + "/" + this.getFullYear();
};

Date.prototype.toShortTimeString = function()
{
	var hour = this.getHours();
	var minute = this.getMinutes();
	var ap = "AM";
	if (hour > 11) { ap = "PM"; }
	if (hour > 12) { hour = hour - 12; }
	if (hour == 0) { hour = 12; }
	if (hour < 10) { hour = "0" + hour; }
	if (minute < 10) { minute = "0" + minute; }
	var timeString = hour + 
		':' + 
		minute + 
		" " + 
		ap;
	return timeString;
};

Date.prototype.to24HourTimeString = function()
{
	var hour = this.getHours();
	var minute = this.getMinutes();
	if (hour < 10) { hour = "0" + hour; }
	if (minute < 10) { minute = "0" + minute; }
	var timeString = hour.toString() + ":" + minute.toString();
	
	return timeString;
};

Date.prototype.addDays = function(days)
{
	this.setDate(this.getDate() + days);
};

Date.prototype.addHours = function(hours)
{
	this.setHours(this.getHours() + hours);
};

Date.prototype.addMinutes = function(minutes)
{
	this.setMinutes(this.getMinutes() + minutes);
};

// Gets an array of radio controls
function GetRadioListControls(TableID)
{
	var table = document.getElementById(TableID);
	var controls = new Array();
	
	for (var i = 0; i < table.rows.length; i++)
	{
		var row = table.rows[i];
		
		for (var j = 0; j < rows.cells.length; j++)
		{
			var cell = rows.cells[j];
			
			for (var k = 0; k < cell.childNodes.length; k++)
			{
				if (cell.childNodes[k].tagName == "INPUT")
					controls.push(cell.childNodes[k]);
			}
		}
	}
	
	return controls;
}

function SetCookie(sSettingName, sSettingValue)
{
	fnStartSetCookie(sSettingName, sSettingValue);
}

function GetCookie(sSettingName)
{
	fnStartGetCookie(sSettingName);
}

function fnStartGetCookie(sSettingName)
{
    var oXmlHttp = zXmlHttp.createRequest();
    oXmlHttp.open("post", "Preferences.aspx", true);
    oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    oXmlHttp.onreadystatechange = function () 
    {
        if (oXmlHttp.readyState == 4) 
        {
            fnEndGetCookie(oXmlHttp.responseText);
        }            
    };
    
    var sBody = 
		encodeURIComponent("Action") + "=" + 
		encodeURIComponent("GET") + "&" + 
		encodeURIComponent("SettingName") + "=" + 
		encodeURIComponent(sSettingName);
    oXmlHttp.send(sBody); 
}

function fnEndGetCookie(sSettingValue)
{
	alert(sSettingValue);
}

function fnStartSetCookie(sSettingName, sSettingValue)
{
	var oXmlHttp = zXmlHttp.createRequest();
    oXmlHttp.open("post", "Preferences.aspx", true);
    oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    oXmlHttp.onreadystatechange = function () 
    {
        if (oXmlHttp.readyState == 4) 
        {
            fnEndSetCookie(oXmlHttp.responseText);
        }            
    };
    
    var sBody = 
		encodeURIComponent("Action") + "=" + 
		encodeURIComponent("SET") + "&" + 
		encodeURIComponent("SettingName") + "=" + 
		encodeURIComponent(sSettingName) + "&" + 
		encodeURIComponent("SettingValue") + "=" + 
		encodeURIComponent(sSettingValue);
    oXmlHttp.send(sBody);
}

function fnEndSetCookie(sResponse)
{
	//alert(sResponse);
}

function goToBulletin(bulletinID, slash) 
{
    if (document.all) 
           var xMax = screen.width, yMax = screen.height;
       else  
           if (document.layers) 
               var xMax = window.outerWidth, yMax = window.outerHeight; 
           else 
               var xMax = 640, yMax=480; 
                                           
       var xOffset = (xMax - 200)/2, yOffset = (yMax - 200)/2; 
                                           
       var features = 
           'width=480px'        + 
           ',height=360px'      +  
           ',directories=no' +  
           ',location=no'    +  
           ',menubar=no'     +  
           ',scrollbars=no'  +  
           ',status=no'      +  
           ',toolbar=no'     +  
           ',resizable=no'   +  
           ',screenX='     + xOffset + 
           ',screenY='     + yOffset +  
          ',top='         + yOffset + 
           ',left='        + xOffset; 
           
       try {                                           
       var theURL = slash + 'BulletinPopup.aspx?b=' + bulletinID; 
       theWindow = window.open(theURL, 'PopUpWindow', features); 
       theWindow.focus(); 
       }
       catch(er) {
            alert("Please Enabled Popups to View Bulletin Information");
       }
 }


function f_PopupTimeZone()
{
	if (document.all) 
		var xMax = screen.width, yMax = screen.height;
	else  
		if (document.layers) 
			var xMax = window.outerWidth, yMax = window.outerHeight; 
		else 
			var xMax = 640, yMax=480; 

	var xOffset = (xMax - 200)/2, yOffset = (yMax - 200)/2; 
	
	var features = 
		'width=1003px'        + 
		',height=520px'      +  
		',directories=no' +  
		',location=no'    +  
		',menubar=no'     +  
		',scrollbars=no'  +  
		',status=no'      +  
		',toolbar=no'     +  
		',resizable=no'   +  
		',screenX='     + xOffset + 
		',screenY='     + yOffset +  
		',top='         + yOffset + 
		',left='        + xOffset;

	try
	{
//		if (window.showModalDialog) 
//		{
//			var sValue = window.showModalDialog("TimeZonePopup.aspx","TimeZonePopup","dialogWidth:1003px;dialogHeight:520px");
//			if (sValue != "")
//			{
//				try
//				{
//					f_TimeZone(sValue);
//				}
//				catch (ex2) {}
//			}
//		} 
//		else 
//		{
			//features += ',modal=yes';
			timeZonePopup = window.open('TimeZonePopup.aspx', 'TimeZonePopup', features);
			timeZonePopup.focus();
//		}
	}
	catch (ex)
	{
		try
		{
			timeZonePopup = window.open("TimeZonePopup.aspx", "TimeZonePopup", features);
			timeZonePopup.focus();
		}
		catch (ex1)
		{
			alert("Please enable popups to view Time Zone display.");
		}
	}
	
	return false;
}

function f_CloseTimeZone()
{
	try
	{
		if (timeZonePopup != null)
			timeZonePopup.close();
	}
	catch (ex) {}
	
	return false;
}
     
function CreateIEFlashControl(DivID, CODEBASE, WIDTH, HEIGHT, MOVIE, QUALITY, BGCOLOR, MENU, SALIGN, ALTLINK, OPENINNEWWINDOW, ALTIMAGE, ALTIMAGEWIDTH, ALTIMAGEHEIGHT, ALTTEXT, FLASHVARS)
{
var szInnerHTML = '';
var d = document.getElementById(DivID);

if (MOVIE != '')
{
szInnerHTML =
'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" id="Flash_' + DivID + '" width="' + WIDTH + '" height="' + HEIGHT +'" codebase="' + CODEBASE + '" allowScriptAccess="always">';

szInnerHTML += '<param name="movie" value="' + MOVIE + '">';
szInnerHTML += '<param name="quality" value="' + QUALITY + '">';

if (BGCOLOR != '')
{
szInnerHTML += '<param name="bgcolor" value="' + BGCOLOR + '">';
}

szInnerHTML += '<param name="salign" value="' + MENU + '">';
szInnerHTML += '<param name="menu" value="' + SALIGN + '">';

if (FLASHVARS != '')
{
szInnerHTML += '<param name="FlashVars" value="' + FLASHVARS + '">';
}
}

if (ALTLINK != '')
{
szInnerHTML += '<a href="' + ALTLINK + '"';
if (OPENINNEWWINDOW)
{
szInnerHTML += ' target="_blank"';
}
szInnerHTML += '>';
}

if (ALTIMAGE != '')
{
szInnerHTML += '<img src="' + ALTIMAGE + '" alttext="' + ALTTEXT + '"';

if (ALTIMAGEWIDTH != '')
{
szInnerHTML += ' WIDTH="' + ALTIMAGEWIDTH + '"';
}

if (ALTIMAGEHEIGHT != '')
{
szInnerHTML += ' HEIGHT="' + ALTIMAGEHEIGHT + '"';
}

szInnerHTML += '></img>';
}
else
{
szInnerHTML += '<span>' + ALTTEXT + '</span>';
}

if (ALTLINK != '')
{
szInnerHTML += '</a>';
}

if (MOVIE != '')
{
szInnerHTML += '</object>';
}
//alert(szInnerHTML);
d.innerHTML = szInnerHTML;
}

function fnSchedulePopup(oChartList, sTimeZoneID, sCalendarDateID, sDefaultTimeZone)
{
	var sChart = oChartList.options[oChartList.selectedIndex].text;
	
	if (sChart == "PRINT")
	{
		var sTimeZone = document.getElementById(sTimeZoneID).options[document.getElementById(sTimeZoneID).selectedIndex].value;
		var sCalendarDate = document.getElementById(sCalendarDateID).value;
		
		if (sTimeZone == "")
			sTimeZone = sDefaultTimeZone;
			
		SchedulePrint(sTimeZone.replace("&", "^").replace("+", "!"), sCalendarDate);
	}
//	else
//	{
//		location.href = oChartList.options[oChartList.selectedIndex].value;
//	}
	
	return false;
}

function SchedulePrint(sTimeZone)	// AirDate
{
	var xMax = 0;
	
	if (document.all)
	{
		xMax = screen.width, yMax = screen.height;
	}
	else
	{ 
		if (document.layers) 
		{
			xMax = window.outerWidth, yMax = window.outerHeight; 
		}
		else 
		{
			xMax = 640, yMax=480; 
		}
	}

	var xOffset = (xMax - 200)/2, yOffset = (yMax - 200)/2; 

	var features = 
		'width=800px'		+ 
		',height=600px'		+  
		',directories=no'	+  
		',location=no'		+  
		',menubar=yes'		+  
		',scrollbars=yes'	+  
		',status=no'		+  
		',toolbar=yes'		+ 
		',resizable=yes'	+ 
		',screenX='			+ xOffset + 
		',screenY='			+ yOffset +  
		',top='				+ yOffset + 
		',left='			+ xOffset; 

	try 
	{
		var sUrl = 'SchedulePrint.aspx?TimeZone=' + sTimeZone; 
		if (arguments.length > 1)
		{
			sUrl += '&AirDate=' + arguments[1];
		}
		var dt = new Date();
		var oWindow = window.open(sUrl, "Win" + dt.getTime().toString(), features); 
		oWindow.focus(); 
	}
	catch(e) 
	{
		alert("Please Enabled Popups to View Schedule Information");
	}
}

function fnGetUrlRoot()
{
	var url = location.href;
	if (url.indexOf("?") > -1)
		url = url.substring(0, url.indexOf("?"));
	if (url.indexOf("/") > -1)
		url = url.substring(0, url.lastIndexOf("/") + 1)
		
	return url;
}

function SearchForProgram(sProgram)	// iRecordCount (10, 20, ..., 50)
{
	var sUrl = 'Search.aspx?q=' + escape(sProgram) + '&p=1&n=';
	if (arguments.length > 1)
		sUrl += arguments[1].toString();
	else
		sUrl += '20';
		
	location.href = sUrl;
}

function PageQuery(q) 
{
	if(q.length > 1) 
		this.q = q.substring(1, q.length);
	else 
		this.q = null;

	this.keyValuePairs = new Array();

	if(q) 
	{
		for(var i=0; i < this.q.split("&").length; i++) 
		{
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}

	this.getKeyValuePairs = function() 
	{ 
		return this.keyValuePairs; 
	}

	this.getValue = function(s) 
	{
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}

	this.getParameters = function() 
	{
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}

	this.getLength = function() 
	{ 
		return this.keyValuePairs.length; 
	}
}

function f_QueryString(key)
{
	var page = new PageQuery(window.location.search);
	return unescape(page.getValue(key));
}

function f_DisplayItem(key)
{
	var v = f_QueryString(key);
	
	if(v == 'false')
	{
		alert("You didn't enter a ?name=value querystring item.");
	}
	else
	{
		return v;
	}
}

function f_GetUrl(url) //target
{
	var target = "_self";
	if (arguments.length > 1)
		target = arguments[1];
		
	if (target == "_self" || target == "")
		location.href = url;
	else if (target == "_blank")
		window.open(url);
	else
		location.href = url + "#" + target;
}

function f_CreateBookmark(tz)
{
	var url = "http://myafn.dodmedia.osd.mil/?ctz=" + escape(tz).replace("&", "^").replace("+", "!");
	var title = "myAFN Home";
	
	if (window.sidebar) // Mozilla Firefox Bookmark
	{ 
		window.sidebar.addPersistentPanel(title, url, url);
	} 
	else if (window.external) // IE Favorite
	{ 
		window.external.AddFavorite( url, title); 
	}
	else if(window.opera && window.print) // Opera Hotlist
	{ 
		var a = document.createElement("A");
		a.rel = "sidebar";
		a.target = "_search";
		a.title = title;
		a.href = url;
		a.click(); 
	}
	else
	{
		return true;
	}
}

function f_GetICSValue()
{
	
}
