/*** Dynamic Drop Down Boxes ***/
function setOptions(chosen)
	{
		var selbox = document.getElementById('supportForm').reason;
 
		selbox.options.length = 0;
		if (chosen == " ")
			{
				selbox.options[selbox.options.length] = new Option('Please select one of the options above first',' ');
				document.getElementById('account_name_row').style.visibility = 'hidden';
				document.getElementById('account_name_row').style.display = 'none';
			}
		if (chosen == "prospective")
			{
				selbox.options[selbox.options.length] = new Option('Sales Question','sales');
				document.getElementById('account_name_row').style.visibility = 'hidden';
				document.getElementById('account_name_row').style.display = 'none';
			}
		if (chosen == "existing")
			{
				selbox.options[selbox.options.length] = new Option('Tech Support Question','support');
				selbox.options[selbox.options.length] = new Option('Billing Question','cs');
				selbox.options[selbox.options.length] = new Option('Sales Question','sales');
				document.getElementById('account_name_row').style.visibility = 'visible';
				if (navigator.appName == 'Microsoft Internet Explorer')
					{
						document.getElementById('account_name_row').style.display = 'block';
					}
				else
					{
						document.getElementById('account_name_row').style.display = 'table-row';
					}
			}
	}
	

/*** Form Validation ***/

// On Sumbit Function
function validateFormOnSubmit(theForm)
	{
		var reason = "";
		
		reason += validateName(theForm.name);
		reason += validateEmail(theForm.address);
		reason += validatePhone(theForm.number);
		reason += validateDropDown(theForm.ctype);
		
		if (theForm.ctype.options[theForm.ctype.selectedIndex].value == "existing")
			{
				reason += validateAccountName(theForm.account_name);	
			}
		  
		if (reason != "")
			{
				alert("Some fields need correction:\n" + reason);
				return false;
			}
		
		return true;
	}

// Validate Name
function validateName(fld)
	{
    var error = "";
 
    if (fld.value == "")
		{
			fld.style.background = 'Yellow'; 
			error = "You didn't enter a name.\n";
    	} 
	else if ((fld.value.length < 5) || (fld.value.length > 30)) 
		{
			fld.style.background = 'Yellow'; 
			error = "The name you entered is the wrong length.\n"; 
    	}
	else
		{
        	fld.style.background = 'White';
    	}
    return error;
}

// Valiate Acocunt Name
function validateAccountName(fld)
	{
    var error = "";
 
    if (fld.value == "")
		{
			fld.style.background = 'Yellow'; 
			error = "You didn't enter an account name.\n";
    	} 
	else if ((fld.value.length < 5) || (fld.value.length > 40)) 
		{
			fld.style.background = 'Yellow'; 
			error = "The account name you entered is the wrong length.\n"; 
    	}
	else
		{
        	fld.style.background = 'White';
    	}
    return error;
}

// Validate Drop Down
function validateDropDown(fld)
	{
    	var error = "";
 
    	if (fld.options[fld.selectedIndex].value == " ")
			{
        		fld.style.background = 'Yellow';
        		error = "Please select your customer type.\n"
    		}
		else
			{
        		fld.style.background = 'White';
    		}
    	return error;  
	}

// Trim the whitespace
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

// Validate Email
function validateEmail(fld)
	{
    	var error = "";
    	var tfld = trim(fld.value);
    	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    	if (fld.value == "")
			{
        		fld.style.background = 'Yellow';
        		error = "You didn't enter an email address.\n";
    		}
		else if (!emailFilter.test(tfld))
			{
       			fld.style.background = 'Yellow';
        		error = "Please enter a valid email address.\n";
    		}
		else if (fld.value.match(illegalChars))
			{
        		fld.style.background = 'Yellow';
        		error = "The email address contains illegal characters.\n";
    		}
		else			
			{
        		fld.style.background = 'White';
    		}
    	return error;
	}
	
// Validate Phone Number
function validatePhone(fld)
	{
    	var error = "";
    	var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   		if (fld.value == "")
			{
        		error = "You didn't enter a phone number.\n";
        		fld.style.background = 'Yellow';
    		}
		else if (isNaN(parseInt(stripped)))
			{
        		error = "The phone number contains illegal characters. Please use a 10 digit format. (Ex. 1234567890)\n";
        		fld.style.background = 'Yellow';
    		}
		else if (!(stripped.length == 10))
			{
        		error = "The phone number is the wrong length. Please use 10 digits (xxxxxxxxx)\n";
        		fld.style.background = 'Yellow';
    		}
		
    	return error;
	}