
if(!cs2) {
	var cs2 = jQuery.noConflict();
}

if(!cs2ident) {
	var cs2ident = window.name;
}

cs2.ajaxSetup({
	timeout: 60000
});

/* Load Event fires when the whole page is loaded, included all images */
	cs2(function() {
		cs2(window).load(function () {
			if(cs2('[id^=logoutbutton]')) {
				cs2('[id^=logoutbutton]').addClass('handpoint');
				cs2('[id^=logoutbutton]').bind("click", function(e) {					
					if(urlloginbox.indexOf('http') < 0){
						urlloginbox = window.location.protocol + '//' + window.location.hostname + '/' + urlloginbox;
					}
					cs2.ajax({
						url: urlloginbox, 
						data: "&cugid=LOGOUTUSER",
						dataType: "script",
						cache: false,
						async: false
					});					
				});		
			}
			
		});
		cs2(document).ready(function(){
			
			
			cs2('div[class=myoption]','#mycountryoptions').each(function(i,obj) {
						var myselected = cs2(obj).attr('selected');
						if(myselected)
							var opt = '<option value="'+cs2(obj).attr('value')+'" selected='+myselected+'>'+cs2(obj).html()+'</option>';
						else 	var opt = '<option value="'+cs2(obj).attr('value')+'" >'+cs2(obj).html()+'</option>';							
						cs2(opt).appendTo('#mycountryselect');
			});
			cs2('#selectwrap').wrap('<form name="cform" method="post"></form>');
			cs2('#mycountryselect').change(function() { cs2('form[name=cform]').submit() });
			
			if(window.name=='content' || !window.name) {
				window.name=Math.floor(Math.random()*1111111).toString();
			}
			cs2ident = window.name;
			var data1="&cs2ident="+window.name+"&code=autocomplete";
			cs2("#newSEARCH").autocomplete({
				source:"index.php?eID=cs2smdnavi"+data1,
				minLength: 3,
				select: function(event,ui) {
					cs2("#newSEARCH").val(ui.item.value);
					//cs2("#topsearchform").submit();
					sendMainSearch();
				}
			});
			cs2("#newSEARCHbutton").click(function(){
				sendMainSearch();
			});	
			cs2("#newSEARCH").keydown(function(e){
	   			keynum = getkey(e);
	   			if(keynum==13) {				
					sendMainSearch();
				}
			});	
			cs2("#newproducts").click(function(){
				InitIfNecessary('1');
			});	
			InitIfNecessary('0');
		});
	});

function sendMainSearch() {
	// http://www.cs2.ch/support/view.php?id=29070
	//if(cs2("#newSEARCH").val() == 'protein name, catalog number, ... one word is enough!' || cs2("#newSEARCH").val() == '')
		//return false;
	var data1="&cs2ident="+window.name+"&code=addSearch&cs2newSEARCH="+cs2("#newSEARCH").val();
	cs2.ajax({
		url:"index.php?eID=cs2smdnavi", 
		cache: false,
		data: data1,
		dataType: "script",
		async: false
	});
	//pardot search string sending
	var protocol1 = (("https:" == document.location.protocol) ? "https://" : "http://");
	var pardot_search_url = protocol1+"go.pardot.com/s/2472/e4ac0b10d356704611a72fcf8e22f86e?newSEARCH="+cs2("#newSEARCH").val();  

	if (jQuery.browser.msie && window.XDomainRequest) {
      // Use Microsoft XDR
      var xdr = new XDomainRequest();
      xdr.open("get", pardot_search_url);
      xdr.onload = function() {
          // XDomainRequest doesnt provide responseXml, so if you need it:
          var dom = new ActiveXObject("Microsoft.XMLDOM");
          dom.async = true;
          dom.loadXML(xdr.responseText);
      };
      xdr.send();
   } else {
		cs2.ajax({			
			url: pardot_search_url, 
			cache: false,
			async: true
		});	
	}
}

function InitIfNecessary(force) {
	var data1="&cs2ident="+window.name+"&code=InitIfNecessary&cs2_actual_pid="+cs2_actual_pid+"&force="+force;
	cs2.ajax({
		url:"index.php?eID=cs2smdnavi", 
		cache: false,
		data: data1,
		dataType: "script",
		async: false
	});
}

