// ==============================
// Javascript for Enrollment Form
// ==============================


// This counter is required to make the image URL unique
var counter = 0;
function newCaptchaImage(imageSrc) {
  counter = counter + 1;
  var image = document.getElementById('captcha-image');
  image.src = '' + imageSrc + counter;
}


//Displays the fields for the Rural Road Adress
function showRRFields() {
	var header = document.getElementById('poBoxHeader');
	header.style.display = "";
	var div = document.getElementById('poBoxFields');
	div.style.display = "block";
	var link = document.getElementById('poBoxLink');
	link.style.display = "none";
	correctLabelSize();
	correctFormNo();
}

//Checks the acceptance for the corporate enrollment form
function checkAcceptanceCorporate(button, form, alertText){
	if (form.accept.checked == true){
		button.disabled = true;
		startSpinner();
	    form.submit();
	} else{
		alert(alertText);
	}
}

function stateSelect(cbo) {
	
	// Unselected or "Select..."
	if ((cbo.selectedIndex == -1)||(cbo.selectedIndex == 0))
		return;

	// Value
	var s = cbo.options[cbo.selectedIndex].value;

	if ((s=='AB')||(s=='BC')||(s=='MB')||(s=='NB')||(s=='NF')||(s=='NS')||
		(s=='ON')||(s=='PE')||(s=='PQ')||(s=='SK')||(s=='SP')||(s=='YU')) {
	
		cbo.form.addr_countryCode.selectedIndex = 1; //Canada
	}
	else {
		cbo.form.addr_countryCode.selectedIndex = 2; //US
	}

	foreign(cbo.form.addr_countryCode);
}


function correctAddr(s) {
	copyAddr(s,'addr_streetNumber');
	copyAddr(s,'addr_streetNumberSuffix');
	copyAddr(s,'addr_suiteNumber');
	copyAddr(s,'addr_streetName');
	copyAddr(s,'addr_streetDirection');
	copyAddr(s,'addr_boxNumber');
	copyAddr(s,'addr_postalStationName');
	copyAddr(s,'addr_ruralRouteNumber');
	copyAddr(s,'addr_municipalityName');
	copyAddr(s,'addr_countryCode');
	copyAddr(s,'addr_stateProvinceCode');
	copyAddr(s,'addr_postalCode');
}

function copyAddr(s,name) {
	var dst = eval('document.getElementById(\'' + name + '\')');
	var src = eval('document.getElementById(\'' + s + name + '\')');

	dst.value = src.value;
}

//Show/Hide the provinces, states or PostalCode/Zip code depending on the country selected
function showHide_ProvState(country,formAction,formName) {
	var actionPath = document.getElementById('actionPath').value;
	if (formName == '' || formName == undefined){
		formName = 'EnrollmentAgentForm';
	}

	if (country == 'CA' || country == 'US') {
		if (formAction == '1') {
			if (document.getElementById('formActionDo').value.match(/address.do/)) {
				document.getElementById(formName).action = '/' + actionPath + '/address.do';
			} else {
				document.getElementById(formName).action = '/' + actionPath + '/process.do';
			}
		}
		
		document.getElementById('div_provinceStateFields').style.display = '';

		if (country == 'CA') {
			document.getElementById('addr_provinceCode').style.display = '';
			document.getElementById('addr_stateCode').style.display = 'none';

			document.getElementById('addr_provinceCode').focus();		
		} else if (country == 'US') {
			document.getElementById('addr_provinceCode').style.display = 'none';
			document.getElementById('addr_stateCode').style.display = '';

			document.getElementById('addr_stateCode').focus();	
		}
	} else {
		if (country != '' && formAction == '1') {
			document.getElementById(formName).action = '/' + actionPath + '/process.do';
		}
		
		document.getElementById('addr_stateProvinceCode').value = '';
		document.getElementById('div_provinceStateFields').style.display = 'none';
		document.getElementById('addr_provinceCode').value = '';
		document.getElementById('addr_stateCode').value = '';
	}
}

// Enable/Disable International Code on Phone based on Country Code
function foreign(cbo) {
	if (cbo.selectedIndex == -1)
		return;

	var label = document.getElementById('overseasLabel');

	if ((cbo.options[cbo.selectedIndex].value == 'CA') || (cbo.options[cbo.selectedIndex].value == 'US')) {
		if (label != null)
			label.className = 'norm disabled';
		cbo.form.contact_zoneH.disabled = 'true';
		cbo.form.contact_zoneB.disabled = 'true';
		cbo.form.contact_zoneF.disabled = 'true';
	}
	else {
		if (label != null)
			label.className = 'norm';
		cbo.form.contact_zoneH.disabled = '';
		cbo.form.contact_zoneB.disabled = '';
		cbo.form.contact_zoneF.disabled = '';
	}
}

function checkAcceptance(current, submit, alertText, baseUrl) {
	var thisform = document.forms.EnrollmentForm;
	
	if (thisform.accept.checked == true) {
		submit.disabled = true;
		startSpinner();
		thisform.action = "/enroll/step" + current + ".do?nextStep=process";
		thisform.submit();
	}
	else {
		alert(alertText);
	}
}

function switchForm(current, step, baseUrl) {
	var thisform = document.forms[1];
	
	thisform.action = "/enroll/step" + current + ".do?nextStep=" + step;
	thisform.submit();
}
