$j = jQuery.noConflict();

function erase(event) {
	event.target.value = '';
	event.target.stopObserving('click');
}

document.observe('dom:loaded', function() {
	$$('.erasable').each(function(element) {
		element.observe('click', erase);
	});	
});

function closePopup() {
	$j('#overlay').css('display','none');
	$j('#identification_form').remove();
}

function showOptions(countries, selected, stateidform) {
	var code;
	
	switch (selected) {
		case 'CANADA':
			code = 'CA';
			break;
		case 'UNITED STATES':
			code = 'US';
			break;
		
		default:
			$j('#' + stateidform).html('<option value=""></option>');
			$j('#tr' + stateidform).css('display', 'none');
			return false;
	}

	var options = '';

	$j('#' + stateidform).html('<option value=""></option>');
	$j.each(countries, function(index, value) {
		if (value && value[1] == code) {
			$j('#' + stateidform).append('<option value="' + value[0] + '">' + value[2] + '</option>');
		}
	});
	$j('#tr' + stateidform).css('display', 'table-row');
}

function countriesStates(countriesFormId, statesFormId) {
	var countries = [];

	if ($j('#' + countriesFormId).length != 0) {
		$j('#' + statesFormId + ' option').each(function() {
			var name = $j(this).html();
			
			countries.push(/([A-Z]+)_([A-Z0-9]+)/.exec(name));
		});

		showOptions(countries, $j('#' + countriesFormId).find('option:selected').html(), statesFormId);
		$j('#' + countriesFormId).change(function() {
			showOptions(countries, $j('#' + countriesFormId).find('option:selected').html(), statesFormId);
		});
	}
}

$j(function() {
	if (document.domain.substr(0, 3) != 'www') {
		document.location.href = document.location.href.replace('://', '://www.');
	}
	$j('.languages').mouseover(function() {
		$j('.listLang').css('display','block');
	}).mouseout(function() {
		$j('.listLang').css('display','none');
	});
	
	/*
	$j('a').click(function(event) {
		var href = $j(this).attr('href'); */
		// if (/^\/documents|userfiles\.*/.test(href)) {
		/*	event.preventDefault();
			$j.post('/documents/check.php', {filename : href}, function(data) {
				if (data == 'valid') {
					document.location.href = href;
				} else {
					$j('#overlay').css('display','block');
					
					$j('html').scrollTop(0);
					$j('body').prepend('<div id="identification_form"><iframe src="' + data + '" width="710" height="365" frameborder="0" scrolling="no"></iframe></div>');
				}
			});
		} 
	});
	*/
	
	$j('#closeBtn,#overlay').click(function() {
		parent.closePopup();
	});
		
	countriesStates('idform11044e244831af7f5', 'idform58094e244831af814');
	countriesStates('idform36644d9c72cd1b4cd', 'idform9344e26ca8b932ca');
	countriesStates('idform49324d42d2a7eb853', 'idform36424e26cadbbeb4f');
});
