// JavaScript Functions

// Global Variables
var previousCommercial = "1";
var previousDiv;
var lastOpenAnswerBoxId = null;	

// Description: tests whether the substring is contained within the string
// Returns: boolean
String.prototype.contains = function (testString){
	if(this.indexOf(testString) > -1){
		return true;
	}
	else{
		return false;
	}
}

// Description: prints browser specific css file links
// Returns: Nothing
function printBrowserCSS()
{
	// get user's browser 
	var userAgentString = navigator.userAgent.toLowerCase();
	
	// only display special styles for Safari and FireFox	
	if(userAgentString.contains("safari") || userAgentString.contains("firefox"))
	{ 
//		document.write('<link rel="stylesheet" type="text/css" href="/CSS/FireFox.css" />');
	}
}

function highlightCommercial(commercial){ // home page 	
	document.getElementById("CommercialLink" + previousCommercial).style.backgroundImage = "url('/Images/Navigation/bg_Commercial.gif')";
	document.getElementById("CommercialLink" + commercial).style.backgroundImage = "url('/Images/Navigation/bg_Commercial_On.gif')";

	// remember active commercial
	previousCommercial = commercial;
}

function checkFor(string){ 
	// returns the starting character position or -1 if not found
	position = UserAgent.indexOf(string) + 1;	
	return position;
}

function displayDiv(divID){ 
	// toggles divs
  	if(divID != previousDiv){  
  		// do not turn off same div
		document.getElementById(divID).style.display = 'block';
		// hide from user
		try
		{
			document.getElementById(previousDiv).style.display = 'none';
		}
		catch(err)
		{
		  /* ignore undefined previousDiv error */
		}
	}
	previousDiv = divID
}
// ** NOTE: Had to keep capitalized due to content in DB **
// Commented function would be better to use, but FAQ in DB must be modified
/*
function DisplayAnswer(elementId, question, answer)
{ // used in FAQs on Product Details page
	// display Answer div
	document.getElementById(elementId).style.display = 'block';
	
	// build content
	var Content = "<div id='Close' onclick=\"document.getElementById('" + elementId + "').style.display='none'\"> X Close </div>";
	Content += "<div class='HeaderQuestion'>" + question + "</div>";
	Content += "<div style='height: 6px;'><!-- line space --></div>";
	Content += answer;
	
	// update "Answer" div content
	document.getElementById(elementId).innerHTML = Content;
}
*/
// function displayAnswer(Question, Answer)
function DisplayAnswer(Question, Answer)
{ // used in FAQs on Product Details page
	// display Answer div
	document.getElementById("Answer").style.display = 'block';
	
	// build content
	var Content = "<div id='Close' onclick=\"document.getElementById('Answer').style.display='none'\"> X Close </div>";
	Content += "<div class='HeaderQuestion'>" + Question + "</div>";
	Content += "<div style='height: 6px;'><!-- line space --></div>";
	Content += Answer;
	
	// update "Answer" div content
	document.getElementById("Answer").innerHTML = Content;
}
	
function displayProductDetail(productDetail){	
  	//alert("previousProductDetail = " + previousProductDetail); // test***
	document.getElementById(previousProductDetail).style.display = 'none';
	document.getElementById(productDetail).style.display = 'block';
	previousProductDetail = productDetail;
}

function hideDiv(divID)
{ // display div initially so search engines can read content
  // hide from user
	document.getElementById(divID).style.display = 'none';
}
	
function highlightTab(tabId){	
	// highlights tab and displays corresponding div
	// unhighlight previous Tab	
	//document.getElementById(previousDivId).style.width = "81px"; // shorten width
	document.getElementById(previousDivId).style.backgroundImage = "url('/images/bg_tab.gif')";
	document.getElementById("detail_"+previousDivId.split("_")[1]).style.display = 'none'; //'none' ; // hide previous div
	
	// highlight Tab
	//document.getElementById(tabId).style.width = "96px"; // widen width
	document.getElementById(tabId).style.backgroundImage = "url('/images/bg_tab_On.gif')";
	document.getElementById("detail_"+tabId.split("_")[1]).style.display = 'block'; // display selected div	
	previousDivId = tabId; // save previous highlight in a global variable
}

function navMouseOver(elementID){
	// skip mouse over function if user is on this page
	if(window.location.toString().toLowerCase().indexOf(elementID.toLowerCase()) < 0){ 
		document.getElementById(elementID + "_On" ).style.display = "inline";
		document.getElementById(elementID + "_Off").style.display = "none" ;	
	}
}
	
function navMouseOut(elementID){	
	 document.getElementById(elementID + "_Off").style.display = "inline";
	 document.getElementById(elementID + "_On" ).style.display = "none" ;	  
}

function playCommercial(commercialNumber){	
	document.getElementById("CommercialContainer").style.visibility = 'visible' ; // display 
	document.getElementById("CommercialContainer").innerHTML = "&nbsp;Loading Commercial " + commercialNumber + "...";
}

function replaceSpace(entry){
	var out = " "; // replace spaces
	var add = "_"; // with an underscore
	var temp = "" + entry; // temporary holder
	
	while (temp.indexOf(out) > -1)
	{	
		pos = temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length));		
	}
	
	return temp;
}

/*
 * ========================================================
 * Form Related Functions - Sign Up, etc.
 * ========================================================
 */

function clearSubscribeField(theFormElement, defaultText){
	if (theFormElement.value == defaultText){
		theFormElement.value = "";
	}
}

function isValidEmail (emailStr){
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)

	if (matchArray==null)
	{
		alert("Email address seems incorrect (check @ and .'s)")
		return false
	}

	var user=matchArray[1]
	var domain=matchArray[2]

	if (user.match(userPat)==null)
	{
		alert("Your username (the part before the '@') doesn't seem to be valid.")
		return false
	}

	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null)
	{
		for (var i=1;i<=4;i++)
		{
			if (IPArray[i]>255)
			{
				alert("Destination IP address is invalid!")
				return false
			}
		}
		return true
	}

	var	domainArray=domain.match(domainPat)
	if (domainArray==null){
		alert("The domain name doesn't seem to be valid.")
		return false
	}

	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3){
		alert("The address must end in a three-letter domain, or two letter country.")
		return false
	}

	if (len<2){
		alert("This address is missing a hostname.")
		return false
	}	

	return true;
}	

//  Subscribe/Unsubscribe validation
function resetSubscribeField(theFormElement, defaultText){
	// if there is nothing in the text area, reset default text
	if (theFormElement.value.length < 1 || theFormElement == ""){
		theFormElement.value = defaultText;
	}			
}
	
function validateSubscribe(theForm){
	// validate friends email
	if (theForm.email.value == ""){
		alert("Please enter your E-Mail address.");
		theForm.subscribeEmail.focus();
		return(false);
	}

	if (isValidEmail(theForm.email.value)){
	}
	else{	
		theForm.email.focus();
		return(false);
	}
	return(true);
}

function validateUnsubscribe(theForm){
	// validate friends email
	if (theForm.unsubscribeEmail.value == ""){
		alert("Please enter your E-Mail address.");
		theForm.unsubscribeEmail.focus();
		return(false);
	}

	if (isValidEmail(theForm.unsubscribeEmail.value)){
		}
	else{	
		theForm.unsubscribeEmail.focus();
		return(false);
	}
	return(true);
}

function showAnswerBox(theElementId){
	if(lastOpenAnswerBoxId != null)	{
		hideAnswerBox(lastOpenAnswerBoxId);
	}

	document.getElementById(theElementId).style.display = "block";
	lastOpenAnswerBoxId = theElementId;
}

function hideAnswerBox(theElementId){
	// Hide the answer box
	document.getElementById(theElementId).style.display = "none";
}

function underlineText(theElement, setUnderline){
	if(setUnderline == true){
		theElement.style.textDecoration = "underline";
	}
	else{
		theElement.style.textDecoration = "none";
	}
}


<!-- jquery homepage slideshow -->
var $$ = $.fn;

$$.extend({
  SplitID : function()
  {
    return this.attr('id').split('-').pop();
  },

  Slideshow : {
    Ready : function()
    {
      $('div.tmpSlideshowControl')
        .hover(
          function() {
            $(this).addClass('tmpSlideshowControlOn');
          },
          function() {
            $(this).removeClass('tmpSlideshowControlOn');
          }
        )
        .click(
          function() {
            $$.Slideshow.Interrupted = true;

            $('div.tmpSlide').hide();
            $('div.tmpSlideshowControl').removeClass('tmpSlideshowControlActive');

            $('div#tmpSlide-' + $(this).SplitID()).show()
            $(this).addClass('tmpSlideshowControlActive');
          }
        );

      this.Counter = 1;
      this.Interrupted = false;

      this.Transition();
    },

    Transition : function()
    {
      if (this.Interrupted) {
        return;
      }

      this.Last = this.Counter - 1;

      if (this.Last < 1) {
        this.Last = 2;
      }

      $('div#tmpSlide-' + this.Last).fadeOut(
        'slow',
        function() {
          $('div#tmpSlideshowControl-' + $$.Slideshow.Last).removeClass('tmpSlideshowControlActive');
          $('div#tmpSlideshowControl-' + $$.Slideshow.Counter).addClass('tmpSlideshowControlActive');
          $('div#tmpSlide-' + $$.Slideshow.Counter).fadeIn('slow');

          $$.Slideshow.Counter++;

          if ($$.Slideshow.Counter > 2) {
            $$.Slideshow.Counter = 1;
          }

          setTimeout('$$.Slideshow.Transition();', 5000);
        }
      );
    }
  }
});

$(document).ready(
  function() {
    $$.Slideshow.Ready();
  }
);
