var day_spread = 30; // The amount of days added to today when creating a start date.

var divs_array = new Array;
divs_array[0] = 'type_of_new_project';
divs_array[1] = 'new_print_project';
divs_array[2] = 'new_web_project';
divs_array[3] = 'new_commun_project';
divs_array[4] = 'new_combo_project';
divs_array[5] = 'new_project_unsure';
divs_array[6] = 'type_updated_project';
divs_array[7] = 'update_print_project';
divs_array[8] = 'update_web_project';
divs_array[9] = 'update_commun_project';
divs_array[10] = 'reorder_proj';


var req_type_array = new Array;
req_type_array[0] = 'new_proj';
req_type_array[1] = 'update_proj';
req_type_array[2] = 'reorder_proj';


var sub_new_proj_array = new Array;
sub_new_proj_array[0] = 'new_print_project';
sub_new_proj_array[1] = 'new_web_project';
sub_new_proj_array[2] = 'new_commun_project';
sub_new_proj_array[3] = 'new_combo_project';
sub_new_proj_array[4] = 'new_project_unsure';

var sub_radio = new Array;
sub_radio[0] = 'print';
sub_radio[1] = 'web';
sub_radio[2] = 'comm';
sub_radio[3] = 'combo';
sub_radio[4] = 'unsure';

var sub_update_proj_array = new Array;
sub_update_proj_array[0] = 'update_print_project';
sub_update_proj_array[1] = 'update_web_project';
sub_update_proj_array[2] = 'update_commun_project';

var sub_update_radio = new Array;
sub_update_radio[0] = 'printup';
sub_update_radio[1] = 'webup';
sub_update_radio[2] = 'commup';


function request_type(id)
{
	var customer_info = document.getElementById('customer_info');
	var new_proj_link = document.getElementById('new');
	var update_proj_link = document.getElementById('update');
	var reorder_proj_link = document.getElementById('reorder');

	switch (id)
	{
		case 'new_proj':
			customer_info.style.display = 'block';
			new_proj_link.className = 'typeproject_on';
			update_proj_link.className = 'typeproject middle';
			reorder_proj_link.className = 'typeproject';
			break;
			
		case 'update_proj':
			customer_info.style.display = 'none';
			new_proj_link.className = 'typeproject';
			update_proj_link.className = 'typeproject_on middle';
			reorder_proj_link.className = 'typeproject';
			break;
			
		case 'reorder_proj':
			customer_info.style.display = 'none';
			new_proj_link.className = 'typeproject';
			update_proj_link.className = 'typeproject middle';
			reorder_proj_link.className = 'typeproject_on';
			break;
		
	}
	
	
	for (var i=0; i<req_type_array.length; i++)
	{
		document.getElementById(req_type_array[i]).style.display = "none";
	}
	
	for (var i=0; i<sub_new_proj_array.length; i++)
	{
		document.getElementById(sub_new_proj_array[i]).style.display = "none";
		document.getElementById(sub_radio[i]).checked = false;
	}
	
	for (var i=0; i<sub_update_proj_array.length; i++)
	{
		document.getElementById(sub_update_proj_array[i]).style.display = "none";
		document.getElementById(sub_update_radio[i]).checked = false;
	}
	
	if (id != 'reorder_proj')
	{
		document.getElementById('reorder_proj').style.display = "none";
	}
	else
	{
		document.getElementById('reorder_proj').style.display = "block";
	}
	document.getElementById(id).style.display = "block";
	document.getElementById('req_type_hidden').value = id;
}


function sub_new_proj(id, radio)
{
	for (var i=0; i<sub_new_proj_array.length; i++)
	{
		document.getElementById(sub_new_proj_array[i]).style.display = "none";
	}
	document.getElementById(id).style.display = "block";
	document.getElementById('sub_req_type_hidden').value = id;
	document.getElementById(radio).checked = true;

}


function sub_update_proj(id, radio)
{
	for (var i=0; i<sub_update_proj_array.length; i++)
	{
		document.getElementById(sub_update_proj_array[i]).style.display = "none";
	}
	document.getElementById(id).style.display = "block";
	document.getElementById('sub_req_type_hidden').value = id;
	document.getElementById(radio).checked = true;

}









function check_other(id)
{
	if (document.getElementById(id).checked != true)
	{
		document.getElementById(id).checked = true;
	}
}






var error_num = 0;
function validate_fields(id)
{
	var this_el = document.getElementById(id);
	if (this_el.value == "")
	{
		this_el.style.borderColor = "#FF0000";
		this_el.style.borderWidth = "1px";
		this_el.style.borderStyle = "solid";
		error_num++;
		//alert("validate_fields");
	}
	else
	{
		this_el.style.borderColor = "#A5ACB2";
	}
}




function validate_radios(this_name, div_id)
{
	this_wrapper = document.getElementById(div_id);
	this_radio = document.proj_form[this_name];
	this_length = document.proj_form[this_name].length;
//	alert(this_length);
	var this_error_num = 0;
	for (var i=0; i<this_length; i++)
	{
		if (this_radio[i].checked == false)
		{
			this_error_num++;
		}
	}
	
	if (this_error_num == this_length)
	{
		this_wrapper.style.borderColor = "#FF0000";
		this_wrapper.style.borderWidth = "1px";
		this_wrapper.style.borderStyle = "solid";
		error_num++;
		//alert("validate_radios");
	}
	else
	{
		this_wrapper.style.borderStyle = "none";
	}
}



function validate_checks(this_array, div_id)
{
	var this_check_num = 0;
	for (var i=0; i<this_array.length; i++)
	{
		if (document.getElementById(this_array[i]).checked == true)
		{
			this_check_num++;
		}
	}
	
	var this_div = document.getElementById(div_id);
	if (this_check_num == 0)
	{
		validation_msg += "Print types:\n";
		this_div.style.borderColor = "#FF0000";
		this_div.style.borderWidth = "1px";
		this_div.style.borderStyle = "solid";
		error_num++;
		//alert('validate_checks')
	}
	else if (this_div.style.borderStyle != "none")
	{
		this_div.style.borderStyle = "none";
	}
}




var error_num;
function validate_proj_form()
{
	validation_msg = "Most fields are required\nFields that need your attention are highhlighted in red.";
	error_num = 0;

	var req_type_hidden = document.getElementById('req_type_hidden').value;
	var sub_req_type_hidden = document.getElementById('sub_req_type_hidden').value;
	switch (req_type_hidden)
	{
		case 'new_proj':
			var begining_fields = new Array;
			begining_fields[0] = 'submitby';
			begining_fields[1] = 'submitemail';
			begining_fields[2] = 'dept';
			begining_fields[3] = 'dept_initials';
			begining_fields[4] = 'start_date';
			begining_fields[5] = 'startdue';
			
			var begining_radio = new Array;
			begining_radio[0] = 'urgency';
			
			
			
			// Validate begining fields
			for (var i=0; i<begining_fields.length; i++)
			{
				validate_fields(begining_fields[i]);
			}
			
			// Validate begining radio
			//validate_radios('req_type', 'req_type_wrapper');
			validate_radios('urgency', 'urgency_wrapper');
			
				validate_radios('new_req_type', 'new_req_type_wrapper');
					switch (sub_req_type_hidden)
					{
						case 'new_print_project':
	
	
	
							var print_type_array = new Array;
								print_type_array[0] = 'flier';
								print_type_array[1] = 'brochure';
								print_type_array[2] = 'newsletter';
								print_type_array[3] = 'postcard';
								print_type_array[4] = 'pdf';
								print_type_array[5] = 'pother';
								validate_checks(print_type_array, 'boxchoices_wrapper');
								
							var print_fields = new Array;
								print_fields[0] = 'ptitle';
								print_fields[1] = 'pcopypts';
								print_fields[2] = 'paud';
								print_fields[3] = 'pachieve';
								print_fields[4] = 'qty';
								print_fields[5] = 'pkg';
								print_fields[6] = 'deliv';
								for (var i=0; i<print_fields.length; i++)
								{
									validate_fields(print_fields[i]);
								}
							break;
							
							
						case 'new_web_project':
							var web_proj_array = new Array;
								web_proj_array[0] = 'webpage';
								web_proj_array[1] = 'website';
								web_proj_array[2] = 'email';
								web_proj_array[3] = 'form';
								web_proj_array[4] = 'wother';
								validate_checks(web_proj_array, 'web_proj_check_wrapper');
								
							var web_proj_fields = new Array;
									web_proj_fields[0] = 'wtitle';
									web_proj_fields[1] = 'wcopypts';
									web_proj_fields[2] = 'waud';
									web_proj_fields[3] = 'wachieve';
									web_proj_fields[4] = 'wotherinfo';
									for (var i=0; i<web_proj_fields.length; i++)
									{
										validate_fields(web_proj_fields[i]);
									}
							break;
							
							
						case 'new_commun_project':
							var commun_proj_array = new Array;
								commun_proj_array[0] = 'memdev';
								commun_proj_array[1] = 'media';
								commun_proj_array[2] = 'approve';
								commun_proj_array[3] = 'commother';
								validate_checks(commun_proj_array, 'new_comm_boxes_wrapper');
								
							var new_comm_fields = new Array;
									new_comm_fields[0] = 'commtitle';
									new_comm_fields[1] = 'commcopypts';
									new_comm_fields[2] = 'commaud';
									new_comm_fields[3] = 'commachieve';
									new_comm_fields[4] = 'commotherinfo';
									for (var i=0; i<new_comm_fields.length; i++)
									{
										validate_fields(new_comm_fields[i]);
									}
							break;
							
							
						case 'new_combo_project':
							var combo_proj_array = new Array;
								combo_proj_array[0] = 'combop';
								combo_proj_array[1] = 'combow';
								combo_proj_array[2] = 'comboc';
								combo_proj_array[3] = 'comboother';
								validate_checks(combo_proj_array, 'new_combo_boxes_wrapper');
								
							var new_combo_fields = new Array;
									new_combo_fields[0] = 'combotitle';
									new_combo_fields[1] = 'combocopypts';
									new_combo_fields[2] = 'comboaud';
									new_combo_fields[3] = 'comboachieve';
									new_combo_fields[4] = 'combootherinfo';
									for (var i=0; i<new_combo_fields.length; i++)
									{
										validate_fields(new_combo_fields[i]);
									}
							break;
							
							
						case 'new_project_unsure':
							var new_unsure_fields = new Array;
									new_unsure_fields[0] = 'unsuretitle';
									new_unsure_fields[1] = 'unsureexp';
									new_unsure_fields[2] = 'unsureaud';
									new_unsure_fields[3] = 'unsureachieve';
									for (var i=0; i<new_unsure_fields.length; i++)
									{
										validate_fields(new_unsure_fields[i]);
									}
							break;
				} // END switch
				break;
			









		case 'update_proj':
				validate_radios('update_req_type', 'update_req_type_wrapper');
					switch (sub_req_type_hidden)
					{
						case 'update_print_project':
							validate_radios('update_print_ungency', 'update_priority_wrapper');
							
							var update_print_fields = new Array;
							update_print_fields[0] = 'pupprojnum';
							update_print_fields[1] = 'pupdate';
							for (var i=0; i<update_print_fields.length; i++)
							{
								validate_fields(update_print_fields[i]);
							}
							
							var update_print_array = new Array;
								update_print_array[0] = 'pupflier';
								update_print_array[1] = 'pupbrochure';
								update_print_array[2] = 'pupnewsletter';
								update_print_array[3] = 'puppostcard';
								update_print_array[4] = 'puppdf';
								update_print_array[5] = 'pupdisp';
								update_print_array[6] = 'pupother';
								validate_checks(update_print_array, 'update_print_boxes');
							break;
							
							
						case 'update_web_project':
							validate_radios('update_web_urgency', 'update_web_urgency_wrapper');
							
							var update_web_fields = new Array;
							update_web_fields[0] = 'wupprojnum';
							update_web_fields[1] = 'wupdate';
							update_web_fields[2] = 'wupurl';
							for (var i=0; i<update_web_fields.length; i++)
							{
								validate_fields(update_web_fields[i]);
							}
							
							var update_web_array = new Array;
								update_web_array[0] = 'wupwebpage';
								update_web_array[1] = 'wupwebsite';
								update_web_array[2] = 'wupemail';
								update_web_array[3] = 'wupform';
								update_web_array[4] = 'wupother';
								validate_checks(update_web_array, 'update_web_boxes');
							break;
							
							
						case 'update_commun_project':
							validate_radios('update_comm_urgency', 'update_comm_urgency_wrapper');
							
							var update_comm_fields = new Array;
							update_comm_fields[0] = 'cupprojnum';
							update_comm_fields[1] = 'cupdate';
							for (var i=0; i<update_comm_fields.length; i++)
							{
								validate_fields(update_comm_fields[i]);
							}
							
							var update_comm_array = new Array;
								update_comm_array[0] = 'cupmemdev';
								update_comm_array[1] = 'cupmedia';
								update_comm_array[2] = 'cupapprove';
								update_comm_array[3] = 'cupother';
								validate_checks(update_comm_array, 'update_comm_boxes');
						break;
				} // END switch
				break;
			








		case 'reorder_proj':
			var reorder_proj_array = new Array;
			reorder_proj_array[0] = 'resubmitby';
			reorder_proj_array[1] = 'resubmitemail';
			reorder_proj_array[2] = 'reprojnum';
			
			for (var i=0; i<reorder_proj_array.length; i++)
			{
				validate_fields(reorder_proj_array[i]);
			}
			
			validate_radios('reorder_radio', 'reorder_radio_wrapper');
			break;
	} // END switch
	

	
	//alert(error_num)

		if (error_num > 0)
		{
			alert(validation_msg);
			return false;
		}



}// END validate_proj_form()








function date_spread()
{
	
	var d = new Date();
	var year = d.getFullYear();
	var month = d.getMonth();
	var day = d.getDate();
	if (month < 10)
	{
		month = "0"+(month+1);
	}
	if (day < 10)
	{
		day = "0"+day;
	}
	var this_date_string = ""+year+""+month+""+day+"";
	var start_date = document.getElementById('start_date');
	var start_date_value = start_date.value;
	var start_date_array = start_date_value.split("/");
	var start_date_string = start_date_array[2]+""+start_date_array[0]+""+start_date_array[1];
	
	var due_date = document.getElementById('startdue');
	var due_date_value = due_date.value;
	var due_date_array = due_date_value.split("/");
	var due_date_string = due_date_array[2]+""+due_date_array[0]+""+due_date_array[1];
	
	var this_spread = start_date_string-this_date_string;
	var spread = (start_date_string-this_date_string);
	if (start_date_value != "")
	{
		if (spread < 30)
		{
		alert("Projects must be requested at a minimum of 30 days in advance.");
		start_date.value = "";
		}
	}
	
	if ((start_date_value != "") && (due_date_value != ""))
	{
		working_days = (due_date_string-start_date_string);
		if ((working_days < 2) && (working_days < 0))
		{
			var alert_msg = "Sorry, time travel to the future to have this project\n";
			alert_msg += "finished before it is to begin is virtually impossible.\n\n";
			alert_msg += "However, a scientist by the name of Dr. Emmett Brown has found\n";
			alert_msg += "some success with a Delorean, and an object called the Flux Capacitor,\n"
			alert_msg += "with a speed requirement of 88 MPH.\n";
			alert_msg += "Unfortunately, we get car sick.\n";
			alert(alert_msg);
			due_date.value = "";
		}
		else if (working_days < 2)
		{
			alert("There may not be ample time to complete this project.");
		}
	}
}



function check_email(id)
{
	var email = document.getElementById(id).value;
	if (email.indexOf("@") == -1)
	{
		alert("Please use a proper email address");
	}
}
