function isValidZipCode(value) {
   var re = /^\d{5}([\-]\d{4})?$/;
   return (re.test(value));
}

function SearchDealer(zip,category)
{
	if (zip!='Zip Code' && zip!='Zip Code Required' &&isValidZipCode(zip)  )
	{
		var lang=getLanguage();
	        Redireccionar('/'+lang+'/home/DealerSearch.aspx?search='+category+'&Location='+zip);
	}
	else
	{
		var t=document.getElementById("ZipCode");
		t.value="Zip Code Required";
	}
}

/*
taked from http://peter.michaux.ca/articles/the-window-onload-problem-still
*/
function init() {
    // quit if this function has already been called
    if (arguments.callee.done) return;
    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;
    // kill the timer
    if (_timer) clearInterval(_timer);
/*do stuff*/
createDynamicDropdown('selectCategory', 'HideSelect', 'SelectUse');
initleftMenu();
//initTip();
ReSizeProductImage();
ReSizeFeaturedImage();
removeDuplicates();
if (window.location.pathname=="/en/home/DealerSearch.aspx")
{
initializeMap();
}
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            init(); // call the onload handler
        }
    };
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            init(); // call the onload handler
        }
    }, 10);
}

/* for other browsers */
window.onload = init;

function removeDuplicates()
{
	var dropDown1 = document.getElementById('selectDealerCategory');
	var total=dropDown1.length;
	for(var i = 0; i < total; i++)
	{
	 	for(var j = i+1; j < total; j++)
		{
			//if (i!=j)
			//{
				if (dropDown1[i] && dropDown1[j])
				{
					if (dropDown1[i].value==dropDown1[j].value)
					{
						dropDown1[j]=null;
						j--;
						total--;
					}
				}
			//}
		}
	}
}

function createDynamicDropdown(dropDown1, dropDown2, dropDown3) {
/*      dropdown1 = lists all the categories
        dropdown2 = this drop down is not used by users. 
        dropdown3 = is a dynamically generated dropdown list which changes based on what is selected in dropdown1. 
*/
           var dropDown1 = document.getElementById(dropDown1);
           var dropDown2 = document.getElementById(dropDown2);
           var dropDown3 = document.getElementById(dropDown3);
	   var allDropDown2Elements = dropDown2.childNodes;
           // remove all <option>s in dropDown3
           while (dropDown3.hasChildNodes()){
               dropDown3.removeChild(dropDown3.firstChild);
           } ; 

           // loop though and insert into dropDown3 all of the city <option>s in dropdown2 that relate to the country value selected in dropdown1
           for(var i = 0; i < allDropDown2Elements.length; i++){
                   if (allDropDown2Elements[i].nodeType == 1 && allDropDown2Elements[i].getAttribute("title") == dropDown1.value) {
                     newDropDown3Element = allDropDown2Elements[i].cloneNode(true);
                     dropDown3.appendChild(newDropDown3Element);
                   }    
    
           } ;
           // (if you have server side logic that adds selected="selected" in dropdown2) extra code for IE to display the correct 'slected="selected"' value in the select box dropdown3
           if (navigator.userAgent.indexOf('MSIE') !=-1){
                   for (var i=0; i < dropDown3.length; i++) {
                   if(dropDown3[i].value == dropDown2.value) {
                     dropDown3[i].selected = true;
                   }
               }
           }
  }

function submitenter(myfield,e,searchstring,category)
{
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else 
		if (e) 
			keycode = e.which;
		else 
			return true;
	if (keycode == 13)
 	{
	//   myfield.form.submit();
		SearchDealer(searchstring,category);
		return false;
	}
	else
		return true;
}

function submitSearch(myfield,e,text)
{
	var keycode;
	if (window.event) 
		keycode = window.event.keyCode;
	else 
		if (e) 
			keycode = e.which;
		else 
			return true;
	if (keycode == 13)
 	{
	//   myfield.form.submit();
		var lang=getLanguage();
		Redireccionar('/'+lang+'/home/Site-Search.aspx?query='+text+'&language='+lang);
		return false;
	}
	else
		return true;
}

//Normal is 162 x 129
function resize(productimage)
{
	var MAXW=162;
	var MAXH=129;
	if (productimage!=null)
	{
		if (productimage.height>MAXH)
		{
			var ratio= MAXH/productimage.height;
			productimage.height=MAXH;
			/*if (productimage.width>MAXW)
			{	
				productimage.width=productimage.width*ratio;
			}*/
		}
		else if (productimage.width>MAXW)
		{
			var ratio= MAXW/productimage.width;
			productimage.width=MAXW;
			if (productimage.height>MAXH)
			{
				productimage.height=productimage.height*ratio;
			}
		}
	}
}





