function show_hide(the_layer) { 
	if(document.getElementById(the_layer)) {
			document.getElementById(the_layer).style.display = 'inline';
			document.getElementById('div_all').style.display = 'inline';
	}
}

function hide_all() {
	if(document.getElementById('div_all')) {
		document.getElementById('div_all').style.display = 'none';
		document.getElementById('div_name').style.display = 'none';
		document.getElementById('div_phone').style.display = 'none';
		document.getElementById('div_email').style.display = 'none';
		document.getElementById('div_comment').style.display = 'none';
		document.getElementById('div_towhom').style.display = 'none';
	}
}

function ValidateForm ( ) {
	hide_all();
	
		if(document.contact_form.to_whom.value == "") {
    	    show_hide('div_towhom');
        	document.contact_form.to_whom.focus();
        	return false;
		}
    	if ( document.contact_form.Cname.value == "" || document.contact_form.Cname.value==null)    {
    	    show_hide('div_name');
        	document.contact_form.Cname.focus();
        	return false;
        }	

        
        if (document.contact_form.ContactByEmail[0].checked)  {
    		if  (document.contact_form.submit_by.value == "" || document.contact_form.submit_by.value==null) {
	    		document.contact_form.submit_by.focus();
			  	show_hide('div_email');
        		return false;
			}
			if(echeck(document.contact_form.submit_by.value)==false){
				alert("Please enter a valid E-mail address.");
			    document.contact_form.submit_by.focus();
			  	show_hide('div_email');
				return false
			}
    	}
		else { 
			if(validatePhone()==false){
				return false;
			} 
		}
    	if ( document.contact_form.comment.value == "" || document.contact_form.comment.value==null)    {
    	    show_hide('div_comment');
        	document.contact_form.comment.focus();
        	return false;
		}
		return true;
}

	function echeck(str) {
	
			var at="@"
			var dot="."
			var lat=str.indexOf(at)
			var lstr=str.length
			var ldot=str.indexOf(dot)
			if (str.indexOf(at)==-1){return false
			}
	
			if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){return false
			}
	
			if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){return false
			}
	
			 if (str.indexOf(at,(lat+1))!=-1){return false
			 }
	
			 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){return false
			 }
	
			 if (str.indexOf(dot,(lat+2))==-1){return false
			 }
			
			 if (str.indexOf(" ")!=-1){return false
			 }	
	 		 return true					
		}



		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;
		}

function validatePhone(){
	var phonenum;
		if (document.contact_form.areacode.value == "" || document.contact_form.areacode.value == null)     {
    		show_hide('div_phone');
        	document.contact_form.areacode.focus();
        	return false;
    	}
		else {
			phonenum = '(' + document.contact_form.areacode.value + ') ';
		}
	
		if (document.contact_form.phnum1.value == "" || document.contact_form.phnum1.value == null)     {
    		show_hide('div_phone');
        	document.contact_form.phnum1.focus();
        	return false;
    	}
		else {
			phonenum = phonenum + document.contact_form.phnum1.value;
		}

    	if (document.contact_form.phnum2.value == "" || document.contact_form.phnum2.value == null)     {
    		show_hide('div_phone');
        	document.contact_form.phnum2.focus();
        	return false;
    	}
		else {
			phonenum = phonenum + '-' + document.contact_form.phnum2.value;
		}
        if (isPhoneNumber(phonenum)==false)   {
    		show_hide('div_phone');
        	document.contact_form.phnum1.focus();
    	    alert("Please enter a valid telephone number. (555) 555-5555");
        	return false;
    	}
return true;
}

function funcTest3(){
	if( document.contact_form.phnum2.value.length == 4 
		&& document.contact_form.phnum1.value.length + document.contact_form.phnum2.value.length == 7){
		document.contact_form.time.focus();
	}
}
function funcTest2(){
	if(document.contact_form.phnum1.value.length == 3){
		document.contact_form.phnum2.focus();
	}
}	
function funcTest1(){
	if(document.contact_form.areacode.value.length == 3){
		document.contact_form.phnum1.focus();
	}
}
