var clicked_button = false;

popup = function(url,array){
	var x = 0;
	res = new Object();
	var_array = new Array();
	
	if(!array){
		array = new Array();
		}
	
	var_array['width'] = '450';
	var_array['height'] = '550';
	var_array['toolbar'] = 'no';
	var_array['menubar'] = 'no';
	var_array['location'] = 'no';
	var_array['scrollbars'] = 'no';
	var_array['resizable'] = 'no';
	var_array['top'] = '150';
	var_array['left'] = '150';
	
	for(x in var_array){
		if(array[x]){
			res[x] = array[x];
		}else{
			res[x] = var_array[x];
			}
		x++;
		}
	OpenWin = this.open(url, 'SFBC', 'height='+res.height+
			',width='+res.width+
			',toolbar='+res.toolbar+
			',menubar='+res.menubar+
			',location='+res.location+
			',scrollbars='+res.scrollbars+
			',resizable='+res.resizable+
			',top='+res.top+
			',left='+res.left);
	OpenWin.focus();
	}
	
popup_program = function(id){
	vars = new Array();
	var url = '/popup/program/'+id+'.html';
			
	vars['width'] = 650;
	vars['height'] = 550;
	vars['scrollbars'] = 'yes';
	popup(url,vars);
	}

step_3_validate = function(pform){
	switch(clicked_button){
		case 'c':
			var confirmed = false;
			confirmed = confirm('Are you sure you wish to cancel the enlistment process?');
			if(confirmed == true){
				pform.action = '/enlist/cancel.html';
				return true;
			}else{
				return false;
				}
		break;
		case 'b':
			window.location  = pform['back_location'].value;
		break;
		case 'n':
			var item_selected = false;
			var x = 0;
			while(x < pform.length){
				if(pform[x].type == 'radio' && pform[x].name == 'payment_method'){
					if(pform[x].checked){
						item_selected = true;
						}
					}
				x++;
				}
			if(!item_selected){
				alert('Please Select a Payment Method');
				return false;
			}else{
				return true;
				}
		break;
		}
	}
	
step_2_validate = function(pform){
	switch(clicked_button){
		case 'c':
			var confirmed = false;
			confirmed = confirm('Are you sure you wish to cancel the enlistment process?');
			if(confirmed == true){
				pform.action = '/enlist/cancel.html';
				return true;
			}else{
				return false;
				}
		break;
		default:
		case 'n':
			var error = '';
			var missing = '';
			required = new Array();
			
			required['first_name'] = 'First Name';
			required['last_name'] = 'Last Name';
			required['address_1'] = 'Address';
			required['city'] = 'City';
			required['province'] = 'Province';
			required['postal_code'] = 'Postal Code';
			required['home_phone'] = 'Home Phone';
			required['email'] = 'E-Mail';
			required['ec_name'] = 'Emergency Contact Name';
			required['ec_number'] = 'Emergency Contact Number';
			
			for(x in required){
				if(pform[x]){
					if(pform[x].value == ''){
						missing += "     "+required[x]+"\n";
						}
				}else{
					missing += "     "+required[x]+"\n";
					}
				}
				
			pform.birthdate.value = pform.dob_day.value+'/'+pform.dob_month.value+'/'+pform.dob_year.value;
			if(pform.birthdate){
				if(pform['birthdate'].value == ''){
					missing += "     Birth Date\n";
				}else{
					var bad_date_reason = '';
					b_date = pform.birthdate.value.split('/');
					if(b_date.length == 3){
						if(b_date[0] >= 31){
							var bad_date = true;
							bad_date_reason = "Input Day is greater than 31";
						}else{
							if(isNaN(b_date[0]*1)){
								b_date[0] = '00';
								var bad_date = true;
								bad_date_reason = "Input Day is not a number";
								}
							}
						if(b_date[1] > 12){
							if(b_date[0] <= 12 && b_date[1] <= 31){
								b_date[3] = b_date[0];
								b_date[0] = b_date[1];
								b_date[1] = b_date[3];
								b_date.pop();
							}else{
								var bad_date = true;
								bad_date_reason = "Input Month is greater than 12";
								}
						}else{
							if(isNaN(b_date[1]*1)){
								b_date[1] = '00';
								var bad_date = true;
								bad_date_reason = "Input Month is not a number";
								}
							}
						if(b_date[2] < 1900 || b_date[2] > 2005){
							var bad_date = true;
							bad_date_reason = "Input Year is not between 1900 and 2005";
						}else{
							if(isNaN(b_date[2]*1)){
								b_date[2] = '00';
								var bad_date = true;
								bad_date_reason = "Input Day is not a number";
								}
							}
						pform['birthdate'].value = b_date.join('/');
					}else{
						var bad_date = true;
						bad_date_reason = "Count not parse";
						}
					}
			}else{
				missing += "     Birth Date\n";
				}

			if(missing){
				error += 'The following required fields were left blank: '+"\n"+missing+"\n";
				}
			if(bad_date){
				error += 'Invalid Birth Date format. Please use: DD/MM/YYYY'+"\n"+bad_date_reason+"\n\n";
				}
				
			if(error){
				alert(error);
				return false;
			}else{
				return true;
				}
		break;
		}
		
	}
	
par_q_validate = function(pform){
	//alert('Validating...');
	//alert(clicked_button);
	
	switch(clicked_button){
		case 'c':
			var confirmed = false;
			confirmed = confirm('Are you sure you wish to cancel the enlistment process?');
			if(confirmed == true){
				pform.action = '/enlist/cancel.html';
				return true;
			}else{
				return false;
				}
		break;
		case 'b':
			window.location  = pform['back_location'].value;
		break;
		case 'n':
		default:
			var x = 0;
			var negative = 0+0;
			var positive = 0+0;
			var empty = 0;
			answers = new Object();
			while(x < pform.length){
				if(pform[x].type == 'radio'){
					if(answers[pform[x].name]){
					}else{
						answers[pform[x].name] = new Object();
						}
					answers[pform[x].name][pform[x].value] = new Object;
					answers[pform[x].name][pform[x].value].id = x;
					answers[pform[x].name][pform[x].value].value = pform[x].value;
					answers[pform[x].name][pform[x].value].checked = pform[x].checked;
					}
				x++;
				}
			
			for(x in answers){
				if(answers[x][1].checked){
					// do nothing
					positive++;
				}else if(answers[x][0].checked == true){
					negative++;
				}else{
					empty = 1;
					}
				}
				
			if(empty){
				alert('Please select an answer to each of the 7 questions');
				return false;
			}else if(negative){
				pform.action = '/enlist/no_par_q.html';
				return true;
			}else{
				pform.action = '/enlist/par_q.html';
				return true;
				}
		break;
		}	
	}
	
clearbox = function(object,default_text){
	if(object.value == default_text){
		object.value= '';
		}
	}
	
cancel_check = function(){
	confirmed = confirm('Are you sure you wish to cancel the enlistment process?');
	if(confirmed == true){
		document.location = '/enlist/logout.html';
		return true;
	}else{
		return false;
		}
	}
	
	