// 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 countryChange(value){
	if(value == 'US'){
		var usDiv = 'City, State, Zip Code<br /><input name="City.City" type="text" class="req text177 text" onfocus="clearInput(this)" onblur="setDefault(this, \'City\')" value="City"  /> \
								<select class="req select86" name="State.State" ><option value="" selected>- Select State -</option><option value="AL" >Alabama</option><option value="AK" >Alaska</option><option value="AR" >Arkansas</option><option value="AZ" >Arizona</option><option value="CA" >California</option><option value="CO" >Colorado</option><option value="CT" >Connecticut</option><option value="DC" >District of Columbia</option><option value="DE" >Delaware</option><option value="FL" >Florida</option><option value="GA" >Georgia</option><option value="IA" >Iowa</option><option value="ID" >Idaho</option><option value="IL" >Illinois</option><option value="IN" >Indiana</option><option value="KS" >Kansas</option><option value="KY" >Kentucky</option><option value="LA" >Louisiana</option><option value="MA" >Massachusetts</option><option value="MD" >Maryland</option><option value="ME" >Maine</option><option value="MI" >Michigan</option><option value="MN" >Minnesota</option><option value="MO" >Missouri</option><option value="MS" >Mississippi</option><option value="MT" >Montana</option><option value="NC" >North Carolina</option><option value="ND" >North Dakota</option><option value="NE" >Nebraska</option><option value="NH" >New Hampshire</option><option value="NJ" >New Jersey</option><option value="NM" >New Mexico</option><option value="NV" >Nevada</option><option value="NY" >New York</option><option value="OH" >Ohio</option><option value="OK" >Oklahoma</option><option value="OR" >Oregon</option><option value="PA" >Pennsylvania</option><option value="RI" >Rhode Island</option><option value="SC" >South Carolina</option><option value="SD" >South Dakota</option><option value="TN" >Tennessee</option><option value="TX" >Texas</option><option value="UT" >Utah</option><option value="VA" >Virginia</option><option value="VT" >Vermont</option><option value="WA" >Washington</option><option value="WI" >Wisconsin</option><option value="WV" >West Virginia</option><option value="WY" >Wyoming</option></select> \
								<input name="Zip Code.Zip Code" type="text" class="req text111 text" onfocus="clearInput(this)" onblur="setDefault(this, \'Zip Code\')" value="Zip Code"  />';
								
		jQuery("#usDiv").html(usDiv);
		jQuery("#usDiv").fadeIn("slow");
		jQuery("#canadaDiv").fadeOut("slow", function (){ jQuery("#canadaDiv").html(''); });
		jQuery("#otherCountry").fadeOut("slow", function (){ jQuery("#otherCountry").html(''); });
		
	}else if(value == 'CA'){
		var canadaDiv = 'City, Provinces, Postal Code<br /><input name="City.City" type="text" class="req text177 text" onfocus="clearInput(this)" onblur="setDefault(this, \'City\')" value="City"  /> \
								<input name="State.State" type="text" class="req text86 text" value="Province"  onfocus="clearInput(this)" onblur="setDefault(this, \'Province\')" /> \
								<input name="Zip Code.Zip Code" type="text" class="req text111 text" value="Postal Code"  onfocus="clearInput(this)" onblur="setDefault(this, \'Postal Code\')" />';
		
		jQuery("#canadaDiv").html(canadaDiv);
		jQuery("#canadaDiv").fadeIn("slow");
		jQuery("#usDiv").fadeOut("slow", function (){ jQuery("#usDiv").html(''); });
		jQuery("#otherCountry").fadeOut("slow", function (){ jQuery("#otherCountry").html(''); });
	}else if(value == 'Other'){
		var otherCountry = 'Country Code<br /><input name="Zip Code.Zip Code" type="text" class="req text195 text" value="Country Code"  onfocus="clearInput(this)" onblur="setDefault(this, \'Country Code\')" />';
		
		jQuery("#otherCountry").html(otherCountry);
		jQuery("#otherCountry").fadeIn("slow");
		jQuery("#canadaDiv").fadeOut("slow", function (){ jQuery("#canadaDiv").html(''); });
		jQuery("#usDiv").fadeOut("slow", function (){ jQuery("#usDiv").html(''); });
	}else{
		jQuery("#canadaDiv").fadeOut("slow", function (){ jQuery("#canadaDiv").html(''); });
		jQuery("#usDiv").fadeOut("slow", function (){ jQuery("#usDiv").html(''); });
		jQuery("#otherCountry").fadeOut("slow", function (){ jQuery("#otherCountry").html(''); });
	}
	
	if(location.pathname == '/contact-us/'){
		jQuery('input[name$="getInfo"]:not(:checked)').each( function (){
			jQuery('#address_fields input').each( function (){
				jQuery(this).removeClass('req');
			});
			jQuery('#address_fields select').each( function (){
				jQuery(this).removeClass('req');
			});
		});
	}
	
}

function countryChangeContact(value){
	if(value == 'US'){
		document.getElementById('changeLabel').innerHTML = '&amp; Zip Code';
		document.getElementById('changeField').innerHTML = '<input name="Zip Code.Zip Code" type="text" class="req 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 name="Zip Code.Zip Code" type="text" class="req 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 name="countrycode" type="text" class="req 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':
		case 'Address / P.O. Box':
		case 'Number of Attendees':
		case 'Best Day and Time':
			input.value = '';
			input.style.color = 'black';
			input.style.fontStyle = 'normal';
			break;
	}
}

function defaults(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':
		case 'Address / P.O. Box':
		case 'Number of Attendees':
		case 'Best Day and Time':
			return true;
			break;
	}
	return false;
}

function checkform(){
	var passedCheck = true;
	jQuery('.req').each( function (){
		if((jQuery(this).val() == '' || defaults(jQuery(this).val())) && jQuery(this).parent().css('display') != 'none'){
			jQuery(this).css('color', 'red');
			passedCheck = false;
		}else{
			jQuery(this).css('background-color','white');
		}
	});
	
	return passedCheck;
}

function showAddress(checked){
	if(checked){
		jQuery('#address_fields').fadeIn("slow");
		if(jQuery('select.select183').val() == 'US'){
			jQuery("#usDiv").fadeIn("slow");
			jQuery("#canadaDiv").fadeOut("slow");
			jQuery("#otherCountry").fadeOut("slow");
		}else if(jQuery('select.select183').val() == 'CA'){
			jQuery("#canadaDiv").fadeIn("slow")
			jQuery("#usDiv").fadeOut("slow");
			jQuery("#otherCountry").fadeOut("slow");
		}else if(jQuery('select.select183').val() == 'Other'){
			jQuery("#otherCountry").fadeIn("slow");
			jQuery("#canadaDiv").fadeOut("slow");
			jQuery("#usDiv").fadeOut("slow");
		}
		jQuery('#address_fields input').each( function (){
			jQuery(this).addClass('req');
		});
		jQuery('#address_fields select').each( function (){
			jQuery(this).addClass('req');
		});
	}else{
		jQuery('#address_fields').hide();
		jQuery('#address_fields input').each( function (){
			jQuery(this).removeClass('req');
		});
		jQuery('#address_fields select').each( function (){
			jQuery(this).removeClass('req');
		});
	}
}

function submitform(){
	jQuery('form.listrak').submit( function (){
		if(checkform()){
			
			jQuery('form input').each( function (){
				if(defaults(jQuery(this).val())){
					jQuery(this).val('');
				}
			})
			
			var data = jQuery('form').serialize();
			var url = jQuery('form').attr('action');
			
			xmlhttp.open("GET", "/formhandler.php?" + data + '&url=' + url);
			xmlhttp.onreadystatechange = function(){
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
					window.location = jQuery('#thank-you-page').val();
					return false;
				}
			}
			xmlhttp.send(null);
		}		
		return false;
	});
}

jQuery('document').ready( function (){
	submitform();
});


