 (function(){

	function showErrorResult(xml)
	{
		
	}
	
	
	function populateResults(xml,localeCountry) 
	{		
		var status = $("status",xml).text();
		if(status == "error") {
			showErrorResult();
			return;
		}
		var country = $("country",xml).text();
		var isUPSStore = $("upsStore",xml).text();
		var isTargetCountry = $("isTargetCountry",xml).text();
		var isException = $("isException",xml).text();
		var isStreetExample =$("isStreetExample",xml).text();
		var streetLabel=$("streetLabel",xml).text();
		var isShowState=$("isShowState",xml).text();
		var streetExample =$("streetExample",xml).text();
		countryValue = country;
		if(isException == "true")
		{
			if(isTargetCountry == "true")
			{
				isTargetCtry = "true";
				$('#div10').show();
				$('#originStreet').show();
				$('#originCity').show();
				//$('#originState').show();
				$('#originPostal').show();
				
				$('#originStreetLabel').text(streetLabel);
				
				if(isStreetExample == "false")
				{
		 			  $('#originStreet').children()[4].style.display = 'none';
		
				}
				else
				{
				 	$('#originStreet').children()[4].innerHTML = streetExample;
					$('#originStreet').children()[4].style.display = 'inline';
				}
					
			    var divAddressEle = $('#origin').children().children();
			    
			    if(isShowState == "false")
		     	{
		     	  	$('#originState').hide();
					divAddressEle[7].style.display = 'none';
		     	  
		     	}
		     	else
		     	{
		     	  	$('#originState').show();
					divAddressEle[7].style.display = 'inline';
		     	
		     	}
			    
			    divAddressEle[1].style.display = 'inline';
			    divAddressEle[3].style.display = 'inline';
			    //divAddressEle[5].style.display = 'inline';
			  

				$("#div6").hide();
				$("#div8").remove();
				$("#div9").remove();
				$("#inDiv2").empty();
				$("#div11").hide();
				$('#div14').show();
			}
			else
			{
				isTargetCtry = "false";
				$('#originStreet').hide();
				$('#originCity').hide();
				$('#originState').hide();
				$('#originPostal').hide();
				
			    var divAddressEle = $('#origin').children().children();
			    divAddressEle[1].style.display = 'none';
			    divAddressEle[3].style.display = 'none';
			    //divAddressEle[5].style.display = 'none';
			    divAddressEle[7].style.display = 'none';
				
				$("#div6").hide();
				$("#div8").remove();
				$("#div9").remove();
				$("#inDiv2").empty();
				//$('#div10').show();
				$("#div11").hide();
				$('#div14').show();
			}
		}
		else
		{
			if(isTargetCountry == "true")
			{
				isTargetCtry = "true";
				
				if(isUPSStore == "true")
				{
					isUPSStoreCtry = "true";
					$('#div10').show();
					$('#originStreet').show();
					$('#originCity').show();
															//$('#originState').show();
					$('#originPostal').show();
					
					$('#originStreetLabel').text(streetLabel);
				
					
					if(isStreetExample == "false")
					{
			 			  $('#originStreet').children()[4].style.display = 'none';
					}
					else
					{
						$('#originStreet').children()[4].innerHTML = streetExample;
					  		$('#originStreet').children()[4].style.display = 'block';
									   
					}

				    var divAddressEle = $('#origin').children().children();
				    
				    if(isShowState == "false")
		        	{
		     	    	$('#originState').hide();
				    	divAddressEle[7].style.display = 'none';
		     	     }
			     	else
			     	{
			     	  	$('#originState').show();
						divAddressEle[7].style.display = 'block';
			     	
			     	}
				    divAddressEle[1].style.display = 'block';
				    divAddressEle[3].style.display = 'block';
				    //divAddressEle[5].style.display = 'block'; // deliberate
				 

					
					
					$("#div6").hide();
					$("#div8").remove();
					$("#div9").remove();
					$('#inDiv2').empty();
					createDivElements("locTypes",xml);
					$('#div11').show();
					//$('#div12').hide(); // There is no div 12
					$('#div14').show();
				}
				else
				{
					isUPSStoreCtry = "false";
					$('#div10').show();
					$('#originStreet').show();
					$('#originCity').show();
					//$('#originState').show();
					$('#originPostal').show();
					
					$('#originStreetLabel').text(streetLabel);
				
					if(isStreetExample == "false")
					{
			 			  $('#originStreet').children()[4].style.display = 'none';
			
					}
					else
					{
					 	$('#originStreet').children()[4].innerHTML = streetExample;
					  		$('#originStreet').children()[4].style.display = 'block';
					}					
						
			
				    var divAddressEle = $('#origin').children().children();
				     if(isShowState == "false")
		     		{
		     	 	 	$('#originState').hide();
						divAddressEle[7].style.display = 'none';
		     	  
		     		}
		     		else
		     		{
		     		  	$('#originState').show();
						divAddressEle[7].style.display = 'block';
		     	   	}
				    divAddressEle[1].style.display = 'block';
				    divAddressEle[3].style.display = 'block';
				    //divAddressEle[5].style.display = 'block';
				    
					
					$("#div6").hide();
					$("#div8").remove();
					$("#div9").remove();
					$('#inDiv2').empty();
					createDivElements("srvcOptions",xml);
					$('#div11').show();
					$('#div14').show();
				}
			}
			else
			{
				isTargetCtry = "false";
				//$('#div10').hide();
				$('#originStreet').hide();
				$('#originCity').hide();
				$('#originState').hide();
				$('#originPostal').hide();
				
			    var divAddressEle = $('#origin').children().children();
			    divAddressEle[1].style.display = 'none';
			    divAddressEle[3].style.display = 'none';
			    divAddressEle[5].style.display = 'none';
			    divAddressEle[7].style.display = 'none';

			    //$("#div6").remove();
				$('#div8').remove();
				$('#div9').remove();
				$('#inDiv2').empty();
				$('#div11').hide();
				$('#div10').show();
				if(country == localeCountry)
				{
					$('#div6').show();
					$('#div14').hide();
				}
				else
				{
					$('#div6').hide();
					$('#div14').show();
				}
			}
		}
	}
	
	function createDivElements(elementType,xml)
	{
		if(elementType == "locTypes")
		{
			var ServiceLocation = $("ServiceLocation",xml);
			var code;
			var description;
			var locationType;
			var temp;
			var accordEle1 = "<DL><DT><LABEL for='select_location_type'>" +  $("locationTypesLabel",xml).text() + 
									"</LABEL></DT><DD><TABLE width= '100%' class='' cellSpacing='0' cellPadding='0' border='0'><TBODY>";
			var accordEle3="";
			
			$(ServiceLocation).find("LocationType").each(
			function() 
			{   		
				locationType = $(this);

				code = $("Code",locationType).text();
				description = $("Description",locationType).text();
				
				var accordEle2 = "<tr valign='top'><td>";
				var chkBox = "<input id=" + code + " type='checkbox'" + " name=" + code +  " value=" + code + " checked>&nbsp;</td>";
				var chkBoxLbl = "<td><label for=" + code + " class='sel'>" + description + "</label></td></tr>";

				temp = accordEle2 + chkBox + chkBoxLbl;
				accordEle3 =  accordEle3 + temp; 
			});
			var allEle = accordEle1 + accordEle3 + '</TBODY></table></dd></dl>'; 
			$('#inDiv2').append(allEle);
			
		}
		else if(elementType == "srvcOptions")
		{
			//var div= document.getElementById(divID);
			var code;
			var description;
			var accordEle4 = "";
			var srvcOptLbl = $("serviceOptionLabel",xml).text();
			var closestLocLbl = $("closestLocationLabel",xml).text();
			var withinLbl = $("withinLabel",xml).text() + " ";
			var srvcOptAll = $("serviceOptionAll",xml).text();
			var accordEle1 = "<DL><TABLE width= '100%' class='' cellSpacing='0' cellPadding='0' border='0'>" + 
								"<TBODY><tr valign='top'><td><input id='service_option' type='radio' name='serviceOption' checked='checked'>" +
									"</td><td><label for='select_service_option'>" + srvcOptLbl + "</label></td></tr><tr valign='top'><td>&nbsp;</td>" +
										"<td><select id='service_options' name='service_options' onChange=''>" +
											"<option id='All' value='All' selected>" + srvcOptAll + "</option>";
			
			var ServiceOptions = $("ServiceOptions",xml);
			var serviceOption;
			
			$(ServiceOptions).find("ServiceOption").each(
			function() 
			{   			
				serviceOption = $(this);
				
				code = $("Code",serviceOption).text();
				description = $("Description",serviceOption).text();
				var accordEle2 = "<option value=" + code +  ">" + description + "</option>";
				accordEle4 = accordEle4 + accordEle2;
			});

			var accordEle3 = "</select></td></tr><tr valign='top'><td><input id='closest_location' type='radio' name='serviceOption'></td>" +
								"<td><label for='closest_location'> " + closestLocLbl + 
									"</label></td></tr><tr valign='top'><td>&nbsp;</td><td>" + withinLbl +
										"<select id='miles' name='miles'>" +
										"<option value='1'>" + "1" + "</option>" +
										"<option value='5'>" + "5" + "</option>" +
										"<option value='10'>" + "10" + "</option>" +
										"<option value='20'>" + "20" + "</option>" +
										"<option value='50'>" + "50" + "</option>" +
										"<option value='200' selected>" + "200" + "</option></select>";
			
			var milesLbl =  " " + $("milesLabel",xml).text();
			
			var accordEle5 = "</td></tr></TBODY></table></dl>";

			var allEle = accordEle1 + accordEle4 + accordEle3 + milesLbl + accordEle5;
			$('#inDiv2').append(allEle);
		}
	}
	
	
	function handleSubmitData() 
	{
		var dolAction;
		var autoSubmitVal=1;
		var stateValue;
		
		if(countryValue == "")
		    countryValue = $('#originCountryValue option:selected').val();
		  
		if(isTargetCtry == "true")
		{
			
			
			if($('#originStateValue').is("select") == "true")
			{
			
				stateValue = $('#originStateValue option:selected').val();
			}
			else
			{
				stateValue = $('#originStateValue').val();
			
			}
			
		}
		
		if(isTargetCtry == "true" && isUPSStoreCtry == "true")
		{
			var locationType="";
			var temp=null;
			var i=0;
			$("#div8 :checked").each(function() { 
				i++;
				if(i == $("#div8 input:checked").size())
				{
					temp = $(this).val();
				}
				else
				{
					temp = $(this).val() + ",";
				}
				
				locationType = locationType + temp;
				
			});
			i=0;
			$("#div11 :checked").each(function() { 
				i++;
				if(i == $("#div11 input:checked").size())
				{
					temp = $(this).val();
				}
				else
				{
					temp = $(this).val() + ",";
				}
				
				locationType = locationType + temp;
				
			});

			dolAction= $(dollaunch_form.formAction).val() + "?country="+  countryValue + 
			"&loc=" + $(dollaunch_form.loc).val() + "&WBPM_lid=" + $(dollaunch_form.WBPM_lid).val() + "&appid=" + $(dollaunch_form.appid).val() + "&autosubmit=" + autoSubmitVal + 
			"&Address=" + $('#originStreetValue').val() + "&PoliticalDiv1=" + stateValue + 
			"&PoliticalDiv2=" + $('#originCityValue').val() + "&Postal=" + $('#originPostalValue').val() + "&loc_type=" + 
			locationType; 

		}
		else if(isTargetCtry == "true" && isUPSStoreCtry == "false")
		{
			if(document.getElementById('service_option').checked)
			{
				if($('#service_options option:selected').val() == "All")
				{	
					dolAction=  $(dollaunch_form.formAction).val() + "?country="+  countryValue + 
					"&loc=" + $(dollaunch_form.loc).val() + "&WBPM_lid=" + $(dollaunch_form.WBPM_lid).val() + "&appid=" + $(dollaunch_form.appid).val() + "&autosubmit=" + autoSubmitVal + 
					"&Address=" + $('#originStreetValue').val() + "&PoliticalDiv1=" + stateValue + 
					"&PoliticalDiv2=" + $('#originCityValue').val() + "&Postal=" + $('#originPostalValue').val() + "&service_option=";
				}
				else
				{
					dolAction= $(dollaunch_form.formAction).val() + "?country="+ countryValue + 
					"&loc=" + $(dollaunch_form.loc).val() + "&WBPM_lid=" + $(dollaunch_form.WBPM_lid).val() + "&appid=" + $(dollaunch_form.appid).val() + "&autosubmit=" + autoSubmitVal + 
					"&Address=" + $('#originStreetValue').val() + "&PoliticalDiv1=" + stateValue + 
					"&PoliticalDiv2=" + $('#originCityValue').val() + "&Postal=" + $('#originPostalValue').val() + "&service_option=" + 
					$('#service_options option:selected').val(); 
				}
			}
			else if(document.getElementById('closest_location').checked)
			{	
				dolAction= $(dollaunch_form.formAction).val() + "?country="+  countryValue + 
							"&loc=" + $(dollaunch_form.loc).val() + "&WBPM_lid=" + $(dollaunch_form.WBPM_lid).val() + "&appid=" + $(dollaunch_form.appid).val() + "&autosubmit=" + autoSubmitVal + 
							"&Address=" + $('#originStreetValue').val() + "&PoliticalDiv1=" + stateValue + 
							"&PoliticalDiv2=" + $('#originCityValue').val() + "&Postal=" + $('#originPostalValue').val() + 
							"&closest_loc=" + $('#miles option:selected').text();
			}
		}
		else if(isTargetCtry == "false")
		{
			var loc = $(dollaunch_form.loc).val().substring(0,2);
			
			dolAction= $(dollaunch_form.formAction).val() + "?country="+  countryValue + 
			"&loc=" + $(dollaunch_form.loc).val() + "&WBPM_lid=" + $(dollaunch_form.WBPM_lid).val() + "&autosubmit=" + autoSubmitVal + "&appid=" + $(dollaunch_form.appid).val();
		}
		
		location.href = dolAction;
		document.dollaunch_submit_form.method = "POST";
		document.dollaunch_submit_form.action = dolAction;
		document.dollaunch_submit_form.submit();
		
	}
	
	function countryChange()
	{
			//$('#div10').hide(); // deliberately commented
			//$('#div11').hide(); // deliberately commented
		
			$('#originStreetValue').val("");
			$('#originCityValue').val("");
			$('#originPostalValue').val("");

			var countryToSend = $('#originCountryValue option:selected').val();
			
			var locale = $(dollaunch_form.loc).val();
			var localeCountry = $(dollaunch_form.localeCountry).val();
			
			var params = {	loc: locale,
					charset: "utf-8",
					javascript:"true",
					country: countryToSend
			};
	
			$.ajax({
				url: '/dollaunch/ajax',
				type: 'post',
				beforeSend: function(xhr) {
					xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
				},
				data: params,
				dataType: 'xml',
				timeout: 4000,
				error: function(){
					showErrorResult();
				},
				success: function(xml) {
					populateResults(xml,localeCountry);
				}
			});
	}

	
	var com_ups_dollaunch = window.com_ups_dollaunch = function() {	};

	com_ups_dollaunch.handleSubmitData = handleSubmitData;	
	com_ups_dollaunch.countryChange = countryChange;
	
})();