// JavaScript Document
//Compressed CSS Styles
//dob validation function

//main function
var alert_msg="";

function checkform(thisform)
{
var alert_count = 0;
alert_msg="";
// Member name validation
//    if(document.addMember.memName.value == "") {
//
//	  alert_count = 1;
//	  alert_msg = "Profile Name cannot be empty! \n";
//
//    }
//    else if(document.addMember.memName.value.length < 6) {
//
//	  alert_count = 1;
//	  alert_msg = "Profile Name must contain at least six characters! \n";
//
//    }
	
	
//password validations	
	
   if(document.addMember.memPassword.value == "" ) 
       {
        alert_count =1;
	  alert_msg=alert_msg + "Password cannot be empty! \n";
		
		
		
      }else
	
	
	
	if(document.addMember.memPassword.value.length < 6) 
       {
        alert_count =1;
	  alert_msg=alert_msg + "Password must contain at least six characters!! \n";
		
		
		
      }
	if((document.addMember.confirmMemPassword.value) != (document.addMember.memPassword.value) ) 
       {
        alert_count =1;
	  alert_msg=alert_msg + "Password and confirm password are not match! \n";
		
		
		
      }
	 
// Member First name  validation
    if(document.addMember.memFirstName.value == "") {
     
	  alert_count = 1;
	  alert_msg =alert_msg + "First name  cannot be empty! \n";
	
    }
	 
	 
	 
// Last name  town validation
    if(document.addMember.memLastName.value == "") {
     
	  alert_count = 1;
	  alert_msg =alert_msg + "Last name  cannot be empty! \n";
	
    }

//Email address check


   if (document.addMember.memEmail.value == "")
	{
	  alert_count =1;
	  alert_msg=alert_msg + "Email cannot be empty! \n";


}else{

if(document.addMember.add.value==0){		
	  alert_count =1;
	  alert_msg=alert_msg + "Email Already Exist! \n";
}else{

	// test if valid email address, must have @ and .
	var checkEmail = "@.";
	var checkStr = document.addMember.memEmail.value;
	var EmailValid = false;
	var EmailAt = false;
	var EmailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)
	{
	ch = checkStr.charAt(i);
	for (j = 0;  j < checkEmail.length;  j++)
	{
	if (ch == checkEmail.charAt(j) && ch == "@")
	EmailAt = true;
	if (ch == checkEmail.charAt(j) && ch == ".")
	EmailPeriod = true;
		  if (EmailAt && EmailPeriod)
			break;
		  if (j == checkEmail.length)
			break;
		}
		// if both the @ and . were in the string
	if (EmailAt && EmailPeriod)
	{
			EmailValid = true
			break;
		}
	}
	if (!EmailValid)
	{
		alert_count =1;
		  alert_msg=alert_msg + " The \"email\" field must contain an \"@\" and a \".\".! \n";
	
	}
}

}
if (document.addMember.memEmail.value != document.addMember.confirmMemEmail.value)
	{
	  alert_count =1;
	  alert_msg=alert_msg + "Email and confirm email are not match ! \n";


}
// for date format validation
    if(document.addMember.memDob.value == "") {
     
	  alert_count = 1;
	  alert_msg =alert_msg + "Date of Birth  cannot be empty! \n";
	
    }else{
		var err_msg=alert_msg;
		if(isValidDate(document.addMember.memDob.value)){
			alert_msg=err_msg;
		}
		else{
			err_msg=err_msg+alert_msg;
			alert_msg=err_msg;
			alert_count = 1;
	  		//alert_msg =alert_msg + "Dtae of birth is not in valid format! \n";
		}
	}



// Location town validation
    if(document.addMember.memLocation.value == "") {
     
	  alert_count = 1;
	  alert_msg =alert_msg + "Location  cannot be empty! \n";
	
    }
	
// Member Home town validation
    if(document.addMember.memHometown.value == "") {
     
	  alert_count = 1;
	  alert_msg =alert_msg + "Home Town  cannot be empty! \n";
	
    }

 // Member Gender validation
    if(document.addMember.memGender.value == "") {
     
	  alert_count = 1;
	  alert_msg =alert_msg + "Gender Field  cannot be empty! \n";
	
    }
	
	 // Member Gender validation
	 
 // Member Status validation
    if(document.addMember.memMstatus.value == "") {
     
	  alert_count = 1;
	  alert_msg =alert_msg + "Status Field  cannot be empty! \n";
	
    }
	
	 // Member Status validation

 // Member Work validation
    if(document.addMember.memWork.value == "") {
     
	  alert_count = 1;
	  alert_msg =alert_msg + "Work cannot be empty! \n";
	
    }
	
	 // Member Work validation
	 
  // Msg1 validation
    if(document.addMember.memMsg1.value == "") {
     
	  alert_count = 1;
	  alert_msg =alert_msg + "The best word to describe me cannot be empty! \n";
	
    }
	
	 // Msg1 validation   
	
	
// check if more than 5 options are selected
// check if less than 1 options are selected

/*alert(document.addMember.memInterestName+"[]".value);

var numSelected = 0;
var i;
for (i = 0;  i < document.addMember.memInterestName+"[]".length;  i++)
{
if (document.addMember.memInterestName+"[]".options[i].selected)
numSelected++;
}

if (numSelected < 1)
{
	
	alert_count = 1;
	  alert_msg =alert_msg + "Please select at least 1 of the \"interests\" options.! \n";
	
//alert("Please select at least 1 of the \"province\" options.");
//document.addMember.memInterestName.focus();
//return (false);
}*/
	
if (alert_count ==1)
  
   {
	   alert(alert_msg);
	 
    return false;
	   
   }
else{
 
 return true;
}
}


//dob validation function
function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables

alert_msg="";

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;
// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {




alert_msg =alert_msg + "Date is not in a valid format. \n";
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert_msg =alert_msg + "Month must be between 1 and 12. \n";
return false;
}
if (day < 1 || day > 31) {
alert_msg =alert_msg + "Day must be between 1 and 31.\n";;
return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert_msg =alert_msg + "Month "+month+" doesn't have 31 days! \n";
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_msg =alert_msg + "February " + year + " doesn't have " + day + " days! \n";
return false;
}
}
return true;  // date is valid
}

