/** 
 * Setup proxy <select> for province/state textfield for USA/CANADA
 */
function proxy_text_provcombo_country(cboCountry, txtfield, proxylabel, proxydefault){
  	var src_country = cboCountry;
	var src_txtprov = txtfield;
	var proxy_name = "state_prov_proxy_"+proxylabel;

        var $j = jQuery.noConflict();   

	
	//hide the textbox unless something in parent combo box value is selected.
	// then hide show the combo/textbox as normal.	
	
	//run initially in case values are already present.
	setupCombo($j(src_country).val(),proxydefault);

	// Change text field to combo box.
	$j(src_country).change(function(){
            var countryVal = this.value;
	    
	    setupCombo(countryVal,"");	    	    
	});
	
	
	

	function setupCombo(combovalue, _proxydefault){

	     //check if the given combo value matches CANADA/USA
	     if("Canada" == combovalue || "United States of America" == combovalue){

	      //remove any existing proxy element.
	      var proxy_element = "select[name='" + proxy_name + "']";
	      $j(proxy_element).remove();
	      
	      var urladdress = "/_includes/states.html";
	      if("Canada" == combovalue){
		      urladdress = "/_includes/prov.html";
	      }


	      $j.get(urladdress, function(data){
		//INSERT prov
                $j(src_txtprov).after(data); //show provstates		
		
		//hide, but re-enable textfield while hidden
		$j(src_txtprov).hide();		
		$j(src_txtprov).removeAttr('disabled');
		                                
		var proxyCbo = $j("select[name='state_prov_proxy']")
		    .attr('name', function() {
			return proxy_name;
		     })
		    .change(function(){
			$j(src_txtprov).val(this.value); //sync selected value
		     });
		    
                 var currTxtValue = $j(src_txtprov).val();
                 if(_proxydefault != ""){
                   //use the default if possible for first time.
                   currTxtValue = getDefaultValForText(proxyCbo,_proxydefault);
                   $j(src_txtprov).val(currTxtValue); //set the text field value.
                 }

                 proxyCbo.val(currTxtValue); //default to current text value (if possible)
		
		 if(proxyCbo.get(0).value != currTxtValue){
		     //reset the text field value, couldn't match to combo
		     $j(src_txtprov).val(""); 
		 }
	      })
	    }else{	       
	       //show the textfield
	       $j(src_txtprov).show();
		    
	       //there was nothing selected for parent, show textbox.
	       if(!combovalue || combovalue == "" || combovalue == "0"){
	          //disable the text field
		  $j(src_txtprov).val("Please select Country");
		  $j(src_txtprov).attr('disabled', 'disabled');
	       }else{
		  //re-enable textfield
		  $j(src_txtprov).val("");
		  $j(src_txtprov).removeAttr('disabled');
	       }
	       
	       //no match, so remove the proxy element combo.
	       var proxy_element = "select[name='" + proxy_name + "']";
	       $j(proxy_element).remove();
	    }
	}

        function getDefaultValForText($combo, defaultVal){	 	
	  var $j = jQuery.noConflict(); 

          var value = "";

          var $options = $j("option",$combo); //get underlying combo options           
          var len = $j("option",$combo).size(); 

          for(index = 0; index < len; index++) { 
             if($options[index].text == defaultVal){ 
                value = $options[index].value;
                break;
             }
          }
          return value;           	
	}

        
};
