//Global variables
var urlValidZip = "/dealers/zipCode.action?zc=";
var zipErrorText = "The Zip Code You've Entered is Not Valid.";

//zipError elements
var miniCitySelect = "miniCitySelector";
var miniZcText = "miniZc";
var miniZipForm = "miniZip";
var zipErrorDiv = "zipPrompt";

/*********************************************
 * Displays the zipError div
 * 
 * @param rform the form
 * @param flw the function
 *********************************************/
function showZipError(rform, flw)
{
	document.getElementById(miniCitySelect).selectedIndex = 0;
	//dimIE6FormElements(false);
	window.scrollTo(0,0);
	
	var zipErrorWindow = document.getElementById(zipErrorDiv);    
	var zipErrorWindowForm = document.getElementById(miniZipForm); 

	zipErrorWindowForm.pform = rform;
	zipErrorWindowForm.pformfollow = flw;
	zipErrorWindowForm.zip.value = rform.zc.value;
	zipErrorWindow.style.display = "block";
	
	//focus cursor
	document.getElementById(miniZcText).focus();
}

/**********************************************
 * Hides the zipError div
 **********************************************/
function hideZipError()
{
	//dimIE6FormElements(true);
	$("#" + zipErrorDiv).css("display", "none");
}

/*********************************************
 * This function determines whether a ZIP
 * code is valid and then executes the
 * function which is passed as a parameter.
 * 
 * @param rform the submitted form
 * @param flw the method to execute
 * @param rzip the ZIP code to verify
 *********************************************/
function checkZipFirst(rform, flw, rzip)
{
	if(rzip.length < 5)
	{
		flw(rform, false);
	}
	else 
	{
		var isValid = false;
		var url = urlValidZip + rzip;

		$.ajax({
		    type: "GET",
			url: url,
			dataType: "text",
		    success: function(data)
		    {
		        isValid = eval(data);
				flw(rform,isValid);
		    }
		});
	}
}

/*********************************************
 * Updates the zipError form with the ZIP code 
 * that is part of the selected miniCitySelect 
 * drop-down.
*********************************************/
function updateZipErrorForm()
{
	var citySelect = $('#' + miniCitySelect).val(); 
	if(citySelect.length > 0)
	{
		var cityZipCode = citySelect.split("|")[0];
		var cityRadius = citySelect.split("|")[1];
		if(cityZipCode.length > 0)
		{
			$('#' + miniZcText).val(cityZipCode);
		}
		if(cityRadius.length > 0)
		{
	     	 if(cityRadius === 'All')
	    	 {
	    		cityRadius = '100000';
	    	 }
	     	 if(cityRadius === '45')
	     	 {
	     		 cityRadius = '30';
	     	 }
     		 $('#[name=sc.radius]').val(cityRadius);
		}
	}
}
/**********************************************
 * Resets selected value of the miniCitySelect
 * drop down.
 **********************************************/
function resetMiniCitySelect()
{
	document.getElementById(miniCitySelect).selectedIndex = 0;
}

/********************************************************
 * 
 * 	Functions passed in as arguments from JSP
 * 
 ********************************************************/
function dealerlocatorSubmit(rForm, zipValid)
{
	if(!zipValid)
	{
		showZipError(rForm, dealerlocatorSubmitContinue);
	} 
	else 
	{
		dealerlocatorSubmitContinue(rForm);
	}
}
function dealerlocatorSubmitContinue(rForm)
{
	rForm.action = "/dealers/search-results.action";
	rForm.submit();
}
function miniFormSubmit(rform, isValid)
{
	if(!isValid)
	{
		//do nothing
	} 
	else 
	{
		rform.pform.zc.value = rform.zip.value;
		rform.pformfollow(rform.pform);
		hideZipError();
	}
}


/*********************************************************
 * 
 * 		TODO			ie6 functions
 * 
 ********************************************************/
function dimIE6FormElements(show)
{
	var visSet = "hidden";
	if(show)
	{
		visSet = "visible";
	}
	for(i=0;i<ie6dimlist.length;i++)
	{
		if(document.getElementById(ie6dimlist[i]))
		{
			document.getElementById(ie6dimlist[i]).style.visibility = visSet;
		}
	}
}
