// 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(){
	$('div.back2top a').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('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'});
	}

	return passedCheck; }

function sendcontact(){	
	if(checkContact()){
		var querystring = $('form').serialize(); //alert(querystring);
		xmlhttp.open("GET", "/ajaxHandler.php?action=contact&" + querystring + "&time=" + new  Date().getTime());
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				var response = xmlhttp.responseText; //alert(response);
				document.getElementById('contact').innerHTML = '<p>Thank you! You\'ve just taken the first step toward an energy-efficient, environmentally friendly new roof for your home or commercial building. We appreciate your interest in Ontario Shake N\' Tile, and look forward to corresponding with you. One of our roofing professionals will contact you soon.</p>';
			}
		}
		xmlhttp.send(null);
	}

	return false; }

function checkEstimate(){
	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('hearAbout').value == ''){
		passedCheck = false;
		$('#hearAbout').parent(0).css({'color':'red','font-style':'italic','font-weight':'bold'});
	}else{
		$('#hearAbout').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'});
	}*/

	return passedCheck; }

function sendestimate(){	
	if(checkEstimate()){
		var querystring = $('form').serialize(); //alert(querystring);
		xmlhttp.open("GET", "/ajaxHandler.php?action=estimate&" + querystring + "&time=" + new  Date().getTime());
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				var response = xmlhttp.responseText; //alert(response);
				document.getElementById('contact').innerHTML = '<p>You\'ve contacted the right company in Ontario for serving and servicing your roofing needs! Your request for a roofing estimate from Ontario Shake N\' Tile has been submitted and you can look forward to good, fair offer. One of our roofing professionals will contact you soon. Thank you.</p>';
			}
		}
		xmlhttp.send(null);
	}

	return false; }

function checkLeadgen(){
	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('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'});
	}

	return passedCheck; }

function sendleadgen(){	
	if(checkLeadgen()){
		var querystring = $('form').serialize(); //alert(querystring);
		xmlhttp.open("GET", "/ajaxHandler.php?action=leadgen&" + querystring + "&time=" + new  Date().getTime());
		xmlhttp.onreadystatechange = function(){
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
				var response = xmlhttp.responseText; //alert(response);
				window.location = '/free-download/thank-you/';
			}
		}
		xmlhttp.send(null);
	}

	return false; }

function h1Click(){
	$("h1").click(function () { 
		window.location = "/";
	});
}

$(document).ready(function() {
	h1Click();
	backToTop();
});