﻿function CreateRoundbox(selector, scope)
{
    $(selector).wrap('<div class=\"' + scope + ' dialog\"><div class=\"bd\"><div class=\"c\"><div class=\"s\"></div></div></div></div>');
    $('div.' + scope).prepend('<div class=\"hd\"><div class=\"c\"></div></div>').append('<div class=\"ft\"><div class=\"c\"></div></div>');
}

function OpenChatWindow()
{
   var winUrl     = 'http://admin.instantservice.com/links/5538/8363';
   var winName    = 'custclient';
   var winOptions = 'width=500,height=320,scrollbars=0';
   window.open( winUrl, winName, winOptions );
}

function agents_available() {

    document.getElementById('smartbutton').innerHTML = '<A HREF="" onClick="window.open(\'https://admin.instantservice.com/links/5538/Default\',' + 
    '\'custclient\',\'width=500,height=320,scrollbars=0\');return false;">' + 
    '<IMG SRC="/App_Themes/Aperion/images/rightcol/rightnav_livechat.gif"></A>';
    return true;
    }
    // IF AGENTS ARE NOT AVAILABLE:
    function agents_not_available() 
    {
    document.getElementById('smartbutton').innerHTML = '<A HREF="mailto:customerservice@aperionaudio.com">' + 
    '<IMG SRC="/App_Themes/Aperion/images/rightcol/rightnav_chatna.gif"></A>';
    return true;
    }

  // Instantservice Live Button
  // IF AGENTS ARE AVAILABLE
    function agents_available_cart() 
    {
    document.getElementById('smartbutton').innerHTML = '<A HREF="" onClick="window.open(\'https://admin.instantservice.com/links/5538/Default\',' + 
    '\'custclient\',\'width=500,height=320,scrollbars=0\');return false;">' + 
    '<IMG SRC="/App_Themes/Aperion/images/rightcol/rightnav_cart_livechat.gif"></A>';
    return true;
    }
    // IF AGENTS ARE NOT AVAILABLE:
    function agents_not_available_cart() 
    {
    document.getElementById('smartbutton').innerHTML = '<A HREF="mailto:customerservice@aperionaudio.com">' + 
    '<IMG SRC="/App_Themes/Aperion/images/rightcol/rightnav_cart_chatna.gif"></A>';
    return true;
}

function strltrim()
{
    return this.replace(/^\s+/,'');
}

function strrtrim()
{
    return this.replace(/\s+$/,'');
}

function strtrim()
{
    return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;

function RunAperionMovie(moviePath, flashVars, width, height)
{
    document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="' + width + '" height="' + height + '">\n');
    document.write('\t<param name="allowScriptAccess" value="sameDomain" />\n');
    document.write('\t<param name="movie" value="' + moviePath + '" />\n');
    document.write('\t<param name="quality" value="high" />\n');
    document.write('\t<param name="wmode" value="transparent" />\n');
    document.write('\t<param name="FlashVars" value="' + flashVars + '" />\n');
    document.write('\t<embed src="' + moviePath + '" quality="high" flashvars="' + flashVars + '" pluginspage="http://www.macromedia.com/go/getflashplayer" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '" wmode="transparent"></embed>\n');
    document.write('</object>\n');
}

/* method for client side tabbed interface */
function setActiveTab(sender, tabId)
{
    $(sender).parent().parent().find("li a").removeClass("selected");
    $(sender).addClass("selected");
    setActiveTabContainer(tabId);
}

/* method for client side tabbed interface */
function setActiveTabContainer(tabId) {
    $("#" + tabId).show();
    $("#" + tabId).siblings().hide();
}

function file_name_only(str) { 
	 var slash = '/' 
	 if (str.match(/\\/)) { 
			slash = '\\' 
	 } 
	 sURL = str.substring(str.lastIndexOf(slash) + 1, str.lastIndexOf('.')) 
	 return sURL
}

function setImageOn(SkuImage, CheckBox, ProductImage)
{
  document.getElementById(SkuImage).style.display="inline"; 
  document.getElementById(SkuImage).setAttribute("src", ProductImage);
}

function setImageOff(SkuImage, CheckBox, ProductImage)
{
    var ctrl = document.getElementById(SkuImage);
    ctrl.src = ProductImage;
}

function clearTextBoxes(divTag, checkbox, SubTotal, ProductTotal, AccessoriesTotal, GrandTotal)
{
  if (checkbox.checked == true)
  {
     $("#" + divTag + " input").each (function() {
	    this.checked = false;
	   })
   	
	   checkbox.checked = true;
   	
	   var oldPrice = document.getElementById(SubTotal).innerHTML.replace("$", "").replace(",", "");
	   document.getElementById(SubTotal).innerHTML = formatCurrency(0);
   	
	    var subtotallabel = document.getElementById(SubTotal);
	    var accessorieslabel = document.getElementById(AccessoriesTotal);
	    var productsubtotallabel = document.getElementById(ProductTotal);
	    var grandtotallabel = document.getElementById(GrandTotal);
	    var accessoriessubtotal = accessorieslabel.innerHTML.replace("$", "").replace(",", "");
	    var productsubtotal = productsubtotallabel.innerHTML.replace("$", "").replace(",", "");
	    var grandtotal = grandtotallabel.innerHTML.replace("$", "").replace(",", "");
   	 
	    accessorieslabel.innerHTML = formatCurrency(Math.floor(accessoriessubtotal) - oldPrice);
	    accessoriessubtotal = Math.floor(accessoriessubtotal) - oldPrice;
	    grandtotallabel.innerHTML = formatCurrency(Math.floor(productsubtotal) + Math.floor(accessoriessubtotal));
  }
}

function includeSku(checkbox, price, noneCheckbox, SubTotal, ProductTotal, AccessoriesTotal, GrandTotal)
{
  if (checkbox.checked)
		document.getElementById(noneCheckbox).checked = false;
		
  var subtotallabel = document.getElementById(SubTotal);
  var accessorieslabel = document.getElementById(AccessoriesTotal);
  var productsubtotallabel = document.getElementById(ProductTotal);
  var grandtotallabel = document.getElementById(GrandTotal);
  
  var accessoriessubtotal = accessorieslabel.innerHTML.replace("$", "").replace(",", "");
  var productsubtotal = productsubtotallabel.innerHTML.replace("$", "").replace(",", "");
  var grandtotal = grandtotallabel.innerHTML.replace("$", "").replace(",", "");
  
  var subtotalprice = document.getElementById(SubTotal).innerHTML.replace("$", "").replace(",", "");
  price = price.replace("$", "").replace(",", "");

  if (checkbox.checked)
  {
		subtotallabel.innerHTML = formatCurrency(Math.floor(subtotalprice) + Math.floor(price));
		subtotalprice = Math.floor(subtotalprice) + Math.floor(price);
		accessorieslabel.innerHTML = formatCurrency(Math.floor(price) + Math.floor(accessoriessubtotal));
		accessoriessubtotal = Math.floor(price) + Math.floor(accessoriessubtotal);

		grandtotallabel.innerHTML = formatCurrency(Math.floor(productsubtotal) + Math.floor(accessoriessubtotal));
  } else {
		subtotallabel.innerHTML = formatCurrency(Math.floor(subtotalprice) - Math.floor(price));
		accessorieslabel.innerHTML = formatCurrency(Math.floor(accessoriessubtotal) - Math.floor(price));
		accessoriessubtotal = Math.floor(accessoriessubtotal) - Math.floor(price);
		grandtotallabel.innerHTML = formatCurrency(Math.floor(productsubtotal) + Math.floor(accessoriessubtotal));
  }
  
}
	 
function formatCurrency(num) {
	  num = num.toString().replace(/\$|\,/g,'');
	  if(isNaN(num))
	  num = "0";
	  sign = (num == (num = Math.abs(num)));
	  num = Math.floor(num*100+0.50000000001);
	  cents = num%100;
	  num = Math.floor(num/100).toString();
	  if(cents<10)
	  cents = "0" + cents;
	  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	  num = num.substring(0,num.length-(4*i+3))+','+
	  num.substring(num.length-(4*i+3));
	  return (((sign)?'':'-') + '$' + num + '.' + cents);
 }
 
 function CompareProducts(compareUrl)
{
	var list = ''; var count = 0;
	$("input.CompareCheckBox").each( function() {
		if( this.checked )
		{
			count ++;
			list += this.value + ",";
		}
	});
	if( count>=2 && count<=5)
		document.location.href = compareUrl + list.substring(0, list.length-1);
	else
		alert('You must select at least 2 and no more than 5 items to compare.');
}

function OpenTBWindow(link, title)
{
   TB_show(title,link.href,false);
   link.blur();
   return false;
}

var qsParm = new Array();
function qs()
{
	var query = window.location.search.substring(1);
	var parms = query.split('&');
	for (var i=0; i<parms.length; i++) 
	{
		var pos = parms[i].indexOf('=');
		if (pos > 0) 
		{
		var key = parms[i].substring(0,pos);
		var val = parms[i].substring(pos+1);
		qsParm[key] = val;
		}
	
	}

} 

function UpdateCartQty(qty)
{
    var strS = qty == "1" ? "" : "s";
    $("#promoBarCartQty").html("Your cart has " + qty + " item" + strS);
}

function setImageSrc(imgCtrlId, imgUrl)
{
    $("#" + imgCtrlId).get(0).src = imgUrl;
}

function setMainImage(img)
{
     $("img.MainGalleryImage").hide();
     $("img.MainGalleryImage").attr("src", img.src);
     $("img.MainGalleryImage").attr("alt", img.alt);
     $("img.MainGalleryImage").fadeIn();
}