function FindPos(Str,SearchStr)
{
	len = Str.length;
	for(i=0;i < len;++i)
	{
		if(Str.charAt(i) == SearchStr)
		{
			return i;
		}
	}
	return -1;
}

function ReplaceSpace(Str)
{
	var			RetStr;
	var Pos	= FindPos(Str," ");
	while(Pos != -1)
	{
		
		var RetStr	= Str.substring(0,Pos);
		Str	= Str.substring(Pos + 1,Str.length);
		Str=RetStr.concat("+") + Str;
		Pos	= FindPos(Str," ");
	}
	return Str;
}

function ReverseFind(Str,SearchStr)
{
	len = Str.length;
	for(i=len;i >= 0;i--)
	{
		if(Str.charAt(i) == SearchStr)
		{
			return i;
		}
	}
	return -1;
}

function TrimLeft(val)
{
	var len = val.length;
	if(len != 0)
	{
		while(1)
		{
			if(val.charAt(0) != " ")
			{
				return val;
			}
			else
			{
				val = val.substr(1);
			}
		}
	}
	return val;
}

function TrimRight(val)
{
	var len = val.length;
	if(len != 0)
	{
		while(1)
		{
			if(val.charAt(len - 1) != " ")
			{
				return val;
			}
			else
			{
				len -= 1;
				val = val.substr(0,len);
			}
		}
	}
	return val;
}

function ValidateName(Name)
{
	if(FindPos(Name,"/") != -1)
	{
		return 0;
	}
	else if(FindPos(Name,"@") != -1)
	{
		return 0;
	}
	else if(FindPos(Name,"!") != -1)
	{
		return 0;
	}
	else if(FindPos(Name,"&") != -1)
	{
		return 0;
	}
	else if(FindPos(Name,"*") != -1)
	{
		return 0;
	}
	else if(FindPos(Name,"?") != -1)
	{
		return 0;
	}
	else if(FindPos(Name,"\"") != -1)
	{
		return 0;
	}
	else if(FindPos(Name,"<") != -1)
	{
		return 0;
	}
	else if(FindPos(Name,">") != -1)
	{
		return 0;
	}
	else if(FindPos(Name,"|") != -1)
	{
		return 0;
	}
	else if(FindPos(Name,"#") != -1)
	{
		return 0;
	}
	else if(FindPos(Name,"%") != -1)
	{
		return 0;
	}
	else if(FindPos(Name,"'") != -1)
	{
		return 0;
	}
	else if(FindPos(Name,"\\") != -1)
	{
		return 0;
	}
	return 1;		
}

function ValidateKeywords(Name)
{
	var Message	= "Invalid Name : { / , @ , ! , \\ , & , : , * , ? , \" , < , > , | , # , % , '} symbols are not allowed.";

	if(FindPos(Name,"\\") != -1)
	{
		alert(Message);
		return 0;
	}
	else if(FindPos(Name,":") != -1)
	{
		alert(Message);
		return 0;
	}
	else if (!ValidateName(Name))
	{
		alert(Message);
		return 0;
	}
	
	var len = Name.length;
	if (len != 0)
	{
		while(1)
		{
			pos = FindPos(Name, ",");
			if (pos != -1)
			{
				str = Name.substr(0, pos);
				if (str.length > 32)
				{
					alert("The Keyword \"" + str + "\" length excedeed the max. 32 characters");
					return 0;
				}
				Name = Name.substr(pos + 1);
			}
			else
			{
				str = Name;
				if (str.length > 32)
				{
					alert("The Keyword \"" + str + "\" length excedeed the max. 32 characters");
					return 0;
				}
				break;
			}
		}

	}

	return 1;
}

function ValidateDate(val)
{
	var Pos = FindPos(val, "/");
	if(Pos == -1)
	{
		return 0;
	}
	
	var Month	= val.substring(0,Pos);
	if(!IsNumericVal(Month))
	{
		return 0;

	}
	
	val			= val.substring(Pos + 1,val.length);
	
	Pos = FindPos(val, "/");
	if(Pos == -1)
	{
		return 0;
	}
	
	var Day		= val.substring(0,Pos);
	if(!IsNumericVal(Day))
	{
		return 0;
	}
	var Year	= val.substring(Pos + 1,val.length);
	if(Year.length != 4)
	{
		return 0;
	}
	if(!IsNumericVal(Year))
	{
		return 0;
	}
	
	if(Year < 1)
	{
		return 0;
	}

	// Validate For Leap Year
	
	switch(parseInt(Month,10))
	{
		case 1 :
		case 3 :
		case 5 :
		case 7 :
		case 8 :
		case 10:
		case 12:
			if(Day > 31 || Day < 1)
			{
				return 0;
			}
			break;

		case 4 :
		case 6 :
		case 9 :
		case 11:
			if(Day > 30 || Day < 1)
			{
				return 0;
			}
			break;
		
		case 2 :
			if((Year % 100) == 0)
			{
				Year = parseInt(Year / 100);
			}

			if((Year % 4) == 0)
			{
				if(Day > 29 || Day < 1)
				{
					return 0;
				}
			}
			else
			{
				if(Day > 28 || Day < 1)
				{
					return 0;
				}
			}
			break;
		default :
			return 0;
	}
	return 1;
}

function ValidateNameforSearch(Name)
{
	if(FindPos(Name,"/") != -1)
	{
		return 0;
	}
	if(FindPos(Name,"@") != -1)
	{
		return 0;
	}
	if(FindPos(Name,"!") != -1)
	{
		return 0;
	}
	if(FindPos(Name,"&") != -1)
	{
		return 0;
	}
	if(FindPos(Name,"?") != -1)
	{
		return 0;
	}
	if(FindPos(Name,"\"") != -1)
	{
		return 0;
	}
	if(FindPos(Name,"<") != -1)
	{
		return 0;
	}
	if(FindPos(Name,">") != -1)
	{
		return 0;
	}
	if(FindPos(Name,"|") != -1)
	{
		return 0;
	}
	if(FindPos(Name,"#") != -1)
	{
		return 0;
	}
	if(FindPos(Name,"%") != -1)
	{
		return 0;
	}
	if(FindPos(Name,"'") != -1)
	{
		return 0;
	}
	return 1;		
}

function IsNumericVal(val)
{
	if(isNaN(val))
	{
		return 0;
	}
	if(IsFloatVal(val))
	{
		return 0;
	}
	return 1;
}

function IsFloatVal(val)
{
	var len = val.length;
	for(i=0; i<=(len - 1);i++)
	{
		if(val.charAt(i) == ".")
		{
			return 1;
		}

	}
	return 0;
}
function CompareDates(strFromDate,strToDate) 
{
	var tempFrom,tempTo;
	var nPos;

	nPos        = ReverseFind(strFromDate,"/");
	tempFrom    = parseInt(strFromDate.substring(nPos+1,strFromDate.length));
	strFromDate = strFromDate.substring(0,nPos);
	
	nPos        = ReverseFind(strToDate,"/");
	tempTo      = parseInt(strToDate.substring(nPos+1,strToDate.length));
	strToDate   = strToDate.substring(0,nPos);
	
	if(tempTo > tempFrom)
	{
		return 1;
	}
	else if(tempFrom > tempTo)
	{
		return 0;
	}
	else
	{
		nPos        = FindPos(strFromDate,"/");
		tempFrom    = parseInt(strFromDate.substring(0,nPos));
		strFromDate = strFromDate.substring(nPos+1,strFromDate.length);
		
		nPos        = FindPos(strToDate,"/");
		tempTo      = parseInt(strToDate.substring(0,nPos));
		strToDate   = strToDate.substring(nPos+1,strToDate.length);
		
		if(tempTo > tempFrom)
		{
			return 1;
		}
		else if(tempFrom > tempTo)
		{
			return 0;
		}
		else
		{
			tempFrom = parseInt(strFromDate);
			tempTo   = parseInt(strToDate);
				
			if(tempTo < tempFrom)
			{
				return 0;
			}
		}
	}
	return 1;
}

function isEmail(Mail){
	var mailLength=0;	
	var atIndex=-1;
	var lastDotIndex=-1;

	Mail = TrimLeft(TrimRight(Mail));
	
	if (Mail=="")
	{
		alert("E-mail Can't be blank,please enter a valid Email Id");
		return false;
	}
		
	mailLength=Mail.length;

	for(i=0; i < mailLength ; i++)
	{
		
		ch=Mail.substring(i,i+1);
		if (!isValid(ch))
		{
			alert("Please Enter a valid Email Id");
			return false;
		}


		if(ch=="@")
		{
			if(atIndex >= 0)
			{
				alert("Please enter a valid Email Id \n\n -invalid no of @");
				return false;
			}
			else
			{
				atIndex=i+1;
				if((atIndex==1)||(atIndex==mailLength)||(atIndex==lastDotIndex+1))
				{
					alert("Please Enter a valid Email Id \n\n -invalid position of @");
					return false;
				}
			}
		}	

		if(ch==".")
		{
			if((i==lastDotIndex)||(i==1)||(i==mailLength-1)||(i==atIndex))
			{
				alert("Please Enter a valid Email Id \n\n -invalid position of .");
				return false;
			}
			lastDotIndex=i+1;
		}
	
	}	// end for	


	if((atIndex==-1)||(lastDotIndex < atIndex)){
		alert("Please Enter a valid Email Id");
		return false;
	}
	return true;
}
	



function isValid(ch){
	if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z')||(ch>='0' && ch<='9')||ch=='@'||ch=='-'||ch=='_'||ch=='.')
		return true;
	else
		return false;
}

function getIcon(Ext,CheckOutStatus)
{
	Ext = Ext.toLowerCase();
	
	if(Ext == "img")
	{
		if(CheckOutStatus == 'Y')
		{
			document.write('<img src="/paperfly/filemanager/icons/image_checkedout.gif" alt="Image Document" WIDTH="17" HEIGHT="17">');
		}
		else
		{
			document.write('<img src="/paperfly/filemanager/icons/image.gif" alt="Image Document" WIDTH="17" HEIGHT="17">');
		}
	}
	else if(Ext == "xls")
	{
		if(CheckOutStatus == 'Y')
		{
			document.write('<img src="/paperfly/filemanager/icons/excel_checkedout.gif" alt="Excel Document" WIDTH="17" HEIGHT="17">');
		}
		else
		{
			document.write('<img src="/paperfly/filemanager/icons/excel.gif" alt="Excel Document" WIDTH="17" HEIGHT="17">');
		}
	}
	else if(Ext == "doc" || Ext == "rtf" || Ext == "dot")
	{
		if(CheckOutStatus == 'Y')
		{
			document.write('<img src="/paperfly/filemanager/icons/word_checkedout.gif" alt="Word Document" WIDTH="17" HEIGHT="17">');
		}
		else
		{
			document.write('<img src="/paperfly/filemanager/icons/word.gif" alt="Word Document" WIDTH="17" HEIGHT="17">');
		}
	}
	else if(Ext == "pdf")
	{
		if(CheckOutStatus == 'Y')
		{
			document.write('<img src="/paperfly/filemanager/icons/pdf_checkedout.gif" alt="Acrobat Document" WIDTH="17" HEIGHT="17">');
		}
		else
		{
			document.write('<img src="/paperfly/filemanager/icons/pdf.gif" alt="Acrobat Document" WIDTH="17" HEIGHT="17">');
		}
	}
	else if(Ext == "ppt")
	{
		if(CheckOutStatus == 'Y')
		{
			document.write('<img src="/paperfly/filemanager/icons/ppt_checkedout.gif" alt="Power Point Presentation" WIDTH="17" HEIGHT="17">');
		}
		else
		{
			document.write('<img src="/paperfly/filemanager/icons/ppt.gif" alt="Power Point Presentation" WIDTH="17" HEIGHT="17">');
		}
	}
	else
	{
		if(CheckOutStatus == 'Y')
		{
			document.write('<img src="/paperfly/filemanager/icons/file_checkedout.gif" alt="Document" WIDTH="17" HEIGHT="17">');
		}
		else
		{
			document.write('<img src="/paperfly/filemanager/icons/file.gif" alt="Document" WIDTH="17" HEIGHT="17">');
		}
	}
}

function getCurrentDateTime()
{
	var currDate = new Date();
	var strDay, strMonth;

	switch(parseInt(currDate.getDay()))
	{
		case 0:
			strDay = "Sunday";
			break;
		case 1:
			strDay = "Monday";
			break;
		case 2:
			strDay = "Tuesday";
			break;
		case 3:
			strDay = "Wednesday";
			break;
		case 4:
			strDay = "Thursday";
			break;
		case 5:
			strDay = "Friday";
			break;
		case 6:
			strDay = "Saturday";
			break;
	}

	switch(parseInt(currDate.getMonth()))
	{
		case 0:
			strMonth = "January";
			break;
		case 1:
			strMonth = "February";
			break;
		case 2:
			strMonth = "March";
			break;
		case 3:
			strMonth = "April";
			break;
		case 4:
			strMonth = "May";
			break;
		case 5:
			strMonth = "June";
			break;
		case 6:
			strMonth = "July";
			break;
		case 7:
			strMonth = "August";
			break;
		case 8:
			strMonth = "September";
			break;
		case 9:
			strMonth = "October";
			break;
		case 10:
			strMonth = "November";
			break;
		case 11:
			strMonth = "December";
			break;
	}

	if(parseInt(currDate.getHours()) < 12)
	{
		var strTime = currDate.getHours() + ":" + currDate.getMinutes() + " AM";
	}
	else
	{
		var strTime = (currDate.getHours() - 12).toString() + ":" + currDate.getMinutes() + " PM";
	}

	var strDate  = strDay + ", " + strMonth + " " + currDate.getDate() + ", " + currDate.getFullYear() + " / " + strTime;

	return strDate;

}