// JavaScript Document

function validateForm(thisFormP)
{
	someGood = false;
	firstBadInput = false;
	allGood = true;
	errMessage="";
	

    myInput=document.getElementById("firstName");
	someGood = myInput.value.length>1;
	errMessage+=colorBorder (myInput, someGood, "First name is required \n");
	allGood = allGood && someGood;
	if (!allGood && !firstBadInput) {
		firstBadInput=true;
		myInput.focus();
	}

    myInput=document.getElementById("lastName");
	someGood = myInput.value.length>1;
	errMessage+=colorBorder (myInput, someGood,"Last name is required\n");
	allGood = allGood && someGood;
	if (!allGood && !firstBadInput) {
		firstBadInput=true;
		myInput.focus();
	}

    myInput=document.getElementById("email");
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/
	someGood = emailPattern.test(myInput.value);
	errMessage+=colorBorder (myInput, someGood,"Email must be valid\n");
	allGood = allGood && someGood;
	if (!allGood && !firstBadInput) {
		firstBadInput=true;
		myInput.focus();
	}
	
    myInput2=document.getElementById("confEmail");
	someGood = (myInput2.value==myInput.value);
	errMessage+=colorBorder (myInput2, someGood,"Confirmation email does not match\n");
	allGood = allGood && someGood;
	if (!allGood && !firstBadInput) {
		firstBadInput=true;
		myInput2.focus();
	}

	myInput3=document.getElementById("yyyy");
	myInput2=document.getElementById("mm");
	myInput=document.getElementById("dd");
	var myDOB = Date.parse(myInput3.value+"/"+myInput2.value+"/"+myInput.value);
	someGood=(!isNaN(myDOB) && Date.parse("1992/05/19")>=myDOB);
	
	errMessage+=colorBorder(myInput, someGood,"You must be 18 years old on the day of the concert\n");
	colorBorder(myInput2,someGood,"");
	colorBorder(myInput3,someGood,"");
	allGood = allGood && someGood;
	if (!allGood && !firstBadInput) {
		firstBadInput=true;
		myInput.focus();
	}
		
    myInput=document.getElementById("whichGigLondon");
	myInput2=document.getElementById("whichGigEdinburgh");
	someGood=myInput.checked || myInput2.checked;
	errMessage+=colorBorder(myInput, someGood,"You must pick a gig in London or Edinburgh\n");
	colorBorder(myInput2,someGood,"");
	allGood = allGood && someGood;

    
    myInput=document.getElementById("RSVYes");
	myInput2=document.getElementById("RSVNo");
	someGood=myInput.checked || myInput2.checked;
	errMessage+=colorBorder(myInput, someGood,"You must tell us if you are a Russian Standard Vodka drinker\n");
	colorBorder(myInput2,someGood,"");
	allGood = allGood && someGood;

	
	if (!allGood) {
		alert(errMessage);
	}
	
    return allGood;
}


function validateAge(thisFormP)
{

	var someGood = false;
	var firstBadInput = false;
	var allGood = true;
	var errMessage="";

	var myInput3=document.getElementById("yyyy2");
	var myInput2=document.getElementById("mm2");
	var myInput=document.getElementById("dd2");
		
	var myDOB = Date.parse(myInput3.value+"/"+myInput2.value+"/"+myInput.value);
		
	var someGood=(!isNaN(myDOB) && Date.parse("1992/05/19")>=myDOB);

	errMessage+=colorBorder(myInput, someGood,"You must be 18 years or older to enter this site.\n");
	colorBorder(myInput2,someGood,"");
	colorBorder(myInput3,someGood,"");
	allGood = allGood && someGood;
	
	if (!allGood) {
		alert(errMessage);
	} else {
    	document.getElementById('modal').style.visibility='hidden';
		document.getElementById('dd').value = myInput.value;
		document.getElementById('mm').value = myInput2.value;
		document.getElementById('yyyy').value = myInput3.value;
	}
	
	return allGood;

}


function colorBorder (myInputP, someGoodP, errMessageP) {
	if (someGoodP) {
		myInputP.style.backgroundColor="#AAA";		
		return "";
    } else {
		myInputP.style.backgroundColor="#981E32";	
		return errMessageP;
	}
}
