//All functions and variables are accessed through the ACORN object
var ACORN = {
	documentform:document.getElementById('form'),//cache the document object for quicker access
	passedsection:[false,false,false,false],
	clickedsection:[false,false,false,false],
	duplicateemailfound:false,
	toggle:function(label,b_flag){
		//true means warninglabel (red)
		if(b_flag){
			$('#'+label).toggleClass('warninglabel',true);
			$('#'+label).toggleClass('validlabel',false);
		} else {
			$('#'+label).toggleClass('validlabel',true);
			$('#'+label).toggleClass('warninglabel',false);
		}
	},
	checktxtinput:function(label,o_qap_input){
		if(typeof o_qap_input==='undefined' || o_qap_input.value.length===0){
			this.toggle(label,true);
		} else {
			this.toggle(label,false);
		}
	},
	checkpassword:function(label,o_qap_input){
		if(typeof o_qap_input==='undefined' || o_qap_input.value.length<4){
			this.toggle(label,true);
		} else {
			this.toggle(label,false);
		}
	},
	checkbothpasswords:function(){
		var _b_flag=false;
		if(this.qap_slt0_581867.value!=this.retype.value ||
			this.qap_slt0_581867.value.length===0 ||
			this.retype.value.length===0){
			this.toggle('label_p1',true);
			this.toggle('label_p2',true);
		} else {
			this.toggle('label_p1',false);
			this.toggle('label_p2',false);
			_b_flag=true;
		}
		return _b_flag;
	},
	checkemail:function(label,o_qap_input){
		var _b_flag=false;
		if(typeof o_qap_input==='undefined' || !o_qap_input.value.match(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i)){
			this.toggle(label,true);
		} else {
			this.toggle(label,false);
			_b_flag=true;
		}
		return _b_flag;
	},
	checkbothemails:function(){
		var _b_flag=false;
		if(this.qap_slt1_329053.value!=this.email2.value ||
			this.qap_slt1_329053.value.length===0 ||
			this.email2.value.length===0){
			this.toggle('label_e1',true);
			this.toggle('label_e2',true);
		} else {
			this.toggle('label_e1',false);
			this.toggle('label_e2',false);
			_b_flag=true;
		}
		return _b_flag;
	},
	checkmsgformat:function(){//todo:refactor to checkradios
		if(this.documentform.qap_r_329054[0].checked == false && this.documentform.qap_r_329054[1].checked == false){
			this.toggle('label_mf',true);
		} else {
			this.toggle('label_mf',false);
		}
	},
	checkselect:function(label,o_qap_input){
		if(typeof o_qap_input==='undefined' || o_qap_input.value=='') {
			this.toggle(label,true);
		} else {
			this.toggle(label,false);
		}
	},
	checkpostcode:function(){
		if(!this.documentform.qap_slt0_329058.value.match(/^[0-9]{4}$/)){
			this.toggle('label_pc',true);
		} else {
			this.toggle('label_pc',false);
		}
	},
	checkradios:function(a_qap_inputs,s_id){
		var _b_flag = false;
		if(typeof a_qap_inputs!=='undefined'){
			for(var i=0;i<a_qap_inputs.length;i++){
				if(a_qap_inputs[i].checked){
					_b_flag= true;
					break;
				}
			}
		}
		if(!_b_flag){
			this.toggle(s_id,true);
		} else {
			this.toggle(s_id,false);
		}
	},
	checkhouseholdchildrenagegroups:function(){
		var _b_flag = false;
		if($('#label_householdkids_under5').attr('class')=='warninglabel' ||
			$('#label_householdkids_5_11').attr('class')=='warninglabel' ||
			$('#label_householdkids_12_17').attr('class')=='warninglabel' ||
			$('#label_householdkids_over18').attr('class')=='warninglabel'){
			this.toggle('label_householdkids',true);
		} else {
			this.toggle('label_householdkids',false);
			_b_flag = true;
		}
		return _b_flag;
	},
	checktopics:function(){
		var _parent = this;
		$(document).ready(function(){
			var _b_other_flag = false;
			var _b_topics_group_flag = false;
			$(_parent.topics_other).each(function(){
				if(this.checked){
					_b_other_flag = true;
				}
			});
			$(_parent.topics).each(function(){
				if(this.checked){
					_b_topics_group_flag = true;
				}
			});
			if(_b_other_flag || _b_topics_group_flag){
				_parent.toggle('topicsofinterest',false);
			} else {
				_parent.toggle('topicsofinterest',true);
			}
		});
		
	},
	checknext3years:function(){
		var _b_flag_next3years_none = false,
		_b_flag_next3years = false;
		if(this.next3years_none.checked){
			_b_flag_next3years_none = true;
			$(this.next3years).each(function(){
				this.checked=false;
			});
		}
		$(this.next3years).each(function(){
			if(this.checked){
				_b_flag_next3years=true;
			}
		});
		if((_b_flag_next3years_none && _b_flag_next3years) || _b_flag_next3years_none || _b_flag_next3years){
			this.toggle('nextthreeyears',false);
		} else {
			this.toggle('nextthreeyears',true);
		}
	},
	formcheck:function(s_id,i_arrayindex){
		$(s_id).each(function(){
			if($(this).hasClass('warninglabel')){
				ACORN.passedsection[i_arrayindex]=false;
				return false;
			} else {
				ACORN.passedsection[i_arrayindex]=true;
			}
		});
	}
};

//store the qaps for reference later
ACORN.qap_slt0_329055=ACORN.documentform.qap_slt0_329055;
ACORN.qap_slt0_329056=ACORN.documentform.qap_slt0_329056;
ACORN.qap_slt1_329053=ACORN.documentform.qap_slt1_329053;
ACORN.email2=ACORN.documentform.email2;
ACORN.qap_slt0_581867=ACORN.documentform.qap_slt0_581867;
ACORN.retype=ACORN.documentform.retype;
ACORN.qap_r_329054=ACORN.documentform.qap_r_329054;
ACORN.qap_dd_329057=ACORN.documentform.qap_dd_329057;
ACORN.qap_r_334971=ACORN.documentform.qap_r_334971;
ACORN.qap_dd_329059=ACORN.documentform.qap_dd_329059;
ACORN.qap_dd_329060=ACORN.documentform.qap_dd_329060;
ACORN.qap_dd_329062=ACORN.documentform.qap_dd_329062;
ACORN.qap_dd_329063=ACORN.documentform.qap_dd_329063;
ACORN.qap_dd_329064=ACORN.documentform.qap_dd_329064;
ACORN.qap_dd_329066=ACORN.documentform.qap_dd_329066;
ACORN.qap_dd_329067=ACORN.documentform.qap_dd_329067;
ACORN.seg=ACORN.documentform.seg;
//part_two
ACORN.qap_dd_329069=ACORN.documentform.qap_dd_329069;
ACORN.qap_r_333265=ACORN.documentform.qap_r_333265;
ACORN.qap_r_333267=ACORN.documentform.qap_r_333267;
ACORN.qap_r_333268=ACORN.documentform.qap_r_333268;
ACORN.qap_r_333269=ACORN.documentform.qap_r_333269;
ACORN.qap_dd_329071=ACORN.documentform.qap_dd_329071;
ACORN.qap_dd_329072=ACORN.documentform.qap_dd_329072;
ACORN.qap_mc_329073_501422=ACORN.documentform.qap_mc_329073_501422;
//part_four
ACORN.topics=[ACORN.documentform.qap_mc_329087_506803,
ACORN.documentform.qap_mc_329087_506810,
ACORN.documentform.qap_mc_329087_506816,
ACORN.documentform.qap_mc_329087_506804,
ACORN.documentform.qap_mc_329087_506811,
ACORN.documentform.qap_mc_329087_506817,
ACORN.documentform.qap_mc_329087_506805,
ACORN.documentform.qap_mc_329087_506812,
ACORN.documentform.qap_mc_329087_506818,
ACORN.documentform.qap_mc_329087_506806,
ACORN.documentform.qap_mc_329087_506821,
ACORN.documentform.qap_mc_329087_506813,
ACORN.documentform.qap_mc_329087_506819,
ACORN.documentform.qap_mc_329087_506807,
ACORN.documentform.qap_mc_329087_506814,
ACORN.documentform.qap_mc_329087_506808,
ACORN.documentform.qap_mc_329087_506815,
ACORN.documentform.qap_mc_329087_506809];
ACORN.topics_other=ACORN.documentform.qap_mc_329087_506820;//the "Other" checkbox

ACORN.next3years=[ACORN.documentform.qap_mc_332467_518978,
ACORN.documentform.qap_mc_332467_518979,
ACORN.documentform.qap_mc_332467_518980,
ACORN.documentform.qap_mc_332467_518981,
ACORN.documentform.qap_mc_332467_518982,
ACORN.documentform.qap_mc_332467_518983,
ACORN.documentform.qap_mc_332467_518984];
ACORN.next3years_none=ACORN.documentform.qap_mc_332467_518985;

ACORN.qap_r_332468=ACORN.documentform.qap_r_332468;
ACORN.captcha_code=ACORN.documentform.captcha_code;

$.postJSON = function(url, data, callback) {$.post(url, data, callback, "json");};

ACORN.lockaccordions = function(){
	$('#part_one_header *').bind('click',function(){
		return false;
	});
	$('#part_two_header *').bind('click',function(){
		return false;
	});
	$('#part_three_header *').bind('click',function(){
		return false;
	});
	$('#part_four_header *').bind('click',function(){
		return false;
	});
	$('#part_one_header').click(function(){
		return false;
	});
	$('#part_two_header').click(function(){
		return false;
	});
	$('#part_three_header').click(function(){
		return false;
	});
	$('#part_three_header').click(function(){
		return false;
	});
};

$('#part_one_header').click(function(){
	if(ACORN.duplicateemailfound){
		ACORN.lockaccordions();
	}
});

$(document).ready(function(){

	//Firefox autocomplete fix
	if($.browser.mozilla){
		$("#form").attr("autocomplete", "off");
	}
	
	//initialise accordion
	$('#accordion').accordion({
		header:'.acornideas_accordionheader',
		autoHeight:false
	});

	

	$(ACORN.next3years_none).click(function(){
		$(ACORN.next3years).each(function(){
			this.checked=false;
		});
	});
	$(ACORN.next3years).each(function(){
		$(this).click(function(){
			if(ACORN.next3years_none.checked){
				ACORN.next3years_none.checked=false;
			}
		})
	});

	//--------------accordion header clicks---------------------------------

	ACORN.lockaccordions();
	
	$('#part_one_header').click(function(){
		if(ACORN.duplicateemailfound){
			ACORN.lockaccordions();
		}
	});
	//--------------'Next' button clicks------------------------------------
	$('#part1').click(function(){
		window.location.replace('#part_one_header');
		var _b_emailflag=false;
		var _b_passwordflag=false;
		ACORN.checktxtinput('label_fn',ACORN.qap_slt0_329055);
		ACORN.checktxtinput('label_sn',ACORN.qap_slt0_329056);
		ACORN.checkemail('label_e1',ACORN.qap_slt1_329053);
		ACORN.checkemail('label_e2',ACORN.email2);
		ACORN.checkpassword('label_p1', ACORN.qap_slt0_581867);
		ACORN.checkpassword('label_p2', ACORN.retype);
		//ACORN.checkmsgformat();
		ACORN.checkselect('label_state',ACORN.qap_dd_329057);
		ACORN.checkpostcode();
		ACORN.checkradios(ACORN.qap_r_334971, 'label_g');
		ACORN.checkselect('label_year',ACORN.qap_dd_329059);
		ACORN.checkselect('label_marital',ACORN.qap_dd_329060);
		ACORN.checkselect('label_work',ACORN.qap_dd_329062);
		ACORN.checkselect('label_edu',ACORN.qap_dd_329063);
		ACORN.checkselect('label_gahi',ACORN.qap_dd_329064);
		ACORN.checkselect('label_emp',ACORN.qap_dd_329066);
		ACORN.checkselect('label_industry',ACORN.qap_dd_329067);
		ACORN.checkselect('label_segs',ACORN.seg);
		_b_emailflag=ACORN.checkbothemails();
		_b_passwordflag=ACORN.checkbothpasswords();
		if(!_b_passwordflag){
			$('#part1error_passworddiv').html("<span style='color:red;'>Passwords must be at least 4 characters long/passwords don't match.</span>");
		} else {
			$('#part1error_passworddiv').html("");
		}
		if(!_b_emailflag){
			$('#part1error_emaildiv').html("<span style='color:red;'>Invalid email(s)/emails don't match.</span>");
		} else {
			$('#part1error_emaildiv').html("");
			$('#dialog').jqmShow();
			$.postJSON("services/check_email.php",{ 'email' : ACORN.qap_slt1_329053.value , 's' : document.form.s.value },
				function(data){
					if (data.status=='0'){
						ACORN.duplicateemailfound = false;
						$('#part1error_emaildiv2').html("");
						ACORN.formcheck('#part_one *', 0);//sets the value of ACORN.passedsection[0]
						$('#part1error').remove();
						if(!ACORN.passedsection[0]){
							$('#part1errortd').html('<br /><span id="part1error" class="ui-state-highlight ui-corner-all errorlabel"><span><span class="ui-icon ui-icon-alert errorlabelicon" style="float: left; margin-right: 0.3em;" />Please complete this section<br />before proceeding. Thanks!</span></span>');
						} else {
							$('#part_one_header *').bind('click',function(){
								$("#accordion").accordion('activate','#part_one_header');
							});
							$('#part_two_header *').bind('click',function(){
								$("#accordion").accordion('activate','#part_two_header');
							});
							$("#accordion").accordion('activate','#part_two_header');
						}
					}else if (data.status=='1'){
						ACORN.duplicateemailfound = true;
						$('#part_one_header *').bind('click',function(){
							return false;
						});
						$('#part_two_header *').bind('click',function(){
							return false;
						});
						$('#part_three_header *').bind('click',function(){
							return false;
						});
						$('#part_four_header *').bind('click',function(){
							return false;
						});
						ACORN.toggle('label_e1', true);
						ACORN.toggle('label_e2', true);
						$('#part1error_emaildiv2').html("<span style='color:red;'>This email is already registered.</span>");
						$('#part1errortd').html('<br /><span id="part1error" class="ui-state-highlight ui-corner-all errorlabel"><span><span class="ui-icon ui-icon-alert errorlabelicon" style="float: left; margin-right: 0.3em;" />Please complete this section<br />before proceeding. Thanks!</span></span>');
					}else{
						$('#content').html("<p class=\"error\">An error occurred. Sorry for the inconvenience, please try again later.</p>")
					}
					$('#dialog').jqmHide();
				}
			);
		}
	});
	$('#part2').click(function(){
		window.location.replace('#part_two_header');
		ACORN.checktxtinput('label_householdpeople', ACORN.qap_dd_329069);
		ACORN.checkradios(ACORN.qap_r_333265, 'label_householdkids_under5');
		ACORN.checkradios(ACORN.qap_r_333267, 'label_householdkids_5_11');
		ACORN.checkradios(ACORN.qap_r_333268, 'label_householdkids_12_17');
		ACORN.checkradios(ACORN.qap_r_333269, 'label_householdkids_over18');
		ACORN.checkhouseholdchildrenagegroups();
		ACORN.checkselect('label_householdpets',ACORN.qap_dd_329071);
		ACORN.checkselect('label_accomodation', ACORN.qap_dd_329072);
		ACORN.formcheck('#part_two *', 1);
		$('#part2errordiv').html('');
		if(!ACORN.passedsection[1]){//true means that validation hasn't passed
			$('#part2errordiv').html('<br /><span id="part2error" class="ui-state-highlight ui-corner-all errorlabel"><span><span class="ui-icon ui-icon-alert errorlabelicon" style="float: left; margin-right: 0.3em;" />Please complete this section<br />before proceeding. Thanks!</span></span>');
		} else {
			$('#part_three_header *').bind('click',function(){
				$("#accordion").accordion('activate','#part_three_header');
			});
			$("#accordion").accordion('activate','#part_three_header');
		}
	});
	$('#part3').click(function(){
		window.location.replace('#part_three_header');
		$('#part_four_header *').bind('click',function(){
			$("#accordion").accordion('activate','#part_four_header');
		});
		$("#accordion").accordion('activate','#part_four_header');

	});
	$('#part4').click(function(){
		window.location.replace('#part_four_header');
		ACORN.checktopics();
		ACORN.checknext3years();
		ACORN.checkradios(ACORN.qap_r_332468,'internetaccess');
		ACORN.checktxtinput('captcha_code_label',ACORN.captcha_code);
		ACORN.formcheck('#part_one *',0);
		ACORN.formcheck('#part_two *',1);
		ACORN.formcheck('#part_four *', 3);
		$('#part4errordiv').html('');
		if(!ACORN.passedsection[0] || !ACORN.passedsection[1]){
			$('#part4warning').html("<br /><span style='color:red;float:right;position:relative;left:-35px;'>Please complete earlier sections.</span>");
		} else if(!ACORN.passedsection[3]){
			$('#part4errordiv').html('<br /><span id="part4error" class="ui-state-highlight ui-corner-all errorlabel"><span><span class="ui-icon ui-icon-alert errorlabelicon" style="float: left; margin-right: 0.3em;" />Please complete this section before proceeding. Thanks!</span></span>');
			document.getElementById('captcha').src = 'includes/securimage/securimage_show.php?' + Math.random();
		} else if(ACORN.passedsection[0] && ACORN.passedsection[1] && ACORN.passedsection[3]){
			$('#dialog').jqmShow();
			$.postJSON("services/subscribe.php",$("#form").serialize(),
				function(data){
					if (data.status=='0'){
						$('#content').html("<h1>Thank You!</h1><p>&nbsp;</p><span><p>Your registration has been submitted! We will send you an email to confirm your email address shortly - <u>you need to click the link in this email to activate your account.</u></p><p>&nbsp;</p><p>Once you confirm your registration, your name will be entered into the draw to win <u>$500 cash</u>!</p><p>&nbsp;</p><p><strong>The Acorn Ideas Team.</strong></p></span>");
					}else if (data.status=='1'){
						ACORN.toggle('captcha_code_label',true);
						$('#part4warning').html("<br /><span style='color:red;float:right;position:relative;left:-35px;'>The code did not match. Please try again.</span>");
						document.getElementById('captcha').src = 'includes/securimage/securimage_show.php?' + Math.random();
					}else{
						$('#content').html("<p class=\"error\">An error occurred. Sorry for the inconvenience, please try again later.</p>")
					}
					$('#dialog').jqmHide();
				}
			);
		}
	});
	
	$('#accordion').show();

});