﻿
//Traverlin 分页公用方法
function ShowWarm(text) {
    $("#AjaxShowWarnTxt").html(text);
    $("#AjaxWarn").show();
}

function HidnWarm() {
    $("#AjaxShowWarnTxt").html("OK");
    $("#AjaxWarn").hide();
}

function ajaxClickUrl(url,panelid,callback){  
        var targetNum=$("#txtTargetNum").val();
        if(isNaN(targetNum)){            
            alert('Please Enter The Correct Page! ');
            $("#txtTargetNum").focus();
        }
        else{
            url=url.replace("{0}",targetNum);    
            if(callback){
                ajaxPanel(url,panelid,callback);
            } 
            else{
                ajaxPanel(url,panelid);
            }       
            
        }    
    }


function ajaxPanel(url,panelIDs){   
    if(!panelIDs|| panelIDs==''){
        return false;
    } 
    url = url || (document.forms[0] && document.forms[0].action) || ajaxHelper.clearTag(location.href, '#'); 
    var IDs = Array.prototype.slice.apply(arguments).slice(1 || 0);
    var callback = IDs[IDs.length - 1];
    if (typeof callback == 'function') {
        IDs = IDs.slice(0, IDs.length - 1);
    }
    else {
        callback = null;
    }
    var panelStrings;
    if( IDs.length > 0){
        panelStrings =IDs.join(',');
    }
    else{
        return false ;
    }   
      
     
    $.ajax
    (
        {
            type: 'get',
            dataType: 'html',
            contentType: "application/x-www-form-urlencoded; charset=utf-8",
            cache: false,
            url: url,
            data: '_AjaxTags=' + panelStrings,                
            success:
            function (result) { 
                if(result){                     
                     if($('#'+panelStrings).length >0){ 
                      
                            $('#'+panelStrings).html(result);  
                                                      
                     }    
                }            
                    
                if (callback != null) 
                {                   
                    callback(result);
                }    
                   
            },
            complete:
            function () { 
                HidnWarm();

            },
            beforeSend:
            function () {
                ShowWarm("Loadding...");                
            },
            error:
            function (XMLHttpRequest, textStatus, errorThrown) {       
                alert(XMLHttpRequest.responseText+"---前面为错误内容！！！");     
                alert(textStatus); 
            },
            timeout:
            function () {   
                            
            }
        }
    );
}


//序列化表单成Url
function serializeForm() {
    var form = document.forms[0];  
    var param = "";
    for (var i = 0; i < form.length; i++) {
        if (form[i].type == "radio" || form[i].type == "checkbox") {
            if (!form[i].checked) {
                continue;
            }
        }
        if (form[i].type == "submit") {
            continue;
        }
        var name = form[i].name;
        if (!name) {
            name = 'noName' + i;
        }
        var val = form[i].value;
        if (!val && form[i].type != "textarea") {
            val = form[i].innerHTML;
        }
        val = encodeURIComponent(val).replace(/\+/g, '%2B').replace(/\"/g, '%22').replace(/\'/g, '%27').replace(/\//g, '%2F')
        param += name + "=" + val + "&";
    }
    return param.substr(0, param.length - 1);
}

//表单提交
function submit(url,callback) {
    $.ajax({
        type: 'post',
        dataType: 'json',
        contentType: "application/x-www-form-urlencoded; charset=utf-8",
        cache: false,
        url: url,
        success:
               function(result) {                  
                   if (callback) {
                       callback(result);
                   }
               },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest.responseText + "---前面为错误内容！！！");
            alert(textStatus);
        }

    });
}




//给字符串添加 截取字符串长度的方法
function SubString(oldstr,n)
{

  if(typeof(oldstr)=="undefined"||oldstr==null)return oldstr;
  var r = /[^\x00-\xff]/g;
  if(oldstr.replace(r, "mm").length <= n) return oldstr;
  n = n - 3;
  var m = Math.floor(n/2);
  for(var i=m; i<oldstr.length; i++)
  {
    if(oldstr.substr(0, i).replace(r, "mm").length>=n)
    {
      return oldstr.substr(0, i) ;
    }
  }
  return oldstr;
};

function $G(id)
{
    return document.getElementById(id);
}
function $F(id)
{
    return $G(id).value;
}

String.prototype.trim = function() 
{ 
    if (this == null) return "";
    return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 
String.prototype.lTrim = function() 
{ 
    if (this == null) return "";
    return this.replace(/(^\s*)/g, ""); 
} 
String.prototype.rTrim = function() 
{ 
    if (this == null) return "";
    return this.replace(/(\s*$)/g, ""); 
} 
function checkEmail(email)
{
    var re = new RegExp("^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$", "ig");
    return re.test(email)
}
function checkWebSiteUrl(url)
{
    var re = new RegExp("^http(s)?://([\w-]+\.)*", "ig");
    return re.test(url);
}
function getXmlHttp()
{
	var http_request;
	if(window.XMLHttpRequest) { 
		http_request = new XMLHttpRequest();
//		if (http_request.overrideMimeType) {
//			http_request.overrideMimeType("text/xml");
//		}
	}
	else if (window.ActiveXObject) { 
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) { 
		window.alert("can't create XMLHttpRequest object.");
		return null;
	}	
	return http_request;
}

function getElementPositionX(elemID)
{
   var offsetTrail = document.getElementById(elemID);
   var offsetLeft = 0;
   
   while(offsetTrail)
   {
      offsetLeft += offsetTrail.offsetLeft;
      offsetTrail = offsetTrail.offsetParent;
   }
   
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof(document.body.leftMargin) != "undefined") {
        offsetLeft += document.body.leftMargin;
    }
    
    return offsetLeft;
}
function getElementPositionY(elemID)
{
   var offsetTrail = document.getElementById(elemID);
   var offsetTop = 0;
   
   while(offsetTrail)
   {
      offsetTop += offsetTrail.offsetTop;
      offsetTrail = offsetTrail.offsetParent;
   }
   
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof(document.body.leftMargin) != "undefined") {
        offsetTop += document.body.topMargin;
    }
    return offsetTop;
}
function dataToUrlParam(datas)
{
    var tmpArray = [];
    var tmpName = "";
    var tmpValue = "";
    var returnStr = "";
    for ( var data in datas )
    {
        tmpName = data.trim();
        tmpValue = datas[data].trim();
        if (tmpValue != "")
        {
            tmpArray.push(encodeURIComponent(tmpName) + "=" + encodeURIComponent(tmpValue));
        }
    }
    returnStr = tmpArray.join("&");
    return returnStr;
}
function goPageWidthData(url, datas)
{
    var paramStr = dataToUrlParam(datas).trim();
    var joinSymbol = (url.indexOf("?") > -1) ? "&" : "?";
    if (paramStr == "") joinSymbol = "";
    window.location.href = url + joinSymbol + paramStr;
}
function controlImgSize(tartgetImgObj, width, height)
{
//    var width = tartgetImgObj.width;
//    var height = tartgetImgObj.height;
    if (width == 0) width = tartgetImgObj.style.width;
    if (height == 0) width = tartgetImgObj.style.height;
    var tmpImage = new Image();
    tmpImage.src = tartgetImgObj.src;
    var resultWidth;
    var resultHeight;
//    alert("Source:" + width + "|" + height);
    // Limit the width
    if ((tmpImage.width/tmpImage.height) >= (width/height))
    {
        if (tmpImage.width > width)
        {
            resultWidth = width;
            resultHeight = width*(tmpImage.height/tmpImage.width);
        }
        else
        {
            resultWidth = tmpImage.width
            resultHeight = tmpImage.height
        }
    }
    else
    {
        if (tmpImage.height > height)
        {
            resultWidth = height*(tmpImage.width/tmpImage.height);
            resultHeight = height;
        }
        else
        {
            resultWidth = tmpImage.width
            resultHeight = tmpImage.height
        }
    }
//    alert("Result:" + resultWidth + "|" + resultHeight);
    tartgetImgObj.style.width = resultWidth + "px";
    tartgetImgObj.style.height = resultHeight + "px";
//    tartgetImgObj.width = resultWidth;
//    tartgetImgObj.height = resultHeight;
}
function buildUrlWithData(url, datas)
{
    var paramStr = dataToUrlParam(datas).trim();
    var joinSymbol = (url.indexOf("?") > -1) ? "&" : "?";
    if (paramStr == "") joinSymbol = "";
    return url + joinSymbol + paramStr;
}
function buildNoCacheUrl(strUrl)
{
    var joinSymbol = (strUrl.indexOf("?") > -1) ? "&" : "?";
    var randomTimeTag = (new Date).getTime();
    return strUrl + joinSymbol + "randomTimeTag=" + randomTimeTag;
}

// 图片自适应大小
function adaptImgSize(id,width,height){
    var src = $("#"+id).attr("src");
    $("#"+id).bind("load",function(){
        $(this).css({width:"auto",height:"auto"});
        var w = $(this).width();
        var h = $(this).height();
        if(w<=width && h<=height){
        }else if(w/h>width/height){
            $(this).css({width:width+"px",height:"auto"});
        }else{
            $(this).css({width:"auto",height:height+"px"});
        }
    }).attr("src",src);
}
// 图片自适应大小
function bindAdaptImgSize(obj,width,height){
    var me=$(obj);
    me.css({width:"auto",height:"auto"});
    var w = me.width();
    var h = me.height();
    if(w<=width && h<=height){
    }else if(w/h>width/height){
        me.css({width:width+"px",height:"auto"});
    }else{
        me.css({width:"auto",height:height+"px"});
    }
}
