// General JavaScript functions for the web site

// Set this to 1 to enable anonymous visitor tracking information.  Set it to zero otherwise.
var enableStats = 0;

// The year to use in copyright notices:
var copyrightYear = 2010;

// Pricing information - expressed as strings to ensure proper formatting:
var monthlyPrice = "$9";
var bimonthlyPrice = "$18";

// Our Better Business Bureau (BBB) rating.  Set this to an empty string to omit:
var bbbRating = "Rated A";

function bbb()
{
	alert("The Better Business Bureau confirmation will show in a new window. "+
		"If it does not appear, turn off your popup blocker and try again.");
    window.open("http://www.bbbonline.org/cks.asp?id=10312101373238004","BBB",
        "toolbar=no,location=yes,status=yes,directories=no,menubar=no,"+
        "width=500,height=350,scrollbars=yes,resizable=yes");
}

// Function to insert our BBB rating:
function insert_bbb_rating()
{
	document.write(bbbRating);
}

// Function to insert pricing info:
function insert_monthly_price()
{
	document.write(monthlyPrice);
}
function insert_bimonthly_price()
{
	document.write(bimonthlyPrice);
}

// This function inserts a field into a form.  The field is inserted via 
// javascript statements so that an automated script cannot see.  The
// the perl script that handles the form inputs can then verify that 
// the field is present.  If the field is not present, then the script 
// quietly ignores the form submission.
function insert_verification_field()
{
	document.write('<input type="hidden" name="verification" value="verification">');
}

// This function inserts our email address, in a way that prevents automated
// scripts from seeing it.
function insert_email()
{
	var string1 = "typin";
	var string2 = "g@c";
	var string3 = "ustomtyping.com";
	var string4 = string1 + string2 + string3;
	document.write("<a href=" + "mai" + "lto:" + string1 + string2 + string3 + ">" + string4 + "</a>");
}

// Get the HTML for a rollover button.  This is a button with 3 separate images: normal, hover, and pressed.  Inputs are:
// - The URL to go to when the button is clicked.
// - A unique element ID for the button.  Use any string.  Valid characters are: letters, numbers, underscore (_), and minus (-)
// - The image for the normal state
// - The image for the hover state
// - The image for the pressed state.  Leave this out or set it to null if not used.
function get_rollover_button(url,elementId,normalUrl,hoverUrl,pressedUrl)
{
	// Preload the hover and pressed images:
	(new Image()).src = hoverUrl;
	if (pressedUrl)
	{
		(new Image()).src = pressedUrl;
	}
	
	var out = "";
	
	// The hyperlink:
	out = "<a href='"+url+"' onmouseover='document.getElementById(\""+elementId+"\").src=\""+hoverUrl+"\";' "+
		"onmouseout='document.getElementById(\""+elementId+"\").src=\""+normalUrl+"\";' "+
		"onmouseup='document.getElementById(\""+elementId+"\").src=\""+normalUrl+"\";'";
	if (pressedUrl)
	{
		out += " onmousedown='document.getElementById(\""+elementId+"\").src=\""+pressedUrl+"\";'";
	}
	out += ">";
	
	// The image:
	out += "<image id='"+elementId+"' src='"+normalUrl+"' border=0 hspace=0 vspace=0>";
	out += "</a>";
	
	return(out);
}

// Same as above, but it uses document.write to insert it into the current document:
function insert_rollover_button(url,elementId,normalUrl,hoverUrl,pressedUrl)
{
	document.write(get_rollover_button(url,elementId,normalUrl,hoverUrl,pressedUrl));
}

// Insert a copyright notice.  This gets the current year from the user's computer (if it makes sense).  Note that no
// text formatting is done by this script.  This is up to the caller.
function insert_copyright()
{
	document.write("&copy; 2003-"+copyrightYear+" "+"<a href='http://www.customsolutions.us'>Custom Solutions</a>.  "+
		"All Rights Reserved.");
}

// Get the html for a flash movie:
var flashMovieSequenceNum = 0;
function get_flash_html(url,width,height,version,windowMode)
{
	var result = "";
	var windowModeStr = "";
	var flashVariables = "";
	var flashVarStr = "";
	
	var varStart = url.indexOf("?");
	if (varStart>-1)
	{
		// Some flash variables are being passed in.  Use the flashVars parameter to allow the SWF to be cached.  (It won't be 
		// cached if differing sets of variables are included after the ? in the url.)
		flashVariables = url.substr(varStart+1);
		url = url.substr(0,varStart);
	}
	
	result = '<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" id="obj'+flashMovieSequenceNum+'" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+version+'" border="0" width="'+width+'" height="'+height+'">';
	result += '<param name="movie" value="'+url+'">';
	result += '<param name="quality" value="High">';
	result += '<param name="allowscriptaccess" value="samedomain">';
	if (flashVariables.length>0)
	{
		result += '<param name="FlashVars" value="'+flashVariables+'">';
		flashVarStr = 'FlashVars="'+flashVariables+'" ';
	}
	if (windowMode != null)
	{
		if (windowMode.length>0)
		{
			result += '<param name="WMode" value="'+windowMode+'">';
			windowModeStr = 'wmode="'+windowMode+'" ';
		}
	}	
	result += '<embed '+windowModeStr+flashVarStr+'src="'+url+'" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" name="obj'+flashMovieSequenceNum+'" width="'+width+'" height="'+height+'" quality="High" allowscriptaccess="samedomain"></object>';
	flashMovieSequenceNum++;
	return(result);
}

// Get a cookie value.  Returns null if not defined.
function get_cookie(cookieName)
{
	var pos = document.cookie.indexOf(cookieName+"=");
	if (pos==-1)
	{
		return(null);
	}
	var valueStart = pos+cookieName.length+1;
	var valueEnd = document.cookie.indexOf(";",valueStart);
	if (valueEnd==-1)
	{
		valueEnd = document.cookie.length;
	}
	var value = document.cookie.substring(valueStart,valueEnd);
	return(value);
}

// Log a stat at the web server
function record_stat(myEvent,searchTerm,info0,info1,info2,info3)
{
    if (myEvent==null || searchTerm==null)
    {
    	// Can't log nothing.
    	return;
  	}
  	
	var urlStr = "http://www.customtyping.com/cgi-bin/kb2/record_stat?event="+escape(myEvent)+"&search_term="+escape(searchTerm);
	if (info0)
	{
		urlStr += "&info0="+escape(info0);
	}
	if (info1)
	{
		urlStr += "&info1="+escape(info1);
	}
	if (info2)
	{
		urlStr += "&info2="+escape(info2);
	}
	if (info3)
	{
		urlStr += "&info3="+escape(info3);
	}
	
	// This "image" never really displays.  This is just a trick to call a URL at our server which records the stat:
	(new Image()).src = urlStr;
}

// Log a signup stat, based on the current cookie values:
function log_signup_stat(myEvent,info0,info2,info3)
{
	if (enableStats==0)
	{
		return;
	}
	
	var disableLogging = get_cookie("disable_logging");
	var searchTerm = get_cookie("search_term");
	var signupScheme = get_cookie("signup_scheme");
	var statsLogged = get_cookie("stats_logged");
	var cookieExpiry = get_cookie("cookie_expiry");
	
	// Check to see if the stat has already been logged:
	var isLogged = 0;
	if (statsLogged!=null && statsLogged.indexOf("/"+myEvent+"/")>-1)
	{
		isLogged = 1;
	}
	
	if (searchTerm && signupScheme && isLogged==0 && (disableLogging==null || disableLogging!="yes"))
	{
		if (signupScheme.indexOf("start_at_home")==-1)
		{
			if (info0==null)
			{
				info0 = "null";
			}
			record_stat(myEvent,searchTerm,info0,signupScheme,info2,info3);
			
			// Record a special cookie to prevent double-logging:
			if (cookieExpiry && cookieExpiry!="0")
			{
				statsLogged = statsLogged + "/" + myEvent + "/";
				document.cookie = "stats_logged="+statsLogged+"; path=/; domain=.customtyping.com; expires="+cookieExpiry;
			}
			else
			{
				document.cookie = "stats_logged="+statsLogged+"; path=/; domain=.customtyping.com";
			}
		}
	}
}

function get_x_pos(elementID,includeBody)
{
	var pos = 0;
	var element = document.getElementById(elementID);
	while (element && (element.tagName != "BODY" || includeBody))
	{
		pos = pos + element.offsetLeft;
		element = element.offsetParent;
	}
	return(pos);
}
function get_y_pos(elementID,includeBody)
{
	var pos = 0;
    var msg = "";
	var element = document.getElementById(elementID);
	while (element && (element.tagName != "BODY" || includeBody))
	{
		pos = pos + element.offsetTop;
		element = element.offsetParent;
	}
	return(pos);
}
