// This function checks that a field has not exceeded its maximum limit
function checkmaxlength(strtemp, maxlength, fldname)
{
	if(strtemp.length>maxlength)
	{
		window.alert(fldname + " has exceeded it's maximum limit");
		return false;
	}
	
	return true;
}

// This function checks that a field is not empty and does not start with a space
function checkstring(strtemp, fldname)
{
	temp=new String(strtemp);
	if(strtemp=="")
	{
		window.alert("Please enter " + fldname);
		return false;
	}
	
	if(temp.charAt(0)==" ")
	{
		window.alert(fldname + " cannot start with a space");
		return false;
	}
	
	return true;
}

// This function checks that a field is not empty and contains only number
function checknumber(strtemp, fldname)
{
	if(strtemp=="")
	{
		window.alert("Please enter " + fldname);
		return false;
	}
	if(strtemp.charAt(0)==" ")
	{
		window.alert(fldname + " can contain only numbers");
		return false;
	}
	if(isNaN(strtemp))
	{
		window.alert(fldname + " can contain only numbers");
		return false;
	}
	
	return true;
}

// This function checks that a field is not empty and contains only number
function checkcurrency(strtemp, fldname)
{
	if(strtemp=="")
	{
		window.alert("Please enter " + fldname);
		return false;
	}
	
	if(isNaN(strtemp))
	{
		window.alert(fldname + " can contain only positive numbers");
		return false;
	}

	var numCur=new Number(strtemp);
	if(numCur<0)
	{
		window.alert(fldname + " can contain only positive numbers");
		return false;
	}
	
	var arrCur=strtemp.split(".");
	if(arrCur.length!=2)
	{
		window.alert(fldname + " should have proper number format (ie. 10000.00)");
		return false;
	}
	if(arrCur[1].length!=2)
	{
		window.alert(fldname + " should have proper number format (ie. 10000.00)");
		return false;
	}
	return true;
}

// This function checks that a textarea is not empty and does not start with a space
function checktextarea(strtemp, fldname)
{
	var temp = new String(strtemp);
	
	if(strtemp=="")
	{
		window.alert("Please enter " + fldname);
		return false;
	}
	
	if(temp.charAt(0)==" ")
	{
		window.alert(fldname + " cannot start with a space");
		return false;
	}
	
	var re=/ /gi;
	temp=temp.replace(re,'');
	
	var count=0;
	for(i=0; i<temp.length; i++)
	{
		if(temp.charCodeAt(i)==13)
		{
			count++;
		}
		else if(temp.charCodeAt(i)==10)
		{
			count++;
		}
	}
	if(temp.length==count)
	{
		window.alert("Please enter valid " + fldname);
		return false;
	}
	return true;
	
}

//This function checks the date
function checkdate(dateval, fldname)
{
	var err=0
	var a=new String(dateval);
	
	if(a=="")
	{
		window.alert("Please enter " + fldname);
		return false;
	}
	else
	{
		if (a.length != 10) err=1
		b = a.substring(0, 2)// month
		c = a.substring(2, 3)// '/'
		d = a.substring(3, 5)// day
		e = a.substring(5, 6)// '/'
		f = a.substring(6, 10)// year
		
		// error checking
		if (b<1 || b>12) err = 1
		if (c != '/') err = 1
		if (d<1 || d>31) err = 1
		if (e != '/') err = 1
		// error checking
		
		// months with 30 days
		if (b==4 || b==6 || b==9 || b==11)
		{
			if (d==31) err=1
		}
		
		// february, leap year
		if (b==2)
		{
			// feb
			var g=parseInt(f/4)
			if (isNaN(g))
			{
				err=1
			}
			if (d>29) err=1
			if (d==29 && ((f/4)!=parseInt(f/4))) err=1
		}
		
		if (err==1)
		{
			alert("Please enter valid " + fldname);
			return false;
		}
		else
		{
			return true;
		}
	}
}

//This function checks the email
function checkemail(str_email, fldname)
{
 // check for blank value
 if(str_email=="")
 {
  window.alert("Please enter " + fldname);
  return false;
 }
 /////////////////////////////
 
 //check for invalid characters
 str_valid=new String("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._@-");
 for (c=0; c<str_email.length; c++)
 {
  if(str_valid.indexOf(str_email.charAt(c))==-1)
  {
   alert("Please enter valid " + fldname);
   return(false);
  }
 }
 /////////////////////////////
 
 //check for single @
 arr_temp=str_email.split("@");
 if(arr_temp.length!=2)
 {
  alert("Please enter valid " + fldname);
  return(false);
 }
 /////////////////////////////
 
 //check for . before @
 arr_temp1=arr_temp[0].split(".");
 /////////////////////////////
 
 //check for invalid characters before @
 str_valid=new String("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
 for(i=0; i<arr_temp1.length; i++)
 {
  if(arr_temp1[i]=="")
  {
   alert("Please enter valid " + fldname);
   return(false);
  }
  bln_valid=false;
  for (c=0; c<arr_temp1[i].length; c++)
  {
   if(str_valid.indexOf(arr_temp1[i].charAt(c))>=0)
   {
    bln_valid=true;
    break;
   }
  }
  if(bln_valid==false)
  {
   alert("Please enter valid " + fldname);
   return(false);
  }
 }
 /////////////////////////////
 
 //check for single . after @
 arr_temp1=arr_temp[1].split(".");
 if(arr_temp1.length<2)
 {
  alert("Please enter valid " + fldname);
  return(false);
 }
 /////////////////////////////
 
 //check for invalid characters after @
 str_valid=new String("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
 for(i=0; i<arr_temp1.length; i++)
 {
  if(arr_temp1[i]=="")
  {
   alert("Please enter valid " + fldname);
   return(false);
  }
  bln_valid=false;
  for (c=0; c<arr_temp1[i].length; c++)
  {
   if(str_valid.indexOf(arr_temp1[i].charAt(c))>=0)
   {
    bln_valid=true;
    break;
   }
  }
  if(bln_valid==false)
  {
   alert("Please enter valid " + fldname);
   return(false);
  }
 }
 /////////////////////////////
 
 return(true);
}
