// This fuction is to check the special characters.
//fieldname = The Value of the field coming from form.
// message = The Message passed by the user.

function spChars(fieldname,message) 
{
	chars=new Array("~","`","!","#","$","%","^","&","*","(",")","+","=","|","\\","{","}","[","]",":",";","'","/","'","<",">","?",",","\"")
	var varLen=fieldname.value.length
	
	for (var i=0;i<varLen;i++)
		{
			
		 for(j=0 ;j<=chars.length;j++)
			{
			if (fieldname.value.charAt(i) == chars[j]) // to get the position of special character
				{
				strval = fieldname.value.charAt(i) // display the special character
				alert(message +"---- "+strval)				
				return false;
				}
			}
		}
		return true;	
		
}

// This fuction is to check numeric value.
// fieldname = The Value of the field coming from form.
// message = The Message passed by the user.

function digits(fieldname,message)
{
	
	var name = fieldname.value ;
	
		for(i=0;i<name.length;i++)
		{
			newChar = name.substring(i,i+1);
			if (((newChar >= 0 && newChar <= 9)== false) && ((newChar == '.') == false ))//checking for numeric value
			{
				alert(message);
				return false;
			}
		}
	return true;
	
}
function digitswithoutdot(fieldname,message)
{
	//debugger
	var name = fieldname.value ;
	
		for(i=0;i<name.length;i++)
		{
			newChar = name.substring(i,i+1);
			if ((newChar >= 0 && newChar <= 9)== false) //checking for numeric value
			{
				alert(message);
				return false;
			}
		}
	return true;
	
}

// This fuction is to check alphabet value.
// fieldname = The Value of the field coming from form.
// message = The Message passed by the user.

function alphabet(fieldname,message)
{
	
	var name = fieldname.value ;
	var mess=message;	
		for(i=0;i<name.length;i++)
		{
			newChar = name.substring(i,i+1);
			
			if (((newChar >= 'a' && newChar <= 'z') == false) && ((newChar >= 'A' && newChar <= 'Z') == false)) 
			
			{
				alert(message);
				return false;
			}
		}
	return true;
	
}

// This fuction is to check email validation.
// fieldname = The Value of the field coming from form.
// message = The Message passed by the user.

function emailval(fieldname,message)
{
	//alert("hello");
	//var field = fieldname;  email field
  	var str = fieldname.value; 
	//alert(str);
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; 
        // Regular expression to check email validation 
  	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; 
  	if (!reg1.test(str) && reg2.test(str)) //email checking
		{ 				
   			return true;
	  	}
 	else
		{	
			mailval = str;
			alert(message);
			return false;
		}
	
}

// This fuction is to check empty field.
// fieldname = The Value of the field coming from form.
// message = The Message passed by the user.

function chkempty(fieldname,message)
{
		
		if (trimAll(fieldname.value) == '') // checking for empty field 
		{
			alert(message);
			return false;			
		}
		return true;
}	

//Empty check Function
function empty()
{
	for(i=0;i<5;i++)
	{
		if(document.forms(0).elements(i).value == "")
		{
			alert("PLEASE FILL" + "   " + document.forms(0).elements(i).name.toUpperCase());
			document.forms(0).elements(i).focus();
			return false;
		}
		
	}
	return true;
}
//To use all these functions on KeyPress
//functions for Numeric,single quote and double quote,special characters
function sp_chars()
{
	if ((event.keyCode > 32 && event.keyCode < 48) || (event.keyCode > 57 && event.keyCode < 65) || (event.keyCode > 90 && event.keyCode < 97)) event.returnValue = false;
}
function single_quote()
{
	if (event.keyCode==34 || event.keyCode==39) event.returnValue = false;
}
function chkNumeric()
{
	if (event.keyCode < 46 || event.keyCode > 57) event.returnValue = false;
}
function chkNumeric()
{
	if (event.keyCode < 46 || event.keyCode > 57) event.returnValue = false;
} 

function allowNumeric(evt)
{  
    var charCode = (evt.which) ? evt.which : event.keyCode
    if(charCode == 8 || charCode == 46)
    {
        return true;
    }
    else
    {
        if (charCode < 46 || charCode > 57) 
        {
            return false;
        }
        else
        {
            return true;        
        }    
        
    }
     
    
}

function chkPercentage(evt)
{  
    var charCode = (evt.which) ? evt.which : event.keyCode
    if(charCode == 8 || charCode == 46 || charCode == 110)
    {
        return true;
    }
    else
    {
        if (charCode < 46 || charCode > 57) 
        {
            return false;
        }
        else
        {
            return true;        
        }    
        
    }
     
    
}

function chkDate(fdate,message)
{
	strDate = fdate.value;
	//alert(strDate)
	//Must be longer than 7 characters, first (two) digit(s) NOT greater than 12, second (two) digit(s) NOT greater than 31 and last 4 greater than 1900
	if(strDate.length>7&&!(strDate.split("/")[0]*1>12)&&!(strDate.split("/")[1]*1>31)&&!(strDate.split("/")[2]*1<1900))
	//if(strDate.length>7&&!(strDate.split("/")[0]*1>31)&&!(strDate.split("/")[1]*1>12)&&!(strDate.split("/")[2]*1<1900))
	{
		return true;	
	}
	else
	{
		alert(message);
		return false;
	}
}
function isDdlist(objField,message)
				{ 
					if (objField.selectedIndex == 0 )
					{
						alert(message);
						return false;
					}
					return true;
				}
function isDdlistfirst(objField,message)
				{ 
					if (objField.selectedIndex == -1 )
					{
						alert(message);
						return false;
					}
					return true;
				}				
				
function opennewwindow(VarPageName,VarWidth,VarHeight)
    {
        var width = VarWidth;
        var height = VarHeight;
        var left = parseInt((screen.availWidth/2) - (width/2));
        var top = parseInt((screen.availHeight/2) - (height/2));
        var windowFeatures = "width=" + width + ",height=" + height + ",status=yes,resizabl=yes,toolbar=no,menubar=no,location=no,scrollbars=no,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
        mywindow = window.open(VarPageName,"hello",windowFeatures);
        return false;
    }
function opennewwindowMax(VarPageName,VarWidth,VarHeight)
    {
        
        var width = VarWidth;
        var height = VarHeight;
        var left = parseInt((screen.availWidth/2) - (width/2));
        var top = parseInt((screen.availHeight/2) - (height/2));
        var windowFeatures = "width=" + width + ",height=" + height + ",resizable=yes,scrollbars=yes,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
        mywindow = window.open(VarPageName,"",windowFeatures);
        return false;
    }
function chekImageExt(fileName,message)
{   if (!(fileName.value < 1))
    {
	    var objRule = /^(\S*.jpeg)$|^(\S*.jpg)$|^(\S*.JPG)$|^(\S*.gif)$|^(\S*.GIF)$|^(\S*.bmp)$|^(\S*.BMP)$|^(\S*.JPEG)$/;
	    //var objRule = /^(\*.jpeg)$|^(\*.jpg)$|^(\*.gif)$|^(\*.bmp)$|^(\*.JPEG)$/;
	    var str;
	    var str1;
	    var lvarfile;
	    str = fileName.value;
	    pos = str.lastIndexOf("\\");
	    lvarfile = str.substring(pos+1);
	    lvarfile = lvarfile.replace(/ /g,"") //remove space 
	    if (objRule.test(lvarfile))
	        {
	            return true;
	        }
	    else
	        {
	            alert(message);
	            return false;
	        }    
    }
}       

function chekDocExt(fileName,message)
{   if (!(fileName.value < 1))
    {
	    var objRule = /^(\S*.Doc)$|^(\S*.DOC)$|^(\S*.doc)$/;
	    //var objRule = /^(\*.jpeg)$|^(\*.jpg)$|^(\*.gif)$|^(\*.bmp)$|^(\*.JPEG)$/;
	    var str;
	    var str1;
	    var lvarfile;
	    str = fileName.value;
	    pos = str.lastIndexOf("\\");
	    lvarfile = str.substring(pos+1);
	    lvarfile = lvarfile.replace(/ /g,"") //remove space 
	    if (objRule.test(lvarfile))
	        {
	            return true;
	        }
	    else
	        {
	            alert(message);
	            return false;
	        }    
    }
}       

   //***************   Date Function MM/DD/YYYY   **************************				
/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
	    alert("The date format should be : dd/MM/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
	    alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}				
 
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;
}
