/* JavaScript */

/******************************* SUBMIT APP CHECK ************************************/
function app_form_check() {
	var theform = document.app_form; // to keep code cleaner below
	var error_msg = '';
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; // for email validation
	
	var radio_isapplicant = 'not_checked';
	for (var i=0; i < theform.is_applicant.length; i++) {
		if(theform.is_applicant[i].checked) {
			radio_isapplicant = theform.is_applicant[i].value;
		}
	}
	 if(radio_isapplicant=='not_checked') { // NEITHER SELECTED
	 	if(!theform.mbr_number) {
			error_msg += 'Please select whether the person filling out the application is the prospective member \n';
		} else { // this is a renewal 
			error_msg += 'Please select whether the person filling out the application is the renewing member \n'; 
		}
		isapplicant.style.background = 'Yellow';
	} else {
		isapplicant.style.background = 'White';
	}
	if(radio_isapplicant=='0') { // NOT THE APPLICANT
		if(theform.rep_name.value=='') {
			error_msg += 'Please enter the Representative\'s Name \n';
			theform.rep_name.style.background = 'Yellow';
		} else {
			theform.rep_name.style.background = 'White';
		}
		if(theform.rep_company.value=='') {
			error_msg += 'Please enter the Representative\'s Company \n';
			theform.rep_company.style.background = 'Yellow';
		} else {
			theform.rep_company.style.background = 'White';
		}
		if((theform.rep_phone.value.length < 10) || (theform.rep_phone.value=='')) {
			error_msg += 'Please enter the Representative\'s Phone Number (including area code) \n';
			theform.rep_phone.style.background = 'Yellow';
		} else {
			theform.rep_phone.style.background = 'White';
		}
		if(reg.test(theform.rep_email.value) == false) {
			error_msg += 'Please enter the Representative\'s Email Address \n';
			theform.rep_email.style.background = 'Yellow';
		} else {
			theform.rep_email.style.background = 'White';
		}
	} // END IF NOT APPLICANT
	
	if(theform.mbr_company.value=='') {
		error_msg += 'Please enter the Applicant\'s Company \n';
		theform.mbr_company.style.background = 'Yellow';
	} else {
		theform.mbr_company.style.background = 'White';
	}
	if(theform.mbr_contact.value=='') {
		error_msg += 'Please enter the Contact Person \n';
		theform.mbr_contact.style.background = 'Yellow';
	} else {
		theform.mbr_contact.style.background = 'White';
	}
	if(reg.test(theform.mbr_email.value) == false) {
		error_msg += 'Please enter the Contact Person\'s Email Address \n';
		theform.mbr_email.style.background = 'Yellow';
	} else {
		theform.mbr_email.style.background = 'White';
	}
	if(theform.mbr_street.value=='') {
		error_msg += 'Please enter the Applicant\'s Street \n';
		theform.mbr_street.style.background = 'Yellow';
	} else {
		theform.mbr_street.style.background = 'White';
	}
	if(theform.mbr_city.value=='') {
		error_msg += 'Please enter the Applicant\'s City \n';
		theform.mbr_city.style.background = 'Yellow';
	} else {
		theform.mbr_city.style.background = 'White';
	}
	if(theform.mbr_state.value=='') {
		error_msg += 'Please enter the Applicant\'s State \n';
		theform.mbr_state.style.background = 'Yellow';
	} else {
		theform.mbr_state.style.background = 'White';
	}
	if(theform.mbr_zip.value=='') {
		error_msg += 'Please enter the Applicant\'s Zip Code \n';
		theform.mbr_zip.style.background = 'Yellow';
	} else {
		theform.mbr_zip.style.background = 'White';
	}
	if(theform.mbr_phone.value.length < 10) {
		error_msg += 'Please enter the Applicant\'s Phone Number \n';
		theform.mbr_phone.style.background = 'Yellow';
	} else {
		theform.mbr_phone.style.background = 'White';
	}
	if(theform.mbrship_type.value=='') {
		error_msg += 'Please Select the Membership Type \n';
		theform.mbrship_type.style.background = 'Yellow';
	} else {
		theform.mbrship_type.style.background = 'White';
	}
	if((theform.mbrship_type.value=='General_Contractor') || (theform.mbrship_type.value=='Sub_Contractor') || (theform.mbrship_type.value=='Project_Member')) {
		if(theform.ins_agent_name.value=='') {
			error_msg += 'Please enter your General Liability Insurance Agent Name \n';
			theform.ins_agent_name.style.background = 'Yellow';
		} else {
			theform.ins_agent_name.style.background = 'White';
		}
		if(theform.ins_agent_company.value=='') {
			error_msg += 'Please enter your General Liability Insurance Agent Company \n';
			theform.ins_agent_company.style.background = 'Yellow';
		} else {
			theform.ins_agent_company.style.background = 'White';
		}
		if((theform.mbrship_type.value=='General_Contractor' || theform.mbrship_type.value=='Sub_Contractor') && theform.carrier.value!=='Invitation Only - NuWay') {
			if(theform.gross_sales.value=='') {
				error_msg += 'Please enter your Projected Gross Sales \n';
				theform.gross_sales.style.background = 'Yellow';
			} else {
				theform.gross_sales.style.background = 'White';
			}
		}
		if(theform.mbrship_type.value=='Project_Member' && theform.carrier.value!=='Invitation Only - NuWay') {
			if(theform.project_length.value=='') {
				error_msg += 'Please enter the Estimated Project Length \n';
				theform.project_length.style.background = 'Yellow';
			} else {
				theform.project_length.style.background = 'White';
			}
			if(theform.project_sellout.value=='') {
				error_msg += 'Please enter the Projected Project Sell Out \n';
				theform.project_sellout.style.background = 'Yellow';
			} else {
				theform.project_sellout.style.background = 'White';
			}
		}
	}
	if (theform.mbrship_type.value=='Associate') {
		if(theform.biz_type.value=='') {
			error_msg += 'Please enter your Type of Business \n';
			theform.biz_type.style.background = 'Yellow';
		} else {
			theform.biz_type.style.background = 'White';
		}
		if(theform.num_employees.value=='') {
			error_msg += 'Please enter your Number of Employees \n';
			theform.num_employees.style.background = 'Yellow';
		} else {
			theform.num_employees.style.background = 'White';
		}
	}
	if (theform.mbrship_type.value!=='Associate') {
		var chk_rfj = 'not_checked';
		for (var i=0; i < theform.rfj_list.length; i++) {
			if(theform.rfj_list[i].checked) {
				chk_rfj = 'checked';
			}
		}
		if(theform.rfj_insurance.checked) {			
			chk_rfj = 'checked';
		}
		if(theform.rfj_other.checked) {			
			chk_rfj = 'checked';
		}
		if(chk_rfj=='not_checked') {
			error_msg += 'Please Select your Reason For Joining \n';
			rfj_label.style.background = 'Yellow';
		} else {
			rfj_label.style.background = 'White';
		}
		if(theform.rfj_insurance.checked) {
			if(theform.carrier.value=='') {
				error_msg += 'Please select your Insurance Carrier \n';
				theform.carrier.style.background = 'Yellow';
			} else {
				theform.carrier.style.background = 'White';
			}
		}
		if(theform.rfj_other.checked) {
			if(theform.other_rfj.value=='') {
				error_msg += 'Please select your Other Reason for Joining \n';
				theform.other_rfj.style.background = 'Yellow';
			} else {
				theform.other_rfj.style.background = 'White';
			}
		}
	}
	////// NuWay
	/*if(theform.carrier.value!=='Invitation Only - NuWay') {
		var radio_pymnt = 'not_checked';
		for (var i=0; i < theform.payment_method.length; i++) {
			if (theform.payment_method[i].checked) {
				radio_pymnt = theform.payment_method[i].value;
			}
		}			
		 if(radio_pymnt=='not_checked') { // NO PAYMENT TYPE SELECTED
			error_msg += 'Please select a Payment Method \n';
			lbl_paymentmethod.style.background = 'Yellow';
		} else {
			lbl_paymentmethod.style.background = 'White';
		}
	}*/
	////// END NuWay
	if(theform.ref1_name.value=='') { // References 
		error_msg += 'Please enter Reference #1 Name \n';
		theform.ref1_name.style.background = 'Yellow';
	} else {
		theform.ref1_name.style.background = 'White';
	}
	if(theform.ref1_company.value=='') {
		error_msg += 'Please enter Reference #1 Company \n';
		theform.ref1_company.style.background = 'Yellow';
	} else {
		theform.ref1_company.style.background = 'White';
	}
	if(theform.ref1_phone.value.length < 10) {
		error_msg += 'Please enter Reference #1 Phone (including area code) \n';
		theform.ref1_phone.style.background = 'Yellow';
	} else {
		theform.ref1_phone.style.background = 'White';
	}
	if (!theform.ref1_contractor.checked) {
		error_msg += 'You must verify Reference #1 to be a contractor \n';
		lbl_ref1_contractor.style.background = 'Yellow';
	} else {
		lbl_ref1_contractor.style.background = 'White';
	}
	if(theform.referral_name.value=='') {
		error_msg += 'Please enter the Name or Company of who referred you to the NWCA \n';
		theform.referral_name.style.background = 'Yellow';
	} else {
		theform.referral_name.style.background = 'White';
	}
	if (!theform.terms.checked) {
		error_msg += 'You must agree to the NWCA Terms and Conditions \n';
		lbl_terms.style.background = 'Yellow';
	} else {
		lbl_terms.style.background = 'White';
	}
	
	if(error_msg.length > 0) { // Generate Error Message 
		var msg = 'To submit your application data, we need you to correct the following errors:\n\n';
		alert(msg + error_msg);
		return false;
	}
} /******************************* END submit_app_check() ************************************/

function save_app_check() {
	var theform = document.app_form; // to keep code cleaner below
	var error_msg = '';
	var radio_value = 'not_checked';
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; // for email validation
	
	for (var i=0; i < theform.is_applicant.length; i++) {
		if (theform.is_applicant[i].checked) {
			radio_value = theform.is_applicant[i].value;
		}
	}
	if(radio_value=='0') { // NOT THE APPLICANT
		if(reg.test(theform.rep_email.value) == false) {
			error_msg += 'Please enter the Representative\'s Email Address \n';
			theform.rep_email.style.background = 'Yellow';
		} else {
			theform.rep_email.style.background = 'White';
		}
	} 
	if(radio_value=='1') { // IS THE APPLICANT
		if(reg.test(theform.mbr_email.value) == false) {
			error_msg += 'Please enter the Contact Person\'s Email Address \n';
			theform.mbr_email.style.background = 'Yellow';
		} else {
			theform.mbr_email.style.background = 'White';
		}
	}
	 if(radio_value=='not_checked') {
		error_msg += 'Please select whether the person filling out the application is the prospective member \n';
		isapplicant.style.background = 'Yellow';
	} else {
		isapplicant.style.background = 'White';
	}
	if(theform.mbr_company.value=='') {
		error_msg += 'Please enter the Applicant Company \n';
		theform.mbr_company.style.background = 'Yellow';
	} else {
			theform.mbr_company.style.background = 'White';
		}
	if(theform.mbr_contact.value=='') {
		error_msg += 'Please enter the Applicant Name \n';
		theform.mbr_contact.style.background = 'Yellow';
	} else {
			theform.mbr_contact.style.background = 'White';
		}
	if(error_msg.length > 0) {
		var msg = 'To save your application data, we need you to correct the following errors:\n\n';
		alert(msg + error_msg);
		return false;
	}
} /******************************* END SUBMIT APP CHECK ************************************/

function show_hide_fields() {
	var theform = document.app_form; // just to keep code cleaner below
	var radio_isapplicant = '';
	for (var i=0; i < theform.is_applicant.length; i++) {
		if(theform.is_applicant[i].checked) {
			radio_isapplicant = theform.is_applicant[i].value;
		}
	}
	if(radio_isapplicant=='0') { // is_applicant is set to NO
		show('rep_info');
	} else {
		hide('rep_info');
	}
	if(theform.mbrship_type.value == 'General_Contractor' || theform.mbrship_type.value == 'Sub_Contractor') {
		show('grosssales');
		show('ins_agent');
		show('reason');
		hide('sellout');
		hide('projectlength');
		hide('numemployees');
	} else {
		hide('grosssales');
	}
	if(theform.mbrship_type.value == 'Associate') {
		show('biztype');
		show('numemployees');
		hide('reason');
		hide('sellout');
		hide('projectlength');
		hide('ins_agent');
	} else {
		hide('biztype');
		hide('numemployees');
	}
	if(theform.mbrship_type.value == 'Project_Member') {
		show('ins_agent');
		show('reason');
		show('projectlength');
		show('sellout');
	} else {
		hide('projectlength');
		hide('sellout');
	}
	///////
	if(theform.rfj_insurance.checked) {
		show('carrier');
		//show('ins_billing');
	} else {
		hide('carrier');
		//hide('ins_billing');
		theform.carrier.value = '';
	}
	if(theform.rfj_other.checked) {
		show('other_reason');
	} else {
		hide('other_reason');
		theform.other_rfj.value = '';
	}
	if(theform.also_interested_other.checked) {
		show('other_also');
	} else {
		hide('other_also');
		theform.other_also_interested.value = '';
	}
///// COMARKETING WITH ASSOCIATE
	if(theform.comarketing_chkbox.checked) {
		show('comarketing');
	} else {
		hide('comarketing');
		theform.co_marketing_assoc.value = '';
	}
	/////// NuWay
	if(theform.carrier.value=='Invitation Only - NuWay') {
		show('nuway_dues');
		hide('dues_calc_title');
		hide('grosssales');
		hide('amt_due');
		hide('sellout');
		//hide('paymentmethod');
	} else {
		hide('nuway_dues');
		show('dues_calc_title');
		show('amt_due');
		//show('paymentmethod');
	}
	var radio_save_submit = '';
	for (var i=0; i < theform.save_submit.length; i++) {
		if(theform.save_submit[i].checked) {
			radio_save_submit = theform.save_submit[i].value;
		}
	}
	if(radio_save_submit=='save') {
		show('save_app');
		//hide('paymentmethod');
		hide('submit_app');
	}
	if(radio_save_submit=='submit') {
		show('submit_app');
		hide('save_app');
	}
	/////// end NuWay
}
function toss(evt) {
	var unicode = evt.charCode ? evt.charCode : evt.keyCode;	
	// Allows numbers, backspace, and a period.
	if ((unicode < 48 || unicode > 57) && String.fromCharCode(unicode) != "."  && unicode != 8 && unicode != 9) {
		return false;
	} else {
		return true;
	}	
}
function calculate_dues() {
	var theform = document.app_form; // just to keep code cleaner below
	var input_num = 0;
	var dues_factor = 0;
	var dues = 0;
	if(theform.mbrship_type.value == 'General_Contractor') {
		input_num = eval(theform.gross_sales.value);
		//dues_factor = eval('0.0015');
		dues_factor = eval('0.0010');
		dues = input_num * dues_factor;
		if(dues < 250) {
			dues = 250;
		}
		if(dues > 10000) {
			dues = 10000;
		}
		theform.show_dues.value = '$' + dues.toFixed(2); // .toFixed(2) keeps 2 numbers after the decimal
		theform.total_dues.value = dues.toFixed(2);
	}
	if(theform.mbrship_type.value == 'Sub_Contractor') {
		input_num = eval(theform.gross_sales.value);
		//dues_factor = eval('0.002');
		dues_factor = eval('0.0010');
		dues = input_num * dues_factor;
		if(dues < 250) {
			dues = 250;
		}
		if(dues > 10000) {
			dues = 10000;
		}
		theform.show_dues.value = '$' + dues.toFixed(2); // .toFixed(2) keeps 2 numbers after the decimal
		theform.total_dues.value = dues.toFixed(2);
	}
	if(theform.mbrship_type.value == 'Associate') {
		if(theform.num_employees.value == '1-20') { dues = 500; }
		if(theform.num_employees.value == '21-50') { dues = 1000; }
		if(theform.num_employees.value == '51-100') { dues = 2500; }
		if(theform.num_employees.value == '100+') { dues = 5000; }
		theform.show_dues.value = '$' + dues;
		theform.total_dues.value = dues;
	}	
	if(theform.mbrship_type.value == 'Project_Member') {
		input_num = eval(theform.project_sellout.value);
		dues_factor = eval('0.001');
		dues = input_num * dues_factor;
		if(dues < 250) {
			dues = 250;
		}
		if(dues > 10000) {
			dues = 10000;
		}
		theform.show_dues.value = '$' + dues.toFixed(2); // .toFixed(2) keeps 2 numbers after the decimal
		theform.total_dues.value = dues.toFixed(2);
	}
}
function clear_dues() {
	var theform = document.app_form; // just to keep code cleaner below
	theform.show_dues.value = '';
	theform.total_dues.value = '';
	theform.gross_sales.value = '';	
}
function same_as_mailing() {
	var theform = document.app_form; // to keep code cleaner below
	var checkbox_status = theform.same.checked;
	if ( !checkbox_status ) {		
		theform.same.checked.checked = checkbox_status;
		theform.biz_street.value = '';
		theform.biz_city.value = '';
		theform.biz_state.value = '';
		theform.biz_zip.value = '';
	} else {
		theform.same.checked = checkbox_status;
		theform.biz_street.value = theform.mbr_street.value;
		theform.biz_city.value = theform.mbr_city.value;
		theform.biz_state.value = theform.mbr_state.value;
		theform.biz_zip.value = theform.mbr_zip.value;
	}
}
function show(id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	} else {
		if (document.layers) {	
			document.id.display = 'block';
		} else {
			document.all.id.style.display = 'block';
		}
	} 
}
function hide(id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	} else {
		if (document.layers) {	
			document.id.display = 'none';
		} else {
			document.all.id.style.display = 'none';
		}
	}
}
function reset_BGs() {
	var theform = document.app_form; // to keep code cleaner below
	theform.reset();
}
function index_form_check() {
	var theform = document.index_form; // to keep code cleaner below
	var error_msg = '';	
	var radio_index_form_action = 'not_checked';
	for (var i=0; i < theform.index_form_action.length; i++) {
		if (theform.index_form_action[i].checked) {
			radio_index_form_action = theform.index_form_action[i].value;
		}
	}
	 if(radio_index_form_action=='not_checked') { // NEITHER SELECTED
		error_msg += 'Please select an option \n';
	}
	 if(radio_index_form_action=='renew') { // NEITHER SELECTED
	 	if(theform.member_number.value=='') {
			error_msg += 'Please enter your NWCA Membership Number \n';
			theform.member_number.focus();
		}
	}
	if(error_msg.length > 0) { // Generate Error Message 
		alert(error_msg);
		return false;
	}
}

/*** POPUP WINDOW ***/
var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}
