//Checking for Valid Email
function validEmail(email)
{
	var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
	var checkend=/\.[a-zA-Z]{2,3}$/;

	if((email.search(exclude) != -1) || (email.search(checkend) == -1))
	{
		return false;
	}

	var atPos = email.indexOf("@",0);
	var pPos1 = email.indexOf(".",0);
	var periodPos = email.indexOf(".",atPos);
	var pos1 = pPos1;
	var pos2 = 0;

	while (pos2 > -1)
	{
		pos2 = email.indexOf(".",pos1+1);
		if (pos2 == pos1+1)
		{
			return false;
		}
		else
		{
			pos1 = pos2;
		}
	}

	if (atPos == -1)
	{
		return false;
	}

	if (atPos == 0)
	{
		return false;
	}

	if (pPos1 == 0)
	{
		return false;
	}

	if(email.indexOf("@",atPos+1) > -1)
	{
		return false;
	}

	if(periodPos == -1)
	{
		return false;
	}

	if(atPos+1 == periodPos)
	{
		return false;
	}

	if(periodPos+3 > email.length)
	{
		return false;
	}
	   	return true;
}
//Checking for lower case characters only (without spaces)
function lowercharactersOnly(fieldName)
{
	var validChars = "abcdefghijklmnopqrstuvwxyz0123456789_";
	var temp;

	for(var i=0; i < fieldName.length; i++)
	{
		temp = fieldName.substring(i,i+1);
		if(validChars.indexOf(temp) == "-1")
		{
			return false;
		}
	}
}
//Checking for Characters only (without spaces)
function charactersOnly(fieldName)
{
	var validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
	var temp;

	for(var i=0; i < fieldName.length; i++)
	{
		temp = fieldName.substring(i,i+1);
		if(validChars.indexOf(temp) == "-1")
		{
			return false;
		}
	}	
}
//Checking for Characters only (with spaces)
function charactersOnlyWithSpaces(fieldName)
{
	var validChars = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var temp;

	for(var i=0; i < fieldName.length; i++)
	{
		temp = fieldName.substring(i,i+1);
		if(validChars.indexOf(temp) == "-1")
		{
			return false;
		}
	}
	
}

//Checking for Numbers only
function numbersOnly(fieldName)
{
	var validChars = "0123456789";
	var temp;

	for(var i=0; i < fieldName.length; i++)
	{
		temp = fieldName.substring(i,i+1);
		if(validChars.indexOf(temp) == "-1")
		{
			return false;
		}
	}
	
}
//validating DOB
function isDate()	{
	var yy,mm,dd;
	var im,id,iy;
	var present_date = new Date();
	yy = 1900 + present_date.getYear();
	if (yy > 3000)	{
		yy = yy - 1900;
	}
	mm = present_date.getMonth();
	mm = mm + 1;
	dd = present_date.getDate();
	im = document.forms[0].month.selectedIndex;
	var entered_month = document.forms[0].month.options[im].value;
	var invalid_month = document.forms[0].month.options[im].value-1;
	var entered_day = document.forms[0].day.value;
	var entered_year = document.forms[0].year.value;
	if ( (entered_day == 0) || (entered_month == 0) || (entered_year == 0) )	{
		alert("Please enter your birthday");
		return false;
	}
	if ( is_valid_day(invalid_month,entered_day,entered_year) )	{
		return true;
	}
	return false;
}

function is_valid_day(entered_month,entered_day,entered_year)	{
	
	if ((entered_year % 4) == 0)
	{
		var days_in_month = "312931303130313130313031";
 	}
 	else	{

		var days_in_month = "312831303130313130313031";
 	}
	if (entered_month != -1)
	{
		if ( parseInt(entered_day) > parseInt(days_in_month.substring(2*entered_month,2*entered_month+2)) )
		{
			alert ("The birthday field is entered wrongly (the day field value exceeds the number of days for the month entered).");
			return false;
		}
	}
	return true;
}
  function valid(form){
   var field0 = form.username;
   var field = form.passwordA;
   var field2 = form.passwordB;
   var field3 = form.email;
   var field4 = form.day;
   var field5 = form.month;
   var field6 = form.year;
//username length
   if (field0.value.length < 4) {
      alert("You must enter a Username of atleast 4 characters");
      field0.focus();
      field0.select();
      return false;
   }
//username length
   if (field0.value.length > 20) {
      alert("Hellloooo! A 20 character username? We will go cracy remembering that! why not try a shorter username?");
      field0.focus();
      field0.select();
      return false;
   }
//username no invalid chars
   if (lowercharactersOnly(field0.value) == false) {
      alert("Username can contain only lowercase characters and numbers. Spaces and Symbols are invalid characters");
      field0.focus();
      field0.select();
      return false;
   }
//password length
   if (field.value.length < 5) {
      alert("You must enter a password of atleast 5 characters");
      field.focus();
      field.select();
      return false;
   } else if (field2.value == "") {
      alert("You must enter a password for the re-type field too. Sorry about this, we are quiet dumb and dont always get this right the first time!");
      field2.focus();
      field2.select();
      return false;
   } 
   if (validEmail(field3.value) == false) {
      alert("You must enter a VALID Email address, trust us, you will see the perks of not lying soon...");
      field3.focus();
      field3.select();
      return false;
   }
/* Additional validations for Birth date begins*/
	if(field5.value == "")
	{
		alert("Select a month of birth");
		field5.focus();
		return false;	
	}
	if(isNaN(field4.value))
	{
		alert("Enter a valid date of birth");
		field4.value="";
		field4.focus();
		return false;
	}
	if(field4.value <= 0 || field4.value > 31 )
	{
		alert("Enter a valid date of birth");
		field4.value="";
		field4.focus();
		return false;
	}
	if(isNaN(field6.value))
	{
		alert("Enter a valid year of birth");
		field6.value="";
		field6.focus();
		return false;
	}
	var present_date = new Date();
	curr_year	= 1900 + present_date.getYear();
        if (curr_year > 3000)  {
                curr_year = curr_year - 1900;
        }
	if(field6.value <= 1900 || field6.value > curr_year )
	{
		alert("Enter a valid year of birth");
		field6.value="";
		field6.focus();
		return false;
	}
/* Additional validations for Birth date ends */
   if (field.value == field2.value) {
      return true;
   } else {
      alert("You are one step away from pet heaven, please make sure your passwords match! You dont want to get there and not be able to get in right?");
      field.focus();
      field.select();
      return false;
   }

}

