var count=1;
var NumberOfInputFields = 100;
var number_of_competitors;

function add_row()
{
	tab = document.getElementById("table_competitors");
	
	for(row_id=1; row_id<=NumberOfInputFields; row_id++)
	{
		var new_row = tab.insertRow(row_id);
		//new_row.innerHTML = row_id;
		for(column_id=0; column_id<7; column_id++)
		{
			new_cell = new_row.insertCell(column_id);
			//new_cell.innerHTML = row_id + "," + column_id;
			if( column_id == 0)
			{
				new_cell.innerHTML = row_id+".";
				new_cell.className = "stronger";
			}
			else if( column_id == 1 )
			{
				var inputElm = document.createElement("input");
				inputElm.setAttribute("id", "data_FirstName_"+row_id);
				inputElm.setAttribute("name", "data_FirstName_"+row_id);
				inputElm.setAttribute("type", "text");
				inputElm.setAttribute("size", "20");
				new_cell.appendChild(inputElm);
			}
			else if( column_id == 2 )
			{
				var inputElm = document.createElement("input");
				inputElm.setAttribute("id", "data_LastName_"+row_id);
				inputElm.setAttribute("name", "data_LastName_"+row_id);
				inputElm.setAttribute("type", "text");
				inputElm.setAttribute("size", "20");
				new_cell.appendChild(inputElm);
			}
			else if( column_id == 3 )
			{
				var new_Select = document.createElement("select");
				var newOption_youth = document.createElement("option");
				var newOption_junior = document.createElement("option");
				var newOption_adult = document.createElement("option");
				new_Select.add(newOption_youth,null);
				new_Select.add(newOption_junior,null);
				new_Select.add(newOption_adult,null);
				newOption_youth.innerHTML = "youth";
				newOption_youth.selected = "selected";
				newOption_junior.innerHTML = "junior";
				newOption_adult.innerHTML = "adult";
				new_Select.setAttribute("id", "data_age_"+row_id);
				new_Select.setAttribute("name", "data_age_"+row_id);
				//new_Select.setAttribute("onchange", "justtotry("+row_id+")");
				new_Select.setAttribute("onchange", "determine_categories("+row_id+")");
				new_cell.appendChild(new_Select);
			}
			else if( column_id == 4 )
			{
				var newSelect = document.createElement("select");
				var newOption_male = document.createElement("option");
				var newOption_female = document.createElement("option");
				newSelect.add(newOption_male,null);
				newSelect.add(newOption_female,null);
				newOption_male.innerHTML = "male";
				newOption_male.selected = "selected";
				newOption_female.innerHTML = "female";
				newSelect.setAttribute("id", "data_gender_"+row_id);
				newSelect.setAttribute("name", "data_gender_"+row_id);
				newSelect.setAttribute("onchange", "determine_categories("+row_id+")");
				new_cell.appendChild(newSelect);
			}
			else if( column_id == 5 )
			{
				var newSelect = document.createElement("select");
				newSelect.setAttribute("id", "data_pattern_"+row_id);
				newSelect.setAttribute("name", "data_pattern_"+row_id);
				new_cell.appendChild(newSelect);
			}
			else if( column_id == 6 )
			{
				var newSelect = document.createElement("select");
				newSelect.setAttribute("id", "data_sparing_"+row_id);
				newSelect.setAttribute("name", "data_sparing_"+row_id);
				new_cell.appendChild(newSelect);
				determine_categories(row_id);
			}
		}
	}
}

function determine_categories(row_id)
{
	
	sp = document.getElementById("data_sparing_"+row_id);
	pt = document.getElementById("data_pattern_"+row_id);
	//age = document.getElementById("data_age_"+row_id).value;
	age = document.getElementById("data_age_"+row_id).selectedIndex;
	//gen = document.getElementById("data_gender_"+row_id).value;
	gen = document.getElementById("data_gender_"+row_id).selectedIndex;
	
	for(i=0; i<9; i++)
	{
		pt.remove(8-i);
		sp.remove(8-i);
	}
	
	pt.add(new Option("---", ""), null);
	pt.add(new Option("yellow belt (VIII and VII Gup)", "yellow"), null);
	pt.add(new Option("green belt (VI and V Gup)", "green"), null);
	pt.add(new Option("blue belt (IV and III Gup)", "blue"), null);
	pt.add(new Option("red belt (II and I Gup)", "red"), null);
	pt.add(new Option("black belt, I Dan", "I"), null);
	
	sp.add(new Option("---", ""), null);
	
	//if ( age == "youth")
	if ( age == 0)
	{
		sp.add(new Option("-25", "-25"), null);
		sp.add(new Option("-30", "-30"), null);
		sp.add(new Option("-35", "-35"), null);
		sp.add(new Option("-40", "-40"), null);
		sp.add(new Option("-45", "-45"), null);
		sp.add(new Option("-50", "-50"), null);
		sp.add(new Option("-55", "-55"), null);
		sp.add(new Option("+55", "+55"), null);
		
	}
	//else if( age == "junior" & gen == "male" )
	else if( age == 1 & gen == 0 )
	{
		sp.add(new Option("-45", "-45"), null);
		sp.add(new Option("-51", "-51"), null);
		sp.add(new Option("-57", "-57"), null);
		sp.add(new Option("-63", "-63"), null);
		sp.add(new Option("-69", "-69"), null);
		sp.add(new Option("-75", "-75"), null);
		sp.add(new Option("+75", "+75"), null);
		pt.add(new Option("black belt, II Dan", "II"), null);
	}
	//else if( age == "adult" & gen == "male")
	else if( age == 2 & gen == 0)
	{
		//sp.add(new Option("-50", "-50"), null);
		//sp.add(new Option("-57", "-57"), null);
		//sp.add(new Option("-64", "-64"), null);
		sp.add(new Option("-71", "-71"), null);
		sp.add(new Option("-78", "-78"), null);
		sp.add(new Option("-85", "-85"), null);
		sp.add(new Option("+85", "+85"), null);
		pt.add(new Option("black belt, II Dan", "II"), null);
		pt.add(new Option("black belt, III Dan", "III"), null);
	}
	//else if( age == "junior" & gen == "female" )
	else if( age == 1 & gen == 1 )
	{
		sp.add(new Option("-40", "-40"), null);
		sp.add(new Option("-46", "-46"), null);
		sp.add(new Option("-52", "-52"), null);
		sp.add(new Option("-58", "-58"), null);
		sp.add(new Option("-64", "-64"), null);
		sp.add(new Option("-70", "-70"), null);
		sp.add(new Option("+70", "+70"), null);
		pt.add(new Option("black belt, II Dan", "II"), null);
	}
	//else if( age == "adult" & gen == "female")
	else if( age == 2 & gen == 1)
	{
		sp.add(new Option("-45", "-45"), null);
		sp.add(new Option("-51", "-51"), null);
		sp.add(new Option("-57", "-57"), null);
		sp.add(new Option("-63", "-63"), null);
		sp.add(new Option("-69", "-69"), null);
		sp.add(new Option("-75", "-75"), null);
		sp.add(new Option("+75", "+75"), null);
		pt.add(new Option("black belt, II Dan", "II"), null);
		pt.add(new Option("black belt, III Dan", "III"), null);
	}
	else
	{
		//alert( "Please choose Age and Gender first." );
	}

}


function form_val()
{
	number_of_competitors = 0;
	
	var Team = document.getElementById("data_team");
	if( (Team.value == "") || (Team.value == null ) )
	{
		alert( "Please enter the name of your club/team." );
		Team.focus();
		return false;
	}
	
	var Counry = document.getElementById("data_country");
	if( (Counry.value == "")||(Counry.value == null) )
	{
		alert( "Please select the country you are coming from." );
		Counry.focus();
		return false;
	}
	
	var Coach = document.getElementById("data_coach");
	if( (Coach.value == null)||(Coach.value == "") )
	{
		alert( "Please enter the full name of the responsible person/coach." );
		Coach.focus();
		return false;
	}
	
	var Phone = document.getElementById("data_phone");
	if ( (Phone.value==null)||(Phone.value=="") )
	{
		alert("Please Enter the responsible person's phone number");
		Phone.focus();
		return false;
	}
	else if (checkInternationalPhone(Phone.value)==false)
	{
		alert("Please enter a valid phone number (e.g. +381 67 77 999 33)");
		Phone.value="";
		Phone.focus();
		return false
	}

	for(cnt=1;cnt<=NumberOfInputFields;cnt++)
	{
		fn = document.getElementById("data_FirstName_" + cnt);
		ln = document.getElementById("data_LastName_" + cnt);
		s = document.getElementById("data_sparing_" + cnt);
		p = document.getElementById("data_pattern_" + cnt);
		
		if( (fn.value != "") && (ln.value != "") )
		{
			
			if( s.value == "" && p.value == "")
			{
				alert( "Competitor number " + cnt + " is not applied neither for patterns nor sparing!" );
				return false;
			}
			if( s.value != "" || p.value != "")
			{
				number_of_competitors++;
			}
		}
	}
//	alert( number_of_competitors );
	if( number_of_competitors == 0)
	{
		alert("Please apply at least one competitor.");
		document.getElementById("data_FirstName_1").focus();
		return false;
	}
	
	return true;

}



// Declaring required variables
var digits = "0123456789";

// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";

// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";

// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{
	var i;
	for (i = 0; i < s.length; i++)
	{
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	
	// All characters are numbers.
	return true;
}

function trim(s)
{
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not a whitespace, append to returnString.
	for (i = 0; i < s.length; i++)
	{
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (c != " ") returnString += c;
	}
	return returnString;
}

function stripCharsInBag(s, bag)
{
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++)
	{
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function checkInternationalPhone(strPhone)
{
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
