
// JavaScript Document
// tabs.js - Original code by Justin Palmer available at www.encytemedia.com/effectspack
// Internet Explorer fix by Jake Tracey
 var canchange=true;
 var Btestproducts=new Array();
 var Btestdiscount=new Array();
 
 TabToggle = function(element,tabset,prodID,price,media_type) {
  if(canchange){
  //if(prodID.indexOf('-Custom')!=-1) {
	if($('is_custom').value==1 || prodID.indexOf('-Custom')!=-1){	
		$('optn_3').value='';
		if(media_type=='flash'){
			var flash_movie = getFlashMovieObject("iFrog");
			flash_movie.SetVariable('screen_overlay','');
		}
	}
	//alert(prodID);
	if(media_type=='flash' && tabset==1){
		var flash_movie = getFlashMovieObject("iFrog");
		if(prodID.indexOf('treadz')!=-1){
			flash_movie.SetVariable('simple_case','true');			
		} else {
			flash_movie.SetVariable('simple_case','false');
		}
			flash_movie.SetVariable('wrap_overlay','0xFFFFFF');
			$('optn_1').value='';
			$('type_option_1').innerHTML='(select a color)';
			$('type_option_1').style.background='';
	}
    
  //sets the price of the product
  //var type=prodID.substr(2,1);
  //document.getElementById('price_'+type).innerHTML='$'+price;
  // This gives the active tab its look
  var tabs = $('tabs_'+tabset).getElementsByTagName('li');
  for(j = 0; j < tabs.length; j++) {
    link = tabs[j].getElementsByTagName('a')[0];
    link.id = "";
    tabs[j].id = "";
  }
  element.id = "current"
  element.parentNode.id = "active"
  //Hide all content containers
  contents = document.getElementsByClassName('tabset_content_'+tabset);
  for(var i = 0; i < contents.length; i++) {
    contents[i].style.display = 'none';
  }
  //Extract content container id from href
  tabname = element.getAttribute('href').replace(/^.*\#/,'');
  //alert(tabname);
  ele = $(tabname);
  //Magic Happens
  if(prodID!='') load_options(prodID,'',media_type,'',tabset,price);
  }
}
//loads options
function load_options(prodID,optID,media_type,day,tabset,price){
	var url = "/vsadmin/options_ajax.php";
	var prod_array = prodID.split(',');
	var opt_array = optID.split(',');
	//console.log("Prod Array:"+prod_array);
	//console.log("Opt Array:"+opt_array);
	//alert(tabset);
	if(canchange){
		for(i=0;i<prod_array.length;i++)
		{
			var newProdID = prod_array[i];
			var params = "product_id="+prod_array[i]+"&media_type="+media_type+"&day="+day;
			if(optID!='') {
				params +="&optID="+opt_array[i]
			}
			//alert(params);
			var holder= "holder_"+prod_array[i];
			//alert(holder);
			var ajax = new Ajax.Updater(
				{success: holder},
				url,
				{method: "post", 
				parameters: params,
				evalScripts: true,
				onLoading: function(){  canchange=false;
										Element.show('working_'+newProdID);
									 },
				onComplete: function(){ new Effect.Appear('tab_'+newProdID);
										Element.hide('working_'+newProdID);
										canchange = true;
										initLightbox();
									  }
				})
			
		}
	}
	
	//for(i=0;i<price_array.length;i++)
	//{
		//if(tabset_array[i]!='' && price_array[i]!=''){		
			//set_price_item(tabset_array[i],price_array[i],'','');
		//}
	//}
	//var test=true;
	//window.open('','test','width=100 height=100');
	//alert(url);
	
}
function set_price_item(type,price,prodID,opt_discount,listPrice){
	if(price!=''){
		//alert(type+','+price+','+prodID+','+opt_discount);
		star='';
		//if(type==3) star='**';
		$('optn_disc_'+type).value=opt_discount;
		var index='';
		index=Btestproducts.indexOf(prodID);
		if(index!=-1 && Btestdiscount[index]!=undefined) price=Btestdiscount[index];
		
		if ($('price_'+type)) {
			$('price_'+type).innerHTML = star + '$' + getDiscount(opt_discount,price);
		}
		if ($('listPrice_' + type)) {
			$('listPrice_' + type).innerHTML = star + '$' + getDiscount(opt_discount,listPrice);
		}
		
		//find set price
		//total_disc=0;
		if($('hd_set_price').value!=''){
			var opt_percent=false;
			var total_disc=0;
			var this_opt_disc='';
			var set_price='';
			var set_price_custom='';
			var set_name='';
			for(i=1;i<=3;i++){
				this_opt_disc=$('optn_disc_'+i).value;
				this_opt_type='';
				if(this_opt_disc.indexOf('%')!=-1){
					opt_percent=true;
				}
				this_value=this_opt_disc.replace('%','');
				total_disc+=(this_value*1);21
			}
			if(opt_percent) this_opt_type='%';
			if(total_disc==0) {
				this_opt_type='';
				total_disc='';
			}
			total_disc=total_disc+this_opt_type;
			
			if(type==3){
				if(prodID.indexOf('-Custom')==-1) $('is_custom').value=''; 
				else $('is_custom').value=1;	
			}
			
			set_price=$('hd_set_price').value;
			set_price_custom=$('hd_set_price_custom').value;
			set_name=$('set_name').value;
			if($('is_custom').value==1) set_name+='-Custom';
			index2=Btestproducts.indexOf(set_name);
			if(index2!=-1 && Btestdiscount[index2]!=undefined){
				set_price=Btestdiscount[index2];
				set_price_custom=Btestdiscount[index2];
				//alert(prodID+'='+Btestproducts.indexOf(prodID));
			}
			if($('is_custom').value!=1) $('set_price').innerHTML='$'+getDiscount(total_disc,set_price);
			else $('set_price').innerHTML='$'+getDiscount(total_disc,set_price_custom);
		}
	}
}
function getDiscount(discount,price){
	//alert(discount+'-'+price);
	price=price*1;	
	if(discount!='') {
		if(discount.indexOf('%')!=-1) {
			discount=discount.replace('%','')*1;
			price=price+((price*discount)/100);
		} else price=price+(discount*1);
	} 
	if(price!='')return price.toFixed(2);
	else return price
}
function set_Options(option_id,option_name,voption,option_color,count,prodID,style_ID,media_type,price,opt_discount,listPrice)
{
	//alert(opt_discount);
	var type=count.substr(0,1);
	//alert(price+ type);
	
	if(type==1 && $('shim30')) {
		if(!$('shim30').checked && !$('shim6080').checked){		
			alert('Must select size first before selecting color!');
			$('shim30').focus();
			return;			
		}
	}
	
	//set price
	if(price!='')set_price_item(type,price,prodID,opt_discount,listPrice);
	
	var tabs = document.getElementsByClassName('type_'+type);
	for(var i = 0; i < tabs.length; i++) {    	
		//tabs[i].style.border = '1px solid #ffffff';//sets all the borders to white
		tabs[i].innerHTML='';
  	}
	//$(prodID+'_'+option_name).style.border = '1px dotted #666666';//then set the select to gray
	if (type==3) {
    	img_src='<img src="/images/screen_selected.gif" border="0" alt=""/>';
    } else {
    	img_src='<img style="margin:0px; padding:0px; z-index:1;" width="31" height="31" src="/images/circle_selected.gif" border="0" alt=""/>';
    }
	if (prodID.indexOf('-Custom')!=-1) {
    	img_src='<img src="/images/screen_selected_custom.gif" border="0" alt=""/>';
    }
	if (voption!="") {
    	$(prodID+'_'+voption).innerHTML = img_src;       //change src for selected
    } else {
    	$(prodID+'_'+option_name).innerHTML = img_src;   //change src for selected
    }
	$('optn_'+type).value=option_id;
	$('voptn_'+type).value=voption;
	$('prod_'+type).value=prodID;
	$('type_option_'+type).innerHTML='('+option_name+')';//sets option name
	//$('type_option_'+type).style.background='#7089B1';//sets background of option name
	if(type==1) {
		str_type='wrap_overlay';
		//alert(str_type);
        if(option_color.substr(0,2)=='t_'){
			str_value=option_color;//passes wrap color to flash
		} else {
			str_value='0x'+option_color;//passes wrap color to flash
		}
		str_option='wrap_type';
		shim='';
		if($('shim30') && type!=2 && type!=3) {	 
			if($('shim30').checked) shim=1;
		} 
		if ($('emailfriendwrap')) {		
            $('emailfriendwrap').value=option_id+'|'+prodID+'|'+shim;
        }
		setEmailUrl('wrap_overlay','emailfriendwrap','1');
	}
	else if (type==2) {
		str_type='band_overlay';
		if(option_color.substr(0,2)=='t_'){
			str_value=option_color;//passes wrap color to flash
		} else {
			str_value='0x'+option_color;//passes wrap color to flash
		}
		str_option='band_type';
		if ($('emailfriendband')) {	
			$('emailfriendband').value=option_id;
		}
		setEmailUrl('band_overlay','emailfriendband','2');
	}
	else if (type==3) {
		str_type='screen_overlay';
		//strpos=prodID.indexOf('-');
		//firstnum=prodID.substring(strpos+1);
		str_value=style_ID;//passes category and style to flash
		str_option='screen_type';
		if ($('emailfriendscreen')) {	
			$('emailfriendscreen').value=option_id+'|'+prodID;
		}
		setEmailUrl('screen_overlay','emailfriendscreen','3');
	}
	//alert(media_type);
	if(media_type=='flash') {
		str_option_txt=option_name;
		var flash_movie = getFlashMovieObject("iFrog");
		if(type==1){
			if(prodID.indexOf('treadz')!=-1){
				flash_movie.SetVariable('simple_case','true');			
			} else {
				flash_movie.SetVariable('simple_case','false');
			}
		}
		
        
        
		flash_movie.SetVariable(str_option,str_option_txt);
        
		if (prodID == 'IP3GSiliconeWrapz') { // These use new flash communication method
			if (option_id == 4520) { // Clear
				flash_movie.callGotoClear();
			} else {
				flash_movie.callChangeWrap(str_value);
			}
		} else {
			if (option_id == 4204) {
			
			} else if(option_id >= 4197 && option_id <= 4199) {
				flash_movie.SetVariable('band_overlay',str_value);
				flash_movie.SetVariable('wrap_overlay','0xFFFFFF');
				//alert('in');
			} else if(option_id >= 4195 && option_id <= 4196) {
				flash_movie.SetVariable('band_overlay',str_value);
				flash_movie.SetVariable('wrap_overlay',str_value);
			} else if (option_id == 4492) {
				// Do Nothing
			} else {
				flash_movie.SetVariable(str_type,str_value);
			}
		}
		
	} else if(media_type=='image') {
		
		
		
	}
	
}
function setEmailUrl(type,tbox,numtype){
	var this_href_final='';
	var this_href = '';
	var tboxVal = '';
	if ($(tbox)) {
		tboxVal = $(tbox).value;
	}
	if ($('emailfriend')) {	
        this_href=$('emailfriend').href;
    }
	this_url=this_href.split('?');
	if(this_url[1]!=null) {
		qrystr=this_url[1].split('&');
		for(i=0;i<qrystr.length;i++) {
			//alert(qrystr[i].indexOf(type));
			if(qrystr[i].indexOf(type)!=-1) {
				qrystr[i]='&'+type+'='+$(tbox).value;
				//alert(type);
			} 
			stradd='';
			if(i!=0 && qrystr[i].indexOf('&')==-1) stradd='&';
			this_href_final+=stradd+qrystr[i];
			//alert(i+'='+qrystr[i]);
		}
	}
	//alert(this_href_final.indexOf(type));
	if(this_href_final.indexOf(type)==-1) this_href_final+='&'+type+'='+tboxVal; 
	//alert(this_url[0]+'?'+this_href_final);
	if ($('emailfriend')) {	
        $('emailfriend').href='';
        $('emailfriend').href=this_url[0]+'?'+this_href_final;
    }
	$('product_url').value='/products.php?'+this_href_final;
}
function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  } 
}
function addToCart(type,addcartdiv,wishlist){	
	var complete=true;
	if(type.indexOf(',')!=-1) {
		var cart_array = new Array();
		var prod_id='';
		var str_options='';
		var str_voptions='';
		var comma='';
		var stop_index;
		for(i=1;i<=3;i++) {
			if(type.indexOf(i)!=-1){
			//alert(i+' '+type);
			cart_array = fetchSelected(i,type);
			//alert(cart_array+'='+cart_array[1]);
			if(!cart_array) {complete=false;break;}
			var stop_index=cart_array[1].indexOf('-');
			if(stop_index>0) prod_id+=cart_array[1].substr(0,stop_index);
			else prod_id+=cart_array[1];
			str_options+=comma+cart_array[0];			
			str_voptions+=comma+cart_array[3];
			comma=',';
			}
		}
		//call ajax function that add this to the cart
		//alert(prod_id + str_options);
		if(complete) {
			if($('is_custom').value==1) prod_id+='-Custom';
			//alert(prod_id);
			updateCart(prod_id,str_options,str_voptions,'1','add',addcartdiv,type,wishlist);
		}
	} else {
		cart_array = fetchSelected(type,type);
		if(!cart_array) complete=false; 
		if(complete) {
			//call ajax function that add this to the cart
			//alert(cart_array[0]+'  '+cart_array[1]+'  '+cart_array[2])
			updateCart(cart_array[1],cart_array[0],cart_array[3],cart_array[2],'add',addcartdiv,type,wishlist);
		}
	}
}
function fetchSelected(product,type) {
	var arr = new Array();
	arr[0] = $('optn_'+product).value;
	arr[1] = $('prod_'+product).value;
	if($('qty_'+product)){
		arr[2] = $('qty_'+product).value;
	}else {
		arr[2]=1;
	}
	arr[3] = $('voptn_'+product).value;
	if(type.indexOf(',')!=-1) type='set';
	else {
        if(type.indexOf(1)!=-1) {
        	if($('set_name').value == 'carcharger' || $('set_name').value == 'wallcharger') type='Color';
            else type='Wrap';
        }
		if(type.indexOf(2)!=-1) type='Band';
		if(type.indexOf(3)!=-1) type='Screen';
	}	
	if(arr[0]=='') {
		if(type=='set') {
			alert('You must select one of each for a '+type+'!');
		} else {
			alert('You must select a '+type+'!');
		}
		return false;
	}  
	if(arr[2]=='0') {
		alert('Quantity for a '+type+' must be greater than zero!');
		return false;
	}
	return arr;
}
function addCustomScreen(optn,voptn,did){
	var id = $('prodID').value;
	var qty = $('qty_'+did).value;
	if(id=='') {alert('Please Select An iPod from the drop down!');$('prodID').focus();}
	else {
		var url = "../vsadmin/miniCart.php";
		var params = "id="+id+"&optn="+optn+"&voptn="+voptn+"&mode=add&quant="+qty;
		//alert (params);
		var ajax = new Ajax.Updater(
			{success: "miniCart"},
				url,
				{
					method: "post", 
					parameters: params,
					onLoading: function() { Element.show("miniCartWorking"); },
					onComplete: function() { Element.hide("miniCartWorking"); }
				});
	} 
}
function showCart(addcartdiv,show)
{
	var url = "/vsadmin/miniCart.php";
	var params = "show="+show+"&addcartdiv="+addcartdiv;
	if(show=='FALSE') holder=addcartdiv;
	else holder=addcartdiv+'_holder';
	//alert(holder);
	var ajax = new Ajax.Updater(
			{success: holder},
			url,
			{
				method: "post", 
				parameters: params,
				evalScripts: true,
				onLoading: function() {  Element.show(addcartdiv); },
				onComplete: function() { if(show)Element.hide(addcartdiv+'_Working'); }
			});
}
function delItem(name,addcartdiv)
{
	
	var url = "/vsadmin/miniCart.php";
	var params = "mode=delete&" + name + "=" + name + "&addcartdiv=" +addcartdiv;;
	var ajax = new Ajax.Updater(
			{success: addcartdiv+"_holder"},
			url,
			{
				method: "post", 
				parameters: params,
				evalScripts: true,
				onLoading: function() { Element.show(addcartdiv+'_Working'); },
				onComplete: function() { Element.hide(addcartdiv+'_Working'); }
			});
	showCart('cart_items','FALSE');
}
function updateCart(id,optn,voptn,qty,mode,addcartdiv,type,wishlist)
{
	//alert(optn);
	//new Effect.Highlight(addcartdiv);
	
	//wish list check
	var wish='';
	if($('list_'+id)) {
		wish=($('list_'+id).value);
	}
	var option_array = optn.split(",");
	var str_options='';
	var change_product=false;
	if(optn!=''){
		for(i=0;i<option_array.length;i++) {
			str_options+='&optn'+i+'=' + option_array[i];
			
		}
	} else str_options='';
	var voption_array = voptn.split(",");
	var str_voptions='';
	if(voptn!=''){
		for(i=0;i<voption_array.length;i++) {
			if(voption_array[i]!='') str_voptions+='&voptn'+i+'=' + voption_array[i];
			if(voption_array[i].indexOf('.jpg')!=-1) change_product=true;
		}
	} else str_voptions='';
	//alert(id.indexOf('Custom'));
	//check to add shim for video case
	if($('shim30') && type!=2 && type!=3) {	 
		if($('shim30').checked) id=id.replace(/C/,'CC');
		//alert(id);
	}
	
	
	 
	var Bprice=0;
	if((change_product) && (id.indexOf('Custom')==-1)) id+='-Custom';
	index3=Btestproducts.indexOf(id);
	//alert(id+'='+index3);
	if(index3!=-1 && Btestdiscount[index3]!=undefined) Bprice=Btestdiscount[index3];

	

	var params = "id=" + id + str_options + str_voptions + "&quant=" + qty+ "&mode=" + mode + "&addcartdiv=" +addcartdiv+"&Btestdiscount="+Bprice+"&wish="+wish;
	//alert(params);
	if(wishlist){
		$('add_url').value=params;
		$('pID').value=id;
		$('wishlist').submit();
	} else {
		var url = "/vsadmin/miniCart.php";
		//alert(params);
		var ajax = new Ajax.Updater(
				{success: addcartdiv+"_holder"},
				url,
				{
					method: "post", 
					parameters: params,
					evalScripts: true,
					onLoading: function() { Element.show(addcartdiv);   },
					onComplete: function() { Element.hide(addcartdiv+'_Working');  }
				});
		//Element.show(addcartdiv+'_Working');
		showCart('cart_items','FALSE');
	}
}

function addReview()
{
	var url = "/reviewAdd.php";
	var params = Form.serialize('reviewform');
	//alert(params);
	
	var ajax = new Ajax.Updater(
			{success: "reviewresponse"},
			url,
			{
				method: "post", 
				parameters: params,
				onLoading: function() { Element.show("miniCartWorking"); },
				onComplete: function() { Element.show("reviewresponse");Element.hide("review");Form.reset("reviewform"); }
			});
	
}

function showAllCart(what){
	contents = document.getElementsByClassName('all_cart');
	  for(var i = 0; i < contents.length; i++) {
		contents[i].style.display=what;
	  }
	  
	  Element.toggle('hide_all');
	  Element.toggle('view_all')
}

function searchProducts(search)
{
	
	var url = "/aj/search_products.php";
	var params = "search_site=" + search;
	var ajax = new Ajax.Updater(
		{success: "search_holder"},
		url,
		{
			method: "post", 
			parameters: params,
			onLoading: function() { Element.hide('search_holder'); Element.show('search_working'); },
			onComplete: function() { Element.hide('search_working'); Element.show('search_holder'); }
		});
}