var tID=0; 
var fID=0; 
var mID=0; 
var nID=0;

var bSetHome = false;

//新加入的
var SearchSuggest = "输入信息 键以直达";
var ZhidainputtextSuggest = "新浪";
var JingqueinputtextSuggest = "天气 北京";
var WhoisSuggest = "请输入您想查询的信息";
var BottomsearchSuggest = "考试培训";
var KwSearchUrl = "cn.dll?name=";


var last_tabname = "tab_tool1";

function  isChineseCharacter(str)
{
	for(i=0; i<str.length; i++)
	{
		c = str.charCodeAt(i);
			
	    if(c >127)
		{
			return true;
		}
	}	
	
	return false;
}


function checkIP(value)
{
	
  var pattern = /^\d{1,3}(\.\d{1,3}){3}$/;
  
  if (!pattern.exec(value)) 
  {
     return false;
  }
  
  var ary = value.split('.');
  
  for(key in ary)
  {
    if (parseInt(ary[key]) > 255)
	{
      return false;
    }
  }
  
  return true ;
}


$(document).ready(function(){
						   
	
	$("#searchinput").autocomplete("tip.dle", {
	max: 10,
	scrollHeight:250,
	highlight: false,
	selectFirst: true
	});	
     
	$("#searchinput").example(SearchSuggest,{ 
	class_name: 'not_example'							  
	}).attr("title",SearchSuggest);
	
	
	
	$("#zhidainput").val(ZhidainputtextSuggest).css({
     'color':'#C9C9C9'
    });
	
	$("#jingqueinput").val(JingqueinputtextSuggest).css({
     'color':'#C9C9C9'
    });
	

	$("#whoissearch").example(WhoisSuggest,{ 
	class_name: 'not_example'							  
	}).attr("title",WhoisSuggest);
	
	
	$("#bottomsearch").example(BottomsearchSuggest,{ 
	class_name: 'not_example'							  
	});
	

	for (i = 1; i <= 10; i++)
	{
		name = "#tool" + i;	
		
	    $(name).bind("click",function(){
		BasicmodalNavigate($(this));       
        }); 		
	}
	
	
	var buttons = $("a").filter(function(index) {
										 
	 return $(this).attr("id").lastIndexOf("_") > 0;
	 
	});
	
	buttons.each(function(i){   

      $(this).bind("click",function(){
									
		SearchNavigate($(this)); 
		
      }); 
	  
   });
   
   
   var forms = $("form").filter(function(index) {
										 
	 return true;
	 
	});
	
	forms.each(function(i){   

      $(this).submit( function () {
									
		   	id = $(this).attr("id");		   	
		   		
	      num = id.lastIndexOf("_");
	      
	      if (num <= 0) return false;	     
	      
	      inputname = id.substring(0,num);      
	      
	     inputtext = $('#' + inputname);
	   
	      seachvalue = inputtext.val();
	      
/*	      var c;
	      var i;
	      for(i=0; i<seachvalue.length; i++)
		 {
			 c = seachvalue.charCodeAt(i);
			if(c>127)
			{
				alert("请输入所要查询的通用网址");
				break;
				return false;
			}
		 }*/
var ipv4_1Reg =/^[ ]*(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])[ ]*$/;
var ipv4_2Reg =/^[ ]*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])) - ((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))[ ]*$/;
var ipv4_3Reg =/^[ ]*((([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))\/([12]?[0-9]|3[012])[ ]*$/;
		 
	      if ((seachvalue .indexOf(".")!=-1) || (seachvalue .indexOf("。")!=-1) && (!ipv4_1Reg.test(seachvalue) && !ipv4_2Reg.test(seachvalue) && !ipv4_3Reg.test(seachvalue)))
	      {
	      	inputtext.focus();
	      	alert("请输入所要查询的通用网址");
	      	return false;
	      }
	      
	      if (seachvalue.length > 0)
	      {
	      	 return true;
	      }
	      else 
	      {
		       inputtext.focus();
		   
		       alert("请输入查询信息");
		   
		       return false;
	   }
	      
	      
		
      }); 
	  
   });

	
	$("#whoissearchbutton").bind("click",function(){	
												  
	   SearchWhois();   
	   
    });
	
						  
	$("#whoissearch").keypress(function(event){
										
    if(event.keyCode==13 || event.which==13){
		
        SearchWhois();		
    }
    }); 
	
	
	$("#index_r2_c8").bind("click",function(){
											
       	this.style.behavior='url(#default#homepage)';	
		
	    this.setHomePage('http://name.knet.cn');
		
	    bSetHome = true;
	   
    });
	
	$("#index_r2_c12").bind("click",function(){
											
       window.external.AddFavorite('http://name.knet.cn','通用网址首页');
	   
    });
	
	
	
				
});


function SetHome(e)
{	
	e.style.behavior='url(#default#homepage)';
												  
	e.setHomePage('http://name.knet.cn'); 
	
	bSetHome = true;
}


function SearchWhois()
{
		strName = $("#whoissearch").val();	
		
		if ($("#whoissearch").attr("title") == strName || strName.length == 0)
		{
			alert("请输入所要查询的内容");
			
			$("#whoissearch").focus();
			
			return;
		}		
		
       selectvalue = $("#whoissearch_select").val();	 //document.all.whoissearch_select.value;  	
	   
	   isChinese = isChineseCharacter(strName);
	   
	   
	   if (selectvalue == "ip")
	   {
		   if (checkIP(strName)) window.open("http://ipwhois.cnnic.net.cn/ipwhoisresult.php?query_option=ipv4&txtquery="+strName);
		   
		   else if (strName.indexOf(":") > 0) 
		   {
		       window.open("http://ipwhois.cnnic.net.cn/ipwhoisresult.php?query_option=ipv6&txtquery="+strName);
		   }
		   
		   else 
		   {
			   alert("请输入所要查询的IP地址");
		   }	   
	   }
	   
	   else if (selectvalue == "cn")
	   {
		 if(strName.substring(strName.length-3,strName.length)!=".cn")
		 {
			strName = strName + ".cn";
		 }
		 
		 if (!isChinese)
		 {
			 window.open("http://ewhois.cnnic.cn/validatecode/validate.jsp?value="+ strName + "&entity=domain&service=/whois&inputfield=value");
			 
			 return;
		 }
		 
		 else 
		 {
			 alert("cn域名中不能有中文"); return;
		 }
		 
		 alert("请输入所要查询的CN域名"); 
		 
	   }
	   
	   else if (selectvalue == "zh")
	   {
		   
		 if(strName.substring(strName.length -3,strName.length)!=".cn")
		 {
			strName = strName + ".cn";
		 }
		 
		 window.open("http://cwhois.cnnic.cn/validatecode/validate.jsp?value="+strName+"&entity=domain&service=/whois&inputfield=value");   
	   }
	   
	   else if (selectvalue == "kw")
	   {
		 if (strName.length > 32 )
		 {
			 alert("输入的通用网址太长！");  return;		 
		 }	
		 else  window.open("http://seal.cnnic.cn/validatecode/validate.jsp?nameInfo="+strName+"&entityName=keyword&service=/queryserver&inputfield=nameInfo");
	   }
	   
	   else if (selectvalue == "wx")
	   {
		   window.open("http://whois.wkey.cn/validatecode/validate.jsp?name="+strName+"&entity=keyword&service=/queryserver&inputfield=name");
	   }   	
}



function SearchNavigate(e)
{
	id = e.attr("id");
	
	num = id.lastIndexOf("_");
	
	if (num > 0)
	{	
	   inputname = id.substring(0,num);
	   
	   inputtext = $('#' + inputname);
	   
	   seachvalue = inputtext.val();
	   
	   pidtext = 	$('#' + inputname + '_pid'); 
	   
	   pidvalue = '&pid=' + pidtext.val();
	   
	   title = inputtext.attr("title");	
	   
	   formobj = $('#' + inputname + '_form'); 
   
	   if (seachvalue.length > 0 && (seachvalue!= title))
	   {
		   //window.open( KwSearchUrl + seachvalue + pidvalue , "_blank");	
		   
		   formobj.submit();
	   }
	   
	   else 
	   {
		   inputtext.focus();
		   
		   alert("请输入查询信息");
	   }
	   
	  // alert(inputname);
	}
	



}



function BasicmodalNavigate(e)
{
	urlstr = e.attr("title");
	
	$('#frame_tool',document.body).attr("src",urlstr);
	
	$('#dialog_tool').modal();
	
	
	tabname = "tab_" + e.attr("id");	
	
	
	if (last_tabname != tabname)
	{	
	   $('#' + tabname).addClass("menu_bottomB");
	   
	   $('#' + last_tabname).removeClass("menu_bottomB");
	   
	   last_tabname = tabname;		
	}
	
	sent(e,null,page_type);  
}


function ShowTabs(ID)
{ 
   if(ID!=tID)
   { 
      document.getElementById("Tabs" + ID).className='menu_bottom21';
	  
      document.getElementById("Tabs"+ tID).className='menu_bottom11';
	  
      document.getElementById("TabTitle"+ID).style.display='';
	  
      document.getElementById("TabTitle" + tID).style.display='none';
	  
      tID=ID; 	  
   } 
} 



function ShowTabs(ID)
{ 
   if(ID!=tID)
   { 
      document.getElementById("Tabs" + ID).className='menu_bottom21';
	  
      document.getElementById("Tabs"+ tID).className='menu_bottom11';
	  
      document.getElementById("TabTitle"+ID).style.display='';
	  
      document.getElementById("TabTitle" + tID).style.display='none';
	  
      tID=ID; 	  
   } 
} 



function ShowTabskk(ID)
{ 
   if(ID!=mID)
   { 
      document.getElementById("Tabskk" + ID).className='menu_bottom21';
	  
      document.getElementById("Tabskk"+ mID).className='menu_bottom11';
	  
      document.getElementById("TabTitlekk"+ID).style.display='';
	  
      document.getElementById("TabTitlekk" + mID).style.display='none';
	  
      mID=ID; 	  
   } 
} 





function ShowTabsjj(ID)
{ 
   if(ID!=nID)
   { 
      document.getElementById("Tabsjj" + ID).className='menu_bottom21';
	  
      document.getElementById("Tabsjj"+ nID).className='menu_bottom11';
	  
      document.getElementById("TabTitlejj"+ID).style.display='';
	  
      document.getElementById("TabTitlejj" + nID).style.display='none';
	  
      nID=ID; 	  
   } 
} 


function ShowTabs2(ID)
{ 

   if(ID!=fID)
   { 
      document.getElementById("TabTitle2"+ID).className='menu_bottomB';
	  
      document.getElementById("TabTitle2"+fID).className='menu_bottomA';
	  
      fID=ID; 	 
   } 
} 




function jump2search()
{
	strName = document.searchfrm.name.value;
		
	window.location.href="cn.dll?name=" + strName + "&pid=123" ;
}



function chkkey(e)
{
  var k = window.event?window.event.keyCode:e.which;
  
  var src = window.event?window.event.srcElement:e.target;
  
  if (k==13)
  {
	  NavigateSearch();
  }
}
