var tb_pathToImage = "./Images/sLogin/waiting.gif";
$(document).ready(function(){   
	imgLoader = new Image();
	imgLoader.src = tb_pathToImage;
	$("input[name=Button]").click(function(){
	var m_Name = $("#m_Name").val();
	var m_Subject  = $("#m_Subject").val();
	var m_Contents = $("#m_Contents").val();
	var SS_ID = $("#SS_ID").val();
	var NumRegex = /^[0-9]/;
		if(m_Name.trim() == "") {
			inlineMsg('m_Name','姓名不能为空.');
			return false;
		}
		if(m_Subject.trim() == "") {
			inlineMsg('m_Subject','主题不能为空.');
			return false;
		}

		if(m_Contents.trim() == "") {
			inlineMsg('m_Contents','内容不能为空.');
			return false;
		}
		
		if(SS_ID.trim() == "") {
			alert('缺少必要的参数，请联系管理员。');
			return false;
		}
		
		if(!SS_ID.match(NumRegex)) {
			alert('缺少必要的参数，请联系管理员。');
			return false;
		}

		$.ajax({
				type: "POST",
				url: "/system/sys0_inc_NewMessage.asp",
				data: "action=add&SS_ID="+SS_ID+"&m_Name="+escape(m_Name)+"&m_Subject="+escape(m_Subject)+"&m_Contents="+escape(m_Contents)+"&s="+Math.random(),
				timeout: 2500,
				error: function(){	
					s_MsgText('未能准确提交留言，联系管理员。');
				},
				success: function(m){
					s_Error(m);
				}
		});	
	});
	
});

function s_Error(m){
	var text = unescape(m).split("|");
	if (text[0]==1){
		$("#form1").each(function() {
        if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType))
            this.reset();
		});
		alert('提交留言成功.');
		//s_MsgText('提交留言成功.');	
	}else{
		inlineMsg(text[0],text[1]);
	}
}

String.prototype.trim = function(){ 
return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 

function tb_show() {
	try {
		if (typeof document.body.style.maxHeight === "undefined") {
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			if (document.getElementById("TB_HideSelect") === null) {
				$("body").append("<iframe id='TB_HideSelect'></iframe><div id='TB_overlay'></div><div id='TB_window'></div>");
			}
		}else{
			if(document.getElementById("TB_overlay") === null){
				$("body").append("<div id='TB_overlay'></div><div id='TB_window'></div>");
			}
		}
		if(tb_detectMacXFF()){
			$("#TB_overlay").addClass("TB_overlayMacFFBGHack");
		}else{
			$("#TB_overlay").addClass("TB_overlayBG");
		}
		
	} catch(e) {
		//nothing here
	}
}

function tb_load(){
		$("body").append("<div id='TB_load'><img src='"+imgLoader.src+"' /></div>");
		$('#TB_load').show();
}

function tb_showIframe(){
	$("#TB_load").remove();
	$("#TB_window").css({display:"block"});
}

function tb_remove() {
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_load").remove();
	if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
		$("body","html").css({height: "auto", width: "auto"});
		$("html").css("overflow","");
	}
	return false;
}

function tb_position() {
$("#TB_window").css({marginLeft: '-' + parseInt((TB_WIDTH / 2),10) + 'px', width: TB_WIDTH + 'px'});
	if ( !(jQuery.browser.msie && jQuery.browser.version < 7)) { // take away IE6
		$("#TB_window").css({marginTop: '-' + parseInt((TB_HEIGHT / 2),10) + 'px'});
	}
}

function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

function s_MsgText(m){
	var sMsgstr = "";
	sMsgstr="<div id='msgInfo' align='center'><img src='/images/aL.gif' style='margin-bottom:-4px;'/>";
	sMsgstr+="<span class='alertTxt'>"+m+"</span><img src='/images/aR.gif' style='margin-bottom:-4px;'/></div>";
    $("body").append(sMsgstr);
	$('#msgInfo').show();
	setTimeout("$('#msgInfo').remove()",2000);
}

var MSGTIMER = 20;
var MSGSPEED = 3;
var MSGOFFSET = 3;
var MSGHIDE = 3;

// build out the divs, set attributes and call the fade function //
function inlineMsg(target,string,autohide) {
  var msg;
  var msgcontent;
  if(!document.getElementById('msg')) {
    msg = document.createElement('div');
    msg.id = 'msg';
    msgcontent = document.createElement('div');
    msgcontent.id = 'msgcontent';
    document.body.appendChild(msg);
    msg.appendChild(msgcontent);
    msg.style.filter = 'alpha(opacity=0)';
    msg.style.opacity = 0;
    msg.alpha = 0;
  } else {
    msg = document.getElementById('msg');
    msgcontent = document.getElementById('msgcontent');
  }
  msgcontent.innerHTML = string;
  msg.style.display = 'block';
  var msgheight = msg.offsetHeight;
  var targetdiv = document.getElementById(target);
  targetdiv.select();
  var targetheight = targetdiv.offsetHeight;
  var targetwidth = targetdiv.offsetWidth;
  var topposition = topPosition(targetdiv) - ((msgheight - targetheight) / 2);
  var leftposition = leftPosition(targetdiv) + targetwidth + MSGOFFSET;
  msg.style.top = topposition + 'px';
  msg.style.left = leftposition + 'px';
  clearInterval(msg.timer);
  msg.timer = setInterval("fadeMsg(1)", MSGTIMER);
  if(!autohide) {
    autohide = MSGHIDE;  
  }
  window.setTimeout("hideMsg()", (autohide * 1000));
}

// hide the form alert //
function hideMsg(msg) {
  var msg = document.getElementById('msg');
  if(!msg.timer) {
    msg.timer = setInterval("fadeMsg(0)", MSGTIMER);
  }
}

// face the message box //
function fadeMsg(flag) {
  if(flag == null) {
    flag = 1;
  }
  var msg = document.getElementById('msg');
  var value;
  if(flag == 1) {
    value = msg.alpha + MSGSPEED;
  } else {
    value = msg.alpha - MSGSPEED;
  }
  msg.alpha = value;
  msg.style.opacity = (value / 100);
  msg.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(msg.timer);
    msg.timer = null;
  } else if(value <= 1) {
    msg.style.display = "none";
    clearInterval(msg.timer);
  }
}

function imgalpha(){
	$("#imgCode").animate({opacity: "1",left: "+=20"},"slow").animate({opacity: "0.1",left: "5"},1200)	
}
// calculate the position of the element in relation to the left of the browser //
function leftPosition(target) {
  var left = 0;
  if(target.offsetParent) {
    while(1) {
      left += target.offsetLeft;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.x) {
    left += target.x;
  }
  return left;
}

// calculate the position of the element in relation to the top of the browser window //
function topPosition(target) {
	if(s_DetectMacXFF()){
		var	top = 0;
	}else{
		var	top = 0;
	}
  if(target.offsetParent) {
    while(1) {
      top += target.offsetTop;
      if(!target.offsetParent) {
        break;
      }
      target = target.offsetParent;
    }
  } else if(target.y) {
    top += target.y;
  }
  return top;
}


function s_DetectMacXFF() {
  if ($.browser.mozilla || $.browser.opera || $.browser.safari)return true;
}
