function postalFunction(fieldName){
	var firstPostalChar = document.getElementById(fieldName).value;
	document.getElementById(fieldName).value = firstPostalChar.toUpperCase();
	if (fieldName == "postalF3"){
	if (firstPostalChar.length ==3){
		//postalCodeF3(); //Canceled this function, bug with IE if call back function has no time to fire before the next one.
		document.getElementById("postalL3").focus();
	}}
	if (fieldName == "postalL3"){
	if (document.getElementById("postalF3").value.length == 3 && document.getElementById("postalL3").value.length == 3){
		postalCodeL3();
	}}
}




function showLocation() {
      $("#locationBox").slideToggle("slow");
    }

function changeLocation() {
      $("#locationSpan").height(0);
      $("#postalBox").height(50);
    }

function ipLocate(){
	if (document.getElementById("locationName").innerHTML == ""){
		document.getElementById("locationName").innerHTML = "Locating...";
		geoIp();
	}
}

function geoIp(){
  /*$("#locationBox").slideDown("slow");*/
  var myurl = './module/geoIp/ListingGeoIp.php';
  http.open("GET", myurl);
  http.onreadystatechange = postalCodeL3Response;
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.send("");
}

function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
  try {
  req = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (err2) {
    try {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (err3) {
      req = false;
    }
  }
}
return req;
}

var http = getXMLHTTPRequest();

function postalCodeL3() {      
	var myurl = './module/ListingPostalCodeAjax.php';
  http.open("POST", myurl);
  http.onreadystatechange = postalCodeL3Response;
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.send("postalL6="+document.getElementById("postalF3").value+" "+document.getElementById("postalL3").value);
}

function postalCodeL3Response() {
   if (http.readyState == 4) {
    if(http.status == 200) {
       	var location = http.responseText.split("|");
       	document.getElementById("locationName").innerHTML = location[1] +", " +location[0];
       	document.getElementById("lN").value = location[1] +", " +location[0];
       	document.getElementById("lL").value = location[2];
       	document.getElementById("lG").value = location[3];
       	mapTarget();
    }
  }
}

function model() {
  var myurl = './module/ListingCarListAjax.php';
  http.open("POST", myurl);
  http.onreadystatechange = modelResponse;
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.send("param1="+document.getElementById("mS").options[document.getElementById("mS").selectedIndex].value);
}

function modelResponse() {
   if (http.readyState == 4) {
    if(http.status == 200) {
       	var myElements = http.responseXML.getElementsByTagName("nf");     
		document.getElementById("tS").options.length = 0;	
		document.getElementById("tS").options[0] = new Option("Tous","0");
	   	for (var i=0; i<myElements.length; i++){
			document.getElementById("tS").options[i+1] = new Option(
			myElements[i].childNodes[0].childNodes[0].nodeValue+ " (" +
			myElements[i].childNodes[1].childNodes[0].nodeValue+")",
			myElements[i].childNodes[0].childNodes[0].nodeValue);
		}
		
    }
  }
}

//START NEW FUNCTIONS FOR WISHLIST MAKE/MODELS by Alex - Changes made on 09-03-2010
var tempWLId; 
var maxWLId = 1; 
//Function to show the model list - receives the Id of the select to populate with the models
function modelWL(elemId) { 
  tempId = elemId.split("_")[1];
  var myurl = './module/wishListGetModel.php';
  http.open("POST", myurl);
  http.onreadystatechange = modelResponseWL;
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.send("param1="+document.getElementById(elemId).options[document.getElementById(elemId).selectedIndex].value);
}
function modelResponseWL() {//response to modelWL ajax call
   if (http.readyState == 4) {
    if(http.status == 200) {
       	var myElements = http.responseXML.getElementsByTagName("nf");     
		document.getElementById("WLmodel_"+tempId).options.length = 0;	
		document.getElementById("WLmodel_"+tempId).options[0] = new Option("Tous","0");
	   	for (var i=0; i<myElements.length; i++){
			document.getElementById("WLmodel_"+tempId).options[i+1] = new Option(
			myElements[i].childNodes[0].childNodes[0].nodeValue,
			myElements[i].childNodes[0].childNodes[0].nodeValue);
		}
		
    }
  }
}
function doAddCarWL(){//add a car make/model pair
	var myurl = './module/wishListAddCar.php';
	http.open("POST", myurl);
	http.onreadystatechange = doAddCarWLResponse;
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.send("param1="+maxWLId);
	maxWLId++;
}
function doAddCarWLResponse(){//response to doAddCarWL ajax call
	if (http.readyState == 4) {
    	if(http.status == 200) {
       		document.getElementById('WLcarListDiv_0').innerHTML += http.responseText;
		}
	}
}
//END NEW FUNCTIONS FOR WISHLIST MAKE/MODELS by Alex


// Adding new function for Location Map on 9/23/2008
function mapTarget(){
		var provinceName = document.getElementById("lN").value;
		if (provinceName.match(", Qc") || provinceName.match(", On") || provinceName.match(", ON") || provinceName.match(", QC")){
		var lat = document.getElementById("lL").value;
		var longi = document.getElementById("lG").value;
		var latDiff = lat - 45.81247;
		var longDiff = longi - (-73.7402);
		var pixMoveLat = latDiff*(-5.88628);
		var pixMoveLong = longDiff*(2.410071);
		var pixPosiLat = Math.round(75 + pixMoveLat);
		var pixPosiLong = Math.round(82 + pixMoveLong);
		var targetStyle = "position:absolute;left:"+pixPosiLong+"px;top:"+pixPosiLat+"px";
		var mapPosition = "<img style=\""+targetStyle+"\" src=\"images/ListingTarget.png\">";
		document.getElementById('mapBox').innerHTML = document.getElementById('mapBox').innerHTML + mapPosition;
		}		  	
}


// Adding new function for SendNewsMail on 9/29/2008



function sendNewsMail() {      
	var myurl = './module/ListingSendNewsMailAjax.php';
  http.open("POST", myurl);
  http.onreadystatechange = sendNewsMailResponse;
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.send(
  "pmtMin=0&"+
  "pmtMax="+document.getElementById("budgetValue").value+"&"+
  "mntMin=0&"+
  "mntMan=0&"+
  "emlVal="+document.getElementById("sendMailEmail").value+"&"+
  "carList="+document.getElementById("carWantList").value+"&"+  
  "geoLat="+document.getElementById("lL").value+"&"+  
  "geoLong="+document.getElementById("lG").value);
  $.cookie('mail', $("#sendMailEmail").val(), { expires: 365 });
}

//New sendNewsMail function - modified on 09/03/2010 by Alex
function WLsendNewsMail() {      
	var myurl = './module/ListingSendNewsMailAjax.php';
	var carList = '';
	for(var i=0; i<maxWLId; i++){
		carList += document.getElementById("WLmake_"+i).value+':'+document.getElementById("WLmodel_"+i).value+','
	}
  http.open("POST", myurl);
  http.onreadystatechange = sendNewsMailResponse;
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.send(
  "pmtMin=0&"+
  "pmtMax="+document.getElementById("budgetValue").value+"&"+
  "mntMin="+document.getElementById("MRMin").value+"&"+
  "mntMan="+document.getElementById("MRMax").value+"&"+
  "emlVal="+document.getElementById("sendMailEmail").value+"&"+
  "carList="+carList+"&"+  
  "geoLat="+document.getElementById("lL").value+"&"+  
  "geoLong="+document.getElementById("lG").value);
  $.cookie('mail', $("#sendMailEmail").val(), { expires: 365 });
}


function sendFeedBack() {
	var myurl = './module/ListingSendFeedBackAjax.php';
  http.open("POST", myurl);
  http.onreadystatechange = sendFeedBackResponse;
  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.send("feedback="+document.getElementById("feedBackBox").value);
}

function sendNewsMailResponse() {
   if (http.readyState == 4) {
    if(http.status == 200) {
       	var response = http.responseText;
       	if (response == "0"){
    	document.getElementById("sendMailEmail").value = "Ce courriel est invalide";}
    	else if (response == "1"){
    	document.getElementById("sendMailButton").value = "Excellent, vous êtes maintenant sur la liste d'envois!";
		document.getElementById("sendMailButton").disabled = true;
		}
		else if (response == "2"){
    	document.getElementById("sendMailButton").value = "Vos modifications ont été apportées";
		document.getElementById("sendMailButton").disabled = true;
		}	
    	}
	}
  }

function sendFeedBackResponse() {
   if (http.readyState == 4) {
    if(http.status == 200) {
    	document.getElementById("sendFeedBackButton").value = "Vos commentaires ont été envoyés! Merci, ces derniers sont très importants pour nous";
		document.getElementById("sendFeedBackButton").disabled = true;
		}
    	}
	}

