 // ------------------------------------------------------------------------
 // Javascript code copyright 2009 by Fiach Reid : www.webtropy.com
 // This code may be used freely, as long as this copyright notice is intact.
 function Calculate(Luhn)
 {
    var sum = 0;
    for (i=0; i<Luhn.length; i++ )
    {
		sum += parseInt(Luhn.substring(i,i+1));
    }
	var delta = new Array (0,1,2,3,4,-4,-3,-2,-1,0);
	for (i=Luhn.length-1; i>=0; i-=2 )
    {		
		var deltaIndex = parseInt(Luhn.substring(i,i+1));
		var deltaValue = delta[deltaIndex];	
		sum += deltaValue;
	}	
	var mod10 = sum % 10;
	mod10 = 10 - mod10;	
	if (mod10==10)
	{		
		mod10=0;
	}
	return mod10;
 }

 function Validate(Luhn)
 {
	var LuhnDigit = parseInt(Luhn.substring(Luhn.length-1,Luhn.length));
	var LuhnLess = Luhn.substring(0,Luhn.length-1);
	if (Calculate(LuhnLess)==parseInt(LuhnDigit))
	{
		return true;
	}	
	return false;
 }
//-----------------------------------------------------------------------------
function luhn_check(card_number)
{
	return Validate(card_number);
}

function validate_form()
{

	if (document.myform.activity.value == "")
	{
		alert("Please Choose an Activity");
		document.myform.activity.focus();
		return false;
	}
	
	if (document.myform.program.value == "")
	{
		alert("Please Choose an Program");
		document.myform.program.focus();
		return false;
	}

	if (document.myform.adult_first_name.value == "")
	{
		alert("Please Enter A First Name");
		document.myform.adult_first_name.focus();
		return false;
	}
	
	if (document.myform.adult_last_name.value == "")
	{
		alert("Please Enter A Last Name");
		document.myform.adult_last_name.focus();
		return false;
	}
	
	if (document.myform.address.value == "")
	{
		alert("Please Enter An Address");
		document.myform.address.focus();
		return false;
	}
	
	if (document.myform.city.value == "")
	{
		alert("Please Enter A City");
		document.myform.city.focus();
		return false;
	}
	
	if (document.myform.zip.value == "")
	{
		alert("Please Enter A Zip");
		document.myform.zip.focus();
		return false;
	} else if (!/^[0-9]{5}(-[0-9]{4})?$/.test(document.myform.zip.value)) {
            alert("Please correct ZIP code.");
            document.myform.zip.focus();
            return false;
        }

	if (document.myform.phone.value == "")
	{
		alert("Please Enter A Phone");
		document.myform.phone.focus();
		return false;
	}
	else
	{
		var valPhone = new Boolean("");
		valPhone = validatePhoneNumber(document.myform.phone.value);
		//alert("I'm testing the phone number "+valPhone);
		if (valPhone == false)
		{
			alert("Please Enter A Valid Phone Number");
			document.myform.phone.focus();
			return false;
		}
	}
	
	if (document.myform.cell1.value != "")
	{
		var temp = document.myform.cell1.value;
		temp = numbersOnly(temp);
		document.myform.cell1.value = temp;
	}
	
	if (document.myform.youth_phone.value != "")
	{
		var temp = document.myform.youth_phone.value;
		temp = numbersOnly(temp);
		document.myform.youth_phone.value = temp;
	}
	
	if (document.myform.em_phone.value != "")
	{
		var temp = document.myform.em_phone.value;
		temp = numbersOnly(temp);
		document.myform.em_phone.value = temp;
	}
	
	if (document.myform.doc_phone.value != "")
	{
		var temp = document.myform.doc_phone.value;
		temp = numbersOnly(temp);
		document.myform.doc_phone.value = temp;
	}
	
	if (document.myform.card_num.value == "")
	{
		alert("Please Enter A Credit Card Number");
		document.myform.card_num.focus();
		return false;
	} else if (!/^[0-9]{16}$/.test(numbersOnly(document.myform.card_num.value))) {
            alert("Please check credit card number.");
            document.myform.card_num.focus();
            return false;
        } else if (!luhn_check(numbersOnly(document.myform.card_num.value))) {
            alert("Please check credit card number.");
            document.myform.card_num.focus();
            return false;
        }
	
	if (document.myform.ccv.value == "")
	{
		alert("Please Enter A Card Code Verification Number");
		document.myform.ccv.focus();
		return false;
	} else if (!/(^[0-9]{3}$)|(^[0-9]{4}$)/.test(document.myform.ccv.value)) {
            alert("Please Check your Card Code Verification Number");
            document.myform.ccv.focus();
            return false;
        }
	
	if (document.myform.exp_month.value == "")
	{
		alert("Please Select An Expiration Month");
		document.myform.exp_month.focus();
		return false;
	}
	
	if (document.myform.exp_year.value == "")
	{
		alert("Please Select An Expiration Year");
		document.myform.exp_year.focus();
		return false;
	}
	
	if (document.myform.email.value == "")
	{
		alert("Please Enter A Email Address");
		document.myform.email.focus();
		return false;
	}
	else if (!validateEmail())
	{
		alert("Please Enter A Valid Email Address");
		document.myform.email.focus();
		return false;
	}
	
	if (document.myform.youth.checked === true)
	{
		if (document.myform.youth_name.value === "")
		{
			alert("Please Enter a Youth Name");
			document.myform.youth_name.focus();
			return false;
		}
		
		if (document.myform.gender.value === "")
		{
			alert("Please Select a Gender");
			document.myform.gender.focus();
			return false;
		}
		
	}

	if (document.myform.legal1.checked == false)
	{
		alert("You must agree to emergency medical treatment.");
		return false;
	}
	
	if (document.myform.legal2.checked == false)
	{
		alert("You must agree to injury risk.");
		return false;
	}
	
	
}

function validatePhoneNumber(elementValue)
{ 
	elementValue = numbersOnly(elementValue);
    var regex = /^(?:\(?([0-9]{3})\)?[-. ]?)?([0-9]{3})[-. ]?([0-9]{4})$/;

    if (regex.test(elementValue)) 
	{
        // Valid phone number
		//alert("This phone was good");
    }
	else 
	{
        // Invalid phone number
		//alert("This phone was BAD!");
    }
	
	return regex.test(elementValue);  
}

function validateEmail()
{
	var x=document.myform.email.value;
	var atpos=x.indexOf("@");
	var dotpos=x.lastIndexOf(".");
	if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
	{
		return false;
	}
	else
	{
		return true;
	}
}

function check_youth()  
{  
	//alert(document.getElementById("youth").checked);
	if (document.getElementById("youth").checked == false)
	{
        document.getElementById("youth_info").style.display = "none";
		document.myform.youth_name.value = "";
		document.myform.gender.value = "";
		document.myform.youth_phone.value = "";
		document.myform.school_name.value = "";
		document.myform.grade.value = "";
		document.myform.birth_month.value = "";
		document.myform.birth_day.value = "";
		document.myform.birth_year.value = "";
	}
	else if (document.getElementById("youth").checked == true)
	{
		document.getElementById("youth_info").style.display = "inline";
	}
  
}

function display_program()  
{  
	//alert("I'm HERE!");
	var pro = document.getElementById("program").options;
	var j = pro.length;
	var test = 0;
	for (i=j-1;i>=0;i--)
	{
		pro.remove(i);
	}
	document.getElementById("es_cost").value = "0.00";
	var choices = document.getElementById("program");
	if (document.getElementById("activity").value == "basketball")
	{
		document.getElementById("program_drop").style.display = "block";
		var option = document.createElement("option");
		option.text="Select One";
		option.value="";
		option.selected=true;
		option.disabled=true;
		choices.add(option, null);
		
		option = document.createElement("option");
		option.text="Mens League";
		option.value="mens_league";
		choices.add(option, null);
		
		option = document.createElement("option");
		option.text="NKYA Youth Individual";
		option.value="nkya_youth";
		choices.add(option, null);
		
		option = document.createElement("option");
		option.text="Youth Basketball Tournament";
		option.value="bball_tourney";
		choices.add(option, null);
		
		option = document.createElement("option");
		option.text="Youth League";
		option.value="youth_league";
		choices.add(option, null);
		
		option = document.createElement("option");
		option.text="SW Ohio";
		option.value="sw_ohio";
		choices.add(option, null);
	}
	else if (document.getElementById("activity").value == "volleyball")
	{
		document.getElementById("program_drop").style.display = "block";
		
		var option = document.createElement("option");
		option.text="Select One";
		option.value="";
		option.selected=true;
		option.disabled=true;
		choices.add(option, null);
		
		option = document.createElement("option");
		option.text="Adult Co-ed 6 Player";
		option.value="adult_co_6team";
		choices.add(option, null);
		
		option = document.createElement("option");
		option.text="Adult Co-ed 4 Player";
		option.value="adult_co_4team";
		choices.add(option, null);
		
		option = document.createElement("option");
		option.text="Adult Individual";
		option.value="adult_ind";
		choices.add(option, null);
		
		option = document.createElement("option");
		option.text="Youth Individual";
		option.value="youth_ind";
		choices.add(option, null);
		
	}
	else if (document.getElementById("activity").value == "soccer")
	{
		document.getElementById("program_drop").style.display = "block";
		
		var option = document.createElement("option");
		option.text="Select One";
		option.value="";
		option.selected=true;
		option.disabled=true;
		choices.add(option, null);
		
		option = document.createElement("option");
		option.text="Adult Co-ed Team Wed. Nights";
		option.value="adult_co_team_wed";
		choices.add(option, null);
		
		option = document.createElement("option");
		option.text="Adult Co-ed Individual Wed. Nights";
		option.value="adult_co_ind_wed";
		choices.add(option, null);
		
		option = document.createElement("option");
		option.text="NKYA Youth Individual";
		option.value="nkya_youth";
		choices.add(option, null);
		
		option = document.createElement("option");
		option.text="Adult Co-ed Team Fri. Nights";
		option.value="adult_co_team_fri";
		choices.add(option, null);
		
		option = document.createElement("option");
		option.text="Adult Co-ed Individual Fri. Nights";
		option.value="adult_co_ind_fri";
		choices.add(option, null);

	}
	else if (document.getElementById("activity").value == "cheer")
	{
		document.getElementById("program_drop").style.display = "block";
	
		option = document.createElement("option");
		option.text="NKYA Rec Program";
		option.value="nkya_rec";
		choices.add(option, null);
		
	}
	else if (document.getElementById("activity").value == "flag_football")
	{
		document.getElementById("program_drop").style.display = "block";
		
		option = document.createElement("option");
		option.text="Individual Registration";
		option.value="reg_ind";
		choices.add(option, null);
		
	}
	display_price()
}

function display_price()  
{  
	//alert("I'm HERE!");
	if (document.getElementById("activity").value == "basketball")
	{
		if (document.getElementById("program").value == "mens_league")
		{
			document.getElementById("es_cost").value = "200.00";
		}
		else if (document.getElementById("program").value == "bball_tourney")
		{
			document.getElementById("es_cost").value = "125.00";
		}
		else if (document.getElementById("program").value == "youth_league")
		{
			document.getElementById("es_cost").value = "250.00";
		}
		else if (document.getElementById("program").value == "nkya_youth")
		{
			document.getElementById("es_cost").value = "85.00";
		}
		else if (document.getElementById("program").value = "sw_ohio")
		{
				document.getElementById("es_cost").value = "125.00";
		}
			
	}
	else if (document.getElementById("activity").value == "volleyball")
	{
		if (document.getElementById("program").value == "adult_co_6team")
		{
			document.getElementById("es_cost").value = "275.00";
		}
		else if (document.getElementById("program").value == "adult_co_4team")
		{
			document.getElementById("es_cost").value = "190.00";
		}
		else if (document.getElementById("program").value == "adult_ind")
		{
			document.getElementById("es_cost").value = "55.00";
		}
		else if (document.getElementById("program").value == "youth_ind")
		{
			document.getElementById("es_cost").value = "90.00";
		}
		
	}
	else if (document.getElementById("activity").value == "soccer")
	{
		if (document.getElementById("program").value == "adult_co_team_wed")
		{
			document.getElementById("es_cost").value = "675.00";
		}
		else if (document.getElementById("program").value == "adult_co_ind_wed")
		{
			document.getElementById("es_cost").value = "75.00";
		}
		else if (document.getElementById("program").value == "adult_co_team_fri")
		{
			document.getElementById("es_cost").value = "525.00";
		}
		else if (document.getElementById("program").value == "adult_co_ind_fri")
		{
			document.getElementById("es_cost").value = "65.00";
		}
		else if (document.getElementById("program").value == "nkya_youth")
		{
			document.getElementById("es_cost").value = "80.00";
		}
		
	}
	else if (document.getElementById("activity").value == "cheer")
	{
		
		if (document.getElementById("program").value == "nkya_rec")
		{
			document.getElementById("es_cost").value = "75.00";
		}
				
	}
	else if (document.getElementById("activity").value == "flag_football")
	{
		
		if (document.getElementById("program").value == "reg_ind")
		{
			document.getElementById("es_cost").value = "90.00";
		}
				
	}
	
}

function init()  
{  
	// alert("I'm HERE!");
	document.getElementById("youth_info").style.display = "none";
	document.getElementById("program_drop").style.display = "none";
	document.myform.card_num.value = "";
	document.myform.ccv.value = "";
	display_program();

}

function numbersOnly(number)  
{  
	// alert("I'm HERE!");
	number.replace(/[^1234567890]/gi,"");
	return number;
}
