// JavaScript Document


function retailerSearch(l){
	var zipVal = document.getElementById("zipSearch").zipSearch.value;
	var valid = true;
	var country = "USA";
	if(zipVal.length == 5){
		for (var i=0; i<zipVal.length; i++){
			if(zipVal.charCodeAt(i) < 47 || zipVal.charCodeAt(i) > 57){
				country = "USA";
				valid = false;
				break;
			}
		}
	}else if(zipVal.length == 6){
		country = "Canada";
		zipVal = zipVal.substr(0,3)+" "+zipVal.substr(3,3);
	}else if(zipVal.length == 7){
		country = "Canada";
	}else{
		valid = false;
	}
	if(valid){
		if(l == "EN"){
			xmlHttp.open("GET", "/content/EN/searchResults.php?zip="+zipVal+"&country="+country, true);
		}else if(l == "FR"){
			xmlHttp.open("GET", "/content/FR/searchResults.php?zip="+zipVal+"&country="+country, true);
		}
		xmlHttp.onreadystatechange = searchResult;
		xmlHttp.send(null);
	}else{
		error("Please enter a valid zip code.");
	}
	
}
function searchResult() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.responseText.substr(0,5) == "ERROR"){
			var temp = xmlHttp.responseText.split(":");
			error(temp[1]);
		}else{
			$(document).ready(function() {
				$("#contentWrap").fadeOut(function() {		
					document.getElementById('resultsArea').innerHTML = xmlHttp.responseText;
				});
				$("#contentWrap").fadeIn();
			});	
		}
	}
}
var contactLang;
function validateComment(language){
	contactLang = language;
	var fNameVal = document.getElementById("firstName").value;
	var lNameVal = document.getElementById("lastName").value;
	var emailVal = document.getElementById("email").value;
	var addressVal = document.getElementById("address").value;
	var cityVal = document.getElementById("city").value;
	var stateVal = document.getElementById("state").value;
	var commentsVal = document.getElementById("comments").value;
	
	if(emailVal == ""){
		if(contactLang == "EN"){
			error("ERROR: Please enter a valid email address.");
		}else{
			error("ERREUR : Veuillez inscrire une adresse courriel.");
		}
	}else if(commentsVal == ""){
		if(contactLang == "EN"){
			error("ERROR: Please enter a comment.");
		}else{
			error("ERREUR : Veuillez inscrire vos remarques.");
		}
	}else{
		var query = "firstName="+escape(fNameVal);
		query += "&lastName="+escape(lNameVal);
		query += "&email="+escape(emailVal);
		query += "&address="+escape(addressVal);
		query += "&city="+escape(cityVal);
		query += "&state="+escape(stateVal);
		query += "&comments="+escape(commentsVal);
		
		xmlHttp.open("POST", "includes/sendContactMsg.php", true);
		xmlHttp.onreadystatechange = backToContact;
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length",query.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(query);
	}
}
function backToContact(){
	if(xmlHttp.readyState == 4) {
		if(contactLang == "EN"){
			error("Thank you. Your comment has been successfully sent.");
		}else{
			error("Merci d'avoir contact&#233; OneGoal. Nous attendons avec impatience de recevoir vos histoires et commentaires !");
		}
	}
	
}
function error(txt){
	document.getElementById("formError").innerHTML = txt;
}
