// JavaScript Document
function isBlank(val)
{
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
	}
	return true;
}
	
function isInteger(val)
{
	if (isBlank(val)){return false;}
	for(var i=0;i<val.length;i++){
		if(!isDigit(val.charAt(i))){return false;}
	}
	return true;
}
function isDecimal(val)	
{
	var DecimalFound;
	DecimalFound = false;
	
	if (isBlank(val)){val = 0;}
	for(var i=0;i<val.length;i++){
		if(!isDigit(val.charAt(i)))
		{
			if (DecimalFound == false)
			{
				if (!isDecimalPoint(val.charAt(i)))
				{
					return false;
				}
				else
				{
					DecimalFound = true;
				}
			}
			else
			{
				return false;
			}
		}
	}
	return true;
}
function isDigit(num) 
{
	if (num.length>1){return false;}
	var string="1234567890";
	if (string.indexOf(num)!=-1){return true;}
	return false;
}
function isDecimalPoint(num) 
{
	if (num.length>1){return false;}
	var string=".";
	if (string.indexOf(num)!=-1){return true;}
	return false;
}
function check_required()
{
	var errorMsg = "";
    var emailReg = "\\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$";
	var regex = new RegExp(emailReg);

    var phoneReg = "\[0-9]{3}[- ]?[0-9]{3}[- ]?[0-9]{4}$";
	var regex2 = new RegExp(phoneReg);

	//Check for a Department
	if (isBlank(document.roof_survey.first_name.value)){
		errorMsg += "\n\t You must enter your first name";
	}
	if (isBlank(document.roof_survey.last_name.value)){
		errorMsg += "\n\t You must enter your last name";
	}					
	if (regex.test(document.roof_survey.email.value) == false)
	{
		errorMsg += "\n\t You must enter a valid e-mail address";
	}		
	if (isBlank(document.roof_survey.title.value)){
		errorMsg += "\n\t You must enter your title";
	}
//	if (isBlank(document.roof_survey.phone.value)){
    if (regex2.test(document.roof_survey.phone.value) == false) {
        errorMsg += "\n\t You must enter your phone number";
	}
	//If there is aproblem with the form then display an error
	if (errorMsg != "")
	{
		msg = "_______________________________________________________________\n";
		msg += "The form has not been submitted because there are problem(s) with the form.\n";
		msg += "Please correct the problem(s) and re-submit the form.\n";
		msg += "_______________________________________________________________\n\n";
		msg += "The following field(s) need to be corrected: -\n";
		
		errorMsg += alert(msg + errorMsg + "\n\n");
		return false;
	}
	return true;
}