
function prepareTabs(){
	var activeTab;

	if (location.hash.substring(1)) {
		activeTab = location.hash.substring(1);
	} else {
		activeTab = $('div.tabContent:first').attr('id');
	}
	$('.tab a').click(function(){
		showTab(this);
		return false;
	});

	//works only if there is an image map with id "worldmap"
	//for "distributors" page
	$('#worldmap area').click(function(){
		showTab(this);
		return false;
	});

	$('.tab a').each(function(i){
		if(this.hash.substring(1) == activeTab){
			showTab(this);
		}
	});
	$('.tabContent h2').attr('style', 'display:none;');
}

function showTab(el){
	if(!el.hash.substring(1)) return;

	//location.hash = el.hash;
	$('.tab').removeClass('active');
	$('.tab a').each(function(i){
		if (this.hash == el.hash) {
			$(this).parent().addClass('active');
		}
	});
	
	$('.tabContent').hide();
	$('#' + el.hash.substring(1)).show();
}

function checkForm(f){
	formErrors = false;

	if (f.topic_content && f.topic_content.value.length < 2) {
		formErrors = "Enter message";
		f.topic_content.focus();
	}

	if (f.post_content && f.post_content.value.length < 2) {
		formErrors = "Enter message";
		f.post_content.focus();
	}

	if (f.email && !f.email.value) {
		formErrors = "Enter email";
		f.email.focus();
	}

	if (f.topic_title && !f.topic_title.value) {
		formErrors = "Enter topic";
		f.topic_title.focus();
	}

	if (f.topic_username && !f.topic_username.value) {
		formErrors = "Enter your name";
		f.topic_username.focus();
	}

	if (f.post_username && !f.post_username.value) {
		formErrors = "Enter your name";
		f.post_username.focus();
	}

	if (formErrors){
		alert(formErrors);
		return false;
	}else{
		return true;
	}
}

function openApplet(url,name,w,h) {
	window.open(url, name, "scrollbars=no,left=20,top=20,width="+ w+ ",height="+ h);
	return false;
}

/*===========================================================================*/

function evaluateFormController(rules) {
	this.rules = rules;
	this.os_store = new Array();
	this.builds_store = new Array();

	if (!$('#anylogic6_evaluate_form').length) {
		return;
	}

	this.startBuildsController();
	this.startOsController();

	var _purpose = $('input[@name="purpose"][@checked]').attr('value');
	if (!_purpose) {
	//if (!_purpose || _purpose == 'evaluation') {
		$('input[@name="purpose"][@value="' + _purpose +'"]').attr('checked', 'checked');
		this.showAllBuilds();
	}
}

evaluateFormController.prototype = {

	startBuildsController: function(){
		var me = this;
		var select = document.getElementById('build');

		// Deleting builds, which have no links (no OS in Rules list)
		for (var i=0; i<select.options.length; i++) {
			if(!me.rules[select.options[i].value].length){
				select.options[i] = null;
				i--;
			}
		}
		this.storeBuilds();

		$('input[@name="purpose"]').click(function(){
			me.showAllBuilds();
		});
	},

	storeBuilds: function (){
		var me = this;
		$('#build option').each(function(i){
			me.builds_store.push({
				value: $(this).attr('value'),
				title: $(this).html()
			});
		});
	},

	showAdvancedBuild: function(){
		var build = document.getElementById('build');
		build.options.length = 0;
		for(var i=0; i < this.builds_store.length; i++){
			var option = this.builds_store[i];
			if (option.value == 'adv') {
				build.options[i] = new Option(option.title, option.value);
			}
		}
		this.updateOs(build);
	},

	showAllBuilds: function(){
		var build = document.getElementById('build');
		build.options.length = 0;
		for(var i=0; i < this.builds_store.length; i++){
			var option = this.builds_store[i];
			build.options[i] = new Option(option.title, option.value);
		}
		this.updateOs(build);
	},

	startOsController: function(){
		var build = document.getElementById('build');
		var me = this;

		this.storeOs();
		build.onchange = function(){
			me.updateOs(this);
		}
		this.updateOs(build);
	},

	storeOs: function (){
		var me = this;
		$('#os option').each(function(i){
			me.os_store.push({
				value: $(this).attr('value'),
				title: $(this).html()
			});
		});
	},

	updateOs: function(build){
		var me = this;
		document.getElementById('os').options.length = 0;
		if(!build.value) {
			return;
		}
		this.populateOsSelect(this.rules[build.value]);
	},

	populateOsSelect: function(items){
		var me = this;
		for(var i=0; i < items.length; i++){
			for(var j=0; j < this.os_store.length; j++){
				var option = this.os_store[j];
				if (option.value == items[i]) {
					document.getElementById('os').options[i] = new Option(option.title, option.value);
				}
			}
		}
	}
}


/*===========================================================================*/

function prepareAppletLauncher(){
	$('a.appletLauncher').click(function(){
		var optionsStr = "scrollbars=no,left=20,top=20";
		optionsStr += setAppletWidth($(this).attr('href'));
		optionsStr += setAppletHeight($(this).attr('href'));

		window.open($(this).attr('href'), 'Model', optionsStr);
		return false;
	});
}

function setAppletWidth(hrefStr){
	var regex  = new RegExp("width=([0-9]+)", "i");
	var width = regex.exec(hrefStr);
	if (!width || !width[1]) {
		return '';
	}
	return ',width=' + width[1];
}

function setAppletHeight(hrefStr){
	var regex  = new RegExp("height=([0-9]+)", "i");
	var height = regex.exec(hrefStr);
	if (!height || !height[1]) {
		return '';
	}
	return ',height=' + height[1];
}
