﻿
/////////////////////////////////////  TO POPULATE MODEL DROP DOWN LIST    ////////////////////////////   
 
    function PopulateModel(obj)
    {
        var sMake = obj.value;
        var objModel = document.getElementById("ddlModel");
        
        if(sMake == null || sMake == "" || sMake == "All")
        {            
            //clear the ddl
            objModel.options.length = 0;
        
            //put the first value
            objModel.options[0] = new Option("Show All", "All");
            return;
        }
        
        sMake = ReplaceSpecialChar(sMake);        
        
        // get the array as the convention is 'ArrayMake'
        var arr = eval("Array" + sMake);        
        
        //clear the ddl
        objModel.options.length = 0;
        
        //put the first value
        objModel.options[0] = new Option("Show All", "All");
        
        for(var i=0; i<arr.length; i++)
        {
            objModel.options[i+1] = new Option(arr[i], arr[i]);
        }
    }
    
    
   function PopulateModels(obj, ddlModelID)
    { 
        var sMake = obj.value;
        var objModel = document.getElementById(ddlModelID);
        
        if(sMake == null || sMake == "" || sMake == "All")
        {            
            //clear the ddl
            objModel.options.length = 0;
        
            //put the first value
            objModel.options[0] = new Option("Show All", "All");
            return;
        }
        
        sMake = ReplaceSpecialChar(sMake);        
        
        // get the array as the convention is 'ArrayMake'
        var arr = eval("Array" + sMake);        
        
        //clear the ddl
        objModel.options.length = 0;
        
        //put the first value
        objModel.options[0] = new Option("Show All", "All");
        
        for(var i=0; i<arr.length; i++)
        {
            objModel.options[i+1] = new Option(arr[i], arr[i]);
        }
    }
    
    
    function ReplaceSpecialChar(str)
    {
        var index = str.indexOf(" ");
        
        while(index !=  -1)
        {
            str = str.replace(" ", "_");
            index = str.indexOf(" ");
        }
        
        index = str.indexOf("-");
        
        while(index !=  -1)
        {
            str = str.replace("-", "Dasshh");
            index = str.indexOf("-");
        }
        
        return str;
    }

/////////////////////////   VALIDATE ZIP CODE   /////////////////////////

function alertContents_(http_request, urls, exts) 
{
	if (http_request.readyState == 4) 
	{
		if (http_request.status == 200) 
		{
				if(http_request.responseText=="false")
				{				    
				    alert("INVALID ZIPCODE!");
				    return false;		    				    
				}
				else
				{
				    var oMk =document.getElementById("ddlMake");
				    var oMdl =document.getElementById("ddlModel");
				    var oZip =document.getElementById("zipCode");
				    var oPrc =document.getElementById("ddlPrice");
				    var oDis =document.getElementById("ddlDistance");



				    urls = urls + exts + "/result.aspx?";
				    urls += "ddlMake=" + oMk.value;
				    urls += "&ddlModel=" + oMdl.value.replace(".",".Dot.").replace("/",".Or.").replace(" ",".").replace("&", "And");
				    				    
				    urls += "&ddlDistance=" + oDis.value;
				    urls += "&ddlPrice=" + oPrc.value;
				    urls += "&zipCode=" + oZip.value;
				    
				    window.location = urls;
				    
				    return true;				       
				}
		}
	} 
}

//////////////////////////////////////////////////////////////////////////

function ValidateZIPCode(browser, url, ext) 
{
	     var http_request = false;	
	    if (window.XMLHttpRequest) 
	    {
		    http_request = new XMLHttpRequest(); 
		    if (http_request.overrideMimeType) 
		    { 
			    http_request.overrideMimeType('text/xml'); 		
		    } 
    		
	    } 
	    else if (window.ActiveXObject) 
	    { 
		    // IE 
	        try 
	        { 
		        http_request = new ActiveXObject("Msxml2.XMLHTTP");
	        } 
	        catch (e) 
	        { 
	            try 
	            { 
		            http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
	            }
	            catch (e) 
	            {} 
	        } 
	    } 
	    
		if (!http_request) 
		{ 			
		}
		
		var zipCode = document.getElementById('zipCode').value;	
		var zip=null;
		if(zipCode == "" || zipCode == null)
		{
		    alert("Please enter zip code.");
		    return false;
		}   
		if(zipCode.length>0)
		    zip = zipCode; 
        
		var urls = url + "Ajax/Server.aspx?request=validatezip&zipcode="+zip;
				
		if(browser == "IE")
		    http_request.open('POST', urls, false);
		else
		    http_request.open('GET', urls, false);
		    
		http_request.send(null); 
		return alertContents_(http_request, url, ext);
}

//////////////////////////////////////////////////

// this is called at the zipcode text box
function OnKeyEvent(e, browser, url, ext)
{
    if(!e)
        e = window.event || event;
        
    if(e.keyCode == 13)
    {
        return ValidateZIPCode(browser, url, ext);
    }
    return true;
}




function ValidateZIP(browser,zipcodeID,url) 
{

  var zip = document.getElementById(zipcodeID);
  if(zip)
  {
  
    var zipCode = zip.value;
   
    
    if(zipCode == "" || zipCode == null)
		{
		    alert("Please enter zip code.");
		    return false;
		}   
	


	    var http_request = false;	
	    if (window.XMLHttpRequest) 
	    {
		    http_request = new XMLHttpRequest(); 
		    if (http_request.overrideMimeType) 
		    { 
			    http_request.overrideMimeType('text/xml'); 		
		    } 
    		
	    } 
	    else if (window.ActiveXObject) 
	    { 
		    // IE 
	        try 
	        { 
		        http_request = new ActiveXObject("Msxml2.XMLHTTP");
	        } 
	        catch (e) 
	        { 
	            try 
	            { 
		            http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
	            }
	            catch (e) 
	            {} 
	        } 
	    } 
	    
		if (!http_request) 
		{ 			
		}
		
		
		
		var urls = url + "Ajax/Server.aspx?request=validatezip&zipcode="+zipCode;
				
		if(browser == "IE")
		    http_request.open('POST', urls, false);
		else
		    http_request.open('GET', urls, false);
		    
		http_request.send(null); 
		
	 if (http_request.readyState == 4) 
	  {
		if (http_request.status == 200) 
		{
				if(http_request.responseText=="false")
				{				    
				    alert("INVALID ZIPCODE!");
				    return false;		    				    
				}
				else
				{
				   return true;
				}
		}
	 }
		
  }
		
		
	
}
