function popup(url,title,w,h,r,s,m,t){
	//if vars aren't provided, use these defaults
	if (title==null) {
		title = 'popup';
	} if (w==null){
		w=600;
	} if (h==null) {
		h=570;
	} if (r==null) {
		r='yes';
	} if (s==null) {
		s='yes';
	} if (m==null) {
		m='no';
	} if (t==null) {
		t='no';
	}
	vars = 'width='+w+',height='+h+',resizable='+r+',scrollbars='+s+',menubar='+m+',toolbar='+t;
	var newWin = window.open(url,title,vars);
	newWin.focus();
	return false;
}
function redirect(url)
{
	window.location.href = url;
}

function confirmUpdate(url){
	if(confirm('Are you sure to Update?')){
		if(url != null){
			location.replace(url);
		}
		return true;
	}
	else
		return false;
}


function confirmDelete(url){
	if(confirm('Are you sure to delete?')){
		if(url != null){
			location.replace(url);
		}
		return true;
	}
	else
		return false;
}


function showTemplate(val,page)
{
	if(val!='select')
	{
		document.f1.action=page+"?template_val="+val;
		document.f1.submit();
	}
	//onChange="showTemplate(this.value,'create_brand.php')"
}
function verify_email()
{
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(document.f1.email.value)))
	{
		alert("Please enter a valid Email address!");
		document.f1.email.focus();
		return false;
	}
}
function suspend_brand(brandid,val)
{
	document.f1.action="create_brand.php?st=list&bstatus=brandstatus&checkedval="+val+"&brandid="+brandid;
	document.f1.submit();
}
function Validate_header_links()
{
	if(document.all['link_name'].value.length<=0)	
	{
		alert("Enter link name");
		document.all['link_name'].focus();
		return false;
	}
	else if(document.all['link_heading'].value.length<=0)	
	{
		alert("Enter page heading");
		document.all['link_heading'].focus();
		return false;
	}
}
function delete_brand(brandid,linkid)
{
	if(confirm("Are you sure to delete this link."))
	{
		window.location.href="add_informative_links.php?st=delete&brandid="+brandid+"&link_id="+linkid;
	}
}	
function Specialcharacterblock(e)
{
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	reg = /[\[\]{}\(\)\|\\\+\=\*\&\^\%\$\#\@\~\!\~\`\;\:\'\"\>\<\,\? ]/;
	return !reg.test(keychar);
}
function specialAlfaCharacterBlock(e)
{
	var key = window.event ? e.keyCode : e.which;
	var keychar = String.fromCharCode(key);
	reg = /[\[\]{}\(\)\|\\\+\=\*\&\^\%\$\#\@\~\!\~\`\;\:\'\"\>\<\,\? ]/;
	return !reg.test(keychar);
}
function validate_field()
{
	if(document.f1.club_name)
	{
		if(document.f1.club_name.value.length<=0)
		{
			alert("Enter Club Name.")
			document.f1.club_name.focus();
			return false;
		}
	}
	if(document.f1.username.value.length<=0)
	{
		alert("Enter Username.")
		document.f1.username.focus();
		return false
	}
	else if(document.f1.password.value.length<=0)
	{
		alert("Enter Password.")
		document.f1.password.focus();
		return false;
	}
	else if(document.f1.conf_password.value.length<=0)
	{
		alert("Enter Confirmation Password.")
		document.f1.conf_password.focus();
		return false;
	}
	else if(document.f1.password.value!=document.f1.conf_password.value)
	{
		alert("Password does not match.")
		document.f1.password.focus();
		return false;
	}
	else if(document.f1.firstname.value.length<=0)
	{
		alert("Enter First Name.")
		document.f1.firstname.focus();
		return false;
	}
	else if(document.f1.lastname.value.length<=0)
	{
		alert("Enter Lastname.")
		document.f1.lastname.focus();
		return false;
	}
	if(document.f1.email.value.length<=0)
	{
		alert("Enter email.")
		document.f1.email.focus();
		return false;
	}
	else
	{
		return verify_email();
	}
	if(document.f1.access_code)
	{
		if(document.f1.access_code.value.length<=0  || document.f1.access_code.value==0)
		{
			alert("Enter promotional code !")
			document.f1.access_code.focus();
			return false;
		}
	}
}
function header_link_show(st,brandid)
{
	document.f1.action="add_informative_links.php?status=changestyle&brand_list="+brandid+"&val="+st;		
	document.f1.submit();
}
function validate_admin()
{
	if(document.f1.email.value.length<=0)	
	{
		alert("Enter email address!")
		document.f1.email.focus();
		return false;
	}
	else if(document.f1.oldpasswd.value.length<=0)
	{
		alert("Enter old password!")
		document.f1.oldpasswd.focus();
		return false;
	}
	else if(document.f1.newpasswd.value.length<=0)
	{
		alert("Enter New password");
		document.f1.newpasswd.focus();
		return false;
	}
	else if(document.f1.cfmpasswd.value.length<=0)
	{
		alert("Enter confirm password!")
		document.f1.cfmpasswd.focus();
		return false;
	}
	if(document.f1.newpasswd.value!=document.f1.cfmpasswd.value)
	{
		alert("New password does not match!")
		document.f1.newpasswd.focus();
		return false;
	}
}
function change_user_status(club_id,attch)
{
	//alert(attch);
	if(document.getElementById('status_'+club_id).checked)
	{
		document.f1.action=attch+"suspend/"+club_id+"/";
	}
	else
	{
		document.f1.action=attch+"activate/"+club_id+"/";
	}
	//alert(document.f1.action);
	document.f1.submit();
}
function show_brand_permission(brand_id)
{
		document.f1.action="brand_permission.php";
		document.f1.submit();
}
function brand_permission(brand_id,module_name)
{
	if(module_name=='all')
	{
		if(document.getElementById('all_feature').checked==true)
		{
			document.f1.action="brand_permission.php?brand_list="+brand_id+"&module_name="+module_name+"&st=activate";
		}
		else if(document.getElementById('all_feature').checked==false)
		{
			document.f1.action="brand_permission.php?brand_list="+brand_id+"&module_name="+module_name+"&st=suspend";
		}
	}
	else
	{
		if(document.getElementById(module_name).checked==true)
		{
			document.f1.action="brand_permission.php?brand_list="+brand_id+"&module_name="+module_name+"&st=activate";
		}
		else if(document.getElementById(module_name).checked==false)
		{
			document.f1.action="brand_permission.php?brand_list="+brand_id+"&module_name="+module_name+"&st=suspend";
		}
	}
	document.f1.submit();
}
function unlimited()
{
	if(document.f1.nolimit.checked==false)
	{
		document.f1.no_users.value="";
		document.f1.no_users.disabled=false;
	}
	if(document.f1.nolimit.checked==true)
	{
		document.f1.no_users.value="";
		document.f1.no_users.disabled=true;
	}
}


	//////CALCULATE CALORIC VALUE////////////////////	
function CAL_caloric(obj)
{
	var BMR = CAL_Matabolic(obj);
	var caloric_maintenence;
	var Lifestyle;
	for(var i=0; i<obj.lifestyle.length;i++){
		if (obj.lifestyle[i].checked==true){
			Lifestyle=obj.lifestyle[i].value;
		}
	}
	if(Lifestyle==1){
		calorie_caculation	=	BMR * 1.2; 
	}
	else if(Lifestyle==2){
		calorie_caculation	=	BMR * 1.375;
	}
	else if(Lifestyle==3){
		calorie_caculation	=	BMR * 1.55;  
	}	
	else if(Lifestyle==4){
		calorie_caculation	=	BMR * 1.725; 
	}	
	else if(Lifestyle==5){
		calorie_caculation	=	BMR * 1.9; 
	}
	for (var i=0; i < obj.dietary_goal.length; i++)
	{
		if (obj.dietary_goal[i].checked)
		{
			var health_goal = obj.dietary_goal[i].value;
		}
	}	
	adjusted_calories	=	(health_goal==1)? 500 : ((health_goal==2) ? -500 :0) ;
	total_needed_cal= parseInt(calorie_caculation) + parseInt(adjusted_calories);	
	obj.Caloric_Need.value=Math.round(total_needed_cal);
}


///////////CALCULATE BMR ////////////////////////////
function CAL_Matabolic(obj)
{
	if(!obj){
		var obj		=	document.f1;
	}
	var dobyear	=	obj.year.value;
	var myDate	=	new Date();
	var curyear	= 	myDate.getYear();
	var age		=	curyear - dobyear;
	var heightInches	=	obj.height_feet.value * parseInt(12);
	heightInches		=	parseInt(heightInches) + parseInt(obj.height_inches.value);
	var	heightCms		= 	parseFloat(heightInches*2.54);
	var	weight			= 	parseInt(obj.weight.value);
	for (var i=0; i < obj.gender.length; i++)
	{
		if (obj.gender[i].checked)
		{
			var gender = obj.gender[i].value;
		}
	}
	for (var i=0; i < obj.lifestyle.length; i++)
	{
		if (obj.lifestyle[i].checked)
		{
			var lifestyle = obj.lifestyle[i].value;
		}
	}
	
	if(gender=="female")
	{
		var	BMR = 655 + parseFloat((9.6 * weight)) + parseFloat((1.8 * heightCms)) - parseFloat((4.7 * age));		 
	}
	else if(gender=="male")
	{
		var BMR = 66 + parseFloat((13.7 * weight )) + parseFloat((5 * heightCms)) - parseFloat((6.8 * age ));
	}
	document.f1.bmr.value=Math.round(BMR);
	return BMR;
}
 //Function to validate the form,fieldRequired,fieldDescription
function validate(formobj,fieldRequired,fieldDescription,email)
{
	var alertMsg = "Following fields are missing or incorrect :\n";
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj     = formobj.elements[fieldRequired[i]];
		if (obj){
			
			switch(obj.type){
			case "select-one":
				if(obj.options.length > 1){
					if (obj.selectedIndex == 0 || obj.options[obj.selectedIndex].text == ""){
						alertMsg += " - " + fieldDescription[i] + "\n";
					}
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "password":
			case "textarea":
				if (trim_string(obj.value) == "" || trim_string(obj.value) == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				
				if(obj.id == 'conf_password' && trim_string(obj.value) != "")
				{
					tempMsg = confirm_password(formobj);
					if(tempMsg != '')
						alertMsg += tempMsg; 
				}
				break;
			case "file":
				if (trim_string(obj.value) == "" || trim_string(obj.value) == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}else{
					msg = extension_check(obj.value);
					if(msg != '')
					alertMsg += msg;
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}
	if(email == 1){
		if(formobj.elements['email'].value != ''){
			if(!verify_emailID(formobj.elements['email'].value))
			alertMsg = ' - Enter valid E-Mail address'; 
		}
	}
	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}
function extension_check(str){
	str = str.substring(str.lastIndexOf('.'),str.length);
	str = str.toLowerCase();
	if(str != '.csv' && str != '.xls'){
		msg = ' - File Type is not allowed'; 
	}else{
		msg ='';
	}
	return msg;
}
//check for password is equal to confirm password

function confirm_password(formobj)
{
	var msg ='';
	var password = formobj.elements['password'];
	var conPass  = formobj.elements['conf_password'];
	if(trim_string(password.value) != trim_string(conPass.value))
	{
		msg = ' - Confirm password \n';
	}
	return msg;
}

function trim_string (str) 
{
  while (str.charAt(0) == ' ')
    str = str.substring(1);
  while (str.charAt(str.length - 1) == ' ')
    str = str.substring(0, str.length - 1);
  return str;
}

function verify_emailID(str){
	if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(str))){
		return false;
	}
	else{
		return true;
	}
}
//Function to check or uncheck all the check box in a form.
function set_all_chk(obj){
	var set = document.getElementById('setAll');
	if(set.checked == true){
		for(i=0;i<obj.elements.length;i++){
			obj[i].checked = true;
		}
	}
	else{
		for(i=0;i<obj.elements.length;i++){
			obj[i].checked = false;
		}
	}
 }

function showhide_goalweight(id){
	if(id ==3){
		goalweightbody.style.display = 'none';
	}
	else{
		goalweightbody.style.display = 'inline';
	}
}







function show_exercise(url,loadingName,datadisplayName,ID,paramstr){
	//alert('url='+url+" lodingName="+loadingName+" datadisplayName="+datadisplayName+" ID="+ID+" paramstr="+paramstr);
	var Digital = new Date();
	var seconds = Digital.getSeconds();
	var url= url+"?"+seconds+"&ID="+ID;
	if(paramstr){
		url=url+paramstr;
	}
	xmlHttp=GetXmlHttpObject(stateChangedState);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
	
	function stateChangedState(){ 
		if(xmlHttp.readyState!=4){
			document.getElementById(datadisplayName).innerHTML = 'Loading....';			
//			document.getElementById(loadingName).options[0].text = 'Loading....';
		}
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
			document.getElementById(datadisplayName).innerHTML = xmlHttp.responseText;
		} 
	}
}

/*function show_exercise(url,loadingName,datadisplayName,ID,paramstr){
	
	//alert('url='+url+" lodingName="+loadingName+" datadisplayName="+datadisplayName+" ID="+ID+" paramstr="+paramstr);
	var Digital = new Date();
	var seconds = Digital.getSeconds();
	var url= url+"?"+seconds+"&ID="+ID;
	if(paramstr){
		url=url+paramstr;
	}
	xmlHttp=GetXmlHttpObject(stateChangedState);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
	function stateChangedState(){ 
		if(xmlHttp.readyState!=4){
			document.getElementById(datadisplayName).innerHTML = 'Loading....';			
//			document.getElementById(loadingName).options[0].text = 'Loading....';
		}
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			alert(xmlHttp.responseText);
			document.getElementById(datadisplayName).innerHTML = xmlHttp.responseText;
		} 
	}
}*/



function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null
	
	if (navigator.userAgent.indexOf("Opera")>=0)
	{
		alert("This doesn't work in Opera") 
		return 
	}
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
		{
			strName="Microsoft.XMLHTTP"
		} 
		try
		{ 
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler 
			return objXmlHttp
		} 
		catch(e)
		{ 
			alert("Error. Scripting for ActiveX might be disabled") 
			return 
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
} 

function showHideDiv(szDivID){
	el = document.getElementById(szDivID);
	var display = el.style.display ? '' : 'none';
	el.style.display = display; 

}

function showMealDesc(QTY,ProId,MealQty,Protein,Carbs,Fats,Cal){
//	return specialAlfaCharacterBlock(event);" onChange=

	document.getElementById('TDProtein_'+ProId).innerHTML=((Protein/MealQty)*QTY).toFixed(2);
	document.getElementById('TDCarbs_'+ProId).innerHTML=((Carbs/MealQty)*QTY).toFixed(2);
	document.getElementById('TDFats_'+ProId).innerHTML=((Fats/MealQty)*QTY).toFixed(2);
	document.getElementById('TDCal_'+ProId).innerHTML=((Cal/MealQty)*QTY).toFixed(2);	
}

function drop(dropMsg){
	if(confirm(dropMsg))
		return true;
	else
		return false;
}

function drop_validate(obj){
	chkFlag = 0;
	if(obj){
		for(i=0; i< obj.elements.length; i++){
			if(obj.elements[i].type=='checkbox'){
				if(obj.elements[i].checked == true){
					chkFlag = 1;
					break;
				}
			}
		}
	}
	else{
		alert('Error in script');
		return false;
	}
	if(chkFlag == 1){
		return drop('Are you sure to delete?');
	}
	else{
		alert('Please select at least one record to delete!');
		return false;
	}
}


function status(){
	if(confirm('Are you sure to change status of following records?'))
		return true;
	else
		return false;
}

function change_status(obj){
	chkFlag = 0;
	if(obj){
		for(i=0; i< obj.elements.length; i++){
			if(obj.elements[i].type=='checkbox'){
				if(obj.elements[i].checked == true){
					chkFlag = 1;
					break;
				}
			}
		}
	}
	else{
		alert('Error in script');
		return false;
	}
	if(chkFlag == 1){
		return status();
	}
	else{
		alert('Please select at least one record to change status!');
		return false;
	}
}


function char_counter(element,span,message,charCount) 
{
  var elementObj	=	document.getElementById(element);
  var spanObj		=	document.getElementById(span);
  var remaining		=	charCount - elementObj.value.length;
  if(remaining <= 0) 
  {
    	remaining	=	0;
    	message		=	'<span class="disable"> '+message+' </span>';
    	elementObj.value=elementObj.value.substr(0,charCount);
  }
  spanObj.innerHTML = message.replace("{CHAR}",remaining);
}