var isIE6=false;
var timer=null;

jQuery.fn.floatdiv=function(location)
{
//ie6要隐藏纵向滚动条

	if($.browser.msie && $.browser.version=="6.0"){
	   //$("html").css("overflow-x","auto").css("overflow-y","hidden");
	   isIE6=true;
	};

			   var loc;//层的绝对定位位置
			   if(location==undefined || location.constructor == String){
					switch(location){
						 case("rightbottom")://右下角
							  loc={right:"0px",bottom:"0px"};
							  break;
						 case("leftbottom")://左下角
							  loc={left:"0px",bottom:"0px"};
							  break; 
						 case("lefttop")://左上角
							  loc={left:"0px",top:"0px"};
							  break;
						 case("righttop")://右上角
							  loc={right:"0px",top:"0px"};
							  break;
						 case("middle")://居中
							  var l=0;//居左
							  var t=0;//居上
							  var windowWidth,windowHeight;//窗口的高和宽
							  //取得窗口的高和宽
							  if (self.innerHeight) {
							   windowWidth=self.innerWidth;
							   windowHeight=self.innerHeight;
							  }else if (document.documentElement&&document.documentElement.clientHeight) {
							   windowWidth=document.documentElement.clientWidth;
							   windowHeight=document.documentElement.clientHeight;
							  } else if (document.body) {
							   windowWidth=document.body.clientWidth;
							   windowHeight=document.body.clientHeight;
							  }
							  l=windowWidth/2-$(this).width()/2;
							  t=windowHeight/2-$(this).height()/2;
							  loc={left:l+"px",top:t+"px"};
							  break;
						 default://默认为右下角
							  loc={right:"0px",bottom:"0px"};
							  break;
					  }
			   }else{
					loc=location;
			   }				


			   if(isIE6)
			   {
					if(loc.right!=undefined)
					{
						$("#win_right_down").css("position","absolute").css("right","0px").css("z-index","9999");
						$("#win_right_down").css("bottom","0px").css("top",document.documentElement.scrollTop+document.documentElement.clientHeight-283+"px");
					}
			   }else{
				   $("#win_right_down").css("z-index","9999").css(loc).css("position","fixed");
			   }
};


$(function(){
		var htmlstr;
		htmlstr='<p><span class="Notetitle">欢迎给我们留言</span> <span id="bts">';
		htmlstr+='<label class="button" id="tomin" title="最小化"> </label>';
		htmlstr+='<label class="button" id="tomax" title="最大化"> </label>';
		htmlstr+='<label class="button" id="toclose" title="关闭"> </label> </span></p>';
		htmlstr+='<div id="win_right_down_con">';
		htmlstr+='<div><textarea style="background-color: #E2EBF0;WIDTH: 200px;overflow:hidden; HEIGHT:80px;color:#9EACB9;"; name="detail" id="I_detail" cols="30" rows="30" onfocus=TextSeach(this.id,"请留言","1") onblur=TextSeach(this.id,"请留言","2")>请留言</textarea></div>';
		htmlstr+='<div id="Div_name"><label class="icon" id="name"></label><input name="name"  id="I_name"  type="text" value="请输入您的姓名" onfocus=TextSeach(this.id,"请输入您的姓名","1") onblur=TextSeach(this.id,"请输入您的姓名","2")></div>';
		htmlstr+='<div id="Div_ComName"><label class="icon" id="Comname"></label><input name="company" id="I_company" type="text" value="请输入您的公司名称" onfocus=TextSeach(this.id,"请输入您的公司名称","1") onblur=TextSeach(this.id,"请输入您的公司名称","2")></div>';
		htmlstr+='<div id="Div_Mail"><label class="icon" id="Mail"></label><input name="mail" id="I_mail" type="text" value="请输入您的电子邮箱" onfocus=TextSeach(this.id,"请输入您的电子邮箱","1") onblur=TextSeach(this.id,"请输入您的电子邮箱","2")></div>';
		htmlstr+='<div id="Div_Mobile"><label class="icon" id="Mobile"></label><input name="telephone" id="I_telephone" type="text" value="请输入您的手机号码" onfocus=TextSeach(this.id,"请输入您的手机号码","1") onblur=TextSeach(this.id,"请输入您的手机号码","2")></div>';
		htmlstr+='<div id="Div_Button"><input name="submit"  onclick="AddTransfer()" id="subInfo" type="button" value="发送"></div>';
		htmlstr+='</div>';

		$("#win_right_down").attr('innerHTML',htmlstr);
		$("win_right_down").floatdiv("rightbottom");
		$(window).load(function(){
		   $("div[id=win_right_down]").slideDown("slow");
		}) 
		$("label[id=tomin]").click(function(){
		   $("div[id=win_right_down_con]","div[id=win_right_down]").slideUp();
		   if(isIE6)
		   {
			   clearTimeout(timer);
			   PXp(26);
		   }
		});
		$("label[id=tomax]").click(function(){
		   $("div[id=win_right_down_con]","div[id=win_right_down]").slideDown();
		   if(isIE6)
		   {
			   clearTimeout(timer);
			   PXp(283);
		   }
		});
		$("label[id=toclose]").click(function(){
		   $("div[id=win_right_down]").hide();
		});
		
		if(isIE6)
		{
			setTimeout(function(){down();},3000);
		}else{
			setTimeout('$("div[id=win_right_down_con]","div[id=win_right_down]").slideUp()',3000);
		}

});

function down()
{
	$("div[id=win_right_down_con]","div[id=win_right_down]").slideUp();
	PXp(26);
}

function PXp(pix)
{
	$("#win_right_down").css("top",document.documentElement.scrollTop+document.documentElement.clientHeight-pix+"px");
	timer=setTimeout(function(){PXp(pix);},50);
}

function TextSeach (Inputid,text,PI)
{
	switch(PI){
		case("1")://得到焦点
			if ($("#"+Inputid).val()==text)
			{
				$("#"+Inputid).val('');	
			}

			break;
		case("2")://失去焦点
			if ($("#"+Inputid).val()==text || $("#"+Inputid).val()=='')
			{
				$("#"+Inputid).val(text);	
			}
			break;
		default://默认为右下角
			break;
	}
}

function AddTransfer()
{
	var detail = $("#I_detail").val();
	var name = $("#I_name").val();
	var company=$("#I_company").val();
	var mail=$("#I_mail").val();
	var telephone=$("#I_telephone").val();
	
	if(CheckSub(detail,name,mail,telephone) == true)
	{
		$("#subInfo").attr('disabled','true');
		$.post("/newversion/resultBaiduNote.asp?detail="+detail+"&name="+name+"&company="+company+"&mail="+mail+"&telephone="+telephone+"",{},
			function(json){
				if(json.isok){
					alert("感谢您的留言，我们会及时与您联系");
					$("#subInfo").attr('disabled','');
					$("#I_name").val("请输入您的姓名");
					$("#I_detail").val("请留言");
					$("#I_company").val("请输入您的公司名称");
					$("#I_mail").val("请输入您的电子邮箱");
					$("#I_telephone").val("请输入您的手机号码");
				}else{
					alert(json.error);
				}
			},"json"
		);
	}
}


function CheckSub(detail,name,mail,telephone)
{
	if(isNull(detail)==true || detail=="请留言"){
		alert("请输入留言内容！");
		return false;
	}
	if(isNull(name)==true || name=="请输入您的姓名"){
		alert("请输入您的姓名！");
		return false;
	}

	if(isNull(mail)==true || mail=="请输入您的电子邮箱"){
		alert("请输入您的邮件地址！");
		return false;
	}
	if(chk_email(mail)==false){
		return false;
	}
	if(isNull(telephone)==true || telephone=="请输入您的手机号码" || isInteger(telephone)==false){
		alert("请输入您正确的手机号码！");
		return false;
	}
	return true;
}

function isNull( str ){
	if ( str == "" || str==null ) return true;
	var regu = "^[ ]+$";
	var re = new RegExp(regu);
	return re.test(str);
}

function isInteger( str ){
	var regu = /^[-]{0,1}[0-9]{1,}$/;
	return regu.test(str);
}

function chk_email(email)//MAIL合法性检测
{
	invalid = "";
    if ( (email.indexOf("@") == -1) || (email.indexOf(".") == -1) )
    invalid += "\n\nEmail地址不合法。应当包含'@'和'.'；例如('.com')。请检查后再递交。";

    if (email.indexOf("your email here") > -1)
    invalid += "\n\nEmail地址不合法，请检测您的Email地址，在域名内应当包含'@'和'.'；例如('.com')。";

    if (email.indexOf("\\") > -1)
    invalid += "\n\nEmail地址不合法，含有非法字符(\\)。";

    if (email.indexOf("/") > -1)
    invalid += "\n\nEmail地址不合法，含有非法字符(/)。";

    if (email.indexOf("'") > -1)
    invalid += "\n\nEmail地址不合法，含有非法字符(')。";

    if (email.indexOf("!") > -1)
    invalid += "\n\nEmail地址不合法，含有非法字符(!)。";

    if ( (email.indexOf(",") > -1) || (email.indexOf(";") > -1) )
    invalid += "\n\n只输入一个Email地址，不要含有分号和逗号。";

    if (email.indexOf("?subject") > -1)
    invalid += "\n\n不要加入'?subject=...'。";
  if (invalid == "")
  {
    return true;
  }else{
    alert("输入的Email可能包含错误：" + invalid);
    return false;
  }
}
