//global variable for error flag
var errfound = false;
var countdot=0;
//function to validate by length
	function ValidLength(item, len) 
		{
	return (item.length >= len);
	}

				
//function to validate an email address
	function ValidEmail(item) 
	{
	if (!ValidLength(item, 5)) return false;
		if (item.indexOf ('@', 0) == -1) return false;
		if (item.indexOf ('"', 0) != -1) return false;
		if (item.indexOf ('\'', 0) != -1) return false;
		if (item.indexOf ('.', 0) == -1) return false;
		return true;
	}

// display an error alert
	function error(elem, text) 
	{
	// abort if we already found an error
		//if (errfound) return;
		window.alert(text);
		elem.select();
		elem.focus();
		//errfound = true;
	}

//trim lefthand spaces
	function LTrim (s)
	{
	while(''+s.charAt(0)==' ') 
	{s = s.substring(1,s.len);}
	return s;
	}

//trim righthand spaces
	function RTrim (s)
	{
		if (s.len>1)
		{
		while(''+s.charAt(s.len-1)==' ')
		{s= s.substring(0,s.len-1);}
		return s;
		}
	}


//trim spaces
	function trim (s)
	{
	s.value=LTrim(s);
	s.value=RTrim(s);
	s=clear(s);
	return s;
	}
	
//function to trim string
	function trimString (str) {
	  while (str.charAt(0) == ' ')
	    str = str.substring(1);
	  while (str.charAt(str.length - 1) == ' ')
	    str = str.substring(0, str.length - 1);
	  return str;
	}


	function clear(item)
	{
	if (item.value=="undefined")
	{item.value="";}
	return item;
	}

// to get the select box value
	function getVal(elem)
	{
		index = elem.selectedIndex;
		val = elem.options[index].value;
		return val;
	}

// to get the select box text
	function getText(elem)
	{
		index = elem.selectedIndex;
		val = elem.options[index].text;
		return val;
	}

// to set the select box
	function setCombo(elem,def) 
	{

		val = def;
		for (i=0;i<elem.length;i++)
		{
			if (elem.options[i].value == val)
			{
				elem.selectedIndex = i;
				break;
			}
		}
	}

// to get the radio box value
	function setRadio(elem,def) 
	{
		val = def;
		if (elem)
		{	

			if (!(elem.length))
			{

				elem.checked = true;	
			}
			else
			{

				for (i=0;i<elem.length;i++)
				{

					if (elem[i].value == val)
					{

						elem[i].checked = true;
						break;
					}
				}
			}	
		}
	}	

//function checking 0 or any number
function IsNumberZeroOne(theInput)
{
	if	(theInput < 0)
	{
	return false;
	}
	else if (theInput == "")
	{
		return false;
	}
	var theLength = theInput.length;

	for (var i = 0; i <= (theLength-1); i++)
	{
		var theChar = theInput.substring(i,i+1);
		if (theChar < "0" || theChar > "9")
		{
			//alert (" Numeric values allowed in this Field");
			return false;
		}
	}
	return true;
}	
// function Checking Integer
function IsNumber(theInput)
{
	if	(theInput < 1)
	{
	return false;
	}
	else if (theInput == "")
	{
		return false;
	}
	var theLength = theInput.length;

	for (var i = 0; i <= (theLength-1); i++)
	{
		var theChar = theInput.substring(i,i+1);
		if (theChar < "0" || theChar > "9")
		{
			//alert (" Numeric values allowed in this Field");
			return false;
		}
	}
	return true;
}
function IsNumberOne(theInput)
{
	if	(theInput < 1)
	{
	return false;
	}
	else if (theInput == "")
	{
		return false;
	}
	var theLength = theInput.length;

	for (var i = 0; i <= (theLength-1); i++)
	{
		var theChar = theInput.substring(i,i+1);
		if (theChar < 0 || theChar > 9)
		{
			//alert (" Numeric values allowed in this Field");
			return false;
		}
	}
	return true;
}
//function to open popup window with specified size
function jPopup(sJName, sJURL, sJWidth, sJHeight, sJToolbar)
{
	if (trim(sJToolbar) == "")
	{
		sJToolbar = "no";
	}
	sJName = window.open(sJURL, sJName, 'toolbar=' + sJToolbar + ',status=no,width=' + sJWidth +',height=' + sJHeight + ",resizable=yes");
}

//function to open popup window with specified size
function jPopupScroll(sJName, sJURL, sJWidth, sJHeight, sJToolbar)
{
	if (trim(sJToolbar) == "")
	{
		sJToolbar = "no";
	}
	sJName = window.open(sJURL, sJName, 'toolbar=' + sJToolbar + ',status=no,scrollbars=yes,width=' + sJWidth +',height=' + sJHeight + ",resizable=yes");
}

//Function for Selecting values in Select Box
function selval(sellist,selvalue)
{
	for(xx=0;xx<sellist.options.length;xx++)
	{
		if (sellist.options[xx].value == selvalue)
		{
			sellist.selectedIndex = xx;
			return;
		}
	}

	return;
}

//function to check valid email id
function checkmailadd(s)
{
	var i;
	var found1=false;
	var found2=false;
	var count1=0;
	for (i = 0; i < s.length; i++)
	    {   
			
      	  
      	  // Check that current character isn't whitespace.
	      if(s.charAt(i)== ' ') {found1=true;found2=true;count1=2;}
		  if(s.charAt(i)== '@') {found1=true;count1++;}
      	  if(s.charAt(i)== '.'){ found2=true; }
	   }
	if(found1==true && found2==true)
	{	
		if(count1 > 1)
			return false;
		else
		    return true;
		}
	else
		return false;
return true;
}


//function to Check the File Extensions
	function ValidExtension(item) 
	{
		if (item.indexOf ('.jpg', 0)>0 || item.indexOf ('.JPG', 0)>0)
		{
		 return true;
		}
		else if (item.indexOf ('.jpeg', 0) >0 || item.indexOf ('.JPEG', 0) >0)
		{
		 return true;
		}
		else if (item.indexOf ('.gif', 0) >0 || item.indexOf ('.GIF', 0) >0)
		{
		 return true;
		}

		else
		{
		return false;
		}
	}
	
// function for checking the US phone format
function checkphoneformat(s) 
{
	if(s.length<12)
	{
		
		return false;
	}
	if ((s.charAt(3)!=" ")||(s.charAt(7)!=" "))
	{
		
		return false;
	}
	if ( isNaN(s.charAt(0))==true || isNaN(s.charAt(1))==true || isNaN(s.charAt(2))==true || isNaN(s.charAt(4))==true || isNaN(s.charAt(5))==true || isNaN(s.charAt(6))==true || isNaN(s.charAt(8))==true || isNaN(s.charAt(9))==true || isNaN(s.charAt(10))==true || isNaN(s.charAt(11))==true )
	{
		
		return false;
	}
	return true;
}

// ******************************************************************
// This function accepts a string variable and verifies if it is a
// proper date or not. It validates format matching either
// mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
// has the proper number of days, based on which month it is.

// The function returns true if a valid date, false if not.
// ******************************************************************

	function isDate(dateStr) 
	{
	    //var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	    var datePat = /^(\d{1,2})(\/|)(\d{1,2})(\/|)(\d{4})$/;
	    var matchArray = dateStr.match(datePat); // is the format ok?
		months= new Array(12);
		months[0]="Jan";
		months[1]="Feb";
		months[2]="Mar";
		months[3]="Apr";
		months[4]="May";
		months[5]="Jun";
		months[6]="Jul";
		months[7]="Aug";
		months[8]="Sep";
		months[9]="Oct";
		months[10]="Nov";
		months[11]="Dec";	
	    if (matchArray == null) 
	    {
		alert("Please enter date as either dd/mm/yyyy");
		return false;
	    }

	     // parse date into variables
	    //day = matchArray[3];
	    //month = matchArray[1];
	    //year = matchArray[5];
		day = matchArray[1];
	    month = matchArray[3];
	    year = matchArray[5];
	    if (day < 1 || day > 31) 
	    {
		alert("Day must be between 1 and 31.");
		return false;
	    }
		if (month < 1 || month > 12) // check month range
	    { 
		alert("Month must be between 1 and 12.");
		return false;
	    }
	    if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	    {
		alert("Month "+ months[month-1]+" doesn't have 31 days!")
		return false;
	    }
		if (year < 1900) 
	    {
		alert("Year must be after 1900");
		return false;
	    }
	    if (month == 2)  // check for february 29th
	    {
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) 
		{
		    alert("February " + year + " doesn't have " + day + " days!");
		    return false;
		}
		
	    }
	    return true; // date is valid
	}

function isNotEmailId(str,Name)
{
	var ch = str.substring(0,1 );
	if ( ((ch < "a" || "z" < ch)&& (ch < "0" || "9" < ch) && (ch < "A" || "Z" < ch))  && (ch != '_')) 
	{
		alert("Invalid Email. Please re-enter in 'abcd@xyz.com' format ");
		return true;
	}
	
	if(str.charAt(str.length-1)=="." )
	{
		alert("Invalid Email. Please re-enter in 'abcd@xyz.com' format ");
		return true;
	}
	
	if ( !isNotAlphNumeric(str,Name) )
	{	
		var flag
		flag = true
		for (var i=0;i < str.length;i++)
		{	if (str.substring(i,i+1) == "@")
			{ 	
				flag = false
				break
			}
		}
		if ((str.indexOf(".")==-1) || (str.indexOf("@") > str.indexOf(".")))
		{ 	 
				flag = true
		}
		if ( flag )
		{	alert("Invalid Email. Please re-enter in 'abcd@xyz.com' format ");
			return true
		}	
		
		return false
	}
		

	
	return true
}

function isNotCharInt(str,Name)
{
	
	for (var i = 0; i < str.length; i++)
	{
		var ch = str.substring(i, i + 1);
		if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '-') && (ch != '_') &&  (ch != '@') &&  (ch != '\\') && (ch != '.') && (ch != ' ') && (ch = "'"))
		{
			//alert("The " + Name + " only accepts letters,numbers & underscore.Please re-enter the " + Name + ".");
			return true;
		}
	}
	
	return false;
}
function isNotAlphNumeric(str,k)
{
	
	for (var i = 0; i < str.length; i++)
	{
		var ch = str.substring(i, i + 1);
		if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '_') &&  (ch != '@') &&  (ch != '\\') && (ch != '.') && (ch != ' ') && (ch != "'"))
		{
			alert("The Item Description " + k + " only accepts letters,numbers & underscore.Please re-enter the Item Description " + k + ".");
			return true;
		}
	}
	
	return false;
}

function VerifyNumericData(pObj)
{
	
	if(window.event.keyCode>45 && window.event.keyCode<59)
	{
		if(window.event.keyCode==46)
		{
			
			var r,re;
			var s=pObj.value;
			re = ".";
			//r = s.search(re);
			var inCtr;
			for (inCtr=0;inCtr<s.length;inCtr++)
			if(s.charAt(inCtr)==".")
			{
				return false;
			}
		}
		return true;
	}
	else
	{
		return false;
	}
	
	
}

function chg()
{
    if(window.event.keyCode>45 && window.event.keyCode<59)
      {
            if(window.event.keyCode==46)
            {
                  var r,re;
                  var s=pObj.value;
                  re = ".";
                  //r = s.search(re);
                  var inCtr;
                  for (inCtr=0;inCtr<s.length;inCtr++)
                  if(s.charAt(inCtr)==".")
                  {
                        return false;
                  }
            }
            return true;
      }
      else
      {
            return false;
      }

}

function onlyint()
{
    if(window.event.keyCode>48 && window.event.keyCode<57)
      {
            return true;
      }
      else
      {
            return false;
      }

}

// This Function is for Checking only float value in a text box.
// You have to pass form_field value to this function eg.(return document.form1.txtname)
// eg.Onkeypress="return onlyfloat(document.form1.txtfloat)
// Harvinder Singh Gurm
function onlyfloat(form_field)
{
    if((window.event.keyCode>48 && window.event.keyCode<57 ) || window.event.keyCode==46)
      {
		
		if(window.event.keyCode==46)
		  {
			 var str=form_field.value;
			 var strpos=str.indexOf(".");
			if(strpos==-1)
			  {
				return true;
			  }
			  else
			  {
				  return false;
			  }
		  }
			 return true;
      }
      else
      {
          return false;
      }

}
// This Function is for Checking only float value in a text box. End


function deleteConfirm(varName)
{
	if (confirm("Are you sure, you want to delete " + varName + " ?" ))
	{		
		return true;
	}
	else
	{
		return false;
	}
}

function verifyIP(IPvalue) 
{
	errorString = "";
	theName = "IPaddress";

	var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ipArray = IPvalue.match(ipPattern); 

	if (IPvalue == "0.0.0.0")
	errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
	else if (IPvalue == "255.255.255.255")
	errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
	if (ipArray == null)
	errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
	else {
	for (i = 0; i < 4; i++) {
	thisSegment = ipArray[i];
	if (thisSegment > 255) {
	errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
	i = 4;
	}
	if ((i == 0) && (thisSegment > 255)) {
	errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
	i = 4;
		  }
	   }
	}
	extensionLength = 3;
	if (errorString == "")
		return "";
	elses
		return errorString;
}
function isNotCharInt123(str,Name)
{
	
	for (var i = 0; i < str.length; i++)
	{
		var ch = str.substring(i, i + 1);
		if ( ((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && (ch < "0" || "9" < ch) && (ch != '-') && (ch != '_') && (ch != '*') &&  (ch != '@') && (ch != '#') && (ch != '$') && (ch != '.') && (ch != ' ') && (ch = "'"))
		{
			//alert("The " + Name + " only accepts letters,numbers & underscore.Please re-enter the " + Name + ".");
			return true;
		}
	}
	
	return false;
}
function isNotFloat(str,Name)
{
 var isDot = false;
 var intPos;
 for (var i = 0; i < str.length; i++)
 {
  var ch = str.substring(i, i + 1);
  if((ch < "0" || ch > "9") && (ch !="-") && (ch !=' ')&& (ch !='.')) 
  {
   //alert("The " + Name + " accepts only a Dot (.) and numeric value.Please re-enter " + Name + ".");
   return true;
  }
  if (ch =='.') 
		{	
			if (!isDot)
			{	isDot=true; 
				intPos = i;
				
			}
			else
			{	
				//alert("The value contains invalid data. Please re-enter.");
				return true;	
			}	
		}
 }
 return false;
}
