
/*
'*********************************************************************
'	Project Name	:-	EZmed
'	Client Name		:-	
'	Page Name		:-	Validations.js
'	Directory path	:-	/ezmedweb/include/Validations.js
'	Called from		:-	index.asp / link present on all pages
'	Author			:-	Praveen Ojha
'	Co-Author		:-	Venu
'	Created On		:-	13-Jul-2002
'	Description		:-	Used for client side validations
'	Tested By		:-	
'	Tested On		:-	
'	Bugs Reported	:-	
'	Bugs Fixed		:-	
'	Remark			:-	
'*********************************************************************




/*function to validate the social security number*/

function checkAllDigit(ssn,len,name){
var count2, i 
var ch1, ch2

count2=0
	var checkdigit="0123456789";

	if(ssn.length != len){
		alert("Enter "+len+" digit number for "+name);
		return false;
	}else	{
		for(i=0; i < ssn.length; i++)
			for(j=0;j<checkdigit.length; j++)
				if(ssn.charAt(i) == checkdigit.charAt(j))
					count2++;				

		if(count2 != len){
					
			
			alert(name + " should contain only numeric values");
			
			return false;
		}	
	}
	
return true;
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

function checkFederalTax(pft){
    
    

	var ch, count, i, count1, tmp, ft;
	ft = pft;
	count=0;
	count1=0;
	if(ft.length < 9 || ft.length > 10){
		alert("The federal tax id can be of 9 numeric values or 10 alpha numeric values");
		return false;
	}
	if(ft.length == 10){
		ch = ft.charAt(0);
		if(!((ch >= 'a' && ch <= 'z') || ( ch >= 'A' && ch <= 'Z'))){
			alert("For a 10 digit Federal Tax ID the first letter has to be of character value");
			//ft.focus();
			return false;
		}
		
		//to get the count of number of characters in 10 digit tax id
		for(i=0; i < ft.length; i++){
			ch = ft.charAt(i);
			if((ch >= 'a' && ch <= 'z') || ( ch >= 'A' && ch <= 'Z'))
				count++;

		}
		if(count > 1){
			alert("10 digit federal tax id can contain only one character");
			return false;
		}
		
		tmp = ft.charAt(0);
		tmp = tmp.toUpperCase() 
		tmp = tmp + ft.substr(1, ft.length);
		pft.value = tmp;
	
	}
	
	if(ft.length == 9){
		ch = ft.charAt(0);
		if((ch >= 'a' && ch <= 'z') || ( ch >= 'A' && ch <= 'Z')){
			alert("In a nine digit Federal Tax Id, you cannot have first letter of character value");
			return false;
		
		}
		
		//to check if any of the nine digits is a alpha numeric
		for(i=0; i<ft.length; i++){
			ch=ft.charAt(i);
			if((ch >= 'a' && ch <= 'z') || ( ch >= 'A' && ch <= 'Z'))
				count1++;
		}
		if(count1>0){
			alert("In a 9 digit federal tax id, no character values are allowed")
			return false;
		}
	
	}
	return true;
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

/*function to validate email id*/

function isEmail (s)
{   if(s == null) 
       if (isEmail.arguments.length == 1) 
       return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    
    //if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we start looking at character position 1  (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

//function to check if the parameter contains a character

function checkNumber(str){
	var cnt
		for(cnt=0; cnt<str.length; cnt++)
		{
		
				if (isNaN(str.substr(cnt,1))==true )
				{
					return false;
				}
		}
		return true;
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/


//written by venu on 13-09-2002

//Code to validate Characters in the string for Name	
function ValidateName(name,fieldname)
{

var count4,k 
var cnt1

count4=0;


	var checkname="0123456789~!@#$%^&*()<>?|,;";
	
		for(cnt1=0; cnt1<name.length; cnt1++)
		{
		 
		 
		  
		for(k=0;k<checkname.length; k++)
		{
			if(name.charAt(cnt1) == checkname.charAt(k))
			{
			
			count4++;				
			}
		}
		}
				
		if(count4 > 0){
			
			alert(fieldname + " should contain only characters");
			
			return false;
		}				
			
	return true;
}



/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
//written by venu on 13-09-2002

//Code to check  special characters exist in the field.	


function Validatespecialchar(str,field)
{

var count3,j 
var cnt

count3=0;


	var checkschar="~!@#$%^&*()<>?|,;";
	
		for(cnt=0; cnt<str.length; cnt++)
		{
		  
		for(j=0;j<checkschar.length; j++)
		{
			if(str.charAt(cnt) == checkschar.charAt(j))
			{
			
			count3++;				
			}
		}
		}
				
		if(count3 > 0){
			
			alert("Special characters are not allowed for "+field+" field");
			return false;
		}				
			
	return true;
}










/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/





//Code to convert first character of each word to upper case
function U_Case(obj){ 
  
	var i, str1,str,splitStr, str2,str3;
	i=0;
	str = obj.value;
	
	str2 = "";
	splitStr = str.split(" ");
		
	for(i=0;i<splitStr.length;i++){
		str3 = splitStr[i].substr(0,1);
		if(splitStr[i].charAt(0) >= 'a' && splitStr[i].charAt(0) <= 'z')
			str3 = str3.toUpperCase();

		str2 += str3;
		str2 += splitStr[i].substr(1, splitStr[i].length);
		str2 += " ";				
	}
	obj.value = str2;	
}

/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

//Validations for Date

function validateDate(ldate)
{
	
	var arrDate = new Array(3);
	var pdate, lMonth, lDay, lYear, arrYear;
	var tmpYear;
	pdate = trimed(ldate);
	
	if(pdate.length != 8 ){
		alert("Please enter the date as Month xx Day xx Year xxxx.");
		return false;
	}
	
	if (!checkNumericDate(pdate))
	{
	 alert("Invalid date");
	 return false;
	}
	arrDate[0] = pdate.substr(0, 2);
	arrDate[1] = pdate.substr(2, 2);
	arrDate[2] = pdate.substr(4,4);
	lMonth = arrDate[0];
	lDay = arrDate[1];
	lYear = arrDate[2];
	
	
	arrYear = lYear.substr(0, 2);
	tmpYear = lYear.substr(2, 2);
	
	//validation for month
	
	if(parseFloat(lMonth) < 1 || parseFloat(lMonth) > 12){
		alert("The Month value has to be between 01 and 12. Please check your entry");
		return false;
	}
	//validation for day specific to months having 31 days
	
	
	if(parseInt(lMonth)%2 == 1 && parseInt(lDay)>30 && parseInt(lMonth)==11)
	{	
	    
	   	alert("The month " + lMonth + " has maximum 30 days.Please enter correct day");
		return(false);
	}
	
	
	
	if(parseInt(lMonth)%2 == 1 && parseInt(lDay)>31)
	{	
	    
	   	alert("Invalid day for the specified month");
		return(false);
	}
	
	
	
	//validation for day specific to months having 30 days
	
	
	if(parseInt(lMonth)%2 == 0 && parseInt(lDay)>30 && (parseInt(lMonth)==8 || parseInt(lMonth)==9) && parseInt(lMonth)!=12)
	{	
		alert("Invalid day for the specified month");
		return(false);
	}
	
	  
	
	if(lMonth!= "08" && parseInt(lMonth)!= 10)
	{	
	 
	if(parseInt(lMonth)%2==0 && parseInt(lDay)>30 && parseInt(lMonth) != 2 && parseInt(lMonth) != 12)
	{
		alert("The month " + lMonth + " has maximum 30 days.Please enter correct day");
		return false;
	}
	}
	
	 if (lMonth == "08" || parseInt(lMonth) == 12 || parseInt(lMonth) == 10)
	 {
	   
		 if(parseInt(lDay)>31)
		{
		alert("The month " + lMonth + " has maximum 31 days.Please enter correct day");
		return false;		
		}
	 }	  
	
	
	//Code to check if days in Feb as per Leap Year.
	if (lMonth == 02)
	{
		if ((parseInt(tmpYear)%4==0 && parseInt(tmpYear)/100!=0) ||( parseInt(tmpYear)/400==0))
		{
			//As leap year the Number of Days can be = 29
			if (lDay > 29 && lMonth==02)
			{
				alert ("Invalid day for the specified month");
				return false;
			}			
		}
		else
		{
			if (lDay > 28 && lMonth==02)
			{
				alert ("Invalid day for the specified month");
				return false;
			}
		}
	}
	//validating the value for the millenium number
	if(arrYear != 20 && arrYear != 19){
		alert("Please check the Millenium value. The year has to be 19xx or 20xx");
		return false;
	}
	
	
return true;
}
	
//function to validate controls for char, numeric values

function validate(param, type, name){
		if(type=="char"){
			if(param != ""){
				if(!ValidateCharacter(param.value)){
					alert(name + " should contain only character values");
					param.focus();
					return false;
				}
				else{
					U_Case(param);
				}
			}
		}
		
		if(type=="number"){
			if(param != ""){
				if(name != "Federal Tax Id"){
					if(!checkNumber(param.value)){
						alert(name + " should contain only numeric values");
						param.focus();
						return false;
					}
				}
				/*else{  //if param is for federal tax id
					ch = param.value
					if(param.length == 9){
						checkFT(param);
				
					} 
				}*/
				if(name == "Federal Tax Id"){
					if(!checkFederalTax(param)){
						
						//alert("Please Enter Valid Federal Tax ID")
						return false;
					}			
				}
			}
			
		}
		
		if(type=="varchar"){
			if (param != ""){
				U_Case(param);
			}
		}
		return true;
	}


/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

//Validations for Date to grater than from date

function CheckDate(fdate, tdate)
{
	
	var fromdate, todate, fMonth, fDay, fYear, tMonth, tDay, tYear, arrYear;
	var tmpYear;
	fromdate = fdate;
	todate = tdate;
			
	if(fromdate=="")
	{
	alert ("Enter from date");	
	return false;
	}
	
	fMonth= fromdate.substring(0,2);
	fDay = fromdate.substring(2,4);
	fYear = fromdate.substring(4,8);	
	
	fromdate = new Date(fYear,fMonth,fDay)
	
	tMonth = todate.substring(0, 2);
	tDay = todate.substring(2, 4);
	tYear = todate.substring(4,8);
	
	todate = new Date(tYear,tMonth,tDay)
	
	if(fromdate > todate)
	{
		alert("Todate must be greater than FromDate!!");
		return false;	
	}
	
return true;
}
//--------------------------------------------------------------------------
//function to convert original date format to ddmmyyyy format
//--------------------------------------------------------------------------
function DateForDisplay(pDate)
{
var tp, dDate, mm, dd, yy;

if(pDate != '')
{	
	tp =  pDate.split("/");
		
	if(tp[0].length < 2 )
		mm = "0" + tp[0];
	else
		mm = tp[0];
	
	if(tp[1].length < 2 ) 
		dd = "0" + tp[1];
	else
		dd = tp[1];
	
	yy = tp[2];
		
	dDate = mm + dd + yy;			
	
	return dDate;
}
else{
	return pDate;
}	
}

// function to check numeric charcters exist or not


function checkNumericDate(field)
{
var count2, i 
var ch1, ch2

count2=0
	var checkdigit="0123456789";

			for(i=0; i < field.length; i++)
			for(j=0;j<checkdigit.length; j++)
			if(field.charAt(i) == checkdigit.charAt(j))
				count2++;				
			
			
			if(count2 != 8){					
			
			//alert(name + " should contain only numeric values");
			
			return false;
		}	
		return true;

}
//--------------------------------------------------------------------------
function trimed(trimstring)
{
	startcounter = 0;
	for(i=0; i < trimstring.length; i++)
	{ 
	//alert(trimstring.charAt(i));
		if(trimstring.charAt(i) == " ") 
			++startcounter;
		else
			break;
				   
	}

	endcounter = trimstring.length;
	for(i=(trimstring.length-1); i > 0; i--)
	{ 
	//alert(trimstring.charAt(i));
	   if(trimstring.charAt(i) == " ") 
			--endcounter;
		else
			break;
				   
    }
	
	trimmedstring = trimstring.substring(startcounter, endcounter);
	if(endcounter <= startcounter)
	{
		trimmedstring = "";
	}
	
	return trimmedstring;
}
//--------------------------------------------------------------------------
function checkNumeric(field)
{
var count2, i 
var ch1, ch2

count2=0
var checkdigit="0123456789.-";

	for(i=0; i < field.length; i++)
	for(j=0;j<checkdigit.length; j++)
	if(field.charAt(i) == checkdigit.charAt(j))
		count2++;						
			
		if(count2 != field.length){								
			return false;
		}	
		return true;
}
//--------------------------------------------------------------------------
function popUp(strURL,strType,strHeight,strWidth) {
var strOptions="";
if (strType=="console") strOptions="scrollbars=yes,resizable=no,titlebar=no, left=0, top=0, screenX=300, screen=200,height="+strHeight+",width="+strWidth;
if (strType=="fixed") strOptions="status,height="+strHeight+",width="+strWidth;
if (strType=="elastic") strOptions="toolbar,menubar,scrollbars,resizable,location,height="+strHeight+",width="+strWidth;
newWin = window.open(strURL, 'newWin', strOptions);
newWin.focus();
}
