/*
 * Various Javascript utility/commonly-used functions.
 */
 
// Generic popup window
function popUp( url ) {
	var windowFeatures = "scrollbars=yes,resizable=yes,width=350,height=300";
    var popUp = window.open(url,"popup",windowFeatures);
    popUp.focus();
}

// Popup window that resizes if it is open when it is called again
function popUp( url, width, height ) {
	var windowFeatures = "scrollbars=yes,resizable=yes,dependent=yes,width=" + width + ",height=" + height;
    var popUp = window.open(url,"popup",windowFeatures);
    if (window.focus) { popUp.focus(); }
    popUp.resizeTo(width + 2, height + 30);
}

// Reloads the current page
function refresh() 
{
	window.location.reload(false);
}

// Prints the current page
function printPage() 
{  
	if (window.self) 
	{
		window.print(); 
	}
}
//used in glossary.jsp
function changeDivStatus(i)
			{
				for(j=1;j<=5;j++){
					if(j==i){
						document.getElementById('div'+j).style.display='block';
						document.getElementById('span'+j).style.textDecoration='none';
						
					}else{
						document.getElementById('div'+j).style.display='none';
						document.getElementById('span'+j).style.textDecoration='underline';
					}
				}
			}
function checkKeyPress(e){
	var key = window.event ? e.keyCode : e.which;
    if(key.toString() == "13"){
        if(window.event){
            e.returnValue = false;
            e.cancelBubble = true
        }else{
            e.stopPropagation();
            e.preventDefault();
        }
    }
}

function isIE6()
{
	if(navigator.userAgent.indexOf("MSIE 6")   !=-1)
		return true;
	else
		return false;
}
function dayPreviewPopup( url, width, height ) { 
     var popupFeatures = "scrollbars=yes,resizable=yes,width=" + width + ",height=" + height; 
     var popup = window.open(url,"popup",popupFeatures); 
     if (window.focus) { popup.focus(); } 

     // for some reason causes IE7 to ignore 'return false' in the onclick and send the main window to the href
	 // popup.resizeTo(width + 2, height + 30); 
}
function toggleDetails(id, hideDIV, type) {
  var url = 'getTrainingPlanDetail.do';
  if (type == 'membership') {
  	url = 'getMembershipDetail.do';
  }
  var innerHtml = $(hideDIV).innerHTML;
  if(innerHtml.empty()){
	  new Ajax.Request(url, {
	      method: 'POST',
	      parameters: { 'tpId': id, 'uuid': id, hideDIV :  hideDIV},
	      onSuccess: function(t) {
	      	$(hideDIV).innerHTML = t.responseText;    	
	      },
	      onFailure: function() { 
	      alert("There was an error with the connection"); 
	    }
	  });
  }
  Effect.toggle(hideDIV, 'blind', { duration: 1 });
}

//
function tpSportSearch(sport, range){
	if (sport < 0) {
		sport = 0;
	}	
	window.location = "tpSportSearch.do?sport=" + sport + "&range=" + range;
}
//highlight author area
function highlightAuthor(elem) {
	if (document.getElementById) {
		theElement = document.getElementById(elem);
	} else if (document.all) {
		theElement = document.elem;
	}
	theElement.style.background = '#DFEFFF';
}

function unHighlightAuthor(elem,color) {
	if (document.getElementById) {
		theElement = document.getElementById(elem);
	} else if (document.all) {
		theElement = document.elem;
	}
theElement.style.background = color;
} 

function changeSize(mypic){ 
    var maxWidth=990; 
    var minHeight=89; 

    var width = mypic.width;
    if (width > maxWidth){
    	mypic.width = maxWidth;
	}       
    var height = mypic.height;      
    if (height >= minHeight){
	   var divHeight = height + "px";
	   var emptyBoxHeight = (height - minHeight)/2;
	   document.getElementById("athlete_header").style.height=divHeight;
	   if(emptyBoxHeight > 13){
		   var boxHeight = emptyBoxHeight +"px";
		   document.getElementById("emptyBox").style.height=boxHeight; 
	   }
	}
}
var TPSearchDelay = {
    timeout : null,
    updateSearch : function(){
       	this.abort();
        this.timeout = setTimeout(function(){
           	TPSearchDelay.abort();
			updateSearchListForTP();
       }, 1200);
       
    },
    abort : function(){
        if(this.timeout != null) {
            clearTimeout(this.timeout);
			this.timeout = null;
        }
    } 
};

function searchFilterForTP() {
	TPSearchDelay.updateSearch();
}

function updateSearchListForTP() {
	$('refineSearch').value = 'refineSearch';
	new Dialog.Box("messagebox");
	$("messagebox").persistent_show();
	$('searchFilterForTPForm').request({
  		method : 'POST',
  		onCreate : function() {
  		},
  		onComplete : function(t) {  			
  			$('search_results_div').innerHTML = t.responseText;
  			$("messagebox").hide();
  		},
  		onFailure : function() { 
	      	//alert("There was an error happening!");
	      	$("messagebox").hide();
	    },
	    onException : function() {
	    	$("messagebox").hide();
	    }
	})
}
var downloadTpStatus = false;
function downloadTpHint(id, uuid) {
	if (myLightWindow == null) {
		myLightWindow = new lightwindow();
	}
	if (!downloadTpStatus) {
		downloadTpStatus = true;
		myLightWindow.activateWindow({
			href: 'downloadTPHint.do?id=' + id + '&uuid=' + uuid,
			type : 'page',
			rel	: '',
			title : '',
			width: 500,
			height : 200
		});
		var tempTimeout = setTimeout(function(){
			downloadTpStatus = false;
			clearTimeout(tempTimeout);
		}, 1000);
	}	
}

function blinkMyTrainingContent() {
	var target = document.getElementById('myTrainingContentDiv');
	if (target.style.display == 'block') {
		target.style.display = 'none';
		document.getElementById('myTrainingTitleSymbolSpan').innerHTML = "&#9658";
	} else {
		target.style.display = 'block';
		document.getElementById('myTrainingTitleSymbolSpan').innerHTML = "&#9660";
	}
}

//when user input the value to the Start(end)field, then select the start(end) radio and clear the end(start)date field
function checkSelect(objName){;
	var dateField = objName;
	if(dateField.id=='startDateString'){
		document.getElementById("start").checked=true;
		document.getElementById("eventDateString").value='';
	}else{
		document.getElementById("end").checked=true;
		document.getElementById("startDateString").value='';
	}
}

//when use select the start radio, then clear the end date field
function selectStart(){		
	var temp =document.getElementById("start");
	if(temp.checked){		
		document.getElementById("eventDateString").value='';
		document.getElementById("startDateString").value = currentDate();		 
	}		
}
//when use select the end radio,then clear the start date field
function selectEnd(){	
	var temp =document.getElementById("end");
	if(temp.checked){
		document.getElementById("startDateString").value='';
		document.getElementById("eventDateString").value = currentDate();
	}	
}
function currentDate(){
	var datDate4= new Date();
	var MSIE=navigator.userAgent.indexOf("MSIE");
	 var month=datDate4.getMonth() + 1;
	 if(month<10){
	 	month = "0"+month;
	 }
	 var day =datDate4.getDate();
	 if(day<10){
	 	day = "0"+day;
	 }
	 if(MSIE!=-1){
	 var year=datDate4.getYear();
	 }else{
	 var year=datDate4.getYear()+1900;
	 }
	 var newDate = (month + '/' + day + '/' + year);
	 return newDate;
} 
function updateExpireStatus(tmtId) {
	new Ajax.Request("updateExpireStatus.do", {
	      method: 'POST',
	      parameters: { 'tmtId': tmtId},
	      onSuccess: function(t) {
	      // do nothing
	      },
	      onFailure: function() { 
	      //do nothing
	    }
	});
}
function downloadFreeTrainingplan(planId) {
	window.location = "downloadTP.do?id=" + planId;
}

function updateCurrentAlertStatus(alertId) {
	new Ajax.Request("updateAlertStatusById.do", {
	      method: 'POST',
	      parameters: {'alertId': alertId},
	      onSuccess: function(t) {
	    	  $('changeTpDateBar').innerHTML = t.responseText;
	      },
	      onFailure: function() { 
	      //do nothing
	    }
	});
}
