// JavaScript Document

var panel1_show = true;
var panel2_show = false;
var panel3_show = false;
var panel4_show = false;

var is_hold = false;

function panel_hold(){is_hold = true;}
function panel_unhold(){is_hold = false;}

function toggle_panel_1(){
	if(is_hold) return;
	
	if(panel1_show){
		Effect.BlindUp('step1', {beforeStart: panel_hold,
					   			 afterFinish: panel_unhold});
		document.getElementById('toggle_image1').src = '/Assets/expand.gif';
		panel1_show = false;
	}else{
		Effect.BlindDown('step1', {beforeStart: panel_hold,
					   			   afterFinish: panel_unhold});
		document.getElementById('toggle_image1').src = '/Assets/collapse.gif';
		panel1_show = true;
	}
}

function toggle_panel_2(){
	if(is_hold) return;
	
	if(panel2_show){
		Effect.BlindUp('step2', {beforeStart: panel_hold,
					   			 afterFinish: panel_unhold});
		document.getElementById('toggle_image2').src = '/Assets/expand.gif';
		panel2_show = false;
	}else{
		Effect.BlindDown('step2', {beforeStart: panel_hold,
					   			   afterFinish: panel_unhold});
		document.getElementById('toggle_image2').src = '/Assets/collapse.gif';
		panel2_show = true;
	}
}

function toggle_panel_3(){
	if(is_hold) return;
	
	if(panel3_show){
		Effect.BlindUp('step3', {beforeStart: panel_hold,
					   			 afterFinish: panel_unhold});
		document.getElementById('toggle_image3').src = '/Assets/expand.gif';
		panel3_show = false;
	}else{
		Effect.BlindDown('step3', {beforeStart: panel_hold,
					   			   afterFinish: panel_unhold});
		document.getElementById('toggle_image3').src = '/Assets/collapse.gif';
		panel3_show = true;
	}
}

function toggle_panel_4(){
	if(is_hold) return;
	
	if(panel4_show){
		Effect.BlindUp('step4', {beforeStart: panel_hold,
					   			 afterFinish: panel_unhold});
		document.getElementById('toggle_image4').src = '/Assets/expand.gif';
		panel4_show = false;
	}else{
		Effect.BlindDown('step4', {beforeStart: panel_hold,
					   			   afterFinish: panel_unhold});
		document.getElementById('toggle_image4').src = '/Assets/collapse.gif';
		panel4_show = true;
	}
}

function show_all(){
	
	if(!panel1_show){
		Effect.BlindDown('step1');
		document.getElementById('toggle_image1').src = '/Assets/collapse.gif';
		panel1_show = true;
	}
	
	if(!panel2_show){
		Effect.BlindDown('step2');
		document.getElementById('toggle_image2').src = '/Assets/collapse.gif';
		panel2_show = true;
	}
	
	if(!panel3_show){
		Effect.BlindDown('step3');
		document.getElementById('toggle_image3').src = '/Assets/collapse.gif';
		panel3_show = true;
	}
	
	if(!panel4_show){
		Effect.BlindDown('step4');
		document.getElementById('toggle_image4').src = '/Assets/collapse.gif';
		panel4_show = true;
	}
}

function panel1_next(){
	if(is_hold) return;
	
	Effect.BlindUp('step1', {beforeStart: panel_hold,
					   		 afterFinish: panel_unhold});
	document.getElementById('toggle_image1').src = '/Assets/expand.gif';
	panel1_show = false;
	
	if(!panel2_show){
		Effect.BlindDown('step2', {beforeStart: panel_hold,
					   			   afterFinish: panel_unhold});
		document.getElementById('toggle_image2').src = '/Assets/collapse.gif';
		panel2_show = true;
	}
	
}


function panel2_next(){
	if(is_hold) return;
	
	Effect.BlindUp('step2', {beforeStart: panel_hold,
					   		 afterFinish: panel_unhold});
	document.getElementById('toggle_image2').src = '/Assets/expand.gif';
	panel2_show = false;
	
	if(!panel3_show){
		Effect.BlindDown('step3', {beforeStart: panel_hold,
					   			   afterFinish: panel_unhold});
		document.getElementById('toggle_image3').src = '/Assets/collapse.gif';
		panel3_show = true;
	}
}

function panel3_next(){
	if(is_hold) return;
	
	Effect.BlindUp('step3', {beforeStart: panel_hold,
					   		 afterFinish: panel_unhold});
	document.getElementById('toggle_image3').src = '/Assets/expand.gif';
	panel3_show = false;
	
	if(!panel4_show){
		Effect.BlindDown('step4', {beforeStart: panel_hold,
					   			   afterFinish: panel_unhold});
		document.getElementById('toggle_image4').src = '/Assets/collapse.gif';
		panel4_show = true;
	}
}

function submit_form(){
	
	var fname		 			= document.getElementById("fname").value;
	var lname		 			= document.getElementById("lname").value;
	var email		 			= document.getElementById("email").value;
	var phone		 			= document.getElementById("phone").value;
	var sex		 				= get_radio_value(document.application.sex);
	var housing_type	 		= get_check_value(document.application.housing_type);
	
	var school_year				= get_radio_value(document.application.school_year);
	var major		 			= document.getElementById("major").value;
	var current_landlord		= get_radio_value(document.application.current_landlord);
	var frat_soro	 			= document.getElementById("frat_soro").value;
	var landlord_other	 		= document.getElementById("landlord_other").value;
	
	var address		 			= document.getElementById("address").value;
	var city	 				= document.getElementById("city").value;
	var state		 			= document.application.state[document.getElementById("state").selectedIndex].value;
	var homezip	 				= document.getElementById("homezip").value;
	
	var guarantor_name 			= document.getElementById("guarantor_name").value;
	var guarantor_phone			= document.getElementById("guarantor_phone").value;
	var guarantor_relationship	= document.getElementById("guarantor_relationship").value;
	var guarantor_address		= document.getElementById("guarantor_address").value;
	var guarantor_city 			= document.getElementById("guarantor_city").value;
	var guarantor_state			= document.application.guarantor_state[document.getElementById("guarantor_state").selectedIndex].value;
	var guarantor_email			= document.getElementById("guarantor_email").value;

	var reference		 		= get_check_value(document.application.reference);
	var reference_friend_name	= document.getElementById("reference_friend_name").value;
	var reference_search_engine	= document.getElementById("reference_search_engine").value;
	var reference_tailgate		= document.getElementById("reference_tailgate").value;
	var reference_asu_department= document.getElementById("reference_asu_department").value;
	var reference_other			= document.getElementById("reference_other").value;
		
	new Ajax.Request('/Assets/proc-app.php', {
										method: 'post',
										parameters: {
													 fname: fname,
													 lname: lname,
													 email: email,
													 phone: phone,
													 sex: sex,
													 housing_type: housing_type,
													 
													 school_year: school_year,
													 major: major,
													 current_landlord: current_landlord,
													 frat_soro: frat_soro,
													 landlord_other: landlord_other,
													 
													 address: address,
													 city: city,
													 state: state,
													 homezip: homezip,
													 
													 guarantor_name: guarantor_name,
													 guarantor_phone: guarantor_phone,
													 guarantor_relationship: guarantor_relationship,
													 guarantor_address: guarantor_address,
													 guarantor_city: guarantor_city,
													 guarantor_state: guarantor_state,
													 guarantor_email: guarantor_email,
													 
													 reference: reference,
													 reference_friend_name: reference_friend_name,
													 reference_search_engine: reference_search_engine,
													 reference_tailgate: reference_tailgate,
													 reference_asu_department: reference_asu_department,
													 reference_other: reference_other
													},
										onSuccess: function(transport){
														if(transport.responseText == "success"){
															Effect.Fade('form-all', { afterFinish: function(){
																												document.getElementById('form-all').innerHTML = "<p>Thank you for submitting your application. We will be in contact if we need additional information.</p>";
																												Effect.Appear('form-all');
																											 }
																		});
														}else if(transport.responseText == "email_fail"){
															document.getElementById('error_notice').innerHTML = "Could not submit form at this time. There was a server error.";
															
														}else{
															
															show_all();
															
															var response_string = transport.responseText;
															var error = response_string.split("||");
															
															reset_textbox('fname');															
															reset_textbox('lname');
															reset_textbox('email');
															reset_textbox('phone');
															reset_text('sex-error-text');
															reset_text('housing-type-error-text');
															
															reset_text('school-year-error-text');
															reset_textbox('major');
															reset_textbox('homezip');
															reset_textbox('frat_soro');
															reset_textbox('landlord_other');
															reset_text('current-landlord-error-text');
																														
															reset_textbox('address');
															reset_textbox('city');
															reset_textbox('guarantor_name');
															reset_textbox('guarantor_phone');
															reset_textbox('guarantor_relationship');
															reset_textbox('guarantor_address');
															reset_textbox('guarantor_city');
															reset_textbox('guarantor_email');
															
															reset_text('reference-error-text');
															reset_textbox('reference_other');
															
															document.getElementById('error_notice').innerHTML = "Could not submit form. One or more required fields were not provided or were incorrectly formatted.";
															
															for(var i=0; i<error.length; i++){
																var temp_obj = document.getElementById(error[i]);

																if(error[i] == "fname" || 
																   error[i] == "lname" ||
																   error[i] == "email" ||
																   error[i] == "phone" ||
																   error[i] == "major" ||
																   error[i] == "frat_soro" ||
																   error[i] == "landlord_other" ||
																   error[i] == "address" || 
																   error[i] == "city" ||
																   error[i] == "homezip" ||
																   error[i] == "guarantor_name" ||
																   error[i] == "guarantor_phone" ||
																   error[i] == "guarantor_relationship" ||
																   error[i] == "guarantor_address" ||
																   error[i] == "guarantor_city" ||
																   error[i] == "guarantor_email" ||
																   error[i] == "reference_other") 
																{
																	temp_obj.style.borderColor = "red"; 
																	temp_obj.style.backgroundColor = "#FFEEEE"; 
																} 
															
																if(error[i] == "sex")
																	error_text('sex-error-text');
																if(error[i] == "housing_type")
																	error_text('housing-type-error-text');
																if(error[i] == "school_year")
																	error_text('school-year-error-text');
																if(error[i] == "current_landlord")
																	error_text('current-landlord-error-text');
																if(error[i] == "reference")
																	error_text('reference-error-text');
															}
														}
					 								}
	});		
}

function get_check_value(name){
	var c_value = "";
	for(var i=0; i < name.length; i++){
		if(name[i].checked){
		  c_value = c_value + name[i].value + ", ";
		}
	}
	return c_value;
}

function get_radio_value(name){
	var c_value = "";
	for(var i=0; i < name.length; i++){
		if(name[i].checked){
		  return name[i].value;
		}
	}
}

function reset_textbox(name){
	document.getElementById(name).style.borderColor = "#AAD1EE";
	document.getElementById(name).style.backgroundColor = "#FFFFFF";
}

function reset_text(name){
	document.getElementById(name).style.color = "#666666";
	document.getElementById(name).style.fontWeight = "normal";
}

function error_text(name){
	document.getElementById(name).style.color = "#FF0000";
	document.getElementById(name).style.fontWeight = "bold";
}
