// JavaScript Document
// Coded by :Nithin Amin
// Date:10/09/2009
// Form validatio scripts
var errorMessage	= new Array();
errorMessage[0]="Please enter your email address"; 
errorMessage[1]="Please enter a valid email address"; 
errorMessage[2]="Please confirm your email address"; 
errorMessage[3]="Your email addresses must match"; 
errorMessage[4]="Please enter your password"; 
errorMessage[5]="Please confirm your password";
errorMessage[6]="Your passwords do not match"; 
errorMessage[7]="Please enter email id submitted for registration"; 
errorMessage[8]="You are already registered"; 
errorMessage[9]="Invalid username or password"; 
errorMessage[10]="Your password must be more than 6 characters long"; 
errorMessage[11]="Please enter your old password"; 
errorMessage[12]="You have entered wrong password"; 
errorMessage[13]="Your feedback has beed updated"; 
errorMessage[14]="We cannot find an account with those details, please check the details and try again"; 
errorMessage[15]="Please accept the terms and conditions"; 

function validate_register(){
	var email			=	document.diaryRegister.txt_email.value;
	var confirmEmail	=	document.diaryRegister.txt_confirm_email.value;
	var password		=	document.diaryRegister.txt_pass.value;
	var confirmPassword	=	document.diaryRegister.txt_confirm_pass.value;
	var termsCondition	=	document.diaryRegister.checkBox_terms;
	var flag=true;
	var count=1;
	var alert_register='';
		email  = trim(email);//----------- check for the spaces ------------//  
		if(email == null || email.length==0) {//----------- check whether text field is null ------------//
			  document.getElementById('emaildErr').innerHTML=errorMessage[0];
	          flag=false;
			  alert_register=alert_register+errorMessage[0]+"\n";
		}
		else if(checkemail(email)== false){
			  document.getElementById('emaildErr').innerHTML=errorMessage[1];
			  flag=false; 
			  alert_register=alert_register+errorMessage[1]+"\n";
		 }
		 else{
			  document.getElementById('emaildErr').innerHTML='';//------if  valid then no error message displayed ------//
		 }
	
		confirmEmail  = trim(confirmEmail);//----------- check for the spaces ------------//  
		if(confirmEmail == null || confirmEmail.length==0) {//----------- check whether text field is null ------------//
			  document.getElementById('confirnEmaildErr').innerHTML=errorMessage[2];
	          flag=false; 
			   alert_register=alert_register+errorMessage[2]+"\n";
		 }
		 else if(email!=confirmEmail){
			  document.getElementById('confirnEmaildErr').innerHTML=errorMessage[3];
			  flag=false; 
			  alert_register=alert_register+errorMessage[3]+"\n";
		 }
		 else{
			  document.getElementById('confirnEmaildErr').innerHTML='';//------if  valid then no error message displayed ------//
		 }
		 
		 password  = trim(password);//----------- check for the spaces ------------//  
		 if(password == null || password.length==0) {//----------- check whether text field is null ------------//
			  document.getElementById('passErr').innerHTML=errorMessage[4];
	          flag=false; 
			   alert_register=alert_register+errorMessage[4]+"\n";
		 }
		 else if(password.length <= 6 ){
			  document.getElementById('passErr').innerHTML=errorMessage[10];
	          flag=false;  
			   alert_register=alert_register+errorMessage[10]+"\n";
		 }
		 else{
			  document.getElementById('passErr').innerHTML='';//------if  valid then no error message displayed ------//
		 }
		 
		 confirmPassword  = trim(confirmPassword);//----------- check for the spaces ------------//  
		 if(password == null || password.length==0) {//----------- check whether text field is null ------------//
			  document.getElementById('confirmPassErr').innerHTML=errorMessage[5];
	          flag=false; 
			  alert_register=alert_register+errorMessage[5]+"\n";
		 }
		 else if(password!=confirmPassword){
			  document.getElementById('confirmPassErr').innerHTML=errorMessage[6];
			  flag=false; 
			 alert_register=alert_register+errorMessage[6]+"\n";
		 }
		 else{
			  document.getElementById('confirmPassErr').innerHTML='';//------if  valid then no error message displayed ------//
		 }
		 
		if(termsCondition.checked){
				document.getElementById('termsErr').innerHTML="";
			}
			else
			{
				document.getElementById('termsErr').innerHTML=errorMessage[15];
				 flag=false; 
				  alert_register=alert_register+errorMessage[15]+"\n";
			}
		 if(flag==false){ //-----------------check whether all fields are validated-----------//
			alert(alert_register);
			return false; //-----------------if not return false-----------//
		 }
	     else
		 {
			 
	   		objForm = window.document.diaryRegister;
			objForm.method = "POST";	
			objForm.submit();//-----------------else return true-----------//
		 }
		
}
function validate_forgotPassword(){
	     var email			=	document.diaryForgotPassword.txt_email.value;
		 var flag=true;
		 email  = trim(email);//----------- check for the spaces ------------//  
		  var alert_forgot='';
		 if(email == null || email.length==0) {//----------- check whether text field is null ------------//
				  document.getElementById('unameErr').innerHTML=errorMessage[7];
				  flag=false; 
				  alert_forgot=alert_forgot+errorMessage[7]+"\n";
		 }
		 else if(checkemail(email)== false){
			  document.getElementById('unameErr').innerHTML=errorMessage[1];
			  flag=false; 
			  alert_forgot=alert_forgot+errorMessage[1]+"\n";
		 }
		 else{
			  document.getElementById('unameErr').innerHTML='';//------if  valid then no error message displayed ------//
		 }
		 if(flag==false){ //-----------------check whether all fields are validated-----------//
		 	alert(alert_forgot);
			return false; //-----------------if not return false-----------//
		 }
	     else
		 {
	   		objForm = window.document.diaryForgotPassword;
			objForm.method = "POST";	
			objForm.submit();//-----------------else return true-----------//
		 }
}
function validate_login(){
	     var uName			=	document.diaryLogin.txtUname.value;
		 var password		=	document.diaryLogin.txtPassword.value;
		 var flag=true;
		 var alert_login='';
		 uName  = trim(uName);//----------- check for the spaces ------------//  
		 if(uName == null || uName.length==0) {//----------- check whether text field is null ------------//
				  document.getElementById('unameErr').innerHTML=errorMessage[7];
				  flag=false; 
				  alert_login=alert_login+errorMessage[7]+"\n";
		 }
		 else if(checkemail(uName)== false){
			  document.getElementById('unameErr').innerHTML=errorMessage[1];
			  flag=false; 
			  alert_login=alert_login+errorMessage[1]+"\n";
		 }
		 else{
			  document.getElementById('unameErr').innerHTML='';//------if  valid then no error message displayed ------//
		 }
		 
		 password  = trim(password);//----------- check for the spaces ------------//  
		 if(password == null || password.length==0) {//----------- check whether text field is null ------------//
				  document.getElementById('passwordErr').innerHTML=errorMessage[4];
				  flag=false; 
				  alert_login=alert_login+errorMessage[4]+"\n";
		 }
		 else{
			  document.getElementById('passwordErr').innerHTML='';//------if  valid then no error message displayed ------//
		 }

		 if(flag==false){ //-----------------check whether all fields are validated-----------//
			alert(alert_login);
			return false; //-----------------if not return false-----------//
		 }
	     else
		 {
	   		objForm = window.document.diaryLogin;
			objForm.method = "POST";	
			objForm.submit();//-----------------else return true-----------//
		 }
	 
}

function validate_profile(thisForm){
		var email			=	thisForm.txt_email.value;
		var confirmEmail	=	thisForm.txt_confirm_email.value;
		var flag=true;
		var alert_profile='';
		var delay=1;
		email  = trim(email);//----------- check for the spaces ------------//  
		if(email == null || email.length==0) {//----------- check whether text field is null ------------//
			  document.getElementById('emaildErr').innerHTML=errorMessage[0];
			  flag=false; 
	          alert_profile=alert_profile+errorMessage[0]+"\n";
		}
		else if(checkemail(email)== false){
			  document.getElementById('emaildErr').innerHTML=errorMessage[1];
			  flag=false; 
			  alert_profile=alert_profile+errorMessage[1]+"\n";
		}
		else{
			  document.getElementById('emaildErr').innerHTML='';//------if  valid then no error message displayed ------//
		 }
	
		confirmEmail  = trim(confirmEmail);//----------- check for the spaces ------------//  
		if(confirmEmail == null || confirmEmail.length==0) {//----------- check whether text field is null ------------//
			  document.getElementById('confirnEmaildErr').innerHTML=errorMessage[2];
	          flag=false; 
			 alert_profile=alert_profile+errorMessage[2]+"\n";
		 }
		 else if(email!=confirmEmail){
			  document.getElementById('confirnEmaildErr').innerHTML=errorMessage[3];
			  flag=false; 
			   alert_profile=alert_profile+errorMessage[3]+"\n";
		 }
		 else{
			  document.getElementById('confirnEmaildErr').innerHTML='';//------if  valid then no error message displayed ------//
		 }
		 if(flag==false){
		 	alert(alert_profile);
		 }
	     return flag;
}
function validate_change_password(thisForm){
		var oldPassword		=	thisForm.txt_oldPassword.value;
		var password		=	thisForm.txt_pass.value;
		var confirmPassword	=	thisForm.txt_confirmPass.value;
		var flag=true;
		 oldPassword	= trim(oldPassword);
		 if(oldPassword == null || oldPassword.length==0) {//----------- check whether text field is null ------------//
			  document.getElementById('oldPassErr').innerHTML=errorMessage[11];
	          flag=false; 
		 }
		 else
		 {
				document.getElementById('oldPassErr').innerHTML=""; 
		 }
		 password  = trim(password);//----------- check for the spaces ------------//  
		 if(password == null || password.length==0) {//----------- check whether text field is null ------------//
			  document.getElementById('passErr').innerHTML=errorMessage[4];
	          flag=false; 
		 }
		 else if(password.length <= 6 ){
			  document.getElementById('passErr').innerHTML=errorMessage[10];
	          flag=false;  
		 }
		 else{
			  document.getElementById('passErr').innerHTML='';//------if  valid then no error message displayed ------//
		 }
		 
		 confirmPassword  = trim(confirmPassword);//----------- check for the spaces ------------//  
		 if(password == null || password.length==0) {//----------- check whether text field is null ------------//
			  document.getElementById('confirmPassErr').innerHTML=errorMessage[5];
	          flag=false; 
		 }
		 else if(password!=confirmPassword){
			  document.getElementById('confirmPassErr').innerHTML=errorMessage[6];
			  flag=false; 
		 }
		 else{
			  document.getElementById('confirmPassErr').innerHTML='';//------if  valid then no error message displayed ------//
		 }
		 return flag;
		 
}
function checkPassword(flagValue){
		if(flagValue.length == 0){
			document.getElementById('oldPassErr').innerHTML=errorMessage[11];
			document.getElementById('button_profile_submit').disabled=true;
		}
		else
		{
		 var xmlHttp;
			  xmlHttp=GetXmlHttpObject();
			  if (xmlHttp==null){
	  			alert ("You are using a browser that is not supported. We recommend upgrading your browser to it's latest version to view this website properly.");
	 			return;
	  		  } 
			  var url="ajax-files/abdominal_diary_checkPassword.asp";
			  url=url+"?password="+flagValue;
			  xmlHttp.open("GET",url,true);
			  xmlHttp.onreadystatechange=function(){
				if (xmlHttp.readyState==4){ 
					 err_flag=xmlHttp.responseText;
					 if(err_flag == 0){ 
					 	document.getElementById('oldPassErr').innerHTML=errorMessage[12];
						document.getElementById('button_profile_submit').disabled=true;
					 }
					 else{
						document.getElementById('oldPassErr').innerHTML="";
						document.getElementById('button_profile_submit').disabled=false;
					 }
				 }
			}
		xmlHttp.send(null); 
		}
}
function checkConfirmpassword(thisForm){
	newPass=thisForm.txt_pass.value;
	newPassConfirm=thisForm.txt_confirmPass.value;
	if(newPass != newPassConfirm){
		document.getElementById('confirmPassErr').innerHTML=errorMessage[6];
	}
	else
	{
		document.getElementById('confirmPassErr').innerHTML="";
	}
	
}
//---------------------checkemail function---------------------//
function checkemail(str){        
		var regexEmail=/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/;
		if(str.length <= 0)
		{
			return false;	//---------if not return false----// 
		}
		
		else if (!regexEmail.test(str))
		{ 
			return false;//---------if not return false----// 
		}
}
//---------------------Trim function---------------------//

function update_feedback(txtarea){
	var text_content;
	text_content=trim(txtarea);
	if (text_content == null){
		 document.getElementById('feedbackStatus').innerHTML="You have not entered feedback"; 
		 document.getElementById('buttonConfirm').disabled=true
	}
	else
	{
		document.getElementById('buttonConfirm').disabled=false
			  var xmlHttp;
			  xmlHttp=GetXmlHttpObject();
			  if (xmlHttp==null){
	  			alert ("You are using a browser that is not supported. We recommend upgrading your browser to it's latest version to view this website properly.");
	 			return;
	  		  } 
			  var url="ajax-files/update_feedback.asp";
			  url=url+"?feedback="+txtarea;
			  xmlHttp.open("GET",url,true);
			  xmlHttp.onreadystatechange=function(){
				  if (xmlHttp.readyState==4){ 
						 err_flag=xmlHttp.responseText;
						 if(err_flag==-1){
								window.location ='../../abdominal_diary_login.asp?session=expired';
						}
						else
						{
						document.getElementById('feedbackStatus').innerHTML=errorMessage[13]; 
						document.getElementById('buttonConfirm').disabled=false;
						}
				 }
				 else
				 {
					 	 document.getElementById('buttonConfirm').disabled=true;
						 document.getElementById('feedbackStatus').innerHTML=""; 
				 }
			  }
		xmlHttp.send(null); 
	}
}
//-----------------Create xml object-------------------
function checkRegEmail(email){
	if(email.length==0){
		document.getElementById('emaildErr').innerHTML=errorMessage[0];
	}
	else
	{
			  var xmlHttp;
			  xmlHttp=GetXmlHttpObject();
			  if (xmlHttp==null){
	  			alert ("You are using a browser that is not supported. We recommend upgrading your browser to it's latest version to view this website properly.");
	 			return;
	  		  } 
			  var url="diary/ajax-files/checkEmail.asp";
			  url=url+"?email="+email;
			  xmlHttp.open("GET",url,true);
			  xmlHttp.onreadystatechange=function(){
				if (xmlHttp.readyState==4){ 
					 err_flag=xmlHttp.responseText;
					 if(err_flag == 1){ 
					 	document.getElementById('emaildErr').innerHTML=errorMessage[8];
						flag=false;
					 }
					 else{
						document.getElementById('emaildErr').innerHTML="";
					 }
				 }
			   }
			  xmlHttp.send(null); 
	}
}
function checkUser(user){
	if(user.length==0){
		document.getElementById('unameErr').innerHTML=errorMessage[7];
	}
	else
	{
		document.getElementById('unameErr').innerHTML="";
	 var xmlHttp;
			  xmlHttp=GetXmlHttpObject();
			  if (xmlHttp==null){
	  			alert ("You are using a browser that is not supported. We recommend upgrading your browser to it's latest version to view this website properly.");
	 			return;
	  		  } 
			  var url="diary/ajax-files/checkEmail.asp";
			  url=url+"?email="+user;
			  xmlHttp.open("GET",url,true);
			  xmlHttp.onreadystatechange=function(){
				if (xmlHttp.readyState==4){ 
					 err_flag=xmlHttp.responseText;
					 if(err_flag != 1){ 
					 	document.getElementById('passwordErr').innerHTML=errorMessage[14];
						document.getElementById('loginBotton').disabled=true;	
						
					 }
					 else
					 {
						document.getElementById('passwordErr').innerHTML="";
						document.getElementById('loginBotton').disabled=false;	
						
					 }
				 }
				 else
				 {
					 	document.getElementById('loginBotton').disabled=true;	
				 }
				
			   }
			  xmlHttp.send(null); 
	}
}
function checkUserForgot(forgotId){
if(forgotId.length==0){
		document.getElementById('unameErr').innerHTML=errorMessage[7];
	}
	else
	{
	 var xmlHttp;
			  xmlHttp=GetXmlHttpObject();
			  if (xmlHttp==null){
	  			alert ("You are using a browser that is not supported. We recommend upgrading your browser to it's latest version to view this website properly.");
	 			return;
	  		  } 
			  var url="diary/ajax-files/checkEmail.asp";
			  url=url+"?email="+forgotId;
			  xmlHttp.open("GET",url,true);
			  xmlHttp.onreadystatechange=function(){
				if (xmlHttp.readyState==4){ 
					 err_flag=xmlHttp.responseText;
					 if(err_flag != 1){ 
					 	document.getElementById('unameErr').innerHTML=errorMessage[14];
						document.getElementById('forgot_button').disabled=true;	
					 }
					 else
					 {
						document.getElementById('unameErr').innerHTML="";
						document.getElementById('forgot_button').disabled=false;	
						
					 }
				 }
				
			   }
			  xmlHttp.send(null); 
	}
	
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
function check_length(my_form){
	maxLen = 1024; 
	if (my_form.txtarea_feedback.value.length >=maxLen) {
		var msg = "You have reached the maximum limit of characters allowed";
		document.getElementById('txtAreaError').innerHTML= msg;
		my_form.txtarea_feedback.value = my_form.txtarea_feedback.value.substring(0, maxLen);
		
	}
	else
	{ 
		document.getElementById('txtAreaError').innerHTML= maxLen - my_form.txtarea_feedback.value.length+"&nbsp; charecter left [max 1024 char]";
	}
}
function confirm_delete(){
	var feedText=document.getElementById('feedbackTxt').value;
	feedText=trim(feedText);
	if ((feedText)== null){
		 document.getElementById('feedbackStatus').innerHTML="You have not entered feedback"; 
		 document.getElementById('buttonConfirm').disabled=true;
	}
	else
	{
		document.getElementById('buttonConfirm').disabled=false;
		var isConfirm = confirm("Are you sure you want to cancel your account?");
		if (isConfirm == true){
			window.location = "cancel_account.asp?status=yes";
		}
		else{
			window.location = "cancel_account.asp?status=no";
		}
	}
}
function trim(str){
    if(!str || typeof str != 'string')
        return null;

    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');//---------remove the spaces----// 
}
function resetAction()
{
	document.getElementById('address1').style.display = "none";
	document.getElementById('address2').style.display = "none";
	document.getElementById('address3').style.display = "none";
	document.getElementById('town').style.display = "none";
	document.getElementById('houseNumber').style.display = "";
	document.getElementById('reset').style.display = "none";
	document.getElementById('txt_address1').value = "";
	document.getElementById('txt_address2').value = "";
	document.getElementById('txt_address3').value = "";
	document.getElementById('txt_town').value = "";
	document.getElementById('lookup').style.display =""
}
