

// no conflict
$.noConflict();

jQuery(document).ready(function($) {
	 
		// activate png fix
		jQuery(document).pngFix();
		
		// language switcher
		jQuery('#lang_dropdown').change(function() {
  				location = jQuery('#lang_dropdown :selected').val();
		});
		
		
		// language dropdown
		$("#lang_dropdown").msDropDown();
		jQuery('#lang_dropdown_child a').click(function() {
  				location = jQuery('#lang_dropdown :selected').val();
				//alert("test");
		});
		
		// protected downloads
		jQuery('.protected-download').attr('href',jQuery('.protected-download').attr('href')+'?file='+jQuery('.protected-download').html());
				
		jQuery('.protected').each(function(index){
			jQuery(this).attr('href',jQuery(this).attr('href')+'?file='+jQuery(this).html());
		});

		/*
		 * generate download links if needed
		 */
		 
		// first check if ther eis a table
		if (jQuery('.contenttable').length != 0) {
			
			// now check each table if there is a order number in the table
			jQuery('.contenttable').each(function(index){
					// go threw the head row
					jQuery(this).find('th').each(function(colindex){
						// if this col is the order number
						if (jQuery(this).html() == 'Bestell-Nr.' ||
							jQuery(this).html() == 'Bestellnr.' ||
							jQuery(this).html() == 'Bestell- Nr.' ||
							jQuery(this).html() == 'Order Number' ||
							jQuery(this).html() == 'Order no.' ||
							jQuery(this).html() == 'Número dePedido' ||
							jQuery(this).html() == 'No. de Pedido' ||
							jQuery(this).html() == 'N ° de commande' ||
							jQuery(this).html() == "Numero d'ordine" ||
							jQuery(this).html() == 'No. de orden' ||
							jQuery(this).html() == 'Номер заказа.' ||
							jQuery(this).html() == '订货单编号' ) {
							// col found so add links to this table
							addLinkstoTable(jQuery(this).parents('.contenttable'), colindex);
							jQuery(this).css('width','70px');
						}
							   
					});							  
			});
			
			
		}
		
	function addLinkstoTable(table, col) {
		// first add new col to the head if the last one is not empty
		if(jQuery(table).find("thead tr:last").html()!='' || jQuery(table).find("thead tr:last").html()!='&nbsp;') {
			jQuery(table).find("thead tr").append("<th>&nbsp;</th>");
		}
		
		// go through the rows
		jQuery(table).find('tbody tr').each(function(rowindex){
				// if there are no links already
				if (jQuery(this).find('a').length == 0) {
				// get the order  number
				var orderNr = jQuery(this).find(".td-"+col).html();
				var lang = getCurLang();
				// now work with the orderNr to file name
				
				
				var address = window.location.href.split("/");
				//alert(address[0] + "//" + address[2] + "/fileadmin/downloads/public/" + address[3]+"/"+filename);
				var filename= "fileadmin/Downloads/public/" + address[3]+"/"+orderNr.toLowerCase().replace(" ",'') + ".pdf";
				// check if file exist
				var toAdd = jQuery(this);
				jQuery.get("exist.php", { file: filename },
  				 function(data){
					 // if exist add link else request
					if (data == 1) {
						filelink = '<a class="protected" href="'+filename+'">'+orderNr+'</a>';	
					} else {
						filelink = '<a class="protected" href="index.php?id=210&L='+lang+'&file='+orderNr+'">'+orderNr+'</a>';
					}
					if(jQuery(toAdd).find('.td-last').html()!='' && jQuery(toAdd).find('.td-last').html()!='&nbsp;') {
						jQuery(toAdd).append('<td>'+filelink+'</td>');
					} else {
						jQuery(toAdd).find('.td-last').html(filelink)
					}
   				});
				}	
		});
		//jQuery(table +" tr:gt(0)").append("<td>Col</td>");

	}
	
	// super script the r if there is one not superscripted yet
	jQuery('.csc-firstHeader').each(function(index){
			jQuery(this).html(jQuery(this).html().replace('®', "<sup>®</sup>"));
	});
	
	
	function getCurLang() {
		// get the current path name
		var path = location.pathname.split('/');
		// now get the belongign lang id
		switch(path[1])
		{
		case 'en':
  			var id = 1;
		break;
		case 'br':
		  var id = 2;
		break;
		case 'es':
		  var id = 3;
		break;
		case 'ru':
		  var id = 5;
		break;
		case 'cn':
		  var id = 4;
		break;
		case 'fr':
		  var id = 7;
		break;
		case 'it':
		  var id = 6;
		break;
		case 'pl':
		  var id = 8;
		break;
		default:
		   var id = 0;
		}
		return id;
	}
	
	
});



var Validation = function(form,o){ }
    Validation.prototype = { };


