﻿var recipeItem = new Array();
recipeItem[1] = "Strawberry-Cream Angel Food Cake";
recipeItem[2] = "Savory White Chicken Chili";
recipeItem[3] = "Lemon-Blueberry Muffins";
recipeItem[4] = "Greek Turkey Burgers With Tzatziki Saucee";
recipeItem[5] = "Fresh Dill Yogurt Dip";
recipeItem[6] = "Fresh Apple Streusel Pecan Cake";
recipeItem[7] = "Easy Strawberry-Banana Smoothie";
recipeItem[8] = "Dark Chocolate Mousse";
recipeItem[9] = "Creamy Turkey Mushroom Stroganoff";
recipeItem[10] = "Creamy No-Bake Strawberry Pie";
recipeItem[11] = "Chicken Pasta Salad with Creamy Lemon Dressing";
recipeItem[12] = "Blueberry-Orange Smoothie";

var imageNames = new Array();
imageNames[1] = "r47994fp.jpg";
imageNames[2] = "r47988fp.jpg";
imageNames[3] = "r47991fp.jpg";
imageNames[4] = "r47990fp.jpg";
imageNames[5] = "r47986fp.jpg";
imageNames[6] = "r47995fp.jpg";
imageNames[7] = "r47984fp.jpg";
imageNames[8] = "r47993fp.jpg";
imageNames[9] = "r47989fp.jpg";
imageNames[10] = "r47992fp.jpg";
imageNames[11] = "r47987fp.jpg";
imageNames[12] = "r47985fp.jpg";

//-----BEGIN MAIN--------------------------------------------------------------------------
window.onload = function() {
    var section = document.getElementsByTagName("body").item(0).getAttribute("id"); // get section
    createMenus(section); // create navigation
    
    // if flavors page then do
    if (section == "flavors") { productDetail() }
    
    // if recipes page then do
    if (section == "recipes") { 
        
        defaultRecipe(window.location); // displays overlay recipe if specified in url
        document.getElementById("leftscroll").onclick = function() {scroll(-1, parseInt(document.getElementById("currPosition").value))} 
        document.getElementById("rightscroll").onclick = function() {scroll(1, parseInt(document.getElementById("currPosition").value))} 
        document.getElementById("tellaFriend").onclick = function() { sendFriend(""); }
        document.getElementById("display1").onclick = function() { recipeDisplay(getId("recipe-image1")); }
        document.getElementById("display2").onclick = function() { recipeDisplay(getId("recipe-image2")); }
        document.getElementById("display3").onclick = function() { recipeDisplay(getId("recipe-image3")); }
        document.getElementById("display4").onclick = function() { recipeDisplay(getId("recipe-image4")); }
        document.getElementById("display5").onclick = function() { recipeDisplay(getId("recipe-image5")); }
        
    }
    
    if (section == "home") { 
        goButton() // create dynamic go button that takes you randomly to a recipe
        document.getElementById("tellaFriend").onclick = function() { sendFriend("") } 
    } 
    
    if (section == "greek") { 
        document.getElementById("tellaFriend").onclick = function() { sendFriend("") } 
    } 
    

}

//-----END MAIN--------------------------------------------------------------------------

function goButton() {

buttonLink = new Array(6); 
    buttonLink[1] = new Array(1); 
    buttonLink[2] = new Array(1);
    buttonLink[3] = new Array(1);
    buttonLink[4] = new Array(1);
    buttonLink[5] = new Array(1);
    buttonLink[1][0] = "Creamy Turkey Stroganoff";
    buttonLink[1][1] = 9;
    buttonLink[2][0] = "Greek Turkey Burgers";
    buttonLink[2][1] = 4;
    buttonLink[3][0] = "Strawberry-Banana Smoothie";
    buttonLink[3][1] = 7;
    buttonLink[4][0] = "Cream y Chocolate Mousse";
    buttonLink[4][1] = 8;
    buttonLink[5][0] = "Creamy Strawberry Pie";
    buttonLink[5][1] = 10;
    
    var rndNumber= Math.floor(Math.random()*5) + 1;
    var description = buttonLink[rndNumber][0];
    var recipeNumber = buttonLink[rndNumber][1];

    
    document.getElementById("goRecipe").href = "recipes.aspx?" + recipeNumber;
    document.getElementById("goImage").title = description;
    document.getElementById("goImage").alt = description;
    document.getElementById("goImage").src = "images/go-button-" + rndNumber + ".jpg";
    
}


function defaultRecipe(url) {
        
    var recipeNumber = url.search.substring("1");
    var validateRecipe = /^[1-9]/;
    if ((validateRecipe.test(recipeNumber)) && (recipeNumber < 13)) { recipeDisplay(recipeNumber);}
}

function recipeDisplay (recipeNumber) {
   
    document.getElementById("recipe-detail" + recipeNumber).style.display = "block";
    document.getElementById("close-recipe" + recipeNumber).onclick = function() {
        document.getElementById("recipe-detail" + recipeNumber).style.display = "none";
    }
    document.getElementById("ops-print" + recipeNumber).onclick = function() { window.print(); }
    document.getElementById("ops-share" + recipeNumber).onclick = function() {
        document.getElementById("recipe-detail" + recipeNumber).style.display = "none";
        sendFriend(recipeNumber, recipeItem[recipeNumber]);    
        
    }
}

function sendFriend(recipe, title) {
         
        if (recipe != "") {
            document.getElementById("note").innerHTML = "Yoplait Greek is more than just a cup of delicious, protein-packed, 0% fat yogurt. It can be used in your favorite recipes too, making them extra tasty! Share this with a friend." 
            document.getElementById("recipeTitle").style.display = "block";
        } else { 
            document.getElementById("note").innerHTML = "Twice the protein of the leading yogurt, 0% fat, and deliciously rich? A yogurt like this needs to be shared. Email this coupon to your friends so they can dig into the divine taste of Yoplait Greek." 
            if (document.getElementsByTagName("body").item(0).getAttribute("id") == "recipe" ){
                document.getElementById("recipeTitle").style.display = "none";
            }
        }
        document.getElementById("email-holder").style.display = "block";
        document.getElementById("close").onclick = function() { document.getElementById("email-holder").style.display = "none"; }
        document.getElementById("sendEmail").onclick = function() { 
            if (formValidate(recipe,title)) {document.getElementById("email-holder").style.display = "none";}
        ; }
        
}

function scroll(action, currPosition) {
       
    currPosition = currPosition + action;
    if (currPosition < 1) { currPosition = recipeItem.length-1}
    if (currPosition > recipeItem.length-1) { currPosition = 1}
    document.getElementById("currPosition").value = currPosition;

 
    for (var x = 1; x <= 5; x++) { 
   
        document.getElementById("recipe-image" + x).src = "images/" + getFilename(currPosition) ;
        
        document.getElementById("recipe-title" + x).innerHTML = recipeItem[currPosition];
        currPosition = currPosition + 1;
        if (currPosition > recipeItem.length-1) { currPosition = 1}

    }
    

}

function getFilename(itemNumber){
var imageNames = new Array();
imageNames[1] = "r47994fp.jpg";
imageNames[2] = "r47988fp.jpg";
imageNames[3] = "r47991fp.jpg";
imageNames[4] = "r47990fp.jpg";
imageNames[5] = "r47986fp.jpg";
imageNames[6] = "r47995fp.jpg";
imageNames[7] = "r47984fp.jpg";
imageNames[8] = "r47993fp.jpg";
imageNames[9] = "r47989fp.jpg";
imageNames[10] = "r47992fp.jpg";
imageNames[11] = "r47987fp.jpg";
imageNames[12] = "r47985fp.jpg";

return imageNames[itemNumber];

}

function productDetail() {
    document.getElementById("item1").onclick = function() { swapProduct(getFlavorId("mainItem"),getFlavorId("productHolder1"),"productHolder1","titleHolder1"); }
    document.getElementById("item2").onclick = function() { swapProduct(getFlavorId("mainItem"),getFlavorId("productHolder2"),"productHolder2","titleHolder2"); }
    document.getElementById("item3").onclick = function() { swapProduct(getFlavorId("mainItem"),getFlavorId("productHolder3"),"productHolder3","titleHolder3"); }
    
    //display nutritional info
    document.getElementById("seeInfo").onclick = function(){ 
        document.getElementById("nutriInfo" + getFlavorId("mainItem")).style.display = "block"; 
        document.getElementById("closeInfo" + getFlavorId("mainItem")).onclick = function() {
            document.getElementById("nutriInfo" + getFlavorId("mainItem")).style.display = "none";
        }
    }
}


function swapProduct(mainItemId,selectedItemId,selectedItemName,selectedItemTitle) {
    
    currDisplay = new Array(4); 
    currDisplay[1] = new Array(4); 
    currDisplay[2] = new Array(4);
    currDisplay[3] = new Array(4);
    currDisplay[4] = new Array(4);
    
    currDisplay[1][0] = "Strawberry";
    currDisplay[1][1] = "Serving Size<br />Calories<br />Calories from Fat<br />Protein<br />Calcium<br />Vitamin D";
    currDisplay[1][2] = "1 container<br />130<br />0<br />23 % DV<br />30% DV<br />20% DV";
    currDisplay[1][3] = "THE TASTE OF STRAWBERRY KEEPS YOU GOING";
    currDisplay[1][4] = "blah blah blah.";
    

    currDisplay[2][0] = "Blueberry";
    currDisplay[2][1] = "Serving Size<br />Calories<br />Calories from Fat<br />Protein<br />Calcium<br />Vitamin D";
    currDisplay[2][2] = "1 container<br />130<br />0<br />23 % DV<br />30% DV<br />20% DV";
    currDisplay[2][3] = "A BIT OF BLUEBERRY. A LOT OF TASTE.";
    currDisplay[2][4] = "This protein-packed cup of yogurt is 0% fat and full of flavor. Grab your spoon and savor the zest of fresh blueberry in every delicious spoonful.";
    

    currDisplay[3][0] = "Honey Vanilla";
    currDisplay[3][1] = "Serving Size<br />Calories<br />Calories from Fat<br />Protein<br />Calcium<br />Vitamin D";
    currDisplay[3][2] = "1 container<br />130<br />0<br />23 % DV<br />30% DV<br />20% DV";
    currDisplay[3][3] = "ENRICH YOUR DAY WITH HONEY VANILLA";
    currDisplay[3][4] = "Sweet success in a cup! You'll love how hints of honey complement this 0% fat, protein-packed yogurt.";
    

    currDisplay[4][0] = "Plain";
    currDisplay[4][1] = "Serving Size<br />Calories<br />Calories from Fat<br />Protein<br />Calcium<br />Vitamin D";
    currDisplay[4][2] = "1 container<br />100<br />0<br />26 % DV<br />35% DV<br />20% DV";
    currDisplay[4][3] = "ANYTHING BUT PLAIN.";
    currDisplay[4][4] = "Pair this protein-packed, 0% fat yogurt with fruits, honey, granola, and more. Or simply enjoy its thick tangy flavor by itself.";

    document.getElementById("mainItem").src = "images/productLarge-" + selectedItemId + ".jpg";
    document.getElementById("mainTitleFull").src = "images/nut-header-" + selectedItemId + ".gif";
    document.getElementById(selectedItemName).src = "images/productSmall-" + mainItemId + ".jpg";
  document.getElementById("nutrition-title").innerHTML = currDisplay[selectedItemId][0];
 document.getElementById("specDesc").innerHTML = currDisplay[selectedItemId][1];
 document.getElementById("specInfo").innerHTML = currDisplay[selectedItemId][2];
document.getElementById("mainTitleFull").innerHTML = currDisplay[selectedItemId][3];
    }


function getId(holderName) {
    
    var imagePath = document.getElementById(holderName).src;
    var imageName = imagePath.split("images/");
    var itemId = imageName[1];
    var x;
    for (x=1; x < 13; x++ ) {
        if (itemId == getFilename(x)){ return x; break;}
    }

}

function getFlavorId(holderName) {
    
    var imagePath = document.getElementById(holderName).src;
    var imageName = imagePath.split("-");
    var itemId = imageName[1].split(".");
    var x = itemId[0];
    return x;
    
    
}

function createMenus(section) {

    var currSection = "home"
    if (section != "") {currSection = section;}

    document.getElementById("menu-" + currSection).src = "images/menu-" + currSection + "-on.gif";
    
    document.getElementById("menu-home").onmouseover = function() {
        document.getElementById("menu-home").onclick = function() { location.href = "default.aspx";}
        document.getElementById("menu-home").src = "images/menu-home-on.gif";
        if (currSection != "home") {
            document.getElementById("menu-home").onmouseout = function() {
                document.getElementById("menu-home").src = "images/menu-home.gif";
            }
        }
    }        
    document.getElementById("menu-flavors").onmouseover = function() {
        document.getElementById("menu-flavors").onclick = function() { location.href = "flavors.aspx";}
        document.getElementById("menu-flavors").src = "images/menu-flavors-on.gif";
        if (currSection != "flavors") {
            document.getElementById("menu-flavors").onmouseout = function() {
                document.getElementById("menu-flavors").src = "images/menu-flavors.gif";
            }
        }
    }       
    document.getElementById("menu-recipes").onmouseover = function() {
        document.getElementById("menu-recipes").onclick = function() { location.href = "recipes.aspx";}
        document.getElementById("menu-recipes").src = "images/menu-recipes-on.gif";
        if (currSection != "recipes") {
            document.getElementById("menu-recipes").onmouseout = function() {
                document.getElementById("menu-recipes").src = "images/menu-recipes.gif";
            }
        }
    }  
    document.getElementById("menu-greek").onmouseover = function() {
        document.getElementById("menu-greek").onclick = function() { location.href = "greek-style.aspx";}
        document.getElementById("menu-greek").src = "images/menu-greek-on.gif";
        if (currSection != "greek") {
            document.getElementById("menu-greek").onmouseout = function() {
                document.getElementById("menu-greek").src = "images/menu-greek.gif";
            }
        }
    }  
    

}



function send(emailYourName,emailYourAddress,emailFriendAddress,subject) {

    

    var i=0;
    var tempemailFriendAddress = emailFriendAddress + ";";
    var singleEmail = tempemailFriendAddress.split(";");

    
    for(i = 0; i < singleEmail.length -1 ; i++)
    {
        parseEmail (emailYourName,emailYourAddress,singleEmail[i],subject)

    }
    


}


function parseEmail (emailYourName,emailYourAddress,singleEmail,recipe) {

    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?SENDER_NAME=" + emailYourName + "&SENDER_EMAIL=" + emailYourAddress + "&RECIPIENT_EMAIL=" + singleEmail + "&SUBJECT=" + recipe,true);
      xmlhttp.send(null);
   
}






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";
            }
        }
    };
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}



function validateEmail(elementValue,errMessage,formField){      
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   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";
            }
        }
   }; 
 }


function formValidate(recipe,title) {
               
    
        checkNull("emailPerson","Please enter email(s)");
        checkNull("yourEmail","Please enter email"); 
        checkNull("yourName","Please enter name"); 
        validateEmail(document.getElementById("yourEmail").value,"Please enter valid email address","yourEmail")
        // return success if valid form
        var valid = 1;
        if (document.getElementById("emailPerson").style.color == "red") {valid=0}
        if (document.getElementById("yourEmail").style.color == "red") {valid=0}
        if (document.getElementById("yourName").style.color == "red") {valid=0}
        
        var emailPerson = document.getElementById("emailPerson").value;
        var yourEmail = document.getElementById("yourEmail").value;
        var yourName = document.getElementById("yourName").value;
        if (valid) {
            send(yourName,yourEmail,emailPerson,recipe,title);
     
            return true;
        }
        else {
            return false
        }
       
 
    
}
function send(emailYourName,emailYourAddress,emailFriendAddress,subject,title) {

    

    var i=0;
    var tempemailFriendAddress = emailFriendAddress + ";";
    var singleEmail = tempemailFriendAddress.split(";");

    
    for(i = 0; i < singleEmail.length -1 ; i++)
    {
        parseEmail (emailYourName,emailYourAddress,singleEmail[i],subject,title)

    }
    


}


function parseEmail (emailYourName,emailYourAddress,singleEmail,recipe,title) {

    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)
      {
        
     
      }
    }

    var emailType = (recipe == "") ? "C" : "R";

    xmlhttp.open("GET", "taf/default.aspx?SENDER_NAME=" + emailYourName + "&SENDER_EMAIL=" + emailYourAddress + "&TITLE=" + title + "&EMailType=" + emailType + "&RECIPIENT_EMAIL=" + singleEmail + "&RECIPEID=" + recipe , true);
      xmlhttp.send(null);
   
}






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";
            }
        }
    };
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}



function validateEmail(elementValue,errMessage,formField){      
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   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";
            }
        }
   }; 
 }

