function form_validation()
{	  
   with(document.form)
	{
		if(trimAll(InvoiceDate.value)=="")
		{
			alert("Please enter the invoice date.");
			InvoiceDate.focus();
			return false;			
		}
		else if(!validateUSDate(InvoiceDate.value))
		{
			alert("Please enter the date in the correct format!");
			InvoiceDate.focus();
			return false;
		}
		else if(trimAll(SponsorName.value)=="")
		{
			alert("Please enter sponsor's name.");
			SponsorName.focus();
			return false;
		}
		else if(trimAll(SponsorTitle.value)=="")
		{
			alert("Please enter sponsor's title.");
			SponsorTitle.focus();
			return false;
		}
		else if(trimAll(SponsorOrganization.value)=="")
		{
			alert("Please enter sponsor's organization.");
			SponsorOrganization.focus();
			return false;
		}

		else if(trimAll(SponsorAddress.value)=="")
		{
			alert("Please enter sponsor's address.");
			SponsorAddress.focus();
			return false;
		}
		else if(trimAll(SponsorPhone.value)=="")
		{
			alert("Please enter sponsor's phone number.");
			SponsorPhone.focus();
			return false;
		}

		else if(trimAll(SponsorEmail.value)=="")
		{
			 alert("Please enter sponsor's email address.");
			 SponsorEmail.focus();
			 return false;
		}
		else if(!(validateEmail(trimAll(SponsorEmail.value))))
		{
			alert("Please Enter a valid email address.");
			SponsorEmail.focus();
			return false;
		}

		else if(trimAll(PrimaryContact.value)=="")
		{
			alert("Please enter primary contact. ");
			PrimaryContact.focus();
			return false;
		}
		else if(trimAll(ContactPhone.value)=="")
		{
			alert("Please enter contact phone number.");
			 ContactPhone.focus();
			 return false;
			
		}
		else if(trimAll(ContactEmail.value)=="")
		{
			 alert("Please enter contact's email address");
			 ContactEmail.focus();
			 return false;
		}
		else if(!(validateEmail(trimAll(ContactEmail.value))))
		{
			alert("Please enter a valid email address.");
			ContactEmail.focus();
			return false;
		}
		else if(AgreeToTerms.checked==false)
		{
			alert("You must agree to the terms and conditions");
			AgreeToTerms.focus();
			return false;
		}
		else
		{
			hid_submit.value="yes";
			submit();
			return true;

		}
	  
	}	
}


function trimAll( strValue ) {
 var objRegExp = /^(\s*)$/;
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
    
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function validateEmail( strValue) {
var objRegExp  = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
  return objRegExp.test(strValue);
}

function  validateNumeric( strValue ) {
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
  return objRegExp.test(strValue);
}

function validateUSDate( strValue ) {
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
 
  //check to see if in correct format
  if(!objRegExp.test(strValue))
    return false; //doesn't match pattern, bad date
  else{
    var arrayDate = strValue.split(RegExp.$1); //split date into month, day, year
	var intDay = parseInt(arrayDate[1],10); 
	var intYear = parseInt(arrayDate[2],10);
    var intMonth = parseInt(arrayDate[0],10);
	
	//check for valid month
	if(intMonth > 12 || intMonth < 1) {
		return false;
	}
	
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
  
    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }
		
    //check for February
	var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
    if( ((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0)
      return true; //Feb. had valid number of days
  }
  return false; //any other values, bad date
}

