/**
 *
 * formchecker.js
 * javascript to validate CV forms 
 *
 * Luann Ebert
 * May, 2008; revised July, 2008
 */



String.prototype.trim = function() 
{
	return this.replace(/^\s+|\s+$/g, "");
}

// function to validate required fields
function validateField( field_id, myerrormessage)
{
	var nameField=document.getElementById(field_id);
	if(!nameField){return;}
	var errorContainer=nameField.parentNode.getElementsByTagName('span')[0];
	if(!errorContainer){return;}
	var errorMessage='';
	errorContainer.firstChild.nodeValue=' ';
	var nameValue=nameField.value.trim();
	if (nameValue=='')
	{
		errorMessage=myerrormessage;
	}
	
	if(errorMessage !='')
	{
		errorContainer.firstChild.nodeValue=errorMessage;
		nameField.focus();
		return false;
	}

	else
	{
		return true;
	}

}

/*
 * function to validate sales/general forms
 */
function validateSalesForm()
{	
// make sure current browser supports javascript, otherwise, bail
	if(!document.getElementById || !document.createTextNode) {return;}

//check First Name field value		
	if ( !validateField('fname', 'Please provide your name.') )
	{return false;}

//check Last Name field value		
	if ( !validateField('lname', 'Please provide your name.') )
	{return false;}

//check email field value, includes regular expression		
	var fieldElement=document.getElementById('email');
	if(!fieldElement){return;}
	var errorContainer=fieldElement.parentNode.getElementsByTagName('span')[0];
	if(!errorContainer){return;}
	var errorMessage='';
	errorContainer.firstChild.nodeValue=' ';
	var fieldValue=fieldElement.value.trim();
	if (fieldValue=='')
	{
		errorMessage="Please provide your email.";
	}
	// apply regular expression to email value
	var regex = /.+@.+[.].+/;
	if (!regex.test(fieldValue))
	{
		errorMessage="Please supply a valid email address."
	}
	
	if(errorMessage !='')
	{
		errorContainer.firstChild.nodeValue=errorMessage;
		fieldElement.focus();
		return false;
	}

//check Company field value	
	var companyField=document.getElementById('company');
	if(!companyField){return;}

	// put error span in variable
	var errorContainer=companyField.parentNode.getElementsByTagName('span')[0];
	if(!errorContainer){return;}
	var errorMessage='';
	errorContainer.firstChild.nodeValue=' ';
	// apply lamda trim function to user's input and put into a variable
	var companyValue=companyField.value.trim();
	if (companyValue=='')
	{
		errorMessage="Please provide your company name.";
	}
	if(errorMessage !='')
	{
		errorContainer.firstChild.nodeValue=errorMessage;
		companyField.focus();
		return false;
	}

	// validate city
	if ( !validateField('city', 'Please provide your city.') )
	{return false;}


	// validate country
	if ( !validateField('countrySelect', 'Please provide your country.') )
	{return false;}

	// validate state...applicable only if US
	if ( document.getElementById('countrySelect').value == 'US')
 	{
		if ( !validateField('stateSelect', 'Please provide your state.') )
		{return false;}
	}

	else
	{
		return true;
	}
}

/*
 * function to validate services forms
 */
function validateServicesForm()
{	
// make sure current browser supports javascript, otherwise, bail
	if(!document.getElementById || !document.createTextNode) {return;}

//check Company field value	
	var companyField=document.getElementById('company');
	if(!companyField){return;}
	// put error span in variable
	var errorContainer=companyField.parentNode.getElementsByTagName('span')[0];
	if(!errorContainer){return;}
	var errorMessage='';
	errorContainer.firstChild.nodeValue=' ';
	// apply lamda trim function to user's input and put into a variable
	var companyValue=companyField.value.trim();
	if (companyValue=='')
	{
		errorMessage="Please provide your company name.";
	}
	if(errorMessage !='')
	{
		errorContainer.firstChild.nodeValue=errorMessage;
		companyField.focus();
		return false;
	}
	
//check First Name field value		
	if ( !validateField('fname', 'Please provide your name.') )
	{return false;}

//check Last Name field value		
	if ( !validateField('lname', 'Please provide your name.') )
	{return false;}

//check email field value, includes regular expression		
	var fieldElement=document.getElementById('email');
	if(!fieldElement){return;}
	var errorContainer=fieldElement.parentNode.getElementsByTagName('span')[0];
	if(!errorContainer){return;}
	var errorMessage='';
	errorContainer.firstChild.nodeValue=' ';
	var fieldValue=fieldElement.value.trim();
	if (fieldValue=='')
	{
		errorMessage="Please provide your email.";
	}
	// apply regular expression to email value
	var regex = /.+@.+[.].+/;
	if (!regex.test(fieldValue))
	{
		errorMessage="Please supply a valid email address."
	}
	
	if(errorMessage !='')
	{
		errorContainer.firstChild.nodeValue=errorMessage;
		fieldElement.focus();
		return false;
	}

//check phone number field value, includes regular expression		
	var fieldElement=document.getElementById('phone');
	if(!fieldElement){return;}
	var errorContainer=fieldElement.parentNode.getElementsByTagName('span')[0];
	if(!errorContainer){return;}
	var errorMessage='';
	errorContainer.firstChild.nodeValue=' ';
	var fieldValue=fieldElement.value.trim();
	if (fieldValue=='')
	{
		errorMessage="Please provide your phone number.";
	}
	// apply regular expression to phone value...just checks to make sure a number is there
	var regex = /\d+/;
	if (!regex.test(fieldValue))
	{
		errorMessage="Please supply a valid phone number."
	}
	if(errorMessage !='')
	{
		errorContainer.firstChild.nodeValue=errorMessage;
		fieldElement.focus();
		return false;
	} 

	// validate address1
	if ( !validateField('address1', 'Please provide your address.') )
	{return false;}

	// validate city
	if ( !validateField('city', 'Please provide your city.') )
	{return false;}

	// validate country
	if ( !validateField('countrySelect', 'Please provide your country.') )
	{return false;}

	// validate state...applicable only if US
	if ( document.getElementById('countrySelect').value == 'US')
 	{
		if ( !validateField('stateSelect', 'Please provide your state.') )
		{return false;}
	}

//if everything checks out, submit the form
	else
	{
		return true;
	}
}
