// JavaScript Document

function onaddtocart_click() 
{	//if button clicked, get current quantity in cart and save in cookie, save click in cookie
	var quantity = document.getElementById("shopping_cart_quantity").innerHTML;
	setCookie("addtocart_quantity",quantity,1);
	setCookie("addtocart_click","T",1);
}

function onpageload_check_addtocart_cookies()
{
	//check for click cookie and quantity cookie
	var click_detected = getCookie("addtocart_click");
	var old_quantity = Number(getCookie("addtocart_quantity"));
	//get current quantity
	var new_quantity = Number(document.getElementById("shopping_cart_quantity").innerHTML);
	//if click detected and quantity increased
	if(new_quantity > old_quantity && click_detected == "T")
	{
		//show popup, delete quantity and click cookie
		setCookie("addtocart_quantity",new_quantity, 1);
		setCookie("addtocart_click","F",1);
		show_addtocart_popup(new_quantity-old_quantity);
	} 
	
}

function show_addtocart_popup(quantity_increasse)
{
	if(quantity_increasse == 1)
	{
		Modalbox.show('/item_added_to_cart_modalbox.html', {title:' ', width: 400,  top:250, height:150, overlayOpacity: .4});var t1 = setTimeout("Modalbox.hide()",20000);
	}
	else if(quantity_increasse > 1)
	{
		Modalbox.show('/items_added_to_cart_modalbox.html', {title:' ', width: 400,  top:250, height:150, overlayOpacity: .4}); var t1 = setTimeout("Modalbox.hide()",20000);
	}
}

function turnImageOn(image)
{
	var img_src = image.src;
	patt = /off\.(jpg|png|gif)/; //off.jpg or off.png or off.gif
	
	if(img_src.match(patt)) {
		image.src=img_src.replace(patt, 'on.' + img_src.match(patt)[1]);
	}
}

function turnImageOff(image)
{
	var img_src = image.src;
	patt = /on\.(jpg|png|gif)/; //on.jpg or on.png or on.gif
	if(img_src.match(patt)) {
		image.src=img_src.replace(patt, 'off.' + img_src.match(patt)[1]);
	}
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toUTCString());
}

function getCookie(c_name)
{
if (document.cookie.length > 0) {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 ;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return ""
}

function switchHTML(id1, id2){
var innerHtml1 = document.getElementById(id1).innerHTML;
var innerHtml2 = document.getElementById(id2).innerHTML;
document.getElementById(id2).innerHTML = innerHtml1;
document.getElementById(id1).innerHTML = innerHtml2;
}

//For additional picture functionality
function switchPicture(pic_id, new_url)
{
	var picture = document.getElementById(pic_id);
	picture.src = new_url;
}

function getUrlParameter(parameter)
{
	var currentURL=window.location.href;
	var URL_array = currentURL.split(/\?/);
	var params = URL_array[1];
	var item_array = params.split(/\&/);
	if(item_array!=null){
	for (i=0; i < item_array.length; i++) {
		if (item_array[i] && item_array[i].match(parameter)) {
			it_arr = item_array[i].split(/=/);
			var item = it_arr[1];
			item = item.replace(/%20/g," ");
			return item; 
		}
	}
	return null;
	}
}

 // this function is needed to work around 
  // a bug in IE related to element attributes
  function hasClass(obj) {
     var result = false;
     if (obj.getAttributeNode("class") != null) {
         result = obj.getAttributeNode("class").value;
     }
     return result;
  }   

 function stripeTable(id) {

    // the flag we'll use to keep track of 
    // whether the current row is odd or even
    var even = false;
  
    // if arguments are provided to specify the colours
    // of the even & odd rows, then use the them;
    // otherwise use the following defaults:
    var evenColor = arguments[1] ? arguments[1] : "#fff";
    var oddColor = arguments[2] ? arguments[2] : "#eee";
  
    // obtain a reference to the desired table
    // if no such table exists, abort
    var table = document.getElementById(id);
    if (! table) { return; }
    
    // by definition, tables can have more than one tbody
    // element, so we'll have to get the list of child
    // &lt;tbody&gt;s 
    var tbodies = table.getElementsByTagName("tbody");

    // and iterate through them...
    for (var h = 0; h < tbodies.length; h++) {
    
     // find all the &lt;tr&gt; elements... 
      var trs = tbodies[h].getElementsByTagName("tr");
      
      // ... and iterate through them
      for (var i = 0; i < trs.length; i++) {

        // avoid rows that have a class attribute
        // or backgroundColor style
        if (! hasClass(trs[i]) &&
            ! trs[i].style.backgroundColor) {
 		  
          // get all the cells in this row...
          var tds = trs[i].getElementsByTagName("td");
        
          // and iterate through them...
          for (var j = 0; j < tds.length; j++) {
        
            var mytd = tds[j];

            // avoid cells that have a class attribute
            // or backgroundColor style
            if (! hasClass(mytd) &&
                ! mytd.style.backgroundColor) {
        
              mytd.style.backgroundColor =
                even ? evenColor : oddColor;
            
            }
          }
        }
        // flip from odd to even, or vice-versa
        even =  ! even;
      }
    }
  }
  
  function custecHandleSurveyResponse(response)
{
	var c_name = "waylft_survey";
	var value = "";
	var expiredays = 60; // number of days after which the cookies will expire
	
	if(response == "Yes")
	{
	value = "Yes";
	setCookie(c_name,value,expiredays);
	Modalbox.show("/what_are_you_looking_for_form.html", {title: 'What are you looking for today? Fill out our 2 question survey!', width: 750, height:700, top:100});	
	}
	
	else if(response == "No")
	{
	value = "No";
	setCookie(c_name,value,expiredays);
	Modalbox.hide();
	}
}

function hideElement(id)
{
var elt = document.getElementById(id);
if(elt)
{
elt.style.display = "none";
}
}
function showElement(id)
{
var elt = document.getElementById(id);
if(elt)
{
elt.style.display = "block";
}
}

function getElementInsideElement(elt, id){
	var elts = elt.getElementsByTagName("input");
	for (var i = 0; i < elts.length; i++) {
		if (elts[i].id == id) 
			return elts[i];
	}
}

function addElementEventListener(element, evt, action, useCapture, removeAfter)
{
	
	listener = function(e){ //e is event, parameter is passed by system
		if (!e) {
			e = window.event;
		} //fix for IE
		else 
		{
			var target = e.target; //element, which has been clicked
		}
		if(!target)
		{
				target = this;
		}//another ie fix
		if(target!=null)
		{
			eval(action); //run whatever action was specified
			
			if(removeAfter && target.removeEventListener)
			{
				element.removeEventListener(evt,listener); //if removeAfter is set to true, remove event listener after action has been evaluated
			}
			else if(removeAfter && target.detachEvent)
			{
				element.detachEvent("on" + evt, listener);
			}			
		}
	}
	if (element.addEventListener) {
		element.addEventListener(evt, listener, useCapture); //works in all browsers, but IE
	}
	else if(element.attachEvent){
		element.attachEvent("on" + evt, listener);	
	}	
}

/*--------------------------------------
 * Library Script, object watch function
--------------------------------------- */
//not sure what this script is for, creates problem with chat
//will disable until determine the purpose of the script
//watches for object properties changes
/*if (!Object.prototype.watch)
    Object.prototype.watch = function (prop, handler) {
        var oldval = this[prop], newval = oldval,
        getter = function () {
            return newval;
        },
        setter = function (val) {
            oldval = newval;
            return newval = handler.call(this, prop, oldval, val);
        };
        if (delete this[prop]) { // can't watch constants
            if (Object.defineProperty) // ECMAScript 5
                Object.defineProperty(this, prop, {
                    get: getter,
                    set: setter
                });
            else if (Object.prototype.__defineGetter__ && Object.prototype.__defineSetter__) { // legacy
                Object.prototype.__defineGetter__.call(this, prop, getter);
                Object.prototype.__defineSetter__.call(this, prop, setter);
            }
        }
    };

// object.unwatch
if (!Object.prototype.unwatch)
    Object.prototype.unwatch = function (prop) {
        var val = this[prop];
        delete this[prop]; // remove accessors
        this[prop] = val;
    };*/
	
addElementEventListener(window, "load", "onpageload_check_addtocart_cookies()", false, true);
