// MNB
// 4/7/09

function validateFormAndSubmit()
{
	var errorMsg = "";

	errorMsg += validateFirstName();
	errorMsg += validateLastName();
	errorMsg += validateAddress();
	errorMsg += validateCity();
	errorMsg += validateState();
	errorMsg += validateEmails();
	errorMsg += validateZip();
	errorMsg += validatePhone();

	if (errorMsg != "")
	{
		alert("The following fields require correction before submission:\n\n" + errorMsg);
		return false;
	}
	return(true);
}
/*
function validateEngineerFormAndSubmit()
{
    var errorMsg = "";

	errorMsg += validateFirstName();
	errorMsg += validateLastName();
	errorMsg += validateCompany();
	errorMsg += validateEmails();
	errorMsg += validatePhone();
	errorMsg += validateModel();
	errorMsg += validateSubject();
	errorMsg += validateComments();
	
	if (errorMsg != "")
	{
		alert("The following fields require correction before submission:\n\n" + errorMsg);
		return false;
	}
	return(true);
}
*/
// Validates first name for empty field.
function validateFirstName()
{
	if (document.contactForm.first_name.value == '')
	{
		return "* First Name: Please enter.\n";
	}
	return "";
}
// Validates last name for empty field.
function validateLastName()
{
	if (document.contactForm.last_name.value == '')
	{
		return "* Last Name: Please enter.\n";
	}
	return "";
}
// Validates address1 for empty field.
function validateAddress()
{
	if (document.contactForm.address1.value == '')
	{
		return "* Address 1: Please enter.\n";
	}
	return "";
}
// Validates city for empty field.
function validateCity()
{
	if (document.contactForm.city.value == '')
	{
		return "* City: Please enter.\n";
	}
	return "";
}
// Validates state for empty field.
function validateState()
{
    var selectedIndex = document.contactForm.state.selectedIndex
    
	if (document.contactForm.state.options[selectedIndex].value == '- -')
	{
		return "* State: Please select.\n";
	}
	return "";
}
// Validates zip for empty field.
function validateZip()
{
	if (document.contactForm.zipCode.value == '')
	{
		return "* Zip: Please enter.\n";
	}
	return "";
}
// Validates customer for empty field.
function validateCustomer()
{ 
	if (document.contactForm.customer.value == '')
	{
		return "* Customer #: Please enter.\n";
	}
	return "";
}
// Validates po for empty field.
function validatePO()
{
	if (document.contactForm.po.value == '')
	{
		return "* PO #: Please enter.\n";
	}
	return "";
}
// Validates subject for empty field.
function validateSubject()
{
	if (document.contactForm.subject.value == '')
	{
		return "* Subject: Please enter.\n";
	}
	return "";
}
// Validates company for empty field.
function validateCompany()
{
	if (document.contactForm.company.value == '')
	{
		return "* Company: Please enter your question.\n";
	}
	return "";
}
// Validates shipping for empty field.
function validateShipping()
{
    var selectedIndex = document.contactForm.shipping.selectedIndex
    
    //alert(document.contactForm.shipping.options[selectedIndex].value);
    
	if (document.contactForm.shipping.options[selectedIndex].value == '- -')
	{
		return "* Shipping: Please select.\n";
	}
	return "";
}
// Validates model for empty field.
function validateModel()
{
	if (document.contactForm.model.value == '')
	{
		return "* Model: Please enter your question.\n";
	}
	return "";
}
// Validates comments for empty field.
function validateComments()
{
	if (document.contactForm.comments.value == '')
	{
		return "* Comments: Please enter your question.\n";
	}
	return "";
}
// Validates quantity for empty field.
function validateQty()
{
    var qty1Value = document.contactForm.qty1.value;
    var qty2Value = document.contactForm.qty2.value;
    var qty3Value = document.contactForm.qty3.value;

    var totalQty = qty1Value + qty2Value + qty3Value;

    //alert(totalQty);

	if (totalQty == '' || totalQty < 1)
	{
		return "* Part QTY: Must be greater than zero\n";
	}
	return "";
}
// Validates phone for empty field as well as ensures proper format.
function validatePhone()
{
	var reg = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
	var phone = document.contactForm.phone.value;
	
	/*if (phone != '')
	{
	    if (reg.test(phone) == false) 
	    {
		    return "* Phone: Must be in format (xxx) xxx-xxxx.\n";
	    }
	}*/
	
	if (phone == '')
	{
	    return "* Phone: Please enter.\n";
	}
	
	return "";
}
// Validates emails for empty field, confirmation that both emails are the same, as well as ensures proper format.
function validateEmails()
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var address = document.contactForm.email.value;
	
	if (address == '')
	{
		return "* Email: Please enter.\n";
	}
	else if (reg.test(address) == false) 
	{
		return "* Email: Please enter a valid email address.\n";
	}
	
	return "";
}

function removePartItem(currentRow)
{
	var partTBody = document.getElementById('partsOrderFormTBODY');
	var deleteTRIndex = currentRow.parentNode.parentNode.rowIndex;
	partTBody.deleteRow(deleteTRIndex-1);
	reFactorTable();
}

function addPartItem(currentRow)
{
	var currentTRIndex = currentRow.parentNode.parentNode.rowIndex;
	var partTBody = document.getElementById('partsOrderFormTBODY').insertRow(currentTRIndex);
	var cell0 = partTBody.insertCell(0);
	var cell1 = partTBody.insertCell(1);
	var cell2 = partTBody.insertCell(2);

	currentRow.parentNode.innerHTML = '<input type="image" id="Item" src="/business/images/templates/' + colorSchemeJS + '/expand-collapse/collapse-text.gif" alt="Remove Part" title="Remove Part" onclick="removePartItem(this);" />';

	cell0.innerHTML = '<input type="image" id="addItem" src="/business/images/templates/' + colorSchemeJS + '/expand-collapse/expand-text.gif" alt="Add Part" title="Add Part" onclick="addPartItem(this);" />';
	cell1.innerHTML = currentTRIndex + 1;
	cell2.innerHTML = '<select class="col1Input" onchange="processContact(this);" name="modelNumber" id="modelNumber" title="Model Number"><option selected value="- -">Select a model</option><option>AG-DV2000</option><option>AG-DVC200</option><option>AJ-D200</option><option>AJ-D210</option><option>AJ-D215</option><option>AJ-D220</option><option>AJ-D230</option><option>AJ-D230H</option><option>AJ-D250</option><option>AJ-D440</option><option>AJ-D450</option><option>AJ-D455</option><option>AJ-D640</option><option>AJ-D650</option><option>AJ-D700</option><option>AJ-D750</option><option>AJ-D810</option><option>AJ-D850</option><option>AJ-D950</option><option>AJ-D95DCP</option><option>AJ-LT75</option> <option>AJ-VF10</option></select>';
}

function reFactorTable()
{
	var partTBody = document.getElementById('partsOrderFormTBODY');
	var rowNumber = 1;
	for(x=0;x<partTBody.childNodes.length;x++)
	{
		if(partTBody.childNodes[x].nodeType == 1)
		{
			tempTD = partTBody.childNodes[x].getElementsByTagName('td');
			tempTD[1].innerHTML = rowNumber
			rowNumber++;
		}
	}
}
