// $Id$
/**
 * Script helpers for paymentprocessing.
 */


/**
 * The Payprocessing object.
 */
var Payprocessing = {};

 if (Drupal.jsEnabled) {
    $(document).ready(function() {
      $("#edit-country").change(function(){
		 var selc = $('#edit-country option:selected').val();
	var sel = $("#edit-country :selected").val();  
		var element = $("#edit-state");
		var selList;
	var replacement = '<input style="" class="none" id="edit-statetx" "type="text" 	value="" />';
	  if(sel == 'US' || sel == 'CA') {

		replacement = '<select style="background-color: rgb(255, 255, 160);" name="state" class="form-select required" id="edit-state"><option value="">Please Select One</option></select>';

$("#edit-state")
.after(replacement).next()
.attr('class', element.attr('class')).attr('id',element.attr('id'))
.prev().remove();
		selList = $(Payprocessing.stateList);	
 	// need better error checking, what if JSON returns nothing?
	  } else {
$("#edit-state")
.after(replacement).next()
.attr('class', element.attr('class')).attr('id',element.attr('id'))
.prev().remove();	  
	  }
      });
    });
	
	Payprocessing.checkList = function() {
	sel = $("#edit-country :selected").val();  
		var element = $("#edit-state");
		var selList;
	var replacement = '<input style="" class="none" id="edit-statetx" "type="text" 	value="" />';
	  if(sel == 'US' || sel == 'CA') {

		replacement = '<select style="background-color: rgb(255, 255, 160);" name="state" class="form-select required" id="edit-state"><option value="">Please Select One</option></select>';

$("#edit-state")
.after(replacement).next()
.attr('class', element.attr('class')).attr('id',element.attr('id'))
.prev().remove();
		selList = $(Payprocessing.stateList);	
 	// need better error checking, what if JSON returns nothing?
	  } else {
$("#edit-state")
.after(replacement).next()
.attr('class', element.attr('class')).attr('id',element.attr('id'))
.prev().remove();	  
	  }
	
	}
	  /**
   * A function to fetch quotes from the server, and display in the 
   * designated area.
   */
  Payprocessing.stateList = function() {
    $.get(Drupal.settings.paymentprocessing.json_url  + '/' + $('#edit-country option:selected').val(), function(data) {
	  myQuote = Drupal.parseJson(data);      
      if(!myQuote.status || myQuote.status == 0 || data == 0) {
	  // need better error checking, what if JSON returns nothing?
		$("#edit-state").clearSelect();
		$("#edit-state").fillSelect(myQuote);
       } 
    }); // End inline function
  } // End  function
  
  
   $.fn.clearSelect = function() {
     return this.each(function() {
         if (this.tagName == 'SELECT')
             this.options.length = 0;
      });
 } 
  
 $.fn.fillSelect = function(data) {
     return this.clearSelect().each(function() {
          if (this.tagName == 'SELECT') {
   	        var dropdownList = this;
              $.each(data, function(index, optionData) {
 			 var option = new Option(optionData.Text, optionData.Value);
                  
                 if ($.browser.msie) {
                     dropdownList.add(option);
                  }
                 else {
                      dropdownList.add(option, null);
                  }
              });
          }
      });
   }
  }