﻿var $i = function (id) { return "string" == typeof id ? document.getElementById(id) : id;}
var $n=function(name){return "string" == typeof name ? document.getElementsByName(name) : name;}
var UserInfo={
    Question:function(objid,selected){
        this.text=['--请选择问题--','我母亲的生日？','我父亲的生日？','我最喜欢的歌曲？','我最喜欢的歌星？','我最喜欢吃的食物？','我最喜欢的颜色？','我最欣赏的一位名人？','其他'];
        this.value=['0','1','2','3','4','5','6','7','8'];
       SelectCommon.load(objid,this.text,this.value,selected);
    },
    ShouRu:function(viewid,objid,selected){//家庭年收入
        this.text=['59999元以下','60000元--119999元','120000元--199999元','200000元--299999元','300000元以上'];
        this.value=['1','2','3','4','5'];
        RadioCommon.load(viewid,objid,this.text,this.value,selected);
    },
    XingQu:function(viewid,objid,selected){//理财产品感兴趣
        this.text=['股票','基金','保险','债券','信托','期货','贵金属','其他'];
        this.value=['1','2','3','4','5','6','7','8'];
        CheckCommon.load(viewid,objid,this.text,this.value,selected);
    },
    Product:function(viewid,objid,selected){//投资的理财产品
        this.text=['股票','基金','保险','债券','信托','期货','贵金属','其他'];
        this.value=['1','2','3','4','5','6','7','8'];
        CheckCommon.load(viewid,objid,this.text,this.value,selected);
    },
    ChuDao:function(viewid,objid,selected){//理财信息的渠道
                
        this.text=['杂志','电视','广播','互联网','手机短信','朋友介绍','其他'];
        this.value=['1','2','3','4','5','6','7'];
        CheckCommon.load(viewid,objid,this.text,this.value,selected);
    }
}
GetUserinfo=function(userflag){
    var user=new (eval("UserInfo."+userflag.flag));
    var index=null;
    for(var i in user.value){
        if(userflag.info==user.value[i]){
            index=i;
            break;
        }
    }
    if(index==null)return;
    var result=user.text[index];
    document.write(result);
}
var Each = function(list, fun){
    for (var i = 0, len = list.length; i < len; i++) {
		var rs=fun(list[i], i);
		if(rs!==undefined){
			if(rs===true){
				break;
			}else{
				return rs;
			}
		}
	}
};
var SelectCommon={
    load:function(objid,texts,values,selected){
        if(objid){
            var $obj=$("#"+objid);
             $obj.empty();
            for(var i in values){
                if(selected==values[i]){
                    $obj.append("<option value='"+values[i]+"' selected='selected'>"+texts[i]+"</option>");
                }else{
                    $obj.append("<option value='"+values[i]+"'>"+texts[i]+"</option>");
                }
            }
        }
    },
    selectedItem:function(objid,slitem){
        var obj=$("#"+objid).get(0);
        for(var i=0;i<obj.options.length;i++)
        {   
            if(obj.options[i].value==slitem)
            {
                 obj.options[i].selected=true;
                 break;
            }
        }
    },
    getItem:function(id){//得到选择项
		var el=$i(id),index=el.selectedIndex;
		return {"index":index,"text":el.options[index].text,"value":el.value};
	},
    appendItem:function(objid,item,isSelected){
        var t_temp="<option value='"+item.text+"'";
        if(isSelected){
             t_temp+="selected='selected'";
        }
        t_temp+=">"+item.value+"</option>";
        $("#"+objid).append(t_temp);
    }
}
var CheckCommon={
    load:function(viewid,name,text,value,selected){//加载项 selected参数可选 默认选择的
		var rs="";
		Each(value,function(it,i){
			rs+="<input type=\"checkbox\" name=\""+name+"\" value=\""+it+"\" ";
			Each(selected,function(cit,i){
				if(it==cit){
					rs+=" checked=\"checked\" ";
					selected.splice(i,1);
					return true;
				}
			});
			rs+="/> "+text[i]+" ";
		});
		$i(viewid).innerHTML=rs;
	}
}
var RadioCommon={
    load:function(viewid,name,text,value,selected){//加载项 selected参数可选 默认选择的
		var rs="";
		Each(value,function(it,i){
			rs+="<input type=\"radio\" name=\""+name+"\" value=\""+it+"\" ";
			if(selected&&selected==it){
				rs+=" checked=\"checked\" ";
			}
			rs+="/>"+text[i]+" ";
		});
		$i(viewid).innerHTML=rs;
	},
	getValue:function(name){//获得选中的值
		return Each($n(name),function(it,i){
			if(it.checked==true){
				return it.value;
			}
		});
	}
}
String.prototype.checkContent=function(filter){
    var pattern={
        email:"(@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,})",
        num:"[0-9]{beginNum,endNum}",//连续的数字，位数自定义
        content:"",//自定义内容
        mobile:"^[1-9][0-9]{10}$"
    };
   var reg=new RegExp();
    if(filter.mobile)
    {
        reg.compile(pattern.mobile);
        if(!reg.test(this))
        {
            if(filter.mobile.errorTip){
                alert(filter.mobile.errorTip);
            }
            return false;
        }
    }
    if(filter.email)
    {
        reg.compile(pattern.email);
        if(!reg.test(this))
        {
            if(filter.email.errorTip){
                alert(filter.email.errorTip);
            }
            return false;
        }
    }
    if(filter.num)
    {
        pattern.num=pattern.num.replace('beginNum',filter.num.beginNum).replace('endNum',filter.num.endNum);
        reg.compile(pattern.num);
        if(!reg.test(this))
        {
            if(filter.num.errorTip){
                alert(filter.num.errorTip);
            }
            return false;
        }
    }
    if(filter.content)
    {
        for(var i in filter.content.content)
        {
            pattern.content=filter.content.content[i];
            if(typeof(pattern.content)=="string")
            {
                reg.compile(pattern.content);
                if(!reg.test(this))
                {
                    if(filter.content.errorTip){
                        alert(filter.content.errorTip);
                    }
                    return false;
                }
            }
        }
    }
    return true;
}
String.prototype.realLength=function(){    
    return this.replace(/[^\x00-\xff]/g,"**").length;   
}
String.prototype.trim=function(){
    return this.replace(/(^\s*)|(\s*$)/g, ""); 
}
//验证码的验证
codeblur=function(codeid,submitid,msgid){
    if(codeid==""||codeid==null){
        codeid="vcode";
    }
    if(submitid==""||submitid==null){
        submitid="btnOK";
    }
    if(msgid==""||msgid==null){
        msgid="lblmsg";
    }
    if($("#"+codeid).val()!=""){
        $("#"+submitid).attr("disabled","disabled");
        $("#"+msgid).html("&nbsp;<img src=\"/manager/images/spinner.gif\" />&nbsp;正在检查，请稍等...&nbsp;");
        $.ajax({
            type:"post",
            url:"/Handler/CheckObject.ashx",
            data:{action:"checkcode",params:$("#"+codeid).val()},
            dataType:"json",
            success:function(data){
                if(data.success){
                    $("#"+submitid).removeAttr("disabled");
                    $("#"+msgid).html("");
                }else{
                    $("#"+msgid).html(data.info);
                    $("#"+submitid).attr("disabled","disabled");
                }
            },
            error:function(data){
                alert("服务器发生错误！");
                $("#"+submitid).attr("disabled","disabled");
                //alert(data.responseText);
            }
        });
    }else{
        $("#"+msgid).html("");
    }
}
//判断某个元素是否存在
IsExits_element=function(id){
    if($("#"+id).length>0){
        return true;
    }else{
        return false;
    }
}
function drawImg(id,maxWidth,maxHeight){//等比例缩放
	var img=new Image(),el=$i(id);
	img.src=el.src;
	var rs_width=img.width,rs_height=img.height;
	if (img.width > 0 && img.height > 0){
		if (img.width / img.height >= maxWidth / maxHeight){
			if (img.width > maxWidth){
				rs_width = maxWidth;
				rs_height = (img.height * maxWidth) / img.width;
				if (rs_height>maxHeight){
					rs_height = maxHeight;rs_width = (img.width * maxHeight) / img.height;
				}
			}
		}else{
			if (img.height > maxHeight){
				rs_height = maxHeight;
				rs_width = (img.width * maxHeight) / img.height;
				if (rs_width > maxWidth){
					rs_width = maxWidth;rs_height = (img.height * maxWidth) / img.width;
				}
			}
		}
	}
	el.style.width=rs_width+"px";
	el.style.height=rs_height+"px";
}
