//added js functions from the old Sigma-Aldrich site

function OpenWin(URL, windowattributes) {
	//help=window.open(URL,"Help",'window attributes');
	help=window.open(URL,"Help",windowattributes);
	help.focus();
}

function changeParentURL(url){
	window.opener.location = url;
}

function OpenWindow(URL, windowattributes) {
	    help=window.open(URL,"Help",windowattributes);
	    help.focus();
	}

//SCRIPT FOR A JUMP-TO BOX
      function MM_jumpMenu(targ,selObj,restore){ //v3.0
      eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
      if (restore) selObj.selectedIndex=0;
 }     

//**added for CoreMetrics**//

function getMemberId() {
	var hasWCUserActivityCookie  = document.cookie.indexOf("WC_USERACTIVITY_");
	if (hasWCUserActivityCookie != -1 ){ 
		var startcount2 = hasWCUserActivityCookie + 16;// Start cookie value 
		var endcount2 = document.cookie.indexOf("=", startcount2); // End cookie value 
		if (endcount2 == -1) endcount = document.cookie.length; 
			var MemberID = document.cookie.substring(startcount2, endcount2); //Extract value 
	} 
	else 
	{ 
		MemberID = "Unknown"; 
	}

	if(MemberID == "-1002"){MemberID = "Unknown";}
	return MemberID;
}


function getClientId() {
	var ClientID = "Unknown";
	var userCookie = document.cookie;
 	var hasAnonymousClientIdCookie = userCookie.indexOf("AnonymousClientId="); 
	if (hasAnonymousClientIdCookie != -1 )
		{userCookie = userCookie.substring(hasAnonymousClientIdCookie + 21, userCookie.length);} 
 	var hasClientIdCookie = userCookie.indexOf("ClientId=");
	if (hasClientIdCookie != -1 ){
		var startcount = hasClientIdCookie + 9;
		var endcount = userCookie.indexOf(";", startcount);
			if (endcount == -1) endcount = userCookie.length; 
			{ClientID = userCookie.substring(startcount, endcount);}
	}
	return ClientID;
}

//**added for Bookmark**//

var addthis_pub = "sigmaaldrich";

function writeSocialBookmark() {


}

//**added for Type ahead Search**//
function searchTypeahead(){
	var NewObj = document.getElementById('typeaheadText');
	var text = document.getElementById("N4");
	if (text != null)
	{
		if(text.value.length > 2)
		{
			CreateRequest();
			var url = getCatalogNameFromURL()+ "/SearchTypeahead.do?searchText=" + text.value;
			request.open("GET", url, true);
    			request.onreadystatechange=UpdateTypeahead;
    			request.send(null);
		}
	}
}

function CreateRequest(){
	try{
		request = new XMLHttpRequest();
	}
	catch(trymicrosoft){
		try{
			request = newActiveXObject("Msxml2.XMLHTTP");
		}
		catch(othermicrosoft){
			try{
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(failed){
				request = null;
			}
	     	}	
	}
}

function UpdateTypeahead(){
	if (request.readyState == 4){
		var NewData = request.responseText;
		var NewObj = document.getElementById("typeaheadText");
		NewObj.innerHTML = NewData;
		NewObj.style.display='';
	}
}	

function typeaheadLink(name,id){
	document.regularSearch.N25.value = id;
	var typeahead = document.getElementById('typeaheadText');
	typeahead.style.display='none';
	submitRegSearch();
}

function getCatalogNameFromURL(){
    //use catalog as context path if failed
    var url = '/catalog';
    try{
		var path = location.pathname;
			 
		if(path.indexOf("/")==0){
			  var len = path.length;
			  path = path.substring(1, len);
			      
		      if(path.indexOf("/")>=0){
			        var index = path.indexOf("/");
			        url = "/" + path.substring(0,index);
		      }else{		      		
			     	url = "/" + path;
			  }
		}			
			  
    }catch(err){}		
	return url;
}

function ClickHereToPrint(){
				 if (document.getElementById('ThePrintableContent') != null)
				 {
					 var content=document.getElementById('ThePrintableContent').innerHTML;
	  				 var pwin=window.open('','print_content','width=800,height=800');
					 pwin.document.open();
					 pwin.document.write('<html>');
			     	 pwin.document.write('<link media=\"all\" type=\"text/css\" href=\"/etc/medialib/sigma-aldrich/headers/endeca-search-and/searchcss.Par.0001.File.tmp/search.css\" rel=\"stylesheet\" />');
			 		 pwin.document.write('<body onload="window.print()">'+content+'</body></html>');
					 pwin.document.close();
	 				 setTimeout(function(){pwin.close();},1000);
	 			}
	 			else
	 			{
	 				window.print();
	 			}
			}
