function checkDate(strDate){
   if(strDate.length>0){
      var dateregex=/^[ ]*[0]?(\d{1,2})\/(\d{1,2})\/(\d{4,})[ ]*$/;
      var match=strDate.match(dateregex);
      if (match){
         var tmpdate=new Date(match[3],parseInt(match[1],10)-1,match[2]);
         if (tmpdate.getDate()==parseInt(match[2],10) && tmpdate.getFullYear()==parseInt(match[3],10) && (tmpdate.getMonth()+1)==parseInt(match[1],10)){ 
            return true; 
         }
      }
      //alert("Please enter valid values for the day, month and year!");
      return false;
   }
   else{
      return true;
   }
}

function checkTravelDate(form){
   var strDate = form
   if(strDate.length>0){
      var dateregex=/^[ ]*[0]?(\d{1,2})\/(\d{1,2})\/(\d{4,})[ ]*$/;
      var match=strDate.match(dateregex);
      if (match){
         var tmpdate=new Date(match[3],parseInt(match[1],10)-1,match[2]);
         if (tmpdate.getDate()==parseInt(match[2],10) && tmpdate.getFullYear()==parseInt(match[3],10) && (tmpdate.getMonth()+1)==parseInt(match[1],10)){ 
            return true; 
         }
      }
      alert("Please enter valid values for the day, month and year!");
      return false;
   }
   else{
      return true;
   }
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


function sfCheck(form) {
	var e, title, empty_fields, char_check, eMail, invalid_eMail 
	var tempError, special_Error, msg, upperLine, lowerLine
	var num, invalid_phoneNumber, invalid_eBirthDate, eBirthDate, bdate, invalid_estartDate, estartDate, invalid_eendDate, eendDate

	msg = "";
	empty_fields = "";
	char_check = "";
	special_Error = "";
	tempError = "";
	num = form.length

	for (var i = 0; i < form.length; i++) {
		e = form.elements[i]
		if ((e.title == null)||(e.title == "")) {
			title = e.name;
		}
		else {
			title = e.title;
		}
		if (((e.type == "text") || (e.type == "textarea")|| (e.type == "eBirthDate")||(e.type == "password")) && !e.special && !e.disabled) {
			if (e.value.length <= 0 && !e.optional) {
				empty_fields += "\n            " + title;
				continue;
			}
			if (e.number) {
				num = e.value;
				num = stripChar(num, ".");
				num = stripChar(num, ",");
				if (!isNumber(num)) {
					char_check += "\n             " + title;
				}
			}
			if (e.eMail) {
				eMail = e.value;
				if ((eMail.indexOf("@") != -1) && (eMail.indexOf(".") != -1)) {
					invalid_eMail = false;
				}
				else {
					invalid_eMail = true;
				}
			}
 			if (e.eBirthDate) {
				invalid_eBirthDate = false;
				if (!checkDate(e.value)) {
					invalid_eBirthDate = true;
				}

				//eBirthDate = e.value;
				//if ((eBirthDate.indexOf("/") != -1)) {
				//	bdate = eBirthDate.split("/");
				//	if (bdate.length == 3) {						

				//		if ((IsNumeric(bdate[0])) && (IsNumeric(bdate[1])) && (IsNumeric(bdate[2])) && (bdate[0]>0) && (bdate[0] < 13) && (bdate[1]>0) && (bdate[1] < 32) && (bdate[2]> 1900) && (bdate[2] < 2000)) {
				//			invalid_eBirthDate = false;
				//		}
				//		else {
				//			invalid_eBirthDate = true;
				//		}
				//	}
				//	else {
				//		invalid_eBirthDate = true;
				//	}
				//}
				//else {
				//	invalid_eBirthDate = true;
				//}
			}
 			if (e.estartDate) {
				invalid_estartDate = false;
				if (!checkDate(e.value)) {
					invalid_estartDate = true;
				}
			}
			if (e.eendDate) {
				invalid_eendDate = false;
				if (!checkDate(e.value)) {
					invalid_eendDate = true;
				}
			}
            if (e.name == "txtEmail") {
				eMail = e.value;
				if ((eMail.indexOf("@") != -1) && (eMail.indexOf(".") != -1)) {
					invalid_eMail = false;
				}
				else {
				  
					invalid_eMail = true;
				}
			}	
			if (e.name == "txtFriend") {
				eMail = e.value;
				if ((eMail.indexOf("@") != -1) && (eMail.indexOf(".") != -1)) {
					invalid_eMail = false;
				}
				else {
				  
					invalid_eMail = true;
				}
			}	

			if (e.phoneNumber) {
				num = e.value;
				num = stripChar(num, " ");
				num = stripChar(num, "-");
				num = stripChar(num, "+");
				if (num.length < 10) {
					invalid_phoneNumber = true;
				}	
			}
		}

		if (e.special) {
			checkSpecial = specialCase(e, form);
			if (tempError != checkSpecial) {
				special_Error = special_Error + checkSpecial
			}
			tempError = checkSpecial;
		}
		if (e.type == "select-one" && !e.optional) {
//			if (e.value == "") {
			if (e.selectedIndex == 0) {
				empty_fields += "\n            " + title;
				continue;
			}
		}

	}
	
	if (!empty_fields && !char_check && !special_Error && !invalid_eMail && !invalid_eBirthDate && !invalid_phoneNumber) {return true}
	
	msg += "The form was not submited due to the following error(s).\n";
	
	upperLine = "\n_________________________________________________________\n\n";
	lowerLine = "_________________________________________________________\n";
	
	if (empty_fields) {
		msg += upperLine;
		msg += "The following field(s) must be filled in:\n";
		msg += lowerLine;
		msg += empty_fields;
	}
	if (char_check) {
		msg += upperLine;
		msg += "The following field(s) need a numeric value:\n";
		msg += lowerLine;
		msg += char_check;
	}

	if (invalid_estartDate) {
		msg += upperLine;
		msg += "The Start Date is invalid.\n";
		msg += lowerLine;
	}
	if (invalid_eendDate) {
		msg += upperLine;
		msg += "The End Date is invalid.\n";
		msg += lowerLine;
	}
	if (invalid_eMail) {
		msg += upperLine;
		msg += "The Email Address is in an invalid format.\n";
		msg += lowerLine;
	}
	if (invalid_eBirthDate) {
		msg += upperLine;
		msg += "The Date of Birth is invalid.\n";
		msg += lowerLine;
	}

	if (invalid_phoneNumber) {
		msg += upperLine;
		msg += "Please enter a valid Phone Number with area code.\n";
		msg += lowerLine;
	}
	if (special_Error) {
		msg += upperLine;
		msg += special_Error + "\n";
		msg += lowerLine;
	}
	alert(msg);
	return false;
}	