
//*************************************************************************************************************************
					//Ajax code for Model & Zip code

//***************************************************************************************************************************

function GetXmlHttpObject() 
	{
        var xmlHttp=null;
        try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
                }
        catch (e) {
		// Internet Explorer
        try {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
          
        	}
		catch (e) {
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

        }
        }
        return xmlHttp;
	}
	
	
	
	
function fetchModel(){
//

var url="fetchModel.do?make="+document.getElementById('carVO.make').value;

//alert(url);
writeHere1(url); 
 }
 
 
 
function  writeHere1(url) {
					 
                        xmlHttp=GetXmlHttpObject()
                        if (xmlHttp==null) {
                        alert ("Your browser does not support AJAX!");
                        return;
                        }
	    xmlHttp.onreadystatechange=openPage1;
	  
	  
        xmlHttp.open("GET",url,true);
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xmlHttp.send(null);
}

function openPage1() {

						var models = "<select name=\"carVO.model\" id=\"carVO.model\" style=\"width:135px\"class=\"textfield\"><option value=\"\">Loading ...</option>";
                       	document.getElementById("functionname").innerHTML = models;	
                        if (xmlHttp.readyState==4) {
                     
                        if (xmlHttp.status == 200) {
						
						var msg = xmlHttp.responseXML.getElementsByTagName("models")[0];
						
                 		var action="buy";
                 		var models = "<select name=\"carVO.model\" id=\"carVO.model\" style=\"width:135px\"class=\"textfield\"><option value=\"\">Select a Model</option>";	
                 		if(action=="buy") 
						{
							models = models + "<option value=\"ALL\">All "+document.getElementById('carVO.make').value+" Models</option>";
						}
                 		
						for(i=0; i<msg.childNodes.length/2; i++)
							{
				
								if(action=="sell") {
									models = models + "<option value=\""+msg.getElementsByTagName("model")[i].childNodes[0].nodeValue+"\">"+msg.getElementsByTagName("model")[i].childNodes[0].nodeValue+"</option>";
								}else {
									models = models + "<option value=\""+msg.getElementsByTagName("id")[i].childNodes[0].nodeValue+"\">"+msg.getElementsByTagName("model")[i].childNodes[0].nodeValue+"</option>";
								}
								
							}
							models += "</select>";
							
							document.getElementById("functionname").innerHTML = models;	



       }
     }
   }




//   Sell Car Models

function fetchSellModel(){

var url="fetchModel.do?make="+document.getElementById('carVO.sellmake').value;

	if(document.getElementById('carVO.sellmake').value!=''){
		writeHere(url); 
	}
 }
function  writeHere(url) {
					 
                        xmlHttp=GetXmlHttpObject()
                        if (xmlHttp==null) {
                        alert ("Your browser does not support AJAX!");
                        return;
                        }
	    xmlHttp.onreadystatechange=openPage;
	  
	  
        xmlHttp.open("GET",url,true);
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xmlHttp.send(null);
}

function openPage() {
						var models = "<select name=\"carVO.model\" id=\"carVO.sellmodel\" style=\"width:135px\"class=\"textfield\"><option value=\"\">Loading ...</option>";
						
                       	document.getElementById("sellcar").innerHTML = models;	
                        if (xmlHttp.readyState==4) {
                     		
                        if (xmlHttp.status == 200) {
						
						var msg = xmlHttp.responseXML.getElementsByTagName("models")[0];
						
                 		
                 		var models = "<select name=\"carVO.model\" id=\"carVO.sellmodel\" style=\"width:135px\"class=\"textfield\"><option value=\"\">Select a Model</option>";	
                 		
                 		
						for(i=0; i<msg.childNodes.length/2; i++)
							{
				
								
									models = models + "<option value=\""+msg.getElementsByTagName("model")[i].childNodes[0].nodeValue+"\">"+msg.getElementsByTagName("model")[i].childNodes[0].nodeValue+"</option>";
								
								
							}
							models += "</select>";
							
							document.getElementById("sellcar").innerHTML = models;	



       }
     }
   }

//*********************************************************************************************************************************************************************************************************************************

						// User Exist or not in sell_car.jsp
						
//**********************************************************************************************************************************************************************************************************************************

function isUserExist()
{
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
var email=document.getElementById('userVO.email').value;

var isValid=true;
	if(email==''){
		err="Please Enter Email \n";
		isValid=false;
	}else if(email.match(emailExp))
	{
		isValid=true;
	}else{
		err="Please Enter Valid Email \n";
		isValid=false;
	}
	if(isValid==false){
		alert(err);
		return false;
		}
var url="isUserExist.do?email="+document.getElementById('userVO.email').value;

writeHere2(url); 
}
function  writeHere2(url) {
					 
                        xmlHttp=GetXmlHttpObject()
                        if (xmlHttp==null) {
                        alert ("Your browser does not support AJAX!");
                        return;
                        }
	    xmlHttp.onreadystatechange=openPage2;
	  
	  
        xmlHttp.open("GET",url,true);
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xmlHttp.send(null);
}

function openPage2() {
			
                        if (xmlHttp.readyState==4) {
                     
                        if (xmlHttp.status == 200) {
						
						var msg = xmlHttp.responseXML.getElementsByTagName("user")[0];
						var existUser=msg.getElementsByTagName("mail")[0].childNodes[0].nodeValue;
						
						var size=msg.getElementsByTagName("size")[0].childNodes[0].nodeValue;
						
						oFormObject = document.forms[0]; 
						oFormElement = oFormObject.elements["carVO.car_id"].value=size; 
						//alert(oFormElement);
                 		if(existUser=="true")
                 		{
                 			document.getElementById("existuser").innerHTML="Id Already Exist"
                 			document.getElementById("existuser").style.color="red";
                 			
                 		}
                 		
						


       }
     }
   }
   
function getZipcode(){
var url="getZipcode.do?state="+document.getElementById('userVO.state').value;
alert(url);
writeHere3(url); 



    		
			
}

function  writeHere3(url) {
					 
                        xmlHttp=GetXmlHttpObject()
                        if (xmlHttp==null) {
                        alert ("Your browser does not support AJAX!");
                        return;
                        }
	    xmlHttp.onreadystatechange=openPage3;
	  
	  
        xmlHttp.open("GET",url,true);
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xmlHttp.send(null);
}

function openPage3() {
			
                        if (xmlHttp.readyState==4) {
                     	alert(xmlHttp.statusText);
                        if (xmlHttp.status == 200) {
						
						var msg = xmlHttp.responseXML.getElementsByTagName("zipcodes")[0];
						alert(msg.childNodes.length);
						var zipcode='';
						for(i=0; i<msg.childNodes.length; i++){
						 zipcode=zipcode+msg.getElementsByTagName("zip")[i].childNodes[0].nodeValue;
						}
									
						alert(zipcode);
							
						


       }
     }
   }
   
function imageUpload(type)
{

	fullName = type;
	shortName = fullName.match(/[^\/\\]+$/);
	splitName = fullName.split(".");
	fileType = splitName[1];
	fileType = fileType.toLowerCase();
	
}

function mapDetail()
{
 var map = null;
 var geocoder = null;
 var addr1=document.dealerForm.elements["dealerVO.address"].value;
 var addr2=document.dealerForm.elements["dealerVO.city"].value;
 var addr3=document.dealerForm.elements["dealerVO.state"].value;
 var address=addr1+','+addr2+','+addr3;

if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
      
        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        geocoder = new GClientGeocoder();

        showAddress(address)
      }
       function showAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              
            } else {
              map.setCenter(point, 13);
              var marker = new GMarker(point);
            
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
            }
          }
        );
      }
    }
      
}







