function validateEmail(email) {
  	var str = email;
  	if (window.RegExp) {
    	var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    	var reg2str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
    	var reg1 = new RegExp(reg1str);
    	var reg2 = new RegExp(reg2str);
    	if (!reg1.test(str) && reg2.test(str)) {
      		return true;
    	}
		return false;
 	 } else {
    	if(str.indexOf("@") >= 0){
      		return true;
		}
    	return false;
  	} 
}

function trim2(s) {
	while (s.substring(0,1) == ' ') {
		s = s.substring(1,s.length);
	}
	while (s.substring(s.length-1,s.length) == ' ') {
		s = s.substring(0,s.length-1);
	}
	return s;
}


function subForm(id){
	var form = document.getElementById(id);
	if(validateJoin(form)){
		form.submit();
	}
}

function validateJoin(form){
	var errMsg = '';
	if((!(validateEmail(form.email.value))) || trim2(form.email.value) == ''){
		errMsg += 'Please Enter a Valid Email\n';
	}
	if(trim2(form.email.value) != trim2(form.confirmEmail.value)){
		errMsg += 'Email and Confirm Email Must Match\n';
	}
	if(trim2(form.pass.value) != trim2(form.confirmPass.value)){
		errMsg += 'Password and Confirm Password Must Match\n';
	}
	if(trim2(form.pass.value) == ''){
		errMsg += 'Please Enter a Password\n';
	}
	if(trim2(form.gender.value) == ''){
		errMsg += 'Please Select a Gender\n';
	}
	if(errMsg != ''){
		alert(errMsg);
		return false;
	} else {
		return true;
	}
}


function sendForgot(id){
	var email = document.getElementById(id).value;
	if(email != ''){
		DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'sendPassword', email, showSentPass);
	} else {
		alert('Please Enter your User ID');
	}
}

function showSentPass(r){
	if(r == 0){
		var div = document.getElementById('error');
		div.innerHTML = '*Sorry, this email was not found in our database';
		div.style.display = '';
	}else{
		var div = document.getElementById('error');
		div.innerHTML = 'Your password has been sent to your email account';
		div.style.display = '';
	}
}

function changePass(){
	var opass = document.getElementById('currPassId').value;
	var npass = document.getElementById('newPassId').value;
	var cpass = document.getElementById('conPassId').value;	
	var id = document.getElementById('idnumID').value;	
	var email = document.getElementById('emailID').value;	
	var errMsg = '';
	if(trim2(opass) == ''){
		errMsg += 'Please Enter Current Password\n';
	}
	if(trim2(npass) == ''){
		errMsg += 'Password Cannot Be Blank\n';
	}
	if(trim2(npass) != trim2(cpass)){
		errMsg += 'Please Confirm New Password\n';
	}
	if(errMsg != ''){
		alert(errMsg);
	} else {
		DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'changePass', id, email, opass, npass, changePassSuccess);
	}
}

var errordone = false;

function changePassSuccess(r){
	var table1 = document.getElementById('passTable');
	if(r == 1){
		table1.innerHTML = '<table><tr><td>Password Successfully Changed</td></tr></table>';
	} else {
		var temp = table1.innerHTML;
		if(errordone != true){
			table1.innerHTML = '<table><tr><td colspan="2" style="color:#FF0000;">Invalid Password. Please Try Again</td></tr></table>' + temp;
		}
		errordone = true;
	}
}

function sendTestEmail(id, idnum){
	var email = document.getElementById(id).innerHTML;
	if(!validateEmail(email)){
		alert('Please Enter a Valid Email Address');
	} else {
		DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'testEmail', email, idnum, id, testSent);
	}
}

function unblockNumber(num, objid){
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'unblock', num, objid, numberunblocked);
}

function numberunblocked(r){
	$(r).innerHTML = 'Test Message Sent';
	return;
}

function resendConf(num, idnum){
	//btn.value = "Confirmation Sent";
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'resendConf', num, idnum, confSent);
}

function sendTestTweet(twitter, objid){
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'sendTestTweet', twitter, objid, tweetSent);
}

function confSent(){
	return;
}


function tweetSent(r){
	$(r[0]).innerHTML = r[1];
	return;
}

function testSent(r){
	var temp = document.getElementById(r[1]+'Msg');
	if(r[0] == -1){
		var tempval = temp.innerHTML;
		temp.innerHTML = '<span style="color:#ff0000;">Email Not Found</span>' + tempval;
	} else {
		temp.innerHTML = 'Test Message Sent';
	}
}

function validatePhone (sText)
{
	var ValidChars = "0123456789";
	var IsNumber=true;
	var Char;
	
	if (sText.length != 10)
	{
		IsNumber = false;
	}
	else
	{
		for (i = 0; i < sText.length && IsNumber == true; i++) 
		{ 
			Char = sText.charAt(i); 
			if (ValidChars.indexOf(Char) == -1) 
			{
				IsNumber = false;
				break;
			}
		}
	}
	return IsNumber;	
}

function updateBleachersUser(){
	var form = document.getElementById('addBleachForm');
	var errMsg = '';
	if(form.partiType.value == ''){
		errMsg += 'Please Select What You Would Like to Participate as\n';
	}
	if(trim2(form.address.value) == ''){
		errMsg += 'Please Enter an Address\n';
	}
	if(trim2(form.city.value) == ''){
		errMsg += 'Please Enter a City\n';
	}
	if(trim2(form.state.value) == ''){
		errMsg += 'Please Select a State\n';
	}
	if(trim2(form.zip.value) == ''){
		errMsg += 'Please Enter a Zip Code\n';
	}
	if(trim2(form.mm.value) == 'RT' || trim2(form.dd.value) == 'RT' || trim2(form.yy.value) == 'RT'){
		errMsg += 'Please Enter Your Date of Birth\n';
	}
	if(errMsg != ''){
		alert(errMsg);
	} else {
		form.submit();
	}
}

function changeNotification(id, nottype){
	var notif;
	if(document.getElementById(id+nottype).checked){
		notif = 1;
	}else{
		notif = 0;
	}
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'changeNotifications', id, nottype, notif, notifySuccess);
}

function notifySuccess(r){
	var plural='s';
	if(r[1]==1){plural='';};
	document.getElementById(r[0]+'selectNotifications').innerHTML = r[1] + ' alert'+plural;
	document.getElementById(r[0]+'selectNotifications').style.backgroundColor='#fff000';
	var flashy = document.getElementById(r[0]+'selectNotifications');
	var fx = new Fx.Styles(flashy, {duration:800, wait:false});
	fx.start({
		'background-color': '#ffffff'
	});
	return;
}

function changePriority(id, uid){
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'changePriority', id, uid, prioritySuccess);
}

function prioritySuccess(r){
	var flashy = document.getElementById(r.toString());
	var fx = new Fx.Style(flashy, 'background-color', {duration:800, wait:false}).set("#fffc00");
	fx.start("#ffffff");
	return;
}


function removeSubscriptionConfirm(id){
	if(document.all)e = event;
	var obj = document.getElementById('confirmBox');
	obj.innerHTML = '<div style="padding:5px 5px 5px 5px;">Are You Sure?<br><input type="button" value="Yes" onclick="removeSubscription(\''+id+'\');">&nbsp;<input type="button" value="No" onclick="closeConfirm(\'confirmBox\');"></div>';
	var obj2 = document.getElementById(id+'Trash');
	var curleft = curtop = 0;
	if (obj2.offsetParent) {
		curleft = obj2.offsetLeft
		curtop = obj2.offsetTop
		while (obj2 = obj2.offsetParent) {
			curleft += obj2.offsetLeft
			curtop += obj2.offsetTop
		}
	}
	obj.style.left = curleft + 'px';
	obj.style.top = curtop + 'px';
	obj.style.display = '';
}

function closeConfirm(id){
	var obj = document.getElementById(id);
	obj.style.display = 'none';
}

function removeSubscription(id){
	closeConfirm('confirmBox');
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'removeSub', id, removeSuccess);
}

function removeSuccess(r){
	var obj = document.getElementById(r);
	obj.parentNode.removeChild(obj);
}

function removeAllSuccess(r){
	var trs = document.getElementsByTagName('tr');
	for (var i=0; i < r.getRowCount(); i++){
		for(var j = 0; j < trs.length; j++){
			if(trs[j].id == r.prime[i]){
				trs[j].parentNode.removeChild(trs[j]);
			}
		}
	}
}


function addTeam(uid){
	var team = document.getElementById('teamField').value;
	if(team.indexOf('AddAll') != -1){
		var sc_id = team.substr(team.indexOf(',')+1);
		var team2 = 'RemoveAll,'+sc_id;
		DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'removeAllTeams', uid, team2, removeAllSuccess);
		DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'addAllTeams', uid, team, addTeamSuccess);
	}else if(team.indexOf('RemoveAll') != -1){
		if(document.all)e = event;
		var obj = document.getElementById('confirmBox');
		obj.innerHTML = '<div style="padding:5px 5px 5px 5px;">Are You Sure?<br><input type="button" value="Yes" onclick="removeAllTeams(\''+uid+'\');">&nbsp;<input type="button" value="No" onclick="closeConfirm(\'confirmBox\');"></div>';
		var obj2 = document.getElementById('addTeamLink');
		var curleft = curtop = 0;
		if (obj2.offsetParent) {
			curleft = obj2.offsetLeft
			curtop = obj2.offsetTop
			while (obj2 = obj2.offsetParent) {
				curleft += obj2.offsetLeft
				curtop += obj2.offsetTop
			}
		}
		obj.style.left = curleft + 'px';
		obj.style.top = curtop + 'px';
		obj.style.display = '';
	}else{
		DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'addTeam', uid, team, addTeamSuccess);
	}
}

function removeAllTeams(uid){
	closeConfirm('confirmBox');
	var team = document.getElementById('teamField').value;
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'removeAllTeams', uid, team, removeAllSuccess);
}

function addTeamSuccess(r){
	if(r != -1){
		obj = document.getElementById('notificationBody');		
		var count = 2000;
		var emailInfo = document.getElementById('emailNumID').value;
		var emailInfo2 = document.getElementById('emailListID').value;	
		for (var i=0; i < r.getRowCount(); i++)
		{
			var newRow = obj.insertRow(i);
			newRow.setAttribute('id', r.prime[i]);
			newRow.setAttribute('valign', 'middle');
			var cell1 = newRow.insertCell(0);
			cell1.innerHTML = r.highschool[i]+'<a name="'+r.prime[i]+'"></a><div class="teamSport">'+r.division[i]+' '+r.level_abr[i]+' '+r.sport[i]+'</div>';
			var cell2 = newRow.insertCell(1);
			cell2.innerHTML = '<input type="radio" name="priority" id="'+r.prime[i]+'priority" onchange="changePriority(\''+r.prime[i]+'\', '+r.id[i]+');"/>';
			var cell3 = newRow.insertCell(2);			
			var div1 = document.createElement('DIV');
			div1.setAttribute('class', 'notificationsSlide');
			div1.setAttribute('className', 'notificationsSlide');
			div1.setAttribute('id', r.prime[i]+'notificationsSlide');
			div1.style.zIndex = r.prime[i]+1;
			var div2 = document.createElement('DIV');
			div2.setAttribute('id', r.prime[i]+'selectNotifications');
			div2.setAttribute('class', 'notificationsClosed');
			div2.setAttribute('className', 'notificationsClosed');			
			div2.innerHTML = '3 alerts';
			div1.appendChild(div2);
			var div3 = document.createElement('DIV');
			div3.setAttribute('id', r.prime[i]+'notificationsDrop');
			div3.setAttribute('class', 'dropBox');
			div3.setAttribute('className', 'dropBox');
			div3.innerHTML = '<input type="checkbox" name="notification" id="'+r.prime[i]+'receivechanges" onchange="changeNotification(\''+r.prime[i]+'\', \'receivechanges\');" checked/>Notifications<br /><input type="checkbox" name="scores" id="'+r.prime[i]+'scoresstats" onchange="changeNotification(\''+r.prime[i]+'\', \'scoresstats\');" checked/>Scores & Stats<br /><input type="checkbox" name="pics" id="'+r.prime[i]+'receivepics" onchange="changeNotification(\''+r.prime[i]+'\', \'receivepics\');" checked/>Photos<br />'
			div1.appendChild(div3);
			cell3.appendChild(div1);			
			var cell4 = newRow.insertCell(3);
			var div4 = document.createElement('DIV');
			div4.setAttribute('class', 'subscriptionsSlide');
			div4.setAttribute('className', 'subscriptionsSlide');
			div4.setAttribute('id', r.prime[i]+'subscriptionsSlide');
			div4.style.zIndex = r.prime[i];
			var div5 = document.createElement('DIV');
			div5.setAttribute('class', 'subscriptionsClosed');
			div5.setAttribute('className', 'subscriptionsClosed');
			div5.setAttribute('id', r.prime[i]+'selectSubscriptions');
			div5.innerHTML = emailInfo + ' subscription';
			if(emailInfo != 1){
				div5.innerHTML += 's<br />';
			}
			div4.appendChild(div5);
			var div6 = document.createElement('DIV');
			div6.setAttribute('class', 'dropBox');
			div6.setAttribute('className', 'dropBox');
			div6.setAttribute('id', r.prime[i]+'subscriptionsDrop');
			div6.innerHTML = emailInfo2 + '<a href="editsubscriptions.cfm">edit my email/sms</a>';
			div4.appendChild(div6);
			cell4.appendChild(div4);
			var cell5 = newRow.insertCell(4);
			cell5.setAttribute('class', 'customCal');
			cell5.setAttribute('className', 'customCal');
			cell5.innerHTML = '<input type="checkbox" id="'+r.prime[i]+'calendar" onchange="changeNotification(\''+r.prime[i]+'\', \'calendar\');" checked/>';
			var cell6 = newRow.insertCell(5);
			cell6.setAttribute('class', 'customCal');
			cell6.setAttribute('className', 'customCal');
			cell6.setAttribute('id', r.prime[i]+'Trash');
			cell6.innerHTML ='<a href="javascript:void(0);" onclick="removeSubscriptionConfirm(\''+r.prime[i]+'\');"><img src="images_common/btnDelete.png" border="0" /></a>';
			newRow.style.backgroundColor='#fff000';
			var flashy = document.getElementById(r.prime[i]);
			var fx = new Fx.Styles(flashy, {duration:800, wait:false});
			fx.start({
				'background-color': '#ffffff'
			});
		
		}
		
			var notificationsTrigger = $$('.notificationsClosed');
			
			notificationsTrigger.each(function(element){
				var prime = parseInt(element.id);
				var notificationsSlide = new Fx.Styles(''+prime+'notificationsDrop',{duration:100, wait:false});
				var notificationsBox = new Fx.Styles(''+prime+'notificationsSlide',{duration:100, wait:false});	
				element.removeEvents();
				element.addEvent('click', function(){
	 
				if (element.className.indexOf('notificationsClosed') >= 0)	{
					element.removeClass('notificationsClosed');
					element.addClass('notificationsOpen');
					notificationsSlide.start({
					'height': 100,
					'width':140
					});
					notificationsBox.start({
					'height': 120,
					'width':140
					});
			
				} else {
					element.removeClass('notificationsOpen');
					element.addClass('notificationsClosed');
					notificationsSlide.start({
						'height': 20,
						'width':100
					});
					notificationsBox.start({
						'height': 20,
						'width':100
					});
		
				};
		
			});
											   });
			var subscriptionsTrigger = $$('.subscriptionsClosed');
		
			subscriptionsTrigger.each(function(element){
			var prime = parseInt(element.id);
			var subscriptionsSlide = new Fx.Styles(''+prime+'subscriptionsDrop',{duration:100, wait:false});	
			var subscriptionsBox = new Fx.Styles(''+prime+'subscriptionsSlide',{duration:100, wait:false});	
			element.removeEvents();
			element.addEvent('click', function(){
 
			if (element.className.indexOf('subscriptionsClosed') >= 0)	{
				element.removeClass('subscriptionsClosed');
				element.addClass('subscriptionsOpen');
				subscriptionsSlide.start({
				'height': 100,
				'width':180
				});
				subscriptionsBox.start({
				'height': 120,
				'width':180
				});
		
			} else {
				element.removeClass('subscriptionsOpen');
				element.addClass('subscriptionsClosed');
				subscriptionsSlide.start({
					'height': 20,
					'width':130
				});
				subscriptionsBox.start({
					'height': 20,
					'width':130
				});
	
			};
	
		});	
										   });
	}else{
		alert('You are already subscribed to this team.');
	}
}



function removeEmailConfirm(id){
	if(document.all)e = event;
	var obj = document.getElementById('emailConfirm');
	obj.innerHTML = '<div style="padding:5px 5px 5px 5px;">Are You Sure?<br><input type="button" value="Yes" onclick="removeEmail(\''+id+'\');">&nbsp;<input type="button" value="No" onclick="closeConfirm(\'emailConfirm\');"></div>';
	var obj2 = document.getElementById(id);
	var curleft = curtop = 0;
	if (obj2.offsetParent) {
		curleft = obj2.offsetLeft
		curtop = obj2.offsetTop
		while (obj2 = obj2.offsetParent) {
			curleft += obj2.offsetLeft
			curtop += obj2.offsetTop
		}
	}
	
	obj.style.left = curleft + 200 + 'px';
	obj.style.top = curtop + 'px';
	obj.style.display = '';
}

function removeEmailConfirm2(id, div, obj2){
	if(document.all)e = event;
	var obj = document.getElementById('emailConfirm');
	obj.innerHTML = '<div style="padding:5px 5px 5px 5px;">Are You Sure?<br><input type="button" value="Yes" onclick="removeEmail2(\''+id+'\',\''+div.id+'\');">&nbsp;<input type="button" value="No" onclick="closeConfirm(\'emailConfirm\');"></div>';
	var curleft = curtop = 0;
	if (obj2.offsetParent) {
		curleft = obj2.offsetLeft
		curtop = obj2.offsetTop
		while (obj2 = obj2.offsetParent) {
			curleft += obj2.offsetLeft
			curtop += obj2.offsetTop
		}
	}
	
	obj.style.left = curleft + 300 + 'px';
	obj.style.top = curtop + 'px';
	obj.style.display = '';
}

function removeEmail(id){
	closeConfirm('emailConfirm');
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'removeEmail', id, removeSuccess);
}

function removeEmail2(id, div){
	closeConfirm('emailConfirm');
	$(div).style.display = 'none';
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'removeEmail', id, removeSuccess2);
}

function removeSuccess2(r){
	return;
}


function toggleEventPhotoShow(id){
	var Objs = $$('.'+id);
	Objs.each(function(element){
		if(element.style.display == 'none'){
			element.style.display = '';
		}else{
			element.style.display = 'none';
		}
		
					   });
}

function deletePhoto(id){
	var answer = confirm("Are you sure you want to delete this photo? ");
	if(answer){
		document.getElementById(id).style.display = 'none';
	    hs.close(document.getElementById(id+'pic'));
		DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'removePhoto', id, removePhotoSuccess);
	}
}

function deletePhoto2(id){
	var answer = confirm("Are you sure you want to delete this photo? ");
	if(answer){
		document.getElementById(id).style.display = 'none';
	    hs.close(document.getElementById(id+'pic'));
		DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'removePhoto', id, removePhotoSuccess2);
	}
}


function deletePhotoMulti(id){
	document.getElementById(id).style.display = 'none';
	hs.close(document.getElementById(id+'pic'));
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'removePhoto', id, removePhotoSuccess2);
}

function removePhotoSuccess2(r){
	return;
}

function removePhotoSuccess(r){
	if(r[0][0] == 0){
		document.getElementById(r[0][1]).style.display = 'none';
	}	
}

function rotatePhoto(id, cont_id){
	hs.close(document.getElementById(id+'pic'));
	hs.getExpander(document.getElementById(id+'pic')).showLoading2();
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'callRotateImage', id, cont_id, rotatePhotoSuccess);
}

function rotatePhotoSuccess(r){
	obj = document.getElementById(r[0]+'pic');
	obj2 = document.getElementById(r[0]+'IMG');
	obj.removeChild(obj2);
	var temp = obj.innerHTML;
	obj.innerHTML = '<img src="/bleachers/images/thumb/'+r[1]+'?' + (new Date()).getTime() +'" id="'+r[0]+'IMG" alt="'+r[0]+'" title="Click to enlarge"/></a>' + temp;
	hs.expand(document.getElementById(r[0]+'pic'));
}

function rotatePhotoSuccess2(r){
	obj = document.getElementById(r[0]+'pic');
	obj2 = document.getElementById(r[0]+'IMG');
	if(obj2){
		obj.removeChild(obj2);
	}
	var temp = obj.innerHTML;
	obj.innerHTML = '<img src="/bleachers/images/thumb/'+r[1]+'?' + (new Date()).getTime() +'" id="'+r[0]+'IMG" alt="'+r[0]+'" title="Click to enlarge"/></a>' + temp;
}
function rotatePhotoSuccessMulti(r){
	numRotated++;
	var ids = clickedVals.split(",");
	$('imagesLeft').innerHTML = 'Image ' + numRotated.toString() + ' of ' + (ids.length-1).toString();
	if(r[0] == ids[ids.length-2]){
		$('doActionBtn').disabled = false;
		if($('hideLoadBtn')){
			//var evObj = window.event;
			$('hideLoadBtn').fireEvent('click');
		}
	}
	//alert(r[0]);
	obj = document.getElementById(r[0]+'pic');
	//obj2 = document.getElementById(r[0]+'IMG');
	//alert(obj);
	//alert(obj2);
	//if(obj2){
		//obj2.parentNode.removeChild(obj2);
	//}
	obj.innerHTML = '';
	obj.innerHTML = '<img src="/bleachers/images/thumb/'+r[1]+'?' + (new Date()).getTime() +'" id="'+r[0]+'IMG" alt="'+r[0]+'" title="Click to enlarge"/></a>';
}

function loadTeams(r){
	$('teamField').innerHTML='';
	var Obj = document.getElementById('teamField');
	pObj = document.createElement('option');	
	pObj.setAttribute('value', '-1');
	pObj.innerHTML = 'Choose Team';
	if(r[1] < 1){
		$("warningID").style.display = '';
	}else{
		$("warningID").style.display = 'none';		
	}
	Obj.appendChild(pObj);
	pObj = document.createElement('option');	
	pObj.setAttribute('value', 'AddAll,'+r[0].sc_id[0]);
	pObj.innerHTML = 'Add All Teams';
	Obj.appendChild(pObj);
	pObj = document.createElement('option');	
	pObj.setAttribute('value', 'RemoveAll,'+r[0].sc_id[0]);
	pObj.innerHTML = 'Remove All Teams';
	Obj.appendChild(pObj);
	for (var i=0; i < r[0].getRowCount(); i++)
	{
		pObj = document.createElement('option');
		pObj.setAttribute('value', r[0].sc_id[i]+','+r[0].sport_id[i]+','+r[0].level_id[i]+','+r[0].sex_id[i]);
		pObj.innerHTML = r[0].sport[i]+' '+r[0].level[i]+' '+r[0].division[i];
		Obj.appendChild(pObj);		
	};
}


function joinBleachers(){
	document.getElementById('addBleachForm').submit();
}

function showSchoolObj(){
	var obj = $$('.schoolObjArea');
	obj.each(function(element){
		if(element.style.display == 'none'){
			element.style.display = 'block';
			showRecentSchools();
			$('teamField').style.display = 'none';
			$('schoolField').style.display = 'none';	
		}else{
			element.style.display = 'none';
			$('teamField').style.display = '';
			$('schoolField').style.display = '';	
		}
					  });
		
}

function showRecentSchools(){
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'recentSchools', populateRecentSchools);
}

function populateRecentSchools(r){
	var obj = document.getElementById('schoolRecent');
	obj.innerHTML = '';
	if(r != -1){
		for (var i=0; i < r.getRowCount(); i++){
			var aObj = document.createElement('A');
			//aObj.addClass('schoolLinks');
			aObj.setAttribute('class', 'schoolLinks2');
			aObj.setAttribute('className', 'schoolLinks2');			
			aObj.setAttribute('href', 'javascript:void(0);');
			aObj.setAttribute('id', r.sc_id[i]);
			aObj.innerHTML = r.highschool[i] + ' - ' + r.city[i] + ', ' + r.state[i];
			var brObj = document.createElement('BR');
			obj.appendChild(aObj);
			obj.appendChild(brObj);
		}
		links = $$('.schoolLinks2');
		links.each(function(element){
			element.removeEvents();								
			element.addEvent('click', function(e){
				e = new Event(e);
				DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getTeams', element.id, loadTeams);	   
							showSchoolObj();
							nObj = document.createElement('option');
							nObj.setAttribute('value', element.id);
							nObj.innerHTML= element.innerHTML.substr(0, element.innerHTML.indexOf('-')-1);
							nObj.selected = true;
							opts = $('schoolField').options;
							var addObj = true;
							for(var i = 0; i < opts.length; i++){
								if(opts[i].value == element.id){
									addObj = false;
									$('schoolField').options[i].selected = true;
								}
							}
							if(addObj){
								$('schoolField').appendChild(nObj);
							}
				e.stop();
			});
		});			
	}else{
		obj.innerHTML='No Recent Schools';
	}
}

function searchSchools(searchStr){
	if(searchStr != ''){
		DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'searchSchools', searchStr, populateSchools);		
	}
}

function checkKey(e, str){
	var keynum;
		
	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	if(keynum == 13){
		searchSchools(str.value);
	}
}

function populateSchools(r){
	var obj = document.getElementById('schoolRes');
	obj.style.display = '';
	obj.innerHTML = '';
	if(r.getRowCount() > 0){
		for (var i=0; i < r.getRowCount(); i++){
			var aObj = document.createElement('A');
			//aObj.addClass('schoolLinks');
			aObj.setAttribute('class', 'schoolLinks');
			aObj.setAttribute('className', 'schoolLinks');			
			aObj.setAttribute('href', 'javascript:void(0);');
			aObj.setAttribute('id', r.sc_id[i]);		
			aObj.innerHTML = r.highschool[i] + ' - ' + r.city[i] + ', ' + r.state[i];
			var brObj = document.createElement('BR');
			obj.appendChild(aObj);
			obj.appendChild(brObj);
		}
		links = $$('.schoolLinks');
		links.each(function(element){
			element.removeEvents();								
			element.addEvent('click', function(e){
				e = new Event(e);
				DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getTeams', element.id, loadTeams);	   
							showSchoolObj();
							nObj = document.createElement('option');
							nObj.setAttribute('value', element.id);
							nObj.innerHTML= element.innerHTML.substr(0, element.innerHTML.indexOf('-')-1);
							nObj.selected = true;
							opts = $('schoolField').options;
							var addObj = true;
							for(var i = 0; i < opts.length; i++){
								if(opts[i].value == element.id){
									addObj = false;
									$('schoolField').options[i].selected = true;
								}
							}
							if(addObj){
								$('schoolField').appendChild(nObj);
							}
				e.stop();
			});
		});				
	}else{
		obj.innerHTML='No Schools Found';
	}
}

var prevAddr = -1;
function appendSMSAddress(obj, idnum){
	if(obj.value != -1){
		emailObj = $('email'+idnum+'ID');
		lastI = emailObj.value.indexOf(prevAddr);
		if(lastI != -1){
			emailObj.value = emailObj.value.substr(0, lastI); 
		}
		emailObj.value += obj.value;
		prevAddr = obj.value;
	}
}



//New functions for bleachers management

//function to perform action on selected images
function disableUIforAction(val, e){
	$('imagesLeft').innerHTML = '';
	$('doActionBtn').disabled = true;
	if($('showLoadBtn') && val == 'R'){
		$('showLoadBtn').fireEvent('click', 0 , e);
	}
}

var numRotated;
function doAction(obj, clickedVals, cont, e){
	if(obj.value != -1){
		setTimeout(function(){disableUIforAction(obj.value, e);}, 0);
	}
	switch(obj.value){
		//rotate images
		case 'R':	
			numRotated = 0;
			var ids = clickedVals.split(",");	
			var justVids = true;
			for(var i = 0; i < ids.length-1; i++){
				//check if item is an image
				if($(ids[i]+'Checkbox').value == "image"){
					//rotate if an image
					justVids = false;
					DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'callRotateImage', ids[i], cont, rotatePhotoSuccessMulti);
				}
			}
			if(justVids){
				setTimeout(function(){var evObj = window.event;$('hideLoadBtn').fireEvent('click', 0 , evObj);$('doActionBtn').disabled = false;}, 700);
			}
			break;
		case 'C':
			//show change event dialog
			$('hiddenchange').fireEvent('click', e, 0);
			break;
		case 'D':
			var answer = confirm("Are you sure you want to delete these photos? ");
			if(answer){
				var ids = clickedVals.split(",");
				for(var i = 0; i < ids.length-1; i++){
					//check if item is an image
					if($(ids[i]+'Checkbox').value == "image"){
						//rotate if an image
						deletePhotoMulti(ids[i]);
					}
				}
			}
			$('doActionBtn').disabled = false;
			break;
		default:
			$('doActionBtn').disabled = false;			
			return;
	}	
}

function selectAll(checkVal){
	$$('.highslide').each(function(element){
		//get checkbox
		var theElement = document.getElementById(element.parentNode.id + 'Checkbox');
		if(theElement){
			theElement.checked = checkVal;
			//call handleclick
	   		handleClick(theElement, element.parentNode.id);
		}
	});
}

function handleClick(element, uid){
	var uid = uid.toString();
	var selanimation = new Fx.Style(element.parentNode, 'backgroundColor', {duration:300, wait:false});
	if(element.checked){
		if(clickedVals.indexOf(uid) == -1){
			clickedVals += uid + ',';																	   	
		}
		selanimation.set("#ffffff");
		selanimation.start("#fffc00");
	}else{
		var tempStart = clickedVals.substr(0,clickedVals.indexOf(uid));
		var tempEnd = clickedVals.substr(clickedVals.indexOf(uid)+uid.length+1);
		clickedVals = tempStart + tempEnd;
		selanimation.set("#fffc00");
		selanimation.start("#ffffff");
	}
}


//populate change event when one image clicked
function populateChangeDiag(uid){
	$('changeVals').innerHTML = $(uid.toString()+'pic').innerHTML;
	clickedVals = uid + ',';
	$('currEventTitle').innerHTML = 'Current Event:  '+ $('currEvent').value;
}

//populate change event when multiple images clicked
function populateChangeDiag2(){
	var ids = clickedVals.split(",");
	var obj = $('changeVals');
	obj.innerHTML = "";
	for(var i = 0; i < ids.length-1; i++){
		obj.innerHTML += $(ids[i].toString()+'pic').innerHTML;
	}	
	$('currEventTitle').innerHTML = 'Current Event:  '+ $('currEvent').value;
}

//populate change event when one image clicked on gallery page
function populateChangeDiag3(uid, sc_id, id){
	if(clickedVals != ''){
		selectAll(false);
	}
	$('changeVals').innerHTML = $(uid.toString()+'pic').innerHTML;
	clickedVals = uid+',';
	$('sc_idInput').value = sc_id;
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'getSportsList', sc_id, showSportList);		
	$('currEventTitle').innerHTML = 'Current Event:  '+ $(id).value;
}


//get list of sports for change event on gallery page
function showSportList(r){
	obj = $('sportSelector');
	for (var i=0; i < r.getRowCount(); i++){
		var opt = document.createElement('option');
		opt.setAttribute('value', r.sportid[i]);
		opt.innerHTML = r.sport[i];
		obj.appendChild(opt);
	}
}

//get events for sport and year
function showEvents(obj, yobj, sc_id){
	$('subBtnEvent').style.visibility = 'visible';
	$('subBtnEvent').disabled = false;
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'showEventsForSport', obj.value, yobj.value, sc_id, showEventsSuccess);
}

//show events for sport and year
function showEventsSuccess(r){
	obj = $('eventList');
	obj.innerHTML = '';
	obj.style.display = '';
	var opt = document.createElement('option');
	opt.setAttribute('value', -1);
	opt.innerHTML = 'Select an Event';
	for (var i=0; i < r.getRowCount(); i++){
		var opt = document.createElement('option');
		opt.setAttribute('value', r.event_id[i]);
		opt.innerHTML = r.eventdate[i].getMonth()+1 + '/' + r.eventdate[i].getDate() + '/' + r.eventdate[i].getFullYear() + ' ' + r.reporting[i] + ' ' + r.sport[i] + ' ' + r.level[i] + ' ' + r.division[i];
		if(r.place[i].toLowerCase() == 'home'){
			opt.innerHTML += ' @ ';
		}else{
			opt.innerHTML += ' vs. ';
		}
		if(r.opponent[i] != null)
			opt.innerHTML += r.opponent[i];
		obj.appendChild(opt);
	}
	var opt = document.createElement('option');
	opt.setAttribute('value', -1);
	opt.innerHTML = 'Event Not Listed';
	obj.appendChild(opt);
}

//check form to change event prior to submission
function doBleachChange(form){
	if(form.event_id == -1){
		alert('Please Select an Event');
		return false;
	}else{
		form.imgVals.value = clickedVals;
		return true;
	}
}


//check unsubscribe form
function unsubForm(form, e){
	if(form.unsubradio[1].checked){
		$('unSubBtnId').fireEvent('click', 0, e);
		return false;
	}else{
		return true;
	}
}



//edit subscriptions page
function makeEditable(obj, div, idnum, primary, etype){
	var oldemail = div.innerHTML;
	var oldclick = obj.onclick;
	obj.onclick = function(){
		obj.onclick = function(){makeEditable(obj, div, idnum, primary, etype);};
		div.innerHTML = oldemail;
	};
	var inp1 = document.createElement('input');
	inp1.setAttribute('value', div.innerHTML);
	inp1.setAttribute('type', 'input');
	var span1 = document.createElement('span');
	if(etype == 'email'){
		span1.innerHTML = 'Email Address';
	}else if(etype == 'phone'){
		span1.innerHTML = 'Phone Number';
	}else{
		span1.innerHTML = 'Twitter Username';
	}
	var inp2 = document.createElement('input');
	inp2.setAttribute('value', div.innerHTML);
	if(etype != 'twitter'){
		inp2.setAttribute('type', 'input');
	}else{
		inp2.setAttribute('type', 'password');
	}
	var span2 = document.createElement('span');
	if(etype == 'email'){
		span2.innerHTML = 'Confirm Email Address';
	}else if(etype == 'phone'){
		span2.innerHTML = 'Confirm Phone Number';
	}else{
		span2.innerHTML = "Twitter Password";
	}
	var btn = document.createElement('a');
	btn.innerHTML = 'Save';	
	btn.setAttribute('className', 'btnSave');
	btn.setAttribute('class', 'btnSave');
	var btn2 = document.createElement('a');
	btn2.innerHTML = 'Cancel';	
	btn2.setAttribute('className', 'btnCancel');
	btn2.setAttribute('class', 'btnCancel');
	div.innerHTML = '';
	div.appendChild(span1);
	div.appendChild(inp1);
	div.appendChild(document.createElement('br'));
	div.appendChild(span2);
	div.appendChild(inp2);
	div.appendChild(document.createElement('br'));
	div.appendChild(btn);
	div.appendChild(btn2);
	if(etype != "email" && etype != "twitter"){
		div2 = document.createElement('div');
		div2.style.textAlign = 'left';
		div2.style.margin = '0px 10px 5px 10px'; 
		div2.style.fontSize = '10px';
	    div2.innerHTML = 'HighSchoolSports.net Messages are now powered by 4INFO!<br />Enter your cell phone number like this:  <strong>3043172525</strong><br />Include your area code, but not a "1" or dashes, then click SAVE.'
		div.appendChild(div2);
	}
	btn2.onclick = function(){	
		div.innerHTML = oldemail;
		obj.onclick = oldclick;
	};
	btn.onclick = function(){	
		if(inp1.value != oldemail){				
			if(etype == 'email'){
				if(!validateEmail(inp1.value)){
					alert('Please Enter a Valid Email Address');
				}else if(inp1.value != inp2.value){
					alert('Please Confirm Email Address');
				}else{					
					DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'saveEmail', inp1.value, oldemail, idnum, primary, div.id, obj.id, saveEmailSuccess);				
				}
			}else if(etype == 'phone'){
				if(!validatePhone(inp1.value)){
					alert('Please Enter a Valid Phone Number');
				}else if(inp1.value != inp2.value){
					alert('Please Confirm Phone Number');
				}else{
					DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'savePhone', inp1.value, oldemail, idnum, obj.id, savePhoneSuccess);				
				}
			}else{
				DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'saveTwitter', inp1.value, oldemail, inp2.value, idnum, obj.id, saveTwitterSuccess);				
			}
			
		}else{
			obj.disabled = false;
			div.innerHTML = oldemail;
		}
		obj.onclick = oldclick;
	};
}
function savePhoneSuccess(r){
	if(r != -1){
		window.location = window.location;
	}else{
		alert('Phone number already in use.');
	}
}

function saveTwitterSuccess(r){
	if(r[0] != -1){
		window.location = window.location;
	}else{
		alert(r[1]);
	}
}

function saveEmailSuccess(r){
	if(r[0] != -1){
		$(r[1]).innerHTML = r[0];
		$(r[1]+'Msg').innerHTML = 'Not Activated';
	}else{
		alert('Email address already in use.');
	}
}

function checkAddEmail(form){
	if(form.addtype[0].checked){
		//do email
		var msg = '';
		if(!validateEmail(form.email.value)){
			msg += 'Please Enter a Valid Email Address\n';
		}
		if(form.email.value != form.conemail.value){
			msg += 'Please Confirm Email Address\n';
		}
		if(msg != ''){
			alert(msg);
			return false;
		}else{
			return true;
		}
	}else if(form.addtype[1].checked){
		//do text message
		var msg = '';
		if(!validatePhone(form.email.value)){
			msg += 'Please Enter a Valid Phone Number\n';
		}
		if(form.email.value != form.conemail.value){
			msg += 'Please Confirm Phone Number\n';
		}
		if(msg != ''){
			alert(msg);		
			return false;		
		}else{
			return true;	
		}
		
	}else{
		//do twitter
		return true;	
	}
}

function confirmPhone(conf, idnum, email, count){
	DWREngine._execute(_ajaxConfig._cfscriptLocation, null, 'confirmPhone', conf, idnum, email, count, confirmPhoneSuccess);				
}
function confirmPhoneSuccess(r){
	if(r == -1){
		alert("Invalid Confirmation Code");
	}else{
		$('email'+r+'Msg').innerHTML = 'Activated';
	}
}