var mDomain = getDomain(2);
var mDomainCall = mDomain.split('.');
var mCompany = "";

var mCompany = "nwtv12";
var SiteName = mCompany + ".dayport.com";
checkLaunch();

function LaunchSyndicateArticle(ArtId,template,CustId,ContId,CatID)
{
  var SiteRegExp = new RegExp('@' + SiteName.replace(/\//g,"\\/"),"gi");
  var ArtString=ArtId.toString().replace(SiteRegExp,"");
  var Customer_ID="";
  var Contract_ID="";
  var TemplateFile="";
  var Category_ID="";  
  var URLstring="";
  var ParentFile="";
  var kWidth = 0;
  var kHeight = 0;
  var winName = "Contagion";
  var page;

  kWidth = 608;
  kHeight = 515;
  var winName = "News12";
  var page;

  if ((template != '') && (typeof template != "undefined"))
  {
     TemplateFile = '&tf='+template;
     switch (template)
     {  
        case "News12Viewer.tpl":
          ParentFile = "";
          kWidth = 375;
          kHeight = 340;
          winName = "News12Viewer";
          break;
        case "nwtv12player.tpl":
          ParentFile = "";
          kWidth = 608;
          kHeight = 515;
          winName = "News12Viewer";
          break;

     }
  }

if ((CatID != '') && (typeof CatID != "undefined")) {	
	switch(CatID) {
		case "0":
			//URLstring = "&PreloadContract_DefID=1&Contract_DefID=3";
			break;		
		default:
			URLstring = "";
	} 
}


        if ((CustId != '') && (typeof CustId != "undefined"))
        {
          if (CustId.charAt(0) == "-")
            Customer_ID = '&ExCust='+CustId.slice(1);
          else      
            Customer_ID = '&Customer_ID='+CustId;
        } 
 
	if ((ContId != '') && (typeof ContId != "undefined") && (ContId == "0"))
		Contract_ID = '&NoAds=true';
	else if ((ContId != '') && (typeof ContId != "undefined")) 
		Contract_ID = '&Contract_ID='+ContId;  	
	
	if ((CatID != '') && (typeof CatID != "undefined"))
		Category_ID = '&Category_ID='+CatID;
	else
		Category_ID = '&Category_ID=3';

	page = "http://nwtv12.dayport.com/viewer/viewerpage.php?Art_ID=" + ArtString + TemplateFile + URLstring + Customer_ID + Contract_ID + Category_ID;

  var xPosition = screen.width/2-(kWidth/2);
  var yPosition = screen.height/2-(kHeight/2);
  var Params = "height=" + kHeight + ",width=" + kWidth + ",left=" + xPosition + ",top=" + yPosition + ",screenX=" + xPosition + ",screenY=" + yPosition;
  //window.scrollTo(0,0);
  var kWin=window.open(page,winName,Params); 
  kWin.opener = self;
  kWin.focus();
  //kWin.moveTo(xPosition, yPosition);  
  return true;
}

function BuildSyndicatePlaylist(template,CatID)
{
    var ArtString="";
    var ArtArray = new Array();
    var TemplateFile="";

    //alert(document.forms.length);
    for (var j=0; j<document.forms.length; j++)
    {
        //alert(document.forms[j].name);
        //alert(document.forms[j].name.substr(0,11));
        if (document.forms[j].name.substr(0,11) == 'ArticleList')
        {
            //alert('found a match '+document.forms[j].name)
            FORM = eval('document.'+document.forms[j].name);
            if (FORM.V1)
            {
                if ((!FORM.V1.length) && (FORM.V1.checked == true))
                { 
                    // When only one checkbox exists in form and it is checked
                    v = parseInt(FORM.V1.value);
                    if((!ArtArray[v]) || (ArtArray[v] != 1))
                    {
                        ArtArray[v] = 1;
                        if (ArtString=="")
                            ArtString=ArtString + FORM.V1.value;
                        else
                            ArtString=ArtString + "," + FORM.V1.value;
                    }
                }
                else
                {
                    //alert(FORM.V1.length);
                    for (i=0;i<FORM.V1.length;i++)
                    {
                        if (FORM.V1[i].checked==true)
                        {
                            v = parseInt(FORM.V1[i].value);
                            if((!ArtArray[v]) || (ArtArray[v] != 1))
                            {
                                ArtArray[v] = 1;
                                if (ArtString=="")
                                    ArtString=ArtString + FORM.V1[i].value;
                                else
	                            ArtString=ArtString + ","  + FORM.V1[i].value;
                            }
                        }
                    }
                }
            }
        }
    }

    if (ArtString=="")
    	alert("You must select a story to add to your playlist!");
    else
    	LaunchSyndicateArticle(ArtString,template,'','',CatID);
}

function getURLVar(v,topVar)
{
    // strip off the URL parameters
	if ((topVar != '') && (typeof topVar != "undefined")) {
	    var url_params = window.top.location.search.substr(1);	
	} else {
	    var url_params = window.location.search.substr(1);
	}

    var retn = '';
    if (url_params != '')
    {
        var url_param_array = url_params.split('&');
        var url_param_count = url_param_array.length;
        var temp;

        for(var count = 0; count < url_param_count; count++)
        {
            temp = url_param_array[count].split('=');
            if (temp[0] == v)
                retn = temp[1];
        }
    }
    return retn;
}

function moreLink(catid)
{
    // strip off the URL parameters
    var url_params = window.location.search.substr(1);

    var next = "";
    var cat = "";
    if (url_params != '')
    {
        //alert(url_params);
        var url_param_array = url_params.split('&');
        var url_param_count = url_param_array.length;
        var temp;

        for(var count = 0; count < url_param_count; count++)
        {
            //alert(url_param_array[count]);
            temp = url_param_array[count].split('=');
            //alert(temp[0]); alert(temp[1]);
            if (temp[0] == 'next')
                next = temp[1];
            else if (temp[0] == 'prev')
                prev = temp[1];
            else if (temp[0] == 'cat')
                cat = temp[1];
        }
    }
    if (catid == cat)
        return next;
    else
        return 0;
}

function BuildVideoPlaylist()
{
    var ArtString="";
    var ArtArray = new Array();
    var TemplateFile="";

    //alert(document.forms.length);
    for (var j=0; j<document.forms.length; j++)
    {
        //alert(document.forms[j].name);
        //alert(document.forms[j].name.substr(0,11));
        if (document.forms[j].name.substr(0,11) == 'ArticleList')
        {
            //alert('found a match '+document.forms[j].name)
            FORM = eval('document.'+document.forms[j].name);
            if (FORM.V1)
            {
                if ((!FORM.V1.length) && (FORM.V1.checked == true))
                { 
                    // When only one checkbox exists in form and it is checked
                    v = parseInt(FORM.V1.value);
                    if((!ArtArray[v]) || (ArtArray[v] != 1))
                    {
                        ArtArray[v] = 1;
                        if (ArtString=="")
                            ArtString=ArtString + FORM.V1.value;
                        else
                            ArtString=ArtString + "," + FORM.V1.value;
                    }
                }
                else
                {
                    //alert(FORM.V1.length);
                    for (i=0;i<FORM.V1.length;i++)
                    {
                        if (FORM.V1[i].checked==true)
                        {
                            v = parseInt(FORM.V1[i].value);
                            if((!ArtArray[v]) || (ArtArray[v] != 1))
                            {
                                ArtArray[v] = 1;
                                if (ArtString=="")
                                    ArtString=ArtString + FORM.V1[i].value;
                                else
	                            ArtString=ArtString + ","  + FORM.V1[i].value;
                            }
                        }
                    }
                }
            }
        }
    }

    if (ArtString=="")
    	alert("You must select a story to add to your playlist!");
    else
    	SetContractDefs(ArtString);
}

function changeMenu(strMenu) {
	
    var strArray = strMenu.split(',');        
    CatID = strArray[0];       
    DomID = strArray[1];
	loc = "http://"+DomID+".dayport.com/nw/article/archive/"+CatID+"/?Category_ID="+CatID+"&Limit=6&Offset=0&nc=1&tf=rightmenu.tpl"
	window.location.href=loc;
}


function video_search(srchtxt) {
	if (srchtxt == "") {
    	alert("You must enter a keyword in order to search.");		
		document.forms[1].SearchText.focus();
	} else {
		window.location.href='http://contagion.dayport.com/nw/article/search/?SearchText=' + srchtxt + '&tf=search_video.tpl';
	}
}

var NS = (window.Event) ? 1 : 0;
function checkKey(e) {  
	var code = (NS) ? e.which : event.keyCode;
	if (code == 13)	{
		video_search(document.forms[1].SearchText.value);
		return false;
	}
}
if (NS) document.captureEvents(Event.KEYPRESS)
	document.onkeypress = checkKey;
	
function getDomain(v)
{
	var strWin = window.location.href
	var winArray = strWin.split('/'); 

    var retn = winArray[v];
    return retn;
}
function SetContractDefs(ArtID) {
	/* The category ID is parsed as a template variable by DayPort-AL */
	var CatID = "0"; 

  	 /* This switch statement is based on the Category ID 
	You would add a new case with the appropriate values for every category you want to handle
	You don’t need to do anything for the viewer to use the default contract definitions
   */

	
    /* Now change the URL */
	var strWin = window.location.href
	var winArray = strWin.split('/'); 
	var Offset_ID = '&Offset=0';
	//alert('Marsha ' + getURLVar("Offset"));
if ((CatID != '') && (typeof CatID != "undefined")) {	
	switch(CatID) {
		case "0":case "9":case "10":case "12":
			URLstring = "&PreloadContract_DefID=1&Contract_DefID=4";
			break;		
		case "21":
			URLstring = "&PreloadContract_DefID=1&Contract_DefID=5";
			break;		
		default:
			URLstring = "";
	} 
     }
	
	if ((getURLVar("Offset") != '') && (typeof getURLVar("Offset") != "undefined"))
             Offset_ID = '&Offset='+ getURLVar("Offset");
	
	loc = "http://"+winArray[2]+"/viewer/viewerpage.php?Art_ID=" + ArtID  + URLstring + "&tf=News12Viewer.tpl&Category_ID=" + CatID + Offset_ID +"&SearchText=" + getURLVar("SearchText");
	window.top.location.href=loc;
}