﻿function cityCodingValidation(frm, buttonId, txtFromCity, ddlFromState, lblFromOR, txtFromZip, txtFromDate, txtToCity, ddlToState, lblToOR, txtToZip, txtToDate, valSum, blnProcessOrig, blnProcessDest)
{   
    var blnValid = false;
	var objAJAX = new clsAJAX();
	var blnAJAXSupport;
	var blnAJAXCoding;
	var blnSuccessfulCoding;
	var fromCity = '';
	var fromState = '';
	var fromZip = '';
	var toCity = '';
    var toState = '';
    var toZip = '';
	blnAJAXCoding = true;
	try
	{
		if(objAJAX.ajaxRequest != false)
		{
			blnAJAXSupport = true;
		}
		else
		{
			blnAJAXSupport = false;
		}
	}
	catch(e)
	{
		blnAJAXSupport = true;
	}
	//always use "US" as the country
	if (txtFromCity.value > '')
	{
	    fromCity = txtFromCity.value;
	}
	if (ddlFromState.value > '')
	{
	    fromState = ddlFromState.value + 'US';
	}
	if (txtFromZip.value > '')
	{
	    fromZip = txtFromZip.value;
	}
	if (txtToCity.value > '')
	{
	    toCity = txtToCity.value;
	}
	if (ddlToState.value > '')
	{
	    toState = ddlToState.value + 'US';
	}
	if (txtToZip.value > '')
	{
	    toZip = txtToZip.value;
	}
	//If user's browser supports AJAX and form passed JavaScript validation
	if(blnAJAXSupport)
	{
		var objQuoteButton;
	    objQuoteButton = document.getElementById(buttonId);
        var strOrigImage = "/images/new/FindProviders.jpg";
		var objAnimImage = document.createElement('div');
		objAnimImage.id = "waitingImg";
        if (navigator.appName == "Microsoft Internet Explorer")
        {
            objAnimImage.style.display = "inline";
        }
        else
        {
            objAnimImage.style.display = "inline-block";
        }
		Dom.add(objAnimImage, objQuoteButton.parentNode, frm);
        objAJAX.open("GET", "/AJAX/CityCoding.aspx?citycheck=yes&bypass=yes&oCity="+fromCity+"&oST="+fromState+"&oZip="+fromZip+"&dCity="+toCity+"&dST="+toState+"&dZip="+toZip+"&ms=true&date="+Date(), false);
		objAJAX.send(null);
        var responseText = objAJAX.ajaxRequest.responseText;
		var objCityCoding = eval('(' + objAJAX.ajaxRequest.responseText + ')');
		
		if(!blnAJAXCoding)
		{
            blnValid = true;
		}
		
		if(objCityCoding.blnOrigin && objCityCoding.origin.intMatchCount == 0 && blnProcessOrig)
		{
			if(objCityCoding.origin.objErrors[0].intErrorCode == 4 || objCityCoding.origin.objErrors[0].intErrorCode == 5)
			{
				alert(objCityCoding.origin.objErrors[0].strMessage);
				setFormFocus(frm,txtFromZip);
			}
		}
		else if(objCityCoding.blnDest && objCityCoding.dest.intMatchCount == 0 && blnProcessDest)
		{
			if(objCityCoding.dest.objErrors[0].intErrorCode == 4 || objCityCoding.dest.objErrors[0].intErrorCode == 5)
			{
				alert(objCityCoding.dest.objErrors[0].strMessage);
				setFormFocus(frm,txtToZip);
			}
		}
		else
		{
			var strDisplayMessage = '';
			if (objCityCoding.blnOrigin && blnProcessOrig)
			{
				strDisplayMessage = "There is an error in your Moving From information:\r\n" + objCityCoding.origin.strMessage;
			}
			if (objCityCoding.blnOrigin && objCityCoding.blnDest && blnProcessOrig && blnProcessDest)
			{
				strDisplayMessage += "\r\n\r\n";
			}
			if (objCityCoding.blnDest && blnProcessDest)
			{
				strDisplayMessage += "There is an error in your Moving To information:\r\n" + objCityCoding.dest.strMessage;
			}

			if ((objCityCoding.blnDest && blnProcessDest) || (objCityCoding.blnOrigin && blnProcessOrig))
			{
				alert(strDisplayMessage);
	            objQuoteButton.src = strOrigImage;
	            if (document.getElementById('waitingImg'))
	            {
			        Dom.remove(document.getElementById('waitingImg'));
			    }
			}
		    //If didn't need city coding then submit the form.
		    if(!handleCityCoding(objCityCoding, frm, txtFromCity, ddlFromState, lblFromOR, txtFromZip, txtToCity, ddlToState, lblToOR, txtToZip))
		    {
                blnValid = true;
		    }
		    else
		    {
			    blnSuccessfulCoding = false;
		    }
		}
	}
	else
	{
		var objQuoteButton;
		objQuoteButton = document.getElementById(buttonId);
		var objAnimImage = document.createElement('div');
		objAnimImage.id = "waitingImg";
	    Dom.add(objAnimImage, objQuoteButton, frm);
		blnValid = true;
	}
	//objQuoteButton.src = strOrigImage;
    if (document.getElementById('waitingImg'))
    {
        Dom.remove(document.getElementById('waitingImg'));
    }
	return blnValid;
}
function handleCityCoding(objCityCoding, frm, txtFromCity, ddlFromState, lblFromOR, txtFromZip, txtToCity, ddlToState, lblToOR, txtToZip)
{
	strFromID = 'fromCity';
	strToID = 'toCity';

	if(objCityCoding.blnOrigin || objCityCoding.blnDest)
	{
		//window.scrollTo(0,0);

		if(objCityCoding.blnOrigin)
		{
			if(objCityCoding.origin.intMatchCount > 0)
			{
				createCityCodingSelect(frm, strFromID, txtFromCity, ddlFromState, lblFromOR, txtFromZip, objCityCoding.origin);
			}
		}

		if(objCityCoding.blnDest)
		{
			if(objCityCoding.dest.intMatchCount > 0)
			{
				createCityCodingSelect(frm, strToID, txtToCity, ddlToState, lblToOR, txtToZip, objCityCoding.dest);
			}
		}
		return true;
	}
	else
	{
		return false;
	}
}
function createCityCodingSelect(frm, strID, strCityDestination, strStateDestination, lblORid, strZipDestination, objCityCoding)
{
	var objZipDestination;
	var cszClassName = "cszDetails";
	objZipDestination = strZipDestination.parentNode;
	var objCityDestination = strCityDestination.parentNode;
	var objStateDestination = strStateDestination.parentNode;
	var objOR = document.getElementById(lblORid)
	var objCityTextBox = strCityDestination;
	var objStateSelectBox = strStateDestination;
	var objZipTextBox = strZipDestination;
	objCityDestination.setAttribute("colSpan", "4");
	var blnImageFound = false;

	//Stop duplicate select boxes from being displayed
	if(document.getElementById(strID))
	{
		Dom.remove(document.getElementById(strID));
	}
	//Stop duplicate error images from being displayed
	if(getElementsByClassName("AJAXQQError", objCityDestination).length > 0)
	{
		blnImageFound = true;

		var arrItems = getElementsByClassName("AJAXQQError", objCityDestination);
		for(var i = 0; i < arrItems.length; i++)
		{
			Dom.remove(arrItems[i], frm);
		}
	}
	var objErrorImage = document.createElement('div');
	objErrorImage.className = "AJAXQQError";
    
    //objError
	var objCityDropDown = document.createElement('select');
	objCityDropDown.id = strID;

	objCityDropDown[objCityDropDown.length] = new Option("(Please Select One)");
	for(var i = 0; i < objCityCoding.objMatch.length; i++)
	{
		if(objCityCoding.objMatch[i].strState != "" && objCityCoding.objMatch[i].strZip != "")
		{
			if(objCityCoding.strType=="ZIPS")
			{

				if(objCityCoding.objMatch[i].strZip.substr(0, 1) == "@")
				{
					objCityDropDown[objCityDropDown.length] = new Option(objCityCoding.objMatch[i].strZip.substr(1,objCityCoding.objMatch[i].strZip.length), "@"+objCityCoding.objMatch[i].strCity+', '+objCityCoding.objMatch[i].strState);
				}
				else
				{
					objCityDropDown[objCityDropDown.length] = new Option(objCityCoding.objMatch[i].strCity + ', ' + objCityCoding.objMatch[i].strState, objCityCoding.objMatch[i].strCity);
				}
			}
			else
			{
				objCityDropDown[objCityDropDown.length] = new Option(objCityCoding.objMatch[i].strCity + ', ' + objCityCoding.objMatch[i].strState, objCityCoding.objMatch[i].strCity);
			}
		}
		else
		{
			objCityDropDown[objCityDropDown.length] = new Option(objCityCoding.objMatch[i].strCity, "");
		}
	}
	var strEnterNewCity = "--Enter New City--";
	objCityDropDown[objCityDropDown.length] = new Option(strEnterNewCity);
	objCityTextBox.style.display = "none";
	objStateSelectBox.style.display = "none";
	if (objOR != null) {objOR.style.display = "none";}
	objZipDestination.style.display = "none";
	Dom.add(objErrorImage, objCityDestination, frm);
	Dom.add(objCityDropDown, objCityDestination, frm);
	var objErrorDiv = document.createElement('div');
	Event.add(objErrorImage, 'mouseover',
		function()
		{
			objErrorDiv.style.position = "absolute";
			objErrorDiv.style.zIndex = "202";
			objErrorDiv.style.background = "#FDFFE0";
			objErrorDiv.style.border = "1px solid black";
			objErrorDiv.style.fontSize = "13px";
			objErrorDiv.style.width = "180px";
			objErrorDiv.style.fontWeight = "bold";
			objErrorDiv.style.padding = "5px";
			objErrorDiv.innerHTML = objCityCoding.strMessage;
			objErrorDiv.style.left = objErrorImage.offsetLeft + objErrorImage.offsetWidth;
			objErrorDiv.style.top = objErrorImage.offsetTop - objErrorImage.offsetHeight - objErrorImage.offsetHeight;
			Dom.add(objErrorDiv, objCityDestination, frm);
		}
	);
	Event.add(objErrorImage, 'mouseout',
		function()
		{
			Dom.remove(objErrorDiv, frm);
		}
	);
	blinkYellow(objCityDropDown, 1, 5);
	Event.add(objCityDropDown, 'change',
		function()
		{
			var objSelectedItem = objCityDropDown.options[objCityDropDown.selectedIndex];
			if(objSelectedItem.text == strEnterNewCity)
			{
			    objCityDestination.setAttribute("colSpan", "1");
				Dom.remove(objCityDropDown, frm);
				Dom.remove(objErrorImage, frm);
				objCityTextBox.style.display = "";
				objCityTextBox.className = cszClassName;
				objStateSelectBox.style.display = "";
				objStateSelectBox.className = cszClassName;
				if (objOR != null) {objOR.style.display = "";}
				objZipDestination.style.display = "";
				objZipDestination.className = cszClassName;

				blinkYellow(objCityTextBox, 3, 5);
			}
			else
			{
				var strTempCity, strTempState, strTempZip

				if(objCityCoding.strType=="ZIPS")
				{
					if(objSelectedItem.text.search(",") != -1)
					{
						strTempCity = objSelectedItem.value;
						strTempState = objSelectedItem.text.substr(objSelectedItem.text.length-2, 2);

                        //alert('select item 1');
						selectItem(objStateSelectBox, strTempState);

						objCityTextBox.value = strTempCity;

						if(objCityCoding.strMessage.search("conflicts") != -1)
						{
							objZipTextBox.value = '';
						}
					}
					else if(objSelectedItem.value.search("@") != -1)
					{
						strTempZip = objSelectedItem.text.substr(0,5);

						strTempCity = objSelectedItem.value.substr(1, objSelectedItem.value.length-5);
						strTempState = objSelectedItem.value.substr(objSelectedItem.value.length-2, objSelectedItem.value.length);


						objCityTextBox.value = strTempCity
						selectItem(objStateSelectBox, strTempState);
						objZipTextBox.value = strTempZip;
					}
				}
				else
				{
					if(objSelectedItem.text.search(",") != -1)
					{
						strTempCity = objSelectedItem.value;
						strTempState = objSelectedItem.text.substr(objSelectedItem.text.length-2, 2);

						selectItem(objStateSelectBox, strTempState);
						objCityTextBox.value = objSelectedItem.value;

						if(objCityCoding.strMessage.search("conflicts") != -1)
						{
							objZipTextBox.value = '';
						}
					}
				}
			}
		}
	);
}
function selectItem(objSelectBox, strSelectText)
{
	for(var i = 0; i < objSelectBox.options.length; i++)
	{
		if(objSelectBox.options[i].text == strSelectText)
		{
			objSelectBox.options[i].selected = true;
		}
	}
}