// JavaScript Document

// 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 checkForm(){
	var passedCheck = true;
	var country = document.getElementById('country').value;
	var firstName = document.getElementById('firstName').value;
	var lastName = document.getElementById('lastName').value;
	var email = document.getElementById('email').value;
	if(country == 'US' || country == 'CA'){
		var zipcode = document.getElementById('zipcode').value;
	}
	if(country == 'Other'){
		var countrycode = document.getElementById('countrycode').value;
	}
	
	if(email == '' || email == 'Email Address'){
		passedCheck = false;
		document.getElementById('email').style.color = 'red';
	}else{
		document.getElementById('email').style.color = 'black';
		document.getElementById('email').style.fontStyle = 'normal';
	}
	
	if(firstName == '' || firstName == 'First Name'){
		passedCheck = false;
		document.getElementById('firstName').style.color = 'red';
	}else{
		document.getElementById('firstName').style.color = 'black';
		document.getElementById('firstName').style.fontStyle = 'normal';
	}
	
	if(lastName == '' || lastName == 'Last Name'){
		passedCheck = false;
		document.getElementById('lastName').style.color = 'red';
	}else{
		document.getElementById('lastName').style.color = 'black';
		document.getElementById('lastName').style.fontStyle = 'normal';
	}
	
	if(country == ''){
		passedCheck = false;
		document.getElementById('country').style.color = 'red';
	}else{
		document.getElementById('country').style.color = 'black';
		document.getElementById('country').style.fontStyle = 'normal';
	}
	
	if(country == 'US'){
		if(zipcode == '' || zipcode == 'Zip Code'){
			passedCheck = false;
			document.getElementById('zipcode').style.color = 'red';
		}
	}else if(country == 'CA'){
		if(zipcode == '' || zipcode == 'Postal Code'){
			passedCheck = false;
			document.getElementById('zipcode').style.color = 'red';
		}
	}else if(country == 'Other'){
		if(countrycode == '' || countrycode == 'Country Code'){
			passedCheck = false;
			document.getElementById('countrycode').style.color = 'red';
		}
	}
	
	if(!passedCheck){
		return false;
	}else{
		sendForm();
	}
}

function sendForm(){ //alert('send');
	var zipcode, countrycode;
	var country = document.getElementById('country').value;
	var firstName = document.getElementById('firstName').value;
	var lastName = document.getElementById('lastName').value;
	var email = document.getElementById('email').value;
	if(country == 'US' || country == 'CA'){
		zipcode = document.getElementById('zipcode').value;
	}
	if(country == 'Other'){
		countrycode = document.getElementById('countrycode').value;
	}
	
	document.getElementById('formForm').innerHTML = '<div class="newform"><div class="innerpadding"><p style="text-align:center; padding-top:240px;"><br/><br/><em><strong>Sending Email...</strong></em><br/><img src="images/loading.gif" alt="sending email" width="220" height="19" /></p></div></div>';
	
	xmlhttp.open("GET", "ajaxHandler.php?action=leadGeneration&country=" + country + "&firstName=" + firstName + "&lastName=" + lastName + "&email=" + email + "&zipcode=" + zipcode + "&countrycode=" + countrycode);
	xmlhttp.onreadystatechange = function(){
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
			response = xmlhttp.responseText;
			//alert(response);
			document.getElementById('formForm').innerHTML = '<div class="newform"><div class="innerpadding"><p><a href="http://www.steel-roofing.com/info/FiveTelltaleSigns.pdf" class="downloadReport" target="_blank">Click here to download report.</a></p><p>We\'re  glad you decided to download our "Five Signs That It Could Be Time To  Replace Your Roof" report. Simply click the link above to begin your  download.</p><div style="padding:0px;"><p><strong>IMPORTANT:</strong> There is a chance our response to you will be discarded by your spam  filter. You should receive an acknowledgment email in the next 15  minutes.</p><p>Please check your inbox for our auto-acknowledgment email, which has just been sent to the email address you specified. </p><div>If  you do not see the email, look in your junk-mail folder. If you find it  there, please mark the email as Not Junk and add @kasselandirons.com to  your address book.</div></div></div><span class="roundBottom">&nbsp;</span></div>';
			//setTimeout("closeform()",2500);
		}
	}
	xmlhttp.send(null);
}

function countryChange(value){
	if(value == 'US'){
		$("#usDiv").fadeIn("slow");
		$("#canadaDiv").fadeOut("slow");
		$("#otherCountry").fadeOut("slow");
	}else if(value == 'CA'){
		$("#canadaDiv").fadeIn("slow");
		$("#usDiv").fadeOut("slow");
		$("#otherCountry").fadeOut("slow");
	}else if(value == 'Other'){
		$("#otherCountry").fadeIn("slow");
		$("#canadaDiv").fadeOut("slow");
		$("#usDiv").fadeOut("slow");
	}else{
		$("#canadaDiv").fadeOut("slow");
		$("#usDiv").fadeOut("slow");
		$("#otherCountry").fadeOut("slow");
	}
}

function countryChangeContact(value){
	if(value == 'US'){
		document.getElementById('changeLabel').innerHTML = '&amp; Zip Code';
		document.getElementById('changeField').innerHTML = '<input id="zipcode" name="zipcode" type="text" class="text195 text" value="Zip Code" onfocus="clearInput(this)" onblur="setDefault(this, \'Zip Code\')" />';
	}else if(value == 'CA'){
		document.getElementById('changeLabel').innerHTML = '&amp; Postal Code';
		document.getElementById('changeField').innerHTML = '<input id="zipcode" name="zipcode" type="text" class="text195 text" value="Postal Code" onfocus="clearInput(this)" onblur="setDefault(this, \'Postal Code\')" />';
	}else if(value == 'Other'){
		document.getElementById('changeLabel').innerHTML = '&amp; Country Code';
		document.getElementById('changeField').innerHTML = '<input id="countrycode" name="countrycode" type="text" class="text195 text" value="Country Code" onfocus="clearInput(this)" onblur="setDefault(this, \'Country Code\')" />';
	}else{
		document.getElementById('changeLabel').innerHTML = '';
		document.getElementById('changeField').innerHTML = '';
	}
}

function setDefault(input, defaultValue){
	if(input.value == ''){
		input.value = defaultValue;
		input.style.color = '#999999';
		input.style.fontStyle = 'italic';
	}
}

function clearInput(input){ //alert(input.value);
	var value = input.value;
	switch(value){
		case 'First Name':
		case 'Last Name':
		case 'City':
		case 'Province':
		case 'Postal Code':
		case 'Zip Code':
		case 'Country Code':
		case 'Address':
		case 'Phone Number':
		case 'Email Address':
		case 'Other Country':
			input.value = '';
			input.style.color = 'black';
			input.style.fontStyle = 'normal';
			break;
	}
}


