/*
'////////////////////////////////////////////////////////////////////////////////////'
' (c) Universal Software
' Module			:	Library

' FileName			:	Validations.js

' Description		:	This module would be useful in all the projects. 
'						Wherever client side validations required, an instance of the
'						'Validation' object can be created and then the methods of this object
						can be used.
						
						The following are the methods of the Validation object :
						
						1. isNull
						2. isNumeric
						3. trim
						4. isValidEmail
						5. isValidDate
						6. checkAll
						7. isChecked
						8. isoptSelected
						9. iscboSelected

' Author			:	Kanaiya Parmar

' Created On		:	Feb 9, 2001
'
' Functions			:	
'		Name		:	trimCheck(MyString, msg)
'		Desc.		:	It checks whether input parameter (MyString) contains null or blank
'						value or not. If yes, it alerts a message which is passed as msg. And 
'						it sets the focus on the field which has been passed as an argument.
'
'
'Navigation Details:	This file can be included on any asp page. Direct call is 
'						not possible.
'
'Modification History :
--------------------------------------------------------------------------------------------
' No	 	Date		Engineer	Remarks
--------------------------------------------------------------------------------------------
' 1.		02/09		KP			Original
' 2.		02/12       Vivek	    The file was updated to implement object oriented approach.
 
'-------------------------------------------------------------------------------------------
*/

/*
Function Name	:	descOver()
Descriotion	:	It displays passed message in the <div> named as CaptionTitle specified in the include file. So that
			it gives effect like information getting updated for each and every image or link.
Argument		:	Input - Message (message to get displayed in div)
*/
function descOver(Message)
{

	//document.getElementById('CaptionTitle').innerHTML = Message;
}

/*'**************************************************************************************'
'* Function		- Validation
'* Description	- This is an object created using javascript with 9 various methods
'* Methods 		- There are seven methods associated with this object.
'**************************************************************************************'*/
function Validation()
{
	this.isNull = isNull;
	this.isNumeric = isNumeric;
	this.trim = trim;
	this.isValidEmail = isValidEmail;
	this.isValidDate = isValidDate;
	this.checkAll = checkAll;
	this.isChecked = isChecked;
	this.iscboSelected = iscboSelected;
	this.isoptSelected = isoptSelected;
	this.isoptSelectedSpl = isoptSelectedSpl;
	this.isNumericWithSpace = isNumericWithSpace;
	this.charTextareaLimit = charTextareaLimit;
	this.isValidFilenameAndExtension=isValidFilenameAndExtension;
	
}
/*'**************************************************************************************'
'* Function		- isoptSelected(opt)
'* Args.		- opt 	- The name of the radio button
				
'* Return Value 	- It returns a boolean value.
'**************************************************************************************'*/
function isoptSelectedSpl(opt)
{
	if(opt.checked == false)
	{
		return false;	
	}
	else
	{
		return true;
	}

}


function isoptSelected(opt,msg)
{
	if(opt.checked == false)
	{
		alert(msg)
		opt.focus();
		return false;	
	}
	else
	{
		return true;
	}

	
}

/*'**************************************************************************************'
'* Function		- isoptSelected(cbo, msg)
'* Args.		- opt 	- The name of the radio button
				- msg	- A string which would be displayed in alert if MyString
'						  is empty one.
'* Return Value 	- It returns a boolean value.
'**************************************************************************************'*/
function iscboSelected(cbo,msg)
{
	if(cbo.selectedIndex == 0)
	{
			alert(msg)
			cbo.focus();
			return false;	
	}
	else
	{
		return true;
	}
	
}


/*'**************************************************************************************'
'* Function		- isNull(MyString, msg)
'* Args.		- MyString 	- A string which would be checked.
			- msg		- A string which would be displayed in alert if MyString
'					  is empty one.
'* Return Value 	- It returns a boolean value.
'**************************************************************************************'*/
function isNull(MyString, msg)
{
			var retflag=true;
			if (MyString.value == "")
			{
				alert(msg);
				MyString.select();
				MyString.focus();
				retflag=true;
				return(retflag);
			}
			else
			{
				var blank = true;
				var textlength = MyString.value.length;
				for (var i=0;i<textlength;i++)
				{
					var theChar = MyString.value.substring(i, i+1);
					if ((theChar < "0" || theChar > "9") && (theChar < "a" || theChar > "z") && (theChar < "A" || theChar > "Z"))
					{
						if (theChar != " ")
						{
							blank = false;
							break;
						}
					}
					else
					{
						blank = false;
					}
				}		
				if (blank)
				{
					alert(msg);
					MyString.select();
					MyString.focus();
					retflag = true;
					return(retflag) ;
				}
			
	}	
}
//**************************************************************************************

/*
'**************************************************************************************'
'* Function		- isNumeric(MyString, msg)
'* Args.		- MyString 	- A string which would be checked.
			- msg		- A string which would be displayed in alert if MyString
'					  contains characters other than Numeric.
'* Return Value 	- It returns a boolean value.
'**************************************************************************************'*/

function isNumeric(MyString,msg)
{
	if(MyString.value.charAt(0) == "-")
	{
		alert(msg)
		MyString.select();
		return false;
	}
	if(!(isNaN(MyString.value)))
	{
		return true;
	}
	else
	{
		alert(msg)
		MyString.select();
		return false;
	}
}

/*function isNumeric(MyString,msg)
{
	if (MyString.value == "")
	{
		alert(msg);
		MyString.focus();
		return false;
	}
	else
	{
		var goodqty = true;
		var qtylength = MyString.value.length;
		
		for (var i=0;i<qtylength;i++)
		{
			var theChar = MyString.value.substring(i, i+1);
			if (theChar < "0" || theChar > "9")
			{
				if (theChar != ".")
				{
					goodqty = false;
				}
			}
		}		
		if (goodqty == false)
		{
			alert(msg);
			MyString.focus();
			return false;
		}
	}
}*/
//**************************************************************************************


/*
'**************************************************************************************'
'* Function		- trim(field_name)
'* Args.		- field_name- A string which needs to get left trimmed.
'* Return Value 	- It returns a string after having left trimmed.
'**************************************************************************************'*/
function trim(field_name)
{
	pos = field_name.indexOf(" ")
	while (pos == 0) 
	{ 
		field_name = field_name.substring(1)
		pos = field_name.indexOf(" ")
	}
	
	len = field_name.length
    blank = field_name.charAt(len-1)
    while (blank == " " )
    {
      field_name = field_name.substring(0,len-1)
      len=field_name.length
      blank = field_name.charAt(len-1)
    }
    
	//rtrim(field_name)
	return field_name
}


/*
'**************************************************************************************'
'* Function		- isValidEmail(Email) 
'* Args.		- Email - A string which would be checked whether it can be worked as 
'						  an email address.
				- msg   - A string which is displayed if the given mail address is not a 
						  valid email address
'* Return Value 	- It returns a boolean value.
'**************************************************************************************'*/
function isValidEmail(Email,msg) 
{
    pos1 = Email.indexOf("@");
    len = Email.length;
    if ((pos1 <= 0 ) || (pos1 == (len - 1)))
    {
	alert(msg)
	return false;
    }
    else
    {
	    pos1 = Email.indexOf(".");
	    len = Email.length;
	    if ((pos1 <= 0 ) || (pos1 == (len - 1)))
	    {
      	 alert(msg);
      	 return false
	    }
    }
    return true;	
}
//**************************************************************************************

/*
'**************************************************************************************'
'* Function		- isValidDate(CDate)
'* Args.		- CDate - A string which would be checked whether it is a valid date or not.
				
'* Return Value 	- It returns a boolean value.
'**************************************************************************************'*/
function isValidDate(CDate)
{
	// Date's length should be 10 characters....
	if (CDate.length != 10)
	{
		return false;
	}

	// Making sure that user enters only digits and slash in the box.
	var ValidInputs = true;
	var InputLength = CDate.length;
		
	for (var i=0;i<InputLength;i++)
	{
		var theChar = CDate.substring(i, i+1);
		if (theChar < "0" || theChar > "9")
		{
			if (theChar != "/")
			{
				ValidInputs = false;
			}
		}
	}		
	if (ValidInputs == false)
	{
		return false;
	}

	// Date should contain 2 "/"s and it should be at the position of 3 and 6 only...
	if (CDate.substring(3,2) != "/" || CDate.substring(6,5) != "/")
	{
		return false;
	}
	
	//Taking Day, Month and Year into mem variables...
	var Day, Month, Year;
	Month = CDate.substring(2,0);
	Day = CDate.substring(5,3);
	Year = CDate.substring(10,6);

	// Month having value more than 12 is not valid one...
	if (Month > 12 || Day == 0 || Day == 00 || Month == 0 || Month == 00 || Year == 0000)
	{	
		return false;		
	}
	
	//Checking month and its days....
	if (Year%4 == 0)
	{
		if (Month == 2 && Day > 29)
		{
			return false;
		}
	}
	else
	{
		if ((Month == 2 && Day > 28))
		{
			return false;
		}
	}
	
	if ((Month == 1 || Month == 3 || Month == 5 || Month == 7 || Month == 8 || Month == 10 || Month == 12) && Day > 31)
	{
		return false;
	}
	
	if ((Month == 4 || Month == 6 || Month == 9 || Month == 11) && Day > 30)
	{
		return false;
	}
	
	if(isNaN(Month))
	{
		
		return false;
	}
	
	if(isNaN(Day))
	{
		
		return false;
	}
	
	if(isNaN(Year))
	{
		
		return false;
	}	
	
	return true;
}
//**************************************************************************************




/*'**************************************************************************************'
'* Function		- checkAll(main,sub)
'* Args.		- main	- The chkbox which when checked will check all the 
						  checkboxes. 	
			    - id - This should be the  chkbox id which will be checked.It should 
						  be a common name for all the checkboxes to be checked 
		
'* Return Value 	- It returns a boolean value.
'**************************************************************************************'*/
function checkAll(main,id)
{
	chk = document.all.item(id)
	if(main.checked == true)
	{
		for(i=0;i<chk.length;i++)
		{
			chk[i].checked = true
		}	
	}
	else
	{
		for(i=0;i<chk.length;i++)
		{
			chk[i].checked = false
		}	
	
	}

}

//**************************************************************************************
/*'**************************************************************************************'
'* Function		- checkAll(main,sub,msg)
'* Args.		- main	- The chkbox which when checked will check all the 
						  checkboxes. 	
			    - id   - The chkbox which will be checked.It should be a
						  common name for all the checkboxes to be checked 
			    - msg	- A string which would be displayed in alert if not even one 
						  checkbox is checked
'							
'* Return Value 	- It returns a boolean value.
'**************************************************************************************'*/
function isChecked(main,id,msg)
{
chk = document.all.item(id)
checked = false
	if(chk.length > 1)
	{
		for(i=0;i<chk.length;i++)
		{
			if(chk[i].checked == true)
			{
				checked = true
			}
		}
		
		if(!checked)
		{
			alert(msg)
			return false;
		
		} 
	
	}
	else
	{
		if(sub.checked == false)
		{
			alert(msg)
			sub.focus();
			return false;
		}
	}
	

}


function isNumericWithSpace(MyString,msg)
{
	if (MyString.value == "")
	{
		alert(msg);
		MyString.focus();
		return false;
	}
	else
	{
		var goodqty = true;
		var qtylength = MyString.value.length;
		
		for (var i=0;i<qtylength;i++)
		{
			var theChar = MyString.value.substring(i, i+1);
			if (theChar < "0" || theChar > "9")
			{
				if (theChar != " ")
				{
					goodqty = false;
				}
			}
		}		
		if (goodqty == false)
		{
			alert(msg);
			MyString.focus();
			return false;
		}
	}
	return true;
}


/*'**************************************************************************************'
'* Function		- charTextareaLimit(txtarea,NoOfChars)
'* Args.		- txtarea   - The name of the textarea
'*				- NoOfChars - No of characters that are allowed
				
'* Return Value 	- It returns a boolean value.
'**************************************************************************************'*/
function charTextareaLimit(txtarea,NoOfChars,msg)
{
	
	var intChar=NoOfChars;
	if(txtarea > intChar)
	{
		alert(msg);
		return false;	
	}
	return true;
}


/*'**************************************************************************************'
'* Function		- isValidFilenameAndExtension(fileName,fileType)
'* Args.		- fileName   - The name of the File
'*				- fileType   - Type of the File ie whether it is Audio/video/Picture/normal 
'								ie any extension can be accepted.
				
'* Return Value - It returns a boolean value.
'**************************************************************************************'*/
function isValidFilenameAndExtension(fileName,fileType)
{

	var passedFileName=fileName
	
	//Checks whether the filename passed is not equal to null
	if(passedFileName != "")
		{
			//Length of the filename taken in a variable
			var len =passedFileName.length
			
			var OriginalFilename=passedFileName ;
			var filename=passedFileName.substring(len,len-12);
			if((len) > (filename.length) )
			{ 
				var lenCheckFilename=OriginalFilename.substring(OriginalFilename.lastIndexOf("\\")+1,len);
				if ((lenCheckFilename.length)>12 )
				{
					alert("Filename should not exceed 12 characters i.e. 8 characters filename , . , and 3 characters extension.")
					return false;
				}
			}	
			var spaceCheckFilename=filename.indexOf(" ");
			if (spaceCheckFilename!= -1)
			{
				alert("Filename should not contain any space .");
				return false;
			}	
			
			var FileType=fileType
			FileType=FileType.toUpperCase();
			
			extension = passedFileName.substring(len,len-4);
			extension = extension.toUpperCase();
			
			if(FileType=="AUDIO")
			{
				if(extension != ".ASF" && extension != ".MP3" && extension!=".WAV")
				{
					alert("Invalid audio file. Proper Audio File should be selected.")
			        return false;
				}
			}
			else if(FileType=="PICTURE")
			{
				if(extension != ".GIF" && extension != ".JPG")
				{
					alert("Only .gif or .jpg file formats are allowed.")
			        return false;
				}
			}
			else if(FileType=="VIDEO")
			{
				if(extension != ".ASF" && extension != ".MP3" && extension!=".WAV")
				{
					alert("Invalid video file. Proper Video File should be selected.")
			        return false;
				}
			}
			else if(FileType=="PDF")
			{
				if(extension != ".PDF")
				{
					alert("Invalid pdf file. Proper pdf File should be selected.")
			        return false;
				}
			}	
			else if(FileType=="PPT")
			{
				if(extension != ".PPT")
				{
					alert("Invalid ppt file.Proper ppt File should be selected.")
			        return false
				}
			}	
			else if(FileType=="ZIP")
			{
				if(extension != ".ZIP")
				{
					alert("Invalid zip file.Proper zip File should be selected.")
			        return false
				}
			}	
			else if(FileType=="ANY")
			{
				return true;
			}	
	}	
	return true;
}	
