var GREEN = 0;
var YELLOW = 1;
var RED = 2;
var browser = null;

var ttoReady = RED;
ttoReady = checkBrowser();
var cookiesReady = setAndCheckCookie(ttoReady); 
if((ttoReady == GREEN || ttoReady == YELLOW) && cookiesReady == RED) {
  ttoReady = RED;
}

function checkBrowser() {
  return isBrowserValid();
}

function isBrowserValid() {
  var version;

  // ------------------- Windows Platform -------------------------------------
  if(navigator.userAgent.indexOf("Win") != -1) {
    //64-bit Windows OS yellow
    if(isWinVista64() || isWinXP64()) {
      return YELLOW;
    }
    
    //NT4, win95 are RED
    if(isWinNT() || isWin95()) {
    	return RED;
    }
    
    // Internet Explorer.
    if(isIEBrowser()) {
      version = getIEVersionNumber();
      browser = navigator.appName + ' ' + version;

      if(isWinVista()) {
        if (version >= 7.0) {
          return GREEN;
        } else {
          return RED;
        }
      }

      if(isWin98()) {
      	if (version >= 6.0 && version < 7.0) {
			return GREEN;
      	} else {
			return RED;
        }
      }	

      if(isWinXP()) {
      	if (version >= 6.0) {
      		return GREEN;
      	} else {
      		return RED;
      	}
      }
      
      if(isWin2K()) {
      	if (version >= 6.0 && version < 7.0) {
			return GREEN;
      	} else {
			return RED;
        }
      }
      
      return RED;      
    } //end IE

    // Firefox
    if(isFirefoxBrowser()) {
    
      if(isFirefox15() || isFirefox20()) {
          return GREEN;
      }

      if(isFirefox3()) {
          return YELLOW;
      }
      
      if(isFirefox10()) {
        version = getFirefox10VersionNumber();
        if(version >= 7) {
          if (isWinVista()) {
            return YELLOW;
          } else {
            return GREEN;
          }
        }
      }
      //is firefox, but unsupported version
      return RED;
    } //end Firefox

    // AOL
    if(isAOLBrowser()) {
      // Support AOL 5.0 or better.
      version = getAOLVersion();
      if(version >= 5 && version < 8) {
        return YELLOW;
      }
            
	  if(version >= 9 && !isWinServer2003()) {
	  	return GREEN;  		
      } else {
		return YELLOW;
      }
    } //end AOL

	if(isSafariBrowser()) {
		return RED;
	}

    // Netscape.
    if(isNetscapeBrowser()) {
      version = getNNVersionNumber();
      browser = navigator.appName + ' ' + version;
      
      if (version >= 8) {
        return GREEN;
      }
    } //end Netscape

    browser = "Unsupported Windows Browser";
    return RED;
  } //end IE

  // ----------------------- Mac Platform -------------------------------------
  if(isMac()) {
  
  	if(!isMacOS10()) {
  		return RED;
  	}
  
    // IE on Mac
    if(isIEBrowser()) {
		return RED; // no longer supported
    }



    // Firefox
    if(isFirefoxBrowser()) {
      if(isFirefox15() || isFirefox20()) {
          return GREEN;
      }

      if(isFirefox3()) {
          return YELLOW;
      }      
      
      if(isFirefox10()) {
        version = getFirefox10VersionNumber();
        if(version >= 7) {
            return GREEN;
        } else {
            return RED;
        }
      }    

      //is firefox, but unsupported version
      return RED;
    } //end Firefox

    // Safari
    if(isSafariBrowser()) {
      version = getSafariBuildVersion();
      
      //Mac OS 10.2
      if(version >= 85.8 && version < 86) {
		return YELLOW;
      } 
      
      //Mac OS 10.3
      if(version >= 312.3 && version < 313) {
		return YELLOW;
      } 
        
	  //Mac OS 10.4        
      if(version >= 412) {
		return GREEN;
      }
      
      //Mac OS 10.5
	  if(version >= 523) {
		return GREEN;
      }
      
      return RED;
    }

    // Camino
    if(isCaminoBrowser()) {
      return YELLOW;
    }

    // AOL
    if(isAOLBrowser()) {
      return RED;
    }

    // Netscape
    if(isNetscapeBrowser()) {
		return RED;
    }

    // Mozilla (Gecko)
    if(isMozillaBrowser()) {
      return RED;
    }

    browser = "Unsupported Mac Browser";
    return RED;
  } //end Mac

  // ----------------------- Linux Platform -----------------------------------
  if(isLinux()) {

    // Firefox
    if(isFirefoxBrowser()) {
      if(isFirefox15() || isFirefox20()) {
          return YELLOW;
      }
      
      if(isFirefox3()) {
          return YELLOW;
      }      
      
      if(isFirefox10()) {
        version = getFirefox10VersionNumber();
        if(version >= 7) {
			return YELLOW;
        }
      }
      //is firefox, but unsupported version
      return RED;
    } //end Firefox  
  
  	browser = "Unsupported Browser";
	return RED;
  }
}



// *****************************************************************************
// ********************** Browsers *********************************************
// *****************************************************************************
function isIEBrowser() {
  if (navigator.appName == "Microsoft Internet Explorer") {
    return true;
  }

  return false;
}

function getIEVersionNumber() {
   var ua = navigator.userAgent;
   var MSIEOffset = ua.indexOf("MSIE ");
   if( MSIEOffset == -1 ) {
      return 0;
   } else {
      return parseFloat(ua.substring(MSIEOffset+5,ua.indexOf(";",MSIEOffset)));
   }
}

function isIEWOW64() {
  var ua = navigator.userAgent.toLowerCase();
  if(ua.indexOf("WOW64")) {
    return true;
  }

  return false;
}

function isNetscapeBrowser() {
  if (navigator.appName == "Netscape") {
    return true;
  }

  return false;
}

function getNNVersionNumber() {
   var appVer = parseFloat(navigator.appVersion);
   if(appVer < 5)
      return appVer;
      
   if(typeof navigator.vendorSub != "undefined") {
      var ver = parseFloat(navigator.vendorSub);
      if( !isNaN(ver) )
         return ver;
   }
   return 0;
}

function getFirefox10VersionNumber() {
  if(!isFirefox10())
    return -1;
  
  var ua = navigator.userAgent;
  var offset = ua.indexOf("Firefox/");
  var version = ua.substring(offset+12, offset+13);
  return version;
}

function isFirefox10() {
  var ua = navigator.userAgent;
  var offset = ua.indexOf("Firefox/");
  if(offset == -1)
    return false;
    
  var full = ua.substring(offset+8);
  var version = ua.substring(offset+8, offset+11);
  if(version == 1.0)
    return true;
    
  return false;
}

function isFirefox15() {
  var ua = navigator.userAgent;
  var offset = ua.indexOf("Firefox/");
  if(offset == -1)
    return false;
    
  var full = ua.substring(offset+8);
  var version = ua.substring(offset+8, offset+11);
  if(version == 1.5)
    return true;
    
  return false;
}

function isFirefox20() {
  var ua = navigator.userAgent;
  var offset = ua.indexOf("Firefox/");
  if(offset == -1)
    return false;
    
  var full = ua.substring(offset+8);
  var version = ua.substring(offset+8, offset+9);
  if(version == 2)
    return true;
  
  return false;
}

function isFirefox3() {
  var ua = navigator.userAgent;
  var offset = ua.indexOf("Firefox/");
  if(offset == -1)
    return false;
    
  var full = ua.substring(offset+8);
  var version = ua.substring(offset+8, offset+9);
  if(version == 3)
    return true;
  
  return false;
}

function isAOLBrowser() {
  return (navigator.userAgent.toLowerCase().indexOf("aol") != -1);
}

function getSafariBuildVersion() {
  var ua = navigator.userAgent;
  var offset = ua.indexOf("Safari/");
  if(offset == -1)
    return -1;

  return parseFloat(ua.substring(offset+7));
}

function getAOLVersion() {
  var agent = navigator.userAgent.toLowerCase();
  var aol = "aol";
  var aolOffset = agent.indexOf(aol);

  if(aolOffset == -1) {
    // This is not an AOL browser.
    return 0;
  }

  // Get the version based on the fact that the string must have something like
  // "AOL 7.0" in it.
  var ver = agent.charAt(aolOffset + aol.length + 1);
  return ver;
}

function isFirefoxBrowser() {
   if(navigator.userAgent.indexOf("Firefox") != -1) {
      browser += " (Firefox browser)";
      return true;
   }

   return false;
}


function isSafariBrowser() {
   if( navigator.userAgent.indexOf("Safari") != -1 ) {
      browser += " (Safari browser)";
      return true;
   }

   return false;
}


function isCaminoBrowser() {
   if( navigator.userAgent.indexOf("Camino") != -1 ) {
      browser += " (Camino browser)";
      return true;
   }

   return false;
}


function isMozillaBrowser() {
  var agentStr = navigator.userAgent.toLowerCase();
  if (agentStr.indexOf("mozilla") != -1
      && agentStr.indexOf("gecko") != -1) {
    browser += " (Mozilla browser)";
    return true;
  }

  return false;
}



// *****************************************************************************
// ********************** Operating Systems ************************************
// *****************************************************************************
function isWin95() {
  var agentStr = navigator.userAgent.toLowerCase();
  if (agentStr.indexOf("win95") != -1
      || agentStr.indexOf("windows 95") != -1) {
    return true;
  }

  return false;
}

function isWin98() {
  var agentStr = navigator.userAgent.toLowerCase();
  //checks for both win98 and winME
  if ((agentStr.indexOf("98") != -1) || (agentStr.indexOf("win 9x 4.90") != -1)) {
    return true;
  }

  return false;
}

function isWinNT() {
  // Note no space between NT and 4.0!
  var agentStr = navigator.userAgent.toLowerCase();

  if (agentStr.indexOf("winnt4.0") != -1
      || agentStr.indexOf("windows nt 4.0") != -1) {
    return true;
  }

  return false;
}

function isWin2K() {
 if (navigator.userAgent.toLowerCase().indexOf("windows nt 5.0") != -1) {
    return true;
 }

 return false;
}

function isWinXP() {
  if (navigator.userAgent.toLowerCase().indexOf("windows nt 5.1") != -1) {
    return true;
  }

  return false;
}

function isWinXP64() {
  if (navigator.userAgent.toLowerCase().indexOf("windows nt 5.2") != -1) {
    return true;
  }

  return false;
}  

function isWinServer2003() {
  if (navigator.userAgent.toLowerCase().indexOf("windows nt 5.2") != -1) {
    return true;
  }

  return false;
}  

function isWinVista() {
  if (navigator.userAgent.toLowerCase().indexOf("windows nt 6.0") != -1) {
    return true;
  }

  return false;
}  
function isWinVista64() {
  if(isWinVista() && (navigator.userAgent.toLowerCase().indexOf("win64") != -1)) {
    return true;
  }
  
  return false;
}

function isMac() {
  if (navigator.userAgent.toLowerCase().indexOf("mac") != -1) {
    return true;
  }
  return false;
}

// Be careful with this function.  Some browsers also report OS 9 as macppc.
function isMac10() {
  if (navigator.platform.toLowerCase().indexOf("macppc") != -1) {
    return true;
  }
  return false;
}

function isMacOS10() {
  if (navigator.userAgent.toLowerCase().indexOf("mac os x") != -1) {
    return true;
  }
  return false;
}

function isLinux() {
  if (navigator.userAgent.toLowerCase().indexOf("linux") != -1) {
    return true;
  }
  return false;
}

function setAndCheckCookie(ttoReady) {
  setCookie('ttoReady', ttoReady, '', '/', 'turbotax.intuit.com', '');
  if(!getCookie('ttoReady'))
    return RED;
  
  return GREEN;
}

function updateGetStartedJSFlashVariable(productId) {
  productIdParam = "";
  if(productId != "" && (productId == 2 || productId == 4 || productId == 8 || productId == 16 || productId == 32 || productId == 512)) {
	productIdParam = "%26productid=" + productId;
  }
  
  //no need for continue logic for flash variables
  switch(ttoReady) {
    case RED:
	case YELLOW:
	  return "/system_requirements_check.jsp?priorityCode=" + priorityCode + productIdParam;

    case GREEN:
	  return ttoServer + "?customerSource=" + priorityCode + productIdParam;
	}
}

function updateGetStartedJSVariable(productId) {
  productIdParam = "";
  if(productId != "" && (productId == 2 || productId == 4 || productId == 8 || productId == 16 || productId == 32 || productId == 512)) {
	productIdParam = "&productid=" + productId;
  }
    
  switch(ttoReady) {
    case RED:
	case YELLOW:
		if(window.location.search.indexOf('continue=true') != -1) {
			return "/system_requirements_check.jsp?continue=true&priorityCode=" + priorityCode + productIdParam;
		} else {
			return "/system_requirements_check.jsp?priorityCode=" + priorityCode + productIdParam;
		}

    case GREEN:
		ttoStartPage = "";
      	if(window.location.search.indexOf('continue=true') != -1) {
      		return ttoServer + "open/registration/SignIn.htm?customerSource=" + priorityCode + productIdParam;
		} else {
			return ttoServer + "?customerSource=" + priorityCode + productIdParam;
		}
	  	
	}
}

function updateGetStartedButtons() {
  switch(ttoReady) {
    case RED:
		case YELLOW:    
      $(".getstarted_free").attr("href","/system_requirements_check.jsp?productid=512");
      $(".getstarted_basic").attr("href","/system_requirements_check.jsp?productid=2");
      $(".getstarted_deluxe").attr("href","/system_requirements_check.jsp?productid=16");
      $(".getstarted_premier").attr("href","/system_requirements_check.jsp?productid=8");
      $(".getstarted_home_and_business").attr("href","/system_requirements_check.jsp?productid=32");
      $(".getstarted_homebiz").attr("href","/system_requirements_check.jsp?productid=32");
      $(".getstarted_basic_ffaups").attr("href","/system_requirements_check.jsp?productid=2&customerSource=3468342229");
      $(".getstarted_deluxe_ffaups").attr("href","/system_requirements_check.jsp?productid=16&customerSource=3468342229");
      $(".getstarted_premier_ffaups").attr("href","/system_requirements_check.jsp?productid=8&customerSource=3468342229");
      $(".getstarted_homebiz_ffaups").attr("href","/system_requirements_check.jsp?productid=32&customerSource=3468342229");      
      $(".getstarted_ffa").attr("href","/system_requirements_check.jsp?productid=4&customerSource=3468342228");
      $(".getstarted_ffacal").attr("href","/system_requirements_check.jsp?productid=4&customerSource=3468343755");
      $(".getstarted_rebate").attr("href","/system_requirements_check.jsp?productid=4&customerSource=4509500000");
		  $(".getstarted_rebate_nonffa").attr("href","/system_requirements_check.jsp?productid=512&customerSource=4511300000");
		  $(".getstarted_rebate_partner").attr("href","/system_requirements_check.jsp?productid=512&customerSource=" + priorityCode);
      $(".continue_return").attr("href","/system_requirements_check.jsp?continue=true");
      $(".continue_ffa_return").attr("href","/system_requirements_check.jsp?customerSource=3468342228&productid=4");
      $(".continue_ffacal_return").attr("href","/system_requirements_check.jsp?customerSource=3468343755&productid=4");
      $(".getprioryear").attr("href","/system_requirements_check.jsp?continue=true&productid=16");
    break;
	
    case GREEN:
      $(".getstarted_free").attr("href",ttoServer + "?productid=512&customerSource=" + priorityCode);
      $(".getstarted_basic").attr("href",ttoServer + "?productid=2&customerSource=" + priorityCode);
      $(".getstarted_deluxe").attr("href",ttoServer + "?productid=16&customerSource=" + priorityCode);
      $(".getstarted_premier").attr("href",ttoServer + "?productid=8&customerSource=" + priorityCode);
      $(".getstarted_home_and_business").attr("href",ttoServer + "?productid=32&customerSource=" + priorityCode);
      $(".getstarted_homebiz").attr("href",ttoServer + "?productid=32&customerSource=" + priorityCode);
      $(".getstarted_basic_ffaups").attr("href",ttoServer + "?productid=2&customerSource=3468342229");
      $(".getstarted_deluxe_ffaups").attr("href",ttoServer + "?productid=16&customerSource=3468342229");
      $(".getstarted_premier_ffaups").attr("href",ttoServer + "?productid=8&customerSource=3468342229");
      $(".getstarted_homebiz_ffaups").attr("href",ttoServer + "?productid=32&customerSource=3468342229");      
      $(".getstarted_ffa").attr("href",ttoServer + "?productid=4&customerSource=3468342228");
      $(".getstarted_ffacal").attr("href",ttoServer + "?productid=4&customerSource=3468343755");
      $(".getstarted_rebate").attr("href",ttoServer + "?productid=4&customerSource=4509500000");
		  $(".getstarted_rebate_nonffa").attr("href",ttoServer + "?productid=512&customerSource=4511300000");
		  $(".getstarted_rebate_partner").attr("href",ttoServer + "?productid=512&customerSource=" + priorityCode);
      $(".continue_return").attr("href",ttoServer + "open/registration/SignIn.htm?customerSource=" + priorityCode);
      $(".continue_ffa_return").attr("href",ttoServer + "open/registration/SignIn.htm?customerSource=3468342228&productid=4");
      $(".continue_ffacal_return").attr("href",ttoServer + "open/registration/SignIn.htm?customerSource=3468343755&productid=4");
      $(".getprioryear").attr("href",ttoServer + "open/registration/SignIn.htm?productid=16&customerSource=" + priorityCode);
      break;
	}
	updatePersonalProButtons();
}

function updateTTOButtons(additionalParams) {
  switch(ttoReady) {
    case RED:
		case YELLOW:    
      $(".getstarted_free").attr("href","/system_requirements_check.jsp?productid=512" + additionalParams);
      $(".getstarted_basic").attr("href","/system_requirements_check.jsp?productid=2" + additionalParams);
      $(".getstarted_deluxe").attr("href","/system_requirements_check.jsp?productid=16" + additionalParams);
      $(".getstarted_premier").attr("href","/system_requirements_check.jsp?productid=8" + additionalParams);
      $(".getstarted_home_and_business").attr("href","/system_requirements_check.jsp?productid=32" + additionalParams);
      $(".getstarted_homebiz").attr("href","/system_requirements_check.jsp?productid=32" + additionalParams);
      $(".getstarted_basic_ffaups").attr("href","/system_requirements_check.jsp?productid=2&customerSource=3468342229" + additionalParams);
      $(".getstarted_deluxe_ffaups").attr("href","/system_requirements_check.jsp?productid=16&customerSource=3468342229" + additionalParams);
      $(".getstarted_premier_ffaups").attr("href","/system_requirements_check.jsp?productid=8&customerSource=3468342229" + additionalParams);
      $(".getstarted_homebiz_ffaups").attr("href","/system_requirements_check.jsp?productid=32&customerSource=3468342229" + additionalParams);       
      $(".getstarted_ffa").attr("href","/system_requirements_check.jsp?productid=4&customerSource=3468342228" + additionalParams);
      $(".getstarted_ffacal").attr("href","/system_requirements_check.jsp?productid=4&customerSource=3468343755" + additionalParams);
      $(".getstarted_rebate").attr("href","/system_requirements_check.jsp?productid=4&customerSource=4509500000" + additionalParams);
		  $(".getstarted_rebate_nonffa").attr("href","/system_requirements_check.jsp?productid=512&customerSource=4511300000" + additionalParams);
		  $(".getstarted_rebate_partner").attr("href","/system_requirements_check.jsp?productid=512&customerSource=" + priorityCode + additionalParams);
      $(".continue_return").attr("href","/system_requirements_check.jsp?continue=true" + additionalParams);
      $(".continue_ffa_return").attr("href","/system_requirements_check.jsp?customerSource=3468342228&productid=4" + additionalParams);
      $(".continue_ffacal_return").attr("href","/system_requirements_check.jsp?customerSource=3468343755&productid=4" + additionalParams);
      $(".getprioryear").attr("href","/system_requirements_check.jsp?continue=true&productid=16" + additionalParams);
    break;
	
    case GREEN:
      $(".getstarted_free").attr("href",ttoServer + "?productid=512&customerSource=" + priorityCode + additionalParams);
      $(".getstarted_basic").attr("href",ttoServer + "?productid=2&customerSource=" + priorityCode + additionalParams);
      $(".getstarted_deluxe").attr("href",ttoServer + "?productid=16&customerSource=" + priorityCode + additionalParams);
      $(".getstarted_premier").attr("href",ttoServer + "?productid=8&customerSource=" + priorityCode + additionalParams);
      $(".getstarted_home_and_business").attr("href",ttoServer + "?productid=32&customerSource=" + priorityCode + additionalParams);
      $(".getstarted_homebiz").attr("href",ttoServer + "?productid=32&customerSource=" + priorityCode + additionalParams);
      $(".getstarted_basic_ffaups").attr("href",ttoServer + "?productid=2&customerSource=3468342229" + additionalParams);
      $(".getstarted_deluxe_ffaups").attr("href",ttoServer + "?productid=16&customerSource=3468342229" + additionalParams);
      $(".getstarted_premier_ffaups").attr("href",ttoServer + "?productid=8&customerSource=3468342229" + additionalParams);
      $(".getstarted_homebiz_ffaups").attr("href",ttoServer + "?productid=32&customerSource=3468342229" + additionalParams);       
      $(".getstarted_ffa").attr("href",ttoServer + "?productid=4&customerSource=3468342228" + additionalParams);
      $(".getstarted_ffacal").attr("href",ttoServer + "?productid=4&customerSource=3468343755" + additionalParams);
      $(".getstarted_rebate").attr("href",ttoServer + "?productid=4&customerSource=4509500000" + additionalParams);
	  $(".getstarted_rebate_nonffa").attr("href",ttoServer + "?productid=512&customerSource=4511300000" + additionalParams);
	  $(".getstarted_rebate_partner").attr("href",ttoServer + "?productid=512&customerSource=" + priorityCode + additionalParams);
      $(".continue_return").attr("href",ttoServer + "open/registration/SignIn.htm?customerSource=" + priorityCode + additionalParams);
      $(".continue_ffa_return").attr("href",ttoServer + "open/registration/SignIn.htm?customerSource=3468342228&productid=4" + additionalParams);
      $(".continue_ffacal_return").attr("href",ttoServer + "open/registration/SignIn.htm?customerSource=3468343755&productid=4" + additionalParams);
      $(".getprioryear").attr("href",ttoServer + "open/registration/SignIn.htm?productid=16&customerSource=" + priorityCode + additionalParams);
      break;
	}
	updatePersonalProButtons();
}

function updatePersonalProButtons(){
	if($(".getstarted_ppro").length > 0){
		$.getJSON("/go/ppstatus", function (data){
			if(!data.available){
      	$(".getstarted_ppro").attr("href","/personal-taxes/online/personal-pro/turnaway.jsp");
      }
		});
	} 
}
