var delay_left = 2000;
var delay_right = 2200;
var counter_left;
var counter_right;

function picture(number, logo) {
	this[number] = new Image;
	this.src = logo;
}
function change_left(which) {
    try {
        if ( which == (banner_left.length - 1) ) counter_left = 0;
        else counter_left = which + 1;
        document.images["picture_banner_left"].src = banner_left[which].src;
  
        setTimeout("change_left(counter_left);",delay_left);
    } catch (e) {};
}
function change_right(which) {
    try {
        if ( which == (banner_right.length - 1) ) counter_right = 0;
    	else counter_right = which + 1;
    	document.images["picture_banner_right"].src = banner_right[which].src;
  
    	setTimeout("change_right(counter_right);",delay_right);
    } catch (e) {};
}

var download_bcgOpacity = 0.4;

function openDownloadWindowGF(type, model, id, queryString) {
	$.ajaxSetup({cache: false});
	
	$('select, object, embed').css({visibility: 'hidden'});

	var arrayPageSize = $.fn.lightbox.getPageSize();
	$("#download_bcg").hide().css({width: '100%', height: arrayPageSize[1]+'px', opacity : download_bcgOpacity}).fadeIn();

	var arrayPageScroll = $.fn.lightbox.getPageScroll();
	var download_containerTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
	var download_containerLeft = arrayPageScroll[0];
	$('#download_container')
		.css('top', download_containerTop+'px')
		.css('left', download_containerLeft+'px')
		.show();

	$('#systemWorking').show();
	if (queryString == undefined) {
		queryString = {}
	}
	$.get("/download/" + type + "/" + model + "/" + id + "/", queryString, function(data){
		$('#systemWorking').hide();
		$("#download_window_content").html(data);
	});

}

function openDownloadWindow(type, id, queryString) {
	$.ajaxSetup({cache: false});
	
	$('select, object, embed').css({visibility: 'hidden'});

	var arrayPageSize = $.fn.lightbox.getPageSize();
	$("#download_bcg").hide().css({width: '100%', height: arrayPageSize[1]+'px', opacity : download_bcgOpacity}).fadeIn();

	var arrayPageScroll = $.fn.lightbox.getPageScroll();
	var download_containerTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
	var download_containerLeft = arrayPageScroll[0];
	$('#download_container')
		.css('top', download_containerTop+'px')
		.css('left', download_containerLeft+'px')
		.show();

	$('#systemWorking').show();
	if (queryString == undefined) {
		queryString = {}
	}
	$.get("/download/" + type + "/" + id + "/", queryString, function(data){
		$('#systemWorking').hide();
		$("#download_window_content").html(data);
	});

}

function closeWindow() {
	$('#download_bcg').fadeOut(1000);
	$('#download_container').fadeOut(1000);
	$('select, object, embed').css({visibility: 'visible'});
	$.ajaxSetup({cache: true});
}

function companytype(obj){
	var other_obj = $('#id_company_type_1');
	if (obj.id == 'id_company_type_0_2') {
		if (obj.checked) {
            other_obj[0].disabled = false;
        } else {
			other_obj[0].disabled = true;
			other_obj.value = ''
        }
	};
}

$(function() {
    $('#id_company_type_0_2').each(function() {
        if (!this.checked) $('#id_company_type_1').attr('disabled','disabled');
    });
});

function countryselect(obj){
	var selected_country = obj.options[obj.selectedIndex].value;
	$.post("/account/ajax/country-states/", {country:selected_country} ,
		function(data){
			eval("var country_states=(" + data + ")");
			if (country_states.length > 0 ) {
				var state = $('#id_company_state')[0];
				var parents = state.parentNode;
				parents.removeChild(state);

				var myselect = document.createElement("select");
				myselect.id = 'id_company_state';
				myselect.name = 'company_state';
				myselect.display = 'block';

				var i = 0;

				for (i = 0; i < country_states.length; i++) {
					myselect.options[i] = new Option(country_states[i][1], country_states[i][0]);
				};
				parents.appendChild(myselect);
			} else {
				var state = $('#id_company_state')[0];
				var parents = state.parentNode;
				parents.removeChild(state);

				var myinput = document.createElement("input");
				myinput.id = 'id_company_state';
				myinput.name = 'company_state';
				myinput.display = 'block';
				parents.appendChild(myinput);
			};
		}
	);
	if(selected_country == 'MX' || selected_country == 'CA' || selected_country == 'US'){
		$('#id_last_year_sales').html('(US$)');
		$('#id_this_year_sales_est').html('(US$)');		
	}
	else{
		$('#id_last_year_sales').html('(€)');
		$('#id_this_year_sales_est').html('(€)');		
	};
};

function submit_search() {
	search_size = document.search.search_text.value.length;
    if (search_size > 2) {
    	document.search.submit();
    } else {
		alert('You must provide at least three characters');
	}
};

function TopFormsInit() {
	if (document.getElementById('LoginLink') != null) {
		document.getElementById('LoginLink').innerHTML = '<a href="#" onclick="TopLogin()">' + login_link + '</a>';
	}
	document.getElementById('SearchLink').innerHTML = '<a href="#" onclick="TopSearch()">' + search_link + '</a>';
	document.getElementById('topLogin').style.display = 'none';
};
function TopLogin() {
	document.getElementById('SearchLink').style.display = 'block';
	document.getElementById('topLogin').style.display = 'block';
	document.getElementById('topSearch').style.display = 'none';
	document.getElementById('LoginLink').style.display = 'none';
};
function TopSearch() {
	document.getElementById('SearchLink').style.display = 'none';
	document.getElementById('topLogin').style.display = 'none';
	document.getElementById('topSearch').style.display = 'block';
	document.getElementById('LoginLink').style.display = '';
};

function blanksearch() {
	if (document.getElementById('g').value == '') {
	 	alert('Please specify some words or valid user names to search on.'+"\n"+' There were no matches for those that you specified.');
		return false;
	} else {
		document.forms['searchfaq'].submit();
		return true;
	}
};

function openPreviewWindow() {	
	
	var arrayPageSize = $.fn.lightbox.getPageSize();	
	$("#download_bcg").hide().css({width: '100%', height: arrayPageSize[1]+'px', opacity : download_bcgOpacity}).fadeIn();

	var arrayPageScroll = $.fn.lightbox.getPageScroll();
	var download_containerTop = arrayPageScroll[1] + (arrayPageSize[3] / 10);
	var download_containerLeft = arrayPageScroll[0];
	$('#download_container')
		.css('top', '300px')
		.css('left', download_containerLeft+'px')
		.show();	
	$('#preview_partner_box').show();		
}
