// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//
// Title : RedBox Digital site functions
// Author : RedBox Digital
//
// Description : Core functionality for standard sites developed by RedBox Digital
//
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

function theflash(movie, width, height) {
	document.writeln('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + width + '" height="' + height + '">');
    document.writeln('<param name="movie" value="' + movie + '">');
	document.writeln('<param name="allowScriptAccess" value="sameDomain" />');
	document.writeln('<param name="quality" value="high" />');
	document.writeln('<param name="menu" value="false">');
	document.writeln('<param name="wmode" value="transparent">');
	document.writeln('<embed src="' + movie + '" width="' + width + '" height="' + height + '" quality="high" allowScriptAccess="sameDomain" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" menu="false" wmode="transparent"></embed>');
    document.writeln('</object>');
}

function open_window(fileName, windowName, windowWidth, windowHeight, scrollbars, resizable) {
	window.open(fileName, windowName, 'toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=' + scrollbars + ', resizable=' + resizable + ', left=' + centre(windowWidth, screen.availWidth) + ', top=' + centre(windowHeight, screen.availHeight) + ', width=' + windowWidth + ', height=' + windowHeight);
}

function pageup(e) {
	UAGENT = navigator.userAgent.toUpperCase();
	if (UAGENT.indexOf("MSIE") >=0) { posi = event.y; }
	else { posi = e.pageY; }
	moveObje(posi);
}
	function moveObje(position) {
	move = position / 15;
	point = parseInt(position - move);
	scrollTo(0,point);
	if (point > 0) { setTimeout("moveObje(point)",15); }
}

function imgSwap(oImg)
{
   var strOver  = "On"    // image to be used with mouse over
   var strOff = "Off"     // normal image
   var strImg = oImg.src
   if (strImg.indexOf(strOver) != -1) 
      oImg.src = strImg.replace(strOver,strOff)
   else
      oImg.src = strImg.replace(strOff,strOver)
}

function getQSValue(ji) {
	hu = window.location.search.substring(1);
	gy = hu.split("&");
	for (i=0;i<gy.length;i++) {
		ft = gy[i].split("=");
		if (ft[0] == ji) {
			return ft[1];
		}
	}
	return "";
}

function redirect () 
{
	window.open(getQSValue('url'));
}

function toggleDiv (sender) 
{
	if (sender)
	{
		for (i = 0; i < sender.options.length; i++)
		{
			var divId = sender.options[i].value;
			var display = "none";
			
			if (i == sender.selectedIndex)
			{
				display = "";
			}

			if (document.getElementById(divId)) 
			{
				document.getElementById(divId).style.display = display;
			}
		}
	}
}

function printpage()
{
window.print()
}

function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
window.onload = externalLinks;



	function changeTextExpanded(){
		document.getElementById('nicoretteSelector').innerHTML = '<a href="javascript:void{}" onclick="flashMovie.toggle(); changeTextClosed();"><img src="../../CommonNicorette/Images/Global/nicoretteSelectorExpanded.gif" alt="Open" /></a>';
	}
	function changeTextClosed(){
		document.getElementById('nicoretteSelector').innerHTML = '<a href="javascript:void{}" onclick="flashMovie.toggle(); changeTextExpanded();"><img src="../../CommonNicorette/Images/Global/nicoretteSelectorClosed.gif" alt="Closed" /></a>';
	}

	function imgSwap(oImg)
	{
	   var strOver  = "On"    // image to be used with mouse over
	   var strOff = "Off"     // normal image
	   var strImg = oImg.src
	   if (strImg.indexOf(strOver) != -1) 
		  oImg.src = strImg.replace(strOver,strOff)
	   else
		  oImg.src = strImg.replace(strOff,strOver)
	}

	var theImages = new Array()
	
	// do not edit anything below this line
	
	var j = 0
	var p = theImages.length;
	var preBuffer = new Array()
	for (i = 0; i < p; i++){
	   preBuffer[i] = new Image()
	   preBuffer[i].src = theImages[i]
	}
	var whichImage = Math.round(Math.random()*(p-1));
	function showImage(){
	document.write(''+theImages[whichImage]+'');
	}
	
	var theLargeImages = new Array()

	theLargeImages[0] = '01_05.jpg'
	theLargeImages[1] = '02_05.jpg'
	theLargeImages[2] = '03_05.jpg'
	theLargeImages[3] = '04_05.jpg'
	theLargeImages[4] = '05_05.jpg'
	
	// do not edit anything below this line
	
	var j = 0
	var p = theLargeImages.length;
	var preBuffer = new Array()
	for (i = 0; i < p; i++){
	   preBuffer[i] = new Image()
	   preBuffer[i].src = theLargeImages[i]
	}
	var whichLargeImage = Math.round(Math.random()*(p-1));
	function showLargeImage(){
	document.write('<img src="../CommonNicorette/Images/Photos/'+theLargeImages[whichLargeImage]+'" alt="Nicorette Photo" title="Nicorette Photo" />');
	}
	
function popUp(name, url) {
	   window.open(url,name,'toolbar=0,menubar=0,scrollbars=1,resizable=0,status=0,location=0,left=30,top=30,width=280,height=240');
	}	

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

function motivation_replace_cigs_per_day() {
	var el = document.getElementById("replace_para");
	el.innerHTML = el.innerHTML.replace("{}", getQSValue("cigsPerDay"));
}

function motivation_calculate(invalid_msg, result_url1, result_url2, result_url3, result_url4) {
	if (!check_entered(document.Form.forename, invalid_msg + ' 1.')) {
		return;
	}
	if (!check_entered(document.Form.age, invalid_msg + ' 1.')) {
		return;
	}
	if (!check_selected(document.Form.yearsSmoked, invalid_msg + ' 2.')) {
		return;
	}
	if (!check_entered(document.Form.cigsPerDay, invalid_msg + ' 3.')) {
		return;
	}
	if (!check_selected(document.Form.q4, invalid_msg + ' 4.')) {
		return;
	}
	if (!check_selected(document.Form.q5, invalid_msg + ' 5.')) {
		return;
	}
	if (!check_selected(document.Form.q6, invalid_msg + ' 6.')) {
		return;
	}
	if (!check_selected(document.Form.q7, invalid_msg + ' 7.')) {
		return;
	}
	if (!check_selected(document.Form.q8, invalid_msg + ' 8.')) {
		return;
	}
	if (!check_selected(document.Form.q9, invalid_msg + ' 9.')) {
		return;
	}

	var score = 0;
	for (var i=8; i<=34; i++) {
		if (document.getElementById(i).checked) {
			var el = document.getElementById(i);
			score += parseInt(el.value);
		}
	}

	var yearsSmoked = parseInt(check_value(document.Form.yearsSmoked));
	var cigsPerDay = parseInt(document.getElementById("cigsPerDay").value);
	var totalCigarettes = cigsPerDay * 365 * yearsSmoked;
	var href = null;

	if (score <= 10) {
		if (totalCigarettes < 10000) {
			href = result_url1;
		} else {
			href = result_url2;
		}
	} else if (score > 10 && score < 14) {
		href = result_url3;
	} else if (score > 14) {
		href = result_url4;
	}

	href += "?cigsPerDay=" + cigsPerDay;
	window.location.href = href;
}

function check_entered(el, msg) {
	if (el.value == "") {
		alert(msg);
		return false;
	}
	return true;
}

function check_selected(radio_group, msg) {
	for (var i=0; i<radio_group.length; i++) {
		if (radio_group[i].checked) {
			return true;
		}
	}
	alert(msg);
	return false;
}

function check_value(radio_group) {
	for (var i=0; i<radio_group.length; i++) {
		if (radio_group[i].checked) {
			return radio_group[i].value;
		}
	}
}
