CountryStateControl = function(ddlCountryId, txtStateId, ddlStateId){
	this._ddlCountry = document.getElementById(ddlCountryId);
	this._txtState = document.getElementById(txtStateId);
	this._ddlState = document.getElementById(ddlStateId);
	this._country == '';
	this._init();
}
CountryStateControl.prototype = {
	_init: function(){
		if(this._ddlCountry == null || this._txtState == null || this._ddlState == null){
			return;
		}
		this._country = this._ddlCountry.value;
		this._render();
		this._bind();
	},
	_bind: function(){
		var self = this;
		
		this._ddlState.onchange = function(){
			self._txtState.value = self._ddlState.value;		
		}
		jQuery(this._ddlCountry).change(function(){
			self._country = self._ddlCountry.value;
			self._render();
		});
	},
	_render: function(){		
		var states = this._get_States();
		this._ddlState.style.display = states.length == 0 ? 'none' : '';
		this._txtState.style.display = states.length == 0 ? '' : 'none';
		this._ddlState.innerHTML = '';	
		
		if(states.length != 0){
			this._ddlState.options.add(new Option('[select one]', '', false, false));
			var state = this._txtState.value;
			for(var i = 0; i < states.length; i++){
				this._ddlState.options.add(new Option(states[i], states[i], false, state == states[i]));
			}
			this._txtState.value = this._ddlState.value;
		}
	},
	_isUSA:  function(country){
		return this._country.toLowerCase() == 'usa';
	},	
	_isCanada: function(country){
		return this._country.toLowerCase() == 'canada';
	},
	_get_States: function(){
		if(this._isUSA()){
			return ["Alaska", "Alabama", "Arkansas", "Arizona", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maryland", "Maine", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming", "Washington DC", "Virgin Islands", "Guam", "Puerto Rico", "American Samoa", "States of Micronesia", "Palau", "Marshall Islands", "N. Mariana Islands"];
		}
		if(this._isCanada()){
			return ["Alberta", "British Columbia", "Manitoba", "New Brunswick", "Newfoundland/Labrador", "Northwest Territories", "Nova Scotia", "Nunavut", "Ontario", "Prince Edward Island", "Quebec", "Saskatchewan", "Yukon"];
		}
		return [];
	}
}

NavigationBar = function(){
	this._navigationBar = null;
	this._current_tab = null;
	this._selected_tab = null;
	
	this._menus = [];
	this._rendered_menus = {};	
	this._menuContainer = null;	
	this._solidBackground = null;
	this._css_menuselected = "nbm-selected";
	this._css_menuunselected = "nbm-unselected";	
}

NavigationBar.prototype = {	
	DEFAULT_WIDTH: 180,
	init: function(navigationBarId, menuContainerId, data){
		this._navigationBar = document.getElementById(navigationBarId);
		this._current_tab = this._get_CurrentTab();
		
		this._menuContainer = document.getElementById(menuContainerId);
		this._menuContainer.className = "nbm-container";
		this._menuContainer.style.display = "none";
				
		this._solidBackground = this._createSolidBackground();
		if(this._solidBackground != null){
			this._menuContainer.appendChild(this._solidBackground);
		}
		this._registerDeserializedObject(data);
	},
	
	show: function(parent, menuId){
		if(this._menuContainer != null){
			if(this._current_tab != null) this._current_tab.className = "tab_unselected";		
			if(this._selected_tab != null) this._selected_tab.className = "tab_unselected";
			this._selected_tab = parent.firstChild.firstChild;
			this._selected_tab.className = "tab_selected";
			
			if(this._menuContainer.childNodes[1]){
				this._menuContainer.removeChild(this._menuContainer.childNodes[1]);
			}			
			if(typeof(this._rendered_menus[menuId]) != "undefined"){
				this._menuContainer.appendChild(this._rendered_menus[menuId]);
			}else{
				this._menuContainer.appendChild(this._build(menuId));
			}
			var position = this._getPosition(parent);
			this._menuContainer.style.left = position.left + "px";	
			this._menuContainer.style.top = (position.top + 36) + "px";					
			this._menuContainer.style.display = "";
			this._calculateWidth();
			this._setSolidBackgroundHeight();
		}
	},
	
	hide: function(event){
		try
		{
			if(this._menuContainer == null){
				return;
			}
			event = event || window.event;
			if((event.toElement && jQuery(event.toElement).hasClass("nbm-handler"))
			   || (event.relatedTarget && jQuery(event.relatedTarget).hasClass("nbm-handler")))
			{
				return;
			}
			this._menuContainer.style.display = "none";
			this._menuContainer.style.width = "auto";
			if(this._current_tab != null) this._current_tab.className = "tab_selected";
			if(this._selected_tab != null && this._selected_tab != this._current_tab ) this._selected_tab.className = "tab_unselected";
		}
		catch(ex)
		{
			//DEBUG:
			//alert(ex);
		}
	},
	
	_calculateWidth: function(){
		this._menuContainer.style.fontWeight = "bold";
		if (jQuery.browser.msie && jQuery.browser.version < 7) {
			this._menuContainer.style.width = "1px";
		}
		var wdt = jQuery(this._menuContainer).width();
		
		this._menuContainer.style.width = (wdt < this.DEFAULT_WIDTH) ? (this.DEFAULT_WIDTH+"px") : (wdt + "px");
		this._menuContainer.style.fontWeight = "normal";
	},
	
	_get_CurrentTab: function(){
		var tabs = jQuery(this._navigationBar).find(".tab_selected");
		return tabs.length > 0 ? tabs[0] : null;
	},	
	
	_getPosition: function(parent) {
		var obj_position = jQuery(parent).position();
		var bar_position = jQuery(this._navigationBar).position();
		if((bar_position.left + this._navigationBar.clientWidth) < (obj_position.left + this.DEFAULT_WIDTH)){
			obj_position.left = bar_position.left + this._navigationBar.clientWidth - (this.DEFAULT_WIDTH + 2);
		}
		return obj_position;
	},
	
	_createSolidBackground: function() {
		//if (jQuery.browser.msie && jQuery.browser.version < 7) {*/
			var frame = document.createElement("IFRAME");
			frame.id = "bgrd" + this._navigationBar.id;
			frame.style.position = "absolute";
			frame.style.width = "100%";
			frame.style.height = "100%";
			frame.style.zIndex = -1;
			frame.src = "javascript:false;";
			frame.frameBorder = 0;			
			return frame;			
		//}
	},
	
	_setSolidBackgroundHeight: function() {
		this._solidBackground.style.height = jQuery(this._menuContainer).height() + "px";
	},	
	_build: function(menuId){
		var self = this;
		var container = document.createElement("div");
		//TODO:		
		for (var i = 0; i < this._menus.length; i++) {
			if(this._menus[i].get_ID() == menuId.toLowerCase()){
				container.className = "nbm-handler nbm-container-body";
				var items = this._menus[i].get_Items();
				for (var j = 0; j < items.length; j++) {			
					container.appendChild(this._createMenuItem(items[j]));					
					container.appendChild(this._createDelimiter());
					if(items[j].get_SubMenu() != null){
						this._buildSubMenu(container, items[j].get_SubMenu());
					}
										
				}	
				container.appendChild(this._createEmptyDiv());
				container.onmouseout = function(event){
					self.hide(event);
				}
				break;
			}
		}
		this._rendered_menus[menuId] = container;
		return container;
	},
	
	
	_buildSubMenu: function(container, menus){		
		var self = this;
		var items = menus.get_Items();
		for (var j = 0; j < items.length; j++) {			
			items[j].set_Title('&nbsp; &nbsp; ' + items[j].get_Title());
			container.appendChild(this._createMenuItem(items[j]));					
			container.appendChild(this._createDelimiter());
								
		}
		container.onmouseout = function(event){
			self.hide(event);
		}		
	},
			
	_createMenuItem: function(obj){
		var self = this;
		var div = document.createElement("div");
		div.innerHTML = '<a class="nbm-handler" style="color: #666666; text-decoration:none;" href="'+ obj.get_Url() +'"><div class="nbm-handler '+ this._css_menuunselected +'">'+ obj.get_Title() +'</div></a>';
		div.firstChild.firstChild.onmouseout = function(event){
			this.className = "nbm-handler " + self._css_menuunselected;
		}
		div.firstChild.firstChild.onmouseover = function(event){
			this.className = "nbm-handler " + self._css_menuselected;
		}
		return div.firstChild;
	},	
	_createDelimiter: function(){
		var div = document.createElement("div");
		div.innerHTML = '<div class="nbm-handler nbm-delimeter"><div class="nbm-handler nbm-delimeter-in"></div></div>';
		return div.firstChild;
	},	
	_createEmptyDiv: function(){
		var div = document.createElement("div");
		div.innerHTML = '<div class="nbm-handler '+ this._css_menuunselected +'">&nbsp;</div>';
		return div.firstChild;
	},	
	_registerDeserializedObject: function(menuInstances){
		for (var i = 0; i < menuInstances.length; i++) {
			this._menus.push(NavigationBarMenu.create(menuInstances[i]));
		}
	}
}

NavigationBarMenu = function(id, items){
	this._id = id.toLowerCase();	
	this._items = this._createItems(items);	
}
NavigationBarMenu.prototype = {
	_createItems: function(items) {
		var result = [];
		for (var i = 0; i < items.length; i++) {
			result.push(NavigationBarMenuItem.create(items[i]));
		}
		return result;
	},
	
	get_ID: function(){
		return this._id;	 
	},	
	get_Items: function(){
	 	return this._items;
	}
}
NavigationBarMenu.create = function(obj){
	return new NavigationBarMenu(obj.ID, obj.Items);
}

NavigationBarMenuItem = function(id, title, url, subMenu){
	this._id = id.toLowerCase();
	this._title = title;
	this._url = url;	
	this._subMenu = subMenu == null ? null : NavigationBarMenu.create(subMenu);
}

NavigationBarMenuItem.prototype = {
	get_ID: function(){
		return this._id;	 
	},	
	get_Title: function(){
	 	return this._title;
	},
	set_Title: function(value){
	 	this._title = value;
	},
	get_Url: function(){
		return this._url;	 
	},
	get_SubMenu: function(){
		return this._subMenu;	 
	}
}
NavigationBarMenuItem.create = function(obj){
	return new NavigationBarMenuItem(obj.ID, obj.Title, obj.Url, obj.SubMenu);
}



LeftMenu = function(){
    this._cur_item = null;
    this._sel_item = null;    
    this._cur_subitem = null;
    this._sel_subitem = null; 
    
    this._exp_blocks = [];   
    
    this._CSS_STATUS = "ExpCollStatus";
    this._CSS_SELECTED = "Selected";
    this._CSS_UNSELECTED = "unSelected";
    this._CSS_SUB_SELECTED = "subMenuSelected";
    this._CSS_SUB_UNSELECTED = "subMenu";
}

LeftMenu.prototype = {
    init: function(){
        this._cur_item = jQuery("." + this._CSS_SELECTED).get(0);
        this._cur_subitem = jQuery("." + this._CSS_SUB_SELECTED).get(0);
        this._sel_item = this._cur_item;
        this._sel_subitem = this._cur_subitem;
        this._initSubMenu();
        this._changeStatus();
        
    },
    select: function(selection){
        if(this._cur_item != null){
           this._cur_item.className = this._CSS_UNSELECTED;        
        }   
        if(this._sel_item != null){
           this._sel_item.className = this._CSS_UNSELECTED;        
        } 
        selection.className = this._CSS_SELECTED;
        this._sel_item = selection;
        this._changeStatus();
    },
    diselect: function(selection){
        selection.className = this._CSS_UNSELECTED;    
        if(this._cur_item != null){
           this._cur_item.className = this._CSS_SELECTED;
        }
        this._changeStatus();
    },    
    click: function(selection){
        this._cur_item = selection;
    },    
    subSelect: function(selection){
        if(this._cur_subitem != null){
            this._cur_subitem.className = this._CSS_SUB_UNSELECTED;        
        } 
        if(this._sel_subitem != null){        
            this._sel_subitem.className = this._CSS_SUB_UNSELECTED;
        }        
        selection.className = this._CSS_SUB_SELECTED;  
        this._sel_subitem = selection;
    },
    
    subDiselect: function(){
        this._sel_subitem.className = this._CSS_SUB_UNSELECTED; 
        if(this._cur_subitem != null){
            this._cur_subitem.className = this._CSS_SUB_SELECTED;    
        }
    },    
    subClick: function(){
    
    },    
    toggle: function(id){             
        jQuery(id).slideToggle("400", function(){ leftMenu._changeStatus();});        
    },
    _changeStatus: function(){
        if(this._exp_blocks.length == 0){
            return;
        }        
        for(var i=0; i < this._exp_blocks.length; i++){
            var isExp = jQuery(this._exp_blocks[i].sub_menu).hasClass("notvisiblemenu") 
                               || (this._exp_blocks[i].sub_menu.style.display == 'none');            
            if(this._exp_blocks[i].className == 'Selected'){                    
                this._exp_blocks[i].icon.src = isExp ? "/common/image/BlackUp.png" : "/common/image/BlackDown.png";
            }else{
                this._exp_blocks[i].icon.src = isExp ? "/common/image/BlackUp.png" : "/common/image/BlackDown.png";
            }    
        }             
    },
    
    _initSubMenu: function(){
        var _exp_blocks = this._exp_blocks;
        jQuery('.' + this._CSS_STATUS).parent().each(
            function(i){
                this.icon = jQuery(this).find('img').get(0);                
                this.sub_menu = jQuery("#"+"SubMenu_"+this.id).get(0);
                _exp_blocks.push(this);
            }
        );
    }    
}

var leftMenu = new LeftMenu();


/*Rotate-Control*/
RotateManager = function(){
	this._fade_duration = 4100;
	this._css_handler = 'rotate-handler';		
	this._containers = jQuery('.rotate-container');		
	this._init();
}

RotateManager.prototype = {
	_init: function(){
		var self = this;
		for(var i = 0; i < this._containers.length; i++){
			this._containers[i].items = jQuery(this._containers[i]).find('.' + this._css_handler);				
			if(this._containers[i].items.length > 0){
				this._containers[i].current_index = 0;
				jQuery(this._containers[i].items[0]).removeClass(this._css_handler);
				this._containers[i].duration = this.getDuration(this._containers[i]);
				this._containers[i].timer = this.start(this._containers[i]);				
				this._containers[i].onmouseover = this.onstop(this._containers[i]);
				this._containers[i].onmouseout = this.onstart(this._containers[i]);
			}else{
				this._containers[i].current_index = -1;
			}
		}
	},
	
	getDuration: function(container){
		var result = parseInt(jQuery(container).attr('duration'));		
		return isNaN(result) ? 3500 : result;
	},
			
	start: function(container){
		var self = this;
		return window.setInterval(function(){ self.rotate(container) }, container.duration);
	},
	
	onstart: function(container){			
		var self = this;
		return function(){ container.timer = window.setInterval(function(){ self.rotate(container) }, container.duration); };
	},
	
	onstop: function(container){			
		return function(){
			window.clearInterval(container.timer);
			//jQuery(container.items[container.current_index]).css({ opacity: 1 });
		};
	},
	
	rotate: function(container){
		var self = this;			
		if(container.current_index != -1 ){
			jQuery(container.items[container.current_index]).addClass(this._css_handler);
			this.next(container);
			jQuery(container.items[container.current_index]).removeClass(this._css_handler);
			//jQuery(container.items[container.current_index]).fadeOut(this._fade_duration, function(){this.style.display = '';});
		}
	},
	
	next: function(container){			
		if(container.current_index != -1 ){
			var next_index = container.current_index + 1;
			if(next_index >= container.items.length){
				next_index = 0;
			}
			container.current_index = next_index;
		}
	}	
}

/*Captcha*/

function RefreshCaptcha(captchaImageId, captchaKey, labelMsgId) {
	var dt = new Date().getTime().toString();
	jQuery.ajax({
		async: true,
		type: "POST",
		url: "/captcha/image.aspx?key="+dt,
		dataType: "html",
		contentType: "text/plain;",
		data: "",
		error: function(xhr, msg) {/*alert("DEBUG: \n" + xhr.responseText);*/},
		success: function(result) {
			document.getElementById(captchaKey).value = result;
			document.getElementById(captchaImageId).style.backgroundImage = 'url(/captcha/image.aspx?id='+result+')';
			if(document.getElementById(labelMsgId) != null){ document.getElementById(labelMsgId).style.display = 'none';}
		}
	});
}


