﻿function $(obj)
{
    return document.getElementById(obj);
}

/*------------------------------------------------------------------------*/
//验证邮箱格式
function isEmail(strEmail)
{
    if(strEmail=="")
        return true;
    if (strEmail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

/*------------------------------------------------------------------------*/
//验证是否是日期类型
function isDate(str){ 
var reg = /^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$/
if (reg.test(str)){return true;}else{return false;}
}

/*------------------------------------------------------------------------*/
//验证是否为电话号码
function isPhone( strPhone ) { 
var phoneRegWithArea = /^[0][1-9]{2,3}-[0-9]{5,8}$/; 
var phoneRegNoArea = /^[1-9]{1}[0-9]{5,8}$/; 
if( strPhone.length > 9 ) {
    if( phoneRegWithArea.test(strPhone) ){return true; }else{return false;}
}else{
    if(phoneRegNoArea.test(strPhone)){return true; }else{return false;}
}
}

/*------------------------------------------------------------------------*/
//验证是否为数字
function isNumber( s ){ 
var regu = "^[0-9]+$"; 
var re = new RegExp(regu); 
if (s.search(re) != -1) { return true;} else { return false;} 
} 

/*------------------------------------------------------------------------*/
//验证是否为手机
function isMobile( s ){ 
var regu =/^[1][3,5][0-9]{9}$/; 
var re = new RegExp(regu); 
if (re.test(s)) { 
return true; 
}else{ 
return false; 
} 
} 

/*------------------------------------------------------------------------*/
//验证URL
function isURL(str_url)
{
    var strRegex = "^((https|http|ftp|rtsp|mms)?://)"
          + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@ 
          + "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184 
          + "|" // 允许IP和DOMAIN（域名）
          + "([0-9a-z_!~*'()-]+\.)*" // 域名- www. 
          + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名 
          + "[a-z]{2,6})" // first level domain- .com or .museum 
          + "(:[0-9]{1,4})?" // 端口- :80 
          + "((/?)|" // a slash isn't required if there is no file name 
          + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; 
          var re=new RegExp(strRegex); 
    //re.test()
          if (re.test(str_url)){
              return (true); 
          }else{ 
              return (false); 
     }
}
      
/*------------------------------------------------------------------------*/
//关闭窗口
function CloseWindow()
{
    window.close();
    window.opener=null;
}

/*------------------------------------------------------------------------*/
//获取cookie
function RequestCookies(cookieName, dfltValue)
{
    var lowerCookieName = cookieName.toLowerCase();
    var cookieStr = document.cookie;
    
    if (cookieStr == "")
    {
        return dfltValue;
    }
    
    var cookieArr = cookieStr.split("; ");
    var pos = -1;
    for (var i=0; i<cookieArr.length; i++)
    {
        pos = cookieArr[i].indexOf("=");
        if (pos > 0)
        {
            if (cookieArr[i].substring(0, pos).toLowerCase() == lowerCookieName)
            {
                return unescape(cookieArr[i].substring(pos+1, cookieArr[i].length));
            }
        }
    }
    
    return dfltValue;
}

/*---------------------------------------------------------------------------*/
//验证DropDownList选择是否是选择第一项
function CheckDropList(obj,msg)
{
    if(obj.options[obj.selectedIndex].value=="0")
    {
        alert(msg);
        obj.focus();
        return false;
    }
}

/*--------------------------------------------------------------------------*/
//等比例设置图片大小
function setPhoto(img,width,height){
 if(img != null){
  var imgWidth = img.width;
  var imgHeight = img.height; 
  if(imgWidth>width&&imgHeight>height){
   //both image's width and height are larger than required one
   var widthRate = imgWidth/width;
   var heightRate = imgHeight/height;
   if(widthRate>heightRate){
    //use width to retrieve the image
    img.width=width;
    img.height=imgHeight*(width/imgWidth);
   }else{
    img.height=height;
    img.width=imgWidth*(height/imgHeight);
   }   
  }
  else if(imgWidth>width){
   //the image width lg than the required width
   img.width=width;
   img.height=imgHeight*(width/imgWidth);
  }
  else if(imgHeight>height){
   //the image height lg than the required
   img.height=height;
   img.width=imgWidth*(height/imgHeight); 
  }  
 }
} 
