// 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 submitform()
{
	var chk;
	var istrue;
	var boolisattch;
	var boolemail;
	var boolcomments;
	var boolchkbox;
	chk=document.getElementById('chkrulesandterms');
	boolcomments=true;
	
	var ddlPhotoType = document.getElementById('ddlphototype');

	if(checkstringlenght(document.frmsend.firstname.value,0)==false||checkstringlenght(document.frmsend.lastname.value,0)==false
	   ||checkstringlenght(document.frmsend.txtemail.value,0)==false||checkstringlenght(document.frmsend.subject.value,0)==false
	   ||checkstringlenght(document.frmsend.attachment.value,0)==false )
	{
	  alert("Fields enter mandatory fields.");
	  istrue=false;
	}else
	{
		//check if attachment of proper format
		if(LimitAttach(document.frmsend.attachment.value)){ istrue=true; boolisattch=true;}else{ istrue=false;boolisattch=false;}
	    //validate email
	    if(chkEmail(document.frmsend.txtemail.value)){ istrue=false;boolemail=false;}else{ istrue=true;boolemail=true;}
		//check max string length
		
		if(document.frmsend.comments.value!=""){
			
		   if(checkmaxstringlenght(document.frmsend.comments.value,100))
		      { istrue=true;boolcomments=true;}else{ istrue=false;boolcomments=false;}
		}
		//now submit the document
		if(!chk.checked){alert('Please read the terms and conditions of NakriSakhi photo contest.');istrue=false;boolchkbox=false;}else{boolchkbox=true;}
		
	}
			
	if(istrue==true && boolisattch==true && boolemail==true && boolcomments==true && boolchkbox==true){
	
		document.frmsend.issubmit.value=1;
	     document.frmsend.method="post";
	    document.frmsend.action="balsendmail.php";
	    document.frmsend.submit();
	   return true;}
	else{
				
		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);	
}