function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
win = window.open(mypage,myname,settings)
}
/* Hide right-click block in IE */
 /*var message="\n(C) Copyright 2009 Gail Boswell of Stay In Touch System. All Rights Reserved.   \n\n    Material of StayInTouchSystem.com is Copyrighted.\n";
 function click(e) {
 if (document.all) {
 if (event.button == 2) {
 alert(message);
 return false;
 }
 
 }
 if (document.layers) {
 if (e.which == 3) {
 alert(message);
 return false;
 }
 }
 }*/
 if (document.layers) {
 document.captureEvents(Event.MOUSEDOWN);
 }
 document.onmousedown=click;
 function winPrint(){
if (window.print) 
{
  window.print();
}
else
{
  alert("Your Web Browser DOES NOT support this print method. Close this window and select File>Print from your Menus.");
}
}
function popUp(url) {
sealWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=450');
		self.name = "mainWin";
		}
/* Make a link zoom open by adding the event handler 
   onclick="zoomBox(event,this);return false" 
   To zoom into a new, positioned window add width, height, left, and top 
   onclick="zoomBox(event,this,640,480,100,50);return false" 
   */ 
var maxW,maxH,fromX,fromY,toX,toY,adjX,adjY,zBox,zStep=0,zLink,zNew; 
function zoomBox(evt,zlink,maxw,maxh,tox,toy) { 
if (arguments.length > 2) zNew=1; 
scrollH=(window.pageYOffset!=null)?window.pageYOffset:document.body.scrollTop; 
maxW=maxw?maxw:window.innerWidth?innerWidth:document.body.clientWidth; 
maxH=maxh?maxh:window.innerHeight?innerHeight:document.body.clientHeight; 
toX=tox?tox:0; 
toY=(toy?toy:0)+scrollH; 
fromX=evt.pageX?evt.pageX:evt.clientX; 
fromY=(evt.pageY?evt.pageY:evt.clientY)+(document.all?scrollH:0); 
adjX=toX+evt.screenX-fromX; 
adjY=toY+evt.screenY-fromY; 
if (document.createElement && document.body.appendChild && !zBox) { 
 zBox=document.createElement("div"); 
 zBox.style.position="absolute"; 
 document.body.appendChild(zBox); 
} else if (document.all && !zBox) { 
 document.all[document.all.length-1].outerHTML+='<div id="zBoxDiv" style="position:absolute"></div>'; 
 zBox=document.all.zBoxDiv; 
} else if (document.layers && !zBox) { 
 zBox=new Layer(maxW); 
 zBox.style=zBox; 
 } 
zLink=zlink; 
doZoom(); 
} 
function doZoom() { 
zStep+=1; 
zPct=(10-zStep)/10 
if (document.layers) { 
 zBox.moveTo(toX+zPct*(fromX-toX),toY+zPct*(fromY-toY)); 
 zBox.document.open(); 
 zBox.document.write("<table width='"+maxW*(1-zPct)+"' height="+maxH*(1-zPct)+" border=2 cellspacing=0><tr><td></td></tr></table>"); 
 zBox.document.close(); 
 } else { 
 zBox.style.border="2px solid #999999"; 
 zBox.style.left=toX+zPct*(fromX-toX); 
 zBox.style.top=toY+zPct*(fromY-toY); 
 zBox.style.width=maxW*(1-zPct); 
 zBox.style.height=maxH*(1-zPct); 
 } 
zBox.style.visibility="visible"; 
if (zStep < 10) setTimeout("doZoom("+fromX+","+fromY+","+toX+","+toY+")",30); 
else { 
 zBox.style.visibility='hidden'; 
 zStep=0; 
 if (zLink && !zNew) location.href=zLink.href; 
 else if (zLink && zNew) { 
  var w=window.open(zLink.href,'','width='+maxW+',height='+maxH+',left='+adjX+',top='+adjY+',scrollbars,resizable'); 
  zNew=null; 
  } 
 } 
} 
function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.id; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
// http://www.sitebuildercorp.com/button.html

// Preload Over and Down state images
Image1= new Image(10,12);
Image1.src = "../images/alexButtonBgDown.gif";
Image2= new Image(10,12);
Image2.src = "../images/alexButtonBgOn.gif";

function press(ID)
{
	document.getElementById(ID).style.backgroundImage='url(../images/alexButtonBgDown.gif)';
	document.getElementById(ID).style.backgroundPosition='top';
}
function release(ID)
{
	document.getElementById(ID).style.backgroundPosition='bottom';
	document.getElementById(ID).style.backgroundImage='url(../images/alexButtonBg.gif)';
}
function over(ID)
{
	document.getElementById(ID).style.backgroundPosition='bottom';
	document.getElementById(ID).style.backgroundImage='url(../images/alexButtonBgOn.gif)';
}
	function linkify(URL,target, config)
{
	if(target != '')
	{
		// Open in a new window
		window.open(URL, target, config);
	}
	else
	{
		location.href=URL;
	}
}
function showDiv(ID, styleAttribute)
{
	// If there is no div, don't throw a JavaScript error
	if(!document.getElementById(ID)){ return false; }
	
	document.getElementById(ID).style.display=styleAttribute;
}
function emptyFilter()
{
	defaultAutoEmpty('Your Primary Email Here', 'Primary Email','Your First Name Here', 'First Name');
}
function defaultAutoFill(defaultValue, fieldId)
{
	if(empty(document.getElementById(fieldId).value)) { document.getElementById(fieldId).value = defaultValue; }
}

function defaultAutoEmpty(defaultValue, fieldId)
{
	if(document.getElementById(fieldId).value == defaultValue) { document.getElementById(fieldId).value = ''; }
}
$(function(){
	// setup testimonial tooltip
	$("#testimonialImg").hover(
      function () {
			$("#helpBookmarkHover").show('slow');
			
		}, 
      function () {
			$("#helpBookmarkHover").hide('slow');
			
		}
    );
	// start pop up
	$(".sample").click(function () {
		$('#basicModalContent2').modal({onOpen: function (dialog) {
			dialog.overlay.fadeIn('slow', function () {
				dialog.container.slideDown('slow', function () {
					dialog.data.fadeIn('slow');
					});
				});
			}});
		return false;
		}); // end click
		$("#linkPop").click(function(){
		$('#basicModalContent3').modal({onOpen: function (dialog) {
							  dialog.overlay.fadeIn('slow', function () {
								dialog.container.slideDown('slow', function () {
								  dialog.data.fadeIn('slow');
								});
							  });
							}});
		return false;
		
	}); // end click
	
	// start calculator functionality
	
	$("#calcPop").click(function () {
		$('#basicModalContent').modal({onOpen: function (dialog) {
			dialog.overlay.fadeIn('slow', function () {
				dialog.container.slideDown('slow', function () {
					dialog.data.fadeIn('slow');
					});
				});
			}});
	
	// hide calculations
		
		$("#cardCost").hide();
		$("#yearCost").hide();
		$("#grossProf").hide();
		$("#exp").hide();
		$("#netProf").hide();
		$("#return").hide();
		
		return false;
		}); // end click
	function calcROI()
		{
			
			// What is the ListNum
		  	var ListNum = $("#listNum").val();
			// What is the SentNum
		  	var SentNum = $("#sentNum").val();
			// What is the AvgPrice
		  	var AvgPrice = $("#avgPrice").val();
			// What is the ListNum
		  	var AvgPercentage = $("#avgPercentage").val();
			// What is the NumSold
		  	var NumSold = $("#numSold").val();
			
			// set cost per card
			var CostPerCard = .69;
		 
			isValidData = false;
			alertListNumMessage  = 'Please Enter the Size of your List.';
			alertSentNumMessage = 'Please enter the Number of Times Sent.';
			alertAvgPriceMessage     = 'Please enter the Average Sales Price.';	
			alertAvgPercentageMessage = 'Please enter the Average Profit Percentage.';
			alertNumSoldMessage = 'Please enter the number sold.';
			
			if(ListNum)        			{ alertListNumMessage = ''; }
			if(SentNum)					{ alertSentNumMessage  = ''; }
			if(AvgPrice)				{ alertAvgPriceMessage  = ''; }
			if(AvgPercentage)			{ alertAvgPercentageMessage  = ''; }
			if(NumSold)					{ alertNumSoldMessage  = ''; }
			
			
			alertMessage = alertListNumMessage + '\n' + alertSentNumMessage + '\n' + alertAvgPriceMessage + '\n' + alertAvgPercentageMessage  + '\n' + alertNumSoldMessage;
		
		if(ListNum && SentNum && AvgPrice && AvgPercentage && NumSold) { isValidData = true; }
		
		if(isValidData)
		{
			
			 // hide calculations
		
		$("#cardCost").show();
		$("#yearCost").show();
		$("#grossProf").show();
		$("#exp").show();
		$("#netProf").show();
		$("#return").show();
			
			
			//Run Calculations
			
			//first batch.
			//set cost per card
			$("#costPerCard").val(CostPerCard);
			// Set the yearly cost
			yearlyCost = SentNum * ListNum * CostPerCard;
			$("#yearlyCost").val(yearlyCost);
			// set estimate with same data
			$("#expense").val(yearlyCost);
			
			//set gross profit
			grossProfit = AvgPrice * (AvgPercentage/100) * NumSold;
			$("#grossProfit").val(grossProfit);
			
			// set net profit
			netProfit = (grossProfit) - (yearlyCost);
			$("#netProfit").val(netProfit);
			
			// set ROI
			
			ROI = netProfit/yearlyCost;
			ROI = ROI * 100;
			
			ROI = ROI.toFixed(2);
			ROI = ROI + '%';
			// make 2 decimal places and % after it.
			$("#ROI").val(ROI);
			
		}
		else
		{
			// Fail
			alert(alertMessage); 
			
		}
		 
		 /* <div class="num_input"><input size="16" maxlength="14" name="listNum" id="listNum" /></div>
		
          <div class="prompt">Number of times sent?:</div>
          <div class="num_input"><input size="16" maxlength="14" name="sentNum" id="sentNum" /></div>
          <div class="prompt">Cost Per Card Sent?:</div>
          <div class="num_input"><input size="16" maxlength="14" id="costPerCard" readonly /></div>
		 <div>&nbsp;</div>
          <div class="prompt">Yearly Cost =</div>
          <div class="num_input"><input size="16" maxlength="14" id="yearlyCost" readonly /></div>
         <div>&nbsp;</div>
         <div class="prompt">Average Sales Price?</div>
          <div class="num_input"><input size="16" maxlength="14" name="avgPrice" id="avgPrice" /></div>
          <div class="prompt">Average Profit Percentage?</div>
          <div class="num_input"><input size="16" maxlength="14" name="avgPercentage" id="avgPercentage" /></div>
          <div class="prompt">Number Sold?</div>
          <div class="num_input"><input size="16" maxlength="14" name="numSold" id="numSold" /></div>*/
		 
		 return false;
	 }// end calcROI

		
		//launch calc function
		$("#btnCalc").click(function() {   calcROI(); return false;   });
		
 }); // end document ready
