function doSubmitWeightRecord()
 {
   with(document.frmWeight)
    {
      var fnLbs = txtLbs.value;

      if(fnLbs==null || trim(fnLbs)=="")
       {
         alert("You must specify the Weight!");
         txtLbs.focus();
         return;
       }
      else if(isNaN(fnLbs))
       {
         alert("Weight must be a numeric value!");
         txtLbs.focus();
         return;
       }
      submit();
    }
 }
function openNewWindow(fnURL)
 {
   //set up window options
   stats='toolbar=no,location=no,directories=no,status=no,menubar=no,'
   stats += 'scrollbars=no,resizable=no,width=625,height=300'
   //open the window
   window.open (fnURL,"Weight Records",stats);
 }

function doEditWeightRecord(fnID,fnWeight,fnDate)
 {
   with(document.frmWeight)
    {
      hID.value = fnID;
      hWeight.value = fnWeight;
      hDate.value = fnDate;
      hJob.value = "edit";
      submit();
    }
 }
function doDeleteWeightRecord(fnID,fnDate)
 {
   var x = confirm("Are you sure you want to delete this record?")
   if(x)
    {
     with(document.frmWeight)
      {
        hID.value = fnID;
        hDate.value = fnDate;
        hJob.value = "delete";
        submit();
      }
    }
 }
function doSubmitDietContent()
 {
   with(document.frmDietSettings)
    {
      getBlockContent();
      getEffort();
      submit();
    }
 }
function getBlockContent()
 {
   var c = 0;
   var fnContent = "";
   with(document.frmDietSettings)
    {
      for(c=0;c<9;c++)
         if(chkBlockContent[c].checked==1) fnContent += chkBlockContent[c].value + ",";
      hBlockContent.value = fnContent.substring(0,fnContent.length-1);
    }
 }
function getEffort()
 {
   var c = 0;
   var fnEffort = "";
   with(document.frmDietSettings)
    {
      for(c=0;c<4;c++)
         if(chkEffort[c].checked==1) fnEffort += chkEffort[c].value + ",";
      hEffort.value = fnEffort.substring(0,fnEffort.length-1);
    }
 }

function getBox(url, boxID)
{
	try
	{
		var requester = new XMLHttpRequest();
	}
	catch(error)
	{
		try
		{
			var requester = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(error)
		{
			alert('Error creating the connection!'+e);
		}
	}

	requester.open("GET", url);
	requester.send(null);

	if(requester.readyState==4)
	{
		if (requester.status==200)
		{
			var response = requester.responseText;

			var theBox = document.getElementById(boxID);
			theBox.innerHTML = response;

		}
		else if (requester.status != 0)
		{
			alert("There was an error while retrieving the page: " + requester.statusText);
		}
	}
	else
	{
		var theBox = document.getElementById(boxID);
		theBox.innerHTML = '<div class="ajax-loading"><img src="themes/default/images/indicator.gif" text-align="middle"> Please wait while loading...</div>';
		requester.onreadystatechange = stateHandler;
	}


	function stateHandler()
	{
		if(requester.readyState==4)
		{
			if (requester.status==200)
			{
				var response = requester.responseText;
				var theBox = document.getElementById(boxID);
				theBox.innerHTML = response;
			}
			else if (requester.status != 0)
			{
				alert("There was an error while retrieving the page: " + requester.statusText);
			}
		}
		return true;
	}

}

function trim(s) 
{
	while (s.substring(0,1) == ' ') 
	{
		s = s.substring(1,s.length);
	}
	
	while (s.substring(s.length-1,s.length) == ' ') 
	{
		s = s.substring(0,s.length-1);
	}
	return s;
}

function deleteConfirm(objGroup, damnedObj)
{
	
	return confirm('Are you sure you want to delete the ' + objGroup + ' named `' + damnedObj + '` ?');
	
}

function setTabs(tabNum, dietId)
{
		
	for(i=1; i<=7; i++)
	{
		if(tabNum == i)
		{
			document.getElementById('tabLink' + i).className  = 'active-tab';
			
		}
		else
		{
			document.getElementById('tabLink' + i).className = 'normal-tab';
			
		}	
	}
	
	getBox('index.php?module=admin&action=ajaxed_edit_diet_plan&id=' + dietId +'&dayNum='+tabNum, 'nutrientDiv');	
}

function isValidFloat(S) 
{
    return /^\d+(\.\d+)?$/.test(S);
}

function isNumeric(S)
{
	return /^\d+\.*\d*$/.test(S);
}

function isAlphaNumeric(S)
{
	return /^[\w\d]+$/.test(S);
}

function isValidEmail(S)
{
  return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(S);
}


function validateSignUp()
{
	
	var objContainerDiv = document.getElementById('profile-error');
	var strInnerHtml    = '';
	var strError        = ''
	
	var gw = document.getElementById('goal_weight');
	if( trim(gw.value) == '' )
	{
		strError += '<li>Goal Weight is Required</li>';
	}
	else if( !isValidFloat(trim(gw.value)) )
	{
		strError += '<li>Please Enter a Valid Value for Goal Weight</li>';
	}
	
	var cw = document.getElementById('current_weight');
	if( trim(cw.value) == '' )
	{
		strError += '<li>Current Weight is Required</li>';
	}
	else if( !isValidFloat(trim(cw.value)) )
	{
		strError += '<li>Please Enter a Valid Value for Current Weight</li>';
	}
	
	var age = document.getElementById('age');
	if( trim(age.value) == '' )
	{
		strError += '<li>Age is Required</li>';
	}
	else if( !isNumeric(trim(age.value)) )
	{
		strError += '<li>Please Enter a Valid Value for Age</li>';
	}
	else if( age.value < 10 || age.value > 80)
	{
		strError += '<li>Please Enter a Valid Value for Age</li>';
	}
	
	var fn = document.getElementById('first_name');
	if( trim(fn.value) == '' )
	{
		strError += '<li>First Name is Required</li>';
	}
	
	var ln = document.getElementById('last_name');
	if( trim(ln.value) == '' )
	{
		strError += '<li>Last Name is Required</li>';
	}
	
	var em = document.getElementById('email');
	if( trim(em.value) == '' )
	{
		strError += '<li>Email is Required</li>';
	}
	else if( !isValidEmail(trim(em.value)) )
	{
		strError += '<li>Please Enter a Valid Email Address</li>';
	}
	
	if( strError != '' )
	{
		
		strInnerHtml = '<b>Please answer all questions so we can provide the best diet plan for you.</b>';
		strInnerHtml += '<ul>' + strError + '</ul>'
		objContainerDiv.innerHTML = '';	
		objContainerDiv.innerHTML = strInnerHtml;
		objContainerDiv.style.display = 'block';
		location.hash = "profile-error";
		
		return false;
	}
	else
	{
		return true;	
	}
		
	
}

function validateAccount()
{
	
	var objContainerDiv = document.getElementById('error-box');
	objContainerDiv.innerHTML = '';
	var strInnerHtml          = '';
	var strError              = ''
	
	
	var un    = document.getElementById('uname');
	var unVal = trim(un.value);
	var unLen = unVal.length;
	if( unVal == '' )
	{
		strError += '<b>&bull;</b>&nbsp;Username is Required<BR>';
	}
	else if( !isAlphaNumeric(unVal) )
	{
		strError += '<b>&bull;</b>&nbsp;Please use only alpha numeric values<BR>';
	}
	else if( unLen <=7 )
	{
		strError += '-Username should have at least 8 characters<BR>';
	}
		
	var pw    = document.getElementById('pword');
	var pwVal = trim(pw.value);
	if( pwVal == '' )
	{
		strError += '<b>&bull;</b>&nbsp;Password is required<BR>';
	}
	
	var re_pw    = document.getElementById('re_pword');
	var re_pwVal = trim(re_pw.value);
	if( re_pwVal == '' )
	{
		strError += '<b>&bull;</b>&nbsp;Re-enter your password<BR>';
	}
	else if( pwVal != re_pwVal )
	{
		strError += '<b>&bull;</b>&nbsp;Passwords does not match<BR>';
	}
		
	// Validate credit card.
	var creditCard  = document.getElementById('credit_card_no');
	
	if( creditCard.value == '' )
	{
		strError += '<b>&bull;</b>&nbsp;Please enter your credit card number.<BR>';
	}
	else if (!isNumeric(creditCard.value) )
	{
		strError += '<b>&bull;</b>&nbsp;Invalid credit card format.<BR>';
	}
	
	if (trim(document.getElementById('first_name').value) == '') {
		strError += '<b>&bull;</b>&nbsp;Please enter your first name.<BR>';
	}
	
	if (trim(document.getElementById('last_name').value) == '') {
		strError += '<b>&bull;</b>&nbsp;Please enter your last name.<BR>';
	}
	
	if (trim(document.getElementById('address1').value)  == '') {
		strError += '<b>&bull;</b>&nbsp;Please enter your address.<BR>';
	}
	
	if (trim(document.getElementById('city').value)  == '') {
		strError += '<b>&bull;</b>&nbsp;Please enter your city.<BR>';
	}

	if (trim(document.getElementById('zip').value)  == '') {
		strError += '<b>&bull;</b>&nbsp;Please enter your zip code.<BR>';
	}else if (!isNumeric(document.getElementById('zip').value)) {
		strError += '<b>&bull;</b>&nbsp;Invalid zip code.<BR>';	    
	} else if (document.getElementById('zip').value.length < 3 || document.getElementById('zip').value.length > 5) {
		strError += '<b>&bull;</b>&nbsp;Invalid zip code length.<BR>';	    	    
	}
	
	if (!document.getElementById('terms_condition').checked)
	{
		strError += '<b>&bull;</b>&nbsp;You must agree to our <a href="http://www.smartdiets.com/tc.html" target="_blank">Terms &amp; Conditions.</a><BR>';
	}
	
	if( strError != '' )
	{
		strInnerHtml = '<strong>Please correct the errors below:</strong>';
		strInnerHtml += '<ul>' + strError + '</ul>'
		objContainerDiv.innerHTML = strInnerHtml;
		objContainerDiv.style.display = 'block';
		location.hash = "error-box";
		
		return false;
	}
	else
	{
		return true;	
	}
	

}

function getKeyCode(e)
{
 
	if (window.event) return window.event.keyCode;
 	else if (e) return e.which;
 	else return null;
}


function keyRestrict(e, validchars) 
{
  	
	var letters = 'abcdefghijklmnopqrstuvwqyz';
	var numbers = '0123456789';
	var letterspace = letters + ' ';
	var numberspace = numbers + ' ';
	var numberpoint = numbers + '.';
	
	switch( validchars )
	{
		case 'letters': validchars = letters; break;
		case 'numbers': validchars = numbers; break;
		case 'letterspace': validchars = letterspace; break;
		case 'numberspace': validchars = numberspace; break;
		case 'numberpoint': validchars = numberpoint; break;
	}
	
	var key='', keychar='';
   	key = getKeyCode(e);
   
   	if (key == null) return true;
	
   	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	validchars = validchars.toLowerCase();
	if (validchars.indexOf(keychar) != -1)  return true;
 	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )  return true;
 	
 	return false;
	
}

function onKeyPressBlockNumbers(e)
{
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	reg = /\d/;
	return !reg.test(keychar);
}

