var locations = {

            s : Object,

            wS : Array,

            ctr : 0,

            grpLable : null,

            optiongrp : Object,

            opt : Boolean,

            us_west : ["Alaska","California","Colorado","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Michigan","Minnesota","Missouri","Montana","Nebraska","Nevada","New Mexico","North Dakota","Ohio","Oklahoma","Oregon","South Dakota","Texas","Utah","Washington","Wisconsin","Wyoming"],

            us_east : ["Alabama","Arkansas","Connecticut","Delaware","Florida","Georgia","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Mississippi","New Hampshire","New Jersey","North Carolina","Pennsylvania","Rhode Island","South Carolina","Tennessee","Vermont","Virginia","West Virginia"],

            canada_west : ["British Columbia","Prairies","Alberta","Saskatchewan","Manitoba","Yukon","Northwest Territories"],

            canada_east : ["Ontario","Quebec","Maritimes","New Brunswick","Prince Edward Island","Nova Scotia","Newfoundland and Labrador","Nunavut"],

            init : function(loc) {

                        try{

                                    document.getElementById('states').innerHTML = '';

                                    this.ctr = 0; 

                                    this.s = document.createElement('select');
                                    
                                    this.s.setAttribute("id","dSelect");
                                    
                                    this.s.setAttribute("class","input");
                                    
                                    this.s.setAttribute("style","background-color: #CCCCCC;");
                                    
                                    this.s.style.width = "200px";
                                    
                                    switch(loc){

                                                default:

                                                case 'no_location':

                                                            this.wS = 'no_location';   

                                                break;

                                                case 'US-West':

                                                            this.wS = this.us_west;

                                                break;

                                                case 'US-East':

                                                            this.wS = this.us_east;

                                                break;

                                                case 'Canada-West':

                                                            this.wS = this.canada_west;

                                                            

                                                break;

                                                case 'Canada-East':

                                                            this.wS = this.canada_east;

                                                break;

                                    }

                                    this.traverse(this.wS);

                        }

                        catch(err){

                                    alert(err);

                        }

            },

            traverse : function(objArray) { //alert(objArray);

                        for (var x in objArray) {

                                    if(typeof(objArray[parseInt(x) + 1]) == "object") { 

                                                this.grpLabel = objArray[x];

                                                continue;

                                    }

                                    if(typeof(objArray[x]) == "object"){

                                                this.traverse(objArray[x]);

                                    }

                                    else { 

                                                if(this.ctr==0 && this.grpLabel != null) {

                                                            this.optiongrp = document.createElement('optgroup');

                                                            this.optiongrp.setAttribute('label', this.grpLabel);

                                                            this.opt = true;   

                                                }

                                                

                                                option = document.createElement('option')

                                                option.value = objArray[x];

                                                if(document.getElementById('hdnState').value == objArray[x]) { option.setAttribute('selected', 'true') }

                                                option.appendChild(document.createTextNode(objArray[x]))

                                                this.ctr++; 

                                                arrLen = objArray.length;

                                                

                                                if(this.ctr < arrLen && this.opt == true) { //alert('option')

                                                            this.optiongrp.appendChild(option); 

                                                }           

                                                else if(this.ctr < arrLen) { 

                                                            this.s.appendChild(option);

                                                }

                                                if(this.ctr == arrLen && this.opt == true) {

                                                            this.optiongrp.appendChild(option); 

                                                            this.s.appendChild(this.optiongrp);

                                                            this.opt = false;

                                                            this.ctr = 0; 

                                                }

                                                

                                                if(this.opt == false && this.grpLabel == null) {

                                                            this.s.appendChild(option);

                                                }

                                    }

                        }

                        document.getElementById('states').appendChild(this.s);

                        //this.s.removeChild(this.optiongrp);

            }

}

function changeStateLocation(loc) { 

            locations.init(loc);

}
