// Copyright Acro Media Inc. 1998-2009, www.acromediainc.com 

entryPoint = document.referrer;
entryPoint.toLowerCase();
if(entryPoint.indexOf("tonsoftoner") == -1){
	var expires = new Date();
	expires.setTime(expires.getTime() + 3E11);   // about 10 years = "forever"
	setCookie("entryPoint", document.referrer, 0, "/", ".tonsoftoner.com");
}

function openUp(windowURL, windowWidth, windowHeight) {
	var topPos = "";
	var leftPos = "";
	if(!windowWidth) windowWidth = 0;
	if(!windowHeight) windowHeight = 0;
	if(windowWidth == 0){
		if(screen){
			windowWidth = screen.availWidth -10;
		}
		else{
			windowWidth = 790;
		}
		leftPos = ",left=0";
	}
	if(windowHeight == 0){
		if(screen){
			windowHeight = screen.availHeight -30;
		}
		else{
			windowHeight = 790;
		}
		topPos = ",top=0";
	}
	newWin = window.open(windowURL,"acromedia","toolbar=no,menubar=0,width="+windowWidth+",height="+windowHeight+topPos+leftPos+",scrollbars=yes,resizable=no");
}

function load_phplive(){
  var refer = document.URL;
  window.open('http://www.tonsoftoner.com/phplive/request.php?l=tonerlive&x=1&deptid=0&page='+refer, '', 'scrollbars=no,menubar=no,resizable=0,location=no,screenX=50,screenY=100,width=450,height=350');
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = 0 + dc.indexOf(prefix);
	if (begin == -1){
		return 0;
	}
	begin += prefix.length;
	var end = 0 + document.cookie.indexOf(";", begin);
	if (end == -1){
		end = dc.length;
	}
	return unescape(dc.substring(begin, end));
}	

function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
			((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

function checkValue(name){
	if(eval("document.detailsForm."+name+".value") == ""){
		eval("document.detailsForm."+name+".value = '1'");
	}
	else{
		eval("document.detailsForm."+name+".value = ''");
	}

}

function checkBox(name, otherName){
	if(eval("document.detailsForm."+otherName+".value") == ""){
		eval("document.detailsForm."+name+".checked = false");
	}
	else{
		eval("document.detailsForm."+name+".checked = true");
	}
}

function showModels(path, location, initialLoad){
  
  if(path == "/") {
    if(location == "homepage") {    
      $("#modelSearchContainer .machines").attr("disabled", "disabled");
      $("#modelSearchContainer .machines").val("");
    }
    else {
      $("#searchBlock .machines").attr("disabled", "disabled");
      $("#searchBlock .machines").val("");
    }    
    return;
  }

  if (path.length==0){
    return;
  }

  xmlHttp=GetXmlHttpObject();

  if (xmlHttp==null){
    alert ("Your browser does not support AJAX!");
    return;
  }

  var url="/processList.php?q="+path;

  xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState==4){
      
      var htmlCode = xmlHttp.responseText;
      
      if(location == "homepage") {       
        
        $("#modelSearchContainer .sbModel").html(htmlCode);
      }
      else {
        $("#searchBlock .sbModel").html(htmlCode);    
      }  
      
      if (initialLoad == 1) {
        if($("#searchBlock .workingManufacturer").val() && $("#searchBlock .workingManufacturer").val() != "/") {
          showModels($("#searchBlock .workingManufacturer").val(), "header");
        } 
      }
      
    }
  }
  xmlHttp.open("POST",url,true);
  xmlHttp.send(null);
}

function doRedirect(){  
  var manufactureLink = $("#searchBlock .workingManufacturer");
  var modelsLink = $("#searchBlock .machines");

  if($(modelsLink).val() != ""){
    document.location.href = $(modelsLink).val();
  } 
  else if ($(manufactureLink).val() != "" && $(manufactureLink).val() != "/") {
    document.location.href = $(manufactureLink).val().substring(0,$(manufactureLink).val().indexOf(":"));
  }
  else{
    return false;
  }
}

function doHomeRedirect(){
  var manufactureLink = $("#modelSearchContainer .workingManufacturer");
  var modelsLink = $("#modelSearchContainer .machines");

  if($(modelsLink).val() != ""){
    document.location.href = $(modelsLink).val();
  } 
  else if ($(manufactureLink).val() != "" && $(manufactureLink).val() != "/") {
    document.location.href = $(manufactureLink).val().substring(0,$(manufactureLink).val().indexOf(":"));
  }
  else{
    return false;
  }
}

var currentVisible = "";

function setVisible(section) {
  if(currentVisible){
    document.getElementById(currentVisible).style.display="none";
  }
  document.getElementById(section).style.display="block";
  currentVisible = section;
}
// show
function show(theDiv, theHideDiv){
  document.getElementById(theDiv).style.display = 'block';
  document.getElementById(theHideDiv).style.display = 'none';
}

// hide
function hide(theDiv, theHideDiv){
  document.getElementById(theDiv).style.display = 'none';
}

// on page load, do the following:
$(function() {

  // external links 
  $("a[rel='external']").each(function() {
    $(this).attr("target", "_blank");
  });
    
  // current year
  var date = new Date();
  var year = date.getFullYear();  
  $("#insertYear").html(year);  
  
  // submission of shopping cart  
  $('#cartForm').submit(function() {    
      $('#cartForm input[name*="formaction"]').attr("name", "action");  
  });
  
  // manufacturer dropdown  
  $('#modelSearchContainer .workingManufacturer').change(function() {    
      showModels($('#modelSearchContainer .workingManufacturer').val(), "homepage");
    });
    
    $("#searchBlock .workingManufacturer").change(function() {
      showModels($('#searchBlock .workingManufacturer').val(), "header");
    });
    
    if($("#modelSearchContainer .workingManufacturer").val() && $("#modelSearchContainer .workingManufacturer").val() != "/") {
      showModels($("#modelSearchContainer .workingManufacturer").val(), "homepage", 1);
    }    
    else if($("#searchBlock .workingManufacturer").val() && $("#searchBlock .workingManufacturer").val() != "/") {
      showModels($("#searchBlock .workingManufacturer").val(), "header");
  }   
  
  // add to cart buttons
  $(".bAddSelectedToCart").click(function() {
    $("form[name='detailsForm']").submit();
  });  
  
  // hover class for search "Go" button
  $("#sbSearch .bGoGreen").hover(function() {
    $(this).addClass("goHover");
  }, function() {
    $(this).removeClass("goHover");
  });  
  
  
  // product details page tabs  
  $(".productTab").click(function() {
    $(".activeTab").removeClass("activeTab");
    $(".activeContent").removeClass("activeContent");
    
    $(this).addClass("activeTab");
    
    // get the number for the content div we want to display    
    tabNumber = $(this).attr("id");    
    tabNumber = tabNumber.substr(tabNumber.indexOf("-") + 1);
    
    $("#productContent-" + tabNumber).addClass("activeContent");
    
    return false;    
  });  
});
