$( function( ){
		/*hide currency*/
		if ($("select[name='currency']").length) {
			$("select[name='currency']").parent().hide();
		}
	
		$('A.info_1').tooltip({ extraClass: "hoverbox_1", track: false, delay: 250, showURL: false, showBody: " - ", fade: 250 });
		$("#accordion").accordion({
			icons: {
    			header: "ui-icon-circle-arrow-e",
   				headerSelected: "ui-icon-circle-arrow-s"
			},
			autoHeight: false,
			collapsible: true
		});
		// Activate tabs
		$("#tabs").tabs();
		
		//Add "more info" link to domain conf
		$("form[action='/clientes/cart.php?a=confdomains'] > div.cartbox").prepend('<a href="/dominios.php#opciones" target="_blank">&iquest;Qu&eacute; es esto?</a>');
		
		// colorbox overlays
		$(".thickbox").colorbox();
		
		// Random Testimonials
		if ($("#testimonialBox").length){
			$.getJSON("/js/testimonials.js", function(json){
				var item = (json[Math.floor(Math.random() * json.length)]);
				$("#testimonialBox>.BoxBody>p:first").html(item.text);
				$("#testimonialBox>.BoxBody>p:eq(1)").html(item.author);
				$("#testimonialBox a:first").attr("href", item.link).text(item.linktext);
			});
		} // end if testimonials box exists
		
		/*Change the client details form behavior*/
		if ($("input[name='state']").length){
			$.getScript("/js/countrystate.js", function(){
				var defaultCountry = $("select[name='country']").val();
				if (defaultCountry == "") defaultCountry = "CR"; // TODO: get default country using IP Address lookup
				// create dropdown elements
				$("<select id=\"stateDDL\"></select>").insertBefore("input[name='state']");
				$("<select id=\"cityDDL\"></select>").insertBefore("input[name='city']");
				$("<select id=\"distrito\"></select>").insertAfter("#cityDDL");
				$("#stateDDL").hide();
				$("#cityDDL").hide();
				$("#distrito").hide();
			   $("#stateDDL").bind("change", populateCity);
			   $("#cityDDL").bind("change", populateCounty);
			   $("#distrito").bind("change", updateZipCode);
				
				
			   $("select[name='country']").bind("change", populateState);
			   setTimeout("$(\"select[name='country']\").trigger(\"change\");",1000 );

			});
		}
		
// Populates the counties select with the counties from the counties list
//

function populateCounty( ) {
	$("input[name='city']").val($("#cityDDL").val());
   var selObj = $("#distrito");
	var defaultCounty = $("input[name='postcode']").val();
   var foundCounty = false;

   // Empty options just in case new drop down is shorter
   //
	$("#distrito").children().remove();
	$("#distrito").addOption({'':'Seleccione un distrito'},false);
   // Populate the drop down with states from the selected country
   //
   var countyLineArray   = county.split("|");        // Split into lines

   for (var loop = 0; loop < countyLineArray.length; loop++) {

      lineArray = countyLineArray[loop].split(":");

      cityCode  = $.trim(lineArray[0]);
      countyName    = $.trim(lineArray[1]);
      countyCode   = $.trim(lineArray[2]);

		if ( $("#cityDDL").val() == cityCode && cityCode != '' ) {
		$("#distrito").append(
			$('<option' + (( countyCode == defaultCounty )?" selected=\"selected\"":"") + '></option').val(countyCode).html(countyName)
		 );
		 
		 foundCounty = true;
		}
	}
   // If the country has no states, change the select to a text box
   //
	if ( ! foundCounty ) {
	  $("#distrito").hide();
	}
	else
	{
	   $("#distrito").show();
	}
   
}
function populateCity( ) {
	 $("input[name='state']").val($("#stateDDL").val());
	 
	var cityTxt = $("input[name='city']");
   var foundCity = false;
   var defaultCity = cityTxt.val();
   // Empty options just in case new drop down is shorter
   //
	$("#cityDDL").children().remove();
	$("#cityDDL").addOption({'':'Seleccione un canton'},true);

   // Populate the drop down with states from the selected country
	//
	var cityLineArray   = city.split("|");        // Split into lines
	var selectedState = $("#stateDDL").val() ;
	for (var loop = 0; loop < cityLineArray.length; loop++) {
		lineArray = cityLineArray[loop].split(":");
		stateCode  = $.trim(lineArray[0]);
		cityCode    = $.trim(lineArray[1]);
		cityName    = $.trim(lineArray[2]);
		
		if ( selectedState == stateCode && cityCode != '' ) {
		  $("#cityDDL").append(
			$('<option' + (( cityCode == defaultCity )?" selected=\"selected\"":"") + '></option').val(cityCode).html(cityName)
		  );
		 foundCity = true;
		}
	}
   // If the state has no cities, change the select to a text box
   //
   if ( foundCity ) {
	   $("input[name='city']").hide();
	   $("#cityDDL").show();
	   $("#cityDDL").trigger("change");
   }
   else
   {
      $("input[name='city']").show();
	  $("#cityDDL").hide();
	  $("#distrito").hide();
   }
}

function updateZipCode( ) {
	$( "input[name='postcode']" ).val($( "#distrito" ).val()) ;
}

function populateState(  ) {
   var defaultState = $("input[name='state']").val();
   var selectedCountry = $("select[name='country']").val();
   var foundState = false;
  
   // Empty options just in case new drop down is shorter
	$("#stateDDL").children().remove();
    $("#stateDDL").addOption({'':'Selecione...'},true);

   // Populate the drop down with states from the selected country
   var stateLineArray   = state.split("|");        // Split into lines

   for (var loop = 0; loop < stateLineArray.length; loop++) {

      lineArray = stateLineArray[loop].split(":");

      countryCode  = $.trim(lineArray[0]);
      stateCode    = $.trim(lineArray[1]);
      stateName    = $.trim(lineArray[2]);

      if ( selectedCountry == countryCode && stateCode != '' ) {
		  
		 $("#stateDDL").append(
        	$('<option' + (( stateCode == defaultState )?" selected=\"selected\"":"") + '></option').val(stateCode).html(stateName)
    	  );

		//$("#stateDDL").addOption({stateCode:stateName},( stateCode == defaultState ));
         foundState = true;
      }
   }
   // If the country has no states, change the select to a text box
   //
   if ( foundState ){
	   $("#stateDDL").show();
       $("input[name='state']").hide();
	   $("#stateDDL").trigger("change");
   }
   else
   {
      $("input[name='state']").show();
	  $("#stateDDL").hide();
	  $("input[name='city']").show();
	  $("#cityDDL").hide();
	  $("#distrito").hide();
   }
}


} );


