// JavaScript Document
//***********************************************************
// Function:	addZero
// Description :	places a leading "0" infront of any single digit number
// Parameters :		the number that may require a leading zero
//***********************************************************
function addZero(num)
{
	if(num < 10)
	return '0' + num;
	return num;
	 
}
function isMail(elem)
{
	var str = elem.value;
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	if(!str.match(re))
	{
		return false;		
	}
	return true;
}
function isNum(elem)
{
	var str = elem.value;
	var re = /^[-]?\d*\.?\d*$/;
	str = str.toString();
	if(!str.match(re))
	{
		return false;
	}
	return true;
}
function isNotEmpty(elem)
{
	var str = elem.value;
	var re = /.+/;
	if(!str.match(re))
	{
		return false;
	}
	return true;
}

function validMail(obj)
{
	var invalidChars =" /:,;";
	var email = obj.value;
	var i = 0;
	var Valid = true;
	if (email != "")
	{
		Valid = isMail(obj);
		if (! Valid)
		{
			alert("EMail Address is not valid Please Check");
			obj.select();
		}
	}
}

function validTxt(obj)
{
	return;
}
function validPhone(obj)
{
	var validChars =" ()+1234567890";
	var phoneNum = obj.value;
	var i = 0;
	var Valid = true;
	var theChar = "";
	if (phoneNum != "")
	{
		for (i=0;i<phoneNum.length;i++)
		{   theChar = phoneNum.charAt(i);
			if (validChars.indexOf(theChar,0) == -1)
			{
				Valid = false;
			}
			
		}		
		if (! Valid)
		{
			alert("Phone Number is not valid Please Check");
			obj.select();
		}
	}
}

function validNum(obj)
{
	var valid = false;
	if(obj.value != "")
	{
		valid = isNum(obj);
		if(!valid)
		{
			alert("Value must be Numeric");
			obj.value = "";
			obj.focus();
		}
	}
	return valid;
}

function showInvalid(obj,txt)
{
	document.getElementById("Err"+obj.id).innerHTML = txt;
}

function checkField(obj,objType)
{	var txt = '';
var valid = true;
	switch(objType)
	{
		case "email": if(!isNotEmpty(obj))
					  {
					  	txt = '*'; valid = false;	
					  }					  
					  break;
		case "text": if(!isNotEmpty(obj))
					  {
					  	txt = '*'; valid = false;	
					  }
					  break;
		case "numeric": if(!isNotEmpty(obj) || !isNumber(obj))
					  {
					  	txt = '*'; valid = false;	
					  }
					  break;
		case "select": if(obj.options[obj.selectedIndex].value == "")
					   {
					  	txt = '*'; valid = false;	
					   }
					   break;
	}
	showInvalid(obj,txt);
	return valid;
	
}

function getEventCoords( e )
{
   var docX, docY;
   if( e )
   {
      if( typeof( e.pageX ) == 'number' )
      {
         docX = e.pageX;
         docY = e.pageY;
      }
      else
      {
         docX = e.clientX;
         docY = e.clientY;
      }
   }
   else
   {
      e = window.event;
      docX = e.clientX;
      docY = e.clientY;
      if( document.documentElement
        && ( document.documentElement.scrollTop
            || document.documentElement.scrollLeft ) )
      {
         docX += document.documentElement.scrollLeft;
         docY += document.documentElement.scrollTop;
      } 
      else if( document.body
         && ( document.body.scrollTop
             || document.body.scrollLeft ) )
      {
         docX += document.body.scrollLeft;
         docY += document.body.scrollTop;
      }
   }
   return [ docX, docY ];

}
// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth() 
{
		return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} 
function pageHeight() 
{
		return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
} 
function posLeft() 
{
		return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
} 
function posTop() 
{
		return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
} 
function posRight() 
{
		return posLeft()+pageWidth();
} 
function posBottom() 
{
		return posTop()+pageHeight();
}
                    

function mouseCoords(e) {
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	return [posx,posy];
}

function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX; //+ (document.documentElement.scrollLeft ?
   //document.documentElement.scrollLeft :
   //document.body.scrollLeft);
else return null;
}
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY; // + (document.documentElement.scrollTop ?
   //document.documentElement.scrollTop :
   //document.body.scrollTop);
else return null;
}

//################### AJAX CODE #######################################################
function InitAjax()
{
	 var iAjax=false;
	 try 
	 {
	 	iAjax = new ActiveXObject("Msxml2.XMLHTTP");
	 }  catch (e) 
	 {
		  try 
		  {
		   iAjax = new ActiveXObject("Microsoft.XMLHTTP");
		  } catch (E) 
		  {
		   iAjax = false;
		  }
	 }
	 if(!iAjax && typeof XMLHttpRequest!='undefined') 
	 {
		iAjax = new XMLHttpRequest();
	 }
	 return iAjax;
}

function getParam(inpName)
{
	if(document.getElementById(inpName))
	{
		return document.getElementById(inpName).value;
	}
	else
	{
		return '';
	}
}

//##################################################################################################################################################
//#####################################   CONVERSION ALGORITHMS ####################################################################################

// WEIGHT CONVERSION ###############################################################################################################################
var Wt_Conversion_Const =  0.028343495231;
// Params
// 1. The ID of the Weight (Kgs) text box
// 2. The ID of the Weight (Lbs) text box
// 3. The ID of the Weight (Ozs) text box
function convfromkgs(txtWtKgID, txtWtLbsID, txtWtOzsID)
{
	var kilograms;
	var total_ounces;
	var pounds;
	var ounces;
	var objKgs = document.getElementById(txtWtKgID);
	if(isNaN(objKgs.value))
	{
		alert("Please use numeric values");
		return false;	
	}
	kilograms = objKgs.value;
	total_ounces = Math.floor(kilograms/Wt_Conversion_Const);
	pounds = Math.floor(total_ounces/16);
	ounces = Math.floor(total_ounces - (pounds * 16));

	var objOunces = document.getElementById(txtWtOzsID);
	var objPounds = document.getElementById(txtWtLbsID);
	
	objOunces.value = ounces;
	objPounds.value = pounds;

}

// Params
// 1. The ID of the Weight (Kgs) text box
// 2. The ID of the Weight (Lbs) text box
// 3. The ID of the Weight (Ozs) text box
function convfromlboz(txtWtKgID, txtWtLbsID, txtWtOzsID)
{
	var objOunces = document.getElementById(txtWtOzsID);
	var objPounds = document.getElementById(txtWtLbsID);
	
	if(isNaN(objOunces.value) || isNaN(objPounds.value))
	{
		alert("Please enter numeric values");
		return false;
	}
	var kilograms;
	if(objPounds.value == "")
	{
		objPounds.value = 0;	
	}
	var pounds = parseInt(objPounds.value,10);
	
	if(objOunces.value=="")
	{		
		objOunces.value = 0;
	}
	
	var ounces = parseFloat(objOunces.value);
	
	
	var totalOunces = (pounds * 16) + ounces;
	kilograms = totalOunces * Wt_Conversion_Const;
	kilograms = Math.round(kilograms * 1000)/1000;
	document.getElementById(txtWtKgID).value = kilograms.toFixed(2);
}
// END WEIGHT CONVERSION ##################################################################################################################################
// HEIGHT CONVERSION ######################################################################################################################################
var Ht_Conversion_Const = 0.39370078740157477
// Params
// 1. The ID of the Height (cms) text box
// 2. The ID of the Height (Inches) text box
// 3. The ID of the Height (Feet) text box
function convfromcmeters(txtHtID, txtHtInID, txtHtFtID) 
{ 
	var cm2; 
	var feet2; 
	var inches2; 
	var cmeters2;
	var objHtCms = document.getElementById(txtHtID);
	if(isNaN(objHtCms.value)){ 
		alert("Please enter numbers only"); 
		return false; 
	}
	if(objHtCms.value > 0)
	{
		inches2 = (parseInt(objHtCms.value,10) * Ht_Conversion_Const );
	 
		if(inches2==0){ 
			alert("Please enter valid values into the boxes"); 
		}
		var in2 = inches2 % 12;
		document.getElementById(txtHtInID).value = (Math.round(in2*10))/10;
		document.getElementById(txtHtFtID).value = parseInt(inches2 / 12); 
	}

	return true;
 
}
// Params
// 1. The ID of the Height (cms) text box
// 2. The ID of the Height (Inches) text box
// 3. The ID of the Height (Feet) text box
function convfromftinches(txtHtID, txtHtInID, txtHtFtID) 
{ 
	
	var objInches = document.getElementById(txtHtInID);
	var objFt =	document.getElementById(txtHtFtID); 
	var objCms = document.getElementById(txtHtID);
	
	
	if((isNaN(objFt.value) || isNaN(objInches.value)))
	{
		alert("Please enter numbers only");	
		return false;
	}
	if(objFt.value == "")
	{
		objFt.value = 0;	
	}
	var ft = parseInt(objFt.value);
	if(objInches.value == "")
	{
		objInches.value = 0;	
	}
	var inches = parseFloat(objInches.value);	
	
	if(inches >= 12)
	{
		ft = (inches - (inches%12))/12;
		objFt.value = ft;
		inches = inches - (12*ft);
		objInches.value = inches;
	}
	
	var cms = 0;
	if(ft > 0 && inches > 0)
	{
		cms = ((ft * 12) + inches) * (1/Ht_Conversion_Const);
	}
	else if(ft > 0)
	{
		cms = (ft * 12) * (1/Ht_Conversion_Const);
	}
	else
	{
		cms = (inches) * (1/Ht_Conversion_Const);
	}
	//cms = parseInt(Math.round(cms*10)/10,10);
	cms = cms.toFixed(0);
	
	
	objCms.value = cms;
	
	
	return true;
 
}
// END HEIGHT CONVERSION ##################################################################################################################################
// LENGTH CONVERSION ######################################################################################################################################
var Length_Conversion_Const = 0.39370078740157477
// Params
// 1. The ID of the Length (cms) text box
// 2. The ID of the Length (Inches) text box
function convcmstoinches(txtLengthCmsID, txtLengthInID) 
{ 
	var inches;
	var objCms = document.getElementById(txtLengthCmsID);
	if(isNaN(objCms.value)){ 
		alert("Please enter numbers only"); 
		return false; 
	}
	if(objCms.value > 0)
	{
		inches = (parseInt(objCms.value,10) * Length_Conversion_Const );
	 
		if(inches==0){ 
			alert("Please enter valid values into the boxes"); 
		}		
		document.getElementById(txtLengthInID).value = inches.toFixed(2);
	}

	return true;
 
}
// Params
// 1. The ID of the Length (cms) text box
// 2. The ID of the Length (Inches) text box
function convinchestocms(txtLengthCmsID, txtLengthInID) 
{ 
	
	var objInches = document.getElementById(txtLengthInID);
	var objCms = document.getElementById(txtLengthCmsID);
	
	
	if(isNaN(objInches.value))
	{
		alert("Please enter numbers only");	
		return false;
	}	
	if(objInches.value == "")
	{
		objInches.value = 0;	
	}
	var inches = parseFloat(objInches.value);
	
	var cms = 0;
	cms = (inches) * (1/Length_Conversion_Const);
	
	cms = parseInt(Math.round(cms*10)/10,10);
	
	objCms.value = cms;
	
	
	return true;
 
}
//###########################################  Cholesterol ############################################################
var  cholesterolConversionFactor = 38.67
function doCholesterolConversionMML_To_MgDl(txtCholMML, txtCholMgDl)
{
	var objMML = document.getElementById(txtCholMML);
	var objMgDl = document.getElementById(txtCholMgDl);
	if(validNum(objMML))
	{		
		objMgDl.value = (parseFloat(objMML.value) * cholesterolConversionFactor).toFixed(1);		
	}
	else
	{
		objMgDl.value="";
	}
}

function doCholesterolConversionMgDl_To_MML(txtCholMML, txtCholMgDl)
{
	var objMML = document.getElementById(txtCholMML);
	var objMgDl = document.getElementById(txtCholMgDl);
	if(validNum(objMgDl))
	{		
		objMML.value = (parseFloat(objMgDl.value) / cholesterolConversionFactor).toFixed(1);		
	}
	else
	{
		objMML.value="";
	}
}
//####################################################################################################################
//###########################################  TriGlyceride ############################################################
var  triglycerideConversionFactor = 88.57
function doTriglycerideConversionMML_To_MgDl(txtTrigMML, txtTrigMgDl)
{
	var objMML = document.getElementById(txtTrigMML);
	var objMgDl = document.getElementById(txtTrigMgDl);
	if(validNum(objMML))
	{		
		objMgDl.value = (parseFloat(objMML.value) * triglycerideConversionFactor).toFixed(1);		
	}
	else
	{
		objMgDl.value="";
	}
}

function doTriglycerideConversionConversionMgDl_To_MML(txtTrigMML, txtTrigMgDl)
{
	var objMML = document.getElementById(txtTrigMML);
	var objMgDl = document.getElementById(txtTrigMgDl);
	if(validNum(objMgDl))
	{		
		objMML.value = (parseFloat(objMgDl.value) / triglycerideConversionFactor).toFixed(1);		
	}
	else
	{
		objMML.value="";
	}
}
//####################################################################################################################
//###########################################  Blood Glucose ############################################################
var  bloodGlucoseConversionFactor = 18.0
function doBloodGlucoseConversionMML_To_MgDl(txtBGMML, txtBGMgDl)
{
	var objMML = document.getElementById(txtBGMML);
	var objMgDl = document.getElementById(txtBGMgDl);
	if(validNum(objMML))
	{		
		objMgDl.value = (parseFloat(objMML.value) * bloodGlucoseConversionFactor).toFixed(1);		
	}
	else
	{
		objMgDl.value="";
	}
}

function doBloodGlucoseConversionMgDl_To_MML(txtBGMML, txtBGMgDl)
{
	var objMML = document.getElementById(txtBGMML);
	var objMgDl = document.getElementById(txtBGMgDl);
	if(validNum(objMgDl))
	{		
		objMML.value = (parseFloat(objMgDl.value) / bloodGlucoseConversionFactor).toFixed(1);		
	}
	else
	{
		objMML.value="";
	}
}
//####################################################################################################################


