var sel_field_name = '';
var limitValue = '';

function setFieldname(field_nm) {
   sel_field_name=field_nm
}

function InsertAnd(){
document.forms[0].query.value=document.forms[0].query.value+" AND ";
document.forms[0].query.focus();
}

function InsertOR(){
document.forms[0].query.value=document.forms[0].query.value+" OR ";
document.forms[0].query.focus();
}
function clearall(){
document.forms[0].query.value="";
document.forms[0].query.focus();
}
function getResult(){
	if(document.forms[0].query.value=='')
	{
		alert('Please Enter Search term');
		document.forms[0].query.focus();
	}
	else{

		document.forms[0].action="Limited_search.do";
		document.forms[0].submit();
	}
}
function searchGene(gene){
	document.forms[0].query.value="\""+gene+"\""
	document.forms[0].action="Limited_search.do";
	document.forms[0].submit();

}
function moreGene(limit){
//	document.forms[0].query.value="\""+gene+"\""

	setFieldname('top_gene');
	setDivVisible('top_gene_progress','visible');

	var url = "actionType=getTopGene&gene_limit="+escape(limit);

   	retrieveURL(url,"getTopGene.do");
	//ocument.forms[0].action="Limited_search.do?gene_limit="+escape(limit);
	//document.forms[0].submit();

}
function getStats(div,cat){
//	document.forms[0].query.value="\""+gene+"\""

	setFieldname(div);
	setDivVisible(div+'_progress','visible');
	var url = "actionType=getStats&stats_set="+escape(cat);
   	retrieveURL(url,"getStats.do");

	//ocument.forms[0].action="Limited_search.do?gene_limit="+escape(limit);
	//document.forms[0].submit();

}
function getStatsPie(div,cat){
//	document.forms[0].query.value="\""+gene+"\""

	setFieldname(div);
	setDivVisible(div+'_progress','visible');
	var url = "actionType=getPie&stats_set="+escape(cat);
   	retrieveURL(url,"getPie.do");

	//ocument.forms[0].action="Limited_search.do?gene_limit="+escape(limit);
	//document.forms[0].submit();

}
/*function searchbyType(subsearch){
document.forms[0].action="limited.do?actionType=query&subsearch="+subsearch;
document.forms[0].submit();
}*/

function subSearch(subsearch){
setFieldname('content');
	document.getElementById('content').innerHTML = "<img align='center' src='images/Search/searching.gif'>"
document.getElementById('subcontent').innerHTML = ''

var url = "actionType=subSearch&subsearch="+subsearch;
var search="<%=query %>";
search=search+" >> "+subsearch;
document.getElementById('SearchQuery').innerHTML = search
retrieveURL(url,"limited.do");
}


function Limitedsubbrowse(lowerLimit) {
	setFieldname('subcontent');

   	if(limitValue == ''){
	    limitValue = '200';
	 }
	var url = "actionType=BrowseSubSearch" + "&lowerLimit=" + encodeSpecial(lowerLimit);

   	retrieveURL(url,"limited.do");
}

function linkTarget(link){
document.forms[0].action=link;
window.open(link,'Molecular Connections','')
//document.forms[0].submit();

}
function getSubDetails(key,subsearch){
	document.getElementById('subcontent').innerHTML = "<img align='center' src='images/Search/searching.gif'>"
	setFieldname('subcontent');

	var search="<%=query %>";;
search=search+" >> "+subsearch+" >> "+key;

document.getElementById('SearchQuery').innerHTML = search
	var url = "actionType=subSearchDetails&keyword="+key+"&subsearch="+subsearch;
    retrieveURL(url,"limited.do");
}
function do_toggleinteraction(postid1,stage,cont)
{
 if(stage == 'exp')
   {

   whichpost = document.getElementById(postid1+'exp');
   whichpost1= document.getElementById(postid1+'cls');
   whichpost2= document.getElementById(cont+'cnt');
  // whichpost3=document.getElementById(postid1+'cnt'+'_preview');
    if (whichpost.className=="visible") {
      whichpost.className="invisible";
      whichpost1.className="visible";


      whichpost2.className="visible";
      //whichpost3.className="invisible";

    }

    }else if(stage == 'cls'){

   whichpost = document.getElementById(postid1+'exp');
   whichpost1= document.getElementById(postid1+'cls');
   whichpost2= document.getElementById(cont+'cnt');
      //whichpost3=document.getElementById(postid1+'cnt'+'_preview');
    if (whichpost1.className=="visible") {
        whichpost1.className="invisible";
        whichpost.className="visible";
        whichpost2.className="invisible";
        //whichpost3.className="visible";

    }

      whichpost.className="visible";
     // alert ("Visible");
    }


}

function do_toggle(postid1,stage)
{
 if(stage == 'exp')
   {

   whichpost = document.getElementById(postid1+'exp');
   whichpost1= document.getElementById(postid1+'cls');
   whichpost2= document.getElementById(postid1+'cnt');
  // whichpost3=document.getElementById(postid1+'cnt'+'_preview');
    if (whichpost.className=="visible") {
      whichpost.className="invisible";
      whichpost1.className="visible";


      whichpost2.className="visible";
      //whichpost3.className="invisible";

    }

    }else if(stage == 'cls'){

   whichpost = document.getElementById(postid1+'exp');
   whichpost1= document.getElementById(postid1+'cls');
   whichpost2= document.getElementById(postid1+'cnt');
      //whichpost3=document.getElementById(postid1+'cnt'+'_preview');
    if (whichpost1.className=="visible") {
        whichpost1.className="invisible";
        whichpost.className="visible";
        whichpost2.className="invisible";
        //whichpost3.className="visible";

    }

      whichpost.className="visible";
     // alert ("Visible");
    }


}
function do_toggleAll(postid1,stage,no_div,div_pattern)
{
var curation = window.document.searchForm;
   if(stage == 'exp')
   {

	   whichpost = document.getElementById(postid1+'exp');
	   whichpost1= document.getElementById(postid1+'cls');
	   //whichpost2= document.getElementById(postid1+'cnt');
	  // whichpost3=document.getElementById(postid1+'cnt'+'_preview');
	    if (whichpost.className=="visible") {
		      whichpost.className="invisible";
		      whichpost1.className="visible";

		    var x = document.getElementsByTagName('div');

			for (var i=0;i<x.length;i++) {
				if (x[i].id.indexOf(div_pattern) !=-1) {

					x[i].className="visible"
				}
			}

		      //whichpost3.className="invisible";

	    }

    }else if(stage == 'cls'){

	   whichpost = document.getElementById(postid1+'exp');
	   whichpost1= document.getElementById(postid1+'cls');

	      //whichpost3=document.getElementById(postid1+'cnt'+'_preview');
	    if (whichpost1.className=="visible") {
	        whichpost1.className="invisible";
	        whichpost.className="visible";
	             var x = document.getElementsByTagName('div');

				for (var i=0;i<x.length;i++) {
					if (x[i].id.indexOf(div_pattern) !=-1) {
						x[i].className="invisible"
					}
				}

	        //whichpost3.className="visible";

	    }


	     // alert ("Visible");
    }
}
function showDetails(divname){
 //whichpost = document.getElementById(divname+'exp');
 //whichpost1= document.getElementById(divname+'cls');
 whichpost2= document.getElementById(divname+'cnt');
if(whichpost2.className == "visible"){

 //whichpost.className="visible";
 //whichpost1.className="invisible";
 whichpost2.className="invisible";
}else{
 //whichpost.className="invisible";
 //whichpost1.className="visible";
 whichpost2.className="visible";


}

}
function Limitedbrowse(lowerLimit) {


	setFieldname('SearchResults');
	setDivVisible('SearchResults_progress','visible');
   	if(limitValue == ''){
	    limitValue = '200';
	 }
	var url = "actionType=Browse" + "&lowerLimit=" + encodeSpecial(lowerLimit);

   	retrieveURL(url,"browse.do");

}
function getGene() {
	setFieldname('top_gene');
	setDivVisible('top_gene_progress','visible');
   	if(limitValue == ''){
	    limitValue = '200';
	 }
	var url = "actionType=getGeneClass";

   	retrieveURL(url,"getGeneClass.do");
}

function getMessageForGene(count) {




//document.getElementById('overlay').style.display = 'block';
document.getElementById('popup').style.display = 'block';
document.getElementById('popup').innerHTML="<p style = \"margin:0;padding:0\"><img src=\"images/cross.jpg\" onclick=\"javaScript:popclose()\" alt=\"Close\" style='float:right;' align='top'></p><p style = \"margin:0;padding:25px;\">"+count+" interactions present in NetPro v3.5.<br>Please <a href=\"mailto:business@molecularconnections.com\">contact us</a> to know more.</p>";





	//alert(count+" interactions are present in NetPro v3.5. Please contact us to know more.")


}
function getGeneForClass(gene){

	setDivVisible('top_gene_progress','visible');
	setFieldname('top_gene');
	setDivVisible('top_gene_progress','visible');
   	if(limitValue == ''){
	    limitValue = '200';
	 }
	var url = "actionType=getGeneforClass&query="+escape(gene);

   	retrieveURL(url,"getGeneforClass.do");

}

function Limitedsubbrowse(lowerLimit) {
	setFieldname('subcontent');

   	if(limitValue == ''){
	    limitValue = '200';
	 }
	var url = "actionType=BrowseSubSearch" + "&lowerLimit=" + encodeSpecial(lowerLimit);

   	retrieveURL(url,"limited.do");
}

function retrieveURL(url,action) {
    if (window.XMLHttpRequest) { // Non-IE browsers
      req = new XMLHttpRequest();
      req.onreadystatechange = processStateChange;
      try {
       // req.open("GET", url, true);
        req.open("POST",action );
        req.setRequestHeader('Content-Type',
		     'application/x-www-form-urlencoded');
      } catch (e) {
        alert(e);
      }
      req.send(url);
    } else if (window.ActiveXObject) { // IE
      req = new ActiveXObject("Microsoft.XMLHTTP");

      if (req) {

        req.onreadystatechange = processStateChange;
       // req.open("GET", url, true);
		req.open("POST",action );
        req.setRequestHeader('Content-Type',
		     'application/x-www-form-urlencoded');
        req.send(url);
      }
    }
  }

function processStateChange() {


    if (req.readyState == 4) { // Complete
      if (req.status == 200) { // OK response
				  if(document.getElementById(sel_field_name+'_progress') != null){
	      			   setDivVisible(sel_field_name+'_progress','invisible');
	      		   }
                  if(document.getElementById(sel_field_name+'_res') != null){
	                  document.getElementById(sel_field_name+'_res').innerHTML = req.responseText;
	                  resetData(sel_field_name);

	      		   }else{

	      		  	 document.getElementById(sel_field_name).innerHTML = req.responseText;

	      		   }


      		 	   if(req.responseText.indexOf("Error::") ==-1){
						//dataviewChange(sel_field_name);
						resetData(sel_field_name);

	         		}else{
	         		  alert("Error in the operation,plz check message")
	         		   resetData(sel_field_name);

	         		}




	         		if(document.getElementById(limitValue + '_div') != null) {
	         			document.getElementById(limitValue + '_div').className = 'currentpage';
	         		}


	      		   if(document.getElementById(sel_field_name+'_button') != null){
	      			   setDivVisible(sel_field_name+'_button','visible');
	      		   }

	              if(document.getElementById(sel_field_name+'rescnt') != null){
	      			   setDivVisible(sel_field_name+'rescnt','visible');
	      		   }
				 if(document.getElementById(sel_field_name+'rescls') != null){
	      			   setDivVisible(sel_field_name+'rescls','visible');
	      		   }
	      		    if(document.getElementById(sel_field_name+'resexp') != null){
	      			   setDivVisible(sel_field_name+'resexp','invisible');
	      		   }

	      		    if(document.getElementById(sel_field_name+'_search') != null){
	      			   setDivVisible(sel_field_name+'_search','invisible');
	      		   }

      }else {


       			   if(document.getElementById(sel_field_name+'_button') != null){
          			   setDivVisible(sel_field_name+'_button','visible');
          		   }
          		   if(document.getElementById(sel_field_name+'_progress') != null){
          			   setDivVisible(sel_field_name+'_progress','invisible');
          		   }
                  if(document.getElementById(sel_field_name+'rescnt') != null){
          			   setDivVisible(sel_field_name+'rescnt','invisible');
          		  }
				  if(document.getElementById(sel_field_name+'rescls') != null){
          			   setDivVisible(sel_field_name+'rescls','invisible');
          		  }
          		  if(document.getElementById(sel_field_name+'resexp') != null){
          			   setDivVisible(sel_field_name+'resexp','invisible');
          		  }

         alert("Problem: " + req.statusText);

      }
    }
      //alert(req.readyState);
  }


function resetData(selectarea)
{
		if(selectarea == 'SearchResults'){
		//	document.getElementById("interaction_report_menu").className = 'visible';
			if(document.getElementById('divStayTopRight') != null) {
	         			setDivVisible('divStayTopRight','invisible');

	         		}
		}
		if(selectarea == 'div_stat_1'){
				getStats("div_stat_2","Set2");
		}
		if(selectarea == 'div_stat_2'){
				getStatsPie("div_stat_pie_1","Species");
		}
		if(selectarea == 'div_stat_pie_1'){
				getStatsPie("div_stat_pie_2","Disease");


		}
		if(selectarea == 'div_stat_pie_2'){
				drawChartDefault('Species','disChartSpecies');
				drawChartDefault('Disease','disChartDisease');


		}





}


  function setDivVisible(div_id,visibility){
   //var curation=window.document.netProCurationForm;
   var curation=window.document.seqForm;

   whichpost = document.getElementById(div_id);
   if(whichpost != null){
	   whichpost.className=visibility;
	 }else{
	 	//alert('null--set vi'+div_id)
	 }
}
function encodeSpecial(des_str){
     encodedHtml = escape(des_str);
     encodedHtml = encodedHtml.replace(/\//g,"%2F");
     encodedHtml = encodedHtml.replace(/\?/g,"%3F");
     encodedHtml = encodedHtml.replace(/=/g,"%3D");
     encodedHtml = encodedHtml.replace(/&/g,"%26");
     encodedHtml = encodedHtml.replace(/@/g,"%40");
     encodedHtml = encodedHtml.replace(/\+/g,"%2B");
     des_str= encodedHtml;
     return des_str;
   }

   function dataviewChange(select_data){
    if(select_data == "SearchResults"){
		setDivVisible('querycomponent','invisible');
    }

    if(select_data == "InteractionReportResults"){
		setDivVisible('search_main_bolck','invisible');
	}
}
function ndelimetersplit(del, str)
    {
    var ind=0;
    var lastInd=0;
    var count=0;
    var word;
    var del_len=del.length;
    var array = new Object();

    array.length = 0;
    while( ind< str.length )
        {
        ind = str.indexOf( del, lastInd );
        if (ind == -1 )
            ind = str.length;
        array[count++] = str.substring(lastInd,ind);
        array.length++;
        lastInd = ind+del_len;
        }
    return array;
 }
 function detectBrowser()
{

    var browser=navigator.appName;
    var b_version=navigator.appVersion;
    var version=parseFloat(b_version);
	var nAgt = navigator.userAgent;
	var verOffset,fullVersion;
	if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
	 browserName = "Microsoft Internet Explorer";
	 fullVersion = nAgt.substring(verOffset+5);
	 fullVersion = fullVersion.substring(0,fullVersion.indexOf(";"));
	}
    if (browser=="Microsoft Internet Explorer" && fullVersion <= 7.0)
    {
		return true;
   	}
   	return false;
}



function popclose() {

document.getElementById('popup').style.display = 'none';
}

function clearRefer()
{

	var x=document.limited;
	x.client_name.value="";
	x.email.value="";
	x.organization.value="";
	x.phone.value="";
	x.location.value="";
	x.message.value="";
	document.getElementById('alert_refer_res').innerHTML = "";
	//document.getElementById('alert_refer_res').innerH

}
var emailfilter=/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

function checkEmailId()
{

	//

	var x=document.limited;

	var str=x.email.value;

	/*var returnval=emailfilter.test(x.email.value)
	alert(returnval);
   	return returnval;*/


   		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }


 		 return true
	}

function ValidateForm(){
	var emailID=document.frmSample.txtEmail

	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true


}
function sendRegisterMail()
{

	var x=document.limited;
	var name=x.client_name.value;
	var email=x.email.value;
	var org_name=x.organization.value;
	var ph=x.phone.value;
	var location=x.location.value;
	var mesg=x.message.value;

	var _alert="";

	if(name=='')
	{
		_alert=_alert+"Please enter your name\n";
	}
	if(email=='')
	{
		_alert=_alert+"Please enter your Email Id\n";
	}
	if(org_name=='')
	{
		_alert=_alert+"Please enter the Organization\n";
	}


	if(_alert=='')
	{


		setFieldname('alert_refer');
		var param=name+"<br>"+email+"<br>"+org_name+"<br>"+ph+"<br>"+location+"<br>"+mesg;
		var url = "actionType=sendCSMail"+"&param="+encodeSpecial(param);

	   	retrieveURL(url,"sendmail.do");

	}
	else
	{
		alert(_alert);
	}


}

