var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

function fixPNG(myImage) 
{
    if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
    {
       var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
	   var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
	   var imgTitle = (myImage.title) ? 
		             "title='" + myImage.title  + "' " : "title='" + myImage.alt + "' "
	   var imgStyle = "display:inline-block;" + myImage.style.cssText
	   var strNewHTML = "<span " + imgID + imgClass + imgTitle
                  + " style=\"" + "width:" + myImage.width 
                  + "px; height:" + myImage.height 
                  + "px;" + imgStyle + ";"
                  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                  + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>"
	   myImage.outerHTML = strNewHTML	  
    }
}

function toggleDisplay(divId) {
	
	var obj;
	obj = document.getElementById(divId);
	
	if (obj.style.display=="none") {
		obj.style.display="inline";
	} else {
		obj.style.display="none";
	}	
}

function toggleDisplays(divId, maxDivId) {
	
	var obj;
	if (document.all)
	{
	    obj = document.all[divId];
	}
	else
	{
	    obj = document.getElementById(divId);
	}  	
	
	for (var i = 1; i <= maxDivId; i++) {
		var mobj;
		if (document.all)
		{
		    mobj = document.all[i];
		}
		else
		{
		    mobj = document.getElementById(i);
		}
		mobj.style.display="none";
	}
	
	if (obj.style.display=="none") {
		obj.style.display="inline";
	} else {
		obj.style.display="none";
	}		
}

function go()
{
	box = document.forms[0].urler;
	destination = box.options[box.selectedIndex].value;
	if (destination) location.href = destination;
}

function write_country_select( sel_type, sel_country ) {
  	var sel = '<select class="short" name="' + sel_type + '[country]" id="country_select" onchange="update_locations_by_country(\'' + sel_type + '\')">';
	sel += '<option value="">Select a Country</option>';
	$.each( countries, function(i, country) {
		( sel_country == country.id ) ? is_sel = " selected" : is_sel = "";
		sel += '<option value="' + country.id + '"' + is_sel + '>' + country.name + '</option>';
	});
	sel += '</select>';
	document.write(sel);
}	
  
function write_region_select( sel_type, sel_region, sel_country ) {
	var c_region = 0;
	var f_region = 0;
  	var sel = '<select class="short" name="' + sel_type + '[region]" id="region_select" onchange="update_locations_by_region(\'' + sel_type + '\')">';
	sel += '<option value="">Select a State / Region</option>';
	$.each( regions, function(i, region) {
		is_sel = "";
		if (sel_region == region.id) {
			f_region = 1;
			is_sel = " selected";
		}
		if (sel_region > 0) {
			if (sel_country > 0) {
				if (sel_country == region.cid) {
					sel += '<option value="' + region.id + '"' + is_sel + '>' + region.name + '</option>';
					c_region++;
				}
			}
		}
	});
	sel += '</select>';
	document.write(sel);
	if ( f_region == 0 ) {
		$('#region_select').attr('disabled', true);
	}
	if (sel_country > 0) {
		if (c_region == 0) {
			$('#select_region').html('');
			$('#region_section').hide();
		}
	}	
}	

function build_region_select_by_country( sel_type, sel_country ) {
	var c_region = 0;
  	var sel = '<select class="short" name="' + sel_type + '[region]" id="region_select" onchange="update_locations_by_region(\'' + sel_type + '\')">';
	sel += '<option value="">Select a State / Region</option>';
	$.each( regions, function(i, region) {
		if (sel_country == region.cid) {
			sel += '<option value="' + region.id + '">' + region.name + '</option>';
			c_region++;
		}
	});
	sel += '</select>';
	if ( c_region > 0 ) {
		$('#select_region').html(sel);
		$('#region_select').removeAttr('disabled');
		$('#region_section').show();
		$('#city_select').attr('disabled', true);
	} else {
		$('#select_region').html('');
		$('#region_section').hide();
		$('#city_select').removeAttr('disabled');
	}
}

function update_locations_by_country( sel_type ) {
	sel_country = $("#country_select").val();
	if ( sel_country == "" ) {
		$('#region_select').attr('disabled', true);
		$('#city_select').attr('disabled', true);
	} else {
		build_region_select_by_country( sel_type, sel_country );
	}
	
}

function update_locations_by_region( sel_type ) {
	sel_region = $("#region_select").val();
	if ( sel_region == '') {
		$('#city_select').attr('disabled', true);
	} else {
		$('#city_select').removeAttr('disabled');
	}
}

function delete_confirm( post_url ) {
	$('#delete_confirm').css('left','40%');
	$('#delete_confirm').fadeIn(200);
	$('#delete_confirm').html('<h3>Are you sure?</h3><br><br><a href="" id="close_confirm" class="button">Cancel</a> <a href="'+post_url+'" class="button">Delete</a>');
	$('a#close_confirm').click(function(){
		$('#delete_confirm').hide('slow');
		return false;
	})
}

var loading_img = $('<img>').attr('src','/adm/theme/images/ajax-loader-16-6.gif').attr('border','0');
function add_product_add_on( sec_id, cat_id, prod_id, prod_addon_id, div_id, count_div_id ) {
$( '#' + div_id ).html( loading_img );
	$.post("/ajax/store/" + sec_id + "/add_product_add_on", { section_id: sec_id, category_id: cat_id, product_id: prod_id, product_addon_id: prod_addon_id }, function(data){
			if(data.message.length > 0 ) {
				$( '#' + div_id ).html(data.message);
			}
			if(data.count.length > 0 ) {
				$( '#' + count_div_id ).html(data.count);
			}
	}, "json");
	return false;
}

 $(function () {
    var tabContainers = $('div.apages > div');
    
    $('div.tabs ul.tabNavigation a').click(function () {
        tabContainers.hide().filter(this.hash).show();
        
        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');
        
        return false;
    }).filter(':first').click();
});

$(document).ready(function(){  
	$('#delete_confirm').hide();

	$('#boxtabnav').hide();
	
	var hdrboxcount = 0;
	var hdrboxContainers = $('h1.boxtab');
	hdrboxContainers.each(function(el) { 
		var tabdiv = $('<div id="edittab_' + hdrboxcount + '" class="edittab"><a href="#" onclick="showbox(' + hdrboxcount + ')">' + $(this).text() + '</a></div>');
		tabdiv.appendTo('div#boxtabnav');
		if (hdrboxcount > 0) {
			$(this).hide();
		}
		hdrboxcount++;	
 	});
 	
 	if (hdrboxcount > 1) {
 		$('#boxtabnav').fadeIn();
 	}	
 	
 	var boxcount = 0;
 	var contentContainers = $('div.contentbox');
 	contentContainers.each(function(el) { 
 		if (boxcount > 0) {
			$(this).fadeOut();
		}
		boxcount++;	
 	});
	
	// set focus on anchor boxtab
	var anchor = jQuery.url.attr("anchor");
		if( anchor ) {
		showbox_name(anchor);
	}
 	
}); 

function showbox_name(t) {
	
	var hdrboxcount = 0;
	var hdrboxfound = 0;
	var hdrboxContainers = $('h1.boxtab');
	hdrboxContainers.each(function(el) { 
		if ($(this).attr('id') == ('boxtab_' + t)) {
			$('#edittab_' + hdrboxcount).addClass("edittab_on");
			$(this).fadeIn();
			hdrboxfound = 1;
		} else {
			$('#edittab_' + hdrboxcount).removeClass("edittab_on");
			$(this).hide();
		}
		hdrboxcount++;
 	});
 	
	var boxcount = 0;
 	var contentContainers = $('div.contentbox');
 	contentContainers.each(function(el) { 
		if ($(this).attr('id') == ('contentbox_' + t)) {
			$(this).fadeIn();
		} else {
			$(this).hide();
		}
		boxcount++;	
 	});   
	
	if (hdrboxfound == 0) {
		$('#edittab_0').addClass("edittab_on");
		$('#edittab_0').fadeIn();
		$('#contentbox_0').show();
		
		var boxcount = 0;
	 	var contentContainers = $('div.contentbox');
		$(contentContainers[0]).fadeIn();
	}
	
}	

function showbox(t) {

	var hdrboxcount = 0;
	var hdrboxContainers = $('h1.boxtab');
	hdrboxContainers.each(function(el) { 
		if (hdrboxcount == t) {
			$('#edittab_' + hdrboxcount).addClass("edittab_on");
			$(this).fadeIn();
		} else {
			$('#edittab_' + hdrboxcount).removeClass("edittab_on");
			$(this).hide();
		}	
		hdrboxcount++;	
 	});
 	
 	var boxcount = 0;
 	var contentContainers = $('div.contentbox');
 	contentContainers.each(function(el) { 
 		if (boxcount == t) {
			$(this).fadeIn();
		} else {
			$(this).hide();
		}	
		boxcount++;	
 	});    
}	


	