<!--//

$(document).ready(function(){
	if ($("#alert_message")) {
		$("#alert_message").slideDown("Slow");
	}
	$("table.pretty_table tr:not(tr.row_header):odd").addClass("odd");
	
	goBackLoad();
});


var async = true;
var def_country = 'US';  // Default Country
var alt_country = 'CA'; // Alternate Country
var ALERT_TITLE = "Attention!";
var ALERT_BUTTON_TEXT = "Ok";

if (navigator.appVersion.indexOf("MSIE")!=-1) {
	var bName = 'MSIE';
	var temp = navigator.appVersion.split("MSIE");
	var bVer = parseFloat(temp[1]);
} else {
	var bName = navigator.appName;
	var bVer = parseFloat(navigator.appVersion);
}


//SLIDEHIDE
(function($){$.fn.slidehide=function(settings){var defaults={position:"bottom",hiddenText:"Show",shownText:"Hide",start:"hidden",showBar:true};settings=$.extend(defaults,settings);return this.each(function(){var hide_bar_class="hide_bar_bottom";if(settings.position=="top")hide_bar_class="hide_bar_top";var bar_text=settings.hiddenText;var arrow_class="down";var window_class="hidden display_none";if(settings.start=="shown"){bar_text=settings.shownText;arrow_class="up";window_class="shown";}
$(this).wrapInner('<div class="window '+window_class+'"></div>');var click_bar='<div class="hide_bar '+hide_bar_class+'"><span class="left"></span><p><span style="left:0px;" class="'+arrow_class+'"></span>'+bar_text+'<span style="right:0px;" class="'+arrow_class+'"></span></p><span class="right"></span></div>';if(settings.position=="bottom"){$(this).append(click_bar);}else{$(this).prepend(click_bar);}
$(this).children('.hide_bar').click(function(){var win=$(this).siblings('.window');var hidebar=$(this).children('p');win.stop();if(win.is('.shown')){win.removeClass('shown').addClass('hidden').slideUp('medium');hidebar.html('<span style="left:0px;" class="down"></span>'+settings.hiddenText+'<span style="right:0px;" class="down"></span>');}else{win.removeClass('hidden').addClass('shown').slideDown('medium');hidebar.html('<span style="left:0px;" class="up"></span>'+settings.shownText+'<span style="right:0px;" class="up"></span>');}});if(!settings.showBar){$(this).children('.hide_bar').css('display','none');}});};})(jQuery);

//GOBACK
function goBack(settings){settings=$.extend({text:"to a previous stage.",title:"Go Back",group:"default",display_once:false,position:"top-right",priority:1,expire_time:2678400,url:location.href},settings);var existing_settings=goBackGetCookie();if(existing_settings["priority"]>settings["priority"])return;var today=new Date();var expires_date=new Date(today.getTime()+(settings["expire_time"]*1000));settings['priority']=new Number(settings["priority"]).toFixed(0);if(settings["priority"]>10)settings["priority"]=10;else if(settings["priority"]<1)settings["priority"]=1;var cookie_string="";for(var i in settings){cookie_string=cookie_string+i+"|"+settings[i]+"||";}
cookie_string=cookie_string.substr(0,cookie_string.length-1);document.cookie="go_back="+escape(cookie_string)+"; expires="+expires_date.toGMTString()+"; path=/";}
function goBackLoad(){var i;var settings=goBackGetCookie();if(!settings)return;for(i in window.goBackExemptions){if(window.goBackExemptions[i]=="all"||window.goBackExemptions[i]==settings["group"])return;}
var allowit=false;if(!window.goBackAllows){allowit=true;}else{for(i in window.goBackAllows){if(window.goBackAllows[i]==settings["group"])allowit=true;}}
if(!allowit)return;$.jGrowl.defaults.position=settings["position"];$.jGrowl('<a href="'+settings["url"]+'" onclick="goBackDelete()">Click to go back '+settings["text"]+'</a>',{sticky:true,header:settings["title"],close:goBackDelete});if(settings["display_once"]=="true"){goBackDelete();}}
function goBackExempt(){window.goBackExemptions=new Array();for(var i=0;i<arguments.length;i++){window.goBackExemptions[i]=arguments[i];}}
function goBackAllow(){window.goBackAllows=new Array();for(var i=0;i<arguments.length;i++){window.goBackAllows[i]=arguments[i];}}
function goBackGetCookie(){var cookie_string;var settings=new Array();var ca=document.cookie.split(';');for(var i=0;i<ca.length;i++){if(ca[i].substring(1,8)=="go_back"){cookie_string=unescape(ca[i].substring(9,ca[i].length));}}
if(!cookie_string)return settings;var s=cookie_string.split("||");for(i in s){var se=s[i].split("|");settings[se[0]]=se[1];}
return settings;}
function goBackDelete(e,m,o){var expdate=new Date();expdate.setTime(expdate.getTime()-1);document.cookie="go_back=; expires="+expdate.toGMTString()+"; path=/";}

function getstamp()	{
	date = new Date();
	return date.getTime();
}

function loadDoc(url, postdata, cb_func) {
	var req;
	req = false;
    if (window.XMLHttpRequest) {
    	try { req = new XMLHttpRequest(); } catch(e) { req = false; }
    } else if (window.ActiveXObject) {
       	try { req = new ActiveXObject("Msxml2.XMLHTTP"); } 
		catch(e) { 
			try { req = new ActiveXObject("Microsoft.XMLHTTP"); }
			catch(e) { req = false; }
		}
    }
	if(req) {
		req.open("POST", url, async);
		req.onreadystatechange = function() { 
			if (req.readyState == 4 && req.status == 200 && cb_func != "") {
				eval("cb_"+cb_func+"(req.responseText);");
			}
		}
		req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		req.send(postdata);
	}
}

function stencil_pictures (url) {
	loadDoc(url, "", 'stencilPictures');
}

function cb_stencilPictures (txt) {
	$("#stencil_pictures").html(txt);
}

function selectAll (frm, item) {
	var e = frm.elements;
	var re = new RegExp("^"+item);
	for (i=0;i<e.length;i++) {
		if (e[i].id != '' && e[i].id.match(re)) {
			e[i].checked = true;
		}
	}
}

function del_event (id) {
	if (jsconfirm()) location.href = 'index.php?l=account&d=deletereg&id='+id+'&tab=5';
}

function jsconfirm (note) {
	if (note == undefined) note = 'Are you sure you want to complete this action?';
	if (confirm(note)) {
		return true;
	} else {
		return false;
	}
}

function cb_updatePrice (txt) {
	var result = txt.split("||");
	document.getElementById("product_price").innerHTML = result[0];
	document.getElementById("list_price").innerHTML = result[1];
}

function cb_getResult (txt) {
	document.getElementById("message").innerHTML = txt;
}

function cb_revResult (txt) {
	eval(txt);
}

function cb_payResult (txt) {
	if (txt != "Card OK") {
		document.getElementById("message").innerHTML = txt;
	} else {
		document.forms['payment'].submit();
	}
}

function rowVisibility (strVisibility, intRowIndex) {
	if (navigator.product == "Gecko" && navigator.productSub && navigator.productSub > "20041010" && (navigator.userAgent.indexOf("rv:1.8") != -1 || navigator.userAgent.indexOf("rv:1.9") != -1)) {
		document.getElementById("cc_table").rows[intRowIndex].style.visibility = strVisibility;
	} else if (strVisibility == "visible") {
		if(document.all && document.compatMode && document.compatMode == "CSS1Compat" && !window.opera) {
			document.getElementById("cc_table").rows[intRowIndex].style.display = "block";
		} else if (document.getElementById && document.getElementById("cc_table").rows) {
			document.getElementById("cc_table").rows[intRowIndex].style.display = "table-row";
		}
	} else if (strVisibility == "collapse") {
		document.getElementById("cc_table").rows[intRowIndex].style.display = "none";
	}
}

function cc_type (val) {
	if (val == 'Solo' || val == 'Switch') {
		rowVisibility('visible', 5);
		rowVisibility('visible', 6);
	} else {
		rowVisibility('collapse', 5);
		rowVisibility('collapse', 6);
	}
}

function clearMessages () {
	document.getElementById("message").innerHTML = '';
}

function updatePrice (frm, url) {
	var e = frm.elements;
	var opts = [];
	var post;
	for (i=0; i<e.length; i++) {
		if (e[i].type == 'select-one') {
			opts.push(e[i].name+"="+e[i].value);
		}
		if ((e[i].type == 'radio' || e[i].type == 'checkbox') && e[i].checked) {
			opts.push(e[i].name+"="+e[i].value);
		}
	}
	post = opts.join("&");
	loadDoc(url, post, 'updatePrice');
}

function products_options_cancel (popupid) {
	var url = "http://www.tatstore.com/index.php?l=products_options_cancel";
	var post = $("#options_popup_"+popupid+"_form").serialize();
	
	ajax_json(url, post, "cb_products_options_cancel");
}

function cb_products_options_cancel (ret) {
	
	$(ret["jquery"]).html(ret["button"]);
	
}

function add_cart_stencils (frm, url) {
	var e = frm.elements;
	var opts = [];
	var post;
	for (i=0; i<e.length; i++) {
		opts.push(e[i].name+"="+e[i].value);
	}
	post = opts.join("&");
	
	$("#select_button_"+e[0].value).html("<img src=/themes/classic_red/images/loading_circle_blue.gif alt=Loading />");
	
	ajax_json(url, post, "cb_addcartStencils");
}

function cb_addcartStencils (ret) {
	if (ret["success"] == true) {
		$(".product_tile_item_"+ret["productid"]).addClass("added_cart");
		$("#select_button_"+ret["productid"]).html(ret["remove_button"]);
		
		//remove all existing popups
		$(".jGrowl").remove();
		//cycle through popups that need to be made
		jQuery.each(ret["cat_popups"], function () {
			$.jGrowl('You have '+this+' in your cart.', { sticky: true, header: 'Cart Update' });							  
		});
		
		$(".cart_item_"+ret["cartpid"]).remove();
		$("#table_cart").append(ret["cart_table_listing"]);
		
		$("#table_cart_subtotal").text(ret["subtotal"]);
		
		$("#cart_empty_row").remove();
		
	} else {
		display_choose_options_popup(ret["popup"], ret["popupid"]);
		//window.location = ret["url"];
	}
}

function remove_cart_stencils (url, cid, pid, catid) {
	$("#select_button_"+pid).html("<img src=/themes/classic_red/images/loading_circle_blue.gif alt=Loading />");
	
	ajax_json(url+'&p='+cid+'||'+catid, '', "cb_removecartStencils");
}

function cb_removecartStencils (ret) {
	$(".product_tile_item_"+ret["productid"]).removeClass("added_cart");
	$("#select_button_"+ret["productid"]).html(ret["add_button"]);
	
	$(".jGrowl").remove();
	//cycle through popups that need to be made
	jQuery.each(ret["cat_popups"], function () {
		$.jGrowl('You have '+this+' in your cart.', { sticky: true, header: 'Cart Update' });							  
	});
	
	$(".cart_item_"+ret["cartid"]).remove();
	
	if (ret["emptycart"] != " ") {
		$("#table_cart").html(ret["emptycart"]);
	}
	
	$("#table_cart_subtotal").text(ret["subtotal"]);
}

function affiliate_check_load () {
	var post = "username=";
	loadDoc('http://www.tatstore.com/index.php?l=affiliate_check', post, 'affiliateCheck');
}

function affiliate_check_forum (frm) {
	var e = frm.elements;
	var post = "username="+e[0].value;
	document.getElementById("affiliate_check").innerHTML = "Checking...";
	loadDoc('http://www.tatstore.com/index.php?l=affiliate_check', post, 'affiliateCheck');
}

function cb_affiliateCheck (template) {
	document.getElementById("affiliate_check").innerHTML = template;
}

function affiliate_email (frm) {
	var e = frm.elements;
	var opts = [];
	var post;
	for (i=0; i<e.length; i++) {
		opts.push(e[i].name+"="+e[i].value);
	}
	post = opts.join("&");
	document.getElementById("affiliate_check").innerHTML = "Sending...";
	loadDoc('http://www.tatstore.com/index.php?l=affiliate_email', post, 'affiliateCheck');
}

function check_payment (url) {
	var e = document.forms['payment'].elements;
	var data = [];
	var post;
	for (i=0; i<e.length; i++) {
		if ((e[i].type == 'checkbox' && e[i].checked) || e[i].type != 'checkbox') {
			data.push(e[i].name+"="+e[i].value);
		} else {
		}
	}
	post = data.join("&");
	loadDoc(url, post, 'payResult');
}

function check_payment_financed (url) {
	var e = document.forms['payment'].elements;
	var data = [];
	var post;
	for (i=0; i<e.length; i++) 
		if (e[i].type == 'checkbox' && e[i].checked) {
			
		} else if (e[i].type != 'checkbox') {
			
		} else {
		}
	post = data.join("&");
	loadDoc(url, post, 'payResult');
}

function edit_event (id) {
	loadDoc('index.php?l=edit_registry', 'id='+id, 'revResult');
}

function hold_order () {
	loadDoc('checkout.php?l=hold_order', '', 'revResult');
}

function remove_item (url, id) {
	loadDoc(url, '', 'removeItem');
}

function cb_removeItem (arrin) {
	var arr = arrin.split("||");
	$(".cart_item_"+arr[0]).fadeOut("slow");
	$("#cart_total").text(arr[1]);
	$("#cart_subtotal").text(arr[1]);
	$("#table_cart_itemnum").text(arr[2]);
	$("#table_cart_subtotal").text(arr[1]);
}

function ajaxPost (frm, url, result) {
	if (!result) result = 'getResult';
	var e = frm.elements;
	var data = [];
	var post;
	for (i=0; i<e.length; i++) {
		if ((e[i].type == 'checkbox' && e[i].checked) || e[i].type != 'checkbox') {
			data.push(e[i].name+"="+e[i].value);
		}
	}
	post = data.join("&");
	loadDoc(url, post, result);
}

function pause (form_name, perform) {
	form = document.forms[form_name];
	ajaxPost(form, 'index.php?l=loading', 'revResult');
	setTimeout("ajaxPost(form, '"+perform+"', 'revResult')", 2000);
}

function toggle_country (value, type) {
	if (value != def_country && value != alt_country && value != "") {
		document.getElementById(type+'_other').style.display = 'inline';
		document.getElementById(type+'_state').style.display = 'none';
	} else {
		document.getElementById(type+'_other').style.display = 'none';
		document.getElementById(type+'_state').style.display = 'inline';
	}
}

function reset_forms (items, form) {
	for (x in items) {
		var titem = items[x];
		var fitem = document.forms[form].elements[titem];
		if (fitem.selectedIndex != null)
			fitem.selectedIndex = 0;
		else
			fitem.value = '';
	}
}

function bill_to_ship (form) {
	if (form.elements['userinfo[same]'].checked) {
		form.elements['userinfo[ship_first_name]'].value		= form.elements['userinfo[bill_first_name]'].value;
		form.elements['userinfo[ship_last_name]'].value			= form.elements['userinfo[bill_last_name]'].value;
		form.elements['userinfo[ship_company_name]'].value		= form.elements['userinfo[bill_company_name]'].value;
		form.elements['userinfo[ship_address1]'].value			= form.elements['userinfo[bill_address1]'].value;
		form.elements['userinfo[ship_address2]'].value			= form.elements['userinfo[bill_address2]'].value;
		form.elements['userinfo[ship_city]'].value				= form.elements['userinfo[bill_city]'].value;
		form.elements['userinfo[ship_state]'].selectedIndex		= form.elements['userinfo[bill_state]'].selectedIndex;
		form.elements['userinfo[ship_other]'].value				= form.elements['userinfo[bill_other]'].value;
		form.elements['userinfo[ship_zip]'].value				= form.elements['userinfo[bill_zip]'].value;
		form.elements['userinfo[ship_country]'].selectedIndex	= form.elements['userinfo[bill_country]'].selectedIndex;
		toggle_country(form.elements['userinfo[ship_country]'].value, 'ship');
	}
}

function popupImage (full_url, url) {
	 window.open(full_url+'index.php?l=product_images&'+url,'popupImage','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=100,height=100,screenX=150,screenY=150,top=150,left=150');
}

function reload_image (prefix, num) {
	var stamp = getstamp();
	document.images['verify_image'+num].src = prefix+'include/classes/class.iverify.php?'+stamp;
	if (bName == "MSIE" && bVer == 6) {
		alert('Image Reloaded Successfully!');
	}
}

function toggle_div (fname) {
	if (document.getElementById(fname).style.display == 'block')
		document.getElementById(fname).style.display = 'none';
	else 
		document.getElementById(fname).style.display = 'block';
}

function google_translate (s) {
	var url = escape(window.location);
	var lang = (navigator.language) ? navigator.language : navigator.userLanguage; 
	window.top.location='http://translate.google.com/translate'+'?client=tmpg&hl='+lang+'&u='+url+s.value;
}

//homepage slider
function slide_to (id) {
	var act_now = new Array();
	act_now[id] = $("div.active").attr("id");
	
	if (id == act_now[id].substr(5)) return;
	
	//console.log(act_now);
	//console.log(id);
	$(".button").removeClass("active");
	$("#button_"+id).addClass("active");
	
	$("#"+act_now[id]).removeClass("active");
	$("#pane_"+id).insertAfter("#"+act_now[id]);
	$("#pane_"+id).css("display", "block");
	$("#pane_"+id).addClass("active");
	$("#"+act_now[id]).slideUp(500, function () {
		if ($(this).hasClass("active")) {
			$(this).show();
		}
	});
	$("#"+act_now[id]).css("height", "250px");
	
}

function moresell_add_to_cart (id) {
	var url = "http://www.tatstore.com/index.php?l=moresell_add_to_cart";
	var post = "id="+id;
	
	ajax_json(url, post, "moresell_add_to_cart_ret");
}

function moresell_add_to_cart_ret (ret) {
	if (ret["success"] == "true") {
		
		//update cart table box
		$(".cart_item_"+ret["index"]).remove();
		$("#table_cart").append(ret["cart_addition"]);
		$("#table_cart_subtotal").text(ret["subtotal"]);
		$("#cart_empty_row").remove();
		
		$("#moresell_button_"+ret["id"]).replaceWith(ret["added"]);
	}	
}

function ajax_json (aurl, adata, aretfunc) {
	$.ajax({
	   type: "POST",
	   dataType: "json",
	   url: aurl,
	   data: adata,
	   success: function(msg) {
			eval(aretfunc+'(msg);');
	   }
	});
}

function add_combo_to_cart (comboid) {
	var url = "http://www.tatstore.com/index.php?l=combo_add_to_cart";
	var post = "comboid="+comboid;
	
	ajax_json(url, post, "add_combo_to_cart_ret");
}

function add_combo_to_cart_ret (ret) {
	if (ret["status"] == 'good') {
		//do sidebar stuff
		$("#cart_empty_row").remove();
		$("#table_cart").append(ret["cart_addition"]);
		$("#table_cart_subtotal").text(ret["cart_subtotal"]);
		
		//do main cart stuff
		display_view_cart_stuff(ret);
		
		if (ret["hide_and_mark"] == "yes") combo_item_mark_and_hide(ret["comboid"]);
		
	} else if (ret["status"] == 'options') {
		display_choose_options_popup(ret['popup'], ret["popupid"]);
	}
	if (ret["status"] != 'options') {
		$("#combo_item_"+ret["comboid"]+" .product_combo_button").html(ret["button"]);
	}
}

function display_choose_options_popup (html, popupid) {
	$("body").append(html);
		
	var offsetl=($(window).width()-500)/2;
	var offsett=($(window).height()-400)/2;
	if (offsetl < 0) offsetl = 0;
	if (offsett < 0) offsett = 0;
	
	hs.marginLeft=offsetl;
	hs.marginTop=offsett;
	document.getElementById("option_popup_"+popupid+"_autoclick").onclick();
}

function display_view_cart_stuff (ret) {
	$(".cart_subtotal_row").before(ret["real_cart_addition"]);
	$(".cart_view_combo_item").remove();
	$("#cart_combos_place").after(ret["cart_combo_list"]);
	$("#cart_subtotal").text(ret["cart_subtotal"]);
	$("#cart_total").text(ret["cart_total"]);
}

function products_options_add_to_cart (popupid) {
	var url = "http://www.tatstore.com/index.php?l=choose_options_addtocart";
	var post = $("#options_popup_"+popupid+"_form").serialize();
	
	ajax_json(url, post, "products_options_add_to_cart_ret");
}

function products_options_add_to_cart_ret (ret) {
	//$("#moresell_autoclick, #moresell_window").remove();
	//document.getElementById("option_popup_"+ret["popupid"]+"_autoclick_close").onclick();
	$("#table_cart").append(ret["cart_addition"]);
	$("#table_cart_subtotal").text(ret["cart_subtotal"]);
	$("#cart_empty_row").remove();
	$(ret["jquery"]).html(ret["button"]);
	
	if (ret["type"] == "cart_combos") {
		display_view_cart_stuff(ret);
		combo_item_mark_and_hide(ret["comboid"]);
	}
}

function cart_combos_view_all (numbers) {
	var url = "http://www.tatstore.com/index.php?l=cart_combos_view_all";
	var post = "numbers_todo="+numbers;
	
	ajax_json(url, post, "cart_combos_view_all_ret");
}

function cart_combos_view_all_ret (ret) {
	$("#cart_combos_adver_list .window").append(ret["combos_list"]);
	$("#cart_combos_adver_extra").remove();
}

function combo_item_mark_and_hide (itemid) {
	$("#combo_item_"+itemid).children(":not(h4)").slideUp();
	$("#combo_item_"+itemid+" h4").css("border", 0).css("margin", 0).css("padding", 0);
	$("#combo_item_"+itemid).prepend('<img src="themes/classic_red/images/orange_check_mark.png" alt="Added" class="orange_check" />');
}

//-->
