//function to remove spaces from front and end of string
function trimAll(sString){

    while (sString.substring(0,1) == ' '){
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' '){
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}


//function to check special character in string
function checkSpecialChars(parValue){
     var receivedValue = parValue;
  //   var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?~`";   
     var iChars = "\\\';";
     for (var i = 0; i < receivedValue.length; i++) {
        if (iChars.indexOf(receivedValue.charAt(i)) != -1) {
            return -1;              	
      	}
    }
    return 1;    
}
// Function for email check.
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		var lastdot=str.lastIndexOf(dot)
		
    temp = (str.substring(lastdot+1,lstr));
		//alert(temp);
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		 
		 if(isNaN(temp))
		 {
        return true;
     }
     else
     {
        return false;
     }

 		 return true					
}

function show(val){
  if(val =="CO" || val == "CL"){
    document.getElementById('show').innerHTML='';
  }
  else{
    document.getElementById('show').innerHTML='<a class="forgotpassword" href="index.php?id=27"><img title="arrow" src="assets/images/style/brown-arrow.gif" alt="arrow" width="13" height="11" />&nbsp;Forgot Password?</a>';
  }
}
function validateform(){
 if(document.getElementById('email').value==""){
  alert("Enter Email-address.");
  document.getElementById('email').focus();
  return false;
 }
 if(!echeck(document.getElementById('email').value)){
  alert("Enter a valid email-id.");
  document.getElementById('email').focus();
  return false;
 }
  if(document.getElementById('pwd').value==""){
  alert("Enter password.");
  document.getElementById('pwd').focus();
  return false;
 }
 if(document.getElementById('selectuser').value=="0"){
  alert("Please select the user type.");
  document.getElementById('selectuser').focus();
  return false;
 }
 document.getElementById('loginsubmit').submit();
}
function validateforgotform(){
  var a=document.getElementById('ssn').value.length;  
 if(document.getElementById('email').value==""){
  alert("Enter Email-address.");
  document.getElementById('email').focus();
  return false;
 }
 if(!echeck(document.getElementById('email').value)){
  alert("Enter a valid email-id.");
  document.getElementById('email').focus();
  return false;
 }
  if(document.getElementById('ssn').value==""){
  alert("Enter last 4 digits of ssn number.");
  document.getElementById('ssn').focus();
  return false;
 } 
 if(a>4){
  alert("SSN can not be greater than 4.");
  document.getElementById('ssn').focus();
  return false;
 }
 document.getElementById('forgotsubmit').submit();
}

///function to delete the record
function confirmDelete_info(delUrl)    {
          if (confirm("Are you sure you want to delete ?")) {
           window.location.href = delUrl;
   }
}
///////////
function submitsearch()
{
  var page=document.getElementById('selectpage').value;
  if(page=="0"){
    page=1;
  }  
  document.searchform.action="index.php?id=107&action=search&page="+page;
  document.searchform.submit();

}
function validateftpform(val)
{  
  var coemails=document.getElementById('codoc').value;
  var clientemails=document.getElementById('clientdoc').value;  
  var coemarray=new Array();
  var clientemarray=new Array();
  coemarray=coemails.split(",");
  coemlength=coemarray.length;
  clientemarray=clientemails.split(",");
  clientemlength=clientemarray.length;    
  if(trimAll(document.getElementById('docname').value)==""){
    alert("Please enter document name.");
    document.getElementById('docname').focus();
    return false;
  }
  else if(trimAll(document.getElementById('details').value)=="")
  {
    alert("Please enter document details.");
    document.getElementById('details').focus();
    return false;
  }
else if(trimAll(document.getElementById('document').value)=="" &&  trimAll(document.getElementById('manual_file_name').value)=="" )
  {
    alert("Please enter document to upload.");
    document.getElementById('document').focus();
    return false;
  }
  else if(document.getElementById('check1').checked==true && document.getElementById('codoc').value=="")
  {
    alert("Please enter co-counseller email-ids.");
    document.getElementById('codoc').focus();
    return false;
  }
  else if(document.getElementById('check1').checked==false && document.getElementById('codoc').value!="")
  {
    alert("Please check the co-counseller checkbox.");
    document.getElementById('check1').focus();
    return false;
  }
  else if(document.getElementById('check2').checked==true && document.getElementById('clientdoc').value=="")
  {
    alert("Please enter client email-ids.");
    document.getElementById('clientdoc').focus();
    return false;
  }
  else if(document.getElementById('check2').checked==false && document.getElementById('clientdoc').value!="")
  {
    alert("Please check the client checkbox.");
    document.getElementById('check2').focus();
    return false;
  }
  for(var i=0;i<coemlength;i++){
    if(coemarray[i]!="" && coemarray[i]!= undefined){      
      if(!echeck(trimAll(coemarray[i]))){
        alert('Enter the valid co-council ids.');
        document.getElementById('codoc').focus();
        return false;
      }
    }
  }
  for(var j=0;j<coemlength;j++){
    if(clientemarray[j]!="" && clientemarray[j]!= undefined){     
      if(!echeck(trimAll(clientemarray[j]))){
        alert('Enter the valid client ids.');
        document.getElementById('clientdoc').focus();
        return false;
      }
    }
  }
  
  if(val=="savenadd"){
    document.getElementById('documentftpform').method="post";
     document.getElementById('documentftpform').action="index.php?id=106&action=add";
    document.getElementById('documentftpform').submit();
   
  }
  else if(val=="savenclose"){
    document.getElementById('documentftpform').method="post";
    document.getElementById('documentftpform').action="index.php?id=106&action=add&redirect=1";
    document.getElementById('documentftpform').submit();
    
  }	
}
///Update///
function updateftpform(val,id)
{  
  var coemails=document.getElementById('codoc').value;
  var clientemails=document.getElementById('clientdoc').value;  
  var coemarray=new Array();
  var clientemarray=new Array();
  coemarray=coemails.split(",");
  coemlength=coemarray.length;
  clientemarray=clientemails.split(",");
  clientemlength=clientemarray.length;    
  if(trimAll(document.getElementById('docname').value)==""){
    alert("Please enter document name.");
    document.getElementById('docname').focus();
    return false;
  }
  else if(trimAll(document.getElementById('details').value)=="")
  {
    alert("Please enter document details.");
    document.getElementById('details').focus();
    return false;
  }
   
  else if(document.getElementById('check1').checked==true && document.getElementById('codoc').value=="")
  {
    alert("Please enter co-counseller email-ids.");
    document.getElementById('codoc').focus();
    return false;
  }
  else if(document.getElementById('check1').checked==false && document.getElementById('codoc').value!="")
  {
    alert("Please check the co-counseller checkbox.");
    document.getElementById('check1').focus();
    return false;
  }
  else if(document.getElementById('check2').checked==true && document.getElementById('clientdoc').value=="")
  {
    alert("Please enter client email-ids.");
    document.getElementById('clientdoc').focus();
    return false;
  }
  else if(document.getElementById('check2').checked==false && document.getElementById('clientdoc').value!="")
  {
    alert("Please check the client checkbox.");
    document.getElementById('check2').focus();
    return false;
  }
  for(var i=0;i<coemlength;i++){
    if(coemarray[i]!="" && coemarray[i]!= undefined){     
      if(!echeck(trimAll(coemarray[i]))){
        alert('Enter the valid co-council ids.');
        document.getElementById('codoc').focus();
        return false;
      }
    }
  }
  for(var j=0;j<coemlength;j++){
    if(clientemarray[j]!="" && clientemarray[j]!= undefined ){     
      if(!echeck(trimAll(clientemarray[j]))){
        alert('Enter the valid client ids.');
        document.getElementById('clientdoc').focus();
        return false;
      }
    }
  }  
  if(val=="savenadd"){
    document.getElementById('documentftpform').method="post";
     document.getElementById('documentftpform').action="index.php?id=106&action=update&upid="+id;
    document.getElementById('documentftpform').submit();
   
  }
  else if(val=="savenclose"){
    document.getElementById('documentftpform').method="post";
    document.getElementById('documentftpform').action="index.php?id=106&action=update&redirect=1&upid="+id;
    document.getElementById('documentftpform').submit();
    
  }	
}
function golisting(url){
  window.location.href=url;
}


function setval(val,val1)
{
  var emailid='emailval'+val1;
  var namearray=document.getElementsByName('emailval');
  var count=namearray.length;
  for(var i=0;i<count;i++)
  {
    namearray[i].disabled = true;
  }
  document.getElementById(emailid).disabled=false;
  document.getElementById('casetype').value=val;	
}


