﻿//*****************************************************************************************//
//*******PRELOAD AND ROLLOVER FUNCTIONS START********//

//This function preloads the global navigation elements
var navRoll = false; //Set this variable to false so no global rollovers will work until all preloading is done.
function plGnav()
{
	imgRoll();
	navRoll = true;
}

function uPreload(imgObjName,path,ext)
{
	eval( imgObjName + "a = new Image();" );
	eval( imgObjName + "a.src = '" + path + "/" + imgObjName + "a" + ext + "';");
}
 
//This function works in conjuction with the uPreload function to automatically preload images on a page.
//Images with the "roll" class in the HTML markup will be preloaded using "uPreload" and must be named accordingly.
//Ex. <img src="myimagei.gif" alt="my image alt" class="roll" />
function imgRoll()
{
	var linkArray = document.getElementsByTagName("img");
	for (var loop=0; loop<linkArray.length; loop++)
	{
		imgClassArray = linkArray[loop].className.split(' ');
		for (var i=0; i<imgClassArray.length; i++)
		{
			if (imgClassArray[i] == "roll")
			{
				var imgWithPath = linkArray[loop].src;
				var imgPath = imgWithPath.substring( 0, imgWithPath.lastIndexOf( '/' ));
				var imgName = imgWithPath.substring( imgWithPath.lastIndexOf( '/' ) + 1);
				var imgObjName = imgName.substring( 0, imgName.lastIndexOf( '.' ) ) ;
				var fileExt = imgWithPath.substring(imgWithPath.lastIndexOf('.'), imgWithPath.length); //file extension
				if(imgObjName.substring(imgObjName.length -1)!= 'o')
				{
					uPreload(imgObjName.substring(imgObjName.length - 1,0),imgPath,fileExt);
				}
				linkArray[loop].parentNode.onmouseover=function(){roll(this);};
				linkArray[loop].parentNode.onmouseout=function(){roll(this);};
			}
		}
	}
}
 
//This function is for navigation rollover images; uName is name of graphic, uState is on, off or over state
//This function only works for images that have been preloaded using the "uPreload" function.
//usage: <a href="myPage.aspx" onmouseover="nr('myimage','a')" onmouseout="nr('myimage','i')"><img src="myimagei.gif" alt="my image alt" id="myimage" class="pl" /></a>
function roll(o)
{
	if(navRoll)
	{
	var i,isnode,src,ftype,fsuffix,newsrc,nownode;
	for (i=0;i<o.childNodes.length;i++) // loop through all childNodes
	{
		nownode=o.childNodes[i];
		// if the node is an element and an IMG set the variable and exit the loop
		if(nownode.nodeType==1 && /img/i.test(nownode.nodeName))
		{
			isnode=i;
			break;
		}
	}
	// check src and do the rollover
	src = o.childNodes[isnode].src; //full src with path
	fsrc = src.substring(src.lastIndexOf('.'), 0); //full src with path without file extension
	ftype = src.substring(src.lastIndexOf('.'), src.length); //file extension
	fsuffix = src.substring(src.lastIndexOf('.'), src.length - 5); //type of item ex.'i', 'a' or 'o'
	if(fsuffix == 'a')
	{
		newsrc = fsrc.substring(fsrc.length-1,0)+ 'i' + ftype;
	}
	else if(fsuffix == 'i')
	{
		newsrc = fsrc.substring(fsrc.length-1,0)+ 'a' + ftype;
	}
	else
	{
		newsrc = fsrc.substring(fsrc.length-1,0)+ 'o' + ftype;
	}
	o.childNodes[isnode].src=newsrc;
	}
}

AttachEvent(window, "load", plGnav);

//*******PRELOAD AND ROLLOVER FUNCTIONS END********//
//*****************************************************************************************//

function AttachEvent(elementObj, eventName, eventHandlerFunctionName)
{
  if (elementObj.addEventListener) 
  { // Non-IE browsers
	elementObj.addEventListener(eventName, eventHandlerFunctionName, false);		
  } 
  else if (elementObj.attachEvent) 
  { // IE 6+
	elementObj.attachEvent('on' + eventName, eventHandlerFunctionName);
  } 
  else 
  { // Older browsers 
	var currentEventHandler = elementObj['on' + eventName];
	if (currentEventHandler == null) 
	{
	  elementObj['on' + eventName] = eventHandlerFunctionName;
	} 
	else 
	{
	  elementObj['on' + eventName] = function(e) { currentEventHandler(e); eventHandlerFunctionName(e); }
	}
  }
}


//*******VARIOUS PAGE SPECIFIC EFFECT AND AJAX FUNCTIONS********//
//*****************************************************************************************//
var current_review_id = 0;
var last_selected_review_id = 0;
var previous_review_id = 0;
var next_review_id = 0;
var current_paragraph = 0;
var current_review_page = 0;
var ajax_link = '';
var is_done_blinding = true;
var t = null;
var current_add_to_cart_message_showing_time = 0;
function blind_element(element_id, direction)
{
   if(!is_done_blinding)
   {
       t = setTimeout("blind_element('" + element_id + "','" + direction + "')", 250);
   }
   else
   {
       clearTimeout(t);
       
       if(direction == "up")
       {
           Effect.BlindUp(element_id, {
                beforeStart:  function (effect) {
				    is_done_blinding = false;
			    },
			    afterFinish:  function (effect) {
				    is_done_blinding = true;
		       }, 
		       duration: .5});
       }
       else
       {
	        Effect.BlindDown(element_id, {
	        beforeStart:  function (effect) {
				    is_done_blinding = false;
			    },
			    afterFinish:  function (effect) {
				    is_done_blinding = true;
		       }, 
		       duration: .5});
       }
    }
}

function toggle_element_visibility(element_id)
{
  if($(element_id).style.display == "")
  {
      $(element_id).style.display = "none";
  }
  else 
  {
      $(element_id).style.display = "";
  }
}

function set_element_visibility(element_id, visibility)
{
      $(element_id).style.display = visibility;
}

function set_tab_visibility(element_id, on_image, off_image, visibility)
{
   // toggle_element_visibility(element_id);
    set_element_visibility(element_id, visibility);
    var image_id = element_id + '_image';
	var src = $(image_id).src;
	
	if($(element_id).style.display == "")
	{
	    src = on_image;
	}
	else
	{
	    src = off_image;
	}
	
	 $(image_id).src = src;
}

function set_ajax_link(real_ajax_link)
{
    ajax_link = real_ajax_link.substring(0,real_ajax_link.lastIndexOf('=')+1);
}

function set_current_review_id(id)
{
    if(id != 0)
    {
            
        last_selected_review_id = current_review_id;
        if(last_selected_review_id != 0)
        { 
            $('review_' + last_selected_review_id).className = 'pageNum';
        }
         
        current_review_id = id;
        
        $('review_' + current_review_id).className = 'pageNum_selected';
	}
	
	    current_review_page = $('review_' + current_review_id).childNodes[0].id;
	    current_review_page = current_review_page.substring(current_review_page.lastIndexOf('_') + 1)
	     var next_review_to_show = parseInt(current_review_page) + 1;
         var previous_review_to_show = parseInt(current_review_page) - 1;
         if(previous_review_to_show < 0)
         {
           last_review_id = 0;
           Element.hide('last_review');
         }
         else
         { 
            last_review_id = $('review_number_' + previous_review_to_show).title;
	        Element.show('last_review');
         }
         var element = $('review_number_' + next_review_to_show);
         if(element != null)
         {
            next_review_id = element.title;
            Element.show('next_review');
         }
         else
         {
            next_review_id = 0;
            Element.hide('next_review');
         }
}

function get_review(element_id, review_url)
{
	if(!is_done_blinding)
	{
		t = setTimeout("get_review('" + element_id + "', '" + review_url + "')", 250);
	}
	else
	{
	    set_current_review_id(review_url.substring(review_url.lastIndexOf('=') + 1));
		clearTimeout(t);
		new Ajax.Updater(element_id, review_url, {method: 'get', onComplete:blind_element(element_id, 'down'), asynchronous: true});
	}
}
function show_last_review()
{
    get_review('customer_review', ajax_link + last_review_id);
}

function show_next_review()
{
    get_review('customer_review', ajax_link + next_review_id);
}

function show_next_storyParagraph(element_id, paragraph_count)
{    
    current_paragraph = current_paragraph + 1;
    
    if (current_paragraph + 1 >= paragraph_count)
    {
        document.getElementById("story_morelink").style.visibility = 'hidden';
    }
    
    Effect.Appear(element_id, {from: 0.0, to: 1.0, duration: 0.5});
    new Ajax.Updater(element_id, "/ajax/GetStoryParagraph.ashx?paragraph=" + current_paragraph, {method: 'get', asynchronous: true});
}

function cancel_review(show_element_id, hide_element_id)
{
     $(show_element_id).style.display = '';
     new Effect.BlindUp(hide_element_id);
}

function submit_review(show_element_id, hide_element_id, review_url, group)
{
    var post_body = 'purchasedate=' + escape($('purchasedate').value) + '&name=' + escape($('name').value) + '&location=' + escape($('location').value) + '&body=' + escape($('body').value) + '&title=' + $('title').value + "&group=" + group;
     
     var opt = {
	    // Use POST
	    method: 'post',
	    postBody: post_body,
	    // Handle successful response
	    onSuccess: function(t) {
		    $(show_element_id).style.display = '';
		    new Effect.BlindUp(hide_element_id); 
	    }
    }

   new Ajax.Request(review_url, opt);
}


function clear_review()
{
    $('purchasedate').value = '';
    $('name').value = '';
    $('location').value = '';
    $('body').value = '';
    $('title').value = '';
}

function show_add_to_cart_message(message_element_id, add_button_element_id)
{
    if(current_add_to_cart_message_showing_time < 3500)
    {
        current_add_to_cart_message_showing_time += 250;
        t = setTimeout("show_add_to_cart_message('" + message_element_id + "','" +  add_button_element_id + "')", 250);
    }
    else
    {
        current_add_to_cart_message_showing_time = 0;
        //set_element_visibility(message_element_id, 'none');
        new Effect.Fade(message_element_id);
        //set_element_visibility(add_button_element_id, 'block');
        new Effect.Appear(add_button_element_id);
        clearTimeout(t);
    }
}
function complete_add_to_cart(message_element_id, add_button_element_id)
{
    current_add_to_cart_message_showing_time = 0;
   new Effect.Appear(message_element_id);
   //set_element_visibility(message_element_id, 'block');
  //  new Effect.Highlight(message_element_id);
    show_add_to_cart_message(message_element_id, add_button_element_id);
}

function add_to_cart(message_element_id, add_button_element_id, add_to_cart_url)
{
	set_element_visibility(add_button_element_id, 'none');
	new Ajax.Updater(message_element_id, add_to_cart_url, {method: 'get', onComplete:complete_add_to_cart(message_element_id, add_button_element_id), asynchronous: true});
}

function compareProducts(url)
{    

    new Ajax.Updater('prodCompWrap', url);
 
    
    $('prodCompWrap').toggle();
    $('popoverBlocker').toggle();
}

function hideComparisonList()
{
    $('prodCompWrap').toggle();
    $('popoverBlocker').toggle();
}
function copyShippingToBilling(using_shipping_id, checkbox_id, 
                               firstname, lastname, address1, address2, city, state_value, zip, phone,
                               bill_firstname_id, bill_lastname_id, bill_address1_id, bill_address2_id, bill_city_id, bill_state_id, bill_zip_id, bill_phone_id)
{
  if($(checkbox_id).checked == true)
  {
        $(using_shipping_id).value = 'yes';
        $(bill_firstname_id).value = firstname;
        $(bill_lastname_id).value = lastname;
        $(bill_address1_id).value = address1;
        $(bill_address2_id).value = address2;
        $(bill_city_id).value = city;
        for(var i = 0; i < $(bill_state_id).options.length; i++)
        {
            if($(bill_state_id).options[i].value == state_value)
            {
                    $(bill_state_id).options[i].selected = true;
            }
        }
        
        $(bill_zip_id).value = zip;
        $(bill_phone_id).value = phone;
  }
  else
  { //clear it all out 
        $(using_shipping_id).value = 'no';
        $(bill_firstname_id).value = '';
        $(bill_lastname_id).value = '';
        $(bill_address1_id).value = '';
        $(bill_address2_id).value = '';
        $(bill_city_id).value = '';
        $(bill_state_id).options[1].selected = true;
        $(bill_zip_id).value = '';
        $(bill_phone_id).value = '';
  }
}

function tabSelect(selectedTab)
{
    for(var i = 1; i < 4; i++)
    {
        if(selectedTab == i)
        {
            $('tabContent' + i).show();
            $('tab' + i + 'o').show();
            $('tab' + i + 'i').hide();
        }
        else
        {
            $('tabContent' + i).hide();
            $('tab' + i + 'o').hide();
            $('tab' + i + 'i').show();
        }
    }
}



function submit_newsletter_subscription(entire_form_id, show_element_id, hide_element_id, error_message_id, email_id, newsletter_url)
{
    //validate first
    //[a-zA-Z0-9_\.\-]+?@[a-zA-Z0-9_\-]{2,}?(\.([a-zA-Z0-9_\-]{2,}))+
    var email = $(email_id).value;
    email = email.replace(/^\s+|\s+$/g, '') ;
    if( email != "")
    {
    $(error_message_id).style.display = 'none'; //make sure it is hidden
        var myRegxp = /[a-zA-Z0-9_\.\-]+?@[a-zA-Z0-9_\-]{2,}?(\.([a-zA-Z0-9_\-]{2,}))+/;
        if(myRegxp.test(email))
        {
            $(error_message_id).style.display = 'none'; //make sure it is hidden
            var post_body = 'email=' + escape(email);
             
             var opt = {
	            // Use POST
	            method: 'post',
	            postBody: post_body,
	            // Handle successful response
	            onSuccess: function(t) {
		            $(hide_element_id).style.display = 'none';
		            new Effect.Appear(show_element_id, 
		            {afterFinish:  
		            function (effect) {
				       new Effect.Fade(entire_form_id, {duration:1.5});
		            }});
		            }
            }
            new Ajax.Updater(show_element_id, newsletter_url, opt);
       }
       else
       {
            $(error_message_id).innerHTML = "Email address is invalid.";
            new Effect.Appear(error_message_id);
       }
    }
    else
    {
           $(error_message_id).innerHTML = "Email address is required.";
           new Effect.Appear(error_message_id);
    }
}

function launchProductPlayer(vidUrl)
{
	window.open("/store/productPlayer.aspx?vidUrl=" + vidUrl, "vidPlayer", "toolbar=no,scrollbars=yes,width=640,height=530,resizable=no,menubar=no,location=no,directories=no,statusbar=no");
}

function launchWhatsThis()
{
	window.open("/store/securitycode.aspx", "securityCode", "toolbar=no,scrollbars=yes,width=500,height=400,resizable=no,menubar=no,location=no,directories=no,statusbar=no");
}

function launchArtOfAerobed()
{
	window.open("http://www.artoftheguestbed.com", "artBed", "toolbar=no,scrollbars=yes,width=940,height=600,resizable=no,menubar=no,location=no,directories=no,statusbar=no");
}

function doSearchClick(buttonName,e)
{
    //the purpose of this function is to allow the enter key to 
    //point to the correct button to click.
    var key;

    if(window.event)
        key = window.event.keyCode;     //IE
    else
        key = e.which;     //firefox

    if (key == 13)
    {
        //Get the button the user wants to have clicked
        var btn = document.getElementById(buttonName);
        if (btn != null)
        { 
            //If we find the button click it
            btn.click();
            event.keyCode = 0
        }
    }
}
