// JavaScript Document
//Form Validation Functions
function validate_field(field, fieldValue){
	if((fieldValue==null)||(fieldValue == "")){
		document.getElementById(field).setAttribute('class','rejected');
		document.getElementById(field).setAttribute('className', 'rejected');
		return false;
	} else {
		document.getElementById(field).setAttribute('class','accepted');
		document.getElementById(field).setAttribute('className', 'accepted');
	}
}

function validate() {

	if((document.promo.fname.value=='')||(document.promo.fname.value==null)){
    	alert('Please fill in your first name');
		return false;
	}
	if((document.promo.lname.value=='')||(document.promo.lname.value==null)){
    	alert('Please fill in your last name');
		return false;
	}
	if((document.promo.brewery.value=='')||(document.promo.brewery.value==null)){
    	alert('Please fill in your Brewery Name');
		return false;
	}
	if((document.promo.city.value=='')||(document.promo.city.value==null)){
    	alert('Please fill in your City');
		return false;
	}
	if((document.promo.state.value=='')||(document.promo.state.value==null)){
    	alert('Please fill in your State');
		return false;
	}
	if((document.promo.code.value=='')||(document.promo.code.value==null)){
    	alert('Please fill in your Code');
		return false;
	}
	
	if((document.promo.email.value=='')||(document.promo.email.value==null)){
    	alert('Please fill in your E-Mail');
		return false;
	}
	var emailTest = echeck(document.promo.email.value);

	if((emailTest==false)||(emailTest==null)){
    	alert('Please fill in a valid email address');
    	return false;
	}
	if((document.promo.newmalt.value=='')||(document.promo.newmalt.value==null)){
    	alert('Please select your first choice for malt information');
		return false;
	}
		return true;
}




function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
			document.getElementById('email').setAttribute('class','rejected');
			document.getElementById('email').setAttribute('className', 'rejected');
			return false;

		} else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			document.getElementById('email').setAttribute('class','rejected');
			document.getElementById('email').setAttribute('className', 'rejected');
			return false;
		
		} else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			document.getElementById('email').setAttribute('class','rejected');
			document.getElementById('email').setAttribute('className', 'rejected');
			return false;

		} else if (str.indexOf(at,(lat+1))!=-1){
			document.getElementById('email').setAttribute('class','rejected');
			document.getElementById('email').setAttribute('className', 'rejected');
			return false;

		} else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			document.getElementById('email').setAttribute('class','rejected');
			document.getElementById('email').setAttribute('className', 'rejected');
			return false;

		} else if (str.indexOf(dot,(lat+2))==-1){
			document.getElementById('email').setAttribute('class','rejected');
			document.getElementById('email').setAttribute('className', 'rejected');
			return false;

		} else if (str.indexOf(" ")!=-1){
			document.getElementById('email').setAttribute('class','rejected');
			document.getElementById('email').setAttribute('className', 'rejected');
			return false;

		} else {
			document.getElementById('email').setAttribute('class','accepted');
			document.getElementById('email').setAttribute('className', 'accepted');
			return true;
		}
}

function checkPromo(id){
	code = document.getElementById(id).value;
	if(code != ''){
		length = code.length;
		if(length != 4){
			alert("Promo codes must be four digits");
			return false;
		} else {
			var ValidChars = "0123456789.";
			var IsNumber=true;
			var Char;

			for ( i=0; i< code.length && IsNumber == true; i++){
				Char = code.charAt(i);
				if(ValidChars.indexOf(Char) == -1){
					alert("Promo codes must be numberic");
					return false;
				}
			}
		}
	}
}
