var addressArrayBilling=new Array();
var addressArrayShipping=new Array();


function Address(company, co, id, st1, st2, city, state, country, zip){
	this.company=company;
	this.co=co;
	this.id=id;
	this.st1=st1;
	this.st2=st2;
	this.city=city;
	this.state=state;
	this.zip=zip;
	this.country=country;
	
}


var AddressModel=new Class({
						   
	addressIndex: false, //Currently loaded Address
	selectedCountry: false, //Current Country
	selectedState: false, //Current State
	preFix: false,
	
	initialize: function(ops){
				
		for(var i in ops){			
			eval("this."+i+"=ops[i]");			
		}
		
		
		if(this.preFix){			
			
			this.buildAddressInfo();			
			
		}		
		
		
	},
	
	
	buildAddressInfo: function(){
		this.buildAddressSelect();
		this.fillAddress();
	},
	
	
	buildAddressSelect: function(){
		
		var output="";
			
			
		output="<select id=\""+this.preFix+"_address_select\" name=\""+this.preFix+"_address_select\">\n";
		
		var aArr=new Array();
		eval("aArr=addressArray"+capString(this.preFix));
		
		for(var i=0; i<aArr.length; i++){
			if(aArr[i].st1!="" || aArr[i].st2!=""){
				if(!this.addressIndex || this.addressIndex==i){
					output=output+"<option SELECTED value=\""+aArr[i].id+"\">"+aArr[i].st1+" "+aArr[i].zip+"</option>\n";
				}
				else
					output=output+"<option value=\""+aArr[i].id+"\">"+aArr[i].st1+" "+aArr[i].zip+"</option>\n";
			}
				
				
		}
		
		output=output+"<option  "+(!this.selectedIndex?" SELECTED ":"")+"  value=\"0\">New Address</option></SELECT>\n";
		//alert(type+"  "+output);
		
		$(this.preFix+"_select").innerHTML=output+"<br><br>";
		$(this.preFix+"_address_select").addEvent("change", function(event){	
															 	 this.fillAddress();															 
															 }.bind(this));
		
	},
	
	fillAddress: function(){
		
		var s=$(this.preFix+"_address_select");
		selected=s.options[s.selectedIndex].value;
		
		var Address=this.getAddress(selected);
			
		if(selected!==null && selected!="undefined"){
			$(this.preFix+"_address_co").value=Address.co;
			$(this.preFix+"_address_st1").value=Address.st1;
			$(this.preFix+"_address_st2").value=Address.st2;
			$(this.preFix+"_address_city").value=Address.city;
			$(this.preFix+"_address_zip").value=Address.zip;
		}
		
		var c=new countryStateSelect();
		if(this.preFix=="shipping"){
			c.addEvent('afterStatePopulated', function(){													  
															  updateShippingMethods();												  
													  })					
		}
		
		
		c.makeNew({
				  	curCountry: (Address.country && Address.country!=""?Address.country:"United States"),
					curState: (Address.state && Address.state!=""?Address.state:""),
				  	stateSelectID: this.preFix+"_address_state",
					countrySelectID: this.preFix+"_address_country"
			});	
		
	},
	
	getAddress: function(id){
		
		var aArr=new Array();
		eval("aArr=addressArray"+capString(this.preFix));
		for(var i=0; i<aArr.length; i++){			
			if(aArr[i].id==id)
				return aArr[i];			
		}
		
		return false;
		
	}
	
		  
						  
})


function updateShippingMethods(){
	
	
	var sC=$('shipping_address_country');
	if(sC!=null){
		
		var selectedCountry=sC.options[sC.selectedIndex].value;
		var page="storeViews/ajax/ajax_order_updateShippingMethods.php";	
		
		new Request.JSON({
		
				url: page,
				method: 'post',
				data: {
						'crand': $('crand').value,
						'order_hash': $('order_hash').value,
						'country': selectedCountry
				},
				
				onComplete: function(jsonObj){
					
					JSONHandler(jsonObj);								
					if(! $('shipping_methods_select') )		
						disableCheckout();
					else
						enableCheckout();	
					
				}
				 
		 }).send();
	}
	
}