/* main display function */
function showStep(step){
	
	$$('ul.form').setStyle('display','none');
	
	switch(step) {
		case '1':
			// hide next steps
			$$('#q2a','#q2b','#q2c', '#q3').setStyle('display','none');
			// but display framework
			$('step1').setStyle('display','block');
			
			// start showing steps if user is returning to these steps.
			if($$('input[name=fldSpecific]').getValue()){
				oppGeoInfo();
			}
			if(document.f.fldInterestedRegion2.value =="Other"){
				checkForOther('Other')
			}
			if( (document.f.fldInterestedRegion1.value)|| (document.f.fldInterestedRegion2.value) || (document.f.fldInterestedRegion3.value) ){
				showContinueButton('q3');
			}
		
		break;    
		case '2':
			// hide next steps
			$$('#q22','#q23', '#q24', '#q25', '#q26', '#q27', '#q28').setStyle('display','none');
			// but display framework
			$('step2').setStyle('display','block');
		
			// start showing steps if user is returning to these steps.
			if($$('input[name=fldExperience]').getValue()){
				retailExp();
			}
			if( (document.f.fldExpYears1.value) || (document.f.fldExpOther1.value) ){
				showContinueButton('q28');
			}
			
		break;
		case '3':
			// hide next steps
			$$('#q34').setStyle('display','none');
			// but display framework
			$('step3').setStyle('display','block');
		
			// start showing steps if user is returning to these steps.
			var myVal = $$('input[name=fldHasCapital]').getValue() + '' ;	
			var myVal2 =  $$('input[name=fldFinInfo]').getValue() + '' ;
		
			if(myVal != 'false,false,false' && myVal2 != 'false,false'){
				showContinueButton('q34');
			}			
		break;
		case '4':
			// hide next steps
			$('trueSubmit').setStyle('display','none');
			// but display framework
			$('step4').setStyle('display','block');
			
			checkPrivacy();
		break;
	}
}
/* main validation function */
function checkStep(step){

	switch(step) {
		case '1':
			
			var myVal = $$('input[name=fldSpecific]').getValue() + ' ' ;
			var yes = myVal.search(/Yes/) ;
			var allGood = false;
		
			if( yes != -1 ){
				if(document.f.fldInterestedRegion1.value){
					allGood = true;
				}
			}else{
				var no = myVal.search(/No/);
				if(no!=-1){
					if(document.f.fldInterestedRegion2.value =="Other"){
						if(document.f.fldInterestedRegion3.value){
							allGood = true;
						}
					}else if(document.f.fldInterestedRegion2.value){
						allGood = true;	
					}else{
						//no region
					}
				}
			}
			
			if(allGood){
				showStep('2')
			}else{
				alert('You must specify a retail opportunity.');
			}
		
		break;    
		case '2':
			
			if( (document.f.fldExpYears1.value) || (document.f.fldExpOther1.value) ){
				allGood = true;
			}else{
				var myVal = $$('input[name=fldExperience]').getValue() + ' ' ;
				var yes = myVal.search(/Yes/) ;
			
				if( yes != -1 ){
					var errormsg = "Please complete your retail experience."
				}else{
					var errormsg = "Please complete your qualifications."
				}
			}
			if(allGood){
				showStep('3');
			}else{
				alert(errormsg);
			}
		
		break;
		case '3':
			var myVal = $$('input[name=fldHasCapital]').getValue() + '' ;	
			var myVal2 =  $$('input[name=fldFinInfo]').getValue() + '' ;
	
			if(myVal != 'false,false,false' && myVal2 != 'false,false'){
				showStep('4');
			}else{
				alert('Please answer both Yes/No questions.')
			}
			
		break;
		case '4':
			// check that values are entered for fields
			// fldName,fldAddress,fldCity,fldProvince,fldPostal,fldPhone1,fldEmail
			var err = "";
			var fields = new Array("fldName","fldAddress","fldCity","fldPostal");
			var names = new Array('Name','Address','City','Postal Code');

			for(i=0;i<fields.length;i++){
				var fieldValue = $$('input[name=' + fields[i] + ']').getValue() + '';
				if( fieldValue.length < 1  ){err += "" + names[i] + " is required. \n";}
			}	
			
			if(!(document.f.fldProvince.value)) { err += "Province is required. \n" } 
			
			var phoneInput = $$('#phone1 input').getValue() + '';
			if(phoneInput.length < 11) { err += "Phone number is required. \n" } 		
			
			if( checkEmail( $$('input[name=fldEmail]').getValue() ) ){ }else{ err += "Valid E-mail address is required. \n" } 
			
			if(err.length>0){
				alert(err);
				return false;
			}else{
				$('trueSubmit').setStyle('display','none');
				document.f.submit();
			}

		break;
	}
}


/* step 1 functions */
function oppGeoInfo() {
	$$('#q2a','#q2b','#q2c','#q3').setStyle('display','none');
	
	var myVal = $$('input[name=fldSpecific]').getValue() + ' ' ;

	var yes = myVal.search(/Yes/) ;
	//alert(myVal + ' - ' + yes);
	
	if( yes != -1 ){
		$('q2a').setStyle('display','block');
	}else{
		var no = myVal.search(/No/);
		if(no!=-1){
			$('q2b').setStyle('display','block');
		}
	}
}

function checkForOther(myValue) {

	if( myValue == 'Other') {
		hideContinueButton('q3');
		$('q2c').setStyle('display','block');
	}else{
		$('q2c').setStyle('display','none');
		showContinueButton('q3');
	}
}

function showContinueButton(id) {
	$(id).setStyle('display','block');
}
function hideContinueButton(id) {
	$(id).setStyle('display','none');
}

/* step 2 functions */
function retailExp() {
	$$('#q22','#q23', '#q24', '#q25', '#q26', '#q27', '#q28').setStyle('display','none');
	
	var myVal = $$('input[name=fldExperience]').getValue() + ' ' ;

	var yes = myVal.search(/Yes/) ;
	//alert(myVal + ' - ' + yes);
	
	if( yes != -1 ){
		$$('#q22','#q23', '#q24', '#q25', '#q26').setStyle('display','block');
	}else{
		var no = myVal.search(/No/);
		if(no!=-1){
			$('q27').setStyle('display','block');
		}
	}
}
/* step 3 functions */
/* step 4 functions */
function checkPrivacy() {
	var myVal = $$('input[name=privacy]').getValue() + '' ;

	if( myVal == '1' ){
		$('submit').setStyle('display','none');
		$('trueSubmit').setStyle('display','block');
	}else{
		$('submit').setStyle('display','block');
		$('trueSubmit').setStyle('display','none');
	}
}

function checkEmail(strEmail) {
	//if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strEmail)){
	if (/^[\w\-\.\+\&]+\@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strEmail)){
		return (true);
	}
	return (false);
}

function jumpFocus(element,strLength,nextElement){
	if($(element).value.length == strLength){
		$(nextElement).focus();
	}
}

