DEBUG = true;

$(document).ready( function(){

	// set all forms to submit on enter
	$("form").keypress( function(e){
		if( e.keyCode == 13 ){
			$(this).submit();
			return false;
		}
	});

	// enable language pulldown
	$('#LanguageSelector').change( function(){
		document.location.href = GXPage.Self + '/' + GXPage.Page + '?GXLang=' + $(this).val();
	});

	// msds/tds search button
	$('#SubmitProductList').click( function(){
		$('#ProductList').get(0).submit();
		return false;
	});
	// msds/tds clear filter button
	$('#ClearFilter').click( function(){
		GXPage.go(GXPage.Page);
		return false;
	});

	// enable download tds links
	$('a[id]:contains("TDS")').click( function(){
		downloadProduct($(this).attr('id'),'ID','TDS');
		return false;
	});
	// enable download msds links
	$('a[id]:contains("MSDS")').click( function(){
		downloadProduct($(this).attr('id'),'ID','MSDS');
		return false;
	});
	// enable enter key in DownloadForm
	$('#DownloadForm').submit( function(){
		// reset search key field
		$('input[ name = "key" ]').css('border','');
		// get the search key
		var key = $('input[ name = "key" ]').val();
		var doctype = $('input[ name = "doctype" ]:eq(0)').is(':checked') ? 'MSDS' : 'TDS';
		if( !key || !doctype ){
			$('input[ name = "key" ]').css('border','2px red solid');
			$('input[ name = "key" ]').get(0).focus();
			return false;
		}
		// check for it
		$.get(
			GXPage.Root + 'Tech/CheckDownload?key=' + key + '&keytype=Num&doctype=' + doctype,
			function(rxml){
				if($('ERROR', rxml)[0]){
					alert($('ERROR', rxml).text());
					return false;
				}
				downloadProduct(key, 'Num', doctype);
			}
		);
		return false;
	});
	// enable search download button
	$('#DownloadFormSubmit').click( function(){
		$('#DownloadForm').trigger('submit');
		return false;
	});

	// setup AdminBlock to toggle visibility
	$('#CopyrightLine').click( function(){
		$('div.AdminBlock').toggle();
	});
	// set error list to toggle and indicator to change
	$('span.Indicator a').click( function(){
		//e.preventDefault();
		if( $(this).html() == '[+]' ){
			$(this).html('[-]');
		}else{
			$(this).html('[+]');
		}
		$('div.AdminBlock .GXErrors .List').toggle();
	});
	// set username to have help text
	if( $('#LoginForm input[type="text"]').val() == '' ){
		$('#LoginForm input[type="text"]').val('Username');
	}
	// set the username help text behavior
	$('#LoginForm input[type="text"]').focus( function(){
		if( $(this).val() == 'Username' ){
			$(this).val('');
		}
	});
	$('#LoginForm input[type="text"]').blur( function(){
		if( $(this).val() == '' ){
			$(this).val('Username');
		}
	});

});

function downloadProduct(ProdID, KeyType, DocType){
	// get rid of the existing iframe if it exists
	$('#DownloadDoc').remove();
	URL = GXPage.Root + 'Tech/Download?key=' + ProdID + '&keytype=' + KeyType + '&doctype=' + DocType;
	// create the iframe ... safari will not load this correctly if display is set to none
	$('body').append( '<iframe id="DownloadDoc" style="position: absolute; top: -1000px; left: -1000px;"></iframe>' );
	// set the source to the URL
	$('#DownloadDoc').attr('src',URL);
	return false;
}
