// JavaScript Document

function verifyContactForm(frm){	
	
	var allReq = "\nNote: All fields are required";
	if(isEmpty(frm.contact_name)){
		alert("Cannot submit without your name. Please enter your name."+allReq);
		return false;
	}
	if(isEmpty(frm.contact_email)){
		alert("Cannot submit without your email address. Please enter your email."+allReq);
		return false;
	}
	else if(!isEmailAddr(frm.contact_email.value)){
		alert("You have entered an invalid email address. Please enter valid email address."+allReq);
		return false;		
	}
	if(isEmpty(frm.contact_phone)){
		alert("Cannot submit without your phone number. Please enter your phone."+allReq);
		return false;
	}
	else if(!isPhoneNumber(frm.contact_phone.value)){
		alert("You have entered invalid phone number. Please enter phone number without any characters lik ()or-.");
		return false;
	}
	if(isEmpty(frm.contact_msg)){
		alert("Cannot submit without a message. Please enter your message."+allReq);
		return false;
	}
	else if(frm.contact_msg.value.length >900){
		alert("Cannot submit with such a long message. Please enter your message less than 900 characters."+allReq);
		return false;
	}
	return true;
		
}

function isEmailAddr(email)
{
	var result = false;
	var theStr = new String(email);
	var index = theStr.indexOf("@");
	if (index > 0){
		var pindex = theStr.indexOf(".", index);
		if ((pindex > index + 1) && (theStr.length > pindex + 1)){
			result = true; 
		}
	}
	return result;
}

function isEmpty(fld){
	if(fld.value==""){
		return true;
	}
	else{
		return false;
	}	
}

function phoneMask(strField)
{
    var patt = /([1-9]\d{2}).*(\d{3}).*(\d{4})/;
    var donepatt = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
    var str = strField.value;
    var result;
    if (!str.match(donepatt)){
        result = str.match(patt);
        if (result!= null){
            strField.value = strField.value.replace(/[^\d]/gi,'');
            str = '(' + result[1] + ')' + result[2] + '-' + result[3];
            strField.value = str;
        }
        else{
            if (strField.value.match(/[^\d]/gi)){
                strField.value = strField.value.replace(/[^\d]/gi,'');
            }
        }
    }
}

function isPhoneNumber(s)
{
	// Check for correct phone number
	rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
 	if (!rePhoneNumber.test(s)) {
		return false;
	}
	return true;
}