var currentElement = -1;
var heights = new Array(450, 400, 320);

function showAllForm(index){
	smm.$('agreeToTerms').checked=true;
	smm.$('membertype').selectedIndex=index;	
	showHideAccountTypes(true);
	showForm(index);
}
function showForm(index){
		//alert('Index: '+index+'\nCurrent: '+currentElement);
	if(index > 0){
		if(currentElement != -1) { //Close open blind
			var blind_effect = new Spry.Effect.Blind('form'+currentElement, {duration:800, from:heights[currentElement-1]+'px', to:'0px'});
			blind_effect.start();
		}
		currentElement = index;
		var blind_effect = new Spry.Effect.Blind('form'+currentElement, {duration:800, from:'0px', to:heights[currentElement-1]+'px'});
	}else{
		var blind_effect = new Spry.Effect.Blind('form'+currentElement, {duration:800, from:heights[currentElement-1]+'px', to:'0px'});
		currentElement = -1;
	}
	blind_effect.start();
}
function showHideAccountTypes(show){
	if(show){
		var blind_effect = new Spry.Effect.Blind('accounttypes', {duration:800, from:'0px', to:'90px'});
	}else{
		if(currentElement != -1) { showForm(0); } //hides the accounttypes when checkbox is cleared
		var blind_effect = new Spry.Effect.Blind('accounttypes', {duration:800, from:'1000px', to:'0px'});
	}
	blind_effect.start();
}
function showState(countryField, stateField) {
	if(smm.$(countryField).value == 222 ) {
		//$('StateRow').style.display = 'block';
		smm.$(stateField).disabled = false;
		selectCheck(stateField);
	} else {
		smm.$(stateField).disabled = true;
		smm.$(stateField).selectedIndex = 0;
		smm.$(stateField+'Check').value=true;
		
		//$('StateRow').style.display = 'none';
	}
}

function selectCheck(field) {
	//var field = smm.$(type).name+'Check'; //name of the hidden check field for select box;
		if(smm.$(field).selectedIndex) {
			smm.$(field+'Check').value = true;
		} else {
			smm.$(field+'Check').value = '';
		}
}

function handleCountryChange(field, targetFieldName){
	if(field.selectedIndex == -1) return;
	if(targetFieldName == undefined) targetFieldName = "stateid";
	
	showState(field.name, targetFieldName);
	selectCheck(field.name);
}

window.onload = function(){
	//single form
	selectCheck("genderid"); 
	handleCountryChange(smm.$("countryid"), "stateid"); 
	//couples form
	selectCheck("genderid1"); //field 1
	selectCheck("genderid2"); //field 2
	handleCountryChange(smm.$("countryid1"), 'stateid1'); 
	handleCountryChange(smm.$("countryid2"), 'stateid2'); 
}