// JavaScript Document

var theTimer, flashCounter;

// AJAX INIT STUFF -- START
var xmlhttp=null;

try
{
	xmlhttp = new XMLHttpRequest();
}
catch (error)
{
	try
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (error)
	{
		xmlhttp = null;
	}
}
//  END

function echeck(str){
 var filter=/^.+@.+\..{2,4}$/

 if (filter.test(str))
    result=true
 else {
    result=false
	}
 return (result) }

function trim(str){
	var	str = str.replace(/^\s\s*/, ''),
	ws = /\s/,
	i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

function clearValue(defaultValue, input){ //alert(defaultValue + '==' + input.value);
	if(defaultValue == input.value) input.value = '';
	document.getElementById(input.id).style.color = 'black';
}

function setDefault(defaultValue, input){
	var currentValue = trim(input.value);
	if(currentValue == '') input.value = defaultValue;
}

function backToTop(){
	$('a.back2top').click(function(){
		$('html, body').animate({
			scrollTop: $("#header").offset().top
		}, 650);
		return false;
	});
}

function checkContact(){
	var passedCheck = true;
	var hasChecked = false;
	
	if(document.getElementById('name').value == ''){
		passedCheck = false;
		$('#name').parent(0).css({'color':'red','font-style':'italic','font-weight':'bold'});
	}else{
		$('#name').parent(0).css({'color':'black','font-style':'normal','font-weight':'normal'});
	}
	
	if(!echeck(document.getElementById('email').value)){
		passedCheck = false;
		$('#email').parent(0).css({'color':'red','font-style':'italic','font-weight':'bold'});
	}else{
		$('#email').parent(0).css({'color':'black','font-style':'normal','font-weight':'normal'});
	}
	
	if(document.getElementById('street').value == ''){
		passedCheck = false;
		$('#street').parent(0).css({'color':'red','font-style':'italic','font-weight':'bold'});
	}else{
		$('#street').parent(0).css({'color':'black','font-style':'normal','font-weight':'normal'});
	}
	
	if(document.getElementById('city').value == ''){
		passedCheck = false;
		$('#city').parent(0).css({'color':'red','font-style':'italic','font-weight':'bold'});
	}else{
		$('#city').parent(0).css({'color':'black','font-style':'normal','font-weight':'normal'});
	}
	
	if(document.getElementById('state').value == ''){
		passedCheck = false;
		$('#state').parent(0).css({'color':'red','font-style':'italic','font-weight':'bold'});
	}else{
		$('#state').parent(0).css({'color':'black','font-style':'normal','font-weight':'normal'});
	}
	
	if(document.getElementById('zipcode').value == ''){
		passedCheck = false;
		$('#zipcode').parent(0).css({'color':'red','font-style':'italic','font-weight':'bold'});
	}else{
		$('#zipcode').parent(0).css({'color':'black','font-style':'normal','font-weight':'normal'});
	}
	
	if(document.getElementById('phone').value == ''){
		passedCheck = false;
		$('#phone').parent(0).css({'color':'red','font-style':'italic','font-weight':'bold'});
	}else{
		$('#phone').parent(0).css({'color':'black','font-style':'normal','font-weight':'normal'});
	}
	
	if(document.getElementById('comments').value == ''){
		passedCheck = false;
		$('#comments').parent(0).css({'color':'red','font-style':'italic','font-weight':'bold'});
	}else{
		$('#comments').parent(0).css({'color':'black','font-style':'normal','font-weight':'normal'});
	}
	
	$('input:checked').each( function (){
		hasChecked = true;
	});

	if(hasChecked == false){
		passedCheck = false;
		$('#services').parent(0).css({'color':'red','font-style':'italic','font-weight':'bold'});
	}else{
		$('#services').parent(0).css({'color':'black','font-style':'normal','font-weight':'normal'});
	}
	
	return passedCheck; }

function sendcontact(){	
	if(checkContact()){
		var querystring = $('form').serialize(); //alert(querystring);
		xmlhttp.open("GET", "ajaxHandler.php?action=send&" + querystring + "&time=" + new  Date().getTime());
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				var response = xmlhttp.responseText; //alert(response);
				document.getElementById('form-text').innerHTML = '<p>You\'ve just taken the first step toward worry-free, residential and commercial  landscape maintenance. Thanks for contacting Dave Brown Landscape Group. We appreciate your request, and promise to respond quickly.</p>';
			}
		}
		xmlhttp.send(null);
	}
	
	return false; }


function h1Click(){
	$("h1").css('cursor','pointer');
	$("h1").click(function () { 
		window.location = "index.php";
	});
}

$(document).ready( function(){
	h1Click();
});
