//returns true if numeric
function func_isnumeric(par_text)
{
 	if(!isNaN(par_text))
	{
		return true;
	}
	else
	{
		return false;
	}
}

//returns true if alphabetic
function func_isalphabetic(par_text)
{
 	var text=new String(par_text);
	var ilen;
	var bflag=true;
	for(ilen=0;ilen<=text.length;ilen++)
	{
		if(text.charCodeAt(ilen) < 65 || text.charCodeAt(ilen) > 90 && text.charCodeAt(ilen) < 97 || text.charCodeAt(ilen) > 122)
		{
			bflag=false;
		}
	}
	return bflag;
}

//returns true if alphanumeric
function func_isalphanumeric(par_text)
{
 	var str = new String(par_text);
	for (var i=0;i<str.length;i++)
	{
		if (!((str.charCodeAt(i)>=65 && str.charCodeAt(i)<=90) || (str.charCodeAt(i)>=97 && str.charCodeAt(i)<=122) || (str.charCodeAt(i)>=48 && str.charCodeAt(i)<=57)))
		{
			return false; 
		}
	 }
	return true;	 
} 

// returns true if length is gearter than given value.
function func_isMaxLength(par_text,par_length)
{
	var strlen;
	var text=new String(par_text);
	strlen = func_len(text);
	if (strlen > par_length)
	{
		return true;
	}
	else
	{
		return false;
	}
}

// returns true if length is less than given value.
 function func_isMinLength(par_text,par_length)
{
	var strlen;
	var text=new String(par_text);
	strlen = func_len(text);
	if (strlen < par_length)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//returns true if null
function func_isNull(par_text)
{
	if((func_trim(par_text) == "") || (par_text == null))
	{
		return true;
	}
	else
	{
		return false;
	}
}

//returns true if it is valid date.
function func_isDate(date,month,year)
{ 
	var bflag=true;
	if (isNaN(year) || isNaN(month) || isNaN(date))
	{
		bflag=false;
	}
	if(bflag)
	{
		var dtnew = new Date(year,month-1,date);
		if ((parseInt(dtnew.getFullYear()) == year) && (parseInt(dtnew.getMonth()) == (month-1)) && (parseInt(dtnew.getDate()) == date))
		{
			bflag=true;
		}
		else
		{
			bflag=false;
		}
	}
	return bflag;
}

// returns true if valid Email.
function func_isEmail(emailval)
{
	var tempStr,icount;
	var blnmail,blnperiod; 
	var lastoccofperiod,maxthree;
	var ampicount=0;
	var amppos;
	var servername = 1;
	var dots;
	icount=emailval.length;
	blnperiod = 1;
	maxthree = 1;
	specialchar = 0
	lastoccofperiod = 0;
	if (icount==0)
	{
		return true;
	}
	for(i=0;i<icount;i++)
	{
		tempStr = emailval.charAt(i);
		if ((tempStr >='a')&&(tempStr <='z'))
		{
			specialchar=specialchar+1;
		}
		else
		{
			if ((tempStr >='A')&&(tempStr <='Z'))
			{
				specialchar=specialchar+1;
			}
			else
			{
				if ((tempStr >= 0)&&(tempStr<=9))
				{
					specialchar=specialchar+1;
				}
				else
				{
					if ((tempStr=='_')||(tempStr=='-')||(tempStr=='.')||(tempStr=='@'))
					{
						specialchar=specialchar+1;
					}
					else
					{
						return false;
					}
				}
			}
		}
	}
	dots = emailval.indexOf('..');
	if (dots != -1)
	{
		return false;
	}
	espace = emailval.indexOf(' ');
	if (espace != -1)
	{
		return false;
	}
	lastoccofperiod = emailval.lastIndexOf('.');
	if (lastoccofperiod <= 0)
	{
		blnperiod = 0;
	}
	if (((icount - lastoccofperiod) > 5)||((icount - lastoccofperiod) < 3))
	{
		maxthree = 0;
	}
	 for(i=0;i<=icount;i++)
	{
		tempStr = emailval.charAt(i)
		if (tempStr=='@')
		ampicount=ampicount + 1;
	}
	amppos = emailval.indexOf('@');
	if (emailval.charAt(amppos+1) == '.')
	servername = 0;
	if(icount - emailval.charAt(amppos)< 5) 
	servername = 0;
	if ((ampicount==1)&&(blnperiod==1)&&(maxthree==1)&&(servername==1))
	{
		blnmail=1;
	}
	else
	{
		 blnmail=0;
	}
	 //return blnmail;
	if (blnmail==0)
	{
		//alert('Please enter a valid email address');
		return false;
	}
	else
	{
		return true;
	}
 }

function func_isValidURL(strText)
{
	var bcorrect=true;
	if(bcorrect && func_isNull(strText))
	{
		return true;
	}
	if(bcorrect && strText.indexOf("/")!=-1)
	{
		strText=strText.substring(0,strText.indexOf("/"));
	}
	
	var len = strText.length;
	
	//validate the characters allowed
	if(bcorrect)
	{	
		var i = 0;
		for (i=0; i<len; i++)
		{
			if (!((strText.charCodeAt(i) >= 65 && strText.charCodeAt(i) <= 90)
			  || (strText.charCodeAt(i) >= 97 && strText.charCodeAt(i) <= 122)
			  || (strText.charCodeAt(i) >= 48 && strText.charCodeAt(i) <= 57)
			  || (strText.charAt(i) == "-")
			  || (strText.charAt(i) == "_")
			  || (strText.charAt(i) == ".")))
			{
				bcorrect=false;
			}
		}
	}
	
	if(bcorrect && strText.indexOf("..")!=-1)
		bcorrect=false;
	if(bcorrect && strText.indexOf("-")==0)
		bcorrect=false;
	if(bcorrect && strText.indexOf("_")==0)
		bcorrect=false;
	if(bcorrect && strText.indexOf(".")==0)
		bcorrect=false;
	if(bcorrect && strText.indexOf(".")==-1)
		bcorrect=false;
	
	//validate last index of '.' and number of chars next to that
	if(bcorrect)
	{
		var iLastPos=strText.lastIndexOf('.');
		var iMinChar=len - iLastPos;
		if(iMinChar<3 || iMinChar>5)
			bcorrect=false;
	}
	
	//validate extension
	if(bcorrect)
	{
		var strExtension=strText.substring(strText.lastIndexOf('.')+1);
		if(!func_isalphanumeric(strExtension))
			bcorrect=false;
	}
	return bcorrect;
}

//Checks whether file type is ends with specified extension or not
function isFile(strValue,strFileType)
{
	var bCorrect=false;
	strImage=new String(strValue);
	var bHtml=false;
	var bAll=false;
	var bImage=false;
	if(strValue.length==0)
	{
		return true;
	}
	if(strFileType.toLowerCase()=="htm" || strFileType.toLowerCase()=="html")
	{
		bHtml=true;
	}
	if(strFileType.toLowerCase()=="all")
	{
		bAll=true;
	}
	if(strFileType.toLowerCase()=="img")
	{
		bImage=true;
	}
	if(strImage.indexOf(".")!=-1)
	{
		iIndex=strImage.indexOf(".");
		if(bAll==true)
		{
			strFile=strImage.substring(iIndex+1);
			if(strFile.length>=2 && strFile.length<250)
			{
				bCorrect=true;
				return bCorrect;
				
			}
			else
			{
				bCorrect=false;
				return bCorrect;
				
			}
		}
		else	if(bImage==true)
		{
			if((strImage.substring(iIndex+1)).toLowerCase()=="gif" || (strImage.substring(iIndex+1)).toLowerCase()=="jpg" || (strImage.substring(iIndex+1)).toLowerCase()=="jpeg" || (strImage.substring(iIndex+1)).toLowerCase()=="bmp")
			{
				bCorrect=true;
				return bCorrect;
				
			}
			else
			{
				bCorrect=false;
				return bCorrect;
				
			}
		}
		else	if(bHtml==true)
		{
			if((strImage.substring(iIndex+1)).toLowerCase()=="htm" || (strImage.substring(iIndex+1)).toLowerCase()=="html")
			{
				bCorrect=true;
				return bCorrect;
				
			}
			else
			{
				bCorrect=false;
				return bCorrect;
				
			}
		}
		else
		{
			if((strImage.substring(iIndex+1)).toLowerCase()==strFileType.toLowerCase())
			{
				bCorrect=true;
				return bCorrect;
				
			}
			else
			{
				bCorrect=false;
				return bCorrect;
				
			}
		}
	}
	else
	{
		bCorrect=false;
		return bCorrect;
	}
}








// customised functions
// returns the length of string
function func_len(par_text)
{
	var strText=new String(par_text);
	return strText.length;
}

//returns the trimmed string
function func_trim(par_text)
{
	var stext = new String(par_text);
	var sresult = "";
	//Remove leading spaces
	for (var i=0; i<stext.length; i++)
	{
		if (stext.charAt(i) != " ")
		{
			sresult = stext.substr(i, (stext.length - i));
			break;
		}
	}
	stext = sresult;
	//Remove trailing spaces
	for (var j=(stext.length - 1); j>=0; j--)
	{
		if (stext.charAt(j) != " ")
		{
			sresult = stext.substr(0, (j + 1));
			break;
		}
	}
	return sresult;
}

// selecting & focusing on the element that holds incorrect value
function func_selectFocus(element)
{
	element.select();
	element.focus();
	return true;
}

// HTML Validations
function validate_check_box(obj_form,str_prefix)
{
 	var b_return = true;
	for(i=0;i<obj_form.elements.length;i++)
	{
	 	obj_element = obj_form.elements[i];
		if(obj_element.type == "checkbox" && obj_element.name.indexOf(str_prefix)!=-1)
		{
		 	if(obj_element.checked == true)
			{
			  b_return = false;
			  return b_return;	
			}
		}	
	 }
	 return(b_return);
}

function validate_dropdownlist(obj_listelement)
{
 		 var b_return = true;
		 if(obj_listelement.options[obj_listelement.selectedIndex].value != "")
		 {
		  	  b_return = false;
		 	  return b_return;
		 }		
		 return b_return;
}

function validate_textbox(str_Name,obj_element,b_numeric,b_alphabetic,b_alphanumeric,b_zerolength,i_max_char,i_min_char,str_email,str_url,str_file,str_filetype)
{
 		var b_return = true;
		var str_value=obj_element.value;
		if (b_return && numeric) 
		{
		   alert("Numeric");
		func_selectFocus(obj_element);
		   
		}
		
		return b_return;
}

//returns true if a radio button is checked
function func_IsRadioChecked(objForm,strName)
{
	var bcorrect;
	bcorrect=false;
	for(i=0; i<objForm.elements.length; i++)
	{
		var name=objForm.elements[i].name;
		var type=objForm.elements[i].type;
		if((type=="radio") && (name.indexOf(strName)!=-1))
		{
			var val=objForm.elements[i].checked;
			if(val==true)
			{
				bcorrect=true;
				break;
			}
		}
	}	
	return bcorrect;
}

//returns true if atleast one check box is selected
function func_IsCheckboxSelected(objForm,strName)
{
	var bcorrect=false;
	for(var i=0; i<objForm.elements.length; i++)
	{
		var name=objForm.elements[i].name;
		var type=objForm.elements[i].type;
		if((type=="checkbox") && (name.indexOf(strName)!=-1))
		{
			var val=objForm.elements[i].checked;
			if(val==true)
			{
				bcorrect = true;
				break;
			}
		}
	}
	return bcorrect;
}

function func_add_tolistbox(obj_form,obj_text_element,obj_option_element)
{
 	str_value = obj_text_element.value;
	if(str_value == "")
	{
	 	alert("Please enter value");
	}
	else
	{
		obj_option_element.options[obj_option_element.length] = new Option(str_value,str_value);			
	}
}

function func_get_date_diff(str_fromDate,str_fromHour,str_fromMin,str_fromSec,str_toDate,str_toHour,str_toMin,str_toSec)
{
	str_fromDay = func_get_day(str_fromDate);
	str_fromMonth = func_get_month(str_fromDate);
	str_fromYear = func_get_year(str_fromDate);

	str_toDay = func_get_day(str_toDate);
	str_toMonth = func_get_month(str_toDate);
	str_toYear = func_get_year(str_toDate);

	var fromDate = new Date(str_fromYear,str_fromMonth-1,str_fromDay,str_fromHour,str_fromMin,str_fromSec);
	var toDate = new Date(str_toYear,str_toMonth-1,str_toDay,str_toHour,str_toMin,str_toSec);
	
	return ( toDate.getTime()-fromDate.getTime() );
}

function func_get_day(str_date_string)
{
	var b_return_day = "0";
	if(func_len(str_date_string)==10)
	{
		str_current_day = str_date_string.substring(0,2);
		b_return_day = str_current_day;
	}
	return(b_return_day);
}
function func_get_month(str_date_string)
{
	var b_return_month = "0";
	if(func_len(str_date_string)==10)
	{
		str_current_month = str_date_string.substring(3,5);
		b_return_month = str_current_month;
	}
	return(b_return_month);
}
function func_get_year(str_date_string)
{
	var b_return_year = "0";
	if(func_len(str_date_string)==10)
	{
		str_current_year = str_date_string.substring(6,10);
		b_return_year = str_current_year;
	}
	return(b_return_year);
}
