/*
 * 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);
}
//For the Question.inc page
//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=='fristDate'){
		document.getElementById("start").checked=true;
		document.getElementById("endDate").value='';
	}else{
		document.getElementById("end").checked=true;
		document.getElementById("fristDate").value='';
	}
}
//For the Question.inc page
//when use select the start radio, then clear the end date field
function selectStart(){		
	var temp =document.getElementById("start");
	if(temp.checked){		
		document.getElementById("endDate").value='';
		var current = currentDate();
		document.getElementById("fristDate").value=current;
		 
	}		
} 
//For the Question.inc page
//when use select the end radio,then clear the start date field
function selectEnd(){
	
	var temp =document.getElementById("end");
	if(temp.checked){
		document.getElementById("fristDate").value='';
		var current = currentDate();
		document.getElementById("endDate").value=current;
	}	
}
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 endsWith(str, s){
	var reg = new RegExp(s + "$");
	return reg.test(str);
}
function writeTopBannerFile(bannerName,contomWidth){

	var url = document.URL;
	var end = url.indexOf(":");
	var protocol = url.substring(0,end);

	if (protocol == "https") {
		bannerName = bannerName.replace("http","https");
	}

	if (endsWith(bannerName, '.swf') || endsWith(bannerName, '.SWF')){
		document.write('<embed src="' + bannerName + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" class="'+contomWidth+'"></embed>');
	} else {
		document.write('<img src="' + bannerName + '" style="display:block;" border="0" class="'+contomWidth+'"/>');
	}
}

function writeLoginTopBannerFile(bannerName){
	var url = document.URL;
	var end = url.indexOf(":");
	var protocol = url.substring(0,end);

	if (protocol == "https") {
		bannerName = bannerName.replace("http","https");
	}

	if (endsWith(bannerName, '.swf') || endsWith(bannerName, '.SWF')){
		document.write('<embed src="' + bannerName + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" height="100" border="0" width="990"></embed>');
	} else {
		document.write('<img src="' + bannerName + '" alt="The Active Network" border="0" width="990px"/>');
	}
}

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) {
  var url = 'getTrainingPlanDetail.do';
  var innerHtml = $('plan_'+id+'_detail').innerHTML;
  if(innerHtml.empty()){
	  new Ajax.Request(url, {
	      method: 'POST',
	      parameters: { 'tpId': id },
	      onSuccess: function(t) {
	      	$('plan_'+id+'_detail').innerHTML = t.responseText;    	
	      },
	      onFailure: function() { 
	      alert("There was an error with the connection"); 
	    }
	  });
  }
  Effect.toggle('plan_'+id+'_detail', 'blind', { duration: 1 });
}

//
function tpSportSearch(sport){
	
	window.location = "tpSportSearch.do?sport="+sport.value;
}
//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 questionBack(){
	document.forms['cartForm'].back.value="Back";	
	document.forms['cartForm'].submit();
} 
function goBackToCartPage(){
	document.location="cart.do";
}

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) {
  			$("messagebox").hide();
  			$('search_results_div').innerHTML = t.responseText;
  		},
  		onFailure : function() { 
	      	//alert("There was an error happening!");
	      	$("messagebox").hide();
	    },
	    onException : function() {
	    	$("messagebox").hide();
	    }
	})
}

/*
 * disable the applay button, when user click the checkout button
 */
function disableApplyButton(){
	var applyButton = document.getElementById("applyId");
	if (applyButton != null) {
		applyButton.disabled=true;
		applyButton.className="none";
	}
}
function storeTemporaryCardInformation(targetForm) {
	targetForm.temporaryCardNumber.value = document.getElementById('creditCardNumber').value;
	targetForm.temporaryCardType.value = document.getElementById('creditCardType').value;
	targetForm.temporaryCardCsc.value = document.getElementById('creditCardCsc').value;
	targetForm.temporaryExpireMonth.value = document.getElementById('expMonth').value;
	targetForm.temporaryExpireYear.value = document.getElementById('expYear').value;
}