/************************
**
** global variables 
**
** NOTE: there are four very large arrays that are spit out into the page in a script block at the end... 
**		these contain the list of schools, sports, the user's choices and so on.
************************/
var bg="#D3D3D3";
var usr_skul_list="";
var skul_count=0;
var max_skuls=15;
var max_sports=35;
var sp_count=0;
var schllist="";
var skul_sport_list='';
var sport_list='';

/* $$$$$$$$$$$$$$$$$$$$$$$$$$ */

/*
************************************
** 
**	 sports and related functions 
**
************************************
*/
function select_usr_sport(item){
/*	user has clicked on a sport description in the uses school list. This places "skulsptDD:XX" into the skul_sport_list global var,
	where DD is the school id and XX is the sport id that goes with it. Both ids come from XML files... 
*/
	var id="";
	if (!item.style.backgroundColor){
		item.style.backgroundColor=bg;
		if (item.parentNode.style.backgroundColor){
			item.parentNode.style.backgroundColor='';
			}
		id=item.id;
		skul_sport_list+=id + "#"
		}
		else{
			item.style.backgroundColor='';
			id=item.id;
			var re = new RegExp(id + "#" ,"");
			skul_sport_list=skul_sport_list.replace(re,"");
			}

//alert(skul_sport_list);

}
function fill_sports_list(){
// fills the static list of sports user can choose from
	pnode=document.getElementById("sportslist");
	var list=pnode.innerHTML;
	for (var i=0; i < sports.length; i++){
		list+="<div id='spt" + sports[i].id + "' class='usr_schools'"
			+ "  onClick='select_sport(this);' onMouseOver='handme(this);' >"
			+ sports[i].descr + "</div>\n";
		}
	list+="<span onClick='add_sports();' class='buttonsm buttonxsm' onMouseOver='color_buttons(1,this);' onMouseOut='color_buttons(0,this);' id='btnsports'>Add selected sports</span>&nbsp;<img src='blk_rt.gif' style='vertical-align: bottom;'>&nbsp;";
	// finally spit out that entire mess... 
	pnode.innerHTML=list;
}
function select_sport(item){
	if (!item.style.backgroundColor){
		item.style.backgroundColor=bg;
		sport_list+=item.id + "#";
		}
		else{
			item.style.backgroundColor='';
			var re = new RegExp(item.id + "#" ,"");
			sport_list=sport_list.replace(re,"");
			}
//alert(sport_list);
}
function add_sports(){
	// loop through the list of selected sports...
	//skulsp9:146
	if (sport_list.length == 0){
		alert("Please choose one or more sports");
		return;
		}
	if (usr_skul_list.length == 0){
		alert("Please choose one or more schools in your list");
		return;
		}
	var arrSport=sport_list.replace(/#$/,"").split('#');
	var arrSchools=usr_skul_list.replace(/#$/,"").split('#');
	var sport='';
	var school='';
	var msg="";
	var lert="";
	var i,j;
//	alert(usr_skul_list + " : " + sport_list);
	for (i=0;i<arrSport.length;i++){
		// ... putting each one with any selected schools
		for (j=0;j<arrSchools.length; j++){
		// if it already exists, don't do it and complain.
			if (document.getElementById('skulsp' + arrSchools[j].replace(/skul_spt/,'') + ":" + arrSport[i].replace(/spt/,'')) ){
				sport=identify_sport(arrSport[i]);
				school=identify_user_school(arrSchools[j]);
				msg+="You already have " + sport + " for " +  school + "\n";
				}
				else{
					if (sp_count < max_sports){
			//			alert(arrSchools[j] + " gets " + arrSport[i])
						document.getElementById(arrSchools[j]).innerHTML +="<div class='usr_sports' onClick='select_usr_sport(this);' onMouseOver='handme(this);' id='skulsp"
							 + arrSchools[j].replace(/skul_spt/,'') + ":" + arrSport[i].replace(/spt/,'') + "'>" + document.getElementById(arrSport[i]).innerHTML 
							 + "<input type='hidden' name='sksp[]' value='" + arrSchools[j].replace(/skul_spt/,'') + ":" + arrSport[i].replace(/spt/,'') + "'>"
							 + "</div>";
						document.getElementById(arrSchools[j]).style.backgroundColor='';
						var re = new RegExp(arrSchools[j] + "#" ,"");
						usr_skul_list=usr_skul_list.replace(re,"");
						sp_count++;
						spit_out_skuls_count();
						}
						else{
							sport=identify_sport(arrSport[i]);
							school=identify_user_school(arrSchools[j]);

							if (lert.length==0){
								lert="Sorry, you've reached the limit of " + max_sports + " sports.<br>You must remove one from any school to add another."
									+ "<br><br><b>Not added:</b><br><br>"
									+ sport + " -- " + school.replace(/high$/i,'') + "<br>";
								}
								else{
									lert+=sport + " -- " + school.replace(/high$/i,'') + "<br>";
									}
								
							}
						

					}
			}
		// unhighlight each sport as it's handled and also remove it from the global var (a list of the selected ones)
		document.getElementById(arrSport[i]).style.backgroundColor='';
		var re = new RegExp(arrSport[i] + "[#]?","");
		sport_list=sport_list.replace(re,"");
		}
	if (msg.length > 0){
		alert(msg);
		}
	if (lert.length > 0){
		document.getElementById('errs').innerHTML=lert;
		lert="";
		}
//		else{
//			document.getElementById('errs').innerHTML='';
//			}

}
function remove_sports(){
	if (skul_sport_list.length == 0){
		return;
		}
 	var arrSport=skul_sport_list.replace(/#$/,'').split('#');
	var pnode="";
	var node="";
	for (i=0; i < arrSport.length; i++){
		pnode=document.getElementById(arrSport[i]).parentNode;
		node=document.getElementById(arrSport[i]);
		pnode.removeChild(node);
		var re = new RegExp(arrSport[i] + "#" ,"");
		skul_sport_list=skul_sport_list.replace(re,"");
		sp_count--;
		spit_out_skuls_count();
		}

}

function identify_sport(idnum){
	var c;
	idnum=idnum.replace(/\D/g,'');
	for (c=0; c < sports.length;c++){
		if (idnum==sports[c].id){
			return sports[c].descr;
			}
		}
}


/*
************************************
** 
**	school related functions 
**
************************************
*/
function identify_user_school(idnum){
	var c;
	idnum=idnum.replace(/\D/g,'');
	for (c=0; c < usr_schools.length;c++){
		if (idnum==usr_schools[c].id){
			return usr_schools[c].name;
			}
		}

}
function select_usr_skul_sport(id){
/* user has clicked on a school, but we're passing the name of the enclosing div, not the school div... 
	the id of the school enclosure is put into (or removed from) a global enclosure... this tells us what and how many
	are selected... id='skul_sptX where x is the school idnumber
 */
	var item=document.getElementById(id);
	if (!item.style.backgroundColor){
		item.style.backgroundColor=bg;
		usr_skul_list+=id + "#";
		}
		else{
			item.style.backgroundColor='';
			var re = new RegExp(id + "#" ,"");
			usr_skul_list=usr_skul_list.replace(re,"");
			}
//alert(usr_skul_list);
}
function fill_schls_list(){
/* 
	called onLoad, to build the user's list of schools and sports... 
	 pnode gets the parentNode, which for this is schlslist, the div that has the box surrounding the users list of schools

	usr_schools is the object/array containing the users' schools. (each school is unique)
	schl_sportid is object/array containing the users' sports, tied to the school id. (school id unique, but each school id is 
		mapped to an array of sports. 
	sports is object/array of the possible sports (especially the descriptions)

*/
	pnode=document.getElementById("schlslist");
	var list=pnode.innerHTML;
	var c=0;
	var sp=0;
	var j,i,m,k;
	// if we actually have some schools... 
	if (usr_schools.length > 0){
		for (i = 0; i < usr_schools.length; i++){
			list+="<div id='skul_spt" + usr_schools[i].id + "' class='usr_schools'>\n"
				+ "<div style='width:auto;' onClick='select_usr_skul_sport(\"skul_spt" + usr_schools[i].id + "\")' onMouseOver='handme(this);' id='usrskul"  + usr_schools[i].id + "'>" + usr_schools[i].name + " <em>" + usr_schools[i].loc + "</em></div>\n";
			// now loop thru list of user's sports...
			for (j=0; j < schl_sportid.length; j++){
				// if the current user school matches the school in one of the sport records ... 
				if (schl_sportid[j].skulid==usr_schools[i].id){
					// now gotta loop thru the array of sports associated with this school and ...
					for (k=0; k < schl_sportid[j].arrsport.length;k++){
						if (schl_sportid[j].arrsport[k] != 0){
							list+="<div class='usr_sports' onClick='select_usr_sport(this);' onMouseOver='handme(this);' id='skulsp" + usr_schools[i].id + ":" + schl_sportid[j].arrsport[k] + "'>" 
							// match the sport id against the sports list to get the description... 
							for(m=0; m < sports.length; m++){
								if (sports[m].id == schl_sportid[j].arrsport[k]){
									list+=sports[m].descr;
									}
								}
							sp++;	
							// whew, all done, include the closing div for each sport for this school.... 
							list+="<input type='hidden' name='sksp[]' value='" + usr_schools[i].id + ":" + schl_sportid[j].arrsport[k]  + "'>"
								+"</div>\n";
							} // end of if !=0
						} // end of for (k=0)
					} // end of if skulid=usr_schools.id
				} // end of for j=0
			// include the end div tag for the big enclosure for school and its sports ...
			list+="</div>\n";
			// and add to the count of schools 
			c++;
			}
		}
		else{
			show_hide_bits('off');
			list+="<span class='usr_schools' id='noskuls'>You have not chosen any schools yet.</span>";
			}
	// finally spit out that entire mess...
	document.getElementById("schlslist").innerHTML=list;
	// if we got at least one school... 		
//	if (c > 0){
		// skul_count is global var containing how many schools user has selected. Must be less than or equal to max_skuls
		skul_count=c;
		sp_count=sp;
		spit_out_skuls_count();
		document.getElementById('buttons').innerHTML="<br><span onClick='remove_sports();' class='buttonsm buttonxsm' onMouseOver='color_buttons(1,this);' onMouseOut='color_buttons(0,this);' id='remsports'>Remove selected sports</span>&nbsp;";
		document.getElementById('buttons').innerHTML+="<span onClick='remove_skul(\"" + pnode.id + "\");' class='buttonsm buttonxsm'  onMouseOver='color_buttons(1,this);' onMouseOut='color_buttons(0,this);'>Remove school from list</span>";
//		}
	
}
function show_hide_bits(onoff){
// onoff='on' then show, else hide
	if (onoff=='on'){
		document.getElementById('howto_usrschls').style.display='';
		document.getElementById('btnsave').style.display='';
		}
		else{
			document.getElementById('howto_usrschls').style.display='none';
			document.getElementById('btnsave').style.display='none';
		
			}
}
function remove_skul(pnodeid){
/*
	pnodeid = the parentNode.id from which we will remove child elements, which we know which are which 
			because they are in the global var usr_skul_list, put there by being clicked on and selected
*/
//	alert(usr_skul_list);
	var arrList=usr_skul_list.replace(/#$/,"").split('#');
	var list=document.getElementById("schlslist");
	var pnode=document.getElementById(pnodeid);
	var node="";
	for (var i=0;i < arrList.length;i++){
	//	if (arrList[i] != ""){
			node=document.getElementById(arrList[i]);
			pnode.removeChild(node);
			skul_count--;
			spit_out_skuls_count();
			var re = new RegExp(arrList[i] + "#" ,"");
			usr_skul_list=usr_skul_list.replace(re,"");
			}
	//	}
}

function find_schl(id){
	var listspan=document.getElementById("foundskuls");
	if (document.getElementById(id).value.length > 2){
		var term=document.getElementById(id).value;
		var list="";
		
		term=term.replace(/\./g,"\\.");
		var re = new RegExp( term ,"i");
		for (var i = 0;i < schools.length;i++){
			if (re.test(schools[i].name)){
				list+="<div id='s" + schools[i].id +"' class='usr_schools' onClick='pick_skul(this);' onMouseOver='handme(this);'>" + schools[i].name + " <em>" + schools[i].loc + "</em></div>";
				}
			}
		if (list.length < 3){
			list="Sorry, we don't have a school like that. Try again?";
			}
			else{
				list+="<br>"
					+ "<span class='sm'>Click on one or more schools to choose</span><br>" 
					+"<img src='blk_left.gif' style='vertical-align: bottom;'>&nbsp;<span class='buttonsm buttonxsm' id='selectskuls' onClick='send_skuls();' onMouseOver='color_buttons(1,this);' onMouseOut='color_buttons(0,this);'> Add selected schools</span>";
				}		
		listspan.innerHTML=list;
	/* don't delete these ... 
		setTimeout("document.getElementById('" + id + "').focus();",1);
		setTimeout("document.getElementById('" + id + "').select();",1);
	*/
		}
		else if (document.getElementById(id).value.length < 2){
			listspan.innerHTML="";
			}
		
}
function pick_skul(item){
//		return;
	if (!item.style.backgroundColor){
		schllist+=item.id + "#";
		item.style.backgroundColor=bg;
		}
		else{
			item.style.backgroundColor='';
			var re = new RegExp(item.id + "#" ,"");
			schllist=schllist.replace(re,"");
			}


}
function send_skuls(){
/* sends schools into the user's list... */
	if (schllist.length == 0){
		alert("Please click on a school to add it to your list.");
		return;
		}
	if (document.getElementById("noskuls")){
		pnode=document.getElementById("schlslist");
		pnode.removeChild(document.getElementById("noskuls"));
		document.getElementById('buttons').innerHTML="<br><span onClick='remove_sports();' class='buttonsm buttonxsm' onMouseOver='color_buttons(1,this);' onMouseOut='color_buttons(0,this);' id='remsports'>Remove selected sports</span>&nbsp;";
		document.getElementById('buttons').innerHTML+="<span onClick='remove_skul(\"" + pnode.id + "\");' class='buttonsm buttonxsm'  onMouseOver='color_buttons(1,this);' onMouseOut='color_buttons(0,this);'>Remove school from list</span>";
		}
	var arrList=schllist.replace(/#$/,"").split('#');
	var list=document.getElementById("schlslist");
	for (var i=0;i < arrList.length;i++){
		// if it already exists, don't do it and complain.
		if (document.getElementById("skul_spt" + arrList[i].replace(/s/,'')) ){
			var re = new RegExp(arrList[i] + "#" ,"");
			schllist=schllist.replace(re,"");
			document.getElementById(arrList[i]).style.backgroundColor='';
			alert("You already have that school");
			}
			else{
				if (skul_count < max_skuls){
					list.innerHTML+="<div id='skul_spt" + arrList[i].replace(/s/,'') + "' class='usr_schools'>\n"
					 	+ "<div onClick='select_usr_skul_sport(\"skul_spt" + arrList[i].replace(/s/,'') + "\")' "
						+ "onMouseOver='handme(this);' id='usrskul"  + arrList[i].replace(/s/,'') + "'>" 
						+ document.getElementById(arrList[i]).innerHTML + "</div>\n"
						+ "</div>";
					document.getElementById(arrList[i]).style.display='none';
					var re = new RegExp(arrList[i] + "#" ,"");
					schllist=schllist.replace(re,"");
					skul_count++;
					spit_out_skuls_count();
					}
					else{
						alert("Sorry, you've reached the limit of " + max_skuls + ".\nYou must remove one to add another.");
						}
					}
		}
			
	show_hide_bits('on');


}
function spit_out_skuls_count(){
	document.getElementById("usrskuls").innerHTML="Your schools (" + skul_count + ") and sports (" + sp_count + ")";
}

/*
************************************
** 
**	pwd changing-related functions 
**
************************************
*/

function pwd_lost(){
	document.lostpwd.cellphone.value=document.frmLogon.cellphone.value;
	if (document.lostpwd.cellphone.value != '' && isNaN(document.lostpwd.cellphone.value)==false){
		document.lostpwd.submit();
		}
		else{
			alert('Please enter a cell phone number');
			}


}
function check_pwd(myform){
	if (myform.pwdold.value.replace(/ /g,"").length==0){
		alert('Please enter your current password');
		}
		else if (myform.newpwd.value.replace(/ /g,"").length==0){
			alert('Please enter a new password');
			}
			else if (myform.newpwdagain.value.replace(/ /g,"").length==0){
				alert('Please enter your new password in the third blank');
				}
				else if (myform.newpwdagain.value != myform.newpwd.value){
					alert('You did not type the same new password the second time');
					}
					else{
						myform.submit();
						}

}
/*
************************************
** 
**	UI-related functions 
**
************************************
*/
function handme(item){
	var elem;
	if (typeof(item) == "string"){
		elem=document.getElementById(item);
		}
		else{
			elem=item;
			}
	if (!document.all){
		elem.style.cursor='pointer';
		}
		else{
			elem.style.cursor='hand';
			}
}
function color_buttons(onoff,item){
	var elem;
	if (typeof(item) == "string"){
		elem=document.getElementById(item);
		}
		else{
			elem=item
			}

	if (onoff==1){
		elem.style.backgroundColor='#000000';
		elem.style.color='#FFFFFF';
		handme(elem);
		}
		else{
			item.style.backgroundColor='';
			elem.style.color='#000000';
			}
}
/* $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ */
/*
************************************
** 
**	save the entire mess 
**
************************************
*/
function save_all(){
	if (!document.frmSchls.elements['sksp[]']){
		alert("Please select some sports");
		}
		else{
			document.frmSchls.submit();
			}
	return;

}	