//  Arena Healthy Lifestyle Program JavaScript Document

//Validate Email for main index files//

function validateEmail( emailVal )
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if( filter.test(emailVal) )
		return true;
		
	alert('Invalid email syntax.');
	return false;
}

function validateLogin( lForm )
{
	if( lForm.loginUser.value == '' || lForm.loginPass.value == '' )
	{
		alert('Username and password are required to login.');
		return false;
	}
	
	return true;
}

function validateRegister( rForm )
{
	var err = '';
	
	if( rForm.registerEmail.value == '' )	
		err += 'Email is required.\n';
	else if( !validateEmail( rForm.registerEmail.value ) )
		err += 'Email is invalid.\n';
	
	if( err != '' )
	{
		alert('Please fix the following:\n\n'+err);
		return false;	
	}
	
	return true;	
}

// Calculators//

function conv_Run( pForm )
{
	if( pForm.convertType.selectedIndex == 0 )
		p2k_Run(pForm);
	else if( pForm.convertType.selectedIndex == 1 )
		k2p_Run(pForm);
	else if( pForm.convertType.selectedIndex == 2 )
		m2i_Run(pForm);
	else if( pForm.convertType.selectedIndex == 3 )
		i2m_Run(pForm);
	else
		alert('Please select a conversion type.');
	
	return false;
}

function p2k_Run( pForm )
{
	if( pForm.convertInitValue.value )
	{
		var kilos = Math.round((pForm.convertInitValue.value * 0.45359237)*100)/100;
		document.getElementById("convertValue").innerHTML = kilos+' kilos';
	}
	else
		alert('Please enter a value to convert.');
	
	return false;
}

function k2p_Run( pForm )
{
	if( pForm.convertInitValue.value )
	{
		var pounds = Math.round((pForm.convertInitValue.value / 0.45359237)*100)/100;
		document.getElementById("convertValue").innerHTML = pounds+' pounds';
	}
	else
		alert('Please enter a value to convert.');
	
	return false;
}

function i2m_Run( pForm )
{
	if( pForm.convertInitValue.value )
	{
		var meters = Math.round((pForm.convertInitValue.value / 39.3700787)*100)/100;
		document.getElementById("convertValue").innerHTML = meters+' meters';
	}
	else
		alert('Please enter a value to convert.');
	
	return false;
}

function m2i_Run( pForm )
{
	if( pForm.convertInitValue.value )
	{
		var inches = Math.round((pForm.convertInitValue.value / 0.0254)*100)/100;
		document.getElementById("convertValue").innerHTML = inches+' inches';
	}
	else
		alert('Please enter a value to convert.');
	
	return false;
}

function kcal_Run( pForm )
{
	var error = '';
	var gender = '';
	var ratio;
	var calculation;
	var weight = pForm.kcalWeight.value;
	
	if( weight <= 0 )
	{
		alert('Please enter a valid weight.');
		return false;
	}
	
	if( pForm.kcalGender[0].checked ) gender = "M";
	else if( pForm.kcalGender[1].checked ) gender = "F";
	
	if( pForm.kcalRatio[0].checked ) ratio = 1.3;
	else if( pForm.kcalRatio[1].checked ) ratio = 1.4;
	
	if( gender == '' )
	{
		alert('Gender is required.');
		return false;
	}
	
	if( gender == "M" )
	 	calculation = ((11.60 * weight + 879) * ratio) - 600;
	else if( gender == "F" )
		calculation = ((8.70 * weight + 826) * ratio) - 600;
	
	document.getElementById("kcalValue").innerHTML = Math.round(calculation*100)/100+' kcal';
	
	return false;
}

function bmi_Run( pForm )
{
	var error = '';
	var calculation;
	var type;
	var weight = pForm.bmiWeight.value;
	var height = pForm.bmiHeight.value
	
	if( weight <= 0 )
	{
		alert('Please enter a valid weight.');
		return false;
	}
	if( height <= 0 )
	{
		alert('Please enter a valid height.');
		return false;
	}
	
	if( pForm.bmiType[0].checked ) type = "M";
	else if( pForm.bmiType[1].checked ) type = "I";
	
	if( type == "M" )
	 	calculation = weight / (height * height);
	else if( type == "I" )
		calculation = 703 * (weight / (height * height));
	
	document.getElementById("bmiValue").innerHTML = Math.round(calculation*100)/100+' kg/m^2';
	
	return false;
}
// Popups //
var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

// Form Validation Script //
function validateForm( theForm )
{
	var error = '';
	if( theForm.site_name.value == '' )
		error += "Site Name is required.\n";
	if( theForm.PI_name.value == '' )
		error += "PI Name is required.\n";
	if( theForm.contact_name.value == '' )
		error += "Contact Name is required.\n";
	if( theForm.phone.value == '' )
		error += "Phone is required.\n";
	else
	{	
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
		if( !filter.test(theForm.email.value) )
			error += "Invalid Email Address.\n";
	}
	
	if( error != '' )
	{
		alert("Please fix the following errors:\n\n"+error);
		return false;
	}
	
	return true;
}
