function ic_over(lyr, details, unit_num, xoffset)
{
	var offset=$(lyr).offset();
	$('#ic_hover_'+unit_num).html(details).css({'top': (offset.top+18)+'px','left': (offset.left-xoffset)+'px'}).fadeIn();
}

function ic_out(unit_num)
{
  $('#ic_hover_'+unit_num).hide();
}


var emailme=null;

// Emails the code behind the calculator in an html email, so it can be forwarded on.
function emailmecalc(module_num, unit_num) 
{
	if(emailme==null)
	{
		emailme=unit_num;
	  var email=$('#email_'+unit_num).val();	   
//	  loadXMLDoc('/pages/calculator/email.php?m='+module_num+'&u='+unit_num+'&email='+escape(email), process_emailme);
			
		$.ajax({ url:"/pages/calculator/email.php",
					 global: false,
					 type: "GET",
					 data: ({m: module_num, u:unit_num, email:email}),
					 success: function(data) { 
					   	var div=$('#ic_link_'+emailme);
				 			if(data=='login')
        	      div.html('<div class="yellowbox">Sorry you must <a href="/general.php?subindex=3">login</a> before we can send emails on your behalf</div>'); 
			        else 
  	            div.html('<div class="yellowbox">'+data+'</div>');
					 },
					 failure: function(XMLHttoRequest, textStatus, errorThrown) 
					 {
					   	$('#ic_link_'+emailme).html('<div class="yellowbox">There was a problem sending the email:' + textStatus+'</div>');  
					 }
					 })		
	}
	else
	{
		$('#ic_link_'+unit_num).html('Sorry we\'re already in the process of sending another email for you. One at a time please!'); 
	}
}

function ic_link_popup(module_num, unit_num)
{
	if($('#ic_set_'+unit_num).length)
    height=$('#ic_set_'+unit_num).height()+50;
	else height=350;	
	
	mywindow=window.open('/pages/calculator/link_popup.php?m='+module_num+'&u='+unit_num+'&height='+height,'Instant_Calculator','width=220,height=300,resizable=no');
}

function ic_link(module_num, unit_num)
{
	if($('#ic_set_'+unit_num).length)
    height=$('#ic_set_'+unit_num).height()+50;
	else height=350;	
		
	$('#ic_link_'+unit_num).html('<hr/><strong>YOU CAN SHARE THIS CALCULATOR IN THREE WAYS:</strong><div class="greybox">Embed onto your website with:<textarea readonly="readonly" rows="5" onclick="this.select()"><iframe src="http://www.codecogs.com/ic-'+module_num+'&amp;u='+unit_num+'" frameborder="0" scrolling="no" width="235" height="'+height+'"></iframe></textarea></div><div id="calcemail">Email to a friend:<input id="email_'+unit_num+'" type="text" class="text" value="Enter their email address here" style="width:190px" onclick="this.select()" /><input type="button" onclick="emailmecalc('+module_num+','+unit_num+')" class="bluebutton" value="Email Calculator"/></div><div class="greybox">Make a link to this page:<input type="text" class="text" readonly="readonly" value="http://www.codecogs.com/cog-'+module_num+'" style="width:190px" onclick="this.select()"/></div>').show();
}

// single should be true or false. i.e. do we ignore ranges like 0:3
function getinputs(types, modids, single)
{
	var v='';
	for(i=0;i<types.length;i++)
	{
	  var val=$('#'+modids+i).val();
		if(val)
		{
			if(v!='') v+=',';
			switch(types.substr(i,1))
			{
				case 'b' : { if(val=='true') v+='1'; else v+='0'; } break;
				case 's' : { v+='"'+val+'"'; } break;
				case 'e' : { if(val=='') val='0';
											v+=val; } break;
				default  : if(val=='') v+='0';
										else 
										{ 
										  if(single)
											{
											  j=val.indexOf(':');
											  if(j!=-1) val=val.substr(0,j); // igore colons in decimals
											}
											v+=val.toString();
										} break;
			}
		}
	}
	v=escape(v.replace(/\+/g,"&plus;"));
	return(v);	
}


function confirmAgreement()
{
	
	if($.cookie('instant_calc')!=null || confirm('The calculations made by CodeCogs calculators may not be correct and therefore should not be relied upon. You should not use these calculators if a financial loss, death or injury is possible.\n\nPlease accept the following terms if you wish to continue, otherwise click \'cancel\':\n\nYOU AGREE THAT USE OF THE CODECOGS SITE AND THE SERVICE IS ENTIRELY AT YOUR OWN RISK. THE CODECOGS SITE AND THE SERVICE ARE PROVIDED ON AN "AS IS" OR "AS AVAILABLE" BASIS, WITHOUT ANY WARRANTIES OF ANY KIND. ALL EXPRESS AND IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE WARRANTIES OF MERCHANTABILITY, RELIABILITY, ACCURACY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW.'))
	{		
	  $.cookie('instant_calc',1, {expires: 7});
		return true;
	}
	return false;
}

// Instantly calculates the function 
// The following three are used in the PHP interface. If everything works as we expect then this code will be phased out.
function calcphpnow(module_num, fn, types, divid) 
{
	var modids='ic'+divid.toString()+'_';
	var v=getinputs(types,modids,true);
		
	if(confirmAgreement())
	{
		$("#"+modids+'result').load('/pages/calculator/calc.php?id='+module_num+'&fn='+fn+'&values='+v);
		ic_out(divid);
	}
}

function graphphpnow(module_num, fn, types, divid) 
{
	var modids='ic'+divid.toString()+'_';
	var v=getinputs(types,modids,false);
		
	if(confirmAgreement())
	  window.open('/pages/calculator/graph.php?mod='+module_num+'&fn='+fn+'&values='+v,'Instant_Graph','width=650,height=430,resizable=yes');
}

function tablephpnow(module_num, fn, types, divid) 
{
	var modids='ic'+divid.toString()+'_';
	var v=getinputs(types,modids,false);
		
	if(confirmAgreement())
   	window.open('/pages/calculator/graph.php?table=1&mod='+module_num+'&fn='+fn+'&values='+v,'Instant_Table','width=610,height=400,resizable=yes,scrollbars=yes,scrolling=yes');
}
// ---------------------------------------------------------

// Instantly calculates the function 
function calcnow(mode,file, types, divid) 
{
	var modids='ic'+divid.toString()+'_';
	var v=getinputs(types,modids,true);

	if(confirmAgreement())
	{		
		$('#'+modids+'result').load('/pages/calculator/calc.php?'+mode+'='+file+'&types='+types+'&values='+v);
		ic_out(divid);
	}
}

function graphnow(mode,file, types, divid)
{
	var modids='ic'+divid.toString()+'_';
	var v=getinputs(types,modids,false);
	
	if(confirmAgreement())
	  window.open('/pages/calculator/graph.php?'+mode+'='+file+'&types='+types+'&values='+v,'Instant_Graph','width=650,height=430,resizable=yes');
}

function tablenow(mode,file, types, divid) 
{
	var modids='ic'+divid.toString()+'_';	
	var v=getinputs(types,modids,false);
		
	if(confirmAgreement())
   	window.open('/pages/calculator/graph.php?table=1&'+mode+'='+file+'&types='+types+'&values='+v,'Instant_Table','width=610,height=400,resizable=yes,scrollbars=yes,scrolling=yes');
}
