﻿// START -- SET innerHTML FOR PAGE ELEMENTS (if page element names change, changing them here will change in all functions)
	
function Set_buttons_div(buttons_div_innerHTML)
	{
		document.getElementById("buttons_div").innerHTML = buttons_div_innerHTML;
	}
	
function Set_form_div(form_div_innerHTML)
	{
		document.getElementById("form_div").innerHTML = form_div_innerHTML;
	}
	
function Set_nav_div(nav_div_innerHTML)
	{
		document.getElementById("nav_links").innerHTML = nav_div_innerHTML;
	}
	
function Set_login_div(login_div_innerHTML)
	{
		document.getElementById("login_div").innerHTML = login_div_innerHTML;
	}
	
function Set_page_alert(alert_text)
	{
		document.getElementById("page_alert").innerHTML = alert_text;
	}
	
function Set_user_div(user_div_innerHTML)
	{
		document.getElementById("user_summary").innerHTML = user_div_innerHTML;
	}

// END -- SET innerHTML FOR PAGE ELEMENTS (if page element names change, changing them here will change in all functions)





function Email_authenticate()
	{
	var Email_address_element = document.getElementById("Login_email");
	var Email_address = Email_address_element.value
	
			Email_address = String_trim(Email_address)
			
	var Password_element = document.getElementById("Login_password");
	var Password = Password_element.value

	var Request_text = Email_address + "\t" + Password  // tab delimited
	
	var Post_to_url = POST_TO_URL_PREFIX + "email_authenticate";
	var User_JSON_stringified = ""
	
		
		User_JSON_stringified = XMLHttpRequest_PostTo(Request_text,Post_to_url)
		
		if(User_JSON_stringified == "0")
		{
			// user not found
			USER_RECORD = null
			Set_page_alert("Email and password combination not valid.<br>Please try again or register your contact information below.");
			
			Set_nav_div("");
			Set_user_div("");

			Get_register_form();
		}
		else
		{
		
			eval("USER_RECORD = "+ User_JSON_stringified + ";");
						
			Set_page_alert("Hello "+ USER_RECORD.Salutation_informal + ", please select the type of printing for your quote at the left.");
						
			Set_form_div("")
			Set_login_div("")
			
			User_summary_set_content();
			
			Get_navlinks();
		}
		
	}
	





	
function User_summary_set_content()
	{
	var User_summary_element = document.getElementById("user_summary")
	var Build_html = ""
	
		if(USER_RECORD == null)
		{
			// no user found yet
			Set_page_alert("Error: user data not found.");
		}
		else
		{

			Build_html += '<img src="/images/znull.gif" width="1" height="6" border="0"><br>'
			
			Build_html += "<span class='bodytextsans'><b>" +USER_RECORD.Name+ "</b></span>"+ "<br>"
			
			Build_html += '<img src="/images/znull.gif" width="1" height="2" border="0"><br>'

			Build_html += Build_user_company_contact_HTML(USER_RECORD);
			
			Build_html += '<img src="/images/znull.gif" width="1" height="15" border="0"><br>'
									
			Set_user_div( Build_html );
			

		}	
	}





// ---- START "Get" functions to use in building webpage 

function Get_html_insert(filename)
	{
	var Post_to_url = POST_TO_URL_PREFIX + "htmlinsert";
	var Get_HTML = "";
			
		Get_HTML = XMLHttpRequest_PostTo(filename,Post_to_url);
		
		return Get_HTML;
	
	}	


function Build_user_company_contact_HTML(User_JSON_object)
	{
	var Build_html = ""
	
			if(USER_RECORD.Company != "")
			{
			Build_html += USER_RECORD.Company + "<br>"
			}
			
			if(USER_RECORD.Department != "")
			{
			Build_html += USER_RECORD.Department + "<br>"
			}
						
						
			for(var i = 0; i < USER_RECORD.Contact_points_list.length ; i++)
			{
				if(USER_RECORD.Contact_points_list[i].Contact_string != "")
				{
				
					switch (USER_RECORD.Contact_points_list[i].Category)
					{
						case "Phone":
							if(USER_RECORD.Contact_points_list[i].Description == "Main")
							{
								Build_html += "Phone: " + USER_RECORD.Contact_points_list[i].Contact_string + "<br>";
							}
							else
							{
								Build_html += USER_RECORD.Contact_points_list[i].Description + ": "  + USER_RECORD.Contact_points_list[i].Contact_string +  "<br>"
							}
							break;
						
						case "Email":
							Build_html += USER_RECORD.Contact_points_list[i].Contact_string +  "<br>";
							break;
						
						default:
							Build_html += USER_RECORD.Contact_points_list[i].Category + ", " + USER_RECORD.Contact_points_list[i].Description + ": "  + USER_RECORD.Contact_points_list[i].Contact_string +  "<br>"
					
					}					
				}
			}
			
			return Build_html;
	}


function Build_user_primary_address_HTML(User_JSON_object)
	{
	var Build_html = ""

			for(var i = 0; i < USER_RECORD.Locations_list.length ; i++)
			{
				if(USER_RECORD.Locations_list[i].Is_primary_location)
				{
					
					if(USER_RECORD.Locations_list[i].Address_1 != "")
					{
						Build_html += USER_RECORD.Locations_list[i].Address_1 + "<br>"
					}
					

					if(USER_RECORD.Locations_list[i].Address_2 != "")
					{
						Build_html += USER_RECORD.Locations_list[i].Address_2 + "<br>"
					}
					
					
					Build_html += USER_RECORD.Locations_list[i].City + " " + USER_RECORD.Locations_list[i].State + ", " + USER_RECORD.Locations_list[i].Zip + "<br>"
					
					if( (USER_RECORD.Locations_list[i].Country != "") & (USER_RECORD.Locations_list[i].Country != "USA") & (USER_RECORD.Locations_list[i].Country != "U.S.A.") & (USER_RECORD.Locations_list[i].Country != "United States"))
					{
						Build_html += USER_RECORD.Locations_list[i].Country + "<br>"
					}
					
					
					if(USER_RECORD.Locations_list[i].Attention != "")
					{
						Build_html += "Attn: " + USER_RECORD.Locations_list[i].Attention + "<br>"
					}
					
				}
			}
			
			return Build_html;
	}
	
	

function Get_register_form()
	{
	
		Set_form_div( Get_html_insert("regform.html") );
		
		document.getElementById("Fullname").focus();
		
		Set_buttons_div( Get_html_insert("Buttons_div_Register.html") )
		
		if(document.getElementById("Login_email").value == "")
		{
			Set_page_alert("Please fill out registration form...")
		}
		
		
	}



function Get_navlinks()
	{	
	var Post_to_url = POST_TO_URL_PREFIX + "navlinks";
	var Build_HTML = "";
	var Navlinks_JSON_stringified = "";
	
	// Quote_request_id and Is_update_estimate_form are put into links that bring up Quote Request forms
	// Navlinks should always bring up a fresh form, so set defaults to "" and false
	var Quote_request_id = "\'\'"
	var Is_update_estimate_form = "false"
	
	var Additional_parameters = ',' + Quote_request_id + ',' + Is_update_estimate_form
	
			
		Navlinks_JSON_stringified = XMLHttpRequest_PostTo("",Post_to_url);
		
		eval("NAVLINKS_JSON = "+ Navlinks_JSON_stringified + ";");
		
		Build_HTML += '<span class="brightbluelink" onclick="Get_edit_contact_form();" onmouseover="brightbluelink_over(this);" onmouseout="brightbluelink_out(this);" >Edit contact info</span><br>'
		
		Build_HTML += '<img src="/images/znull.gif" width="1" height="3" border="0"><br>'
		
		Build_HTML += '<span class="brightbluelink" onclick="Onload_quoterequest_init();" onmouseover="brightbluelink_over(this);" onmouseout="brightbluelink_out(this);" >Logout</span><br>'
		
		Build_HTML += '<img src="/images/znull.gif" width="1" height="8" border="0"><br>'
		
		for(var i = 0; i < NAVLINKS_JSON.length ; i++)
		{
			if(i != 0)
			{
				Build_HTML += '<img src="/images/znull.gif" width="1" height="2" border="0"><br>'
			}
						
			if(NAVLINKS_JSON[i].Web_linkImage_URL == "")
			{
				// if no image, use Web_link_title
				Build_HTML += '<div class="brightbluelink12" id="nav_link_' + NAVLINKS_JSON[i].Form_ID + '" onclick="Get_estimate_input_form(' +NAVLINKS_JSON[i].Form_ID + Additional_parameters + ');" onmouseover="brightbluelink_over(this);" onmouseout="brightbluelink_out(this);">'+NAVLINKS_JSON[i].Web_link_title+'</div>'
			}
			else
			{
				Build_HTML += '<div id="nav_link_'  + NAVLINKS_JSON[i].Form_ID + '" style="cursor:pointer;background-image:url(\'' + NAVLINKS_JSON[i].Web_linkImage_URL +'\');background-position:left;height:' +NAVLINKS_JSON[i].Web_linkImage_height_px+ 'px;width:' +NAVLINKS_JSON[i].Web_linkImage_width_px+ 'px;" onmouseover="Slide_backgroundImage_right(this);" onmouseout="Slide_backgroundImage_left(this);" onclick="Get_estimate_input_form(' +NAVLINKS_JSON[i].Form_ID + Additional_parameters + ');"></div>'
			}
			
		}
		
		Set_nav_div( Build_HTML )
		

	}
	
	

	
	
function Get_estimate_input_form(Form_id,Quote_request_id,Is_update_estimate_form)
	{
	var Post_to_url = POST_TO_URL_PREFIX + "inputform";
	var Get_HTML = "";
	
	
		Get_HTML = XMLHttpRequest_PostTo(Form_id,Post_to_url)
		
		if(Get_HTML == "0")
		{
			Set_page_alert( "Error: Quote Request form not found: " + Form_id ); 
			Set_form_div( "" );
		}
		else
		{
			
			Set_form_div( Get_HTML );
			
			if(Is_update_estimate_form)
			{
				Set_buttons_div( Get_html_insert("Buttons_div_Update_quote.html") )
				Set_page_alert( "Please enter your job specifications below,<br>then click the Update Quote Request button." ); 
			}
			else
			{
				// form for new estimate
				
				Set_buttons_div( Get_html_insert("Buttons_div_Create_quote.html") )
				Set_page_alert( "Please revise your job specifications below,<br>then click the Create Quote Request button." ); 
			}
			
			document.getElementById("Submit_button").name = "Submit_button_form_id_" + Form_id
						
			document.getElementById("QuoteRequestFormId").value = Form_id
						
			document.getElementById("QuoteRequestId").value = Quote_request_id
			
			
			
			// if Requested_for_name is in a hidden field, set requested for data from USER_RECORD 
			// which is the case when Customer fills in form.
			// values will not be == "" on forms where Sales Rep fills in form, or if they were already filled and quote is being edited
			
			if( (document.getElementById("Requested_for_name").type == "hidden") & (document.getElementById("Requested_for_name").value == ""))
			{
				
				document.getElementById("Requested_for_name").value = USER_RECORD.Name
				document.getElementById("Requested_for_company").value = USER_RECORD.Department
				document.getElementById("Requested_for_email").value = USER_RECORD.Login_email
				
				for(var i = 0; i < USER_RECORD.Contact_points_list.length ; i++)
				{
					if( (USER_RECORD.Contact_points_list[i].Category == "Phone") & (USER_RECORD.Contact_points_list[i].Description.toLowerCase() == "main") ) 
					{
							document.getElementById("Requested_for_phone").value = USER_RECORD.Contact_points_list[i].Contact_string
					}
				}
				
			}

			
			// fill default dates
			
			if(isIE())
			{
				document.getElementById("DeliveryYear").value=Get_year(new Date());
				document.getElementById("StartYear").value=Get_year(new Date());
			}
			else
			{
				document.getElementById("DeliveryYear").value=Get_year(new Date());
				document.getElementById("StartYear").value=Get_year(new Date());
			}
			
			var month_string = Get_month_name(new Date());
			
			document.getElementById("DeliveryMonth").value = month_string
			document.getElementById("StartMonth").value = month_string
			
			document.getElementById("JobTitle").focus()
			
			
			// if salesrepname and salesrepemail are in hidden fields, set sales rep from global variables set by 4D when this page was built 
			// which is the case when Customer fills in form.
			// values will not be == "" on forms where Sales Rep fills in form, or if they were already filled and quote is being edited
			
			if( (document.getElementById("salesrepname").type == "hidden") & (document.getElementById("salesrepname").value == ""))
			{
	
				document.getElementById("salesrepname").value=SALESREPNAME
				document.getElementById("salesrepemail").value=SALESREPEMAIL
				
			}
		

		}
		
	}
	
	
function Get_estimate_output_form(Form_id)
	{
	var Post_to_url = POST_TO_URL_PREFIX + "outputform";
	var Get_HTML = "";
	
		Get_HTML = XMLHttpRequest_PostTo(Form_id,Post_to_url)
		
		if(Get_HTML == "0")
		{
			Set_page_alert( "Error: Quote Request form not found: " + Form_id ); 
			Set_form_div( "" );
		}
		else
		{
			Set_page_alert( "Your Quote Request has been submitted." ); 
			Set_form_div( Get_HTML );
			Set_buttons_div( Get_html_insert("Buttons_div_Submit_quote.html") )
		}
		
	}



function Get_new_estimate_id(Form_id)
	{
	var Post_to_url = POST_TO_URL_PREFIX + "new_estimate_id";
	var Site_brand = POST_TO_URL_PREFIX.substring(12,POST_TO_URL_PREFIX.length - 1)
	var Request_text = "";
	var New_estimate_id = ""
	
		// pass tab delimited string:  User ID [tab] Form ID
	 
		Request_text += USER_RECORD.User_id ;
				
		Request_text += "\t"
		
		Request_text += Form_id ;
		
		New_estimate_id = XMLHttpRequest_PostTo(Request_text,Post_to_url); 
		
		return New_estimate_id;

	}

function Get_edit_contact_form()
	{
		// determine whether to update contact only or contact and estimate request 
		// so we can set value to hidden field "Edit_mode" in contact edit form when it is brought in
		
		var edit_mode_value = "";
		
		if(document.getElementById("JobTitle"))
		{
			if(document.getElementById("JobTitle").tagName.toLowerCase() == "input")
			{
				//  JobTitle does exist and is an <input>
				// set syntax to retrieve form later = "Contact and input FormId_[number], RequestId_[number].
				edit_mode_value = "Contact and input FormId_" + document.getElementById("QuoteRequestFormId").value + ", RequestId_" + document.getElementById("QuoteRequestId").value + ".";
			}
			else
			{
				// JobTitle does exist and is NOT an <input>
				// set syntax to retrieve form later = "Contact and output FormId_[number], RequestId_[number].
				edit_mode_value = "Contact and output FormId_" + document.getElementById("QuoteRequestFormId").value + ", RequestId_" + document.getElementById("QuoteRequestId").value + ".";
			}
		}
		else
		{
			// else Job Title does NOT exist
			edit_mode_value = "Contact only";
		}

	
		Set_form_div( Get_html_insert("edit_contact_form.html") );
		
		document.getElementById("Edit_mode").value = edit_mode_value
		
		document.getElementById("Fullname").value = USER_RECORD.Name
		
		document.getElementById("NamePrefix").value = USER_RECORD.Name_prefix
		document.getElementById("Firstname").value = USER_RECORD.Name_first
		document.getElementById("Midname").value = USER_RECORD.Name_middle
		document.getElementById("Lastname").value = USER_RECORD.Name_last
		document.getElementById("NameSuffix").value = USER_RECORD.Name_suffix
		
		document.getElementById("Email").value = USER_RECORD.Login_email
		
		document.getElementById("Title").value = USER_RECORD.Title
		document.getElementById("Company").value = USER_RECORD.Department
		
		for(var i = 0; i < USER_RECORD.Locations_list.length ; i++)
		{
			if(USER_RECORD.Locations_list[i].Is_primary_location)
			{
				document.getElementById("Address1").value = USER_RECORD.Locations_list[i].Address_1
				document.getElementById("Address2").value = USER_RECORD.Locations_list[i].Address_2
				document.getElementById("City").value = USER_RECORD.Locations_list[i].City
				document.getElementById("State").value = USER_RECORD.Locations_list[i].State
				document.getElementById("Zip").value = USER_RECORD.Locations_list[i].Zip
			}
		}
		
		for(var i = 0; i < USER_RECORD.Contact_points_list.length ; i++)
		{
			if(USER_RECORD.Contact_points_list[i].Category == "Phone") 
			{
				switch(USER_RECORD.Contact_points_list[i].Description.toLowerCase())
				{
					case "main":
						document.getElementById("Phone").value = USER_RECORD.Contact_points_list[i].Contact_string
				 		Phone_format_hyphen(document.getElementById("Phone"));
						break;
						
					case "cell":
						document.getElementById("Cell").value = USER_RECORD.Contact_points_list[i].Contact_string
				 		Phone_format_hyphen(document.getElementById("Cell"));
						break;
						
					default:
						// nothing
				}
			}
			
			if ( (USER_RECORD.Contact_points_list[i].Category == "Fax") & (USER_RECORD.Contact_points_list[i].Description == "Office") )
			{
				document.getElementById("Fax").value = USER_RECORD.Contact_points_list[i].Contact_string
				
				 Phone_format_hyphen(document.getElementById("Fax"));
			}
			
		}
		
		document.getElementById("Fullname").focus();
		
		Set_buttons_div( Get_html_insert("Buttons_div_edit_contact.html") )
		
		Set_page_alert("Edit your contact information...");
	}


// ---- END "Get" functions to use in building webpage 




function Edit_contact_info()
	{
		var Post_to_url = POST_TO_URL_PREFIX + "updateuser";
		var Request_text = ""
		var Response_text = ""
		
		var edit_mode_value = document.getElementById("Edit_mode").value
		
		// validate data entered
		
		var Registration_input_fields = Validate_contact_info_entered(Registration_input_fields);
	
		if(Registration_input_fields.length == 0)
		{
			//  there was an error, do not submit (Error text was displayed in Validate_contact_info_entered_
		}
		else
		{
			// NO validation errors, proceed...
			
			// update USER_RECORD
			
			USER_RECORD.Name_prefix = document.getElementById("NamePrefix").value
			USER_RECORD.Name_first = document.getElementById("Firstname").value
			USER_RECORD.Name_middle = document.getElementById("Midname").value
			USER_RECORD.Name_last = document.getElementById("Lastname").value
			USER_RECORD.Name_suffix = document.getElementById("NameSuffix").value
			
			USER_RECORD.Login_email = document.getElementById("Email").value
			
			USER_RECORD.Title = document.getElementById("Title").value
			USER_RECORD.Department = document.getElementById("Company").value
			
			for(var i = 0; i < USER_RECORD.Locations_list.length ; i++)
			{
				if(USER_RECORD.Locations_list[i].Is_primary_location)
				{
					USER_RECORD.Locations_list[i].Address_1 = document.getElementById("Address1").value
					USER_RECORD.Locations_list[i].Address_2 = document.getElementById("Address2").value
					USER_RECORD.Locations_list[i].City = document.getElementById("City").value
					USER_RECORD.Locations_list[i].State = document.getElementById("State").value
					USER_RECORD.Locations_list[i].Zip = document.getElementById("Zip").value
				}
			}
			
			for(var i = 0; i < USER_RECORD.Contact_points_list.length ; i++)
			{
				if(USER_RECORD.Contact_points_list[i].Category == "Phone") 
				{
					switch(USER_RECORD.Contact_points_list[i].Description.toLowerCase())
					{
						case "main":
							USER_RECORD.Contact_points_list[i].Contact_string = document.getElementById("Phone").value
							break;
							
						case "cell":
							USER_RECORD.Contact_points_list[i].Contact_string = document.getElementById("Cell").value
							break;
							
						default:
							// nothing
					}
				}
				
				if ( (USER_RECORD.Contact_points_list[i].Category == "Fax") & (USER_RECORD.Contact_points_list[i].Description == "Office") )
				{
					USER_RECORD.Contact_points_list[i].Contact_string = document.getElementById("Fax").value
				}
				
			}
			
			// update Server
			
			// submit fields using tab delimited syntax: [Field id] [Tab Ascii 9] [Field value] [CarriageReturn Ascii 13] 
		
			for (var Input_element_index = 0; Input_element_index < Registration_input_fields.length; Input_element_index++)
			{
				Request_text += Registration_input_fields[Input_element_index].id
				Request_text += "\t"
				Request_text += Registration_input_fields[Input_element_index].value
				Request_text += "\r"
			}
				
			Request_text += "UserID"
			Request_text += "\t"
			Request_text += USER_RECORD.User_id
			Request_text += "\r"
			
			Response_text = XMLHttpRequest_PostTo(Request_text,Post_to_url)
			
			var Response_text_error = Response_text.substr(0,5).toLowerCase()
			
			if(Response_text_error == "error")
			{
				Set_page_alert(Response_text);
				var Server_update_was_successful = false;
			}
			else
			{
				var Server_update_was_successful = true;
			}
			

			// update user_summary <div> 
			
			User_summary_set_content();

			
			// update form_div <div> contents according to edit_mode_value
			
			//  edit_mode_value == "Contact and input FormId_[number], RequestId_[number]." means return Input form form_div
			//  edit_mode_value == "Contact and output FormId_[number], RequestId_[number]." means return Output form form_div
			//  edit_mode_value == "Contact only" means return nothing in form_div
			
			var Form_type = ""
			
			if(edit_mode_value == "Contact only")
			{
				if(Server_update_was_successful)
				{
					Set_buttons_div("");
					Set_form_div("");
					Set_page_alert("Your contact information has been updated.");
				}
				else
				{
					// do nothing else, Set_page_alert() was set above if(Response_text_error == "error")
				}
					
			}
			else
			{
				// edit_mode_value != "Contact only", return 
				
				if(Server_update_was_successful)
				{
					var Form_id = edit_mode_value.substring( ( edit_mode_value.indexOf("FormId_") + 7 ) , edit_mode_value.indexOf(","))
					var Quote_request_id = edit_mode_value.substring( ( edit_mode_value.indexOf("RequestId_") + 10 ) , edit_mode_value.indexOf("."))
					
					if(edit_mode_value.indexOf("input") > -1)
					{
						Form_type = "input"
					}
					else
					{
						Form_type = "output"
					}
				
					// Get estimate input form whether it is Form_type "input" or "output"
					// then for output forms, run Create_or_edit_estimate_request() in order to get output form
					
					// Get_estimate_input_form(Form_id,Quote_request_id,Is_update_estimate_form)
					if(Quote_request_id == "")
					{
						var Is_update_estimate_form = false;
					}
					else
					{
						var Is_update_estimate_form = true;
					}
						
					Get_estimate_input_form(Form_id,Quote_request_id,Is_update_estimate_form);
					
					if(Form_type == "output")
					{
						Create_or_edit_estimate_request();
					}
					
					Set_page_alert("Your contact information has been updated.")
				}
				else
				{
					// do nothing else, Set_page_alert() was set above if(Response_text_error == "error")
				}
								
			}
			
		}
		
	}
	
	
	
	
	
	function Create_or_edit_estimate_request()
	{
		var Form_elements_div = document.getElementById("form_div");
		var Input_elements = Form_elements_div.getElementsByTagName("input");
		var Select_elements = Form_elements_div.getElementsByTagName("select");
		var Text_area_elements = Form_elements_div.getElementsByTagName("textarea");
			
		var Collected_values_array = [];
		var Collected_values_index = 0;
		
		var This_form_element = null;
		
		var	Form_id = document.getElementById("Submit_button").name;
			Form_id = Form_id.substring(22,Form_id.length)
	
		// if user is editing a previously filled form the QuoteRequestId will be present, just use that ID
		// if it is a new request, QuoteRequestId will = ""
	
		var Quote_request_id = document.getElementById("QuoteRequestId").value
		
		if(Quote_request_id == "")
		{
			// This is a new estimate: create estimate request record in 4D and get a Quote Request Id
		
			Quote_request_id = Get_new_estimate_id(Form_id);
			
			Set_page_alert("Creating quote request...")
		}
		else
		{
			// user is updating previously entered estimate request data
					
			Set_page_alert("Updating quote request...")
		}


		
	// collect data
		
		for (var Input_element_index = 0; Input_element_index < Input_elements.length; Input_element_index++)
		{
			
			
			if(Input_elements[Input_element_index].type == "radio")
			{
				// do nothing, skip radio buttons, their value should be put into a hidden field using javascript
			}
			else
			{
				Collected_values_array[Collected_values_index] = {};
				Collected_values_array[Collected_values_index].Element_id = Input_elements[Input_element_index].id;
				Collected_values_array[Collected_values_index].Element_type = Input_elements[Input_element_index].type;
				
				if(Input_elements[Input_element_index].type == "checkbox")
				{
					if(Input_elements[Input_element_index].checked)
					{
						Collected_values_array[Collected_values_index].Element_value = Input_elements[Input_element_index].value;
					}
					else
					{
						Collected_values_array[Collected_values_index].Element_value = "None";
					}
					
				}
				else
				{
					Collected_values_array[Collected_values_index].Element_value = Input_elements[Input_element_index].value;
				}
				
				Collected_values_index++
			}
			
		}
		
		
		for (var Select_element_index = 0; Select_element_index < Select_elements.length; Select_element_index++)
		{
			Collected_values_array[Collected_values_index] = {};
			
			Collected_values_array[Collected_values_index].Element_id = Select_elements[Select_element_index].id;
			Collected_values_array[Collected_values_index].Element_type = "select";
			Collected_values_array[Collected_values_index].Element_value = Select_elements[Select_element_index].value;
			
			Collected_values_index++;
		}
		
		
		for (var Textarea_element_index = 0; Textarea_element_index < Text_area_elements.length; Textarea_element_index++)
		{
			Collected_values_array[Collected_values_index] = {};
			
			Collected_values_array[Collected_values_index].Element_id = Text_area_elements[Textarea_element_index].id;
			Collected_values_array[Collected_values_index].Element_type = "textarea";
			Collected_values_array[Collected_values_index].Element_value = Text_area_elements[Textarea_element_index].value;
			
			Collected_values_index++;
		}
		
		STORED_COLLECTED_VALUES_ARRAY = Collected_values_array;
		
		// fill output form HTML
	
		Get_estimate_output_form(Form_id);
		
		
		// fill output form with values just entered
		
		for (Collected_values_index = 0; Collected_values_index < Collected_values_array.length; Collected_values_index++)
		{
			
			This_form_element = document.getElementById(Collected_values_array[Collected_values_index].Element_id);
						
			switch(This_form_element.tagName.toLowerCase())
			{
				case "input":
					This_form_element.value = Collected_values_array[Collected_values_index].Element_value;
					break;
				
				default:
					This_form_element.innerHTML = Collected_values_array[Collected_values_index].Element_value;
			}
			
		}
		

		// Set IDs for form and estimate request into newly loaded output form
		
		document.getElementById("QuoteRequestId").value = Quote_request_id;
				
		document.getElementById("QuoteRequestFormId").value = Form_id;
		
				
		
		
		// add user information to output form
		
		var Requested_by = USER_RECORD.Name + "<br>";
		
		Requested_by += Build_user_company_contact_HTML(USER_RECORD);
		
		document.getElementById("QuoteRequestedBy").innerHTML = Requested_by;

		var Requested_by_address = Build_user_primary_address_HTML(USER_RECORD);
		
		document.getElementById("QuoteRequestedByAddress").innerHTML = Requested_by_address;
		
		
		// set display for date and estimate request id
		
		document.getElementById("QuoteRequestIdDisplay").innerHTML = Quote_request_id;

		
		var Today_long_date = Long_date(new Date());
		
		document.getElementById("QuoteRequestDate").innerHTML = Today_long_date;
		
		

		// change special formatting based on values entered by user
		
		// NOTE: these fields (Laminate, LaminateThickness, Shrinkwrap, ShrinkwrapLots)
		// must be present in all input and output forms, 
		// if not needed for that item, then make them hidden fields.

		var Laminate_value = document.getElementById("Laminate").innerHTML
		
		if(Laminate_value == "None")
		{
			document.getElementById("LaminateThickness").style.visibility = "hidden"
		}
		
		var Shrinkwrap_value = document.getElementById("Shrinkwrap").innerHTML
		
		if(Shrinkwrap_value == "Yes")
		{
			document.getElementById("ShrinkwrapLots").innerHTML = "(lots of " + document.getElementById("ShrinkwrapLots").innerHTML + ")"
		}
		
		
		
		Set_page_alert("Please review your quote request and edit as needed.<br>When everything is correct, click the Submit Quote Request button.")
			

	}





function Edit_estimate_request()
	{
		// called when an output form is showing on page and user wants to change Estimate Specifications
			
		var Form_id = document.getElementById("QuoteRequestFormId").value;
		
		var Quote_request_id = document.getElementById("QuoteRequestId").value;

		var Is_update_estimate_form = true;
		
		Get_estimate_input_form(Form_id,Quote_request_id,Is_update_estimate_form)
				
		// fill with previously entered values
		
		if(STORED_COLLECTED_VALUES_ARRAY == null)
		{
			Set_page_alert("ERROR: Previously entered values are not available. Contact tech support.")
		}
		else
		{
		
			for (var Stored_collected_values_index = 0; Stored_collected_values_index < STORED_COLLECTED_VALUES_ARRAY.length; Stored_collected_values_index++)
			{
				
				This_form_element = document.getElementById(STORED_COLLECTED_VALUES_ARRAY[Stored_collected_values_index].Element_id);
				
				switch (STORED_COLLECTED_VALUES_ARRAY[Stored_collected_values_index].Element_type)
				{
				
					case "checkbox":
						if(STORED_COLLECTED_VALUES_ARRAY[Stored_collected_values_index].Element_value == "None")
						{
							This_form_element.checked = false;
						}
						else
						{
							This_form_element.checked = true;
						}
						break;
					
					default:
						This_form_element.value = STORED_COLLECTED_VALUES_ARRAY[Stored_collected_values_index].Element_value;
				}
				
			}
			
			document.getElementById("QuoteRequestFormId").value = Form_id
						
			document.getElementById("QuoteRequestId").value = Quote_request_id

			if( document.getElementById("SaddleStitchTable") != null)
			{
				Saddle_stitch_show_hide();
			}
		


			Set_page_alert("Make your desired changes and then click Update Quote Request button.")
		}
	}






function Submit_estimate_request()
	{
		// submit the JSON and OutputHTML to 4D Server   Ascii 11 delimited (vertical tab)
		// syntax: 
		// QuoteRequestId [Ascii 11] 
		// User Given Job Title [Ascii 11] 
		// Collected Values Stringified JSON [Ascii 11] 
		// OutputHTML [Ascii 11] 
		// Requested_for_name [Ascii 11] 
		// Requested_for_email [Ascii 11] 
		// Requested_for_company [Ascii 11] 
		// Requested_for_phone [Ascii 11] 
		// Sales_rep_name [Ascii 11] 
		// Sales_rep_email [Ascii 11] 
		// Quantities_requested (# delimited) [Ascii 11]
		
		
		var Print_qty_1_value = document.getElementById("PrintQty1").innerHTML;
		var Print_qty_2_value = document.getElementById("PrintQty2").innerHTML;
		var Print_qty_3_value = document.getElementById("PrintQty3").innerHTML;
		
		if(String_trim(Print_qty_1_value) == "")
		{
			// error do not allow submit.
			Set_page_alert('Please edit quote specifications to enter a value for "Print Quantity"');
						 
		}
		else
		{
			var Post_to_url = POST_TO_URL_PREFIX + "submitestimate";
					
			var Estimate_request_id = document.getElementById("QuoteRequestId").value;
			
			var User_given_job_title = document.getElementById("JobTitle").innerHTML;
			
			var Stringified_JSON_collected_values = JSON.stringify(STORED_COLLECTED_VALUES_ARRAY);
			
			var Output_HTML = document.getElementById("form_div").innerHTML;
			
			var Requested_for_name = document.getElementById("Requested_for_name").value;
			
			var Requested_for_email = document.getElementById("Requested_for_email").value;
			
			var Requested_for_company = document.getElementById("Requested_for_company").value;
			
			var Requested_for_phone = document.getElementById("Requested_for_phone").value;
			
			var Sales_rep_name = document.getElementById("salesrepname").innerHTML;
			
			var Sales_rep_email = document.getElementById("salesrepemail").innerHTML;
			
			var Quantities_requested = Print_qty_1_value + "#" + Print_qty_2_value + "#" + Print_qty_3_value

	
			
			var Delimiter = String.fromCharCode(11)
			
			// build request
					
			var Submit_estimate_request_data = Estimate_request_id + Delimiter;
			
			Submit_estimate_request_data += User_given_job_title + Delimiter;
			
			Submit_estimate_request_data += Stringified_JSON_collected_values + Delimiter;
			
			Submit_estimate_request_data += Output_HTML + Delimiter;
			
			Submit_estimate_request_data += Requested_for_name + Delimiter;
			
			Submit_estimate_request_data += Requested_for_email + Delimiter;
			
			Submit_estimate_request_data += Requested_for_company + Delimiter;
			
			Submit_estimate_request_data += Requested_for_phone + Delimiter;
			
			Submit_estimate_request_data += Sales_rep_name + Delimiter;
			
			Submit_estimate_request_data += Sales_rep_email + Delimiter;
			
			Submit_estimate_request_data += Quantities_requested + Delimiter;
			
			
			var Response_text = XMLHttpRequest_PostTo(Submit_estimate_request_data,Post_to_url)
	
				
			if(Response_text == "0")
			{
				Set_page_alert("Error saving request, call tech support")
			}
			else
			{
				Set_page_alert("Your request has been submitted")
				
				// reset collected values
				STORED_COLLECTED_VALUES_ARRAY = null
				
				Set_buttons_div("");
				Set_form_div("");
	
			}
			
		}
		
		
		
		
		
	}



function Submit_registration()
	{
	var Post_to_url = POST_TO_URL_PREFIX + "register";
	var Request_text = ""
	var Response_text = ""
	var Registration_input_fields = Validate_contact_info_entered(Registration_input_fields);
	
		if(Registration_input_fields.length == 0)
		{
			//  there was an error, do not submit (Error text was displayed in Validate_contact_info_entered_
		}
		else
		{
			// no error on validation,
			// submit fields using tab delimited syntax: [Field id] [Tab Ascii 9] [Field value] [CarriageReturn Ascii 13] 
		
			for (var Input_element_index = 0; Input_element_index < Registration_input_fields.length; Input_element_index++)
			{
				Request_text += Registration_input_fields[Input_element_index].id
				Request_text += "\t"
				Request_text += Registration_input_fields[Input_element_index].value
				Request_text += "\r"
			}
				
		
			Response_text = XMLHttpRequest_PostTo(Request_text,Post_to_url)
			
			var Response_text_error = Response_text.substr(0,5).toLowerCase()
			
			if(Response_text_error == "error")
			{
				Set_page_alert(Response_text);
				var Registration_was_successful = false;
			}
			else
			{
				var Registration_was_successful = true;
			}
		
		
			if(Registration_was_successful)
			{
				var User_JSON_stringified = Response_text
				
				eval("USER_RECORD = "+ User_JSON_stringified + ";");
							
				Set_page_alert("Hello "+ USER_RECORD.Salutation_informal + ", please select the type of printing for your quote at the left.");
							
				Set_form_div("")
				Set_login_div("")
				
				User_summary_set_content();
				
				Get_navlinks();
			}	
		}
	}
	
	
	
		
	

function Validate_contact_info_entered()
	{
		var Registration_input_fields = form_div.getElementsByTagName("input")
		
		var Error_text = "";
		
		// validate data entered
	
		for (var Input_element_index = 0; Input_element_index < Registration_input_fields.length; Input_element_index++)
		{
			if( (Registration_input_fields[Input_element_index].alt == "required") & ( String_trim(Registration_input_fields[Input_element_index].value) == "") )
			{
				Error_text += Registration_input_fields[Input_element_index].name + " is required.<br>"
			}
		}
			
		if(document.getElementById("Password").value != document.getElementById("PasswordConfirm").value)
		{
			Error_text += "Password entered must match Password Confirm.<br>"
		}
		
		if(document.getElementById("Password").value.indexOf("@") > -1)
		{
			Error_text += "Password may not contain @ sign.<br>"
		}
		
		if(isEmail(document.getElementById("Email").value) != true)
		{
			Error_text += "Email address is invalid.<br>"
		}
		
		if(Error_text != "")
		{
			Set_page_alert(Error_text);
			
			Registration_input_fields = [];
		}

		return Registration_input_fields;
	}



function Set_hidden_radio_value_from_td(radio_element)
	{
	// assumes the label for the radio button is in the next <td> from the radio <input>
	// assumes a hidden field with the ID of the radio group name
		if(radio_element.checked == true)
		{
		  // set value of hidden field
		  var This_cell_index = radio_element.offsetParent.cellIndex 
		  
		  var Row_cells = radio_element.offsetParent.parentNode.cells
		  
		  for(var Row_cells_index = 0 ; Row_cells_index < Row_cells.length ; Row_cells_index++)
		  {
		  	if(Row_cells_index == This_cell_index )
		  	{
		  		Label_value = Row_cells[Row_cells_index + 1].innerHTML
		  	}
		  }
		  
		  document.getElementById(radio_element.name).value = Label_value
		  
		}
		else
		{
			// do nothing
		}
		
	}
	


function Open_password_subwindow()
	{
	var sub_window_password  = window.open('','linksubwindow','width=360, height=280, resizable, scrollbars, status, toolbar, menubar')
	sub_window_password.location = "/password_request.html"
	}


function Set_fold_image(fold_element)
	{
		document.getElementById("foldimage").src = '/images/fold_' + fold_element.value + '.gif'
	}

function Saddle_stitch_show_hide()
{
	var cover_choice = document.getElementById("CoverChoice").value;
	var number_of_pages =  parseInt( document.getElementById("NumberOfPages").value );
	var ink_cover_row = document.getElementById("InkCoverRow")
	var ink_inside_cover_row = document.getElementById("InkInsideCoverRow")
	
	var varnish_outside_cover_row = document.getElementById("VarnishOutsideCoverRow")
	var varnish_inside_cover_row = document.getElementById("VarnishInsideCoverRow")
	
	var paper_cover_row = document.getElementById("PaperCoverRow")
	var bindery_cover_row = document.getElementById("BinderyCoverRow")
	var cover_style_row = document.getElementById("CoverStyleRow")

	var displayed_row_style = "table-row"
	
	if(isIE())
	{
		displayed_row_style = "inline"
	}

	if(cover_choice == "Plus Cover...")
	{
		ink_cover_row.style.display = displayed_row_style
		ink_inside_cover_row.style.display = displayed_row_style
		varnish_outside_cover_row.style.display = displayed_row_style
		varnish_inside_cover_row.style.display = displayed_row_style
		paper_cover_row.style.display = displayed_row_style
		bindery_cover_row.style.display = displayed_row_style
		cover_style_row.style.display = displayed_row_style
		
		if(document.getElementById("InkOutsideCover").value == "Self cover, ink same as inside")
		{
			document.getElementById("InkOutsideCover").value = "4-color process"
		}
		
		if(document.getElementById("InkInsideCover").value == "Self cover, ink same as inside")
		{
			document.getElementById("InkInsideCover").value = "4-color process"
		}
		
		if(document.getElementById("VarnishOutsideCover").value == "Self cover, varnish same as inside")
		{
			document.getElementById("VarnishOutsideCover").value = "AQ - overall gloss aqueous coating"
		}
		
		if(document.getElementById("VarnishInsideCover").value == "Self cover, varnish same as inside")
		{
			document.getElementById("VarnishInsideCover").value = "AQ - overall gloss aqueous coating"
		}
		
		if(document.getElementById("PaperCover").value == "Self cover, paper same as inside")
		{
			document.getElementById("PaperCover").value = "80 pound gloss coated cover"
		}
		
	}
	else
	{
		ink_cover_row.style.display = "none"
		ink_inside_cover_row.style.display = "none"
		varnish_outside_cover_row.style.display = "none"
		varnish_inside_cover_row.style.display = "none"
		paper_cover_row.style.display = "none"
		bindery_cover_row.style.display = "none"
		cover_style_row.style.display = "none"
		document.getElementById("InkOutsideCover").value = "Self cover, ink same as inside"
		document.getElementById("InkInsideCover").value = "Self cover, ink same as inside"
		document.getElementById("VarnishOutsideCover").value = "Self cover, varnish same as inside"
		document.getElementById("VarnishInsideCover").value = "Self cover, varnish same as inside"
		document.getElementById("VarnishCover").value = "Self cover, paper same as inside"
	}
	
	
}

function Saddle_stitch_page_count()
{
	var cover_choice = document.getElementById("CoverChoice").value;
	var number_of_pages =  parseFloat( document.getElementById("NumberOfPages").value );
	
	var signature_count = number_of_pages / 4
	
	if( (signature_count === parseInt(signature_count)) & (number_of_pages != 0) )
	{
	  Set_page_alert("");
	}
	else
	{
		alert("Error: Saddle-stitch book page count must be evenly divisible by 4");
		setTimeout(function(){document.getElementById("NumberOfPages").focus();}, 10)
		
	}
	
}

function Print_estimate_request()
{
var set_width = 700;
var set_height = 460;
var print_window = eval("window.open('','subwindow','width=" + set_width + ",height=" + set_height + ",resizable,scrollbars,status,toolbar,menubar')");

var print_window_contents = '<html><link rel=StyleSheet type="text/css" href="/css_styles/basic.css"><body onload="window.print();">';

print_window_contents += document.getElementById("form_div").innerHTML;

print_window_contents += '</body></html>';

print_window.document.write(print_window_contents);

print_window.document.close();

print_window.focus();


}


