﻿
function getClientWidth ()
{
	var width = 0;
	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		width = window.innerWidth;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		width = document.documentElement.clientWidth;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight )) 
	{
		//IE 4 compatible
		width = document.body.clientWidth;
	}
	
	return width;
}

function getClientHeight()
{
	var height = 0;
	if( typeof( window.innerWidth ) == 'number' ) 
	{
		//Non-IE
		height = window.innerHeight;
	} 
	else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
	{
		//IE 6+ in 'standards compliant mode'
		height = document.documentElement.clientHeight;
	} 
	else if( document.body && ( document.body.clientWidth || document.body.clientHeight )) 
	{
		//IE 4 compatible
		height = document.body.clientHeight;
	}
	
	return height;
}

function trim(str)
{ 
	//删除左右两端的空格
	return str.replace(/(^\s*)|(\s*$)/g, "");
}

function ltrim(str)
{ 
	//删除左边的空格
	return str.replace(/(^\s*)/g,"");
}

function rtrim(str)
{ 
	//删除右边的空格
	return str.replace(/(\s*$)/g,"");
}

function reload()
{
    if (window.ActiveXObject)
    {
        window.location.reload();
    }
    else
    {
        window.location.href = "";
    }
}



function syncAjaxRequest(mothed, url, params) 
{
    var result = null;
    
    $.ajax({
        async:  false,
        cache:  false,
        type:   mothed,
        url:    url,
        data:   params,
        success: function(data) 
        {
            result = data;
        }
    });
    
    if (result == null)
        throw "网络通信错误或者服务器错误。";
    
    return result;
}

function ajax_getDialogUI(params)
{
    var result = "";
    try 
    {
        result = syncAjaxRequest("get", "/ajax_dialog_ui.aspx", params);	            
    }
    catch(ex) {
        result = ex;
    }
    
    return result;
}



function initMessageDialog()
{
    if ($("#message_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"message_dialog"}));
        $("#message_dialog").dialog({
		    autoOpen: false,
		    width: 340,
		    height: 200,
            resizable: false,
            bgiframe: true,		
            modal: true
        });   
    }
}

function showMessageDialog(msg, completeFunction, param)
{
    initMessageDialog();
    
    $("#message_dialog").dialog("option", "open", function(){
         $("#message_dialog_message").text(msg);
    });
    
    $("#message_dialog").dialog("option", "buttons", {
		 " 确定 ": function() {
			$("#message_dialog").dialog("close");
			if (completeFunction)
			    completeFunction(param);
		 }
    });
    
    $("#message_dialog").dialog("open");
}

function initConfirmDialog()
{
    if ($("#confirm_dialog").html() == null)
    {
        $("body").append(ajax_getDialogUI({"dialog":"confirm_dialog"}));
        $("#confirm_dialog").dialog({
		    autoOpen: false,
		    width: 340,
		    height: 200,
            resizable: false,	
            bgiframe: true,		
            modal: true
        });
    }
}

function showConfirmDialog(msg, okFunction, okParam, cancelFunction, cancelParam)
{
    initConfirmDialog();
    
    $("#confirm_dialog").dialog("option", "buttons", {
		 " 否 ": function() {
		    $("#confirm_dialog").dialog("close");
		    if (cancelFunction)
		        cancelFunction(cancelParam);
		 },
		 " 是 ": function() {
			$("#confirm_dialog").dialog("close");
			if (okFunction)
			    okFunction(okParam);
		 }
    });
    
    $("#confirm_dialog").dialog("option", "open", function(){
         $("#confirm_dialog_message").text(msg);
    });
    
    $("#confirm_dialog").dialog("open");
}

$(document).ready(function()
{       
     $(".init_text_input").each(function(){
        setInputControlInitText($(this));
     });
});

function setInputControlInitText(obj)
{
    var initText = obj.attr('title');
          
    if (obj.val() == '' || obj.val() == initText)
        obj.css('color', '#999999').val(initText);
    
    obj.focus(function(){
      if (obj.val() == initText)
      {
          obj.val('').css('color', '');
      }
    });

    obj.blur(function(){
      if ('' + obj.val() == '')
      {
          obj.val(initText).css('color', '#999999');
      }
    });
}


//全站搜索
function searchPageCheckInput()
{

   var keywordtxt = $("#home_web_search_keyword").val();
   if (keywordtxt + "" == "")
   {
       showMessageDialog("请输入搜索关键词。");
       return false;    
   }
   
   return true;
}           





function ajax_Friendlink(friend_name, friend_link, friend_logo,webmaster,QQ,Email)
{
    var result = "";
    try
    {
        result = syncAjaxRequest("post", 
        "/ajax_FriendLink.aspx", 
        { "action": "friendlink", "friend_name": friend_name,"friend_link":friend_link,"friend_logo":friend_logo,"webmaster":webmaster,"QQ":QQ,"Email":Email});
    }
    catch(ex)
    {
        result = ex;
    }
    
    return result;
}

function subFriendlink()
{
    if ("" + $("#friend_name").val() == "")
    {
        showMessageDialog("请填写网站名称。");
        return;    
    }
    
    if ("" + $("#friend_link").val() == "")
    {
        showMessageDialog("请填写网站地址。");
        return;    
    }
    
    var result = ajax_Friendlink($("#friend_name").val(),$("#friend_link").val(),$("#friend_logo").val(),$("#webmaster").val(),$("#QQ").val(),$("#Email").val());
    if (result == "1")
    {
        showMessageDialog("友情链接提交完成，请等待管理员审核。");
        $("#friend_name").val("");
        $("#friend_link").val("");
        $("#friend_logo").val("");
        $("#webmaster").val("");
        $("#QQ").val("");
        $("#Email").val("");
        
        return;
    }
    else
    {
        showMessageDialog(result);
    }
}
