
function cmtrim(str) {
	return str.replace(/^ */,"").replace(/ *$/,"");
}
function isDigit(str) {
    var reDigit = /^\d/
    return reDigit.test(str)
}
//function to check valid email address
function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
   if (strEmail.search(validRegExp) == -1)
		return false;
    else return true;
}
function isDate(theField){
  var dPart = theField.replace("-"," ");
  var nDate = new Date(dPart);
  if(nDate.getDate()!=dPart.split(" ")[0]){
    return false;
  }
 return true; 
 }
 function CheckDecimal(strValue){
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
  return objRegExp.test(strValue);

}
function ValidateEmail(oEmail) {
   	if (cmtrim(oEmail.value) == "") { alert('Please enter Email Address.'); oEmail.focus(); return false; }
	if (!isValidEmail(oEmail.value)) { alert('Please enter a valid email address.'); oEmail.focus(); return false; }
	return true;
}
function ValidateJoinUp(Oper) {

	var sCtl = "Joinup_" ;
	
	if (Oper == "U")
		sCtl = "UpdateProfile_" ;
	
    if (cmtrim(document.getElementById(sCtl + "FirstName").value) == "") { alert("Please enter First name"); document.getElementById(sCtl + "FirstName").focus(); return false; } 
    if (cmtrim(document.getElementById(sCtl + "LastName").value) == "") { alert("Please enter Last name"); document.getElementById(sCtl + "LastName").focus(); return false; } 
    if (cmtrim(document.getElementById(sCtl + "Position").value) == "") { alert("Please enter Position"); document.getElementById(sCtl + "Position").focus(); return false; } 
    //if (cmtrim(document.getElementById(sCtl + "Organisation").value) == "") { alert("Please enter Organisation"); document.getElementById(sCtl + "Organisation").focus(); return false; } 
    if (cmtrim(document.getElementById(sCtl + "Address1").value) == "") { alert("Please enter Address1"); document.getElementById(sCtl + "Address1").focus(); return false; } 
    //if (cmtrim(document.getElementById(sCtl + "Address2").value) == "") { alert("Please enter Address2"); document.getElementById(sCtl + "Address2").focus(); return false; } 
    if (cmtrim(document.getElementById(sCtl + "Suburb").value) == "") { alert("Please enter Suburb"); document.getElementById(sCtl + "Suburb").focus(); return false; } 
    if (cmtrim(document.getElementById(sCtl + "Postcode").value) == "") { alert("Please enter Postcode"); document.getElementById(sCtl + "Postcode").focus(); return false; } 
    if (cmtrim(document.getElementById(sCtl + "Country").value) == "") { alert("Please enter Country"); document.getElementById(sCtl + "Country").focus(); return false; } 
    if (cmtrim(document.getElementById(sCtl + "State").value) == "") { alert("Please enter State"); document.getElementById(sCtl + "State").focus(); return false; } 
    if (!ValidateEmail(document.getElementById(sCtl + "Email"))) return false ; 
    if (cmtrim(document.getElementById(sCtl + "Username").value) == "") { alert("Please enter Username"); document.getElementById(sCtl + "Username").focus(); return false; } 
    if (cmtrim(document.getElementById("Password").value) == "") { alert("Please enter Password"); document.getElementById("Password").focus(); return false; } 
    if (cmtrim(document.getElementById("ConfirmPassword").value) == "") { alert("Please enter Confirm Password"); document.getElementById("ConfirmPassword").focus(); return false; } 
    if (document.getElementById("ConfirmPassword").value != document.getElementById("Password").value) { alert("Password and confirm password does not match"); document.getElementById("Password").focus(); return false; } 
	if (Oper == "J")
    {
	    if (document.getElementById("chk_term_condition").checked == false) { alert("Please read and read and accept the MLA Privacy statement"); document.getElementById("chk_term_condition").focus(); return false; } 
	}
    
    document.getElementById(sCtl + "hdnSubmit").value = "Y";
    if (Oper == "J")
		document.frmJoinUp.submit();
    else
    	document.frmUpdateProfile.submit();
	return true;
}

function ValidateLoginForm(sCtl) {
   if (cmtrim(document.getElementById(sCtl + "Username").value) == "") { alert("Please enter Username"); document.getElementById(sCtl + "Username").focus(); return false; } 
   if (cmtrim(document.getElementById(sCtl + "Password").value) == "") { alert("Please enter Password"); document.getElementById(sCtl + "Password").focus(); return false; } 
   document.getElementById(sCtl + "hdnSubmit").value = "Y";
   document.frmLogin.submit();
   return false;
}
function ValidateUpdateRecipeForm() {
   if (cmtrim(document.getElementById("RecipeId").value) == "") { alert("Please enter Recipe id"); document.getElementById("RecipeId").focus(); return false; } 
   if (!isDigit(document.getElementById("RecipeId").value)) { alert("Recipe id should be numeric"); document.getElementById("RecipeId").focus(); return false; } 
   document.getElementById("hdnSubmit").value = "Y";
   document.frmRecipeOfMonth.submit();
   return false;

}
function ValidateAmount(oField) {
	if (cmtrim(oField.value) != "") {
		if (!isDigit(oField.value)) { alert("Please enter valid amount"); oField.focus(); return false; } 
		if (oField.value > 100) { alert("Amount should be less than 100"); oField.focus(); return false; } 
	}
    return true;
}
function ValidatePatientResources1(Id) {
	var Frm = document.formPatientResources ;
	var bflag = "Y";
	
	var temp = new Array();
	temp = Id.split(',');


	for(var i=0; i<temp.length; i++) {
		if (cmtrim(eval('Frm.txt_resource' + temp[i]).value) != "") {
			bflag = "N";
		}
	}
	if (bflag=="Y"){
		alert("Please enter amount"); Frm.txt_resource1.focus(); return false; 
	}
	for(var i=0; i<temp.length; i++) {
		if (!ValidateAmount(eval('Frm.txt_resource' + temp[i]))) return false;	
	}
	Frm.hdnSubmit.value = "Y";
    Frm.submit();
  	return true;
 }
function ValidatePatientResources() {
	var Frm = document.formPatientResources ;
	if (cmtrim(Frm.txt_resource1.value) == "" && cmtrim(Frm.txt_resource2.value) == "" &&
		cmtrim(Frm.txt_resource3.value) == "" && cmtrim(Frm.txt_resource4.value) == "" &&
		cmtrim(Frm.txt_resource5.value) == "" && cmtrim(Frm.txt_resource6.value) == "") {
			alert("Please enter amount"); Frm.txt_resource1.focus(); return false; 
	}
	if (!ValidateAmount(Frm.txt_resource1)) return false;
	if (!ValidateAmount(Frm.txt_resource2)) return false;
	if (!ValidateAmount(Frm.txt_resource3)) return false;
	if (!ValidateAmount(Frm.txt_resource4)) return false;
	if (!ValidateAmount(Frm.txt_resource5)) return false;
	if (!ValidateAmount(Frm.txt_resource6)) return false;
    Frm.hdnSubmit.value = "Y";
    Frm.submit();
  	return true;
}
function ValidateSearchRecipeFields(ctl) {
/*	if (cmtrim(document.getElementById(ctl + "txt_keywords").value) == "" &&
		document.getElementById(ctl + "opt_CookingStyle").value == "" &&
		document.getElementById(ctl + "opt_MeatType").value == "" &&
		document.getElementById(ctl + "opt_DietType").value == "") {
		alert("Please select one of the search criteria"); 
		document.getElementById(ctl + "txt_keywords").focus();
		return false;
	}*/
	document.getElementById(ctl + "hdnSubmit").value = "Y";
	document.getElementById("formRecipeSearch").submit();
	return true;
}
function ValidateNutrientSearch(ctl) {
	if (cmtrim(document.getElementById(ctl + "txt_keyword").value) == "") { alert("Please enter keywords"); document.getElementById(ctl + "txt_keyword").focus(); return false; } 
  	document.getElementById(ctl + "hdnSubmit").value = "Y";
    document.frmNutrientSearch.submit();
	return false;
}
function ValidateForgottenPasswordForm(sCtl) {
	if (!ValidateEmail(document.getElementById(sCtl + "Email"))) return false ; 
	document.getElementById(sCtl + "hdnSubmit").value = "Y";
	document.frmForgottenPassword.submit();
	return false;
}
function validateEmailAFriendForm(sCtl) {
	if (cmtrim(document.getElementById(sCtl + "txt_recipient_name").value) == "") { alert("Please enter recipient name"); document.getElementById(sCtl + "txt_recipient_name").focus(); return false; } 
	if (!ValidateEmail(document.getElementById(sCtl + "txt_recipient_email"))) return false ; 
	if (cmtrim(document.getElementById(sCtl + "txt_your_name").value) == "") { alert("Please enter your name"); document.getElementById(sCtl + "txt_your_name").focus(); return false; } 
	if (!ValidateEmail(document.getElementById(sCtl + "txt_your_email"))) return false ; 
	if (cmtrim(document.getElementById(sCtl + "txt_message").value) == "") { alert("Please enter message"); document.getElementById(sCtl + "txt_message").focus(); return false; } 
	
	document.getElementById(sCtl + "hdnSubmit").value = "Y";
	document.formRecipe.submit();
	return false;
}
function ValidateNutritionInfoForm() {

	if (!isDigit(document.frmNutritionComposition.txt_meat_cut_order.value)) { alert("Please enter meat cut order in number"); document.frmNutritionComposition.txt_meat_cut_order.focus(); return false; }
	document.frmNutritionComposition.hdnOper.value = "Update";
	document.frmNutritionComposition.submit();
}

//var bSearch="N";
function searchSite(setValue) {
//	if (bSearch == "Y")
	document.getElementById("topBanner_hdnSubmit").value = setValue;
}
function checkEnter(oEvent, oper) {
    var characterCode ;
    if(oEvent && oEvent.which){ 
        characterCode = oEvent.which;
    }
    else{
        characterCode = oEvent.keyCode;
    }
    if(characterCode == 13){ 
    
		/*if (oper == 's') {
		 //bSearch= "Y";
		 searchSite();
		}*/
		if (oper == 'NutrientSearch_') {
			ValidateNutrientSearch(oper);
		}
		if (oper == 'JoinOrLogin_') {
			ValidateLoginForm(oper);
		}
		if (oper == 'J')
			ValidateJoinUp('J');
		if (oper == 'ForgottenPassword_')
			ValidateForgottenPasswordForm('ForgottenPassword_');
    }
    else{
        return true ;
    }
}

function PasswordEnterCheck(oEvent) {
    var characterCode ;
    if(oEvent && oEvent.which){ 
        characterCode = oEvent.which;
    }
    else{
        characterCode = oEvent.keyCode;
    }
    if(characterCode == 13){ 
		return ValidateLoginForm('Login_');
    }
}

function clickcheck(fld, label) {
 //  bSearch= "Y";
   if (fld.value == label) 	fld.value = '';
 //  	alert(fld.value);

}
function DisplayLabel(fld, label) {
  // if (fld.value == '') 	fld.value = label;
}

function AddOrModifySection(Oper, SectionId) {
    Frm = document.frmSectionList;
    Frm.hdnOper.value = Oper;
    Frm.hdnSectionId.value = SectionId;
    Frm.submit();
    return true;
}
function AddOrModifyResourceDetail(Oper, SectionId) {
    Frm = document.frmSectionDetailList;
    Frm.hdnOper.value = Oper;
    Frm.hdnResourceId.value = SectionId;
    Frm.submit();
    return true;
}
function SubmitResourceForm(Oper) {
	Frm = document.frmResourceList;
    Frm.hdnOper.value = Oper;
    Frm.submit();
    return true;
}
function SubmitForm(Oper) {
	Frm = document.frmSectionList;
	
	
	if (Oper == 'update' && !isDigit(Frm.txt_OrderId.value)) { alert("Please enter order in number"); Frm.txt_OrderId.focus(); return false; }
	
    Frm.hdnOper.value = Oper;
    Frm.submit();
    return true;
}