/*****************************************/
// Javascript for LemurMonitors.com
//
// Author:	Greg Browne
// Company:	Root Four Imagination Inc.
// Date:	October 2009
// Contact:	gbrowne@rootfour.com
/*****************************************/

var pEcono = '3=*=1';
var pSafe = '2=*=1';
var pAlert = '0=*=1';

function changePkg(imgFilename, imgID)
{	
	document.getElementById(imgID).src = 'images/' + imgFilename + '.png';
	document.getElementById(imgID).useMap = '#' + imgFilename + 'Map';
	document.getElementById('benefits').src = 'images/' + imgFilename + 'Benefits.jpg';

	document.getElementById('videoButtonHolder').innerHTML = "";
	document.getElementById('soundButtonsHolder').innerHTML = "";
	document.getElementById('soundContainer').innerHTML = "";

	if ('LemurMonitorsSafe' == imgFilename)
		document.getElementById('videoButtonHolder').innerHTML = "<a href='SafeDriverVideo.html'><img src='images/watchvideo.png' alt='Watch Video' /></a>";
	else if ('LemurMonitorsAlert' == imgFilename)
		document.getElementById('soundButtonsHolder').innerHTML = "<a href='javascript:void(0);'><img src='images/alertDriverWarning.png' alt='AlertDriver Warning' onclick=\"playSound('Warning', 'false')\" /></a><br /><a href='javascript:void(0);'><img src='images/alertDriverAlarm.png' alt='AlertDriver Alarm' onclick=\"playSound('Alarm', 'true')\" /></a>";

	var productName = imgFilename.replace("LemurMonitors","") + "Driver";

	//pageTracker._trackPageview(productName);	// Track product clicks in Google Analytics
	document.getElementById('tProduct').href = "http://twitter.com/home?status=" + encodeURIComponent(productName + ' - http://www.LemurMonitors.com'); // Change Twitter link for currently displayed product
	
	document.getElementById('fbProduct').innerHTML = "<iframe src='http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.lemurmonitors.com/index.html%23" + imgFilename.replace("LemurMonitors","") + "&amp;layout=button_count&amp;show_faces=false&amp;width=200&amp;action=like&amp;font=lucida+grande&amp;colorscheme=light&amp;height=35' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:50px; height:22px; padding-left: 8px; position: relative; top: 8px' allowTransparency='true'></iframe>";	// Change Facebook tag for currently displayed product
	document.getElementById('shareProduct').innerHTML = productName;
}

function loadPkg()
{
	var pkg = location.hash.substring(1);
	
	if (pkg == 'Econo' || pkg == 'Safe' || pkg == 'Alert')
		changePkg('LemurMonitors' + pkg, 'frontPkg');	// Custom front package
	else
		changePkg('LemurMonitorsEcono', 'frontPkg');	// Default front package
}

//var pTimeout = "";
function popupInfo(imgID)
{
//	clearTimeout(pTimeout);
	var popupImg = document.getElementsByName('popupImg').length;

	for (i=0; i<popupImg; i++) {
		document.getElementsByName('popupImg')[i].style.display = 'none';
	}

	if(0 == imgID)
		return;

	document.getElementById(imgID).style.display = 'block';
//	pTimeout = setTimeout("popupInfo(0)", 5000);
		
}

function validateEmail(iconID, emailID, buttonID)
{
	var email = document.getElementById(emailID).value;	// Get email input from user
	document.getElementById(emailID).style.backgroundColor = 'white';
	document.getElementById(buttonID).disabled = "disabled";

	if (!email)	// If blank
	{
		document.getElementById(iconID).innerHTML = '';
		return;
	}

	var dot = email.lastIndexOf(".");	// Find last .
	var at 	= email.lastIndexOf("@", dot);	// Find @ before .
	var extLen = email.length - dot;	// Domain extension length

	if ( (at == -1) || (dot == -1) || extLen < 3 )	// No @ or . or extension has less than 2 chars
	{
		document.getElementById(emailID).style.backgroundColor = 'red';
		document.getElementById(iconID).style.color = 'red';
		document.getElementById(iconID).innerHTML = '&#10008;';
	}
	else	// Valid email detected
	{
		document.getElementById(buttonID).disabled = "";
		document.getElementById(iconID).style.color = 'green';
		document.getElementById(iconID).innerHTML = '&#10004;';
	}
}

function decipher(encrypted) {
	var key = 4; 
	var decrypted = "";

	for(i=0; i < encrypted.length; i++) { decrypted += String.fromCharCode( key ^ encrypted.charCodeAt(i) ); }

	return decrypted;
}

function playSound(soundName, loopState)
{
	document.getElementById('soundContainer').innerHTML = "<object data='AlertDriver_Sine_"+ soundName +".mp3' type='audio/mp3' height='0' width='0' loop='"+ loopState +"'><param name='filename' value='AlertDriver_Sine_"+ soundName +".mp3' /><param name='loop' value='"+ loopState +"' /></object>";
}

function loadYoutube(videoCode, divID, width, height)
{
	var videoHTML = "<object width='" + width + "' height='" + height + "'>";
	videoHTML += "<param name='movie' value='http://www.youtube.com/v/" + videoCode + "&hl=en&fs=1&autoplay=1'></param>";
	videoHTML += "<param name='allowFullScreen' value='true'></param>";
	videoHTML += "<param name='allowscriptaccess' value='always'></param>";
	videoHTML += "<embed src='http://www.youtube.com/v/" + videoCode + "&hl=en&fs=1&autoplay=1' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='" + width + "' height='" + height + "'></embed>";
	videoHTML += "</object>";

	document.getElementById(divID).innerHTML = videoHTML;
}

function enterContest()
{
	var submitURL = "http://www.lemurmonitors.com/cgi-bin/contest.pl?lvmAction=enterContest" + "&";
	submitURL += "FirstName=" + encodeURIComponent(document.getElementById('FirstName').value) + "&";
	submitURL += "Email=" + encodeURIComponent(document.getElementById('Email').value) + "&";

	OpenThought.CallUrl(submitURL);
}

/* gup() -	Get URL parameters
 		Source:	http://www.netlobo.com/url_query_string_javascript.html	*/
function gup(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  	var regexS = "[\\?&]"+name+"=([^&#]*)";
  	var regex = new RegExp( regexS );
  	var results = regex.exec( window.location.href );
  	
	if( results == null )
    		return "";
  	else
    		return results[1];
	// Use like: var frank_param = gup( 'frank' );
}
