// JavaScript Document

if (navigator.userAgent.indexOf('Opera') != -1 ) {
document.write('<link rel="stylesheet" type="text/css" href="/css/operahack.css">');
}

if (navigator.userAgent.indexOf("Safari") != -1) {
document.write('<link rel="stylesheet" type="text/css" href="/css/safarihack.css">');
}

function displayTab(obj){
	
	for (i=1;i<5;i++){
		document.getElementById('tab'+i).style.display="none";
		document.getElementById('tabs'+i).className='';
	}

	document.getElementById('tab'+obj).style.display="block";
	document.getElementById('tabs'+obj).className='activeTab';

}


function get_val(id) {
	if (document.getElementById(id)) return document.getElementById(id).value;
	else return "";
}

function load_selector(id) {
	var xscript=document.createElement("SCRIPT");
	xscript.src="/_includes/mods/cpbourg/dealers/load.php?last="+id+"&con="+get_val("select_con")+"&cou="+get_val("select_cou")+"&sta="+get_val("select_sta");
	document.body.appendChild(xscript);
}

var current_dealer=-9;

function open_dealer(id) {
	//CLOSE
	if (current_dealer!=-9) {
		$("dealer_opener_"+current_dealer).style.display="block";
		$("dealer_details_"+current_dealer).style.display="none";
	}
	//OPEN
	$("dealer_opener_"+id).style.display="none";
	$("dealer_details_"+id).style.display="block";
	current_dealer=id;
}

$j = jQuery.noConflict();

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

var file;

function redirectToFile() {
	if (file != undefined) {
		document.location.href = file;
	} else {
		closePopup();
	}
}

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 openDownloadPopup(href) {
	file = href;
	
	$j.post('/documents/check.php', {filename : href}, function(data) {
		if (data == 'valid') {
			document.location.href = href;
		} else {
			// Display login form
			$j('#overlay').css('display','block');
			
			$j('html').scrollTop(0);
			$j('body').prepend('<div id="identification_form"><iframe src="' + data + '" width="800" height="365" frameborder="0" scrolling="no"></iframe></div>');
		}
	});
}

$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();
			openDownloadPopup(href);
		} 
	});
	
	$j('#closeBtn,#overlay').click(function() {
		parent.closePopup();
	});
		
	var countries = [];
	
	if ($j('#idform79684da876ae74424').length != 0) {
		$j('#idform18684e01d948e0007 option').each(function() {
			var name = $j(this).html();
			
			countries.push(/([A-Z]+)_([A-Z0-9]+)/.exec(name));
		});

		showOptions(countries, $j('option:selected', this).html(), 'idform18684e01d948e0007');
		$j('#idform79684da876ae74424').change(function() {
			showOptions(countries, $j('option:selected', this).html(), 'idform18684e01d948e0007');
		});
	}
	
	if ($j('#idform41504b1fc11495a20').length != 0) {
		$j('#idform16644e01f57089c81 option').each(function() {
			var name = $j(this).html();
			
			countries.push(/([A-Z]+)_([A-Z0-9]+)/.exec(name));
		});
		
		showOptions(countries, $j('option:selected', this).html(), 'idform16644e01f57089c81');
		$j('#idform41504b1fc11495a20').change(function() {
			showOptions(countries, $j('option:selected', this).html(), 'idform16644e01f57089c81');
		});
	}
	
	if ($j('#idform18224e01f90e74d32').length != 0) {
		$j('#idform42024e01f90e74d0f option').each(function() {
			var name = $j(this).html();
			
			countries.push(/([A-Z]+)_([A-Z0-9]+)/.exec(name));
		});
		
		showOptions(countries, $j('option:selected', this).html(), 'idform42024e01f90e74d0f');
		$j('#idform18224e01f90e74d32').change(function() {
			showOptions(countries, $j('option:selected', this).html(), 'idform42024e01f90e74d0f');
		});
	}
});
