	
	
	function setCreateStation(doNotAskFlag,changeMyStation){
		var cookieEnabled=(navigator.cookieEnabled)? true : false

		//if not IE4+ nor NS6+
		if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){ 
			document.cookie="testcookie"
			cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
		}
		if (cookieEnabled){
			if(doNotAskFlag=='Yes')
				jQuery('#doNoAskSpan').show();
			else
				jQuery('#doNoAskSpan').hide();
			jQuery('#stationFinder').show();
			return true;
		}
		else{
			if(changeMyStation=='Yes')
				alert('Please enable browser cookies to use this feature');
			return false;
		}
	}
	
	var expdate = new Date (); 
	expdate.setTime(expdate.getTime() + (100 * 365 * 24 * 60 * 60 * 1000));
	var zipcode='';
	var stationName='';
	var callLetters='';
	var tvdata_name='';
	var tvdata_commonname='';
	var createFeedFound=0;
	var headend_id='';
	var headend_name='';
	var channel_no='';
	var createProgram=1;
	var startDate=new Date();
	timezoneJS.timezone.zoneFileBasePath = '/CreateTV.nsf';
	timezoneJS.timezone.defaultZoneFile = ['northamerica'];
	timezoneJS.timezone.init({async: false});
	function getTimeZone(lookup){
jQuery.getJSON("/CreateProgram.nsf/APITimezone?OpenAgent&zip="+lookup,
	        function(data,s,r){
			jQuery.each(data,function(){
				SetCookie('olson_name', this.olson_name, expdate);
			});
       		 });
		return true;
	}
	
	
	function getStationsByZip(){
	var stationFound=0;
		if(jQuery('#ZIP').val()=='' || jQuery('#ZIP').val()==null){
			SetCookie('tvdata_name','', expdate);
			SetCookie('tvdata_commonname','', expdate);
			SetCookie('olson_name', 'US/Eastern', expdate);
			SetCookie('headend_id', '', expdate);
			SetCookie('headend_name', '', expdate);
			SetCookie('channel_no','');
			SetCookie('feed_id','14505', expdate);	//showing default feed of WGBH which is 24-7
			SetCookie('station_feed','1',expdate);
			return null;
		}		
		
		jQuery('#stationsContainer').html('');
		if(GetCookie('tvdata_commonname')!='' && GetCookie('tvdata_commonname')!=null)
			jQuery('#stationsContainer').append('<br/><b> Current station:</b> '+ GetCookie('tvdata_commonname'));	
		jQuery('#stationsContainer').append('<br/><br/><select id="stationsList" onChange="getHeadendsByZip(\'\')"><option>Select your station</option></select>');
		
		
		jQuery.getJSON("/CreateProgram.nsf/APICallLetters?OpenAgent&zip="+jQuery('#ZIP').val(),
	        function(data,s,r){
				jQuery.each(data,function(){
					if(this.tvdata_name!='' && this.tvdata_name!=null){
						jQuery('#stationsList').append('<option value="'+this.tvdata_name+'">'+this.common_name+'</option>');
						stationFound=1;
					}
				});
				jQuery('#stationsContainer').append('<br/>');
 	       });
			jQuery.getJSON("/CreateProgram.nsf/FeedIDLookup?Readform&zip="+jQuery('#ZIP').val(),
	        function(data,s,r){
				jQuery.each(data,function(){
					jQuery('#stationsList').append('<option value="'+this.tvdata_name+'">'+this.common_name+'</option>');
					stationFound=1;
				});
				jQuery('#stationsContainer').append('<br/>');
 	      		 });
			jQuery.getJSON("/CreateProgram.nsf/FeedIDLookup?Readform&zipoverride="+jQuery('#ZIP').val(),
	        function(data,s,r){
				jQuery.each(data,function(){
					jQuery('#stationsList').append('<option value="'+this.tvdata_name+'">'+this.common_name+'</option>');
					stationFound=1;
				});
				jQuery('#stationsContainer').append('<br/>');
 	      		 });
		
	}
	function getHeadendsByZip(flag){
			var zipCode=jQuery('#ZIP').val();
			if(flag=='providerFinder'){
				getFeedsByStation(GetCookie('tvdata_name'));
				jQuery('#zipAction').click(function(){getHeadendsByZip('');});
			}
			else{
				getFeedsByStation(jQuery('#stationsList option:selected').val());
				tvdata_name= jQuery('#stationsList option:selected').val();
				tvdata_commonname=jQuery('#stationsList option:selected').text();
			}
			getTimeZone(zipCode);
jQuery.getJSON("/CreateProgram.nsf/APIHeadends?OpenAgent&zip="+zipCode,
	        function(data,s,r){
			jQuery('#headendsContainer').html('');
			jQuery('#headendsContainer').append('<br/><br/><select id="headendsList" onChange="setHeadend()"><option>Select your provider</option></select>');
			jQuery('#headendsList').append('<option value="OTA">Over the air</option>');
			jQuery.each(data,function(){
				jQuery('#headendsList').append('<option value="'+this.headend_id+'">'+this.headend_name+'</option>');
			});
			jQuery('#headendsContainer').append('<br/><br/>');
 	       });
		
	}
	
	function setHeadend(){
		headend_id= jQuery('#headendsList option:selected').val();
		headend_name= jQuery('#headendsList option:selected').text();
		SetCookie('headend_id', headend_id, expdate);
		SetCookie('headend_name', headend_name, expdate);
		channel_no=getChannelNo();
	}
	function getFeedsByStation(tvdata_name){
		createFeedFound=0;
	//over riding the feed ids from PBS with the ones found in the Db
jQuery.getJSON("/CreateProgram.nsf/FeedIDLookup?Readform&tvdata="+tvdata_name,
	        function(data,s,r){
			jQuery.each(data,function(){
					if(this.feed_id!='' && this.feed_id!=null){
						SetCookie('feed_id',this.feed_id, expdate);	
						SetCookie('feedid_overrideflag',this.feedid_overrideflag, expdate);	
						createFeedFound=1;
						SetCookie('station_feed','1',expdate);
					}
			});
       		 });
		if(createFeedFound==0){
jQuery.getJSON("/CreateProgram.nsf/APIFeedIDs?OpenAgent&tvdataname="+tvdata_name,
		        function(data,s,r){
				jQuery.each(data,function(){
					var common_name=this.feed_common_name;
					if(common_name.search(/create/i)!=-1 && createFeedFound==0){
						SetCookie('feed_id',this.feed_id, expdate);	
						SetCookie('feed_common_name',this.feed_common_name,expdate);	
						SetCookie('station_feed','1',expdate);
						createFeedFound=1;
					}
				});
				if(createFeedFound==0){
					SetCookie('feed_id','14505', expdate);	//default feed of WGBH which is 24-7
					SetCookie('station_feed','0',expdate);
				}	
	       		 });
		}
	}	
	function getChannelNo(){
			if((GetCookie('tvdata_name')=='WCBB' || tvdata_name=='WCBB') && (GetCookie('headend_id')=='450' || headend_id=='307813' || GetCookie('headend_id')=='307813' || headend_id=='450' )){
				SetCookie('channel_no','182 | channel #172 for York & Cumberland Counties',expdate);	
				return true;
			}
			if(GetCookie('feedid_overrideflag')=='1'){
				SetCookie('channel_no','Information not available',expdate);	
				return true;				
			}
		
		if(GetCookie('headend_id')=='OTA'){
			jQuery.getJSON("/CreateProgram.nsf/APIOTAMetaData?OpenAgent&feedid="+GetCookie('feed_id'),
	       		 function(data,s,r){
				var createChannelFound=0;
				jQuery.each(data,function(){
					SetCookie('channel_no',this.channel_number,expdate);
					createChannelFound=1;
				});
				if(createChannelFound==0){
					SetCookie('channel_no','');				
				}	
       			 });
			
		}
		else{
jQuery.getJSON("/CreateProgram.nsf/APIFeedMetaData?OpenAgent&feedid="+GetCookie('feed_id')+"&headendid="+GetCookie('headend_id'),
	       		 function(data,s,r){
				var createChannelFound=0;
				var createChannelNo='';
				jQuery.each(data,function(){
					createChannelNo=this.cable_channel;
					if(createChannelNo.charAt(0)=='0')
						SetCookie('channel_no',createChannelNo.substring(1),expdate);
					else
						SetCookie('channel_no',createChannelNo,expdate);
					createChannelFound=1;
				});
				if(createChannelFound==0){
					SetCookie('channel_no','');				
				}	
       			 });
		}
			
	}
	function bakeCookies(divName){
		if(jQuery('#doNotAsk').is(':checked')){
			SetCookie('doNotAsk','Yes', expdate);
			jQuery('#stationFinder').hide();
			return true;
		}
		SetCookie('zipcode',jQuery('#ZIP').val(), expdate);
		SetCookie('tvdata_name',tvdata_name, expdate);
		SetCookie('tvdata_commonname',tvdata_commonname, expdate);
		jQuery('#stationFinder').hide();
		var temp3=location.href.split('&');
		orgLocation=temp3[0];
		if(orgLocation==location.href){
			window.location.reload();
		}
		else{		
			window.location.href=orgLocation+'&genre=Arts & Crafts';
		}
	}
	function unbakeCookies(){
		if(jQuery('#doNotAsk').is(':checked')){
			SetCookie('doNotAsk','Yes', expdate);
			jQuery('#stationFinder').hide();
			return true;
		}
		SetCookie('zipcode','02145', expdate);
		SetCookie('tvdata_name','', expdate);
		SetCookie('tvdata_commonname',"National schedule", expdate);
		SetCookie('headend_name',"", expdate);
		SetCookie('ucf',"WGBXDT3", expdate);
		jQuery('#stationFinder').hide();
		var temp3=location.href.split('&');
		orgLocation=temp3[0];
		if(orgLocation==location.href){
			window.location.reload();
		}
		else{		
			window.location.href=orgLocation+'&genre=Arts & Crafts';
		}
	}
	
	function convertTime(str,type,currentDay){
		var schedHours='';
		var dateCheck=false;
		var schedMinutes='';
		var schedAMPM='';
		var parts = str.split('T');
		var hourMin='';
		var week3Alpha=['SUN','MON','TUE','WED','THU','FRI','SAT'];
		var month3Alpha=['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'];
		dateParts = parts[0].split('-');
		timeParts = parts[1].split('Z');
		timeSubParts = timeParts[0].split(':');
		timeSecParts = timeSubParts[2].split('.');
		timeHours = Number(timeSubParts[0]);
		var dt = new timezoneJS.Date(Number(dateParts[0]),Number(dateParts[1])-1, Number(dateParts[2]),Number(timeHours) ,Number(timeSubParts[1]),Number(timeSecParts[0]), 'Etc/UTC');
		dt.setTimezone(GetCookie('olson_name'));
		var dt2= new timezoneJS.Date(dt.getTime()+dt.getTimezoneOffset());
				
		if(dt2.getUTCHours()>12)
			schedHours=dt2.getUTCHours()-12;
		else
			schedHours=dt2.getUTCHours();
		if(dt2.getUTCHours()>0 && dt2.getUTCHours()<10 )
			schedHours='0'+dt2.getUTCHours();
		
		
		if(dt2.getUTCHours()=='0'||dt2.getUTCHours()==0)
			schedHours='12';
		if(dt2.getUTCHours()<12)
			schedAMPM='AM';
		else
			schedAMPM='PM';
		if(dt2.getUTCMinutes()<10)
			schedMinutes='0'+dt2.getUTCMinutes();
		else
			schedMinutes=dt2.getUTCMinutes();
		hourMin=schedHours+':'+schedMinutes+' '+schedAMPM;
		
		monthDay=week3Alpha[dt2.getUTCDay()]+', '+month3Alpha[dt2.getUTCMonth()]+' '+dt2.getUTCDate();
		if (dt2.getUTCDate()==currentDay)
			dateCheck=true
		else
			dateCheck=false;
		
	
		if(type=='hourMin')
			return hourMin;
		if(type=='monthDay')
			return monthDay; 
		if(type=="date")
			return dt.toString();
		if (type=='dateCheck')
			return dateCheck;
	
		return dt;	
	}
	function getSchedule2(feed_id,startTime,endTime,currentDay,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1){
		getWhatsOnNow(feed_id,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1);
jQuery('#schedule').html('<div id="loaderImage"><img src="/CreateTV.nsf/ajax-loader.gif" width="50" height="50"></div>');	
jQuery.getJSON("/CreateProgram.nsf/APIAirdatesByFeeds?OpenAgent&feedid="+GetCookie('feed_id')+"&starttime="+startTime+"&endtime="+endTime,
	        function(data,s,r){
			jQuery('#schedule').html('');	
			if(GetCookie('station_feed')=='0'){
					jQuery('#schedule').html('<b> Your PTV station doe s not carry Create. The national schedule is shown below:</b><br/><br/>');
				}
				jQuery.each(data,function(){
					var startTime2=this.pbs_start_time+"Z";
					if(convertTime(startTime2,'dateCheck',currentDay)){
						var episodeNo='';
						if(this.nola_code_2!='0')
							episodeNo=' &bull; Episode #'+this.nola_code_2;
						if(this.title30=='Off-Air')
							jQuery('#schedule').append('<div class="schedContainer"><div class="schedTime"><span class="TVTime"></span></div><div class="schedTitle"><span class="TvTitle">'+this.title30+'</div><div class="clear"></div></div><br/>');
						else{
							jQuery('#schedule').append('<div class="schedContainer"><div class="schedTime"><span class="TVTime">'+convertTime(startTime2,'hourMin')+'</span></div>');
							if(this.nola_code_1==null){
								jQuery('#schedule').append('<div class="schedTitle"><b>'+this.title30+'</b> <br/><span class="textBold">'+this.episode_title+episodeNo+'</span><br/><span class="TVText">'+this.description3+'</span></div><div class="clear"></div>');	
							}	
							else{				
								jQuery('#schedule').append('<div class="schedTitle"><span class="TVDesc"><a href="/CreateProgram.nsf/vProgramsByNola/'+this.nola_code_1+'" class="TvTitle">'+this.title30+'</a></span><br/><span class="textBold">'+this.episode_title+episodeNo+'</span><br/><span class="TVText">'+this.description3+'</span></div><div class="clear"></div>');	
							}	
				jQuery('#schedule').append('</div><br/>');
						}
					}
				});
       		 });
		
							
	}	
	function getWhatsOnNow(feed_id,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1){
	//	showNotice();
		var orgLocation='';
		var temp1='';
		var temp2='';
		var temp3;
		var temp='';
		var callLetters='';
		var stationName='';
		temp1=location.search.split('&callletters=');
		temp2=location.search.split('&station=');
		temp3=location.href.split('&');
		orgLocation=temp3[0];
				
		if(temp1.length>1){
			if(temp1[1].search(/&/i)!=-1){
				temp=temp1[1].split('&');
				callLetters=temp[0];
			}
			else{
				callLetters=temp1[1];
			}
		}
		if(temp2.length>1){
			if(temp2[1].search(/&/i)!=-1){
				temp=temp2[1].split('&');
				stationName=temp[0].replace("+"," ");
			}
			else{
				stationName=temp2[1].replace("+"," ");
			}
		}
		
		if(callLetters!=GetCookie('tvdata_name') && callLetters!='' && callLetters!=null){
			if(stationName!=''){		
				SetCookie('tvdata_commonname',stationName,expdate);
				tvdata_commonname=stationName;
			}
			SetCookie('tvdata_name',callLetters, expdate);
			tvdata_name=callLetters;
			headend_id='';
			headend_name='';
			channel_no='';
			SetCookie('headend_id', '');
			SetCookie('headend_name', '');
			SetCookie('channel_no','')
			jQuery('#zipAction').click(function(){getHeadendsByZip('providerFinder');});
			jQuery('#stationsContainer').hide();
			jQuery('#channelInfo').html('');
			if(GetCookie('doNotAsk')!='Yes')		
				setCreateStation('Yes');
			return true;
		}
	
		
		jQuery.getJSON("/CreateProgram.nsf/APIAirdatesByFeeds?OpenAgent&feedid="+feed_id+"&starttime="+whatsOnStart+"&endtime="+whatsOnEnd,
		function(data,s,r){
			jQuery('#whatsOnNow').html('');
			var i=0;
			jQuery.each(data,function(){
				if(i>0) return null;
				var episodeNo='';
				jQuery('#whatsOnNow').append('<a href="/CreateProgram.nsf/vProgramsByNola/'+this.nola_code_1+'">'+this.title30+'</a><br/>');
				i=i+1;
			});
       		 });
		
		if((GetCookie('tvdata_name')=='' | GetCookie('tvdata_name')==null) && GetCookie('doNotAsk')!='Yes'){
			setCreateStation('Yes');
			return true;
		}
	}	
 	function getProgramScheduleByNola(nola,feed_id,startTime,endTime,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1){
		getWhatsOnNow(feed_id,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1);	
	jQuery.getJSON("/CreateProgram.nsf/APIProgramsByNola?OpenAgent&nola="+nola,
	        function(data,s,r){
			if(data.length==0){
				jQuery('#schedule').html('Not in Schedule now. Please check later');
			}
			else{
				jQuery('#programInfo').html('');
				var i=0;
				jQuery.each(data,function(){
					if(i==0){
						getScheduleByProgramId(this.program_id,feed_id,startTime,endTime,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1);
						i=1;
					}
				});
			}
       		 });
	}
	function getScheduleByProgramId(program_id,feed_id,startTime,endTime,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1){
		
		jQuery('#schedule').html('<div id="loaderImage"><img src="/CreateTV.nsf/ajax-loader.gif" width="50" height="50"></div>');	
		jQuery.getJSON("/CreateProgram.nsf/APIAirdatesByProgramFeed?OpenAgent&programid="
+program_id+"&feedid="+feed_id+"&starttime="+startTime+"&endtime="+endTime,
	        function(data,s,r){
			var temp=GetCookie('olson_name');
			if(temp=='' || temp==null)
				var timeZone='Eastern';
			else{
				var timeZone1=temp.split('/');
				var timeZone=timeZone1[1];
			}
			var currDate='';
			var currEpisode='';
			var episodeTitle='';
			var currTime='';
			var startTime='';
			var i=0;
			var tableRows='';
			jQuery('#schedule').html('<table id="schedTable" border="0" cellspacing="2" cellspacing="2" width="500"></table>');
			jQuery('#schedTable').append('<tr><td colspan="2">Schedule in '+timeZone+' time</td></tr>');
			jQuery.each(data,function(){
				i=i+1;
				startTime=this.pbs_start_time;
				if(this.episode_title=='')
					episodeTitle=this.nola_code_2;
				else
					episodeTitle=this.episode_title;
				if(currDate!=convertTime(startTime+'Z','monthDay')){
					currDate=convertTime(startTime+'Z','monthDay')
					tableRows=tableRows+'<tr><td colspan="2"><br/><span class="textBold">'+currDate+'</span></td></tr>';
					currEpisode=episodeTitle;
					tableRows=tableRows+'<tr><td width="200" valign="top">'+currEpisode+'</td><td valign="top"> &bull;'+convertTime(startTime+'Z','hourMin');
				}else {
					if(currEpisode!=episodeTitle ){
						currEpisode=episodeTitle;
						tableRows=tableRows+'</tr><tr><td width="200" valign="top">'+currEpisode+'</td><td valign="top"> &bull;'+convertTime(startTime+'Z','hourMin');
					}
					else{											
						tableRows=tableRows+' &bull;'+convertTime(startTime+'Z','hourMin');
					}
				}
			});
			jQuery('#schedTable').append(tableRows);
			if(i==0)
				jQuery('#schedule').html('Not in Schedule now. Please check later')
			
       		 });
	
	}
	function tabSchedule(tabNum,nola,feed_id,startTime,endTime,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1){
		showTab(tabNum);
		getProgramScheduleByNola(nola,feed_id,startTime,endTime,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1);
	}
	function showcaseTabSchedule(tabNum,nola,feed_id,startTime,endTime,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1){
		showShowcaseTab(tabNum);
		getProgramScheduleByNola(nola,feed_id,startTime,endTime,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1);
	}
	
	function tabGeneral(tabNum,feed_id,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1){
		showTab(tabNum);
		getWhatsOnNow(feed_id,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1);
	}
//Checks if NOLA is a Create Program
function getSchedule(feed_id,startTime,endTime,currentDay,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1){
	var nolaValues=new Array();	
	getWhatsOnNow(feed_id,whatsOnStart,whatsOnEnd,tvdata_name1,tvdata_commonname1);
	jQuery('#schedule').html('<div id="loaderImage"><img src="/CreateTV.nsf/ajax-loader.gif" width="50" height="50"></div>');	
	nolaCheck= jQuery.parseJSON(
				jQuery.ajax({
					url: "/CreateProgram.nsf/NOLACheck?Readform&NOLA=",
				      	async: false,
				      	dataType: 'json'	
				}).responseText
		);
	var i=0;
	jQuery.each(nolaCheck.nolas,function(){
		nolaValues[i]=this.nola;
		i++;
	});
jQuery.getJSON("/CreateProgram.nsf/APIAirdatesByFeeds?OpenAgent&feedid="+feed_id+"&starttime="+startTime+"&endtime="+endTime,
	        function(data,s,r){
			jQuery('#schedule').html('');	
			if(GetCookie('station_feed')=='0'){
					jQuery('#schedule').html('<b> Your PTV station does not carry Create. The national schedule is shown below:</b><br/><br/>');
				}
				jQuery.each(data,function(){
					var startTime2=this.pbs_start_time+"Z";
					if(convertTime(startTime2,'dateCheck',currentDay)){
						var episodeNo='';
						if(this.nola_code_2!='0')
							episodeNo=' &bull; Episode #'+this.nola_code_2;
						if(this.title30=='Off-Air')
							jQuery('#schedule').append('<div class="schedContainer"><div class="schedTime"><span class="TVTime"></span></div><div class="schedTitle"><span class="TvTitle">'+this.title30+'</div><div class="clear"></div></div><br/>');
						else{
							jQuery('#schedule').append('<div class="schedContainer"><div class="schedTime"><span class="TVTime">'+convertTime(startTime2,'hourMin')+'</span></div>');
													
							
							if((this.nola_code_1==null) | jQuery.inArray(this.nola_code_1,nolaValues)==-1){
								jQuery('#schedule').append('<div class="schedTitle"><b>'+this.title30+'</b> (not a Create Program) <br/><span class="textBold">'+this.episode_title+episodeNo+'</span><br/><span class="TVText">'+this.description3+'</span></div><div class="clear"></div>');	
							}	
							else{				
								jQuery('#schedule').append('<div class="schedTitle"><span class="TVDesc"><a href="/CreateProgram.nsf/vProgramsByNola/'+this.nola_code_1+'" class="TvTitle">'+this.title30+'</a></span><br/><span class="textBold">'+this.episode_title+episodeNo+'</span><br/><span class="TVText">'+this.description3+'</span></div><div class="clear"></div>');	
							}	
				jQuery('#schedule').append('</div><br/>');
						}
					}
				});
       		 });
		
							
	}	
