// JavaScript Document
// Programmed for Kohinoor Fibre Products
 
 //FUNCTION TO TRIM A STRING
 
function trimStr(str)
  {
     str=(str).replace( /^\s*/, "" );
     str=(str).replace( /\s*$/, "" );
     return str;
  }


var numb = '0123456789 ';
var lwr = 'abcdefghijklmnopqrstuvwxyz ';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ';
var dirr = ':abcdefghijklmnopqrstuvwxyz/. ';
var sx='0123456789 *abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

function isValid(parm,val) 
{
	  if (parm == "")
		 return 0;
	  for (i=0; i<parm.length; i++) 
	  {
		 if(val.indexOf(parm.charAt(i),0) == -1) 
		  {
			 return 0;
		  }	 
	  }
	  return 1;
}
 
function isNum(parm) 
  {
     return isValid(parm,numb);
  }

function isPhoneNum(parm) 
  {
     return isValid(parm,numb+",");
  }


function isSize(parm) 
  {
     return isValid(parm,sx);
  }

  
function isDir(parm) 
  {
     return isValid(parm,dirr);
  }
  
function isLower(parm) 
 {
    return isValid(parm,lwr);
 }
function isUpper(parm) 
 {
    return isValid(parm,upr);
 }
function isAlpha(parm) 
 {
    return isValid(parm,lwr+upr);
 }
function isAlphanum(parm) 
 {
    return isValid(parm,lwr+upr+numb);
 } 
 
 function checkDate(day,month,year)
{
    if(day>0 | month>0 | year>0)
	 {
		if(day==0 | month==0 | year==0)
		{
				alert("Invalid date selected");
				return 0;
		}
	 }
	   					
   if(day>0 & month>0 & year>0)
	 {					      
		if(year%4!=0 & month==2)
			{
				if(day>28)
				{
					alert("Invalid date selected");
					return 0;
				}
			 }//
			 
		  if(year%4==0 & month==2)
		   {
			  if(day>29)
				{
					 alert("Invalid date selected");
					 return 0;
				 }
			}//
	 
		  if(month==4 | month==6 | month==9 | month==11)
		  {
			 if(day>30)
			  {
				 alert("Invalid date selected");
				 return 0;
			  }
		  } 
    }
 }

 
 //LOGIN FORM VALIDATIONS
 
 function checkLogin()
  {
     var frm=document.frmlogin;
     if(trimStr(frm.login.value)=="")
      {
         alert("Please enter the Login Name");
	     frm.login.focus();
	     return false;
	  }
	 if(trimStr(frm.password.value)=="")
      {
         alert("Please enter the password");
	     frm.password.focus();
	     return false;
	  }  
	 if(trimStr(frm.type.value)=="")
      {
         alert("Please select the Login type");
	     frm.type.focus();
	     return false;
	  }
  }  

 // CHANGE PASSWORD FORM VALIDATION
 
 function checkPwdForm()
   {
      if(trimStr(document.frmpass.oldpass.value)=="")
	   {
	      alert("You have to enter the old password");
		  document.frmpass.oldpass.value="";
		  document.frmpass.oldpass.focus();
		  return false; 
	   }
	  	   
	  if(trimStr(document.frmpass.newpass.value)=="")
	   {
	      alert("You have to enter a new password");
		  document.frmpass.newpass.value="";
		  document.frmpass.newpass.focus();
		  return false; 
	   }
	   
	  if(document.frmpass.newpass.value!=document.frmpass.confirmpass.value)
	   {
		  alert("Password mismatch");
		  document.frmpass.newpass.focus();
		  return false; 
	   }  
   }


 // CHECK DLETION   
function checkDelete()
 {
    var frm=document.frmdel;
    var len= frm.elements.length;
	flag=0;
    for(i=0;i<len;i++)
	  {
         if( frm.elements[i].checked)
			{
			  flag=1;
			  break;
			}  
	 }   
	 
	if(flag==0)
	 {
	    alert("You have to select atleast one item for deletion");
	    return false;
	 } 
    
	if(!confirm("Are you sure you want to delete the selected item"))
     {
	     return false;
	 }	 
 }
 
////////////////////////////////////////////////////////////////////////////////////

function checkAll()
 {
   var frm=document.frmnews;
   var len=frm.elements.length;
   if(frm.call.checked)
    {
	  for(i=0;i<len;i++)
	   {
          if((frm.elements[i].name).substring(0,3)=="nid")
		   {
		      frm.elements[i].checked=true;
		   }
	  }  
	}
	
   if(!frm.call.checked)
	{  
	   for(i=0;i<len;i++)
	   {
          if((frm.elements[i].name).substring(0,3)=="nid")
		   { 
	          frm.elements[i].checked=false;
	       }
	   }
	}	     
}

function checkForm()
 {
    var frm=document.frmarticle;
	if(trimStr(frm.title.value)=="")
	 {
	     alert("You have to enter the title for the article");
		 frm.title.focus();
		 return false;
	 }
	
 }

//*//////////////////////////////////////////////////////////////////////////////

 function checkRemnews()
  {
    if(!confirm("Are you sure to remove the selected news"))
	{
	   return false;
	}
  }
  
 /////////////////////////// txtmm-txtinch
 function checkCategory()
  {
  		
      var frm=document.frmcat;
	  if(trimStr(frm.txtcategory.value)=="")
	  {
	      alert("Please enter the category name");
		  frm.txtcategory.focus();
		  return false;
	  }
	
	//checking whether no.of comma's are same in mm and inches fields.
	cnt1=0;
	cnt2=0;	
	  for(ch1=0;ch1<frm.txtmm.value.length;ch1++)
	  {
		if(frm.txtmm.value.charAt(ch1)==",")
		  cnt1++;
	  }
 	 for(ch2=0;ch2<frm.txtinch.value.length;ch2++)
	  {
	  	if(frm.txtinch.value.charAt(ch2)==",")
			cnt2++;
	  }
	 if(cnt1!=cnt2)
	   {
	    alert("Number of values in MM and Inches mismatch");
		if(cnt1>cnt2)
			frm.txtinch.focus();
		else
			frm.txtmm.focus();
				
		return false;
	  }
  }
  /////////////////////////////////////////////////////////
  
function goNext(start)
 {
    document.form1.start.value=start;
    document.form1.submit();
 }
 
function checkRemcat()
 {
     if(confirm("Are you sure to remove the category"))
	  {
	     return true;
	  }
	 else
	  {
	     return false;
	  } 
 }

 ////////////////////////////////////////////////////////////////////////////////////////////////////////
 function checkRemnews()
  {
    if(!confirm("Are you sure about deleting this news item?\nThe news item would be lost forever!"))
	{
	   return false;
	}
  }
  
  
  function removeNews()
 {
    var frm=document.frmnews;
    var len= frm.elements.length;
	flag=0;
    for(i=0;i<len;i++)
	  {
         if( frm.elements[i].checked)
			{
			  flag=1;
			  break;
			}  
	 }   
	 
	if(flag==0)
	 {
	    alert("No news item selected. Please select at least one news item and try again");
	    return false;
	 } 
	 
    if(!confirm("Are you sure?"))
	{
	   return false;
	}
 }

function checkAll()
 {
   var frm=document.frmnews;
   var len=frm.elements.length;
   if(frm.call.checked)
    {
	  for(i=0;i<len;i++)
	   {
          if((frm.elements[i].name).substring(0,3)=="nid")
		   {
		      frm.elements[i].checked=true;
		   }
	  }  
	}
	
   if(!frm.call.checked)
	{  
	   for(i=0;i<len;i++)
	   {
          if((frm.elements[i].name).substring(0,3)=="nid")
		   { 
	          frm.elements[i].checked=false;
	       }
	   }
	}	     
}

function checkNews()
 {
    var frm=document.frmnews;
	if(trimStr(frm.subj.value)=="")
	 {
	    alert("Please enter the title for the news");
		frm.subj.focus();
		return false;
	 }
	if(trimStr(frm.news.value)=="")
	 {
	    alert("Please enter the news in detail");
		frm.news.focus();
		return false;
	 } 
 } 

////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////
function setComposeFoc()
	{
	    document.frmcompose.to.focus();   
	}
/////////////////////////////////////////////////////////////////////////////////////////////////////////	
function checkCompose()
   {
      if(trimStr(document.frmcompose.to.value)=="")
	    {
		   alert("Please enter the Name of the addressee");
		   document.frmcompose.to.value="";
		   document.frmcompose.to.focus();
		   return false;
		}   
	    if(trimStr(document.frmcompose.subject.value)=="")
	    {
		   alert("Please enter the Subject of the Message");
		   document.frmcompose.subject.value="";
		   document.frmcompose.subject.focus();
		   return false;
		}  	
	   if(trimStr(document.frmcompose.message.value)=="")
	    {
		   alert("Please enter the Message");
		   document.frmcompose.message.value="";
		   document.frmcompose.message.focus();
		   return false;
		}   	
   }
///////////////////////////////////////////////////////////////////////////////////////////////////////// 
   function setAll()
	{
	   if(document.frmcompose.toall.checked)
	    {
	      document.frmcompose.to.value="All"
		}   
	   else
	    {
		  document.frmcompose.to.value="";
		}	
	}
	
	function address()
	{
	   if(trimStr(document.frmcompose.to.value)=="All")
	     document.frmcompose.to.value="All"; 
	     
	   else
	    {	   
			if(trimStr(document.frmcompose.to.value)=="")
			  document.frmcompose.to.value=document.frmcompose.add.value; 
			else
			 {
			    tempval=","+document.frmcompose.to.value+",";
			    if(tempval.indexOf(document.frmcompose.add.value)==-1)// check whether the selection already exists
				    document.frmcompose.to.value=document.frmcompose.to.value+","+document.frmcompose.add.value; 
			 }   
		}		
	} 
//////////////////////////////////////////////////////////////////////////////////////	
function checkAll()
 {
 
   var len=document.frminbox.elements.length;
   if(document.frminbox.ca.checked)
    {
	  for(i=0;i<len;i++)
	   {
          if((document.frminbox.elements[i].name).substring(0,5)=="msgid")
		   {
		      document.frminbox.elements[i].checked=true;
		   }
	  }  
	}
	
   if(!document.frminbox.ca.checked)
	{  
	   for(i=0;i<len;i++)
	   {
          if((document.frminbox.elements[i].name).substring(0,5)=="msgid")
		   { 
	          document.frminbox.elements[i].checked=false;
	       }
	   }
	}	     
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkMsg()
 {
    var len=document.frminbox.elements.length;
	flag=0;
    for(i=0;i<len;i++)
	  {
         if(document.frminbox.elements[i].checked)
			{
			  flag=1;
			  break;
			}  
	 }   
	 
	if(flag==0)
	 {
	    alert("No message(s) selected. Please select at least one message and try again");
	    return false;
	 } 
    
	if(!confirm("Are you sure? The message will be lost forever!"))
     {
	     return false;
	 }	 
 }
/////////////////////////////////////////////////////////////////////////////////////////////////////////
function confirmDelmsg(id,cat)
 {
     if(confirm("Are you sure? The message will be lost forever!"))
	  {
	     window.location="deletemsg.php?mailid="+id+"&cat="+cat;
	  }
 }
////////////////////////////////////////////////////////////////////////////////////////////////////////
function confirmDelcat()
 {
     if(confirm("Are you sure to remove the category?"))
	  {
	     return true;
	  }
	 else
	  {
	     return false;
	  } 
 }


/////////////////////////////////////////////////////////////////////////////////////////////////////////
 function confirmDelomsg(id,cat)
 {
     if(confirm("Are you sure? The message will be lost forever!"))
	  {
	     window.location="removemsg.php?mailid="+id+"&cat="+cat;
	  }
 }
///////////////////////////////////////////////////////////////////////////////////////////////////////
var i=1;
function blinker()
{
  if(i%2==0)
   {
      div1.style.color= "#0000ff";
	  i=1;
   }
   else
   {
      div1.style.color= "#ff000f";
	  i+=1;
   }
  var timeout=setTimeout("blinker()",600);
}

j=1;
function blinker2()
{
     if(j%2==0)
     { 
        div2.style.color= "#ff000f";
		div3.style.color= "#000000";
		div4.style.color= "#ff000f";
	    j=1;
     }	
	 else
     {
	    div2.style.color= "#0000ff";
		div3.style.color= "#ffffff";
		div4.style.color= "#0000ff";
	    j+=1;
    }
   var timeout=setTimeout("blinker2()",600);	
 }
 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //FUNCTIONS FOR NEWSLETTER MANAGEMENT
 function checkSend()
 {
    var frm=document.frmreg;
	
	if(trimStr(frm.subject.value)=="")
	 {
	    alert("Enter the subject of the newsletter");
		frm.subject.focus();
		return false; 
	 }
	
	 if(trimStr(frm.email.value)=="")
	  {
	    alert("Please enter the email address");
		frm.email.focus();
		return false;
	  } 
	  
	 if(trimStr(frm.email.value)!="")
	  {
		var dotpos=frm.email.value.lastIndexOf('.');
		var amppos=frm.email.value.indexOf('@');
		var lastpos=frm.email.value.length;
		 
		if(dotpos<=amppos | amppos<1 | lastpos==dotpos+1)
		 {
			alert("Please enter the email address correctly");
			frm.email.focus();
			return false;
		 }  
      }
	
	if(trimStr(frm.filename.value)=="" & trimStr(frm.message.value)=="")
	 {
	    alert("Please select the file which is to be sent as newsletter \n\nor\n\nenter the message text");
		frm.filename.focus();
		return false; 
	 }
 } 
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkKey(key)
 {
    var val=key.value.substring(0,key.value.length-1); 
    if((event.keyCode<48 || event.keyCode>57) && (event.keyCode<96 || event.keyCode>105) && (event.keyCode!=190))
     {
	     key.value=val;
		 //alert("Enter Numbers [0-9] only");
		 key.focus();
	 }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkUsername(login,table)
{
	if(trimStr(login.value)!="")
	 {
	   var x=window.open("checkusername.php?login="+login.value+"&table="+table,"Right","width=250,Height=140,tools=no,address=no,scrollbars=no");
	   x.moveTo(screen.width/2-150,screen.height/2-120); 
	 }
	else
	 {
	     alert("Please enter the required Login Name");
		 login.focus();
	 } 
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function checkNum(fld)
{
    
	if(trimStr(fld.value)!="")
	  {
	      estb=trimStr(fld.value);
		  if(estb.length<4)
		   {
		      
		   }  
		  if(!isNum(estb))
		   {
		       alert("Invalid charecters in the field\n\nEnter only in numbers[0-9]")
			   fld.focus();
			   return 0;
		   }  
	  }
	
	return 1;
}
///////////////////////////////////////////////////////////////////////////////////////////////
function checkEmail(mail)
{
    if(trimStr(mail.value)!="")
	{
		var dotpos=mail.value.lastIndexOf('.');
		var amppos=mail.value.indexOf('@');
		var lastpos=mail.value.length;
			 
		if(dotpos<=amppos | amppos<1 | lastpos==dotpos+1)
		 {
			alert("Please enter the email address correctly");
			mail.focus();
			return 0;
		 } 
    }
	return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////////
function checkWeb(web)
{
    if(trimStr(web.value)!="")
	{
		var dotpos=web.value.lastIndexOf('.');
		var lastpos=web.value.length;
			 
		if(dotpos==-1 | dotpos==0 | lastpos==dotpos+1)
		 {
			 alert("Please enter the website address correctly");
			 web.focus();
			 return 0;
		 } 
    }
	
	return 1;
}
//////////////////////////////////////////////////////////////////////////////////////////////
function checkLogindetails(login,password)
 {
        if(trimStr(login.value)=="")
	   {
	      alert("Please enter a login name");
		  login.focus();
		  return 0;
	   }
	 
	  if(trimStr(login.value)!="")
	  {
		  if(trimStr(login.value).length<4)
		    {
			   alert("Please enter the login name in atleast 4 characters");
			   login.focus();
			   return 0;
		    } 
			
		  if(isAlphanum(login.value)==0)
		   {
			  alert("Please enter the login name in alphanumerics only");
			  login.focus();
			  return 0;
		   }
	  } 
    
	if(trimStr(password.value)=="")
	  {
	      alert("Please enter a password");
		  password.focus();
		  return 0;
	  }
	 
    if(trimStr(password.value)!="")
	 {
		  if(trimStr(password.value).length<4)
			{
			    alert("Please enter the password in atleast 4 characters");
			    password.focus();
			    return 0;
		    } 
		  if(isAlphanum(password.value)==0)
		    {
			   alert("Please enter the password in alphanumerics only");
			   password.focus();
			   return 0;
		    }
     }
	 return 1;
 }
////////////////////////////////////////////////////////////////////////////////////////////////
function checkPhoto(photo)
 {
    if(trimStr(photo.value)!="")
	  {
	     var ind=(photo.value).lastIndexOf('.');
		 if(ind==-1)
		 {
			alert("Please select a photo to upload");
			photo.focus();
			return 0;
		 }
	
	    var ext=photo.value.substr(ind);
	    ext=ext.toLowerCase();

	    if(ext==".bmp" | ext==".jpg" | ext==".gif" | ext==".jpeg")
	     {
	        return 1;
	     }
		else
		 {
			alert("Please select a photo of type .gif/.jpg/.bmp to upload");
			photo.focus();
	 	    return 0;
	     }  
    }
 
    return 1;
 }
 //////////////////////////////////////////////////////////////////////////////////////////
function checkUpload()
 {
    var frm=document.frmupload;
	if(trimStr(frm.subject.value)=="")
	 {
	    alert("Enter the subject/description about the file");
		frm.subject.focus();
		return false;
	 }
 }
 
