/*
Effect: When add to cart, popup a floating box saying the item is just added to cart and item info.
Technical Handling: Use PrettyPhoto, submit the cart to the PrettyPhoto iframe.
*/
if( window.cartbutton && !window.original_cartbutton ){
	var original_cartbutton = window.cartbutton;
}
if( !window.cartpopup_width ){
	var cartpopup_width = 745;
}
if( !window.cartpopup_height ){
	var cartpopup_height = 250;
}
var current_addcart_form = null;
var check_addcart_interval = 0;
var $cart = jQuery.noConflict();
var cartbutton = function(){
	$cart("a[rel^='prettyPhoto']").prettyPhoto({
		iframe_markup: '<iframe id="cartbutton_popup_iframe" name="cartbutton_popup_iframe" style="width:100%;border-width:0px;border-collapse:collapse;" frameborder="0" width="100%" height="'+cartpopup_height+'px"></iframe>'
	});
	$cart.prettyPhoto.open('?iframe=true&width='+cartpopup_width+'&height='+cartpopup_height);
	check_addcart_interval = setInterval("check_addcart('Forward_To', null, 'prodPage')", 1000);
}
var cartbuttonoptioncheck = function(){
	$cart("a[rel^='prettyPhoto']").prettyPhoto({
		iframe_markup: '<iframe id="cartbutton_popup_iframe" name="cartbutton_popup_iframe" style="width:100%;border-width:0px;border-collapse:collapse;" frameborder="0" width="100%" height="'+cartpopup_height+'px"></iframe>'
	});
	$cart.prettyPhoto.open('?iframe=true&width='+cartpopup_width+'&height='+cartpopup_height);
	check_addcart_interval = setInterval("check_addcart('Forward_To', null, 'prodPage')", 1000);
}
function check_addcart(form_name, target_url, page_type){
	if( check_addcart_interval > 0 ){
		var d_iframe = document.getElementById("cartbutton_popup_iframe");
		if( d_iframe ){
			if( form_name != null ){
				if( form_name == "form object" ){
					var d_form = current_addcart_form;
				}
				else{
					var d_form = eval("document." + form_name);
				}
				d_form.target = "cartbutton_popup_iframe";
				var d_inputs = d_form.getElementsByTagName("input");
				var has_indicator = false;
				if( d_inputs.length > 0 ){
					for( var i=0; i<d_inputs.length; i++ ){
						if( d_inputs[i].name == "UsePopup" ){
							has_indicator = true;
							d_inputs[i].setAttribute("type","hidden");
							d_inputs[i].value = 1;
							break;
						}
					}
				}
				if( !has_indicator ){
					var indicator = document.createElement("input");
					indicator.name = "UsePopup";
					indicator.setAttribute("type","hidden");
					indicator.value = 1;
					d_form.appendChild(indicator);
				}
			}
			if( page_type == "prodPage" ){
				if( window.original_cartbutton ){
					original_cartbutton();
				}
				else{
					d_form.action = "add_to_cart.cfm";
					d_form.submit();
				}
			}
			else{
				if( form_name != null ){
					d_form.submit();
				}
				else{
					if( target_url.search(/\?/g) < 0 ){
						var target_url_ = target_url + "?UsePopup=1";
					}
					else{
						var target_url_ = target_url + "&UsePopup=1";
					}
					d_iframe.src = target_url_;
				}
			}
			clearInterval(check_addcart_interval);
			check_addcart_interval = 0;	
		}
	}
}
function close_cart_popup(){
	$cart.prettyPhoto.close();
}
function cart_popup_after_close(){
	if( window.location.href.search("/show_cart.cfm") > 0 ){
		window.location.reload();
	}
	else{
		
	}
}
function prodList_cartbutton(form_name, target_url){
	$cart("a[rel^='prettyPhoto']").prettyPhoto({
		iframe_markup: '<iframe id="cartbutton_popup_iframe" name="cartbutton_popup_iframe" style="width:100%;border-width:0px;border-collapse:collapse;" frameborder="0" width="100%" height="'+cartpopup_height+'px"></iframe>',
		callback: function(){cart_popup_after_close();}
	});
	$cart.prettyPhoto.open('?iframe=true&width='+cartpopup_width+'&height='+cartpopup_height);
	if( form_name != null ){
		if( typeof(form_name) == "string" ){/* if form name */
			check_addcart_interval = setInterval("check_addcart('"+form_name+"', null, 'prodList')", 1000);
		}
		else{/* if form object */
			current_addcart_form = form_name;
			check_addcart_interval = setInterval("check_addcart('form object', null, 'prodList')", 1000);
		}
	}
	else{
		check_addcart_interval = setInterval("check_addcart(null, '"+target_url+"', 'prodList')", 1000);
	}
}
function update_IES_minicart(){
	if( ies_is_newtemplate ){
		var divs = document.getElementsByTagName("div");
		var twids = "";
		var pageType = "";
		for( var i=0; i<divs.length; i++ ){
			if( divs[i].getAttribute("iesEleType") == "minicart" ){
				if( twids != "" ){ twids += ","; }
				twids += divs[i].getAttribute("twid");
				pageType = divs[i].getAttribute("pageType");
			}
		}
		var ajaxRequest = new AjaxRequest("update_minicart_ajax.cfm?twid="+twids+"&pageType="+escape(pageType));
		ajaxRequest.sendRequest();
	}
	else{
		var divs = document.getElementsByTagName("div");
		var carteles = "";
		for( var i=0; i<divs.length; i++ ){
			if( divs[i].getAttribute("iesEleType") == "minicart0" ){
				if( carteles != "" ){ carteles += ","; }
				carteles += divs[i].getAttribute("no") + "~" + divs[i].getAttribute("ele");
			}
		}
		var ajaxRequest = new AjaxRequest("update_minicart0_ajax.cfm?carteles="+carteles);
		ajaxRequest.sendRequest();
	}
}
function QtyTextBoxOnEnter(e){
	if( !e ) {
    //if the browser did not pass the event information to the
    //function, we will have to obtain it from the event register
    if( window.event ) {
      //Internet Explorer
      e = window.event;
    } else {
      //total failure, we have no way of referencing the event
      return;
    }
  }
  
  if( typeof( e.keyCode ) == 'number'  ) {
    //DOM
    e = e.keyCode;
  } else if( typeof( e.which ) == 'number' ) {
    //NS 4 compatible
    e = e.which;
  } else if( typeof( e.charCode ) == 'number'  ) {
    //also NS 6+, Mozilla 0.9+
    e = e.charCode;
  } else {
    //total failure, we have no way of obtaining the key code
    return;
  }

  if(e == 13){ // <Enter>
	cartbutton();
	return false;
  }
  return true;
}
