function validate()
{
	var frm = document.forms['frm_reg'];

	if (trim(frm.txt_login.value) == "")
	{
		alert("Please enter login !");
		frm.txt_login.focus();
		return false;
	}

	if (trim(frm.txt_password.value) == "")
	{
		alert("Please enter password !");
		frm.txt_password.focus();
		return false;
	}
	
	if (trim(frm.txt_cpassword.value) == "")
	{
		alert("Please re-type your password !");
		frm.txt_cpassword.focus();
		return false;
	}

	if(trim(frm.txt_password.value) != trim(frm.txt_cpassword.value))
	{
		alert("Password Mismatch !");
		frm.txt_cpassword.focus();
		return false;
	}

	if (trim(frm.txt_firstname.value) == "")
	{
		alert("Please enter first name !");
		frm.txt_firstname.focus();
		return false;
	}
	if (!isString(frm.txt_firstname.value)) {
		alert("Please enter only letter in the First name field.");
		frm.txt_firstname.focus();
		return false;		
	}
	
	// Last name
	if (trim(frm.txt_lastname.value) == "")
	{
		alert("Please enter last name !");
		frm.txt_lastname.focus();
		return false;
	}
	if (!isString(frm.txt_lastname.value)) {
		alert("Please enter only letter in the Last name field.");
		frm.txt_lastname.focus();
		return false;
	}
	
	if (trim(frm.txt_address1.value) == "")
	{
		alert("Please enter address !");
		frm.txt_address1.focus();
		return false;
	}
	
	//Course Date
	if (frm.txt_month.value == '') {
		alert("Please select month of date of birth ! ");
		frm.txt_month.focus();
		return false;
	}	
	if (frm.txt_day.value == '') {
		alert("Please select day of date of birth ! ");
		frm.txt_day.focus();
		return false;
	}
	if (frm.txt_year.value == '') {
		alert("Please select year of date of birth ! ");
		frm.txt_year.focus();
		return false;
	}
//	if (isDate(frm.txt_day.value, frm.txt_month.value, frm.txt_year.value) == false) {
	dtstr = frm.txt_month.value;		
	dtstr = dtstr +  "-" + frm.txt_day.value;			
	dtstr = dtstr +  "-" + frm.txt_year.value;	
		
	if (isDate(dtstr) == false) {
		//frm.txt_month.focus();
		return false
	}

	//Age
//	if (trim(frm.txt_age.value) == "")
//	{
//		alert("Please enter age !");
//		frm.txt_age.focus();
//		return false;
//	}
//	if (!isInteger(frm.txt_age.value) ) {		
//		alert("Please enter valid age !");
//		frm.txt_age.value="";
//		frm.txt_age.focus();
//		return false;
//	}
	
	
	//Address 
//	if (trim(frm.txt_address1.value) == "")
//	{
//		alert("Please enter address !");
//		frm.txt_address1.focus();
//		return false;
//	}

	//City
//	if (trim(frm.txt_city.value) == "")
//	{
//		alert("Please enter city !");
//		frm.txt_city.focus();
//		return false;
//	}	
	
	//Country
	if (trim(frm.txt_country.value) == "")
	{
		alert("Please enter county !");
		frm.txt_country.focus();
		return false;
	}	
	
	//zipcode
	/*
	if (trim(frm.txt_zipcode.value) == "")
	{
		alert("Please enter zipcode !");
		frm.txt_zipcode.focus();
		return false;
	}	
	if (!isZipcode(frm.txt_zipcode.value)) {
		alert("Please enter valid zipcode !");
		frm.txt_zipcode.value = '';
		frm.txt_zipcode.focus();
		return false;		
	}*/	
	
	//Country
	if (trim(frm.txt_country.value) == "")
	{
		alert("Please select country !");
		frm.txt_country.focus();
		return false;
	}	

	//E-mail checking
	if (trim(frm.txt_email.value) == "") {
		alert("Please enter e-mail address !");
		frm.txt_email.focus();
		return false;
	}
	if (!is_email(frm.txt_email.value)) {
		alert("Please enter valid work e-mail address !");
		frm.txt_email.value = '';
		frm.txt_email.focus();
		return false;		
	}	
      	
	//E-mail - Alternet
//	if (trim(frm.txt_email_alternet.value) == "") {
//		alert("Please enter alternate e-mail address !");
//		frm.txt_email_alternet.focus();
//		return false;
//	}	
//	if (!is_email(frm.txt_email_alternet.value)) {
//		alert("Please enter valid alternate e-mail address !");
//		frm.txt_email_alternet.value = '';
//		frm.txt_email_alternet.focus();
//		return false;		
//	}

	//Telephone - office
//	if (trim(frm.txt_phone.value) == ""){
//		alert("Please enter phone number !");
//		frm.txt_phone.focus();
//		return false;
//	}	
//	if (!isPhoneno(frm.txt_phone.value)) {
//		alert("Please enter valid phone number !");
//		frm.txt_phone.value = '';
//		frm.txt_phone.focus();
//		return false;		
//	}	
	
	//Profession
	if (trim(frm.txt_profession.value) == "")
	{
		alert("Please select profession !");
		frm.txt_profession.focus();
		return false;
	}

	//Company
	if (trim(frm.txt_company.value) == "")
	{
		alert("Please enter company name !");
		frm.txt_company.focus();
		return false;
	}

	// Race
	
	if (trim(frm.race.value) == "")
	{
		alert("Please enter race name !");
		frm.race.focus();
		return false;
	}
	
	if(!document.frm_reg.agree.checked){
	alert("You must select the \"I Agree\" box \n confirming that you agree to the Terms \n and Conditions / Rules and Regulations. \n");
		return false;
	}
	
	return true;	
}

function is_email(email)
{
	if(!email.match(/^[A-Za-z0-9\._\-+]+@[A-Za-z0-9_\-+]+(\.[A-Za-z0-9_\-+]+)+$/))
		return false;
	return true;
}

function trim(inputString) 
{
   inputString=inputString.replace(/^\s+/g,"");
   inputString=inputString.replace(/\s+$/g,"");
   return inputString;
}

function isZipcode(checkStr)
{
	  checkStr = trim(checkStr);
	  var checkOK  = "0123456789-";
	  for (i=0;  i < checkStr.length;  i++)	  
	  {
		  var ch = checkStr.charAt(i);
		  for (j = 0; j < checkOK.length; j++)
		  	if (ch == checkOK.charAt(j))
			  break;

		  if (j == checkOK.length)
		  	  return false;
	  }
	  return true
}

function isPhoneno(checkStr)
{
	  checkStr = trim(checkStr);
	  var checkOK  = "0123456789()-+";
	  for (i=0;  i < checkStr.length;  i++)	  
	  {
		  var ch = checkStr.charAt(i);
		  for (j = 0; j < checkOK.length; j++)
		  	if (ch == checkOK.charAt(j))
			  break;

		  if (j == checkOK.length)
		  	  return false;
	  }
	  return true
}

   
function isString(checkStr)
{
	  checkStr = trim(checkStr);
	  var checkOK  = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz. '";
	  for (i=0;  i < checkStr.length;  i++)
	  {
		  var ch = checkStr.charAt(i);
		  for (j = 0; j < checkOK.length; j++)
		  	if (ch == checkOK.charAt(j))
			  break;

		  if (j == checkOK.length)
		  	  return false;
	  }
	  return true
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

//function isDate(dt_day, dt_month, dt_year) {
function isDate(dtStr) {	

	var dtCh= "-";
	var minYear=1900;
	var maxYear=2100;

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)

	//var strDay=dtStr.substring(pos2+1)
	//var strYear=dtStr.substring(0,pos1)
	
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)	
	var strYear=dtStr.substring(pos2+1)
	
//	var strMonth= dt_month;
//	var strDay=	dt_day;
//	var strYear=dt_year;
	
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd-mm-yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		document.frm_reg.txt_month.focus()
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		document.frm_reg.txt_day.focus()
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear) {
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		document.frm_reg.txt_day.focus()
		return false
	}
	return true
}

function textCounter(field, maxlimit) {
	var tval = trim(field.value);	
	if (tval.length > maxlimit) // if too long...trim it!
	{
		field.value = field.value.substring(0, maxlimit);		
		alert("You can not enter more than " + maxlimit + " characters ! ");
		return false;
	}
}

