// JavaScript Document

function resetMessage()
{
	var responseHTML = "<p style='font-size: .9em;'>Please enter your question in the area below.<br />We will contact you ASAP.</p>";
	var myDiv = document.getElementById('selectMessage');
	myDiv.innerHTML = responseHTML;
}


function addCustomerOptions(ctrlField)
{
	var selector = document.getElementById('selectReq');
	var items = selector.getElementsByTagName("option");
	if (items.length < 3)
	{
		if (!IsEmpty(ctrlField))
		{
			var optn = document.createElement("OPTION");
			optn.text = "I need an inventory update.";
			optn.value = "inventory";
			selector.options.add(optn);
			
			var optn2 = document.createElement("OPTION");
			optn2.text = "I would like to receive packing materials.";
			optn2.value = "materials";
			selector.options.add(optn2);
	
			var optn3 = document.createElement("OPTION");
			optn3.text = "I need more packing materials.";
			optn3.value = "morematerials";
			selector.options.add(optn3);
	
			var optn4 = document.createElement("OPTION");
			optn4.text = "I need a tax exemption letter.";
			optn4.value = "tax";
			selector.options.add(optn4);
	
			
			var optn5 = document.createElement("OPTION");
			optn5.text = "I would like to schedule a pick-up.";
			optn5.value = "pickup";
			selector.options.add(optn5);
	
			var optn6 = document.createElement("OPTION");
			optn6.text = "I am ready to close my shipment.";
			optn6.value = "close";
			selector.options.add(optn6);
			
			var optn7 = document.createElement("OPTION");
			optn7.text = "What is the status of my shipment?";
			optn7.value = "status";
			selector.options.add(optn7);
			
			var optn8 = document.createElement("OPTION");
			optn8.text = "What is the contact info for my shipment at destination?";
			optn8.value = "destination";
			selector.options.add(optn8);
		}
	}
}

function displayContextMessage(selector)
{
	var selection = selector.options[selector.selectedIndex].value;
	var responseHTML;
	if (selection =="estimate")
	{
			responseHTML = "<p style='font-size: .9em;'>Estimates are available days and evenings by appointment.<br />Please enter your contact details in this form or call (908) 258-7983 to arrange an appointment.</p>";
	}
	else if (selection =="inventory")
	{
		responseHTML = "<p style='font-size: .9em;'>Inventory reports are sent by email to the address your provide above.</p>";
	}
	else if (selection =="materials")
	{
		responseHTML = "<p style='font-size: .9em;'>Please enter your availability to receive materials below or let us know a safe place at your home for delivery.</p>";
	}
	else if (selection =="morematerials")
	{
		responseHTML = "<p style='font-size: .9em;'>Please let us know what additional materials you need in the space below.</p>";
	}
	else if (selection =="tax")
	{
		responseHTML = "<p style='font-size: .9em;'>Please make sure to include your control number above.</p>";
	}		
	else if (selection =="pickup")
	{
		responseHTML = "<p style='font-size: .9em;'>Please enter your desired pick-up date below.<br />We will contact you ASAP to discuss.</p>";
	}
	else if (selection =="close")
	{
		responseHTML = "<p style='font-size: .9em;'>This message will act as confirmation that all items you are expecting have arrived at our warehouse and that you are ready to ship.</p>";
	}
	else if (selection =="status")
	{
		responseHTML = "<p style='font-size: .9em;'>Providing shipment status may require us to contact an overseas office.<br />Please be aware that time differences may mean a 24hr turnaround.</p>";
	}		
	else if (selection =="destination")
	{
		responseHTML = "<p style='font-size: .9em;'>Please make sure to include your control number above.</p>";
	}		
	else if (selection =="question")
	{
		responseHTML = "<p style='font-size: .9em;'>Please enter your question in the area below.<br />We will contact you ASAP.</p>";
	}
	else if (selection =="expert")
	{
		responseHTML = "<p style='font-size: .9em;'>Please let us know briefly how we can help.<br />We will contact you ASAP.</p>";
	}
	var myDiv = document.getElementById('selectMessage');
	myDiv.innerHTML = responseHTML;
}

function addressBlur(addrField)
{
	var emailAddress = addrField.value;
	var addrOK = validAddress(emailAddress);
	if (addrOK == false)
	{
		alert ("Invalid address format.");
		addrField.focus();
	}
}

function validAddress(addr)
	{
	   	if (addr != "")
		{
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			var address = addr;
			if(reg.test(address) == false) {
				return false;
			}
			else {return true;}
		}
	}


function IsEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
   (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}

function formValidate(myForm)
{
		//alert(myForm.custName.value);
		var errorText = "<ul style='color: #cc3300; font-weight: bold;'>";
		var valid = true;
		
		if (IsEmpty(myForm.custName))
		{
				errorText += "<li>Please enter your name.</li>";
				valid = false;
		}
		if (IsEmpty(myForm.tel) && IsEmpty(myForm.email))
		{
				errorText += "<li>You must enter either a telephone number or an email address.</li>";
				valid = false;
		}
		
		if (!IsEmpty(myForm.email))
		{
				var addrOK = validAddress(myForm.email.value);
				if (addrOK == false)
				{
					errorText += "<li>Invalid email format.</li>";
					valid = false;
				}
		}
		
		if (valid == false)
		{
			errorText += "</ul>";
			var myDiv = document.getElementById('selectMessage');
			myDiv.innerHTML= errorText;
		}
		return valid;
}
