
//********************************************************************
//allows alphabets, numbers, underscore
//********************************************************************
function isAlphaNumeric(txtObj)
{   
	var str = txtObj.value;   
	// Return false if name field is blank.   
	if (str == "")
	{   
		alert("\nThis field is blank.\n\nPlease enter the value.")      
		txtObj.focus();      
		return false;      
	}   
	for (var i = 0; i < str.length; i++)
	{      
		var ch = str.substring(i, i + 1);      
		if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && ch != '_' && ch !=' ' &&  ch !='-')
		{         
			alert("\nThis field accepts only alphabets, numbers, hyphen & underscore.\n\nPlease re-enter !!.");
			txtObj.select();         
			txtObj.focus();         
			return false;      
		}      
	}   
}
//********************************************************************

//********************************************************************
//allows alphabets, space, dot, comma
//********************************************************************
function isText(txtObj)
{   
	var str = txtObj.value;   
	// Return false if name field is blank.   
	if (str == "")
	{   
		alert("\nThis field is blank.\n\nPlease enter the value.")      
		txtObj.focus();      
		return false;      
	}   
	// Return false if characters are not a-z, A-Z, or a space and a dot.   
	for (var i = 0; i < str.length; i++)
	{      
		var ch = str.substring(i, i + 1);      
		if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && ch != ' ' && ch != '.' && ch != ',' && ch != ')' && ch != '(' && ch != '-')
		{         
			alert("\nThis field accepts only alphabets, space, dot & spaces.\n\nPlease re-enter !!.");
			txtObj.select();         
			txtObj.focus();         
			return false;      
		}      
	}   
}
//********************************************************************

//********************************************************************
//allows alphabets, space
//********************************************************************
function isName(txtObj,alertmsg)
{   
	var str = txtObj.value;   
	// Return false if characters are not a-z, A-Z, or a space.   
	for (var i = 0; i < str.length; i++)
	{      
		var ch = str.substring(i, i + 1);      
		if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && ch != ' ')
		{         
			alert(alertmsg);
			txtObj.select();         
			txtObj.focus();         
			return false;      
		}      
	}   
}
//********************************************************************

//********************************************************************
//allows alphabets only
//********************************************************************
function isAlphabet(txtObj,alertmsg)
{   
	var str = txtObj.value;   

	// Return false if characters are not a-z (or) A-Z.   
	for (var i = 0; i < str.length; i++)
	{      
		var ch = str.substring(i, i + 1);      
		if ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch))
		{         
			alert(alertmsg);
			txtObj.select();         
			txtObj.focus();         
			return false;      
		}      
	}   
}
//********************************************************************




//********************************************************************
//	Function for Email Address validation
//********************************************************************
function isEmail(mailfield)
{
	var mail=mailfield.value
	// Looking for a @,.//Looking for @ in the last position//looking for . in the first position
	if (mail.indexOf("@") == -1 || mail.indexOf("@")+1 == mail.length || mail.indexOf(".") == -1 || mail.indexOf(".")+1 == mail.length  || mail.substring(0,1) == ".")
 	{
 		 	alert("Invalid Email ID")
			mailfield.focus()
			mailfield.select()
     		return false;
		}
 		else
 		{
			//looking for . just before & after @		//looking for @ for the first position
			var n = mail.indexOf("@")
			var n1 = mail.indexOf(".",n)
 		 	var n2 = mail.lastIndexOf(".",n)
			if(n+1 == n1 || n-1 == n2)
 		 	{
 		 		alert("Invalid Email ID")
				mailfield.focus()
				mailfield.select()
				return false;
	 		}
	 	}
		//Looking for two consecutive .'s		//Looking for special characters
		var k=0
 		for(var j=0;j<mail.length;j++)
		{
			if((mail.substring(j,j+1) >= 'a' && mail.substring(j,j+1) <= 'z') || (mail.substring(j,j+1) >= 'A' && mail.substring(j,j+1) <= 'Z') || (mail.substring(j,j+1) >= '0' && mail.substring(j,j+1) <= '9') || mail.substring(j,j+1) == '.' || mail.substring(j,j+1) == '@' || mail.substring(j,j+1) == '_' || mail.substring(j,j+1) == '-')
 		 	{}
 		 	else
 		 	{
 		 		alert("Invalid Email ID")
				mailfield.focus()
				mailfield.select()
				return false;
			}
			if(mail.substring(j,j+1) == ".")
			{
				if(k+1 == j && j!=1)
				{
 		 			alert("Invalid Email ID")
					mailfield.focus()
					mailfield.select()
					return false;		
				}
				k = j
			}	
		}
		// looking for .,@ at the last position
		if(mail.lastIndexOf(".")+1 == mail.length || mail.lastIndexOf("@")+1 == mail.length)
		{
 		 	alert("Invalid Email ID")
			mailfield.focus()
			mailfield.select()
			return false;
		}
		//looking for more than one @
		if(mail.indexOf("@") != mail.lastIndexOf("@"))
		{
 		 	alert("Invalid Email ID")
			mailfield.focus()
			mailfield.select()
			return false;
		}
}
//********************************************************************



//********************************************************************
//allows numbers and dot
//********************************************************************
function isNumber(txtObj,alertmsg)
{
	var x=txtObj.value
	var anum=/(^\d+$)|(^\d+\.\d+$)/
	if (anum.test(x))
		return true
	else
	{
		alert(alertmsg)
		txtObj.focus()
		txtObj.select()
		return false
	}
}
//********************************************************************


//********************************************************************
//allows numbers only
//********************************************************************
function isOnlyNumber(txtObj,alertmsg)
{
	for (var i = 0; i < txtObj.value.length; i++)
	{      
	var ch = txtObj.value.substring(i, i + 1);      
	if (ch < "0" || "9" < ch)
	{
		alert(alertmsg)
		txtObj.focus()
		txtObj.select()
		return false
	}
	}
}
//********************************************************************


//********************************************************************
//allows alphabets, numbers & underscore
//checks for identity in the given two password fields
//********************************************************************
function isPassword(objpw1,objpw2)
{
	pw1 = objpw1.value;
	pw2 = objpw2.value;

	pw1 = objpw1.value;
	str = "' "
	for (i=0;i<=pw1.length-1;i++)
		{
		ch = pw1.substring(i,i+1)
		if (str.indexOf(ch)>-1)
			{
			if (ch==" ")
				alert("Space is not allowed");			
			else
				alert(ch + "   is not allowed");
			objpw1.focus();
			objpw1.select();
			return false;	
			}
		}
/*	if (pw1.length < 5)
	{
		alert("Password should be alteast 5 characters")
		objpw1.focus()
		objpw1.select()
		return false;
	}
*/	pw2 = objpw2.value;
	str = "' "
	for (i=0;i<=pw2.length-1;i++)
		{
		ch = pw2.substring(i,i+1)
		if (str.indexOf(ch)>-1)
			{
			if (ch==" ")
				alert("Space is not allowed");			
			else
				alert(ch + "   is not allowed");
			objpw2.focus();
			objpw2.select();
			return false;	
			}
		}
	if (pw1 != pw2)
	{
		alert ("\nPassword and Confirm Password should be same. \n\nPlease re-enter your Confirm password.")
		objpw2.focus()
		objpw2.select()
		return false;
	}
}





//********************************************************************
//allows alphabets, numbers & underscore
//checks for identity in the given two password fields
//********************************************************************
function isPassword1(objpw1)
{
	pw1 = objpw1.value;
	str = "' "
	for (i=0;i<=pw1.length-1;i++)
		{
		ch = pw1.substring(i,i+1)
		if (str.indexOf(ch)>-1)
			{
			if (ch==" ")
				alert("Space is not allowed");			
			else
				alert(ch + "   is not allowed");
			objpw1.focus();
			objpw1.select();
			return false;	
			}
		}
	if (pw1.length < 4)
	{
		alert("Password should be alteast 4 characters")
		objpw1.focus()
		objpw1.select()
		return false;
	}
}



//********************************************************************
//Function to trap all the special characters listed below
//********************************************************************
function isValue(txtObj)
{
	str = "@#$%^`~'|?"
	val = txtObj.value
	for (i=0;i<=val.length-1;i++)
		{
		ch = val.substring(i,i+1)
		if (str.indexOf(ch)>-1)
			{
			alert(ch + "   is not allowed");
			txtObj.focus();
			txtObj.select();
			return false;	
			}
		}
}
//********************************************************************




//********************************************************************
//Function to check trailing spaces and leading spaces
//********************************************************************
function isTrimmed(txtObj) {
// This function will trim leading and/or trailing spaces from a string
// arg = the value you wish to have trimmed..
// func = "left" for Ltrim(), "right" for RTrim() or "both" for Trim()
var arg,func
arg = txtObj.value
func = "both"
var trimvalue = "";
arglen = arg.length;

//alert(arg)
if (func == "left" || func== "both") {
i = 0;
pos = -1;
while (i < arglen) {
if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) {
pos = i;
break;
}
i++;
}
}

if (func == "right" || func== "both") {
var lastpos = -1;
i = arglen;
while (i >= 0) {
if (arg.charCodeAt(i) != 32 && !isNaN(arg.charCodeAt(i))) {
lastpos = i;
break;
}
i--;
}
}

if (pos!=0)
	{
	alert("Remove the leading spaces")
	txtObj.focus()
	//txtObj.select()
	return false
	}

if (lastpos!=arglen-1)
	{
	alert("Remove the trailing spaces")
	txtObj.focus()
	//txtObj.select()
	return false
	}
}
//********************************************************************


//********************************************************************
// Date Validation Version 2 (both month and year should be in combo).
//********************************************************************
function isDate(df,mf,yf)
{
	if (df.options[df.selectedIndex].value=="###")
	{
		alert("Please Select the Day");
		df.focus();
		return false;
	}
	if (mf.options[mf.selectedIndex].value=="###")
	{
		alert("Please Select the Month");
		mf.focus();
		return false;
	}
	if (yf.options[yf.selectedIndex].value=="###") 
	{
		alert("Please Select the Year");
		yf.focus();
		return false;
	}
	if (yf.options[yf.selectedIndex].value>=0)
		{	i=0	}
	else
		{	i=1	}
	yy = yf.options[yf.selectedIndex].value
	mm = mf.options[mf.selectedIndex].value
	dd = df.options[df.selectedIndex].value
	le = yy%4
	mo = mm%2

	if(mm > 12) 
	{
		alert("Invalid Month")
		mf.focus()
		return false
	}
	else 
	{
		if(mo==0)
		{
			if(mm==2)
			{
				if (le == 0)
				{
					if(dd>29)
					{
						alert("Invalid Date")
						df.focus()
						return false
					}
				}
				else
				{
					if(dd>28)
					{
						alert("Invalid Date")
						df.focus()
						return false
					}				
				}
			}
			if(mm==8 || mm==10 || mm==12)
			{
				if(dd>31)
				{
					alert("Invalid Date")
					df.focus()
					return false
				}
			}
			else
			{
				if (dd>30)
				{
					alert("Invalid Date")
					df.focus()
					return false
				}
			}
		}
		else		//if mm = 1
		{
			if (mm == 9 || mm == 11)
			{
				if(dd>30)
				{
					alert("Invalid Date")
					df.focus()
					return false
				}
			}
			else
			{
				if (dd>31)
				{
					alert("Invalid Date")
					df.focus()
					return false
				}
			}
		}	
	}
}	
//********************************************************************




//********************************************************************

//********************************************************************
// - Used to find whether the MinDate is greater than the MaxDate - Comparing two dates
//********************************************************************
//Returns True if the First Date is lesser than or equal to Second Date
//Returns False if the Second Date is lesser than First Date


function checkMonth(od,om,oy,cd,cm,cy)
{
//alert(om+"\n"+cm)
	if(parseInt(om)<parseInt(cm))
		{//alert('retCMOIF')
		//return checkDay(od,om,oy,cd,cm,cy)
		
		}
	else if(om==cm)
	
		{
		//alert('retCMOelseIF')
		return checkDay(od,om,oy,cd,cm,cy)	}
	else
		{
		
		//alert('retCMOElse')
		return false	}
}

function checkDay(od,om,oy,cd,cm,cy)
{
//alert(od+"\n"+cd)
	if(parseInt(od)<parseInt(cd))
		{
		//alert('retCDOIF')
		}
	else
	{	
	//alert('retCDOelse')
	return false	}
}


function isFromToDate(od,om,oy,cd,cm,cy)
{
//alert(od+"\n"+om+"\n"+oy)
//alert(cd+"\n"+cm+"\n"+cy)

	if(oy<cy)
		{
		//alert('retMIF')
		}
	else if(oy==cy)
		{
		//alert('retMElsif')
		 return checkMonth(od,om,oy,cd,cm,cy)	}
	else
		{
		//alert('retMelse')
			return false	}

}
//********************************************************************


//********************************************************************
//Function for phone number validation
//********************************************************************
function isPhone(txtObj)
{
	str = "0123456789+,-()."
	val = txtObj.value
	for (i=0;i<=val.length-1;i++)
		{
		ch = val.substring(i,i+1)
		if (str.indexOf(ch)>-1)
			{}
		else
			{
			alert("Enter a valid Phone Number\n");
			txtObj.focus();
			txtObj.select();
			return false;	
			}
		}
}
//********************************************************************


//********************************************************************
//Function for fax number validation
//********************************************************************
function isFax(txtObj)
{
	str = "0123456789+,-()."
	val = txtObj.value
	for (i=0;i<=val.length-1;i++)
		{
		ch = val.substring(i,i+1)
		if (str.indexOf(ch)>-1)
			{}
		else
			{
			alert("Enter a valid Fax Number\n");
			txtObj.focus();
			txtObj.select();
			return false;	
			}
		}
}
//********************************************************************


//********************************************************************
//allows alphabets, numbers & underscore
//Function for User ID validation
//********************************************************************
function isUserID(txtObj,alrtmsg)
{
var str = txtObj.value;
if((str.substring(0,1)<"a" || str.substring(0,1)>"z") && (str.substring(0,1)<"A" || str.substring(0,1)>"Z"))
	{
	alert(alrtmsg+ " should begin with an alphabetic character.");
	txtObj.focus();
	txtObj.select();
	return false;
	}
for (var i = 1; i < str.length; i++) 
	{
	var ch = str.substring(i, i + 1);
	if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '_')) 
	{
	alert(alrtmsg+ " accepts letters,numbers & underscore.\n\nPlease re-enter");
	txtObj.focus();
	txtObj.select();
	return false;
	}
	}
}
//********************************************************************



//********************************************************************
// - can be used ISBN validation
//********************************************************************
function isISBN(txtObj)
{   
	var str = txtObj.value;   
	// Return false if name field is blank.   
	for (var i = 0; i < str.length; i++)
	{      
		var ch = str.substring(i, i + 1);      
		if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && ch != '-')
		{         
			alert("\nEnter Valid ISBN\n\nPlease re-enter !!.");
			txtObj.select();
			txtObj.focus();
			return false;
		}      
	}   
}
//********************************************************************

function addimage(intImageID,mode)
{

	var imagewin,style,filename;
	filename = "upload.php?imgid="+intImageID+"&Mode="+mode+""
	style="toolbar=no,height=250,width=300,top=150,left=200"
	imagewin = window.open(filename,"imagewin",style)

}

//********************************************************************
//Function to check null value of the field
//********************************************************************
function isCheckAlert(txtObj,alertmsg)
{

var str = txtObj.value;   
	// Return false if name field is blank.   
if (str == "")
	{   
		alert(alertmsg)      
		txtObj.focus();      
		return false;   
	}   
	if (isTrimmed(txtObj)==false)
			{return false}
}		

//********************************************************************
//Function to check null value of the field
//********************************************************************
function isEmpty(txtObj,alertmsg)
{

var str = txtObj.value;   
	// Return false if name field is blank.   
if (str == "")
	{   
		alert(alertmsg)      
		txtObj.focus();      
		return false;   
	}   
}		

//********************************************************************
//Function for image validation
//********************************************************************
function image(imgfield)
{
var str = imgfield.value;   

	if(str == "")
	{
		alert("Upload Image");
		imgfield.focus();
		return false;
	}
	else
	{
		n = imgfield.value;
		var getstr = n.split(".");
		len = getstr.length;
		ext = getstr[len-1]; 
		if(ext != "jpg" && ext != "gif" && ext != "jpeg" && ext != "bmp" && ext != "png")
		{
			alert("Upload image files only");
			return false;
		}
	}
}

//********************************************************************
//Function for check box validation
//********************************************************************
function isCheck(checkfield,alertmsg)
{
	var len = checkfield.length;
	//alert(len); exit();
	var total=0;
	for(var i=0; i < len; i++){
		if(checkfield[i].checked)
		total +=checkfield[i].value + "\n";
	}
	if(total==0){
		alert(alertmsg);
		return false;
	}
}

//********************************************************************
//Function for radio button validation
//********************************************************************
function radio_button(buttons,alertmsg)  
{ 
  var checked = false; 
  var len = buttons.length;
  for (var i=0; i<buttons.length; i++)  
  {  
    if (buttons[i].checked) {  
      checked = true; 
      break;  
    }  
   } 
   if(!checked){
     alert(alertmsg);  
   return false; 
   }
}                  

//********************************************************************
//Function for jpg image validation
//********************************************************************
function isjpg(imgfield)
{
var str = imgfield.value;   
if(str != "") {
	n = imgfield.value;
	var getstr = n.split(".");
	len = getstr.length;
	ext = getstr[len-1]; 
	if(ext != "jpg" && ext != "jpeg" && ext != "JPG" && ext != "JPEG")
	{
		alert("Upload jpg files only");
		imgfield.focus();
		return false;
	} }
}

//********************************************************************
//Function for pdf file validation
//********************************************************************
function ispdf(field)
{
var str = field.value;   
if(str != "") {
	n = field.value;
	var getstr = n.split(".");
	len = getstr.length;
	ext = getstr[len-1]; 
	if(ext != "pdf" && ext != "PDF")
	{
		alert("Upload PDF files only");
		field.focus();
		return false;
	}}
}
