var BROWSER_IE = "IE";
var BROWSER_GECKO = "GECKO";
var BROWSER_KONQUEROR = "KONQUEROR";
var BROWSER_NETSCAPE = "NETSCAPE";
var BROWSER_OPERA = "OPERA";
var BROWSER_OTHER = "OTHER";
var BROWSER_SAFARI = "SAFARI";

var OS_OTHER = "OTHER";
var OS_WIN95 = "WIN95";
var OS_WINNT = "WINNT";
var OS_WIN98 = "WIN98";
var OS_WINME = "WINME";
var OS_WIN2K = "WIN2K";
var OS_WINXP = "WINXP";
var OS_LINUX = "LINUX";
var OS_MAC = "MAC";

function ShowOrHide(imgId, divId) {
	ToggleElement(divId);
	
	// Added some code that is somewhat a hack to support different kind of expand/collapse.
	// It should be backward compatible but can probably be cleaned up a bit - Martin Karlsson
	
	oImg = document.getElementById(imgId);
	var sImgSrc = new String(oImg.src);
	
	if( sImgSrc.indexOf("_on.gif") > 0 || sImgSrc.indexOf("_off.gif") > 0 )
	{
		if(sImgSrc.indexOf("_on.gif") > 0)
		{
			oImg.src = sImgSrc.replace(/_on.gif/, "_off.gif");
		}
		else
		{
			oImg.src = sImgSrc.replace(/_off.gif/, "_on.gif");
		}
	}
	else if(oImg.src.indexOf("arrow") > 0)
	{
		if(IsHidden(divId))
			oImg.src = "/images/zoominfo/icons/arrow_right.gif";	
		else
			oImg.src = "/images/zoominfo/icons/arrow_down.gif";
	}
	else
	{
		if(IsHidden(divId))
			oImg.src = "/images/zoominfo/icons/plus.gif";	
		else
			oImg.src = "/images/zoominfo/icons/minus.gif";
	}
}
function HideDiv(name)
{		
	var Div = document.getElementById(name);
	if(Div != null)
		Div.style.display = "none";
}
function ShowDiv(name)
{
	var Div = document.getElementById(name);
	if(Div != null)
		Div.style.display = "";
}

function OpenClaimSummary()
{
	OpenModalDialog('/MyAccount/ClaimSummary.aspx?', 550, 400, true, true);
}

function OpenPrivacyPolicy()
{
	OpenModalDialog('/Privacy.aspx?', 600, 450, true, true);
}

function OpenTermsOfService()
{
	OpenModalDialog('/TermsOfService.aspx?', 640, 450, true, true);
}

function OpenFAQDialog()
{
	OpenModalDialog('https://server.iad.liveperson.net/hc/s-87670148/cmd/kbresource/rep-32147242948240903158674858092/kb-8571150660142841950/front_page!PAGETYPE?category=5', 660, 625, true, false);
}

function OpenServiceOptions()
{
	OpenModalDialog('/Registration/ServiceOptions.aspx', 620, 500, true, true);
}

function OpenSecurityCode()
{
	OpenModalDialog('/Registration/SecurityCode.aspx', 520, 450, true, true);
}

function OpenHelpDialog()
{
	OpenDialog('https://server.iad.liveperson.net/hc/s-87670148/cmd/kbresource/rep-32147242948240903158674858092/kb-8571150660142841950/front_page!PAGETYPE?category=2', 660, 625, true, false);
}

function OpenHowDialog()
{
	OpenModalDialog('/Search/How.aspx', 550, 400, true, false);
}

function OpenContactForm(publicSite, firstName, lastName, email)
{
    var url = publicSite + '/contact/default.aspx?url=' + escape(document.location);
    if(firstName != null) url += '&first_name=' + firstName;
    if(lastName != null) url += '&last_name=' + lastName;
    if(email != null) url += '&email=' + email;
	document.location = url;
}

//replaces special characters in strings with standard escape characters
function UrlEncode(sStr){
	if (typeof sStr == "undefined") {
		return "";
	}
	
	var sOutput = encodeURIComponent(sStr);
	
	//Workaround for discrepancy between Server.UrlEncode and JavaScript encodeURIComponent() function
	//encodes characters missed by encodeURIComponent()
	//important for cookie functions
	var arrChars = "! ' ( ) * - . _ /".split(" ");
	var arrEncoded = "%21 %27 %28 %29 %2A %2D %2E %5F %2F".split(" ");
	var oRe;
	
	for (var i=0; i<arrChars.length; i++) {
		oRe = new RegExp("\\" + arrChars[i], "gi");
		sOutput = sOutput.replace(oRe, arrEncoded[i]);
	}
	
	return sOutput;
}

String.prototype.urlEncode = function () {
	return UrlEncode(this);
}

function UrlDecode(sStr){
	if (typeof(sStr) == "undefined") {
		return '';
	}
	sStr = sStr.replace(/\+/gi, " ");
	
	return decodeURIComponent(sStr);
}

String.prototype.urlDecode = function () {
	return UrlDecode(this);
}

function CloseWindow() {
	ModalEnableWindow(opener, true);
	window.close();
}

// Function to handle if the value in a country dropdown changes and updates state dropdown
//EAK 09-22-2006 :: in the state array the stored state name is in the form: stateName_stateCode
//the state that is selected by default is in the form of stateName_stateCode_SELECTED
//Use the same string to pass several pieces of information with the same array.
function UpdateStates(oCountryControl, sStateControlID)
{
	var oState = document.getElementById(sStateControlID);
	var sStateArray = oCountryControl.options[oCountryControl.selectedIndex].id.toLowerCase() + "State";
	
	oState.options.length = 0;
	oState.disabled = false;
	
	try
	{
		aStateArray = eval(sStateArray);
		if(sStateArray.length != 0)
		{
			oState.options[0] = new Option('Select...', '');
			for(i = 1; i <= aStateArray.length; i++)
			{
				iOptions = oState.options.length;
				var curState = aStateArray[i-1]; //string in form of stateName_stateCode[_SELECTED]
				if(curState.indexOf("_SELECTED")!=-1){
					curState = curState.substring(0, curState.indexOf("_SELECTED"));//cut off _SELECTED
					var stateCode = curState.substring(curState.indexOf("_")+1, curState.length);
					curState = curState.substring(0, curState.indexOf("_"));
					oState.options[iOptions] = new Option(curState, stateCode);
					oState.selectedIndex = iOptions;
					
				}
				else{
				    var test = curState.indexOf("_");
				    var stateCode = curState.substring(curState.indexOf("_")+1, curState.length);
				    curState = curState.substring(0, curState.indexOf("_"));
				    oState.options[iOptions] = new Option(curState, stateCode);
				}
			}
		}
		else
		{
			//Disable drop down
			oState.options[0] = new Option('No States Available', '');
			oState.disabled = true;
		}
	}
	catch(e)
	{
		//Disable drop down
		oState.options[0] = new Option('No States Available', '');
		oState.disabled = true;
	}
}

function SortResults() 
{
	var sValue = GetSelectValue(document.getElementById("SortBySelect"));

	if (sValue != '') 
	{
		ProgressBar.Show ("Sorting Results...");
		
		var locationHref = new LocationHref();
	    locationHref.SetQueryValue('searchSource', 'sort');
	    locationHref.SetQueryValue('page', '1');
	    locationHref.SetQueryValue('sort', sValue);

		document.location.replace(locationHref.Url + '?' + locationHref.QueryString);
	}
}
function InitMinMaxSelect(oMinSelect, oMaxSelect){
	
	if (!oMinSelect || !oMaxSelect) {
		return;
	}

	oMinSelect.OptionValues = new Array();
	oMaxSelect.OptionValues = new Array();
	oMinSelect.OptionLabels = new Array();
	oMaxSelect.OptionLabels = new Array();
	
	var i = 0;
	
	for (i=0; i<oMinSelect.length; i++) {
		oMinSelect.OptionValues[i] = GetInt(oMinSelect.options[i].value);
		oMinSelect.OptionLabels[i] = oMinSelect.options[i].text;
	}
	
	for (i=0; i<oMaxSelect.length; i++) {
		oMaxSelect.OptionValues[i] = GetInt(oMaxSelect.options[i].value);
		oMaxSelect.OptionLabels[i] = oMaxSelect.options[i].text;
	}
	
	UpdateMinMaxSelect(oMinSelect, oMaxSelect, true);
	UpdateMinMaxSelect(oMinSelect, oMaxSelect, false);
}

function UpdateMinMaxSelect(oMinSelect, oMaxSelect, bUseMin) {

	var nMinVal = GetInt(GetSelectValue(oMinSelect));
	var nMaxVal = GetInt(GetSelectValue(oMaxSelect));
	
	var oTargetSelect = bUseMin ? oMaxSelect : oMinSelect;
	var nSourceVal = bUseMin ? nMinVal : nMaxVal;
	var nTargetPrevSelection = bUseMin ? nMaxVal : nMinVal;
	var bDoAdd;
	var bSelectionFound = false;

	//remove all on the target
	while (oTargetSelect.options.length > 0) {
		oTargetSelect.options[0] = null;
	}

	//repopulate
	for (var i=0; i<oTargetSelect.OptionValues.length; i++) {
		bDoAdd = false;
		if (bUseMin && (oTargetSelect.OptionValues[i] >= nSourceVal || oTargetSelect.OptionValues[i] == 0)) {
			bDoAdd = true;
		} else if (!bUseMin && (oTargetSelect.OptionValues[i] <= nSourceVal || nSourceVal == 0)) {
			bDoAdd = true;
		}
		
		if (bDoAdd) {
			oTargetSelect.options[oTargetSelect.options.length] = new Option(oTargetSelect.OptionLabels[i], (oTargetSelect.OptionValues[i] == 0 ? "" : oTargetSelect.OptionValues[i]));
			if (oTargetSelect.OptionValues[i] == nTargetPrevSelection) {
				oTargetSelect.options[oTargetSelect.options.length - 1].selected = true;
				bSelectionFound = true;
			}
		}
	}

	//restore the selection 
	if (!bSelectionFound) {
		SetSelectValue(oTargetSelect, "");
	}
}
function IsCreditCardExpired(nMonth, nYear)
{
	dNow = new Date();

	if (nYear < dNow.format("YYYY")) 	return false;
	if (nYear == dNow.format("YYYY") && nMonth < dNow.getMonth()+1) return false;
	return true;
}
function ValidateState(countryId, stateId)
{
	var eleCountry = document.getElementById(countryId);
	var eleState = document.getElementById(stateId);
	
	if(eleCountry != null) {
		var countryCode = eleCountry.options[eleCountry.selectedIndex].value;
		if(countryCode != "US" && countryCode != "CA") return true;
	}
	if(eleState.selectedIndex == 0) {
		return false;
	}
	return true;
}
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();
addEvent(window,'unload',EventCache.flush);

function GotoXing(url) {
	window.open(url, '_blank');
}