// JavaScript Document
// JavaScript Document
/**********************************************************************************************************************************
 Author: Gautam G Gosavi
 Organization : Indelible Technologies
 Description : This contain javascript for sendmail file
 
 Modified By :                   Date                     Purpose
 
 
**********************************************************************************************************************************/


/*********************************************************************************************************************************
 Name : checkforregularstring
 Purpose : Will check for numeric and special chars and will disallow them.
*********************************************************************************************************************************/
function checkforregularstring(string)
{

  if (!string) return true;
  var Chars = "0123456789-@!~#$%^&*()"; //declaring all the chars defined which will be checked

	for (var i = 0; i < string.length; i++)
	{ 
	
	  if (Chars.indexOf(string.charAt(i)) >0)
	    return false;
	}
	return true;

	
}

/*********************************************************************************************************************************
 Name:numeralsOnly
 Purpose:only for numeric data
*********************************************************************************************************************************/
function numeralsOnly(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : 
        ((evt.which) ? evt.which : 0));
    if (charCode > 31 && (charCode < 48 || charCode > 57)  && charCode != 46 ) {
        return false;
    }
    return true;
}

/*********************************************************************************************************************************
 Name:validatestring
 Purpose:Will validate for numeric and special chars
*********************************************************************************************************************************/
function validatestring(str,contorlname)
{
   var ctrlobj;
   ctrlobj=document.getElementById(contorlname);
   
		 if (!checkforregularstring(str)){   
             alert('No numeric and special chars are allowed.');
			 
			 ctrlobj.value='';
			 //document.frmsend.from_email.focus();
			 ctrlobj.focus();
			 return false;
		}	
	
}


/*********************************************************************************************************************************
 Name:chkEmail
 Purpose:Will validate email address
*********************************************************************************************************************************/

function chkEmail(strEmail){
     
	 if(strEmail.value!=""){ 
         isemail=validate_email(strEmail);
	  
	    if(isemail=="false"){
	                        alert("Invalid Email!.");
		                    return true;
	                      }else
						  { return false;}
	  }
}

/*********************************************************************************************************************************
 Name:validate_email
 Purpose:Will will be used by chkEmail 
*********************************************************************************************************************************/
function validate_email(str)
{
  var ValidChars = "0123456789.";
  var validdom   ="0123456789";
  var i;
  var c;
  var l;
  var n;
  var at="@";
  var dot=".";
  var lat=str.indexOf(at);
  var lstr=str.length;
  var ldot=str.indexOf(dot);
  i=0;
 
   c = str.charAt(i); 
  
   if (ValidChars.indexOf(c) !=-1) 
   { return "false"; }
 
   l=str.indexOf(dot)+1;

   for(i=l;i<lstr;i++)
   {
	 l=str.charAt(i); 
	 if (validdom.indexOf(l)!=-1) 
     { return "false"; }
   }
  
  
  if (str.indexOf(at)==-1){
    
     return "false";
  }
  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
     
     return "false";
  }
  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
      
      return "false";
  }
   if (str.indexOf(at,(lat+1))!=-1){
      
      return "false";
   }
   if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
       
      return "false";
   }
   if (str.indexOf(dot,(lat+2))==-1){
      
      return "false";
   }
   if (str.indexOf(" ")!=-1){
      
      return "false";
   }
    
      return "true" ;        
}

/*********************************************************************************************************************************
 Name:submitform
 Purpose:Will submit the form and validate mandatory fields
*********************************************************************************************************************************/

function submitrecipeform()
{
	 //
	
	  var validateemail;
	  if(document.frmsubmitrecipe.txtemail.value!="" && document.frmsubmitrecipe.txtname.value!="" 
		 && document.frmsubmitrecipe.txtcomments.value!="" && document.frmsubmitrecipe.attachment.value!="")
	  {
		  validateemail=validate_email(document.frmsubmitrecipe.txtemail.value);
		 
		  if(validateemail=="false")
		  {
			 alert("Invalid email");
			 return false;
		  }else
		  {
			    
			 document.frmsubmitrecipe.txtissubmitrecipe.value=1;
			 document.frmsubmitrecipe.method="post";
			 document.frmsubmitrecipe.action="phpfiles/balsendmail.php";
			 document.frmsubmitrecipe.submit();	
			  
		  }
	  }else
	  {
		   alert("Please enter mandatory fields");
		   return false;
	  }
	

	 
	
}
/*********************************************************************************************************************************
 Name:checkstringlenght
 Purpose:Will check string length
*********************************************************************************************************************************/

function checkstringlenght(string,len)
{
	 if (!string) return false;
 

	if(string.length<len)
	{ 
	   return false;
	}else{
	return true};
 
	  
}

/*********************************************************************************************************************************
 Name:checkmaxstringlenght
 Purpose:Will check max string length
*********************************************************************************************************************************/

function checkmaxstringlenght(string,len)
{
	 if (!string) return false;
 

	if(string.length>len)
	{ 
	   return false;
	}else{
	return true};
 
	  
}
/*********************************************************************************************************************************
 Name:LimitAttach
 Purpose:Will limit file attachment to .jpg,jpeg,bmp,gif,png
*********************************************************************************************************************************/

function LimitAttach(file) {
extArray = new Array(".gif", ".jpg", ".png",".jpeg",".bmp");
allowSubmit = false;
if (!file) return;
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\\") + 1);
ext = file.slice(file.indexOf(".")).toLowerCase();
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; break; }
}
if (allowSubmit) {return true;}
else{
alert("Please only upload files that end in types:  " 
+ (extArray.join("  ")) + "\nPlease select a new "
+ "file to upload and submit again."); return false;
}
}

/********************************************************************************************************************************
 Name: showmessage
 Purpose: This will show message to user
********************************************************************************************************************************/
function showmessage(msg)
{
  alert(msg);	
}