﻿window.onload = function() {
 
        
        initFlash ();
        createLinks ();
        createBanner ();

        
}

function createBanner () {
        // init banner
        
        var bannerType = getParams("type");
        document.getElementById("bannerholder").style.visibility = "visible";
        if (bannerType == 1) { bannerType = creatBannerName();}
        bannerLoop (bannerType,1);
        buttonClick ();
        
        // createLink
   //     var bLink = "http://fiberone.com/default.aspx";
    //    document.getElementById("bannerlinkHolder").href = bLink;
        
}

function bannerLink () {
    location.href = "http://freesample.fiberone.com/default.aspx?source=" + getParams("source") + "&type=" + getParams("type") ;
}

function bannerLoop(bannerType,count) {
    
    if (count > 3 ) {count = 1}
    document.getElementById("banner").src = "images/banner" + count + ".jpg"; 
    count = count + 1;
    setTimeout("bannerLoop('" + bannerType +"'," + count + ")",6000);
}

function buttonClick () {
  //  document.getElementById("bannerlink").onmouseover = function() { document.getElementById("bannerlink").src = "images/button-free-on.gif"}
 //   document.getElementById("bannerlink").onmouseout = function() { document.getElementById("bannerlink").src = "images/button-free-off.gif"}
}

function creatBannerName() {

    var nBanner = new Array(3);
    nBanner[1] = "bars"; nBanner[2] = "cereal";  nBanner[3] = "yogurt";
    return nBanner[Math.floor(Math.random()*3+1)];
    
    

}

function shareThis(thisvideo,titleScreen) 
{   

    //get video parameters
    var videoParam= "";
    var title="Coping with Disbelief"; 
    if (thisvideo != null) { 
        videoParam = "#" + thisvideo;
        title = titleScreen;
    }

    //initialize paramaters based on form to default/blank
    document.getElementById("thankyouVideo").style.visibility = "hidden";
	document.getElementById("thankyouEmail").style.visibility = "hidden";
	document.getElementById("email-site").style.visibility = "visible";
	document.getElementById("email").style.visibility = "visible";
	document.getElementById("video").style.visibility = "visible";

	
    if (videoParam == "") {
        document.getElementById("emailYourName").value = "";
        document.getElementById("emailFriendName").value = "";
        document.getElementById("emailYourAddress").value = "";
        document.getElementById("emailFriendAddress").value = "";
        document.getElementById("sendCopyEmail").checked = 0;
        
        document.getElementById("closeShareEmail").onclick = function() {
            document.getElementById("TB_window").style.display = "none";
            tb_remove();
        }
        document.getElementById("closeThanksEmail").onclick = function() {
            document.getElementById("TB_window").style.display = "none";
            tb_remove();
        }
        tb_show(title,'#TB_inline?height=470&width=924&inlineId=emailIDS&modal=true');
        validateFormEmail(videoParam);
    } else
    {
        document.getElementById("videoYourName").value = "";
        document.getElementById("videoFriendName").value = "";
        document.getElementById("videoYourAddress").value = "";
        document.getElementById("videoFriendAddress").value = "";
        document.getElementById("sendCopyVideo").checked = 0;
        
        document.getElementById("closeShareVideo").onclick = function() {
            document.getElementById("TB_window").style.display = "none";
            tb_remove();
        }
        document.getElementById("closeThanksVideo").onclick = function() {
            document.getElementById("TB_window").style.display = "none";
            tb_remove();
        }
        tb_show(title,'#TB_inline?height=470&width=924&inlineId=emailVideo&modal=true');
        validateFormVideo(videoParam);
        document.getElementById("shareTitle").innerHTML = title;
        document.getElementById("smallTitle").innerHTML = title;
        
        // create source to copy
        if (title == "Crisis Averted") { document.getElementById("embedCode").value = '<object style="height: 344px; width: 425px"><param name="movie" value="http://www.youtube.com/v/btUUIBzlF1M"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/btUUIBzlF1M" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="425" height="344"></object>' }
        if (title == "We Are Live") { document.getElementById("embedCode").value = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/bkrzfEe77Zw"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/bkrzfEe77Zw" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>' }
        if (title == "Protect Yourself") { document.getElementById("embedCode").value = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/W1MR_qXV8W0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/W1MR_qXV8W0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>' }
        if (title == "Brain Analysis") { document.getElementById("embedCode").value = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/VvaH1hZ7ajg"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/VvaH1hZ7ajg" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>' }
        if (title == "Charts and Stats") { document.getElementById("embedCode").value = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/tNQSJ2qDvTs"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/tNQSJ2qDvTs" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>' }
        if (title == "THE BOARDROOM PT 2") { document.getElementById("embedCode").value = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/kzjwKws1Pjs"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/kzjwKws1Pjs" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>' }
        if (title == "THE BOARDROOM PT 1") { document.getElementById("embedCode").value = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/aQSRO9ZzlsE"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/aQSRO9ZzlsE" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>' }
        if (title == "TRACED TO BEEP") { document.getElementById("embedCode").value = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/2yE3S7KCKGk"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/2yE3S7KCKGk" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>' }
        if (title == "CONNECTIONS") { document.getElementById("embedCode").value = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/_jokzejIVv4"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/_jokzejIVv4" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>' }
        if (title == "CRISIS ANNOUNCEMENT") { document.getElementById("embedCode").value = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/7YDRUI72Z-0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/7YDRUI72Z-0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>' }
        if (title == "ANIMALS") { document.getElementById("embedCode").value = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/fekElK34L2w&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/fekElK34L2w&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>' }
        if (title == "WHAT IS I.D.S.?") { document.getElementById("embedCode").value = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/-POPvLSyU4s&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/-POPvLSyU4s&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>' }
        if (title == "TASTY FIBER") { document.getElementById("embedCode").value = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/BnI7T4bB_oM&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/BnI7T4bB_oM&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>' }
        if (title == "GRAVITY") { document.getElementById("embedCode").value = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/TSMOXEWJrdI&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/TSMOXEWJrdI&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>' }
        if (title == "BLEEP") { document.getElementById("embedCode").value = '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/jy_57okt1qo&hl=en&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/jy_57okt1qo&hl=en&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>' }
    }
  
    
    //create addthis links
    
    var u=location.href;
	var t=document.title;
	var arr = u.split("localhost");
	
	if (arr.length > 1)
	{
	    u = arr[0] + "www.testurl.com" + arr[1];
	}

    

    document.getElementById("add-facebook").onclick = function(){
        dcsMultiTrack('DCS.dcssip','www.fiberone.com/copingwithdisbelief','DCS.dcsuri','/tellafriend/social_bookmarks','WT.ti','Click_Add_Facebook');
        window.open("http://www.addthis.com/bookmark.php?v=250&pub=&s=facebook&url=" + encodeURIComponent(u)+ "&title=" + encodeURIComponent(title),"newWindow");
    }
    document.getElementById("add-Twitter").onclick = function(){
        dcsMultiTrack('DCS.dcssip','www.fiberone.com/copingwithdisbelief','DCS.dcsuri','/tellafriend/social_bookmarks','WT.ti','Click_Add_Twitter');
        window.open("http://www.addthis.com/bookmark.php?v=250&pub=&s=twitter&url=" + encodeURIComponent(u)+ "&title=" + encodeURIComponent(title),"newWindow");
    }
    document.getElementById("add-Delicious").onclick = function(){
        dcsMultiTrack('DCS.dcssip','www.fiberone.com/copingwithdisbelief','DCS.dcsuri','/tellafriend/social_bookmarks','WT.ti','Click_Add_Delicious');
        window.open("http://www.addthis.com/bookmark.php?v=250&pub=&s=delicious&url=" + encodeURIComponent(u)+ "&title=" + encodeURIComponent(title),"newWindow");
    }
    document.getElementById("add-MySpace").onclick = function(){
        dcsMultiTrack('DCS.dcssip','www.fiberone.com/copingwithdisbelief','DCS.dcsuri','/tellafriend/social_bookmarks','WT.ti','Click_Add_MySpace');
        window.open("http://www.addthis.com/bookmark.php?v=250&pub=&s=myspace&url=" + encodeURIComponent(u)+ "&title=" + encodeURIComponent(title),"newWindow");
    }
    document.getElementById("add-stumbleupon").onclick = function(){
        dcsMultiTrack('DCS.dcssip','www.fiberone.com/copingwithdisbelief','DCS.dcsuri','/tellafriend/social_bookmarks','WT.ti','Click_Add_StumbleUpon');
        window.open("http://www.addthis.com/bookmark.php?v=250&pub=&s=stumbleupon&url=" + encodeURIComponent(u)+ "&title=" + encodeURIComponent(title),"newWindow");
    }
    
    
    document.getElementById("addIDS-facebook").onclick = function(){
        dcsMultiTrack('DCS.dcssip','www.fiberone.com/copingwithdisbelief','DCS.dcsuri','/tellafriend/social_bookmarks_IDS','WT.ti','Click_Add_Facebook');
        window.open("http://www.addthis.com/bookmark.php?v=250&pub=&s=facebook&url=" + encodeURIComponent(u)+ "&title=" + encodeURIComponent(title),"newWindow");
    }
    document.getElementById("addIDS-Twitter").onclick = function(){
        dcsMultiTrack('DCS.dcssip','www.fiberone.com/copingwithdisbelief','DCS.dcsuri','/tellafriend/social_bookmarks_IDS','WT.ti','Click_Add_Twitter');
        window.open("http://www.addthis.com/bookmark.php?v=250&pub=&s=twitter&url=" + encodeURIComponent(u)+ "&title=" + encodeURIComponent(title),"newWindow");
    }
    document.getElementById("addIDS-Delicious").onclick = function(){
        dcsMultiTrack('DCS.dcssip','www.fiberone.com/copingwithdisbelief','DCS.dcsuri','/tellafriend/social_bookmarks_IDS','WT.ti','Click_Add_Delicious');
        window.open("http://www.addthis.com/bookmark.php?v=250&pub=&s=delicious&url=" + encodeURIComponent(u)+ "&title=" + encodeURIComponent(title),"newWindow");
    }
    document.getElementById("addIDS-MySpace").onclick = function(){
        dcsMultiTrack('DCS.dcssip','www.fiberone.com/copingwithdisbelief','DCS.dcsuri','/tellafriend/social_bookmarks_IDS','WT.ti','Click_Add_MySpace');
        window.open("http://www.addthis.com/bookmark.php?v=250&pub=&s=myspace&url=" + encodeURIComponent(u)+ "&title=" + encodeURIComponent(title),"newWindow");
    }
    document.getElementById("addIDS-stumbleupon").onclick = function(){
        dcsMultiTrack('DCS.dcssip','www.fiberone.com/copingwithdisbelief','DCS.dcsuri','/tellafriend/social_bookmarks_IDS','WT.ti','Click_Add_StumbleUpon');
        window.open("http://www.addthis.com/bookmark.php?v=250&pub=&s=stumbleupon&url=" + encodeURIComponent(u)+ "&title=" + encodeURIComponent(title),"newWindow");
    }
}


function initFlash ()
{
    // initialize flash

    var flashvars = {
            xmlPath: "xml/config.xml&skipIntro=" + readCookie("fiber_one_intro_cookie")
        }; 
       
    var params = {
            wmode: "transparent",
            bgcolor: "#d9f2ff",
            scale:  "noscale"
        };
    var attributes = false;
    
    var flashName = "swf/preloader.swf";
   

    if (!swfobject.hasFlashPlayerVersion("9.0.0")) { 

            document.getElementById("flashError").style.display = "block"; 
    } 

    swfobject.embedSWF(flashName,"flashContent","100%", "512", "9.0.0", false , flashvars, params, attributes);



}











function createLinks ()
{
    // create logo link
    document.getElementById("fiberLink").onclick = function() {
         regular_win("http://fiberone.com/"); return false;
    }
    
    // create footer menu link
    var footerLinks = document.getElementById("menulinks");
    
    footerLinks.getElementsByTagName("a").item(0).onclick = function(){
        new_win("http://www.generalmills.com/corporate/privacy/privacy.aspx",800,580); return false;
    }
    footerLinks.getElementsByTagName("a").item(1).onclick = function(){
        new_win("http://consumercontacts.generalmills.com/ConsolidatedContact.aspx?page=http://www.fiberseekers.com",800,580); return false;
    }
    footerLinks.getElementsByTagName("a").item(2).onclick = function(){
        regular_win("http://fiberone.com/"); return false;
    }
    footerLinks.getElementsByTagName("a").item(3).onclick = function(){
        new_win("http://fiberone.com/healthpros/healthpro-moreinfo.asp",580,580); return false;
    }
    
    
    
   
   
}


function full_win(url)
{
    window.open(url,"mywin","width=" + +screen.width + ",height=" + +screen.height );
}

function regular_win(url)
{
    window.open(url,"mywin");
}

function new_win(url, width, height)
{
    window.open(url,"mywin","width=" + width + ",height=" + height + "toolbar=no,location=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,screenX=160,left=150,screenY=150,top=150");
}



function validateFormVideo(videoParam) {

    document.getElementById("submitVideoTitle").onclick = function() {  
        checkNull("videoYourName","Please enter name");
        checkNull("videoFriendName","Please enter name"); 
        validateEmail(document.getElementById("videoYourAddress").value,"Please enter valid email address","videoYourAddress") 
        validateEmail(document.getElementById("videoFriendAddress").value,"Please enter valid email address","videoFriendAddress")
        // return success if valid form
        var valid = 1;
        var videoYourName =         document.getElementById("videoYourName").value;
        var videoFriendName =       document.getElementById("videoFriendName").value;
        var videoYourAddress =      document.getElementById("videoYourAddress").value;
        var videoFriendAddress =    document.getElementById("videoFriendAddress").value;
        
        if (document.getElementById("videoYourName").style.color == "red") {valid=0}
        if (document.getElementById("videoFriendName").style.color == "red") {valid=0}
        if (document.getElementById("videoYourAddress").style.color == "red") {valid=0}
        if (document.getElementById("videoFriendAddress").style.color == "red") {valid=0}
        
        var checked = document.getElementById("sendCopyEmail").checked;
        var BCopy = "F";
        if (checked) {BCopy = "T"}
        
        if (valid) {
            document.getElementById("errorMessageVideo").style.visibility = "hidden";
            document.getElementById("thankyouVideo").style.visibility = "visible";
            document.getElementById("email").style.visibility = "hidden";
            document.getElementById("video").style.visibility = "visible";
            sendEmail("V",BCopy,videoYourName,videoFriendName,videoYourAddress,videoFriendAddress,videoParam);
            }
        else {document.getElementById("errorMessageVideo").style.visibility = "visible";}
    }
    
    
    
}



function validateFormEmail(videoParam) {

    document.getElementById("submitEmailTitle").onclick = function() {  
        checkNull("emailYourName","Please enter name");
        checkNull("emailFriendName","Please enter name"); 
        validateEmail(document.getElementById("emailYourAddress").value,"Please enter valid email address","emailYourAddress") 
        validateEmail(document.getElementById("emailFriendAddress").value,"Please enter valid email address","emailFriendAddress")
        // return success if valid form
        var valid = 1;
        var emailYourName  =        document.getElementById("emailYourName").value;
        var emailFriendName  =      document.getElementById("emailFriendName").value;
        var emailYourAddress  =     document.getElementById("emailYourAddress").value;
        var emailFriendAddress  =   document.getElementById("emailFriendAddress").value;
        
        if (document.getElementById("emailYourName").style.color == "red") {valid=0}
        if (document.getElementById("emailFriendName").style.color == "red") {valid=0}
        if (document.getElementById("emailYourAddress").style.color == "red") {valid=0}
        if (document.getElementById("emailFriendAddress").style.color == "red") {valid=0}
        
        var checked = document.getElementById("sendCopyEmail").checked;
        var BCopy = "F";
        if (checked) {BCopy = "T"}
        
        if (valid) {
            document.getElementById("errorMessageEmail").style.visibility = "hidden";
            document.getElementById("thankyouEmail").style.visibility = "visible";
            document.getElementById("email-site").style.visibility = "hidden";
            document.getElementById("postVideo").style.visibility = "visible";
            sendEmail("H",BCopy,emailYourName,emailFriendName,emailYourAddress,emailFriendAddress,videoParam);
        }
        else {document.getElementById("errorMessageEmail").style.visibility = "visible"; }
    }
    
    
    
}

function checkNull(formField,errMessage) {
    if ((document.getElementById(formField).value).trim() == "") {
        document.getElementById(formField).value = errMessage;
        document.getElementById(formField).style.color = "red";
        document.getElementById(formField).onfocus = function() {
            if (document.getElementById(formField).style.color == "red"){
                document.getElementById(formField).value = "";
                document.getElementById(formField).style.color = "black";
            }
        }
    };
}




function validateEmail(elementValue,errMessage,formField){      
   var emailPattern = /^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$/;
   if  (!(emailPattern.test(elementValue))) { 
        document.getElementById(formField).value = errMessage;
        document.getElementById(formField).style.color = "red";
        document.getElementById(formField).onfocus = function() {
            if (document.getElementById(formField).style.color == "red"){
                document.getElementById(formField).value = "";
                document.getElementById(formField).style.color = "black";
            }
        }
   }; 
 }


//trim
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}



function getParams (variable)
{
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
            return pair[1];
            }
        } 
      return 1;
}



function sendEmail (eType,BCopy,emailYourName,emailFriendName,emailYourAddress,emailFriendAddress,videoParam) {

    
    //send out emai, multiples if supplied
    var i=0;
    var tempemailFriendAddress = emailFriendAddress + ";";
    var tempemailFriendName = emailFriendName + ";";
    var singleEmail = tempemailFriendAddress.split(";");
    var singleFriendName = tempemailFriendName.split(";");
    
    for(i = 0; i < singleEmail.length -1 ; i++)
    {
        parseEmail (eType,BCopy,emailYourName,singleFriendName[i],emailYourAddress,singleEmail[i],videoParam)
       
    }
    


}


function parseEmail (eType,BCopy,emailYourName,emailFriendName,emailYourAddress,emailFriendAddress,videoParam) {

    var xmlhttp;
    if (window.XMLHttpRequest)
      {
      // code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else if (window.ActiveXObject)
      {
      // code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    else
      {
      alert("Your browser does not support XMLHTTP!");
      }
    xmlhttp.onreadystatechange=function()
    {
    if(xmlhttp.readyState==4)
      {
        
     
      }
    }
    

      xmlhttp.open("GET","taf.aspx?EMailType=" + eType + "&BCopy=" + BCopy + "&SName=" + emailYourName + "&RName=" + emailFriendName + "&SEMail=" + emailYourAddress + "&REMail=" + emailFriendAddress + "&videoParam=" + videoParam ,true);
      xmlhttp.send(null);
   
    


}