//
//Healthclubs javascript controls
//
/*******************
Don't need virtually all the main site js functionality, so just take what we need in order to cut down load
*******************/

var Dom = YAHOO.util.Dom;
var Evt = YAHOO.util.Event;
var $ = function(id) { return document.getElementById(id); }

Evt.onDOMReady( function (e) {
	// Set up all the hcFunctions
	YAHOO.Muscle.hcFunctions.init();
});

YAHOO.namespace("Muscle");

YAHOO.Muscle.hcFunctions = {
	init : function() {
		var hcFuncs = Dom.getElementsByClassName("hcFunction");
		
		if(hcFuncs.length > 0) {
			for(var i=0; i<hcFuncs.length; i++) {
				this.attachHandleControl(hcFuncs[i]);
			}
		}
	},
	
	attachHandleControl : function(handle) {
		Evt.addListener(handle,"click",this.clickHandler);
	},
	
	clickHandler : function(e) {
	    var eventFrom = Evt.getTarget(e);
	    var allowDefault = true;
	    var doSubmit = false;
	    
	    if (eventFrom.type == 'button') {
	    }
	    if (eventFrom.type == 'submit') {
	    }
	    if (eventFrom.type == 'checkbox') {
	    }
	    if (eventFrom.type == 'radio') {
	        if ((eventFrom.id == 'GenderFemale') && (Dom.hasClass('PreviewImage','malePreview'))) {
	            Dom.replaceClass('PreviewImage','malePreview','femalePreview');
	            previewLinkUpdate();
	        }
	        if ((eventFrom.id == 'GenderMale') && (Dom.hasClass('PreviewImage','femalePreview'))) {
	            Dom.replaceClass('PreviewImage','femalePreview','malePreview');
	            previewLinkUpdate();
	        }
	    }
	    
	    if (eventFrom.parentNode) {
	        if (eventFrom.parentNode.id.substring(0,9) == 'ListRowID') {
	            if (Dom.getElementsByClassName('rowSelected').length > 0) {
	                Dom.removeClass(Dom.getElementsByClassName('rowSelected'),'rowSelected');
	            }
	            Dom.addClass(eventFrom.parentNode,'rowSelected');
	        }
	    }

	    if ((Dom.hasClass(eventFrom,'activeLink')) && (Dom.getElementsByClassName('rowSelected').length > 0)) {
            $('Action').value = eventFrom.parentNode.id;
            $('Selected').value = Dom.getElementsByClassName('rowSelected')[0].id.substring(9)
	        
            document.MemberControls.submit();
        }
	    
	    if (!allowDefault) {
	        Evt.preventDefault(e);
	    }
	}
}

function chainSelectReload() {
    // Cause a page reload on the PT registration page when a gym chain is selected
    // IE doesn't get on with the unobtrusive js and the onchange event, so we have to leave this floating...
    
    $('ChainSelected').value = $('ChainSelect').value;
    document.AddPTForm.submit();
}

function goalProductUpdate() {
	// This is linked to inline for the same reason as the chainSelectReloader
	
	var baseProducts;
    var productIndex;

    if ($('AllowPreview').value = 'yes') {
        if ($('GoalSelect')[$('GoalSelect').selectedIndex].value != $('CurrentGoal').value) {
	        switch($('GoalSelect')[$('GoalSelect').selectedIndex].value) {
		        case 'BuildMuscle':
					baseProducts = new Array('progain','progainextreme','cyclone');
		        break;
		        case 'LoseFat':
		        	baseProducts = new Array('thermobol','promaxdiet','promaxdietbars');
		        break;
		        case 'LoseFatBuildMuscle':
		        	baseProducts = new Array('thermobol','promax','cyclone');
		        break;
		        case 'IncreaseStrength':
		        	baseProducts = new Array('creatamaxextreme','promax','cyclone');
		        break;
		        case 'ToneUp':
		        	baseProducts = new Array('thermobol','promaxdiet','promaxdietbars');
		        break;
		        case 'ImproveEndurance':
		        	baseProducts = new Array('viper','viperextreme','promax');
		        break;
		        case 'ImproveSportingPerformance':
		        	baseProducts = new Array('viperextreme','creatamaxextreme','cyclone');
		        break;
	        }
        
	        for (productIndex = 0; productIndex < $('Product1Select').length; productIndex++) {
		        if ($('Product1Select')[productIndex].value == baseProducts[0]) {
			        $('Product1Select').selectedIndex = productIndex;
		        }
				if ($('Product2Select')[productIndex].value == baseProducts[1]) {
			        $('Product2Select').selectedIndex = productIndex;
		        }
		        if ($('Product3Select')[productIndex].value == baseProducts[2]) {
			        $('Product3Select').selectedIndex = productIndex;
		        }
	        }
	        	        
	        $('CurrentGoal').value = $('GoalSelect')[$('GoalSelect').selectedIndex].value;
	    }
    }
}

function previewLinkUpdate() {
    // This is linked to inline for the same reason as the chainSelectReloader
    
    var linkBase;
    
    if ($('AllowPreview').value = 'yes') {
        if ($('GenderFemale').checked) {
            linkBase = 'http://www.maxiwomen.com/general/mypage.html?build=1';
        }
        else {
            linkBase = 'http://www.maximuscle.com/general/mypage.html?build=1';
        }
        
        linkBase += '&name=' + $('FirstNameEdit').value + ' ' + $('LastNameEdit').value;
        linkBase += '&goal=' + $('GoalSelect')[$('GoalSelect').selectedIndex].value.replace('&','and');
                
        if ($('Product1Select') != 'hidden') {
            linkBase += '&prod1=' + $('Product1Select')[$('Product1Select').selectedIndex].value;
            linkBase += '&prod2=' + $('Product2Select')[$('Product2Select').selectedIndex].value;
            linkBase += '&prod3=' + $('Product3Select')[$('Product3Select').selectedIndex].value;
        }
        
        if ($('GenderFemale').checked) {
            linkBase += '&gender=female';
        }
        else {
            linkBase += '&gender=male';
        }
       
        $('PreviewLink').innerHTML = 'Click this link to <a href="' + linkBase + '" target="_blank">preview page</a>.';
    }
}

function ShowAuxSetts(RptID) {
    // This is linked to inline for the same reason as the chainSelectReloader
    // TODO
}