/*common 绑定事件*/
$(document).ready(
    function(){
        //simple select 选择事件
        $('span[must=simple]> select').change(
            function(){
                if ($(":selected",this).attr('value')==0)
                {
                    $(this).parent().parent().addClass('fm-error');
                }else{
                    $(this).parent().parent().removeClass('fm-error');
                }
            }
            );
        $('input[max]').keyup(
            /*function(){
                if (!$(this).attr('value')&&$(this).attr('must')) {
                    $(this).parent().addClass('fm-error');
                }else if($(this).attr('value')){
                    $(this).parent().removeClass('fm-error');
                }
								var name = $(this).attr('name');
								must.fm_text(name);
            }*/
            function () {
                var name = $(this).attr('name');
                must.fm_text(name);
            }
            );
        $('input[must=number]').blur(
            function(){
                var name = $(this).attr('name');
                must.fm_num(name);
            }
            );
        $('textarea[max]').keyup(
            function(){
                var name = $(this).attr('name');
                must.fm_textarea(name);
            }
            );
        //鼠标移动变色
        $(".fm-div").hover(function(){
            $(this).addClass("fm-hover");
        },function(){
            $(this).removeClass("fm-hover");
        });
        //input focus
        $(".fm-div .text").focus(function(){
            $(this).parent().addClass('fm-focus');
        });
        $(".fm-div .textarea").focus(function(){
            $(this).parent().addClass('fm-focus');
        });
        //input blur
        $(".fm-div .text").blur(function(){
            $(this).parent().removeClass('fm-focus');
        });
        $(".fm-div .textarea").blur(function(){
            $(this).parent().removeClass('fm-focus');
        });
        // label.radio checked
	
        $(".fm-radio").click(function(){
            $(".fm-radio").removeClass("checked");$('.fm-radio > :checked').parent().addClass('checked');
        });
    });
/**
 *  fm_raido(id)
 *  fm_checkbox(name)
 *  fm_simple(name|name|name)
 *  fm_textarea(name)
 *  fm_text(name)
 *  fm_time()
 *  fm_dselect( id )
 *  fm_num(name)
 */
function must_object(){
    this.fm_radio = function(id){
        this.span = $(id);
        this.radio = $('.checked',this.span);
        if (this.radio.length == 0)
        {
            this.span.parent().addClass('fm-error');

        }else{
            this.span.parent().removeClass('fm-error');
         
        }
      
    }
    this.fm_checkbox = function(name){
        var checkboxs = $("input[name='"+name+"']:checked");
			
        if (!checkboxs.length)
        {
				
            $("input[name="+name+"]").parent().parent().parent().addClass('fm-error');
        }else{
            checkboxs.parent().parent().parent().removeClass('fm-error');
        }
    }
    this.fm_simple = function(names){
        this.name =  names.split('|');
        this.lengther = this.name.length;
        for (i = 0; i < this.lengther; i++) {
            var s_simple = $("select[name='"+this.name[i]+"'] > :selected");
            if (s_simple.attr('value') == 0)
            {
                s_simple.parent().parent().parent().addClass('fm-error');
            }else{
                s_simple.parent().parent().parent().removeClass('fm-error');
            }
        }
		
    }
    this.fm_textarea = function(name){
        this.name = name;
  
        this.txtarea = $('textarea[name='+name+']');
        this.notice = $('#'+name+'-notice');
        this.max = this.txtarea.attr('max');
				this.must = this.txtarea.attr('must');
        if (!this.txtarea.attr('value')) {
            this.notice.html('您还没有填写内容呢') ;
            return false;
        }else{
            this.lengther = this.txtarea.attr('value').length;
            if (this.lengther>=this.max) {
                this.notice.html('允许字数为<span class="bold">'+this.max+'</span>,您填写的字数超过了<span class="bold">'+(this.lengther-this.max)+'</span>个字');
                this.notice.parent().addClass('fm-error');
            }else{
                this.notice.html('您已经填写了<span class="bold">'+this.lengther+'</span>个字,当前允许字数为<span class="bold">'+this.max+'</span>');
                this.notice.parent().removeClass('fm-error');
            }
        }
   
    }
    this.fm_text = function(name){
        this.name = name;
        this.text = $('input[name='+name+']');
        this.notice = $('#'+name+'-notice');
        this.max = this.text.attr('max');
				this.must = this.text.attr('must');
        if (!this.text.attr('value')) {
            if (this.must){
              this.notice.html('您还没有填写内容呢') ;
              this.notice.parent().addClass('fm-error');
            }
            return false;
        }else{
            this.lengther = this.text.attr('value').length;
            if (this.lengther>=this.max) {
                this.notice.html('允许字数为<span class="bold">'+this.max+'</span>,您填写的字数超过了<span class="bold">'+(this.lengther-this.max)+'</span>个字');
                this.notice.parent().addClass('fm-error');
            }else{
                this.notice.html('您已经填写了<span class="bold">'+this.lengther+'</span>个字,允许字数为<span class="bold">'+this.max+'</span>');
                this.notice.parent().removeClass('fm-error');
            }
        }
    }
    this.fm_time = function(){
        var sYear = Number($('select[name=syear] > :selected').attr('value')) ,
        sMonth = Number($('select[name=smonth] > :selected').attr('value')) ,
        eYear = Number($('select[name=eyear] > :selected').attr('value')) ,
        eMonth = Number($('select[name=emonth] > :selected').attr('value')) ;
        if (sYear < 1900 || sMonth < 1 || sMonth > 12){
            $('select[name=eyear]').parent().parent().addClass('fm-error');
            $('#time-notice').html('开始的年份必填.');
            return false;
        }
        if (eMonth > 0 && eMonth <12 || eYear > 1900) {
            if (eYear < 1900 || eMonth< 1 || eMonth > 12){
                $('select[name=eyear]').parent().parent().addClass('fm-error');
                $('#time-notice').html('至今请留空,否则结束年份和月份必须同时选择.');
                return false;
            }else {
                var stime = sYear*100+sMonth ;
                var etime = eYear*100+eMonth ;
                if (etime<=stime){
                    $('select[name=eyear]').parent().parent().addClass('fm-error');
                    $('#time-notice').html('开始年份不能大于或等于结束的时间');
                    return false;
                }
            }
        }
        $('select[name=eyear]').parent().parent().removeClass('fm-error');
        $('#time-notice').html('正确');
    }
		this.fm_timeall = function(){
		var sYear = Number($('select[name=syear] > :selected').attr('value')) ,
        sMonth = Number($('select[name=smonth] > :selected').attr('value')) ,
        eYear = Number($('select[name=eyear] > :selected').attr('value')) ,
        eMonth = Number($('select[name=emonth] > :selected').attr('value')) ;
        if (sYear < 1900 || sMonth < 1 || sMonth > 12){
            $('select[name=eyear]').parent().parent().addClass('fm-error');
            $('#time-notice').html('所有年份必填.');
            return false;
        }
				if (eYear < 1900 || eMonth < 1 || eMonth > 12)
				{
						$('select[name=eyear]').parent().parent().addClass('fm-error');
            $('#time-notice').html('所有年份必填.');
            return false;
				}
        if (eMonth > 0 && eMonth <12 || eYear > 1900) {
            if (eYear < 1900 || eMonth< 1 || eMonth > 12){
                $('select[name=eyear]').parent().parent().addClass('fm-error');
                $('#time-notice').html('结束年月必选.');
                return false;
            }else {
                var stime = sYear*100+sMonth ;
                var etime = eYear*100+eMonth ;
                if (etime<=stime){
                    $('select[name=eyear]').parent().parent().addClass('fm-error');
                    $('#time-notice').html('开始年份不能大于或等于结束时间');
                    return false;
                }
            }
        }
        $('select[name=eyear]').parent().parent().removeClass('fm-error');
        $('#time-notice').html('正确');
    }
    this.fm_dselect = function( id ) {
        this.span = $( id );
        this.select = $('select:eq(1)',this.span);
        this.value = Number(this.select.attr('value'));
        if (this.value < 1)
        {
            this.span.parent().addClass('fm-error');
        }else{
         
            this.span.parent().removeClass('fm-error');
        }
    }
    this.fm_num = function( name ){
        this.input  = $('input[name='+name+']');
        this.notice = $('#'+name+'-notice');
        this.num = CtoH(this.input.attr('value'));
        this.num = Math.floor(this.num);
        if (isNaN(this.num)||this.num ==0){
            this.input.parent().addClass('fm-error');
            this.notice.html('请填写正确有效的数字。');
            return false;
        }else if(this.num<0){
            this.num = 0-this.num;
        }
        this.input.parent().removeClass('fm-error');
        this.input.attr('value',this.num);
        this.notice.html('必填项,请填写有效数字。');
    }
		this.fm_mobile = function ( name ){
				this.input  = $('input[name='+name+']');
				this.notice = $('#'+name+'-notice');
				this.mobile = CtoH( this.input.attr('value') );
				this.pattern = /^((13|15|18)\d{9})?$/;
				if (isNaN ( this.mobile ) || ! this.mobile.match( this.pattern ))
				{
					this.input.parent().addClass('fm-error');
					return false;
				}
				this.input.parent().removeClass('fm-error');
        this.input.attr('value',this.mobile);
		}
		this.fm_phone = function ( name ){
				this.input  = $('input[name='+name+'1]');
				this.phone1 = CtoH( $('input[name='+name+'1]').attr('value') );
				this.phone2 = CtoH( $('input[name='+name+'2]').attr('value') ); 
				this.phone3 = $('input[name='+name+'3]').attr('value') ? CtoH( $('input[name='+name+'3]').attr('value') ): '';
				this.notice = $('#'+name+'-notice');
				this.phone  = this.phone1 + '-' + this.phone2 + ( this.phone3? '-' + this.phone3 : '' );
				this.pattern = /^(0[0-9]{2,3}\-)?([2-9][0-9]{6,7})+(\-[0-9]{1,6})?$/;
			if ( ! this.phone.match(this.pattern) )
				{
					this.input.parent().addClass('fm-error');
					return false;
				}
				this.input.parent().removeClass('fm-error');
				this.input.attr('value',this.phone1);
				$('input[name='+name+'2]').attr('value',this.phone2);
				$('input[name='+name+'3]').attr('value',this.phone3);
		}
}

function add(id){
    $(id).show();
    $('div[class=option]').hide();
}
function exit(id){
    $(id).hide();
    $('#error-notice').hide();
    $('div .option').show();
    var error = $('.fm-error');
    error.removeClass('fm-error');
    $(id+'> form')[0].reset();
}
function rsmdelete(domains,extent,opid){
    var opUrl = domains+"index.php";
    var opAction = "action=user&option=rsmEdit&extent="+extent+"&opera=delete&ajax=on&info="+opid;
    var tr = $('tr[id='+extent+opid+']');
    var optd = $('.option',tr);
    optd.html('删除中...');
    $.ajax({
        type: "POST",
        url: opUrl,
        data:opAction,
        success : function(msg){
				msg = trim(msg);
            if (msg == 'ok') {
                tr.hide();
            }else if(msg == 'empty'){
                window.location.reload();
            }
        }
    })
}
//全角和半角的转化
function CtoH(str){
    var result='';
    if (!str) {
        return false;
    }
    for (var i = 0; i < str.length; i++)
    {
        if (str.charCodeAt(i)==12288)
        {
            result+= String.fromCharCode(str.charCodeAt(i)-12256);
            continue;
        }
        if (str.charCodeAt(i)>65280 && str.charCodeAt(i)<65375)
        {
            result+= String.fromCharCode(str.charCodeAt(i)-65248);
        }else{
            result+= String.fromCharCode(str.charCodeAt(i));
        }
    }
    return result;
}


function high(form){
		var high_box = $('#high-search');
		var high_label =high_box.parent();
		var input = $('input[name=searchType]');
		if (high_label.hasClass('checked'))
		{
			$('#high-option').addClass('hide');
			input.attr('value','hide');

		}else{
			$('#high-option').removeClass('hide');
			input.attr('value','show');
		}
		if (form)
		{		
			high_label.removeClass('checked');
			high_box.attr('checked','');
			$('form[name='+form+']').submit();
		}
}



function setSelectEmpty(name,fm_name){
	var obj = $('select[name='+name+'] > :eq(0)');
	var fm = $('form[name='+fm_name+']');
	obj.attr('selected','selected');
	fm.submit();
}

//in_array add by iwege
Array.prototype.in_array = function(e){
	for(i=0;i<this.length && this[i]!=e;i++);
	return !(i==this.length);
}

var must = new must_object();