function notEmpty(input, name){
	//take care of select form objects
	var inputFocus = input;
	
	if(input.type == "select-one")
		 input = input.options[input.selectedIndex];
	
	//check if empty
	if(input.value == "" || input.value == null) {
		alert( name + " must contain a value.");
		inputFocus.focus();
		return false;
	}
	return true;
}

function isEmpty(input){
	//take care of select form objects
	var inputFocus = input;
	
	if(input.type == "select-one")
		 input = input.options[input.selectedIndex];
	
	//check if empty
	if(input.value == "" || input.value == null) {
		//alert( input.value + " must contain a value.");
		//inputFocus.focus();
		return true;
	}
	return false;
}



function checkEmail(input){
	//take care of select form objects
	var inputFocus = input;
	
	if(input.type == "select-one")
		 input = input.options[input.selectedIndex];
	
	//check if empty

	if(input.value.match("@") != null){
		return true;
	}
	else{
		alert("Email address does not contain an '@' symbol");
		return false;
	}
}
function checkSubs(form){
		if((form.unsubscribe_all.checked) && (form.unsubscribe_notes_newbies.checked || form.unsubscribe_notes_how.checked || form.unsubscribe_news.checked ||	form.unsubscribe_promo.checked || form.unsubscribe_product_microphones.checked || form.unsubscribe_product_wireless.checked || form.unsubscribe_product_psm.checked || form.unsubscribe_product_headphones.checked || form.unsubscribe_product_earphones.checked || form.unsubscribe_events.checked)){
			alert("You have choosen to unsubscribe from all Shure communications AND selected to receive one or more email communications.  Please uncheck 'I do not want to receive any communications from Shure' if you wish to receive email communcations from Shure.");
				  return false;
			
		}
		else{
			return true;	
		}
	
}


function checkDate(date, name){
	if(date.value != null && date.value != ""){

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
	var matchArray = date.value.match(datePat);
	
	if(matchArray == null){
			alert("All Date fields should be in the form: ##/##/####");
			return false;
	}
	month = matchArray[1]; // parse date into variables
	day = matchArray[3];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
	alert("Month must be between 1 and 12.");
	return false;
	}
	if (day < 1 || day > 31) {
	alert("Day must be between 1 and 31.");
	return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	alert("Month "+month+" doesn't have 31 days!")
	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("February " + year + " doesn't have " + day + " days!");
	return false;
    }}
	}
	else{
		//alert( name + " must contain a value.");
		//date.focus();
		//return false;
	
	}
	return true;  // date is valid
}



function checkData(form){
	
	if(checkSubs(form)){
	if(form.unsubscribe_all.checked){
		if(notEmpty(form.emailAddress, "Email")){ 
		if(checkEmail(form.emailAddress)){ 
			if(form.emailAddress.value != form.emailAddressconfirm.value){
				alert("Email Address and Email Address Confirmation do not match.");
				form.emailAddress.focus();
				return false; 
			
			}
			
			else {
				return true;
			}
		
		} //if(checkEmail(form.emailAddress)){ 
		}
		return false;
	}else{
	
	if(notEmpty(form.firstname, "First Name")){ 
	if(notEmpty(form.lastname, "Last Name")){ 
	if(notEmpty(form.emailAddress, "Email")){ 
	if(checkEmail(form.emailAddress)){ 
	//if (checkDate(form.modelDate, "Date Purchased")){
	if (checkDate(form.birthDate, "Date of Birth")){

	//alert("countryCheck-country="+form.country.options[form.country.selectedIndex].value+"state="+form.state.options[form.state.selectedIndex].value+ "postalcode="+form.postal_code.value);
		if(form.country.options[form.country.selectedIndex].value == "United States" && (form.postal_code.value == "" )  ) {
			alert("Please fill in zipcode.");
			form.postal_code.focus();
			return false; 
		}else{
			
			if(form.emailAddress.value != form.emailAddressconfirm.value){
				alert("Email Address and Email Address Confirmation do not match.");
				form.emailAddress.focus();
				return false; 
			
			}
			
		else {
		
		
			return true;
			}
		}
		//return true;

	}//if (checkDate(form.birthDate)){
	//}//if (checkDate(form.modelDate)){
	} //if(checkEmail(form.emailAddress)){ 
	}//if(notEmpty(form.emailAddress, "Email")){ 
	}//if(notEmpty(form.lastname, "Last Name"))
	}//if(notEmpty(form.firstname, "First Name"))
	return false;
	}//end if(form.unsubscribe_all.checked){
	} //checkSubs()
	else{
		return false;	
	}
}//function checkData

function unCheck(form){
		if (form.unsubscribe_all.checked){
			form.unsubscribe_notes_newbies.checked = false;
			form.unsubscribe_notes_how.checked = false;
			form.unsubscribe_news.checked = false;
			form.unsubscribe_otws.checked = false;
			form.unsubscribe_promo.checked = false;
			form.unsubscribe_product.checked = false;
			form.unsubscribe_events.checked = false;
			
		}
			
}

