// Set this to true if the first item should be selected by default. Else, set this to false.
var setSelected = true;

// Error messages to display if an invalid operation is performed.
var addErrorMsg = "You must select at least one player to add.";
var removeErrorMsg = "You should first select a player to remove.";
var moveErrorMsg = "You should select at least one player to move.";
var duplicateErrMsg = "The following player(s) already appear(s) in your Top-11 list: ";

// Set this to true if a selection should be reset once the item is added.
var resetSelection = true;

// The parent, child and submit window sizes.
var parentN = 8;
var childN = 15;
var submitN = 11;

var childlistname = 'conf';

var parentValues = new Array("Chennai Superkings","Deccan Chargers (Hyderabad)","Delhi Daredevils","King X1 Punjab (Mohali)","Kolkata Knight Riders","Mumbai Indians","Rajasthan Royals","Royal Challengers (Bangalore)");
var childValues = new Array();
childValues[0] = new Array("Abhinav Mukund (IND)","Albie Morkel (SA)","Arun Karthik (IND)","Jacob Oram (NZ)","Joginder Sharma (IND)","Lakshmipathy Balaji(IND)","Mahendra Singh Dhoni (IND)","Makhaya Ntini (SA)","Manpreet Gony (IND)","Matthew Hayden (AUS)","Michael Hussey (AUS)","Muttiah Muralitharan (SL)","Napolean Einstien(IND)","Palani Amarnath (IND)","Parthiv Patel (IND)","R Ashwin (IND)","Shadab Jakati (IND)","Srikkanth Anirudha(IND)","Stephen Fleming (NZ)","Subramaniam Badrinath (IND)","Suresh Kumar (IND)","Suresh Raina (IND)","Vidyut Sivaramakrishnan (IND)","Viraj Kabde (IND)");
childValues[1] = new Array("Adam Gilchrist (AUS)","Andrew Symonds (AUS)","Arjun Yadav (IND)","Chamara Silva (SL)","Chaminda Vaas (SL)","D Kalyankrishna (IND)","Doddapaneni Kalyankrishna (IND)","Dwaraka Ravi Teja (IND)","Dwarka Ravi Teja (IND)","Halhadar Das (IND)","Herschelle Gibbs (SA)","Kalyan Krishna (IND)","Nuwan Zoysa (SL)","Paidikalva Vijaykumar (IND)","PM Sarvesh Kumar (IND)","Pragyan Ojha (IND)","Rohit Sharma (IND)","RP Singh (IND)","Sanjay Bangar (IND)","Sarveesh kumar (IND)","Scott Styris (NZ)","Shahid Afridi (PAK)","Venugopal Rao (IND)","VVS Laxman (IND)");
childValues[2] = new Array("AB de Villiers (SA)","Amit Mishra (IND)","Brett Geeves (AUS)","Daniel Vettori (NZ)","Dinesh Karthik (Ind)","Farveez Maharoof (SL)","Gautam Gambhir (IND)","Glenn McGrath (AUS)","Manoj Tiwary (IND)","Mayank Tehlan (IND)","Mithun Manhas (IND)","Mohammad Asif (PAK)","Pradeep Sangwan (IND)","Rajat Bhatiya (IND)","Shikhar Dhawan (IND)","Shoaib Malik (PAK)","Tejashwi Yadav (IND)","Tillakaratne Dilshan (SL)","Virender Sehwag (IND)","Yo Mahesh(IND)","Yogesh Nagar (IND)");
childValues[3] = new Array("Ajitesh Agral (IND)","Brett Lee (AUS)","Irfan Pathan (IND)","James Hopes (AUS)","Karan Goel (IND)","Karan Goel (IND)","Kumar Sangakkara (SL)","Kyle Mills (NZ)","Luke Pomersbach (AUS)","Mahela Jayawardene (SL)","Nitin Saini (IND)","Pankaj Dharmani (IND)","Piyush Chawla (IND)","Ramesh Powar (IND)","Ramnaresh Sarwan (WI)","Rishi Dhawan (IND)","Sahil Kukreja (IND)","Shantha Sreesanth (IND)","Shaun Marsh (AUS)","Simon Katich (AUS)","Sunny Sohal (IND)","Tanmay Srivastava (IND)","Uday Kaul (IND)","VRV Singh (IND)","Wilkin Mota (IND)","Yuvraj Singh (IND)");
childValues[4] = new Array("Ajit Agarkar (IND)","Akash Chopda (IND)","Brendon McCullum (NZ)","Chetashwar Pujara (IND)","Chris Gayle (WI)","David Hussey (AUS)","Debabrata Das (IND)","Iqbal Abdullah (IND)","Ishant Sharma (IND)","Lakshmi Ratan Shukla (IND)","Mohammad Hafeez (PAK)","Murali Kartik (IND)","Prasanta Saha (IND)","Ranadeb Bose (IND)","Ricky Ponting (AUS)","Rohan Banerjee (IND)","Shoaib Akhtar (PAK)","Sidhartha Kaul (IND)","Sourashish Lahiri (IND)","Sourav Ganguly (IND)","Tatenda Taibu (ZIM)","Umar Gul (PAK)","Yashpal Singh(IND)");
childValues[5] = new Array("Abhishek Nayar (IND)","Ajinkya Rahane (IND)","Ankeet Chavan (IND)","Ashish Nehra (IND)","Ashwell Prince (SA)","Dhawal Kulkarni (IND)","Dilhara Fernando (SL)","Dominic Thornely (AUS)","Gaurav Dhiman (IND)","Harbhajan Singh (IND)","Lasith Malinga (SL)","Loots Bosman (SA)","Luke Ronchi (AUS)","Manish Pandey (IND)","Musavir Khote(IND)","Pinal Shah (IND)","Rajesh Pawar (IND)","Robin Uthappa (IND)","Rohan Raje (IND)","Sachin Tendulkar (IND)","Sanath Jayasuriya (SL)","Saurabh Tiwary (IND)","Shaun Pollock (SA)","Siddharth Chitnis (IND)","Swapnil Singh(IND)","Vikrant Yeligati (IND)","Yogesh Takawale (IND)");
childValues[6] = new Array("Aditya Angle (IND)","Anup Revandkar (IND)","Darren Lehmann (AUS)","Dinesh Salunke (IND)","Dmitri Mascarenhes (ENG)","Graeme Smith (SA)","Jaydev Shah (IND)","Justin Langer (AUS)","Kamran Akmal (PAK)","Mahesh Rawat","Mohammad Kaif (IND)","Morne Morkel (SA)","Munaf Patel (IND)","Niraj Patel (IND)","Pankaj Singh (IND)","Parag More (IND)","Ravindar Jadega (IND)","Shane Warne (AUS)","Shane Watson (AUS)","Siddharth Trivedi (IND)","Sohail Tanveer (Pak)","Sumit Khatri (IND)","Swapnil Asnodkar (IND)","Taruvar Kohli (IND)","Younis Khan (PAK)","Yusuf Pathan (IND)");
childValues[7] = new Array("Abdur Razzak (BAN)","Anil Kumble (IND)","B Akhil (IND)","Bharat Chipli(IND)","Cameron White (AUS)","Dale Steyn (SA)","Devraj Patil (IND)","J Arun kumar (IND)","Jacques Kallis (SA)","K P Appanna (IND)","Mark Boucher (SA)","Misbah-ul-Haq (PAK)","Nathan Bracken (AUS)","Praveen Kumar (IND)","Rahul Dravid (IND)","Ross Taylor (NZ)","S Goswami(IND)","S Johil (IND)","Shivnarine Chanderpaul (WI)","V Kohli (IND)","Vinay Kumar(IND)","Wasim Jaffer (IND)","Zaheer Khan (IND)");

var allValues = new Array("AB de Villiers (SA)","Abdur Razzak (BAN)","Abhinav Mukund (IND)","Abhishek Nayar (IND)","Adam Gilchrist (AUS)","Aditya Angle (IND)","Ajinkya Rahane (IND)","Ajit Agarkar (IND)","Ajitesh Agral (IND)","Akash Chopda (IND)","Albie Morkel (SA)","Amit Mishra (IND)","Andrew Symonds (AUS)","Anil Kumble (IND)","Ankeet Chavan (IND)","Anup Revandkar (IND)","Arjun Yadav (IND)","Arun Karthik (IND)","Ashish Nehra (IND)","Ashwell Prince (SA)","B Akhil (IND)","Bharat Chipli(IND)","Brendon McCullum (NZ)","Brett Geeves (AUS)","Brett Lee (AUS)","Cameron White (AUS)","Chamara Silva (SL)","Chaminda Vaas (SL)","Chetashwar Pujara (IND)","Chris Gayle (WI)","D Kalyankrishna (IND)","Dale Steyn (SA)","Daniel Vettori (NZ)","Darren Lehmann (AUS)","David Hussey (AUS)","Debabrata Das (IND)","Devraj Patil (IND)","Dhawal Kulkarni (IND)","Dilhara Fernando (SL)","Dinesh Karthik (Ind)","Dinesh Salunke (IND)","Dmitri Mascarenhes (ENG)","Doddapaneni Kalyankrishna (IND)","Dominic Thornely (AUS)","Dwaraka Ravi Teja (IND)","Dwarka Ravi Teja (IND)","Farveez Maharoof (SL)","Gaurav Dhiman (IND)","Gautam Gambhir (IND)","Glenn McGrath (AUS)","Graeme Smith (SA)","Halhadar Das (IND)","Harbhajan Singh (IND)","Herschelle Gibbs (SA)","Iqbal Abdullah (IND)","Irfan Pathan (IND)","Ishant Sharma (IND)","J Arun kumar (IND)","Jacob Oram (NZ)","Jacques Kallis (SA)","James Hopes (AUS)","Jaydev Shah (IND)","Joginder Sharma (IND)","Justin Langer (AUS)","K P Appanna (IND)","Kalyan Krishna (IND)","Kamran Akmal (PAK)","Karan Goel (IND)","Karan Goel (IND)","Kumar Sangakkara (SL)","Kyle Mills (NZ)","Lakshmi Ratan Shukla (IND)","Lakshmipathy Balaji(IND)","Lasith Malinga (SL)","Loots Bosman (SA)","Luke Pomersbach (AUS)","Luke Ronchi (AUS)","Mahela Jayawardene (SL)","Mahendra Singh Dhoni (IND)","Mahesh Rawat","Makhaya Ntini (SA)","Manish Pandey (IND)","Manoj Tiwary (IND)","Manpreet Gony (IND)","Mark Boucher (SA)","Matthew Hayden (AUS)","Mayank Tehlan (IND)","Michael Hussey (AUS)","Misbah-ul-Haq (PAK)","Mithun Manhas (IND)","Mohammad Asif (PAK)","Mohammad Hafeez (PAK)","Mohammad Kaif (IND)","Morne Morkel (SA)","Munaf Patel (IND)","Murali Kartik (IND)","Musavir Khote(IND)","Muttiah Muralitharan (SL)","Napolean Einstien(IND)","Nathan Bracken (AUS)","Niraj Patel (IND)","Nitin Saini (IND)","Nuwan Zoysa (SL)","Paidikalva Vijaykumar (IND)","Palani Amarnath (IND)","Pankaj Dharmani (IND)","Pankaj Singh (IND)","Parag More (IND)","Parthiv Patel (IND)","Pinal Shah (IND)","Piyush Chawla (IND)","PM Sarvesh Kumar (IND)","Pradeep Sangwan (IND)","Pragyan Ojha (IND)","Prasanta Saha (IND)","Praveen Kumar (IND)","R Ashwin (IND)","Rahul Dravid (IND)","Rajat Bhatiya (IND)","Rajesh Pawar (IND)","Ramesh Powar (IND)","Ramnaresh Sarwan (WI)","Ranadeb Bose (IND)","Ravindar Jadega (IND)","Ricky Ponting (AUS)","Rishi Dhawan (IND)","Robin Uthappa (IND)","Rohan Banerjee (IND)","Rohan Raje (IND)","Rohit Sharma (IND)","Ross Taylor (NZ)","RP Singh (IND)","S Goswami(IND)","S Johil (IND)","Sachin Tendulkar (IND)","Sahil Kukreja (IND)","Sanath Jayasuriya (SL)","Sanjay Bangar (IND)","Sarveesh kumar (IND)","Saurabh Tiwary (IND)","Scott Styris (NZ)","Shadab Jakati (IND)","Shahid Afridi (PAK)","Shane Warne (AUS)","Shane Watson (AUS)","Shantha Sreesanth (IND)","Shaun Marsh (AUS)","Shaun Pollock (SA)","Shikhar Dhawan (IND)","Shivnarine Chanderpaul (WI)","Shoaib Akhtar (PAK)","Shoaib Malik (PAK)","Siddharth Chitnis (IND)","Siddharth Trivedi (IND)","Sidhartha Kaul (IND)","Simon Katich (AUS)","Sohail Tanveer (Pak)","Sourashish Lahiri (IND)","Sourav Ganguly (IND)","Srikkanth Anirudha(IND)","Stephen Fleming (NZ)","Subramaniam Badrinath (IND)","Sumit Khatri (IND)","Sunny Sohal (IND)","Suresh Kumar (IND)","Suresh Raina (IND)","Swapnil Asnodkar (IND)","Swapnil Singh(IND)","Tanmay Srivastava (IND)","Taruvar Kohli (IND)","Tatenda Taibu (ZIM)","Tejashwi Yadav (IND)","Tillakaratne Dilshan (SL)","Uday Kaul (IND)","Umar Gul (PAK)","V Kohli (IND)","Venugopal Rao (IND)","Vidyut Sivaramakrishnan (IND)","Vikrant Yeligati (IND)","Vinay Kumar(IND)","Viraj Kabde (IND)","Virender Sehwag (IND)","VRV Singh (IND)","VVS Laxman (IND)","Wasim Jaffer (IND)","Wilkin Mota (IND)","Yashpal Singh(IND)","Yo Mahesh(IND)","Yogesh Nagar (IND)","Yogesh Takawale (IND)","Younis Khan (PAK)","Yusuf Pathan (IND)","Yuvraj Singh (IND)","Zaheer Khan (IND)");


function changeView(type)
{
	if(type == 'conf')
	{
		document.getElementById('conf').style.visibility = 'visible';
		document.getElementById('conf_label').style.visibility = 'visible';
		loadChildren();
		document.getElementById("team_conf").size = 15;
	}
	else
	{
		document.getElementById('conf').style.visibility = 'hidden';
		document.getElementById('conf_label').style.visibility = 'hidden';
		loadAllChildren('team_conf');
		document.getElementById("team_conf").size = 25;
	}
}

// Fill the submit menu with 1 to submitN values
function resetRanking()
{
	var plist = document.getElementById("select3").getElementsByTagName("option");
	for(i=0;i<plist.length;i++)
	{
		plist[i].innerHTML = (i+1) + ".";
		plist[i].value = "";
	}
}

// Fill the submit menu with 1 to submitN values
function fillSubmit()
{
	if(document.getElementById('select3').getElementsByTagName("option").length > 0)
		return;
	var slist = document.getElementById("select3");
	for(i=1;i<=submitN;i++)
	{
		var ele = document.createElement("option");
		ele.innerHTML = i + ".";
		ele.value = "";
		slist.appendChild(ele);
	}
}

// default page load function to populate all menu boxes.
function loadPage()
{
	var plist = document.getElementById("conf");
	for(i=0;i<parentValues.length;i++)
	{
		var ele = document.createElement("option");
		ele.innerHTML = parentValues[i];
		ele.onclick = loadChildren;
		ele.onchange = loadChildren;
		ele.onkeydown = loadChildren;
		if(i == 0)
			ele.selected = "selected";
		plist.appendChild(ele);
	}
	loadChildren();
	fillSubmit();
}

// Load all children menu items.
//function loadAllChildren()
//{
//	loadAllChildren('team_conf');
//}

// Load all children menu items.
function loadAllChildren(id)
{
	// Populate the children menu with the array of children.
	var clist = document.getElementById(id);
//	alert(clist);
	clist.innerHTML = "";
//	alert(allValues.length);
	for(i=0;i<allValues.length;i++)
	{
		var ele = document.createElement("option");
		ele.innerHTML = allValues[i];
		ele.value = allValues[i];
		if(i == 0 && setSelected == true)
			ele.selected = "selected";
		clist.appendChild(ele);
	}
}

// Load children corresponding to selected parent.
function loadChildren()
{
	// Find parent menu
	var plist = document.getElementById("conf").getElementsByTagName("option");
	var found = -1;
	// Find the selected index and store in found.
	for(i=0;i<plist.length;i++)
	{
		if(plist[i].selected == "selected" || plist[i].selected == true)
		{
			found = i;
			break;
		}
	}
	// Find children menu
	var clist = document.getElementById("team_conf");
	clist.innerHTML = "";
	// Populate the children menu with the [found]th array of children.
	for(j=0;j<childValues[found].length;j++)
	{
		var ele = document.createElement("option");
		ele.innerHTML = childValues[found][j];
		ele.value = childValues[found][j];
		if(j == 0 && setSelected == true)
			ele.selected = "selected";
		clist.appendChild(ele);
	}
}

// Add multiple selected items from children menu onto submit menu
function addSubmit()
{
	var clist = document.getElementById('team_conf').getElementsByTagName("option");
	var slist = document.getElementById("select3").getElementsByTagName("option");
	var added = 0;
	var duplicates = "";
	var location = 0;
	// Iterate over the children menu
	for(i=0;i<clist.length;i++)
	{
		// If the current item is selected
		if(clist[i].selected == "selected" || clist[i].selected == true)
		{
			var dontadd = false;
			// Find a suitable location to add it
			for(location=0;location<slist.length;location++)
			{
				if(slist[location].value == null || slist[location].value == "")
					break;
				else
					if(slist[location].value == clist[i].value)
					{
						// If the item to be added already exists, then set dontadd
						duplicates += clist[i].value + ", ";
						dontadd = true;
						break;
					}
			}
			// List is already full
			if(location == slist.length)
			{
				alert('The list is already full. You cannot pick more than 11 players.');
				break;
			}
			// If the item does not already exists, then
			if(dontadd == false)
			{
				slist[location].innerHTML += " " + clist[i].value;
				slist[location].value = clist[i].value;
				added++;
				if(resetSelection == true)
					clist[i].selected = null;
			}
		}
	}
	// If already existing items were found, display their list
	if(duplicates.length > 0)
		alert(duplicateErrMsg + duplicates.substring(0,duplicates.length-2) + ".");
	else
		if(added == 0 && location != slist.length)
			alert(addErrorMsg);
}

// Remove an item from submit menu
function removeSubmit()
{
	var slist = document.getElementById("select3").getElementsByTagName("option");
	var removed = false;
	// Find the selected item to remove
	for(i=0;i<slist.length;i++)
	{
		// If found, then remove the item and set removed = true
		if(slist[i].selected == "selected" || slist[i].selected == true)
		{
			if(slist[i].value == null || slist[i].value == "")
			{
				alert(removeErrorMsg);
				return;
			}
			removed = true;
			slist[i].selected = null;
			slist[i].innerHTML = Number(Number(i) + Number(1)) + ".";
			slist[i].value = "";
			continue;
		}
		// if removed is true, then move all subsequent items up.
		if(removed == true)
			if(slist[i].value == null || slist[i].value == "")
				return;
			else
			{
				slist[i-1].innerHTML = Number(i) + ". " + slist[i].value;
				slist[i-1].value = slist[i].value;
				slist[i].value = "";
				slist[i].innerHTML = Number(Number(i) + Number(1)) + ".";
			}
	}
	// If nothing was removed, display error.
	if(removed == false)
		alert(removeErrorMsg);
}

// Move an item in submit menu UP.
function moveUp()
{
	var slist = document.getElementById("select3").getElementsByTagName("option");
	var moved = false;
	// Iterate over the submit menu
	for(i=0;i<slist.length;i++)
	{
		// Find the selected item
		if(slist[i].selected == "selected" || slist[i].selected == true)
		{
			moved = true;
			// If an empty row is selected for move, then display error
			if(slist[i].value == null || slist[i].value == "")
				alert("You can not move an empty row.");
			else
			{
				// If the first row is selected for moving up, then display error
				if(i == 0)
					alert("The first row can not be moved up.");
				else
				{
					// Swap the current item with the item above it.
					// Maintain focus on current item
					var value1 = slist[i-1].value;
					slist[i-1].innerHTML = Number(i) + ". " + slist[i].value;
					slist[i-1].value = slist[i].value;
					slist[i-1].selected = "selected";
					slist[i].value = value1;
					slist[i].innerHTML = Number(Number(i) + Number(1)) + ". " + value1;
					slist[i].selected = null;
				}
			}
			return;
		}
	}
	// If nothing was selected for move, display error.
	if(moved == false)
		alert(moveErrorMsg);
}

function moveDown()
{
	var slist = document.getElementById("select3").getElementsByTagName("option");
	var moved = false;
	// Iterate over the submit menu
	for(i=0;i<slist.length;i++)
	{
		// Find the selected item
		if(slist[i].selected == "selected" || slist[i].selected == true)
		{
			moved = true;
			// If an empty row is selected for move, then display error
			if(slist[i].value == null || slist[i].value == "")
				alert("You can not move an empty row.");
			else
			{
				// If the last row is selected for moving down, then display error
				if(slist[i+1].value == null || slist[i+1].value == "")
					alert("The last row can not be moved down.");
				else
				{
					// Swap the current item with the item below it.
					// Maintain focus on current item
					var value1 = slist[i+1].value;
					slist[i+1].innerHTML = Number(Number(i) + Number(2)) + ". " + slist[i].value;
					slist[i+1].value = slist[i].value;
					slist[i+1].selected = "selected";
					slist[i].value = value1;
					slist[i].innerHTML = Number(Number(i) + Number(1)) + ". " + value1;
					slist[i].selected = null;
				}
			}
			return;
		}
	}
	// If nothing was selected for move, display error.
	if(moved == false)
		alert(moveErrorMsg);
}

