function loadScript(src, callback) {
    var node = document.createElement("script");
    if (node.addEventListener)
        node.addEventListener("load", callback, false);
    else
    node.onreadystatechange = function() {
        if ((this.readyState == "complete") || (this.readyState == "complete")) {
            callback.call(this);
        }
    }
    node.src = src;
    document.getElementsByTagName("head").item(0).appendChild(node);
    node = null;
}
function twvalidate() {
      if (isEmpty(document.twdownload.email.value) || (!isEmail(document.twdownload.email.value))){
         alert('Please enter your email address.');
         return;}

      if (isEmpty(document.twdownload.zip.value) || (!isZIPCode(document.twdownload.zip.value))){
         alert('Please enter your zip.');
         return;}

      document.twdownload.referral_id.value = QString('referral_id');
      if (document.twdownload.referral_id.value == 'undefined')
         document.twdownload.referral_id.value = '';
      
         document.twdownload.submit();

   }

function Image_Viewer(s, i, h, t) {
        try {
                var node = t.responseXML;
                var html = node.getElementsByTagName('html').item(0);
        } catch(e) {
                return false;
        }
        if ((!i) || (!s)) return;
        var imgs = html.getElementsByTagName('img');
        for (var n=0; n<imgs.length; n++) {
                src = imgs[n].getAttribute('src') + "?" + imgs[n].getAttribute('href');
                alt = imgs[n].getAttribute('alt');
                s.options[s.options.length] = new Option(alt, src);
        }
        i.src = s.options[0].value;
        if (h) h.href = i.src.split("?")[1];
}

function Image_Change(i, h, v) {
        if (i) i.src = v.split("?")[0];
        if (h) h.href = v.split("?")[1];
}

function Calendar_Writer(c_id) {
	var mydate= new Date()
	var theyear=mydate.getFullYear()
	var themonth=mydate.getMonth()+1
	var thetoday=mydate.getDate()
	var theday=mydate.getDay() 
	var dow = new Array()
	var html = "";
	dow[0] = "Sun"
	dow[1] = "Mon"
	dow[2] = "Tue"
	dow[3] = "Wed"
	dow[4] = "Thu"
	dow[5] = "Fri"
	dow[6] = "Sat" 
	for (i=0;i<7;i++) {
		myday = theday+i
		mytoday = mydate.getDate()+i
		newmonth = themonth 
		if ((themonth == 4 || themonth == 6 || themonth == 9 || themonth == 11) && mytoday > 30) {
			mytoday = mytoday - 30
			newmonth = themonth + 1
		} else if ((themonth == 1 || themonth == 3 || themonth == 5 || themonth == 7 || themonth == 8 || themonth == 10 || themonth == 12) && mytoday > 31) {
			mytoday = mytoday - 31
			newmonth = themonth + 1
		} else if (themonth == 2) {
			if ((theyear/4 == parseInt(theyear/4)) && mytoday > 29) {
				mytoday = mytoday - 29
				newmonth = themonth + 1
			} else if (mytoday > 28) {
				mytoday = mytoday - 28
				newmonth = themonth + 1
			}
		}
		if (myday>6) { myday=myday-7 }
	
		html += "<div align='center' style='border: 1px solid #333; padding: 2px; margin-left: 2px; margin-right: 2px; float: left; background:white; cursor:pointer; width: 31px;' class='eventscalendar' onClick=location='http://lookatokc.newsok.com/calendar/?sdate="+theyear+"-"+newmonth+"-"+mytoday+"&edate="+theyear+"-"+newmonth+"-"+mytoday+"' onmouseover=this.style.background='#EDCACA' onmouseout=this.style.background='white'><b>"+dow[myday]+"</b><br><span class='calendar_day'>"+mytoday+"</span></div>"
	}
	$(c_id).innerHTML = html;
}

//Brightcove Omniture Implementation Code

var player;
var content;
var video;

// called by the player when it loads

// Brightcove 2 code
function onStreamStart (evt) {
var eventName = "play";
var app = "HomepageVideoPlayer";
callFlash("getCurrentTitle");
}
function onTemplateLoaded(message) {
// Brightcove 2 code
callFlash("addEventListener", "streamStart", "onStreamStart");
callFlash("getCurrentTitle");
}

function getTitleById_Result(evt) {
if (evt == undefined) {
callFlash("fetchTitleById", title_id);
} else {
callFlash("loadTitleById", title_id);
}
}

function getCurrentTitle_Result(titleDTO) {
    clearStats();
    s.pageName='Video Play: ' + titleDTO.displayName
    s.hier1="Brightcove"
    s.prop7=readCookie("nid")
    s.prop8='Video Play: ' + titleDTO.displayName
    var s_code=s.t();if(s_code)document.write(s_code)
}

//Event listener for Cell Phone Alerts

function omnitureCellPhone() {
        if (parent.s) {
                var ss=parent.s_gi('newsokprod');
                ss.linkTrackVars='prop9';
                ss.prop9='Cell Phone Alert';
                void(ss.tl(true, 'o', ss.prop9))
        
                var s_code=parent.s.tl();
        }       
}

// Weather Function
function loadWeatherOutlook() {
    var zip = ($('current_weather_zip')) ? $('current_weather_zip').value : '';

    url = '/ajax/weather/weather_outlook.php?current_weather_zip='+zip+'&noCacheIE=' + (new Date()).getTime();
    new Ajax.Updater('weather_outlook', url, {asynchronous:true});
}

function clearStats() {
    s.pageName=""
    s.server=""
    s.channel=""
    s.pageType=""
    s.prop1=""
    s.prop2=""
    s.prop3=""
    s.prop4=""
    s.prop5=""
    s.campaign=""
    s.state=""
    s.zip=""
    s.events=""
    s.products=""
    s.purchaseID=""
    s.eVar1=""
    s.eVar2=""
    s.eVar3=""
    s.eVar4=""
    s.eVar5=""
    s.hier1=""
}
function swapBars(holder, bar){
    var url = '/ajax/bar.php';
    var params = 'bar='+bar;
    new Ajax.Updater(holder, url, {parameters: params, asynchronous:true});
}
