// 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)
{
	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;
	}
	
	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 + " cannot start with a space");
		return false;
	}
	if(isNaN(strtemp))
	{
		window.alert(fldname + " can contain only numbers");
		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;
		var b = a.substring(0, 2);	// month
		var c = a.substring(2, 3);	// '/'
		var d = a.substring(3, 5);	// day
		var e = a.substring(5, 6);	// '/'
		var 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)
{
 var c=0;
 var i=0;
 
 // check for blank value
 if(str_email=="")
 {
  window.alert("Please enter " + fldname);
  return false;
 }
 /////////////////////////////
 
 //check for invalid characters
 var 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 @
 var arr_temp=str_email.split("@");
 if(arr_temp.length!=2)
 {
  alert("Please enter valid " + fldname);
  return(false);
 }
 /////////////////////////////
 
 //check for . before @
 var 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);
  }
  var 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);
}


function validateDate(objmonth,objday,objyear,fldname)
{
	var iMonth;
	var iDay;
	var iYear;
	var validDate;
	
	validDate="";
	
	if(objmonth.value=="" && objday.value=="" && objyear.value=="")
	{
		alert("Please select " + fldname);
		return validDate;
	}

	iMonth=new Number(objmonth.value);
	iDay=new Number(objday.value);
	iYear=new Number(objyear.value);
	
	validDate=iMonth+"/"+iDay+"/"+iYear;
	
	if(iYear<1900)
	{
		validDate="";
	}
	else if(iMonth<1 || iMonth>12)
	{
		validDate="";
	}
	else if((iMonth==4)||(iMonth==6)||(iMonth==9)||(iMonth==11))
	{
		if (!(iDay>=1 && iDay <= 30))
		validDate="";
	}
	else if(iMonth==2)
	{	
		
		if ((iYear%4==0 && (iYear%100!=0)) || iYear%400==0)
		{
			if (!(iDay>=1 && iDay <= 29))
			validDate="";
		}
		else
		{
			if (!(iDay>=1 && iDay <= 28))
			validDate="";
		}				
	}
	else
	{
		if (!(iDay>=1 && iDay <= 31))
		validDate="";
	}
	
	if(validDate=="")
	{
		alert("Please select valid " + fldname);
	}
	
	return validDate;
}

function DateAdd(startDate, numDays, numMonths, numYears, numHours, numMinutes, numSeconds)
{
	var objNum = new Number(numDays);
	numDays = objNum;
	var objNum = new Number(numMonths);
	numMonths = objNum;
	var objNum = new Number(numYears);
	numYears = objNum;
	var objHours = new Number(numHours);
	numHours = objHours;
	var objMinutes = new Number(numMinutes);
	numMinutes = objMinutes;
	var objSeconds = new Number(numMinutes);
	numSeconds = objSeconds;
	
	var returnDate = new Date(startDate.getTime());
	//Add Minutes
	returnDate.setTime(returnDate.getTime()+60000*numMinutes);
	//Add Hours
	returnDate.setTime(returnDate.getTime()+60000*60*numHours);
	
	var yearsToAdd = numYears;
	
	var month = returnDate.getMonth() + numMonths;
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear() + yearsToAdd);
	
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
	return returnDate;
}

function YearAdd(startDate, numYears)
{
	return DateAdd(startDate,0,0,numYears,0,0,0);
}

function MonthAdd(startDate, numMonths)
{
	return DateAdd(startDate,0,numMonths,0,0,0,0);
}

function DayAdd(startDate, numDays)
{
	return DateAdd(startDate,numDays,0,0,0,0,0);
}

function HourAdd(startDate, numHours)
{
	return DateAdd(startDate,0,0,0,numHours,0,0);
}

function MinuteAdd(startDate, numMinutes)
{
	return DateAdd(startDate,0,0,0,0,numMinutes,0);
}

function SecondAdd(startDate, numSeconds)
{
	return DateAdd(startDate,0,0,0,0,0,numSeconds);
}

function checkmobile(strmobile,fldname)
{
	if(strmobile=="")
	{
		alert("Please enter " + fldname);
		return(false);
	}
	//check for invalid characters
	var str_valid=new String("0123456789");
	var c=0;
	for (c=0; c<strmobile.length; c++)
	{
		if(str_valid.indexOf(strmobile.charAt(c))==-1)
		{
			alert("Please enter valid " + fldname);
			return(false);
		}
	}
	if(strmobile.length!=10)
	{
		alert("Please enter valid " + fldname);
		return(false);
	}
	
	return(true);
}