/********************************************************************************/
var g_windowWidth = 800;
var g_windowHeight = 600;

var g_scrollX = 0;
var g_scrollY = 0;

var g_isViewingScreenshot = 0;

var g_tempImageObject = new Image();

var g_frameThickness = 50;

var g_browserType = "u";
var g_browserVer = 0;
var g_isIE7orLater = false;

var g_tooltipXOffset = 18;
var g_tooltipYOffset = 12;
var	g_showingTooltip = false;
/********************************************************************************/
function getWindowSize()
{
	if (self.innerWidth)
	{
		g_windowWidth = self.innerWidth;
		g_windowHeight = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		g_windowWidth = document.documentElement.clientWidth;
		g_windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		g_windowWidth = document.body.clientWidth;
		g_windowHeight = document.body.clientHeight;
	}
}
/********************************************************************************/
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }

  g_scrollX = scrOfX;
  g_scrollY = scrOfY;
}
/********************************************************************************/
function getBrowserVersion()
{
	// Test for MSIE x.x;
	g_isIE7orLater = false;
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{ 
		// capture x.x portion and store as a number
		var ieversion=new Number(RegExp.$1) 
		if (ieversion>=7)
			g_isIE7orLater = true;
	}
}
/********************************************************************************/
function getBrowserName()
{
	var browserName=navigator.appName; 
	if (browserName=="Netscape")
	{ 
		g_browserType = "nt";
	}
	else 
	{ 
		if (browserName=="Microsoft Internet Explorer")
		{
			g_browserType = "ie";
		}
		else
		{
			g_browserType = "u";
		}
	}	
}
/********************************************************************************/
function onWindowScroll()
{
	alert("Scroll!");
}
/********************************************************************************/
function initPage()
{
	getWindowSize();
	getBrowserName();	
	getBrowserVersion();
}
/********************************************************************************/
function iLoad(isrc) 
{
	var oImg = new Image();
	oImg.src = isrc;
	if (oImg.complete) 
	{
		window.alert(oImg.src + ' ' + oImg.width + ' x ' + oImg.height);
	}
	else 
	{
		window.setTimeout('iLoad(imgsrc)', 1000);
	}
}
/********************************************************************************/
function showScreenshot(screenshotIndex, which_product)
{
	// Open a new div which obscures everything
	var backgr_div = document.getElementById("scr_back");
	
	getBrowserName();
	getBrowserVersion();

	backgr_div.style.width = "100%";
	backgr_div.style.height = "100%";	
	
	if(g_browserType == "ie" && g_isIE7orLater == false)
	{
		backgr_div.style.setExpression("left",'('+ 0 +'+ ( ignoreMe2 = document.documentElement.scrollLeft ? 					document.documentElement.scrollLeft : document.body.scrollLeft ) ) + \'px\'');
		
		backgr_div.style.setExpression("top", '('+ 0 +'+ ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + \'px\'');	
	}
	else
	{
		backgr_div.style.top = 0 + "px";
		backgr_div.style.left = 0 + "px";
	}	

	g_isViewingScreenshot = 1;
	
	// Now load the image and gets its dimensions
	getImageDims(screenshotIndex, false, which_product);
}
/********************************************************************************/
function getImageDims(iIndex, isRecursive, which_product)
{
	if(!isRecursive)
	{
		g_tempImageObject = new Image();
		var scr_info = getScreenshotInfos(which_product);
		g_tempImageObject.src = scr_info[1] + "scr" + (iIndex+1) + ".jpg";
	}
	
	if(g_tempImageObject.complete)
	{
		showScreenShotFinal(iIndex, g_tempImageObject.width, g_tempImageObject.height, which_product);
	}
	else
	{
		setTimeout('getImageDims('+iIndex+', true, \''+ which_product + '\')', 500);
	}
}
/********************************************************************************/
function showScreenShotFinal(iIndex, iWidth, iHeight, which_product)
{
	var image_div =  document.getElementById("scr_wnd");
	
	var divWidth = iWidth + g_frameThickness*2;
	var divHeight = iHeight + g_frameThickness*2;
	
	getWindowSize();
	
	var centerX = (g_windowWidth - divWidth)/2.0;
	var centerY = (g_windowHeight - divHeight)/2.0;
	
	if(centerX < 0)
		{ centerX = 0; }
	if(centerY < 0)
		{ centerY = 0; }
	
	image_div.style.width = divWidth + "px";
	image_div.style.height = divHeight + "px";

	if(g_browserType == "ie" && g_isIE7orLater == false)
	{
		image_div.style.setExpression("left",'('+ centerX +'+ ( ignoreMe2 = document.documentElement.scrollLeft ? 					document.documentElement.scrollLeft : document.body.scrollLeft ) ) + \'px\'');
		
		image_div.style.setExpression("top", '('+ centerY +'+ ( ignoreMe = document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ) ) + \'px\'');	
	}
	else
	{
		image_div.style.top = centerY + "px";
		image_div.style.left = centerX + "px";
	}

	var divCont = "";
	var scr_info = getScreenshotInfos(which_product);
	
	divCont += "<a href=\"javascript:closeScreenshot();\" class=\"scr-close-link\">close <img src=\"img/btn_close.gif\" style=\"position:relative;top:3px;\"></a>\n";	
	divCont += "<center><div style=\"position:relative;top:"+g_frameThickness+"px;\">";
	
	divCont += "<a href=\"javascript:closeScreenshot();\">";
	divCont += "<img src=\"";
	divCont += scr_info[1] + "scr" + (iIndex+1) + ".jpg";
	divCont += "\" style=\"width:"+iWidth+"px;height:"+iHeight+"px;position:relative;border:1px solid #777777;\" />";
	divCont += "</a><br /><br />";
	divCont += "<span class=\"scr-comment-small\">"+scr_info[0][iIndex]+"</span>";
	divCont += "</div></center>";
	
	image_div.innerHTML = divCont;
}
/********************************************************************************/
function closeScreenshot()
{
	var backgr_div = document.getElementById("scr_back");	
	backgr_div.style.width = "0px";
	backgr_div.style.height = "0px";

	if(g_browserType == "ie" && g_isIE7orLater == false)
	{
		backgr_div.style.removeExpression("top");
		backgr_div.style.removeExpression("left");			
	}
	
	backgr_div.style.top = "-1000px";
	backgr_div.style.left = "-1000px";	
	
	var image_div =  document.getElementById("scr_wnd");	
	image_div.innerHTML = "";
	image_div.style.width = "0px";
	image_div.style.height = "0px";

	if(g_browserType == "ie" && g_isIE7orLater == false)
	{
		image_div.style.removeExpression("top");
		image_div.style.removeExpression("left");	
	}
	
	image_div.style.top = "-1000px";
	image_div.style.left = "-1000px";		

	g_isViewingScreenshot = 0;
}
/********************************************************************************/
function validateSurveyForm(form)
{
	// Set the checked price into a separate variable
	for(i = 0; i < form.SFPrice.length; i++)
	{
		if(form.SFPrice[i].checked)
		{
			form.SFSelPrice.value = form.SFPrice[i].value;
		}
	}
	
	if( form.SFIdeas.value.length <= 0 && form.SFFeatures.value.length <= 0 && form.SFComments.value.length <= 0)
	{
		alert("Please kindly fill in at least one text field. You can also simply navigate away from this page without sumbitting anything.");
		return false;
	}

	return true;
}
/********************************************************************************/
function validateFeedbackForm(form)
{
	if(form.FBMessage.value.length <= 0)
	{
		alert("Thank you for deciding to write us! However, in this case it would be nice to have a non-blank message, please. ");
		return false;	
	}
	return true;
}
/********************************************************************************/
function getScreenshotInfos(which_product)
{
	if(which_product == 'OS')
	{
		return [g_screenshotDescr, g_screenshotFolder];
	}
	else if(which_product == 'SMN')
	{
		return [g_screenshotDescrSMN, g_screenshotFolderSMN];		
	}
	else
	{
		return [g_screenshotDescrDMN, g_screenshotFolderDMN];
	}
	
}
/********************************************************************************/
function generateGallery(strDivId, which_product)
{
	var gall_div = document.getElementById(strDivId);	
	var div_cont = "";
	
	var scr_info = getScreenshotInfos(which_product);
	
	var forwardCount = 0;
	for(i = scr_info[0].length-1; i >=0 ; i--, forwardCount++)
	{
		div_cont += "<div class=\"scr-link\">";
		div_cont += "<a href=\"javascript:showScreenshot(";
		div_cont += i + ",'" + which_product + "');\" ><img class=\"scrsht\" src=\"";
		div_cont += scr_info[1] + g_screenshotThumbPrefix + "scr" + (i+1) + ".gif";
		div_cont += "\" /></a><br />";
		div_cont += "<span class=\"scr-comment-small\"><br />"+scr_info[0][i]+"</span>";
		div_cont += "</div>";
		
		if( forwardCount % 2 != 0)
		{
			div_cont += "<div class=\"clear\"></div>\n";
		}
		
		div_cont += "\n";
	}
	
	gall_div.innerHTML = div_cont;
}
/********************************************************************************/
function currencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}
/********************************************************************************/
function updateTotal()
{
	var os_num_copies = 0;
	var dmn_num_copies = 0;
	var smn_num_copies = 0;	
	var total_sum = 0.0;
	
	// Reset unit price, as well.
	var up_elem = document.getElementById("dispUnitPrice");
	up_elem.innerHTML = "US <b>$"+g_osPrice+"</b>";
	
	var up_elem_dmn = document.getElementById("dispUnitPriceDMN");
	up_elem_dmn.innerHTML = "US <b>$"+g_dmnPrice+"</b>";	

	var up_elem_smn = document.getElementById("dispUnitPriceSMN");
	up_elem_smn.innerHTML = "US <b>$"+g_smnPrice+"</b>";	
	
	var up_elem_smn_pro = document.getElementById("dispUnitPriceSMNPro");
	up_elem_smn_pro.innerHTML = "US <b>$"+g_smnProPrice+"</b>";		

	var text_box = document.getElementById("BQuant");
	var target_div = document.getElementById("totalDiv");
	
	// Read the value, multiply by the price
	var num_copies = text_box.value * 1; 
	total_sum += num_copies * g_osPrice;
	os_num_copies = num_copies;

	// Do DMN
	text_box = document.getElementById("BQuantDMN");
	num_copies = text_box.value * 1; 
	dmn_num_copies = num_copies;
	
	total_sum += num_copies * g_dmnPrice;	
	
	// Do SMN
	text_box = document.getElementById("BQuantSMN");
	num_copies = text_box.value * 1; 
	smn_num_copies = num_copies;

	var smn_edition_sel = document.getElementById("smnPro");
	if(smn_edition_sel.checked == true)
	{ total_sum += num_copies * g_smnProPrice;	}
	else
	{ total_sum += num_copies * g_smnPrice;	}
	
	// Set as the total
	if(total_sum < 0 || total_sum >100000)
	{ total_sum = 0; }

	var cont = "";
	var roundedSum = currencyFormatted(total_sum);
	
	cont+="<b>Total: &nbsp; &nbsp; &nbsp; $";
	cont += roundedSum;
	cont +="</b>";
		
	target_div.innerHTML = cont;
	
	return [roundedSum, os_num_copies, dmn_num_copies, smn_num_copies];
}
/********************************************************************************/
function changeProductQuantity(which_product, change_by)
{
	var elem_id = "";
	if(which_product == 'OS')
	{ elem_id = "BQuant"; }
	else if(which_product == 'SMN')
	{ elem_id = "BQuantSMN"; }
	else
	{ elem_id = "BQuantDMN"; }
	var text_box = document.getElementById(elem_id);
	
	var num_copies = text_box.value * 1; 	
	num_copies += change_by;
	if(num_copies < 0)
		num_copies = 0;
	text_box.value = num_copies;
	
	updateTotal();
}
/********************************************************************************/
function validatePurchaseForm(form)
{
	var amount = updateTotal();
	if(amount[0] <= 0)
	{
		alert("Please enter a valid number of copies for at least one of the products.");
		return false;	
	}

	var currForm = form.innerHTML;
	
	var currItemIdx = 1;
	// OS, if any
	if( amount[1] > 0)
	{
		currForm += "<input type=\"hidden\" name=\"item_name_"+currItemIdx+"\" value=\"Organic Studio\">\n";
		currForm += "<input type=\"hidden\" name=\"item_number_"+currItemIdx+"\" value=\"1.1.130\">\n";
		currForm += "<input type=\"hidden\" name=\"amount_"+currItemIdx+"\" value=\""+g_osPrice+"\">\n";
		currForm += "<input type=\"hidden\" name=\"quantity_"+currItemIdx+"\" value=\""+amount[1]+"\">\n";
		currItemIdx++;
	}
	// DMN, if any
	if( amount[2] > 0)
	{
		currForm += "<input type=\"hidden\" name=\"item_name_"+currItemIdx+"\" value=\"DateMeNow\">\n";
		currForm += "<input type=\"hidden\" name=\"item_number_"+currItemIdx+"\" value=\"1.2.160\">\n";
		currForm += "<input type=\"hidden\" name=\"amount_"+currItemIdx+"\" value=\""+g_dmnPrice+"\">\n";
		currForm += "<input type=\"hidden\" name=\"quantity_"+currItemIdx+"\" value=\""+amount[2]+"\">\n";
		currItemIdx++;
	}
	// SMN, if any
	if( amount[3] > 0)
	{
		var smnPrice = 1.0;
		var smnProductString = "";
		var smn_edition_sel = document.getElementById("smnPro");
		if(smn_edition_sel.checked == true)
			{ smnProductString = "SizeMeNow Professional"; smnPrice = g_smnProPrice; }
		else
			{ smnProductString = "SizeMeNow Personal"; smnPrice = g_smnPrice; }
		
		currForm += "<input type=\"hidden\" name=\"item_name_"+currItemIdx+"\" value=\""+smnProductString+"\">\n";
		currForm += "<input type=\"hidden\" name=\"item_number_"+currItemIdx+"\" value=\"1.4.102\">\n";
		currForm += "<input type=\"hidden\" name=\"amount_"+currItemIdx+"\" value=\""+smnPrice+"\">\n";
		currForm += "<input type=\"hidden\" name=\"quantity_"+currItemIdx+"\" value=\""+amount[3]+"\">\n";
		currItemIdx++;
	}


   	form.innerHTML = currForm;
	
	return true;
}
/********************************************************************************/
function getMousePos(e)
{
	var mouseX = 0;
	var mouseY = 0;
	if(!e) 	 { e = window.event;	}
	if (e.pageX || e.pageY)
	{
		mouseX = e.pageX;
		mouseY = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		getScrollXY();
		mouseX = e.clientX + g_scrollX;
		mouseY = e.clientY + g_scrollY;
	}

	return [mouseX, mouseY];
}
/********************************************************************************/
function showTooltipMulti(e, toolTipId, which_product)
{
	var target_array;	
	if(which_product == 'OS')
	{ target_array = g_toolTips; }
	else if(which_product == 'SMN')
	{ target_array = g_toolTipsSMN; }	
	else if(which_product == 'Misc')
	{ target_array = g_toolTipsMisc; }	
	else
	{ target_array = g_toolTipsDMN; }
	
	var currTTDiv = document.getElementById("tooltipDiv");
	
	var mousePos = getMousePos(e);

	currTTDiv.style.left = (mousePos[0] + g_tooltipXOffset) + "px";
	currTTDiv.style.top = (mousePos[1] + g_tooltipYOffset) + "px";	
	

	currTTDiv.innerHTML = target_array[toolTipId];
	
	g_showingTooltip = true;
	
}
/********************************************************************************/
function showTooltip(e, toolTipId)
{
	var currTTDiv = document.getElementById("tooltipDiv");
	
	var mousePos = getMousePos(e);

	currTTDiv.style.left = (mousePos[0] + g_tooltipXOffset) + "px";
	currTTDiv.style.top = (mousePos[1] + g_tooltipYOffset) + "px";	
	

	currTTDiv.innerHTML = g_toolTips[toolTipId];
	
	g_showingTooltip = true;
}
/********************************************************************************/
function killTooltip()
{
	var currTTDiv = document.getElementById("tooltipDiv");
	if(currTTDiv)
	{
		currTTDiv.innerHTML = "";
		currTTDiv.style.left = "-1000px";
		currTTDiv.style.top = "-1000px";		
	}
	g_showingTooltip = false;
}
/********************************************************************************/
function onMouseMove(e)
{
	if(g_showingTooltip)
	{
		var	mousePos = getMousePos(e);
		
		var currTTDiv = document.getElementById("tooltipDiv");
		currTTDiv.style.left = (mousePos[0] + g_tooltipXOffset) + "px";
		currTTDiv.style.top = (mousePos[1] + g_tooltipYOffset) + "px";			
	}
}
/********************************************************************************/
function startHighlight(hltIndex)
{
	if(g_hltWorkArray[hltIndex] == false)
	{
		stopAllHighlights(hltIndex);
		
		g_hltWorkArray[hltIndex] = true;
		var divName = "hlt" + hltIndex;
		var divObject = document.getElementById(divName);
		
		// Replace the div's image with something more useful
		var cont = "";
	
		cont += "<center>";
		cont += "<img";
//		cont += "<img onmouseover=\"javascript:startHighlight("+hltIndex+");\" onmouseout=\"javascript:stopHighlight("+hltIndex+");\"";
		cont += " width=\""+g_hltWidth+"px\" height=\""+g_hltHeight+"px\" src=\"hl/hl"+(hltIndex+1)+".gif\" />";
	cont += "<br/><a href=\"javascript:stopHighlight("+hltIndex+");\"><img class=\"small-btn\" src=\"img/btn_stop.gif\" /></a>";		
		cont += "</center>";		
		divObject.innerHTML = cont;
	}
}
/********************************************************************************/
function stopHighlight(hltIndex)
{
//	alert("TT!");
	
	var divName = "hlt" + hltIndex;
	var divObject = document.getElementById(divName);	
	
	var cont = "";
	
	cont += "<center>";	
	cont += "<img";	
//	cont += "<img onmouseover=\"javascript:startHighlight("+hltIndex+");\" onmouseout=\"javascript:stopHighlight("+hltIndex+");\"";
	cont += " width=\""+g_hltWidth+"px\" height=\""+g_hltHeight+"px\" src=\"hl/t_hl"+(hltIndex+1)+".gif\" />";
	cont += "<br/><a href=\"javascript:startHighlight("+hltIndex+");\"><img class=\"small-btn\" src=\"img/btn_show.gif\" /></a>";
	cont += "</center>";	
	divObject.innerHTML = cont;	
	g_hltWorkArray[hltIndex] = false;
}
/********************************************************************************/
function stopAllHighlights(iExcludedIdx)
{
	var curr_hlt = 0;
	for(curr_hlt = 0; curr_hlt <g_hltNumHighlights; curr_hlt++)
	{
		if(curr_hlt != iExcludedIdx)
		{
			stopHighlight(curr_hlt);	
		}
	}
}
/********************************************************************************/
function preloadAnimations()
{
	var curr_hlt = 0;
	for(curr_hlt = 0; curr_hlt <g_hltNumHighlights; curr_hlt++)
	{
		g_preloadedHlts[curr_hlt] = new Image;
		g_preloadedHlts[curr_hlt].src = "hl/hl" + (curr_hlt+1) + ".gif";
	}	
}
/********************************************************************************/
function removeNotice(divName) 
{
	var backgr_div = document.getElementById(divName);	
	
	// Remove the div
	backgr_div.innerHTML = "";
	backgr_div.style.top = "-1000px";
	backgr_div.style.left = "-1000px";
	
	// Enable the button
	var btn_div = document.getElementById("submitBtnDiv");		
	btn_div.innerHTML = "<input type=\"image\" src=\"img/btn_buy.gif\"  name=\"submit\">";
}
/********************************************************************************/
